class DefaultSettings(object):
Known subclasses: cma.fitness_models.LQModelSettings
, cma.fitness_models.ModelInjectionCallbackSettings
, cma.fitness_models.SurrogatePopulationSettings
Constructor: DefaultSettings(params, number_of_params, obj)
resembling somewhat types.SimpleNamespace
from Python >=3.3
but with instantiation and resembling even more the dataclass
decorator
from Python >=3.7.
MyClassSettings(DefaultSettings) is preferably used by assigning a settings attribute in __init__ like:
>>> class MyClass: ... def __init__(self, a, b=None, param1=None, c=3): ... self.settings = MyClassSettings(locals(), 1, self)
The 1
signals, purely for consistency checking, that one parameter defined
in MyClassSettings is to be set from locals(). MyClassSettings
doesn't use any names which are already defined in self.__dict__. The
settings are defined in a derived parameter class like
>>> from cma.fitness_models import DefaultSettings >>> class MyClassSettings(DefaultSettings): ... param1 = 123 ... val2 = False ... another_par = None # we need to assign at least None always
The main purpose is, with the least effort, (i) to separate parameters/settings of a class from its remaining attributes, and (ii) to be flexible as to which of these parameters are arguments to __init__. Parameters can always be modified after instantiation. Further advantages are (a) no typing of self. to assign the default value or the passed parameter value (the latter are assigned "automatically") and (b) no confusing name change between the passed option and attribute name is possible.
The class does not allow to overwrite the default value with None
.
Now any of these parameters can be used or re-assigned like
>>> c = MyClass(0.1) >>> c.settings.param1 == 123 True >>> c = MyClass(2, param1=False) >>> c.settings.param1 is False True
Method | __init__ |
Overwrite default settings in case. |
Method | __str__ |
Undocumented |
Instance Variable | inparams |
Undocumented |
Instance Variable | obj |
Undocumented |
Method | _set |
defaults are taken from the class attributes |
Method | _set |
Only existing parameters/attributes and non-None values are set. |
Instance Variable | _number |
Undocumented |
Overwrite default settings in case.
Parameters | |
params | A dictionary (usually locals()) containing the parameters to set/overwrite |
number | Number of parameters to set/overwrite |
obj | elements of obj.__dict__ are in the ignore list. |