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_attrib(key, value)[source]

Add an attribute to the container.

_set_attribs(d)[source]

Add the attributes to the container.

_set_child(tag, attribs=None)[source]

Add a child (of the same class) to the container.

_set_as_child(child, change_parent=True)[source]

Associate a ParamContainer as a child.

_make_dict_tree()[source]

A tree-like nested dictionary, including attributes and children.

_make_xml_text()[source]

Produce and return a xml text representing the container.

_print_as_xml()[source]

Print the xml text representing the container.

_save_as_xml(path_file=None, comment=None, find_new_name=False)[source]

Save the xml text in a file.

_save_as_hdf5(path_file=None, hdf5_object=None, hdf5_parent=None, invalid_netcdf=False)[source]

Save in a hdf5 file.

_modif_from_other_params(params)[source]

Modify the object with another similar container.

Functions

convert_capword_to_lowercaseunderscore(name)

sanitize_for_json(d)

JSON rendering requires values in the dictionary to be a basic type.

tidy_container(cont)

Modify the names in a ParamContainer and its organization.

Classes

ParamContainer([tag, attribs, path_file, …])

Structured container of values.