Skip to main content

Simple Models for Python

Project description

https://travis-ci.org/lamenezes/simple-model.svg?branch=master https://coveralls.io/repos/github/lamenezes/simple-model/badge.svg?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 of 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

pip install pysimplemodel

How to use

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
>> 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:

>> 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:

>> person = Person()
>> person.validate(raise_exception=False)
False
>>> person = Person(name='Jane Doe', age=60, gender='F')
>>> 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', 'gender')

    def clean_name(self, value):
        return value.strip()

    def clean_gender(self, value):
        return value.upper()

>> person = CleanPerson(name='John Doe  \n', gender='m')
>> person.name, person.gender
('John Doe  \n', 'm')
>> person.clean()
>> person.name, person.gender
('John Doe', 'M')

Serialization

Simple serialization is pretty straight-forward:

>> 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:

>> person.serialize(exclude_fields=('gender', 'weight'))
{'age': 60, 'height': None, 'name': 'Jane Doe'}

Simple model also supports nested models:

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:

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


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

Uploaded Source

File details

Details for the file pysimplemodel-0.1.4.tar.gz.

File metadata

File hashes

Hashes for pysimplemodel-0.1.4.tar.gz
Algorithm Hash digest
SHA256 5c0f8ec7c331bbe0c98d81f43e2473b29357e45bbdcb3e0fc2ad2899c1391040
MD5 8279927b94f2f93d70bc860d3e790e3b
BLAKE2b-256 6f87266c6927522f55792ada9c2116bbd566c7088b88199072eb366b9bbd90c9

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