Skip to main content

Utility functions for nested dictionaries (dict of dicts)

Project description

Nestees

Provides a simple set of utility functions for dealing with nested dictionaries - a.k.a. dict of dicts. Inspired by the 'field path' concept in Google's superb Firebase product, these functions fully support the field path syntax and DELETE_FIELD sentinel to allow values nested arbitrarily deep in standard Python dictionaries to be get, set, updated and deleted.

Example Usage: deep_get

from nestees import deep_get

data = {
   'top': {
       'mid_leaf': 'magic',
       'middle': {
           'leafA': 20,
           'leafB.has.dots': 'its real!!!',
       },
   }
}

deep_get(data, 'top.mid_leaf')
>>> 'magic'

deep_get(data, 'top.middle.leafA')
>>> 20

# field path syntax supports backtics
deep_get(data, 'top.middle.`leafB.has.dots`')
>>> 'its real!!!'

# Get supports default values
deep_get(data, 'my.road.to.nowhere`', 'some default')
>>> 'some default'

Example Usage: deep_set and deep_delete

from nestees import deep_set, deep_delete

data = {}

deep_set(data, 'top.mid_leaf', 'magic')
repr(data)
>>>  {
   'top': {
       'mid_leaf': 'magic',
   }
}

deep_set(data, 'top.middle.leafA', 20)
repr(data)
>>> {
   'top': {
       'mid_leaf': 'magic',
       'middle': {
           'leafA': 20
       },
   }
}

# To delete a value, use the DELETE_SENTINEL
deep_set(data, 'top.mid_leaf.leafA', DELETE_SENTINEL)
repr(data)
>>> {
   'top': {
       'middle': {
           'leafA': 20
       }
   }
}

# or just use deep_delete
deep_delete(data, 'top.middle.leafA')
repr(data)
>>> {
   'top': {
       'middle': {}
   }
}

Example of deep_update

deep_update works like Python's built-in dict update method, in that it allows values to be merged at the leaf level, rather than set, if the existing leaf is a dict...

from nestees import deep_update

data = {
    'top': 53
}

# Updating an integer lead replaces the integer with a dict...
deep_update(data, 'top.middle', {'name':'John', 'occupation':'CEO'})
repr(data)

>>> {
    'top': {
        'middle': {
            'name':'John',
            'occupation':'CEO'
        }
    }
}

>>> deep_update(data, 'top.middle', {'gender':'non-binary'})
>>> data
{
    'top': {
        'middle': {
            'name':'John',
            'occupation':'CEO',
            'gender':'non-binary'
        }
    }
}

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

nestees-1.0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

nestees-1.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file nestees-1.0.1.tar.gz.

File metadata

  • Download URL: nestees-1.0.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.3

File hashes

Hashes for nestees-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5a9fdb812b886acb14602324a27fa431118df979e94c66e864fd108f82813bbe
MD5 2e8d9498ea16e21d5d2678fe96e2da42
BLAKE2b-256 0bc8ca7f8c995a3859f345806b7960d3d14dd2b9e923c1a08441596e25bd9ee1

See more details on using hashes here.

File details

Details for the file nestees-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: nestees-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.8.3

File hashes

Hashes for nestees-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b0c21be9825993a461c780614c429b71bd83a85073d42bfdc37399deb59cff0e
MD5 eccc41fe1ad63e463b187f53ab838a23
BLAKE2b-256 dfd4921970e4e1b2dfdd464a9ab44349a9c4c057d38e0532e578325cc0f9a380

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