wulfric.add_sugar#

wulfric.add_sugar(dictionary: dict) _SyntacticSugar[source]#

Takes any dictionary and add attribute-like access to the key-values to it.

Parameters:
dictionarydict

Dictionary for the addition of the syntax sugar.

Returns:
candy_SyntacticSugar

Same dictionary with the easy access to the key-value pairs.

Raises:
ValueError

If not isinstance(dictionary, dict).

Examples

>>> import wulfric as wulf
>>> atoms = {"names" : ["Cr1", "Cr2"]}
>>> atoms.names
Traceback (most recent call last):
...
AttributeError: 'dict' object has no attribute 'names'
>>> atoms = wulf.add_sugar(atoms)
>>> atoms.names
['Cr1', 'Cr2']
>>> atoms.positions = [[0, 0, 0], [0.5, 0.5, 0.5]]
>>> atoms.positions
[[0, 0, 0], [0.5, 0.5, 0.5]]
>>> # Note that it still behaves as a dictionary
>>> atoms["positions"] = [[0.5, 0.5, 0.5], [0, 0, 0]]
>>> atoms.positions
[[0.5, 0.5, 0.5], [0, 0, 0]]
>>> atoms["positions"]
[[0.5, 0.5, 0.5], [0, 0, 0]]
>>> atoms
{'names': ['Cr1', 'Cr2'], 'positions': [[0.5, 0.5, 0.5], [0, 0, 0]]}