hydrac.util.paramcontainer¶
Container for parameters (hydrac.util.paramcontainer)¶
Provides:
-
class
hydrac.util.paramcontainer.ParamContainer(tag=None, attribs=None, path_file=None, elemxml=None, hdf5_object=None, doc='', parent=None)[source]¶ Bases:
objectStructured container of values.
This class has been developped within the fluiddyn package (https://fluiddyn.readthedocs.io/en/latest/index.html).
The objects ParamContainer can be used as containers of parameters. They can be printed as xml text.
They are used to contain parameters that can be modified by the user, for example in this way:
>>> params = ParamContainer(tag='params') >>> params._set_attribs({'a0': 1, 'a1': 1}) >>> params._set_attrib('a2', 1) >>> params._print_as_xml() <params a1="1" a0="1" a2="1"/> >>> params._set_child('child0', {'a0': 2, 'a1': 2}) >>> params.child0.a0 = 3 >>> params._print_as_xml() <params a1="1" a0="1" a2="1"> <child0 a1="2" a0="3"/> </params>
Here,
params.child0is another ParamContainer.An interesting feature of the ParamContainer objects is that if one uses a non-existing parameter, an AttributeError is raised:
>>> params.a3 = 3 ------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-9-a91118d8b23e> in <module>() ----> 1 params.child1.a0 = 3 AttributeError: a3 is not already set in params. The attributes are: set(['a1', 'a0', 'a2']) To set a new attribute, use _set_attrib or _set_attribs.
Note that for a parameter container, it is much better to raise an error rather than just add an unused parameter!
A ParamContainer object can be saved in xml and in hdf5 and then reloaded from the file:
>>> params._save_as_xml() >>> params_loaded = ParamContainer(path_file=params._tag + '.xml') >>> assert params_loaded == params
Parameters: - (for the __init__ method)
- tag : (None) str
A tag for the root container.
- attribs : (None) dict
Some attributes.
- path_file : (None) str
Path of a file (xml or hdf5).
- elemxml : (None) xml.etree.ElementTree.Element
An xml element.
- hdf5_object : (None) file
An open hdf5 file.
Functions
convert_capword_to_lowercaseunderscore(name) |
|
tidy_container(cont) |
Modify the names in a ParamContainer and its organization. |
Classes
ParamContainer([tag, attribs, path_file, …]) |
Structured container of values. |