create efficent models starting from json schema
Project description
Usage
You can use inheritance to create your model:
class User(modeller.Model):
id = ''
_schema = {
'type': 'object',
'properties': {
'name': { 'type': 'string' },
'id': { 'type': 'integer' },
'age': { 'type': 'integer' },
},
'required': [
'name',
'surname',
],
# 'additionalProperties': False,
}
# model will be validated after every instance
me = User(id=01, name='Tommy', surname='Der')
# you can also add additional properties
me.state = 'Italy'
me._validate()
print(me._json())
print(me._yaml())
print(me.surname)
With the schema in types/schema.yaml
$schema: http://json-schema.org/schema#
properties:
name:
type: string
surname:
type: string
age:
type: integer
required:
- name
- surname
- age
you can load a model with automatic validation, easy attribute access with dots and no exceptions while trying to access a property defined in the schema.
import yaml
import modeler
schema = yaml.load(open('types/schema.yaml').read())
Model = modeler.make_model(schema=schema,)
Model(name='Tommaso', surname='De Rossi', age=19)
Details
Model validate itself as soon as instantiated, if you want to change this behavior overwrite _on_init
method.
Model will return None if you try to access a property present in the json schema .properties
but not in the object.
Model will throw if you try to access a property not present in the object and not in the json schema .properties
.
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
modeller-0.0.27.tar.gz
(5.7 kB
view hashes)
Built Distribution
Close
Hashes for modeller-0.0.27-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fc66952b4cc9d157e3d6dddd4e211559bade9b08bb06ee5fe0b6f321f4dc688 |
|
MD5 | 99a98eec67d1c8b2304f94c7e426f840 |
|
BLAKE2b-256 | 6a09b1cfccf254fc8ba99c8314e696e89cf0504e90076bf4462a348d4ab39bac |