Skip to main content

yet another dict library

Project description

pyaddict

PyPI version PyPI pyversions PyPI license

Description

Yet another python library to safely work with json data. It implements many useful features such as default values, type casting and safe indexing.

Installation

pip install pyaddict

Usage

from pyaddict import JDict, JList

jdict = JDict({
    "name": "John",
    "age": 30,
    "cars": [
        {"model": "BMW 230", "mpg": 27.5},
        {"model": "Ford Edge", "mpg": 24.1}
    ]
})

# Get value
print(jdict.ensureCast("name", str))  # John
print(jdict.ensureCast("age", int))  # 30
print(jdict.ensureCast("age", str))  # "30"
print(jdict.optionalGet("age", str)) # None
print(jdict.tryGet("age", str))  # "30"
print(jdict.optionalGet("gender", str)) # None
print(jdict.tryGet("gender", str)) # None
print(jdict.ensure("gender", str)) # ""

cars = jdict.ensureCast("cars", JList)
print(cars.ensureCast(0, JDict).ensureCast("mpg", str))  # "27.5"
print(cars.ensureCast(1, JDict).ensureCast("mpg", str))  # "24.1"
print(cars.ensureCast(2, JDict).ensureCast("mpg", str))  # ""

print(cars.ensureCast(2, JDict).optionalGet("mpg", str))  # None
print(cars.assertGet(2, JDict))  # AssertionError

The library is fully typed and thus can be used with mypy & pylint. Check out the wiki for more information.

When to use

When working with json data, it is common to have to deal with missing keys, wrong types, etc. This library provides a simple way to deal with these issues. Additionally, it provides easy-to-use typing support for mypy and pylint and detailed documentation.

License

MIT

Author

dxstiny

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

pyaddict-0.9.1.tar.gz (17.4 kB view hashes)

Uploaded Source

Built Distribution

pyaddict-0.9.1-py3-none-any.whl (15.3 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