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 optional chaining, schema validation, type casting, safe indexing and default values.

Installation

pip install pyaddict

Usage

from pyaddict import JDict, JList
from pyaddict.schema import Object, String, Integer, Array

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

# schema validation
schema = Object({
    "name": String(),
    "age": String().coerce(),
    "dogs": Array(String()).min(1).optional()
}).withAdditionalProperties()
print(schema.error(jdict)) # None

badSchema = Object({
    "name": String().min(5),
    "age": Float(),
    "cars": Object()
})
print(badSchema.error(jdict)) # ValidationError(expected 4 to be greater than or equal to 5, name: min)

staticSchema = Object({
    "name": "John",
    "age": 30,
    "cars": [
        {"model": "BMW 230", "mpg": 27.5},
        {"model": "Ford Edge", "mpg": 24.1}
    ]
})
print(staticSchema.error(jdict)) # None

mixedSchema = Object({
    "name": String().enum("John"),
    "age": 30,
    "dogs": Array(String()).min(1).optional()
}).withAdditionalProperties()
print(mixedSchema.error(jdict)) # 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. Starting with version 1.0.0, pyaddict includes a schema validation feature inspired by zod. It is especially useful when validating user input, e.g. in web applications.

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-1.2.2.tar.gz (27.4 kB view details)

Uploaded Source

Built Distribution

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

pyaddict-1.2.2-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

Details for the file pyaddict-1.2.2.tar.gz.

File metadata

  • Download URL: pyaddict-1.2.2.tar.gz
  • Upload date:
  • Size: 27.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for pyaddict-1.2.2.tar.gz
Algorithm Hash digest
SHA256 6a56e6aed861d5c6cf82186f4f95464eee661d82dd9fef937a997cd1a5be8e0b
MD5 e91291dc7bd22c4f92446d978b9585e7
BLAKE2b-256 135d4d59c834a26dc4dbefb38b9530c3f2e0c5f86d6a5ba747962e35ebbb5899

See more details on using hashes here.

File details

Details for the file pyaddict-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: pyaddict-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 23.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for pyaddict-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5c4cad577d0737722d7af718560d5de4354753b8ab0168551ef070cb99f2d7df
MD5 85a73cdea24f2f964a4bf832b6e4589a
BLAKE2b-256 889b1b0fa69d35dc0931b51659120e88ba0a37a8b12f4e64a0627e5017d334c4

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