pyjo
Python JSON Objects
Easily specify and (de)serialize data models in Python.
Install
pip install pyjo
How to use
First you need to create a specification of your models and attributes by using the Model and the Field classes:
from pyjo import Model, Field, RangeField, EnumField
class Gender(Enum):
female = 0
male = 1
class Address(Model):
city = Field(type=str)
postal_code = Field(type=int)
address = Field()
class User(Model):
name = Field(type=str, repr=True, required=True)
age = RangeField(min=18, max=120)
# equivalent to: Field(type=int, validator=lambda x: 18 <= x <= 120)
gender = EnumField(enum=Gender)
address = Field(type=Address)
By default any field is considered optional unless specified with required attribute. If required, its presence will be checked during initialization.
u = User()
# ...
# pyjo.exceptions.RequiredField: Field 'name' is required
User(name='john', age=18, address=Address(city='NYC'))
# <User(name=john)>
When the type argument is provided, the type of the value assigned to a field will be checked during initialization and assignment:
User(name=123)
# ...
# pyjo.exceptions.InvalidType: The value of the field 'name' is not of type str, given 123
u.address.city = 1
# ...
# pyjo.exceptions.InvalidType: The value of the field 'city' is not of type str, given 1
In order to serialize a model you need to call to_dict:
u = User(name='john', age=18, address=Address(city='NYC'))
print(u.to_dict())
# {
# "name": "john",
# "age": 18,
# "address": {
# "city": "NYC"
# }
# }
To create a model starting from its python dictionary representation, use from_dict:
u = User.from_dict({
"name": "john",
"gender": "male",
"age": 18,
"address": {
"city": "NYC"
}
})
print(u)
# <User(name=john)>
print(u.gender)
# Gender.male
print(u.address.city)
# NYC
Note that from_dict will also recreate all the nested pyjo objects.
API Documentation
Field
The Field object has several optional arguments:
typespecifies the type of the field. If specified, the type will be checked during initialization and assignmentdefaultspecifies the default value for the field. The value of a default can be a function and it will be computed and assigned during the initialization of the objectrequiredboolean flag to indicate if the field must be specified and can't beNone, even during assignment (Falseby default)reprboolean flag to indicate if the field/value should be shown in the Python representation of the object, when printed (Falseby default)to_dict,from_dict(functions) to add ad-hoc serialization/deserialization for the fieldvalidator(function) function that gets called to validate the input (after cast) of a fieldcast(function) cast value of the field (if not empty) before (validation and) assignment
Model
to_dict(),from_dict()serialize/deserialize to/from python dictionariesto_json(),from_json()shortcuts forjson.dumps(model.to_dict())andModel.from_dict(json.loads(<dict>))
Field subclasses
You can easily create subclasses of Field to handle specific types of objects. Several of them are already integrated in pyjo and more are coming (feel free to create a PR to add more):
ListFieldfor fields containing a list of elementsRegexFieldfor fields containing a string that matches a given regexRangeFieldfor fields containing a int with optional minimum/maximum valueDatetimeFieldfor fields containing a UTC datetime
Extensions
- pyjo_mongo easily interact with MongoDB documents, a lightweight replacement of the
mongoenginelibrary
Release files for pyjo 3.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyjo-3.3.0.tar.gz | 10.7 kB | Details |
Release files / pyjo-3.3.0.tar.gz
| Download URL | pyjo-3.3.0.tar.gz |
|---|---|
| Size | 10.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f92fd05544902ac6fbcc8eb90e3a2ddc015dc64ad045a7fe28b4cffa59360745
|
|
BLAKE2b-256 checksum How to use checksums |
ed5802231b697319a02adbf35cbeb415cc8726100bfd56ff37b2413e2111d2c1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.1 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.7
|