fastoad.base.dict module

class fastoad.base.dict.AddKeyAttribute(attr_name, default_value=None, doc=None)[source]

Bases: object

A decorator for a dict class that adds a property for accessing the matching dict item.

The getter and the setter of the property are defined. Setting None or np.nan when setting the property will delete the dict key, so that next calls to the getter will return default_value.

Calling AddKeyAttribute for an already defined key will redefine the default value.

The “attribute_keys” property is created in decorated class for returning the list of attributes that have been defined by AddKeyAttribute or by AddKeyAttributes.

Parameters
  • attr_name – the dict key that will be paired to a property

  • default_value – the default value that will be returned if dict has not the attr_name as key

class fastoad.base.dict.AddKeyAttributes(attribute_definition: Union[dict, Iterable[str]])[source]

Bases: object

A decorator for a dict class that adds properties for accessing the matching dict item.

This class simply does several call of AddKeyAttribute.

Parameters

attribute_definition – the list of keys that will be attributes. If it is a dictionary, the values are the associated default values. If it is a list or a set, default values will be None.

class fastoad.base.dict.DynamicAttributeDict(*args, **kwargs)[source]

Bases: dict

A dictionary class where keys can also be used as attributes.

The keys that can be used as attributes are defined using decorators AddKeyAttribute or SetKeyAttributes.

They can also be used as keyword arguments when instantiating this class.

Note

Using this class as a dict is useful when instantiating another dict or a pandas DataFrame, or instantiating from them. Direct interaction with DynamicAttributeDict instance should be done through attributes.

Example:

>>> @AddKeyAttributes({"foo": 0.0, "bar": None, "baz": 42.0})
... class MyDict(DynamicAttributeDict):
...     pass
...

>>> d = MyDict(foo=5, bar="aa")
>>> d.foo
5
>>> d.bar
'aa'
>>> d.baz  # returns the default value
42.0
>>> d["foo"] = 10.0  # can still be used as a dict
>>> d.foo  # but change are propagated to/from the matching attribute
10.0
>>> d.foo = np.nan  # setting None or numpy.nan returns to default value
>>> d["foo"]
0.0
>>> d.foo # But the attribute will now return the default value
0.0
>>> d.bar = None  # If default value is None, setting None or numpy.nan deletes the key.
>>> # d["bar"]  #would trigger a key error
>>> d.bar # But the attribute will return None
Parameters
  • args – a dict-like object where all keys are contained in attribute_keys

  • kwargs – argument keywords must be names contained in attribute_keys