Skip to main content

Easy access to items in deep collections.

Project description

DeepCollection

PyPI version Code style: black

deep_collection is a Python library that provides tooling for easy access to deep collections (dicts, lists, deques, etc), while maintaining a great portion of the collection's original API. The class DeepCollection class will automatically subclass the original collection that is provided, and add several quality of life extensions to make using deep collections much more enjoyable.

Got a bundle of JSON from an API? A large Python object from some data science problem? Some very lengthy set of instructions from some infrastructure as code like Ansible or SaltStack? Explore and modify it with ease.

DeepCollection can take virtually any kind of object including all built-in iterables, everything in the collections module, and dotty-dicts, and all of these nested in any fashion.

Features

  • Path traversal by supplying an list of path components as a key. This works for getting, setting, and deleting.
  • Setting paths when parent parts do not exist.
  • Path traversal through dict-like collections by dot chaining for getting
  • Finding all paths to keys or subpaths
  • Finding all values for keys or subpaths, and deduping them.
  • Provide all of the above through a class that is:
    • easily instantiable
    • a native subclass of the type it was instantiated with
    • easily subclassable

Path concept

DeepCollections has a concept of a "path" for nested collections, where a path is a sequence of keys or indices that if followed in order, traverse the deep collection. As a quick example, {'a': ['b', {'c': 'd'}]} could be traversed with the path ['a', 1, 'c'] to find the value 'd'.

DeepCollections natively use paths as well as simple keys and indices. For dc = DeepCollection(foo), items can be retrieved through the familiar dc[path] as normal if path is a simple key or index, or if it is an non-stringlike iterable path (strings are assumed to be literal keys). This is done with a custom __getitem__ method. Similarly, __delitem__ and __setitem__ also support using a path. The same flexibility exists for the familiar methods like .get, which behaves the same as dict.get, but can accept a path as well as a key.

DeepCollection object API

DeepCollections are instantiated as a normal class, optionally with a given initial collection as an arguement.

from deep_collections import DeepCollection

dc = DeepCollection()
# or
dc = DeepCollection({"a": {"b": {"c": "d"}}})
# or
dc = DeepCollection(["a", ["b", ["c", "d"]]])

These are the noteworthy methods available on all DCs:

  • __getitem__
  • __delitem__
  • __setitem__
  • get
  • paths_to_value
  • paths_to_key
  • values_for_key
  • deduped_values_for_key

There are also corresponding functions availble that can use any native object that could be deep, but is not a DeepCollection, like a normal nested dict or list. This may be a convenient alternative to ad hoc traverse an object you already have, but it is also faster to use because it doesn't come with the initialization cost of a DeepCollection object. So if speed matters, use a function.

deep_collections function API

All of the useful methods for DeepCollection objects are available as functions that can take a collection as an argument, as well as several other supporting functions, which are made plainly availble.

The core functions are focused on using the same path concept. The available functions and their related DC methods are:

  • getitem_by_path - DeepCollection().__getitem__
  • get_by_path - DeepCollection().get
  • set_by_path - DeepCollection().set_by_path
  • del_by_path - DeepCollection().del_by_path
  • paths_to_value - DeepCollection().paths_to_value
  • paths_to_key - DeepCollection().paths_to_key
  • values_for_key - DeepCollection().values_for_key
  • deduped_values_for_key - DeepCollection().deduped_values_for_key
  • dedupe_items
  • resolve_path
  • matched_keys

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

deep_collections-0.3.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

deep_collections-0.3.0-py3-none-any.whl (14.5 kB view details)

Uploaded Python 3

File details

Details for the file deep_collections-0.3.0.tar.gz.

File metadata

  • Download URL: deep_collections-0.3.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.6 Linux/6.0.12-76060006-generic

File hashes

Hashes for deep_collections-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4794289cac51d32e07eff8bb92a0a80b12d15473c3e286a9a7694c026b68e45c
MD5 23233b2aefb32ea40d67622da8e06f74
BLAKE2b-256 966294a5228afaf39974a47be30eed865d7397b510643ff807edbcf5ac595466

See more details on using hashes here.

File details

Details for the file deep_collections-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: deep_collections-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.6 Linux/6.0.12-76060006-generic

File hashes

Hashes for deep_collections-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2af55f395019d393429e7f866640c75a715d4caf00cb9e3bb66ff89a2c4e166b
MD5 db3938c12daf4c663f128d92a70e667b
BLAKE2b-256 477d7664395e7952f9bef4cb23a334388860b7f75b8f30767365a030fbef7847

See more details on using hashes here.

Supported by

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