Skip to main content

Dictionary Library including good deep merge and dictionary as objects

Project description

Various tools, including:

dictlib.union()

>>> dict3 = dictlib.union(dict1, dict2)
>>> dict3 = dictlib.union_copy(dict1, dict2)

Deep union of dict2 into dict1, where dictionary values are recursively merged. Non-dictionary elements are replaced, with preference given to dict2.

This alters dict1, which is the returned result, but it will have references to both dictionaries. If you do not want this, use union_copy(), which is less efficient but data-safe.

dictlib.dig()

Recursively pull from a dictionary, using dot notation.

>>> dictlib.dig({"a":{"b":{"c":1}}}, "a.b.c")
1
>>> dictlib.dig({"a":{"b":[{"c":1},{"d":2}]}}, "a.b[1].d")
2
>>> dictlib.dig({"a":{"b":{"c":1}}}, "a.b.d")
Traceback:...

There is also dig_get(), which allows for a default, similar to dict.get:

>>> dictlib.dig_get({"a":{"b":{"c":1}}}, "a.b.c", 2)
1
>>> dictlib.dig_get({"a":{"b":{"c":1}}}, "a.b.d", 2)
2

dictlib.dug()

Inverse of dig, puts an item into a nested dictionary, using dot notation. This does not behave functionally, it alters the origin dictionary.

>>> test = {"a":{"b":{"c":1}}}
>>> dug(test, "a.b.c", 200)
>>> test
{'a': {'b': {'c': 200}}}

dictlib.Obj()

Represent a dictionary in object form, while handling tokenizable keys, and can export to original form. Recursive.

Not python zen because it provides an alternate way to use dictionaries. But I'm okay with this, becuase it is handy.

Limitations:

  • raises error if there is a name conflict with reserved words
  • reserves the prefix \f$\f for internal use (raises error)
  • because of namespace conflict problems, this is a deal breaker for universal use--you must be cautious on what keys are input.
  • Two keys exist for each non-tokenized name, such as ugly var!, which is tokenized to ugly_var_. While both exist, if exporting to original object only the value of the tokenized name is used.

Examples:

>>> dictlib.Obj(key1=1, a=2)
{'key1': 1, 'a': 2}
>>> test_dict = {"a":{"b":1,"ugly var!":2}, "c":3}
>>> test_obj = dictlib.Obj(**test_dict)
>>> orig_obj = test_obj.copy() # referenced later
>>> test_obj.keys()
['a', 'c']
>>> 'a' in test_obj
True
>>> test_obj.get('c')
3
>>> test_obj['c']
3
>>> test_obj.c
3
>>> test_obj.c = 4
>>> test_obj.c
4
>>> test_obj.a.b
1
>>> test_obj.a.ugly_var_
2
>>> test_obj.a['ugly var!']
2
>>> test_obj.__original__()
{'a': {'b': 1, 'ugly var!': 2}, 'c': 4}

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

dictlib-1.1.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

dictlib-1.1.2-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file dictlib-1.1.2.tar.gz.

File metadata

  • Download URL: dictlib-1.1.2.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.6

File hashes

Hashes for dictlib-1.1.2.tar.gz
Algorithm Hash digest
SHA256 7945a38c37d137cc2a854b3bc803a53f8b3b9c32478caf534c64ffc020416742
MD5 ef70b6e6e77525ea9982817588e988b6
BLAKE2b-256 a5b1cc6991ea90fea352170213529f2aa245b4b6443f58ad048de593c5430073

See more details on using hashes here.

File details

Details for the file dictlib-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: dictlib-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 18.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.19.5 CPython/3.6.6

File hashes

Hashes for dictlib-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6db94be9bbd80602ad32afb1507543275c973c05bebf69f1236c8a73506b0b5e
MD5 d025dbdecc638d53d21800c9bc9e99d8
BLAKE2b-256 b6e4c66681aa95fffe77a0e8c211523c3ff25bed371eaac6e23a897d66a05987

See more details on using hashes here.

Supported by

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