Skip to main content

A dict implementation with support for easy and clean access of its values through attributes

Project description

attridict

Build Status GitHub

A Python package implementing atrribute dictionary.

This provides an easier and cleaner way to access dict values using their keys as attributes. It is typically a dict child, maintaining all the dict functionalities, but including some extra features.

Installation

To install the package from PyPI, use:

pip install attridict

Usage

Creating an attridict object

>>> import attridict
>>> att = attridict()
>>> att
{}
>>> att.foo = "bar"
>>> att.foo
'bar'
>>> att
{'foo': 'bar'}

A dict can be converted into an attridict object by passing it as an argument

>>> import attridict

>>> data = {'red': 'hot', 'blue': 'cold'}

>>> colors = attridict(data)
>>> colors
{'red': 'hot', 'blue': 'cold'}

>>> colors.red
'hot'

Modifying attridict object

>>> import attridict

>>> data = {'red': 'hot', 'blue': 'cold'}

>>> colors = attridict(data)
>>> colors
{'red': 'hot', 'blue': 'cold'}

>>> colors.blue
'cold'

>>> colors.blue = "sky"
>>> colors.red = "rose"
>>> colors.blue
'sky'
>>> colors.red
'rose'
>>> colors
{'red': 'rose', 'blue': 'sky'}

>>> colors.green = "grass"
>>> colors
{'red': 'rose', 'blue': 'sky', 'green': 'grass'}

Typical dict operations work on attridict objects

>>> import attridict

>>> data = {'red': 'rose', 'blue': 'sky'}

>>> colors = attridict(data)
>>> colors
{'red': 'rose', 'blue': 'sky'}

>>> colors.red
'rose'
>>> colors["red"]
'rose'

>>> colors["red"] = "tomato"
>>> colors["red"]
'tomato'
>>> colors.red
'tomato'

Nested attribute access

>>> import attridict

>>> data = {'foo': {}}
>>> att = attridict(data)

>>> att
{'foo': {}}

>>> att.foo.bar = 'baz'

>>> att.foo.bar
'baz'

>>> att
{'foo': {'bar': 'baz'}}

YAML serializable

>>> import attridict
>>> import yaml

>>> data = {'foo': {'bar': 'baz'}}

>>> att = attridict(data)

>>> yaml.dump(att)
'!attridict\nfoo:\n  bar: baz\n'

>>> yaml.safe_dump(att)
'foo:\n  bar: baz\n'

License

The project is MIT licensed

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

attridict-0.0.9.tar.gz (5.4 kB view hashes)

Uploaded Source

Built Distribution

attridict-0.0.9-py3-none-any.whl (5.9 kB view hashes)

Uploaded Python 3

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