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
Built Distribution
File details
Details for the file nested_dicts-0.0.0.tar.gz
.
File metadata
- Download URL: nested_dicts-0.0.0.tar.gz
- Upload date:
- Size: 9.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42ba1f6d9a583308cb12528c0e849816dc751eefca347f0041f324e2e47ade2d |
|
MD5 | a7a4e4028c323b4cfcd5ac78b1e47128 |
|
BLAKE2b-256 | ae8cd974236efa6004ae0dd9f9e77e3192664e9f16a33241b2cda907ccec5b99 |
File details
Details for the file nested_dicts-0.0.0-py3-none-any.whl
.
File metadata
- Download URL: nested_dicts-0.0.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2f5705f7e42495244cd86844872b30911a5aa29fbfdd2052165052e4ee62b96 |
|
MD5 | a78fd6a651d4bc901adedb13f67e56d7 |
|
BLAKE2b-256 | 399bb520bcf06910c23fe6795f16972314effc712ba1292a689f534b1a8783b2 |