calliope.attrdict.AttrDict(source_dict=None)
¶
Bases: dict
Extended dict
class.
A subclass of dict
with key access by attributes.
Examples:
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()
¶
Return the AttrDict as a pure dict, converting nested AttrDicts.
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, allow_override=False)
classmethod
¶
Returns an AttrDict initialized from the given path or file path.
If resolve_imports
is True, top-level import:
statements
are resolved recursively.
If resolve_imports
is False, top-level import:
statements
are treated like any other key and not further processed.
If resolve_imports
is a string, such as foobar
, import
statements underneath that key are resolved, i.e. foobar.import:
.
When resolving import statements, anything defined locally
overrides definitions in the imported file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str | Path
|
YAML file. |
required |
resolve_imports
|
bool | str
|
top-level |
True
|
allow_override
|
bool
|
whether or not to allow overrides of already defined keys. Defaults to False. |
False
|
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
constructed AttrDict |
Source code in src/calliope/attrdict.py
from_yaml_string(string, resolve_imports=True, allow_override=False)
classmethod
¶
Returns an AttrDict initialized from the given string.
Input string must be valid YAML.
If resolve_imports
is True, top-level import:
statements
are resolved recursively.
If resolve_imports
is False, top-level import:
statements
are treated like any other key and not further processed.
If resolve_imports
is a string, such as foobar
, import
statements underneath that key are resolved, i.e. foobar.import:
.
When resolving import statements, anything defined locally
overrides definitions in the imported file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
string
|
str
|
Valid YAML string. |
required |
resolve_imports
|
bool | str
|
top-level |
True
|
allow_override
|
bool
|
whether or not to allow overrides of already defined keys. Defaults to False. |
False
|
Returns:
Type | Description |
---|---|
Self
|
calliope.AttrDict: |
Source code in src/calliope/attrdict.py
get_key(key, default=_MISSING)
¶
Looks up the given 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, including nested keys.
Nested subdicts 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)
¶
Conversion to YAML.
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)
¶
In-place merge with another AttrDict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other
|
Self | dict
|
other AttrDict to use in union. Takes precedence. |
required |
allow_override
|
bool
|
whether or not to allow overrides of already defined keys. Defaults to False. |
False
|
allow_replacement
|
bool
|
allow "REPLACE" key to replace an entire sub-dict. Defaults to False. |
False
|
allow_subdict_override_with_none
|
bool
|
if False, keys in the
form |
False
|
Raises:
Type | Description |
---|---|
KeyError
|
|