Skip to main content

minimal attrdict implementation

Project description

attribute-mapping is a minimalistic python library to allow attribute lookups in 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.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.

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

attribute-mapping-1.2.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

attribute_mapping-1.2.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file attribute-mapping-1.2.0.tar.gz.

File metadata

  • Download URL: attribute-mapping-1.2.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for attribute-mapping-1.2.0.tar.gz
Algorithm Hash digest
SHA256 4d4cb45bb026f89357b6dc4115749d1b0935c66ce177876ae4060f01ff07e454
MD5 86ab6ed00683056d45cd5f571b5c0822
BLAKE2b-256 e316cea8d0e298004c7a2aeb5f2e99f98bf53bff9fe799d74f581b775849dc81

See more details on using hashes here.

File details

Details for the file attribute_mapping-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: attribute_mapping-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for attribute_mapping-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d32e504f64197c8290fcaf80ccadfc7fd6669e72ef587fb38b87b54a4e16431e
MD5 b8425f2e1eb1d4a080e773d45c6c81fd
BLAKE2b-256 bdd7f97c1f68f8eb47ae688651b50530253ed94bfcf37e814230501bd821a8da

See more details on using hashes here.

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