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.2.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

dictat-1.2.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dictat-1.2.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for dictat-1.2.0.tar.gz
Algorithm Hash digest
SHA256 e68be789a70fb9377f5701a03208839bab18f77e37d69d4eafa61b55bfc5e07b
MD5 2b3203f12c602527cd0807d53eb31c92
BLAKE2b-256 526263f9043135c0a9eab8029eeaaa6be10da53c36269c66330d27e5322a28cf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dictat-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for dictat-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b3d0ffcab13761aec7a6ae9b84100e4a5ef324e7c053010821863c145dc371b
MD5 53ed349755c28aa1b6cc29260ef1766b
BLAKE2b-256 1f3e3acf8a792f977a69999d66886550808b1d9cce8768e1d69bb9ffc7c93584

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