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 optional chaining, type casting, safe indexing and default values.
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}
]
})
# dicts
print(jdict.ensure("name", str)) # John
print(jdict.ensure("age", int)) # 30
print(jdict.ensure("age", str)) # ""
print(jdict.ensureCast("age", str)) # "30"
print(jdict.optionalGet("age", str)) # None
print(jdict.optionalCast("age", str)) # "30"
print(jdict.optionalGet("gender", str)) # None
print(jdict.optionalCast("gender", str)) # None
print(jdict.ensure("gender", str)) # ""
# lists
cars = jdict.ensureCast("cars", JList)
print(cars.assertGet(1, dict)) # {'model': 'Ford Edge', 'mpg': 24.1}
print(cars.assertGet(2, dict)) # AssertionError
# iterators
for car in cars.iterator().ensureCast(JDict):
print(car.ensureCast("model", str)) # BMW 230, Ford Edge
# chaining
chain = jdict.chain()
print(chain.ensureCast("cars.[1].mpg", str)) # "24.1"
print(chain.ensureCast("cars.[2].mpg", str)) # ""
# or via direct access (returns Optional[Any]!)
print(chain["cars.[2].mpg"]) # IndexError
print(chain["cars.[2]?.mpg"]) # None
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.10.2.tar.gz
(21.7 kB
view hashes)
Built Distribution
pyaddict-0.10.2-py3-none-any.whl
(17.7 kB
view hashes)
Close
Hashes for pyaddict-0.10.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df4cece3d6eafb78873beaedb6175271aa22aebb795b7626880b6a446df3ae99 |
|
MD5 | d8f1970f1900c208b70469c90d0f7f3d |
|
BLAKE2b-256 | 5a1b58a33dc19b2ba9654fdc721a2aa1daa0f6b00485370297b4c7532267d5bc |