Self-nesting list-keyed dicts, multiple-defaults dicts, and TOML-style Array of Tables lists/dicts.
Project description
Nested Dicts
Python multiple-default dicts, list-keyed dicts, dotted-key dicts and Arrays of Tables classes.
pip install nested_dicts
Examples
DefaultsDictABC
Implement the choose_factory method on a subclass to select a factory. The returned factory is itself called with the key value (unlike collections.defaultdict.missing)
The following 4 examples all define a dictionary subclass d
for which
d == {'k': {'sub_dict_key': 'v'}}
is True.
NestedDefaultsDict
>>> d = NestedDefaultsDict()
>>> d['k']['sub_dict_key'] = 'v'
ListKeyedDict
>>> d = ListKeyedDict.from_nested_dict({'k': {'sub_dict_key': 'v'}})
>>> d[['k', 'sub_dict_key']]
'v'
ListKeyedNestedDefaultsDict
>>> d = ListKeyedNestedDefaultsDict()
>>> d[['k', 'sub_dict_key']] = 'v'
DottedKeyedNestedDefaultsDict
>>> d = DottedKeyedNestedDefaultsDict()
>>> d['k.sub_dict_key'] = 'v'
TOMLTable
>>> from nested_dicts import TOMLTable
>>> t = TOMLTable()
>>> t['table-1'].update(
... key1 = "some string",
... key2 = 123)
>>> t['table-2'].update(
... key1 = "another string",
... key2 = 456)
>>> t
{'table-1': {'key1': 'some string', 'key2': 123}, 'table-2': {'key1': 'another string', 'key2': 456}}
Values in TOMLTable()s indexed with lists behave like Arrays of Tables, even when only read
>>> from nested_dicts import TOMLTable
>>> t = TOMLTable()
>>> t[['products']].update(
name = "Hammer",
sku = 738594937
)
>>> t[['products']] # empty table within the array
{}
>>> t[['products']].update(
name = "Nail",
sku = 284758393,
color = "gray"
)
>>> t
{'products': [{'name': 'Hammer', 'sku': 738594937}, {}, {'name': 'Nail', 'sku': 284758393, 'color': 'gray'}]}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
nested_dicts-0.0.0.tar.gz
(9.6 kB
view hashes)
Built Distribution
Close
Hashes for nested_dicts-0.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2f5705f7e42495244cd86844872b30911a5aa29fbfdd2052165052e4ee62b96 |
|
MD5 | a78fd6a651d4bc901adedb13f67e56d7 |
|
BLAKE2b-256 | 399bb520bcf06910c23fe6795f16972314effc712ba1292a689f534b1a8783b2 |