Skip to main content

Wrapper for the dict class that extends the functionality for nested dicts including navigating using keypaths and nested key searching. This includes mixed dicts and lists.

Project description

Nested Dictionary

Version: 1.2.0

A wrapper for python dicts that allows you to search and navigate through nested dicts using key paths. Also works with mixed dictionaries (mixuture of nested lists and dicts). Derived from a need to search for keys in a nested dictionary; too much time was spent on building yet another full class for nested dictionaries, but it suited our needs.

Features (Example nested dictionary: nested_dict = NestedDict( { 'path':{'to':{'key':val}} )):

  • Uses keypaths in subscripting to navigate nested dictionaries ( ex: nested_dict[ ['path','to','key'] ] which is the same as nested_dict['path']['to']['key'] )
  • Adds functionality without violating any existing dict operations (that I know of); keypaths are in the form of a list which cannot be used as a key for a normal dict anyway. All other dict rules still apply.
  • findall method: Finds all nested keys within a nested dictionary.
  • get and set methods: Navigate using a keypath string with seperator ( ex: nested_dict.get('path.to.key') )
  • insert method: create a full path to a nested key and set a value, even if the parent keys leading to the destination key don't already exist ( i.e., nested_dict.insert( ['newpath','to','key'], newval ) will add to the existing dictionay, resulting in: NestedDict({ 'path':{'to':{'key':val}}, 'newpath':{'to':{'key':newval}} }) ).

Limitations:

  • While fast, it adds some overhead and therefore cannot ever be as fast as accessing dicts the regular way.

Changes (PEP 440: major.minor.patch):

  • v0.1.0: Developed methods for searching keys in nested dictionaries.
  • v1.0.1: Initial working version of the nesteddictionary class.
  • v1.2.0: Changed dictionary traversing from recursive to functools.reduce; This is less pythonic yet faster (however, still not nearly as fast as directly accessing dicts and list). For comparison, when doing (these are not included in tests, but are easy enough to write and test on your own):
    >>> d = [{1:{2:'value'}}]   
    >>> %timeit using_reduce(d,[0,1,2])     #reduce from functools
    >>> %timeit using_recursion(d,[0,1,2])  #what was used in v1, more pythonic
    >>> %timeit d[0][1][2]                  #direct access; fastest
    
    Yields:
    >>> 648 ns ± 3.17 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)    #reduce
    >>> 1.77 µs ± 4.05 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)   #recursion
    >>> 89.3 ns ± 0.448 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) #direct
    

References:

  • Scalpl: A similar implementation to nested dictionaries. Some good methodology here.
  • Functools Reduce for dicts: Speed up dictionary acces, but non-pythonic.
  • Others I'm sure I forgot to mention. Thank you.

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

nesteddictionary-1.2.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

nesteddictionary-1.2.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file nesteddictionary-1.2.0.tar.gz.

File metadata

  • Download URL: nesteddictionary-1.2.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0

File hashes

Hashes for nesteddictionary-1.2.0.tar.gz
Algorithm Hash digest
SHA256 1a2d6d5ac726fb94dc6a7d1cc882c34e42c9727a4509b651fb39ecd6e7f00b07
MD5 198da24b5a9d11c84cf3b7f799e992a7
BLAKE2b-256 94c481077f72b27cd240a42fad42f3e1b185cc4958597d34c760f15350b456cb

See more details on using hashes here.

File details

Details for the file nesteddictionary-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: nesteddictionary-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0

File hashes

Hashes for nesteddictionary-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3904a6c7743365c346d7b435335beca7f77009ba3eae2e418d61136ceb1bc04
MD5 e62ea09c5e12228ef8abfe23f54169ef
BLAKE2b-256 0bf4627924d561d2a021e0fb146fb6d4ea9f04539d3381a88ab2c5be34facc54

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