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 AttrDict attribute, the key is not used. This also applies if the key starts with __ (two underscores). For example:
>>> a = AttrDict(keys=1) >>> a.keys <bound method AttrDict.keys of AttrDict([('keys', 1)])> >>> a['keys'] 1
JSON files can be parsed using AttrDict as follows:
>>> import json >>> data = json.load(open('file.json'), object_pairs_hook=AttrDict)
YAML files can be parsed using AttrDict as follows:
>>> import yaml >>> from orderedattrdict import AttrDictYAMLLoader >>> data = yaml.load(open('file.yaml'), Loader=AttrDictYAMLLoader)
YAML files can be saved from AttrDict structures as follows:
>>> from orderedattrdict import AttrDictYAMLDumper >>> yaml.dump(data, Dumper=AttrDictYAMLDumper)
Installation
pip install orderedattdict
This is a pure-Python package is build for Python 2.7+ and Python 3.0+.
Project details
Release history Release notifications | RSS feed
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.0.1.zip
(5.3 kB
view hashes)
Built Distribution
Close
Hashes for orderedattrdict-1.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4397b3b0649d56a7839e6e6db27c4338855b3068106c86da9614595f1334687f |
|
MD5 | 636112af63b361f27cae38f7b537dd68 |
|
BLAKE2b-256 | 1860a9b052a743f62ca5e3a577c7b714a8f5cb772cd2cda4508e5ae23949bb1f |