Skip to main content

Attribute access for dictionary-like objects

Project description

https://github.com/knowikow/ya.dotdict.py/workflows/build/badge.svg

Yet Another DotDict

A Python library providing attribute access for dictionaries. The library provides these classes:

  • ya.dotdict.DotDict as a replacement for dict and collections.defaultdict

  • ya.dotdict.DotDictMixin: a mixin class that can be used to add attribute-style access to any dict-like class. ya.dotdict.DotDict is implemented in terms of this mixin class

All the code examples assume:

>>> from ya.dotdict import *

Simple use case: access dictionary items as attributes

DotDict can be used as a replacement for dict:

>>> d = DotDict()
>>> d['spam'] = 'eggs'
>>> d.spam
'eggs'
>>> d.spam = 100
>>> d['spam']
100

Deleting attributes also works as expected:

>>> del d.spam
>>> d.spam
Traceback (most recent call last):
   ...
AttributeError: spam

Create default values (like collections.defaultdict)

DotDict can be used as a replacement for defaultict:

>>> d = DotDict(lambda: 'eggs')
>>> d.spam
'eggs'
>>> d.spam = 100
>>> d['spam']
100
>>> d._default_factory = lambda: 'foo'
>>> d.bar
'foo'

If the default value factory takes an argument, then the key is passed to it:

>>> d._default_factory = lambda key: [key, 1000]
>>> d.foo
['foo', 1000]

If the default value factory takes more than one argument, a TypeError will be raised:

>>> d._default_factory = lambda x, y: [x, y]
Traceback (most recent call last):
   ...
TypeError: defult_factory can only take zero or one argument

DotDictMixin

A mixin class to provide attribute access to dict-like classes. DotDict is implemented using DotDictMixin like this:

class DictClass(DotDictMixin, dict): pass

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

ya.dotdict-1.0.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

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

ya.dotdict-1.0.2-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file ya.dotdict-1.0.2.tar.gz.

File metadata

  • Download URL: ya.dotdict-1.0.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for ya.dotdict-1.0.2.tar.gz
Algorithm Hash digest
SHA256 539f9190e60daacd3ee8ab279b86629798ba771e19c315288a304b66d0230b31
MD5 a62a8ae7091f2bdb55a756763491537b
BLAKE2b-256 69aa8b85eba2762dbb0232eb71fdbd02be3414bded3c92c37400cc974a521fe3

See more details on using hashes here.

File details

Details for the file ya.dotdict-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: ya.dotdict-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7

File hashes

Hashes for ya.dotdict-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7b7f7d069883c27643f73db338718395b1926606e4bbe68160b2a42143e7ddb8
MD5 10292c24556d283166816f2ad8c6c931
BLAKE2b-256 86ce132aaba735b20a5b049b1117b64ffbd8356a3b993a1d32f88d5dd730bdc0

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