Skip to main content

OrderedDict with attribute-style access

Project description

An ordered dictionary with attribute-style access.

Usage

AttrDict behaves exactly like collections.OrderedDict, but also allows keys to be accessed as attributes:

>>> from orderedattrdict import AttrDict
>>> conf = AttrDict()
>>> conf['z'] = 1
>>> assert conf.z == 1
>>> conf.y = 2
>>> assert conf['y'] == 2
>>> conf.x = 3
>>> assert conf.keys() == ['z', 'y', 'x']

NOTE: If the key clashes with an OrderedDict attribute or starts with __ (two underscores), you can’t access it as an attribute. For example:

>>> a = AttrDict(keys=1)
>>> a.keys
<bound method AttrDict.keys of AttrDict([('keys', 1)])>
>>> a['keys']
1

Load JSON preserving the order of keys:

>>> import json
>>> data = json.load(open('test.json'), object_pairs_hook=AttrDict)

Load YAML preserving the order of keys:

>>> import yaml
>>> from orderedattrdict.yamlutils import AttrDictYAMLLoader
>>> data = yaml.load(open('test.yaml'), Loader=AttrDictYAMLLoader)

Make PyYAML always load all dictionaries as AttrDict:

>>> from orderedattrdict.yamlutils import from_yaml
>>> yaml.add_constructor(u'tag:yaml.org,2002:map', from_yaml)
>>> yaml.add_constructor(u'tag:yaml.org,2002:omap', from_yaml)

json.dump, yaml.dump and yaml.safe_dump convert AttrDict into dictionaries, retaining the order:

>>> json.dumps(data)
>>> yaml.dump(data)

You can subclass AttrDict with mixins for more functionality. Set the __exclude_keys__ to exclude certain keys from attribute access. For example, to implement a Counter that uses AttrDict, create this class:

>>> from collections import Counter
>>> class CounterAttrDict(AttrDict, Counter):
>>>     def __init__(self, *args, **kwargs):
>>>         AttrDict.__init__(self, *args, **kwargs)
>>>         Counter.__init__(self)
>>>         self.__exclude_keys__ |= {'most_common', 'elements', 'subtract'}

This CounterAttrDict acts like a counter but with ordered keys via attribute access. However, .most_common, .elements and .subtract will not be used as keys:

>>> c = CounterAttrDict()
>>> c.x
0
>>> c.elements
<bound method CounterAttrDict.elements of CounterAttrDict()>
>>> c.x += 1
>>> c.y += 2
>>> c.most_common()
[('y', 2), ('x', 1)]
>>> list(c.elements())
['x', 'y', 'y']
>>> c.subtract(y=1)
>>> c
CounterAttrDict([('x', 1), ('y', 1)])

To create a defaultdict that is ordered and has attribute access, subclass from AttrDict and defaultdict:

>>> from collections import defaultdict
>>> class DefaultAttrDict(AttrDict, defaultdict):
>>>     def __init__(self, default_factory, *args, **kwargs):
>>>         AttrDict.__init__(self, *args, **kwargs)
>>>         defaultdict.__init__(self, default_factory)
>>>         self.__exclude_keys__ |= {'default_factory', '_ipython_display_'}

This can be used with a list factory:

>>> d.x
[]
>>> d.y.append(10)
>>> d
DefaultAttrDict([('x', []), ('y', [10])])

You can create a tree structure where you can set attributes in any level of the hierarchy:

>>> tree = lambda: DefaultAttrDict(tree)
>>> node = tree()
>>> node.x.y.z = 1
>>> node
DefaultAttrDict([('x', DefaultAttrDict([('y', DefaultAttrDict([('z', 1)]))]))])

Installation

This is a pure-Python package is build for Python 2.7+ and Python 3.0+. Set it up using:

pip install orderedattdict

Changelog

  • 1.0: Basic implementation

  • 1.1: Add utilities to load and save as YAML

  • 1.2: Allow specific keys to be excluded from attribute access

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

orderedattrdict-1.2.zip (8.5 kB view details)

Uploaded Source

Built Distribution

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

orderedattrdict-1.2-py2.py3-none-any.whl (6.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file orderedattrdict-1.2.zip.

File metadata

  • Download URL: orderedattrdict-1.2.zip
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for orderedattrdict-1.2.zip
Algorithm Hash digest
SHA256 d69fdfcdcd13362d29db88c9522b8dd54a87b61fd511e94c1125e2e1f9283cff
MD5 b65a34b9715ec67d7bbffc451ab0bbd4
BLAKE2b-256 b8fb5a6247dd37df75b057102089f10b4e2252ef75a8dad58d25bf54845cb8dc

See more details on using hashes here.

File details

Details for the file orderedattrdict-1.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for orderedattrdict-1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5a5b33c812ec50b3cc1fae19d65d4300d99ec5a2343fb45280508fde5851685e
MD5 6b735163f4e40bd501b9e53d67586aed
BLAKE2b-256 126461b15421f4b0435e8caf8652f3d15385ab3c78b269689abdc3c7697e158c

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