very simple model framework
Project description
simple_model
============
As the name says, this is a very simple model framework. It can be used for data
verification and (de-)serialization.
Installation
------------
Install with pip::
$ pip install --user simple_model
Usage
-----
Examples::
>>> from simple_model import Model, Attribute
>>> class Data(Model):
... name = Attribute(str)
... some_value = Attribute(str, nullable=True)
... another_value = Attribute(int, fallback=0)
>>> Data(name = 'test', some_value = None, another_value = 12).__attributes__()
{ 'name': 'test', 'some_value': None, 'another_value': 12 }
>>> Data(name = 'test', _allow_missing=True).__attributes__()
{ 'name': 'test', 'some_value': None, 'another_value': 0 }
>>> Data(name = 'test', unknown_value = True, _allow_missing=True, _allow_unknown=True).__attributes__()
{ 'name': 'test', 'some_value': None, 'another_value': 0 }
>>> init_dict = {'name': 'test', 'some_value': 'val', 'another_value': 3}
>>> Data(**init_dict)
{ 'name': 'test', 'some_value': 'val', 'another_value': 3 }
Initializing with unknown or missing attributes while not specifying to allow
them will result in *TypeError*s.
Serialization can be achieved easily, for example::
>>> import json
>>> def serialize(model):
... return json.dumps(model.__attributes__())
>>> def deserialize(json):
... return Data(**json.loads(json))
============
As the name says, this is a very simple model framework. It can be used for data
verification and (de-)serialization.
Installation
------------
Install with pip::
$ pip install --user simple_model
Usage
-----
Examples::
>>> from simple_model import Model, Attribute
>>> class Data(Model):
... name = Attribute(str)
... some_value = Attribute(str, nullable=True)
... another_value = Attribute(int, fallback=0)
>>> Data(name = 'test', some_value = None, another_value = 12).__attributes__()
{ 'name': 'test', 'some_value': None, 'another_value': 12 }
>>> Data(name = 'test', _allow_missing=True).__attributes__()
{ 'name': 'test', 'some_value': None, 'another_value': 0 }
>>> Data(name = 'test', unknown_value = True, _allow_missing=True, _allow_unknown=True).__attributes__()
{ 'name': 'test', 'some_value': None, 'another_value': 0 }
>>> init_dict = {'name': 'test', 'some_value': 'val', 'another_value': 3}
>>> Data(**init_dict)
{ 'name': 'test', 'some_value': 'val', 'another_value': 3 }
Initializing with unknown or missing attributes while not specifying to allow
them will result in *TypeError*s.
Serialization can be achieved easily, for example::
>>> import json
>>> def serialize(model):
... return json.dumps(model.__attributes__())
>>> def deserialize(json):
... return Data(**json.loads(json))
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
simple_model-0.0.2.tar.gz
(2.6 kB
view details)
File details
Details for the file simple_model-0.0.2.tar.gz
.
File metadata
- Download URL: simple_model-0.0.2.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48b91af69e7efff3edb5e173a5e7c5c3ce5da1466846bbc908a92b54b5dbb71f |
|
MD5 | faaafd2df51f64c266d79009085e1844 |
|
BLAKE2b-256 | 87b66d9f32257880a9dba028c72f29348580edfacabd85957d571d7eef411ddf |