Skip to main content

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


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

Uploaded Source

Built Distribution

modeller-0.1.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file modeller-0.1.1.tar.gz.

File metadata

  • Download URL: modeller-0.1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for modeller-0.1.1.tar.gz
Algorithm Hash digest
SHA256 42ae688e511826c45a02f05c50dbecd3647b78c5c988a7ff97ad0c3ad58e8cf8
MD5 5a4ebfc3353c6074c22c5091465cba6b
BLAKE2b-256 d460a48392d8d95ed1c4493f6d4709e71ceab3a9e5720b990d9b32f6084daebe

See more details on using hashes here.

File details

Details for the file modeller-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: modeller-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for modeller-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eabdf9773b64d05aa4bd8f9485980cef76d42e37dab3fd225819f1c53b056911
MD5 d871c4f2284020a5952b0ca62e1f784d
BLAKE2b-256 9c428a4db7febcfedee4e0cfb6335e06a92dfb0915553b722295a1a7fd931bf8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page