fluiddyn.util.paramcontainerο
Container for parameters (fluiddyn.util.paramcontainer
)ο
Provides:
- class fluiddyn.util.paramcontainer.ParamContainer(tag=None, attribs=None, path_file=None, elemxml=None, hdf5_object=None, doc='', parent=None)[source]ο
Bases:
object
Structured container of values.
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.child0
is 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.
- _set_child(tag, attribs=None, doc=None)[source]ο
Add a child (of the same class) to the container.
- _save_as_xml(path_file=None, comment=None, find_new_name=False)[source]ο
Save the xml text in a file.
Functions
|
|
|
JSON rendering requires values in the dictionary to be a basic type. |
|
Modify the names in a ParamContainer and its organization. |
Classes
|
Structured container of values. |