Skip to main content

Wraps python dictionary keys are attributes

Project description

Python Dict Wrapper

This is a simple class the exposes a dictionary’s keys as class attributes, making for less typing when accessing dictionary values. This class also enforces that the dictionary’s overall shape is enforced.

A common use of this class may be in retrieving and updating model object retrieves from web services (i.e. RESTful web services) where the shape of the model object must be maintained.

Example:

from python_dict_wrapper import DictWrapper

actor = {
    "name": "Steve Carell",
    "career": [{
        "medium": "TV",
        "title": "The Office"
    }, {
        "medium": "MOVIE",
        "title": "Bruce Almighty"
    }]
}

wrapper = DictWrapper(actor)
wrapper.career[1].title = "Despicable Me"

print(wrapper.to_json(pretty=True))

class DictWrapper(dict, strict=False)

Each DictWrapper instance takes two arguments: * dict - A python dictionary that the wrapper will use as it’s source. * strict - An optional boolean that indicates if the wrapper should enforce types when setting attribute values. * key_prefix - A string or list of strings that contains characters that dictionary keys should be prefixed with before they become attributes.

Attributes

Once a DictWrapper instance has been created, the keys of it’s source dictionary will be exposed as attributes. So for example if a DictWrapper is instanciated with the following dictionary:

>>> from dict_wrapper import DictWrapper
>>> address_dict = {'street': '221B Baker Street', 'city': 'London', 'country': 'UK'}
>>> address = DictWrapper(address_dict)

The keys: street, city, and ‘country’ will be exposed as attributes of address

>>> address.street
'221B Baker Street'
>>> address.city
'London'
>>> address.country
'UK'

The attributes are both readable and writeable, so you can update the values simply by assigning to them:

>>> address.country = "United Kingdom"
>>> address.country
'United Kingdom'

If the strict argument to the constructor was set to True, then the DictWrapper will enforce that that when you assign a new value to an attribute, it must be the same Type as the original dictionary value.

>>> address = DictWrapper(address_dict, strict=True)
>>> address.street = 221
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "dict_wrapper.py", line 62, in __setattr__
    raise TypeError("Value for %s must be a %s, not %s" % (
TypeError: Value for street must be a str, not int

If the key_prefix argument to the constructor is set to a string or list of strings, attributes in the dictionary are searched without their prefixes. This is typically used for dictionaries that have keys that cannot be represented in attributes. Here’s an example:

>>> the_dict = {'@timestamp': '2020-04-19 05:00:00', 'author': 'Arthur Conan Doyle'}
>>>
>>> entry = DictWrapper(the_dict)
>>> entry.timestamp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "python_dict_wrapper.py", line 49, in __getattr__
    self._check_for_bad_attribute(key)
  File "python_dict_wrapper.py", line 87, in _check_for_bad_attribute
    raise AttributeError("'%s' object has no attribute '%s'" % (self.__class__.__name__, key))
AttributeError: 'DictWrapper' object has no attribute 'timestamp'
>>>
>>>
>>> entry = DictWrapper(the_dict, key_prefix='@')
>>> entry.timestamp
'2020-04-19 05:00:00'

Methods

DictWrapper instances have to methods: to_json() and to_dict().

to_json(pretty=False)

Converts the dictionary values to a JSON string. If the pretty argument is set to True, the returned JSON will be multi-lined and indented with 4 characters. If it’s false, the returned JSON will a single-line of text.

to_dict()

Converts the DictWrapper back to a Python dictionary.

Nesting

DictWrapper instances should be able to handle nested dictionaries and lists without issue. It automatically wraps any nested dictionaries in their own DictWrapper instances for you.

>>> shelock_dict = {
...     'name': 'Sherlock Holmes',
...     'address': {
...             'street': '221B Baker Street',
...             'city': 'London',
...             'country': 'UK'
...     }
... }
>>> sherlock = DictWrapper(sherlock_dict)
>>> sherlock.address.country = 'United Kingdom'
>>> print(sherlock.to_json(pretty=True))
{
    "name": "Sherlock Holmes",
    "address": {
        "street": "221B Baker Street",
        "city": "London",
        "country": "United Kingdom"
    }
}

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

python-dict-wrapper-0.5.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_dict_wrapper-0.5-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file python-dict-wrapper-0.5.tar.gz.

File metadata

  • Download URL: python-dict-wrapper-0.5.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for python-dict-wrapper-0.5.tar.gz
Algorithm Hash digest
SHA256 d3a24aec59a6bd77e9908ee24ce0b226e95c937d7b81160233e8d73bf2aebfc1
MD5 16f5a4d4cfb0707bb11c634376ebc1f5
BLAKE2b-256 4ecac59b8d13bd4bf8a7e90f8f731e81b45abaede41d87dbf191eae54a60a16e

See more details on using hashes here.

File details

Details for the file python_dict_wrapper-0.5-py3-none-any.whl.

File metadata

  • Download URL: python_dict_wrapper-0.5-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for python_dict_wrapper-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b8aac330313ad9477394c3227b44254d198d3802aa5d74f5d2c468932cabbab3
MD5 42cfde738e883ca95d0b67fde212a9e3
BLAKE2b-256 0bd4595f6bfadcec85dbbdfd48a74babdce5d9ba8ba1a04afdc0aba08c4957e6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page