Skip to main content

attribute-mapping is a minimalistic Python library for attribute-based access to dictionaries and other mappings.

Compared to many other implementations of the same idea, going by names such as AttrDict and various others, this library is extremely minimal and free of cruft:

  • Almost no API

  • AttributeMapping instances do not pretend to be a dict

  • Can be used with dict as well as other objects implementing the collections.abc.Mapping abstract base class

  • No restrictions on key/attribute names

  • No unpleasant surprises in behaviour or weird corner cases

  • Modern code; Python 3.4+ only

  • 100% test coverage

Installation

python -m pip install attribute-mapping

Usage

Make an AttributeMapping instance by passing a dictionary or another mapping:

from attribute_mapping import AttributeMapping

d = {"a": 1, "b": {"c": 2, "d": 3}}
x = AttributeMapping(d)

Now you can access the contents using attribute lookups:

x.a  # gives 1
x.b.c  # gives 2

x.foo = 123
hasattr(x, "foo")  # True
del x.foo

In addition to attribute access, subscription (__getitem__ and friends) and containment checks (in) also work:

x["a"]  # gives 1
x["b"]["c"]  # gives 2
x["foo"] = 123
"foo" in x  # True
del x["foo"]

However, there are no other dict-like methods or reserved names, so you can happily use attributes like keys and items:

x.items = [1, 2, 3]

Iteration yields (key, value) tuples, just like .items() on normal mappings would do:

for key, value in x:
    ...

Finally, to obtain the original object that was used for the AttributeMapping, use the built-in vars() function:

d = {"a": 1}
x = AttributeMapping(d)
vars(x) is d  # True

Credits

This library is written by wouter bolsterlee (wbolster).

There are a gazillion similar implementations, so the author thanks the whole Python community for the inspiration to make yet another implementation of this idea.

Version history

  • 1.3.0 (2019-03-13)

    • Add support for len()

  • 1.2.0 (2019-03-12)

    • Add support for (in)equality tests

  • 1.1.0 (2019-03-09)

    • Add support for custom mappings

  • 1.0.0 (2019-03-08)

    • Initial release

License

BSD; see LICENSE file for details.

Metadata

Release files for attribute-mapping 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for attribute-mapping 1.3.0
File Size Uploaded
attribute-mapping-1.3.0.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for attribute-mapping 1.3.0
File Interpreter ABI Platform
attribute_mapping-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.2 kB

Release files / attribute-mapping-1.3.0.tar.gz

Download URL attribute-mapping-1.3.0.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f0a9a32cd8850977675a67642a0f050b7bcc03be3701e46ce9fc8d3cc61b9595
BLAKE2b-256 checksum
How to use checksums
a57bc4adf610559652851ae613dfdbeae86281d0dfeb78b33ad1a8c8aa78582a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3

Release files / attribute_mapping-1.3.0-py3-none-any.whl

Download URL attribute_mapping-1.3.0-py3-none-any.whl
Size 4.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0df141be071a72ef03685d1c35930fb331a430f508e7d71c32dc3cda848f4bb
BLAKE2b-256 checksum
How to use checksums
bfd70a25b69095230c4e93441cbef62f7f0aca8f58c414f06c53d5d02bfebe48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.3

Release history Release notifications | RSS feed

This release

1.3.0 This release

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page