Skip to main content

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.root == original_data
True
>>> writable.root 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ncw-0.1.1.tar.gz (1.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ncw-0.1.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file ncw-0.1.1.tar.gz.

File metadata

  • Download URL: ncw-0.1.1.tar.gz
  • Upload date:
  • Size: 1.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.8

File hashes

Hashes for ncw-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0cf405b2085e6c650fba70e98754947fe9f45894c66cd6a9fe69c98a105dea67
MD5 ae27cd97149e3d4eeb65e631218fa9a7
BLAKE2b-256 b246b6134516fba1b287a41f897c7e1ab0a374da043a9d76af333905f0074837

See more details on using hashes here.

File details

Details for the file ncw-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: ncw-0.1.1-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

Hashes for ncw-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 202b3e60a3fb823d502b945e66613bad1c05133053b1fb984f2e7a78022cbb76
MD5 42d80cf9a6bc1d6ce1a7a47cbd506038
BLAKE2b-256 f6024b71bda2076bbd0991f9568a8b9a6b52d4c9b8c75d9559fb4358d2eadd14

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page