Skip to main content

A Python library for building custom document object models (DOMs) with built-in JSON schema checking

Project description

Python application docs codecov

A Python library for building custom document object models (DOMs) with built-in JSON schema checking.

Motivation

A common requirement in Python projects is to be able to load serialized structured data into Python objects. Typically, you might begin by using dictionaries to store this data and a library like json or PyYAML to parse the input.

However, this simple approach may create additional work elsewhere in your project, for the following four reasons.

First, the serialized data you are loading might be user-generated, untrusted strings, and so some validation is necessary to check that the document schema matches what you are expecting.

Second, you may want to provide some promises to the rest of your application about the attributes that exist in your data object and what their data types are. For example, given a data object config which holds your application’s configuration parameters, it is preferable to know that config.allow_nulls is a bool, as opposed to the subscript access style config[‘allow_nulls’] which may return a KeyError or an unexpected data type. This also enables your data objects to be covered by static type checking.

Third, you may want to add some additional functionality to your data objects in the form of properties and methods, including within the substructure of your data object. For example, a method config.database.test_connection().

Fourth, you may want to pass some sub-elements of your data object to components in your application, but have that sub-element retain awareness of the whole document. For example, a class instance db_service = DatabaseService(config.database) might need to access some attributes from the parent object config.

What wysdom does

With wysdom, you have one simple declarative way of defining the class structure of your data objects. You can then instantiate a data object using raw data in dict form, or directly deserialize it using one of wysdom’s mixin classes.

Objects created by wysdom retain an awareness of the overall Document Object Model (DOM), which can be queried using the supplied functions document(obj), parent(obj) and key(obj).

Classes created by wysdom also auto-generate a JSON object schema and use the jsonschema library to enable you to validate potential input.

Example Usage

User class definition:

from wysdom import UserObject, UserProperty, SchemaArray

class Address(UserObject):
    first_line = UserProperty(str)
    second_line = UserProperty(str)
    city = UserProperty(str)
    postal_code = UserProperty(int)

class Person(UserObject):
    first_name = UserProperty(str)
    last_name = UserProperty(str)
    current_address = UserProperty(Address)
    previous_addresses = UserProperty(SchemaArray(Address))

Loading from dict:

person_instance = Person({
  "first_name": "Marge",
  "last_name": "Simpson",
  "current_address": {
    "first_line": "123 Fake Street",
    "second_line": "",
    "city": "Springfield",
    "postal_code": 58008
  },
  "previous_addresses": [{
    "first_line": "742 Evergreen Terrace",
    "second_line": "",
    "city": "Springfield",
    "postal_code": 58008
  }]
})

Reading attributes:

>>> person_instance.last_name
'Simpson'

>>> person_instance.current_address.first_line
'123 Fake Street'

>>> person_instance.previous_addresses[0].first_line
'742 Evergreen Terrace'

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

wysdom-0.1.0.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

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

wysdom-0.1.0-py3-none-any.whl (23.4 kB view details)

Uploaded Python 3

File details

Details for the file wysdom-0.1.0.tar.gz.

File metadata

  • Download URL: wysdom-0.1.0.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for wysdom-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4c853a3e31047c88a04990bcedb9f094076ab2edd30f04b3f233220f2c60f07f
MD5 4c8cf5925197630d1c0c4e5ef446991e
BLAKE2b-256 f93f3ca346775b473f8295dbe6f6fd130a750e51556b0a35143dd58e142ea002

See more details on using hashes here.

File details

Details for the file wysdom-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: wysdom-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 23.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for wysdom-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 243f991e720aecf1a9da3a9f33a07ec814185978c871383bebdb5ae1b84bef19
MD5 788b294eed09a624940022fe4e5ed593
BLAKE2b-256 458f81c0d6cad1116c603afb1b117ea95310a01ada6eb4b777218b53429867e4

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