yet another dict library
Project description
pyaddict
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
Author
Project details
Release history Release notifications | RSS feed
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.0.tar.gz
(17.3 kB
view hashes)
Built Distribution
pyaddict-0.9.0-py3-none-any.whl
(15.2 kB
view hashes)