Skip to content
Snippets Groups Projects
Commit d79bea2a authored by pswain's avatar pswain
Browse files

some annotation

parent eba6fa82
No related branches found
No related tags found
No related merge requests found
...@@ -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 add yaml functionality to parameters Defines parameters as attributes and 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 saved there as well. If path is provided, 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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment