Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
alibylite
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Swain Lab
aliby
alibylite
Commits
d79bea2a
Commit
d79bea2a
authored
2 years ago
by
pswain
Browse files
Options
Downloads
Patches
Plain Diff
some annotation
parent
eba6fa82
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
abc.py
+38
-20
38 additions, 20 deletions
abc.py
with
38 additions
and
20 deletions
abc.py
+
38
−
20
View file @
d79bea2a
...
@@ -3,23 +3,30 @@ from collections.abc import Iterable
...
@@ -3,23 +3,30 @@ from collections.abc import Iterable
from
pathlib
import
Path
,
PosixPath
from
pathlib
import
Path
,
PosixPath
from
typing
import
Union
from
typing
import
Union
from
copy
import
copy
from
copy
import
copy
from
yaml
import
dump
,
safe_load
from
yaml
import
dump
,
safe_load
class
ParametersABC
(
ABC
):
class
ParametersABC
(
ABC
):
"""
"""
Base class to
a
d
d
yaml functionality to
parameters
Defines parameters as attributes
a
n
d
allows
parameters
to
be converted to either a dictionary or to yaml.
"""
"""
def
__init__
(
self
,
**
kwargs
):
def
__init__
(
self
,
**
kwargs
):
'''
Defines parameters as attributes
'''
for
k
,
v
in
kwargs
.
items
():
for
k
,
v
in
kwargs
.
items
():
setattr
(
self
,
k
,
v
)
setattr
(
self
,
k
,
v
)
###
def
to_dict
(
self
,
iterable
=
"
null
"
):
def
to_dict
(
self
,
iterable
=
"
null
"
):
"""
"""
Recursive function that converts class to nested dictionary.
Recursive function that converts class to nested dictionary
Recursive function to return a dictionary version of the attributes
of the class instance.
"""
"""
if
isinstance
(
iterable
,
dict
):
if
isinstance
(
iterable
,
dict
):
if
any
(
if
any
(
...
@@ -35,35 +42,40 @@ class ParametersABC(ABC):
...
@@ -35,35 +42,40 @@ class ParametersABC(ABC):
}
}
return
iterable
return
iterable
elif
iterable
==
"
null
"
:
elif
iterable
==
"
null
"
:
# use instance's built-in __dict__ dictionary of attributes
return
self
.
to_dict
(
self
.
__dict__
)
return
self
.
to_dict
(
self
.
__dict__
)
else
:
else
:
return
iterable
return
iterable
@classmethod
def
from_dict
(
cls
,
d
:
dict
):
return
cls
(
**
d
)
def
to_yaml
(
self
,
path
:
Union
[
PosixPath
,
str
]
=
None
):
def
to_yaml
(
self
,
path
:
Union
[
PosixPath
,
str
]
=
None
):
"""
Return instance as yaml stream and optionally export to file.
"""
Return instance as a yaml stream and optionally exports to file.
Returns the yaml version of the
class instance. If path is provided, it
Returns the yaml version of the
attributes of the class instance.
is sav
ed the
re as well
.
If path is provid
ed
,
the
yaml version is saved there
.
Parameters
Parameters
----------
----------
path : Union[PosixPath, str]
path : Union[PosixPath, str]
Output path.
Output path.
"""
"""
if
path
:
if
path
:
with
open
(
Path
(
path
),
"
w
"
)
as
f
:
with
open
(
Path
(
path
),
"
w
"
)
as
f
:
dump
(
self
.
to_dict
(),
f
)
dump
(
self
.
to_dict
(),
f
)
return
dump
(
self
.
to_dict
())
return
dump
(
self
.
to_dict
())
###
@classmethod
def
from_dict
(
cls
,
d
:
dict
):
return
cls
(
**
d
)
@classmethod
@classmethod
def
from_yaml
(
cls
,
source
:
Union
[
PosixPath
,
str
]):
def
from_yaml
(
cls
,
source
:
Union
[
PosixPath
,
str
]):
"""
Returns class from a yaml filename or stdin
"""
"""
Returns class from a yaml filename or stdin
"""
is_buffer
=
True
is_buffer
=
True
try
:
try
:
if
Path
(
source
).
exists
():
if
Path
(
source
).
exists
():
...
@@ -75,26 +87,32 @@ class ParametersABC(ABC):
...
@@ -75,26 +87,32 @@ class ParametersABC(ABC):
else
:
else
:
with
open
(
source
)
as
f
:
with
open
(
source
)
as
f
:
params
=
safe_load
(
f
)
params
=
safe_load
(
f
)
return
cls
(
**
params
)
return
cls
(
**
params
)
@classmethod
@classmethod
def
default
(
cls
,
**
kwargs
):
def
default
(
cls
,
**
kwargs
):
overriden_defaults
=
copy
(
cls
.
_defaults
)
overriden_defaults
=
copy
(
cls
.
_defaults
)
for
k
,
v
in
kwargs
.
items
():
for
k
,
v
in
kwargs
.
items
():
overriden_defaults
[
k
]
=
v
overriden_defaults
[
k
]
=
v
return
cls
.
from_dict
(
overriden_defaults
)
return
cls
.
from_dict
(
overriden_defaults
)
###
class
ProcessABC
(
ABC
):
class
ProcessABC
(
ABC
):
"""
Base class for processes
"""
"""
Base class for processes.
Defines parameters as instances and requires run method to be defined.
"""
def
__init__
(
self
,
parameters
):
def
__init__
(
self
,
parameters
):
"""
Arguments
---------
parameters: instance of ParametersABC
"""
self
.
_parameters
=
parameters
self
.
_parameters
=
parameters
# convert parameters to dictionary
for
k
,
v
in
parameters
.
to_dict
().
items
():
# access parameters directly
# and then define each parameter as an attribute
for
k
,
v
in
parameters
.
to_dict
().
items
():
setattr
(
self
,
k
,
v
)
setattr
(
self
,
k
,
v
)
@property
@property
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment