Simple Models for Python
Project description
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 of type classes whatever)
Support for field validation
Convert 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
pip install pysimplemodel
How to use
from simple_model import Model
from simple_model.exceptions import ValidationError
class Person(Model):
fields = ('name', 'age', 'height', 'weight')
allow_empty = ('height', 'weight')
def validate_age(self, value):
if 0 > value > 150:
raise ValidationError
def validate_height(self, value):
if value <= 0:
raise ValidationError
>> person = Person(name='John Doe', age=18)
>> person.name
'John Doe'
>> person.validate()
>> person.as_dict()
{'name': 'John Doe', 'age': 18, 'height': '', 'weight': ''}
Validation
Model values aren’t validated until the validated method is called:
>> person = Person() # no exception
>> person.validate()
...
EmptyField: name field cannot be empty
>> person = Person(name='Jane Doe', age=60)
>> person.validate() # now it's ok!
You may change the validate method to return a boolean instead of raising an exception:
>> person = Person()
>> person.validate(raise_exception=False)
False
>>> person = Person(name='Jane Doe', age=60)
>>> person.validate(raise_exception=False)
True
Cleaning
Sometimes it is necessary to clean some values of your models, this can be easily done using simple-model:
class CleanPerson(Model):
fields = ('name', 'age')
def clean_name(self, value):
return value.strip()
def clean_age(self, value):
return int(value)
>> person = CleanPerson(name='John Doe \n', age='10')
>> person.name, person.age
('John Doe \n', '10')
>> person.clean()
>> person.name, person.age
('John Doe', 10)
Conversion to Dict
To convert to dict is pretty straight-forward task:
>> person = Person(name='Jane Doe', age=60)
>> person.as_dict()
{'age': 60, 'height': None, 'name': 'Jane Doe', 'weight': None}
You may also hide some fields from the returned dict by passing a list to the as_dict method:
>> person.as_dict(exclude_fields=('weight', 'age'))
{'height': None, 'name': 'Jane Doe'}
Simple model also supports nested models:
class SocialPerson(Model):
fields = ('name', 'friend')
>> person = Person(name='Jane Doe', age=60)
>> other_person = SocialPerson(name='John Doe', friend=person)
>> other_person.as_dict()
{'friend': {'age': 60, 'height': None, 'name': 'Jane Doe', 'weight': None}, 'name': 'John Doe'}
It also supports nested models as lists:
class MoreSocialPerson(Model):
fields = ('name', 'friends')
>> person = Person(name='Jane Doe', age=60)
>> other_person = Person(name='John Doe', age=15)
>> social_person = MoreSocialPerson(name='Foo Bar', friends=[person, other_person])
{
'name': 'Foo Bar',
'friends': [
{
'age': 60,
'height': None,
'name': 'Jane Doe',
'weight': None
},
{
'age': 15,
'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
File details
Details for the file pysimplemodel-0.7.0.tar.gz
.
File metadata
- Download URL: pysimplemodel-0.7.0.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 956c97ef7348504d5767b9699bd3a06bf702e2b273f689c807b8ad4e2dbe9fae |
|
MD5 | 9e85bbc1e08527d9ab34a08a7467aeab |
|
BLAKE2b-256 | df174adabad6b99588a0113e71c14042023df0560f4919c4b0992d28a51add63 |