Nested collections wrapper
Project description
ncw
Nested collections wrapper
Classes to access and/or modify data in nested collections, i.e. possibly deep nested dicts or lists of str, int, float, bool, or None.
Installation
Install from PyPI
pip install ncw
Example Usage
The Structure class prevents accidental changes to the underlying data structure by preventing direct access. All returned substructures are deep copies of the internally stored substructures.
The MutableStructure class allows changes (ie. deletions and updates) to the underlying data structure, and returns the internally stored substructures themselves.
Please note that both classes make a deep copy of the data structure at initialization time (thus preventing accidental changes to the original data through the instance).
>>> serialized = '{"herbs": {"common": ["basil", "oregano", "parsley", "thyme"], "disputed": ["anise", "coriander"]}}'
>>>
>>> import json
>>> from ncw import Structure, MutableStructure
>>>
>>> readonly = Structure.from_json_string(serialized)
>>> readonly["herbs"]
{'common': ['basil', 'oregano', 'parsley', 'thyme'], 'disputed': ['anise', 'coriander']}
>>> readonly["herbs.common"]
['basil', 'oregano', 'parsley', 'thyme']
>>> readonly["herbs", "common"]
['basil', 'oregano', 'parsley', 'thyme']
>>> readonly["herbs", "common", 1]
'oregano'
>>> readonly["herbs.common.1"]
'oregano'
>>> readonly["herbs.common.1"] = "marjoram"
Traceback (most recent call last):
File "<python-input-9>", line 1, in <module>
readonly["herbs.common.1"] = "marjoram"
~~~~~~~~^^^^^^^^^^^^^^^^^^
TypeError: 'Structure' object does not support item assignment
>>>
>>> original_data = json.loads(serialized)
>>> writable = MutableStructure(original_data)
>>> writable.data == original_data
True
>>> writable.data is original_data
False
>>> writable["herbs.common.1"]
'oregano'
>>> writable["herbs.common.1"] = "marjoram"
>>> del writable["herbs", "common", 2]
>>> writable["herbs.common"]
['basil', 'marjoram', 'thyme']
>>>
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ncw-0.2.0.tar.gz.
File metadata
- Download URL: ncw-0.2.0.tar.gz
- Upload date:
- Size: 1.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79bb3345cd67417b5cf96d15aa55e3dedd8bb1cdb92afca2d034040822798e0d
|
|
| MD5 |
e4a5ff5af01f0827b673393a0ee3ac75
|
|
| BLAKE2b-256 |
ee1741c98bfa715057505fa24bf51368596a8dfa0603be25fc6f5392015b02a2
|
File details
Details for the file ncw-0.2.0-py3-none-any.whl.
File metadata
- Download URL: ncw-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.7.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb7fe3b36bf7453747a379a4099f1e059ec4c2f37ac82ab777f749570b3b570c
|
|
| MD5 |
cb38fa5c5a8f4c57e36d9911197e636a
|
|
| BLAKE2b-256 |
779291c30f9c94c57c70c62ef5f37e72c0b22731758e395f1b26c050b44ff71a
|