Skip to main content

The Python dictionary for humans dealing with evil/complex data.

Project description

Build Status codecov Codacy Requirements Status PyPI version PyPI downloads Py versions License

python-benedict

The Python dictionary for humans dealing with evil/complex data.

Features

  • Full keypath support (using the dot syntax)

  • Many utility and parse methods to retrieve data as needed (all methods listed below)

  • Give benediction to dict objects before they are returned (they receive benedict casting)

  • 100% backward-compatible (you can replace existing dicts without pain)

Requirements

  • Python 2.7, 3.4, 3.5, 3.6, 3.7

Installation

  • Run pip install python-benedict

Testing

  • Run tox / python setup.py test

Usage

benedict is a dict subclass, so it is possible to use it as a normal dict (you can just cast an existing dict).

Basic get/set using keypath

from benedict import benedict

d = benedict()
d['profile.firstname'] = 'Fabio'
d['profile.lastname'] = 'Caccamo'
print(d) # -> { 'profile':{ 'firstname':'Fabio', 'lastname':'Caccamo' } }
print(d['profile']) # -> { 'firstname':'Fabio', 'lastname':'Caccamo' }
print('profile.lastname' in d) # -> True

API

# Clean the current dict removing empty values: None, '', {}, [], ().
# If strings, dicts or lists args are set to False, related empty values will be ignored.
d.clean(strings=True, dicts=True, lists=True)
# Return a deepcopy of the dict.
d.deepcopy()
# Return a readable representation of any dict/list.
s = benedict.dump(d.keypaths())
print(s)
# Return a readable representation of the dict for the given key (optional).
s = d.dump_items(key=None)
print(s)
# Get value by key or keypath trying to return it as bool.
# Values like `1`, `true`, `yes`, `on`, `ok` will be returned as `True`.
d.get_bool(key, default=False)
# Get value by key or keypath trying to return it as list of bool values.
# If separator is specified and value is a string it will be splitted.
d.get_bool_list(key, default=[], separator=',')
# Get value by key or keypath trying to return it as datetime.
# If format is not specified it will be autodetected.
# If options and value is in options return value otherwise default.
d.get_datetime(key, default=None, format=None, options=[])
# Get value by key or keypath trying to return it as list of datetime values.
# If separator is specified and value is a string it will be splitted.
d.get_datetime_list(key, default=[], format=None, separator=',')
# Get value by key or keypath trying to return it as Decimal.
# If options and value is in options return value otherwise default.
d.get_decimal(key, default=Decimal('0.0'), options=[])
# Get value by key or keypath trying to return it as list of Decimal values.
# If separator is specified and value is a string it will be splitted.
d.get_decimal_list(key, default=[], separator=',')
# Get value by key or keypath trying to return it as dict.
# If value is a json string it will be automatically decoded.
d.get_dict(key, default={})
# Get email by key or keypath and return it.
# If value is blacklisted it will be automatically ignored.
# If check_blacklist is False, it will be not ignored even if blacklisted.
d.get_email(key, default='', options=None, check_blacklist=True)
# Get value by key or keypath trying to return it as float.
# If options and value is in options return value otherwise default.
d.get_float(key, default=0.0, options=[])
# Get value by key or keypath trying to return it as list of float values.
# If separator is specified and value is a string it will be splitted.
d.get_float_list(key, default=[], separator=',')
# Get value by key or keypath trying to return it as int.
# If options and value is in options return value otherwise default.
d.get_int(key, default=0, options=[])
# Get value by key or keypath trying to return it as list of int values.
# If separator is specified and value is a string it will be splitted.
d.get_int_list(key, default=[], separator=',')
# Get value by key or keypath trying to return it as list.
# If separator is specified and value is a string it will be splitted.
d.get_list(key, default=[], separator=',')
# Get list by key or keypath and return value at the specified index.
# If separator is specified and list value is a string it will be splitted.
d.get_list_item(key, index=0, default=None, separator=',')
# Get phone number by key or keypath and return a dict with different formats (e164, international, national).
# If phone number doesn't include country code, country should be specified to parse it correctly.
d.get_phonenumber(key, country=None, default=None)
# Get value by key or keypath trying to return it as slug.
# If options and value is in options return value otherwise default.
d.get_slug(key, default='', options=[])
# Get value by key or keypath trying to return it as list of slug values.
# If separator is specified and value is a string it will be splitted.
d.get_slug_list(key, default=[], separator=',')
# Get value by key or keypath trying to return it as string.
# Encoding issues will be automatically fixed.
# If options and value is in options return value otherwise default.
d.get_str(key, default='', options=[])
# Get value by key or keypath trying to return it as list of str values.
# If separator is specified and value is a string it will be splitted.
d.get_str_list(key, default=[], separator=',')
# Return a list of all keypaths in the dict.
d.keypaths()

License

Released under MIT License.

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-benedict-0.4.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

python_benedict-0.4.0-py2-none-any.whl (11.4 kB view details)

Uploaded Python 2

File details

Details for the file python-benedict-0.4.0.tar.gz.

File metadata

  • Download URL: python-benedict-0.4.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.3

File hashes

Hashes for python-benedict-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9d3c27d81d358873fcd2b2ee34d8d81e593bb68d3adaa95d36e66f9647d6a5a6
MD5 03851309610e727d05291a59192981dc
BLAKE2b-256 56f0e43b6d8f55f2ec3a4800ebe273ebeb25135c6730beb8b3e3c76d371c927a

See more details on using hashes here.

File details

Details for the file python_benedict-0.4.0-py2-none-any.whl.

File metadata

  • Download URL: python_benedict-0.4.0-py2-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.3

File hashes

Hashes for python_benedict-0.4.0-py2-none-any.whl
Algorithm Hash digest
SHA256 709f6ff033cc9331472362f710ca92017763b823c2ec09a8a9f556863f38764b
MD5 2181e3ce00cee6cb071fce3206386b98
BLAKE2b-256 490a1c0bf52d7933851dae875c1d44cc64267cdfc20df95e98e01433dcf8f279

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