Skip to main content

Adict is an attribute-accessible dynamic dictionary wrapper

Project description

Latest Release pipeline status coverage report

Adict is an attribute-accessible dynamic dict wrapper, which allows to access dict items in attribute notation (".") and allows friendly checks for non-existing items.

Adict is not an extension of Python's dict (as for example adict and dict), but a wrapper around dict objects. This allows to use attribute syntax not only for objects created using the Adict() constructor, but also for child dictionaries, which are automatically wrapped into an Adict when they're returned.

Features

  • Default dict behavior
  • Full wrapping of nested dicts
  • Fail-safe attribute notation (adict.key) doesn't raise KeyError
  • Save traversing using parenthesis syntax (('key'))
  • Supports nested dicts
  • Supports JSON encoding

Installation

pip install dictat

Examples

from dictat import Adict

dict1 = Adict()
print('noob' in dict1)          # False
print(dict1.noob)               # None - doesn't fail
# print(dict1['noob'])          # raises KeyError - default dict behavior
dict1.noob = 'me'
print(dict1['noob'])            # "me" - dict notation
print(dict1.noob)               # "me" -, attribute notation
dict1.sub = {}
dict1.sub.dict = {'noobs': ['me', 'you']}
print(dict1.sub.dict.noobs[1])  # "you"

dict2 = {'noob': 'me', 'sub': {'you': 'noob'}}
dict3 = Adict(dict2)            # construct around existing dict
print('noob' in dict3)          # True
print(dict3['noob'])            # "me", dict notation
print(dict3.noob)               # "me", attribute notation
print(dict3.sub.you)            # "noob', nested attribute notation

Safe traversing using paranthesis syntax

At the cost of not having None values, the () operator allows key access, which always returns a valid (empty) Adict instance when the key doesn't exist. This allowes to traverse dicts into depper levels, without intermediate None checks. This syntax is basically an abbreviation of the dict.get(key, default) function, but has the additional feature to again wrap default dict values into Adict(dict) results.

dd = Adict({'noob': 'me', 'sub': {'you': 'noob'}})

print(dd('sub')('you'))      # "noob"
# is equivalent to
print(dd('sub', {})('you', {}))
# is equiivalent to
print(dd.get('sub', {}).get('you', {}))

print(dd('nokey'))           # {} (isinstance Adict)
print(dd('nokey', {}))       # {} (isinstance Adict)
print(dd('nokey', None)      # None

JSON encoding

from dictat import Adict, JsonEncoder

import json
import pathlib

adict = Adict()
adict.key = 'string'
adict.sub = dict(subkey='subvalue', obj=object())
adict.path = pathlib.Path('somepath')  # normally not JSON serializable

dump = json.dumps(adict, cls=JsonEncoder)
print(dump)

# {"key": "string", "sub": {"subkey": "subvalue"}, "path": "somepath"}

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

dictat-1.1.1.tar.gz (6.2 kB view hashes)

Uploaded Source

Built Distribution

dictat-1.1.1-py3-none-any.whl (6.1 kB view hashes)

Uploaded Python 3

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