EasyScience Base Classes #168
Locked
damskii9992
announced in
ADRs
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
General
Base classes in EasyScience are developed to provide functionality which is shared among objects, such as serialization/de-serialization to dictionaries and
unique_namesids.Some functionality is shared among all (non-special) objects, this functionality is implemented in the
NewBaseclass which is the root base class of EasyScience.Other functionality is only shared among certain technique-library classes, one such example are all
Model-type classes which handleParameterattributes. The functionality to handleParameterattributes is implemented in theModelBaseclass which itself inherits from theNewBaseclass.NewBase
This class will be renamed in the future, when we have properly switched to the new classes.
NewBaseis the root base class of EasyScience and thus inherits from no other classes. It adds serialization/de-serialization to dictionary functionality to classes using theSerializerBaseclasss' static methods. Specifically it adds 3 methods:Which is needed for the
SerializerBaseclass to serialize theNewBaseclass. TheSerializerBase(which should be renamed), is a class with static methods which holds logic for generic serialization/deserialization. In EasyScience, serialization is handed by theto_dictmethod:Which serializes the class to a dictionary. For serialization to JSON or other formats, this dictionary can then be re-serialized. Deserialization is handled by the
from_dictmethod, which by default uses the generic deserialization of theSerializerBaseclass. Inherited classes should then overwrite this method for custom deserialization.Additionally, the
NewBaseclass implements theunique_nameattribute and the logic to theglobal_objectmap (see #29), as well as the optionaldisplay_nameattribute.Finally the class implements the dunder methods:
__dir____copy____deepcopy____repr__ModelBase
This class inherits from
NewBaseand adds functionality forVariable(ParametersandDescriptors) attributes which are modifiable for the user but not replaceable. This is done by implementing the setter method to simply overwrite the value of theVariablewhereas the getter method return theVariableinstance, allowing users direct access to the Variables methods and attributes, likefixedormin, i.e.:When Variable attributes are defined as such,
ModelBaseprovides functionality for correct de-serialization by overwriting thefrom_dictmethod to set the underlying_my_paramattributes, since the class does not (and should not) takeVariableobjects as inputs in its constructor.Additionally
ModelBaseprovides 4 convenience methods to get all its Variables.get_all_variables, which recursively finds allVariableattributes in the class:And also
get_all_parameterswhich filters out allDescriptorsreturning onlyParameterattributes,get_fittable_parameterswhich filters out dependentParametersto only provide fittable Parameters andget_free_parameterswhich additionally filters out all fixedParameters.SerializerBase
The
SerializerBaseclass is the current class responsible for serialization/de-serialization of objects. This class has adeserialize_dictstatic method, which takes a dictionary and de-serializes it, using classes ownfrom_dictmethod if they're EasyScience classes, and the generic de-serializer otherwise.This method allows classes themselves to define a
from_dictmethod to determine how they are de-serialized, instead of just assuming that the__init__constructor works with a serialized dictionary, which it won´t forParameterattributes for example.Link to the ADR suggestions:
#46
#110
#161
Beta Was this translation helpful? Give feedback.
All reactions