Skip to main content

Attribute-access ordered dictionary

Project description

PyPI version DOI Build Status codecov

atdict

An attribute-access ordered dictionary in Python


atdict is an attribute-access ordered dictionary. You can use a key name as an attribute to access the value of the dictionary for a key, for example, o.key_name rather than o['key_name']. Only a minimum set of methods are implemented so as to minimize the chances of name conflicts.

atdict was originally developed as the class Object in scribblers, which represents event objects in alphatwirl. Since Object became useful in other contexts, it was released as an independent package as atdict.



Requirement

  • Python 2.7, 3.6, or 3.7

Install

You can install with pip.

$ pip install -U atdict

How to use

Import atdict

Import atdict from the package atdict.

from atdict import atdict

Initialize atdict

You can initialize an atdict with any arguments that can initialize collections.OrderedDict.

For example:

o1 = atdict([('foo', 40), ('bar', 'ham')])
print(o1)

It will prints.

atdict(foo=40, bar='ham')

An atdict can be also initialized with another atdict.

o2 = atdict(o1)
print(o2)

The o2 is initialized with a (shallow) copy of the contents of o1.

atdict(foo=40, bar='ham')

Access to a value

Yon can use a key name as an attribute of atdict.

print(o1.foo)

This will print the value for the key foo, which is 40.

40

Modify a value

To modify a value, you can assign a new value to the attribute.

o1.foo = 50
print(o1)
atdict(foo=50, bar='ham')

The value for the key foo changed from 40 to 50.

Add a key

To add a key, you can also assign a value to the attribute

o1.baz = 'eggs'
print(o1)
atdict(foo=50, bar='ham', baz='eggs')

Delete a key

del deletes a key.

del o1.bar
print(o1)
atdict(foo=50, baz='eggs')

Copy and deepcopy

A copy will be created if atdict is initialized with another atdict. However, this will be a shallow copy.

l = [1, 2]
o1 = atdict([('foo', l)])
o2 = atdict(o1)
print(o2.foo is o1.foo)
True

To make a deep copy, you can use copy.deepcopy().

import copy
l = [1, 2]
o1 = atdict([('foo', l)])
o2 = copy.deepcopy(o1)
print(o2)
atdict(foo=[1, 2])

o2.foo and o1.foo are not the same object.

print(o2.foo is o1.foo)
False

Pickle

An atdict is picklable as long as all values are picklable.

import pickle
o1 = atdict([('foo', 40), ('bar', 'ham')])
p1 = pickle.dumps(o1)
o2 = pickle.loads(p1)
print(o2)
atdict(foo=40, bar='ham')

License

  • atpbar is licensed under the BSD license.

Contact

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

atdict-0.9.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

atdict-0.9.0-py2.py3-none-any.whl (5.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file atdict-0.9.0.tar.gz.

File metadata

  • Download URL: atdict-0.9.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for atdict-0.9.0.tar.gz
Algorithm Hash digest
SHA256 543c954786d74c96ebf49b57525d8f26118782ddcdaf729d8a85dedbb5704e45
MD5 bd09a887c42cb1dad2105e3c5d950897
BLAKE2b-256 0b4bc53bb6e68360c715e4b79337b3231bcfc2b7b5782e00d59ff4eba1160aaf

See more details on using hashes here.

Provenance

File details

Details for the file atdict-0.9.0-py2.py3-none-any.whl.

File metadata

  • Download URL: atdict-0.9.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.8

File hashes

Hashes for atdict-0.9.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 89e3a1988dc4453c81ebb0c5e3cc5ee1e4cf715b1d32d7a414c9ac91ae53be93
MD5 21ba3584c02d74c6bb8a4ddfcc4875ef
BLAKE2b-256 f7da680f4c34a1a63ddf56ee13678759006a20fee5344830544c57847556635f

See more details on using hashes here.

Provenance

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