Simple Models for Python
Project description
============
Simple Model
============
.. image:: https://travis-ci.org/lamenezes/simple-model.svg?branch=master
:target: https://travis-ci.org/lamenezes/simple-model
.. image:: https://coveralls.io/repos/github/lamenezes/simple-model/badge.svg?branch=master
:target: https://coveralls.io/github/lamenezes/simple-model?branch=master
*SimpleModel* offers a simple way to handle data using classes instead of a
plenty of lists and dicts.
It has simple objectives:
- Define your fields easily (just a tuple, not dicts or instances from type classes whatever)
- Support for field validation
- Serialize to dict
That's it. If you want something more complex there are plenty of libraries and frameworks that does a lot of cool stuff.
--------------
How to install
--------------
.. code:: shell
pip install pysimplemodel
----------
How to use
----------
.. code:: python
from simple_model import Model
from simple_model.exceptions import ValidationError
class Person(Model):
fields = ('name', 'age', 'gender', 'height', 'weight')
allow_empty = ('height', 'weight')
def validate_age(self, value):
if 0 > value > 150:
raise ValidationError
def validate_gender(self, value):
if value not in ('M', 'F'):
raise ValidationError
.. code:: python
>> person = Person(name='John Doe', age=18, gender='M')
>> person.name
'John Doe'
>> person.validate()
>> person.serialize()
{'name': 'John Doe', 'age': 18, 'gender': 'M', 'height': '', 'weight': ''}
Validation
----------
Model values aren't validated until the `validated` method is called:
.. code:: python
>> person = Person() # no exception
>> person.validate()
...
EmptyField: name field cannot be empty
>> person = Person(name='Jane Doe', age=60, gender='F')
>> person.validate() # now it's ok!
You may change the validate method to return a boolean instead of raising an exception:
.. code:: python
>> person = Person()
>> person.validate(raise_exception=False)
False
>>> person = Person(name='Jane Doe', age=60, gender='F')
>>> person.validate(raise_exception=False)
True
Custom validation can make your simple models awesome:
.. code:: python
Serialization
-------------
Simple serialization is pretty straight-forward:
.. code:: python
>> person = Person(name='Jane Doe', age=60, gender='F')
>> person.serialize()
{'age': 60, 'gender': 'F', 'height': None, 'name': 'Jane Doe', 'weight': None}
You may also hide some fields from serialization by passing a list to the `serialize` method:
.. code:: python
>> person.serialize(exclude_fields=('gender', 'weight'))
{'age': 60, 'height': None, 'name': 'Jane Doe'}
Simple model also supports nested models:
.. code:: python
class SocialPerson(Model):
fields = ('name', 'friend')
>> person = Person(name='Jane Doe', age=60, gender='F')
>> other_person = SocialPerson(name='John Doe', friend=person)
>> other_person.serialize()
{'friend': {'age': 60, 'gender': 'F', 'height': None, 'name': 'Jane Doe', 'weight': None}, 'name': 'John Doe'}
It also supports nested models as lists:
.. code:: python
class MoreSocialPerson(Model):
fields = ('name', 'friends')
>> person = Person(name='Jane Doe', age=60, gender='F')
>> other_person = Person(name='John Doe', age=15, gender='M')
>> social_person = MoreSocialPerson(name='Foo Bar', friends=[person, other_person])
{
'name': 'Foo Bar',
'friends': [
{
'age': 60,
'gender': 'F',
'height': None,
'name': 'Jane Doe',
'weight': None
},
{
'age': 15,
'gender': 'M',
'height': None,
'name': 'John Doe',
'weight': None
}
]
}
Simple Model
============
.. image:: https://travis-ci.org/lamenezes/simple-model.svg?branch=master
:target: https://travis-ci.org/lamenezes/simple-model
.. image:: https://coveralls.io/repos/github/lamenezes/simple-model/badge.svg?branch=master
:target: https://coveralls.io/github/lamenezes/simple-model?branch=master
*SimpleModel* offers a simple way to handle data using classes instead of a
plenty of lists and dicts.
It has simple objectives:
- Define your fields easily (just a tuple, not dicts or instances from type classes whatever)
- Support for field validation
- Serialize to dict
That's it. If you want something more complex there are plenty of libraries and frameworks that does a lot of cool stuff.
--------------
How to install
--------------
.. code:: shell
pip install pysimplemodel
----------
How to use
----------
.. code:: python
from simple_model import Model
from simple_model.exceptions import ValidationError
class Person(Model):
fields = ('name', 'age', 'gender', 'height', 'weight')
allow_empty = ('height', 'weight')
def validate_age(self, value):
if 0 > value > 150:
raise ValidationError
def validate_gender(self, value):
if value not in ('M', 'F'):
raise ValidationError
.. code:: python
>> person = Person(name='John Doe', age=18, gender='M')
>> person.name
'John Doe'
>> person.validate()
>> person.serialize()
{'name': 'John Doe', 'age': 18, 'gender': 'M', 'height': '', 'weight': ''}
Validation
----------
Model values aren't validated until the `validated` method is called:
.. code:: python
>> person = Person() # no exception
>> person.validate()
...
EmptyField: name field cannot be empty
>> person = Person(name='Jane Doe', age=60, gender='F')
>> person.validate() # now it's ok!
You may change the validate method to return a boolean instead of raising an exception:
.. code:: python
>> person = Person()
>> person.validate(raise_exception=False)
False
>>> person = Person(name='Jane Doe', age=60, gender='F')
>>> person.validate(raise_exception=False)
True
Custom validation can make your simple models awesome:
.. code:: python
Serialization
-------------
Simple serialization is pretty straight-forward:
.. code:: python
>> person = Person(name='Jane Doe', age=60, gender='F')
>> person.serialize()
{'age': 60, 'gender': 'F', 'height': None, 'name': 'Jane Doe', 'weight': None}
You may also hide some fields from serialization by passing a list to the `serialize` method:
.. code:: python
>> person.serialize(exclude_fields=('gender', 'weight'))
{'age': 60, 'height': None, 'name': 'Jane Doe'}
Simple model also supports nested models:
.. code:: python
class SocialPerson(Model):
fields = ('name', 'friend')
>> person = Person(name='Jane Doe', age=60, gender='F')
>> other_person = SocialPerson(name='John Doe', friend=person)
>> other_person.serialize()
{'friend': {'age': 60, 'gender': 'F', 'height': None, 'name': 'Jane Doe', 'weight': None}, 'name': 'John Doe'}
It also supports nested models as lists:
.. code:: python
class MoreSocialPerson(Model):
fields = ('name', 'friends')
>> person = Person(name='Jane Doe', age=60, gender='F')
>> other_person = Person(name='John Doe', age=15, gender='M')
>> social_person = MoreSocialPerson(name='Foo Bar', friends=[person, other_person])
{
'name': 'Foo Bar',
'friends': [
{
'age': 60,
'gender': 'F',
'height': None,
'name': 'Jane Doe',
'weight': None
},
{
'age': 15,
'gender': 'M',
'height': None,
'name': 'John Doe',
'weight': None
}
]
}
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
pysimplemodel-0.1.1.tar.gz
(3.8 kB
view details)
File details
Details for the file pysimplemodel-0.1.1.tar.gz
.
File metadata
- Download URL: pysimplemodel-0.1.1.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23e42c9201649eb1591aaee459b71b65a95fd5622e550ac86caf00ef08d5f417 |
|
MD5 | 694a49062d0a3b50926683fbc1b8790e |
|
BLAKE2b-256 | b73920b9754839a023ab2b4c39ab653c44b61b8589e9e51050cc14668ff8e79d |