calliope.attrdict.AttrDict(source_dict=None)
¶
Bases: dict
A subclass of dict
with key access by attributes::
d = AttrDict({'a': 1, 'b': 2})
d.a == 1 # True
Includes a range of additional methods to read and write to YAML, and to deal with nested keys.
Source code in src/calliope/attrdict.py
as_dict(flat=False)
¶
Return the AttrDict as a pure dict (with nested dicts if necessary).
as_dict_flat()
¶
as_dict_nested()
¶
Source code in src/calliope/attrdict.py
copy()
¶
del_key(key)
¶
Delete the given key. Properly deals with nested keys.
Source code in src/calliope/attrdict.py
from_yaml(filename, resolve_imports=True)
classmethod
¶
Returns an AttrDict initialized from the given path or
file object f
, which must point to a YAML file. The path can
be a string or a pathlib.Path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename |
str | Path
|
|
required |
resolve_imports |
bool | str
|
If |
True
|
Returns: calliope.AttrDict:
Source code in src/calliope/attrdict.py
from_yaml_string(string, resolve_imports=True)
classmethod
¶
Returns an AttrDict initialized from the given string.
Input string must be valid YAML.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string |
str
|
Valid YAML string. |
required |
resolve_imports |
bool | str
|
If |
True
|
Returns: calliope.AttrDict:
Source code in src/calliope/attrdict.py
get_key(key, default=_MISSING)
¶
Looks up the given key
. Like set_key(), deals with nested
keys.
If default is anything but _MISSING
, the given default is
returned if the key does not exist.
Source code in src/calliope/attrdict.py
init_from_dict(d)
¶
Initialize a new AttrDict from the given dict. Handles any nested dicts by turning them into AttrDicts too::
d = AttrDict({'a': 1, 'b': {'x': 1, 'y': 2}})
d.b.x == 1 # True
Source code in src/calliope/attrdict.py
keys_nested(subkeys_as='list')
¶
Returns all keys in the AttrDict, sorted, including the keys of nested subdicts (which may be either regular dicts or AttrDicts).
If subkeys_as='list'
(default), then a list of
all keys is returned, in the form ['a', 'b.b1', 'b.b2']
.
If subkeys_as='dict'
, a list containing keys and dicts of
subkeys is returned, in the form ['a', {'b': ['b1', 'b2']}]
.
Source code in src/calliope/attrdict.py
set_key(key, value)
¶
Set the given key
to the given value
. Handles nested
keys, e.g.::
d = AttrDict()
d.set_key('foo.bar', 1)
d.foo.bar == 1 # True
Source code in src/calliope/attrdict.py
to_yaml(path=None)
¶
Saves the AttrDict to the path
as a YAML file, or returns
a YAML string if path
is None.
Source code in src/calliope/attrdict.py
union(other, allow_override=False, allow_replacement=False, allow_subdict_override_with_none=False)
¶
Merges the AttrDict in-place with the passed other
AttrDict. Keys in other
take precedence, and nested keys
are properly handled.
If allow_override
is False, a KeyError is raised if
other tries to redefine an already defined key.
If allow_replacement
, allow "REPLACE" key to replace an
entire sub-dict.
If allow_subdict_override_with_none
is False (default),
a key of the form this.that: None
in other will be ignored
if subdicts exist in self like this.that.foo: 1
, rather
than wiping them.