simple model: data handling made easy¶
simple model offers a simple way to work with data. It is very common to use lists and dict to handle data, but your code can get ugly very easily when building larger applications.
It allows you to easily define models using classes and perform common tasks such as data cleaning, validation, type conversion and much more.
this lib has simple objectives:
- Define models easily (support type hints)
- Perform validation, cleaning, default values and type conversion
- Convert models to dict
basic example¶
To define your models is as simple as stated in the following example:
from simple_model import Model
from simple_model.exceptions import ValidationError
class Person(Model):
age: int
is_active: bool = False
name: str
def validate_age(self, age):
if age < 0 or age > 150:
raise ValidationError('Invalid age')
return age
def validate_name(self, name):
if len(name) == 0:
raise ValidationError('Invalid name')
return name.strip()
>>> person = Person(name='John Doe', age=18)
>>> person.name
'John Doe'
>> print(person.is_active)
False
>>> person.validate()
>>> dict(person)
{'name': 'John Doe', 'age': 18, is_active: False}
>> other_person = Person(name='', age=44)
>> other_person.validate()
Traceback (most recent call last):
...
ValidationError: Invalid name
If you want to understand better other simple model features consult the following pages simple model’s docs.
More Docs¶
Customizing the fields on your model¶
Models are used to manage business logic, data and rules of applications. Data is represented by model classes and its fields. The easiest way to define fields using simple model is by using type hints syntax:
from decimal import Decimal
from simple_model import Model
class Category(Model):
name: str
is_active: bool = False
class Product(Model):
title: str
description: str
category: Category
price: Decimal
is_active: bool = False
In the example above we create two models Category
with name
and is_active
fields and Product
with title
, description
and
other fields.
simple model will automtically create custom initializers (__init__
methods
or constructors) that receives all the specified fields as parameters.
To create model instances just do the following:
category = Category(
name='clothing',
is_active=True,
)
product = Product(
title='Pants',
description='Pants are great',
category=category,
price=80,
is_active=True,
)
As shown in the models above it is possible to can easily customize field types by defining the class attributes using type hints. It is also possible to provide a default value for each field by setting values on each field on the model class.
Note:
Attributes and methods that starts with “__” (dunder) are considered private and not included in model.
For a better understanding, check this python underscore convention link.
Defining fields without explicit types¶
If you don’t want to enforce types on your fields it is possible to use
typing.Any
as a field type. This way simple model will ignore any type-related
feature on the declared model:
from typing import Any
from simple_model import Model
class Bag(Model):
name: Any
brand: Any
items: Any
Converting models to dict¶
To convert model to dict you should use to_dict()
from simple_model import Model
from simple_model.converters import to_dict
class Category(Model):
name: str
is_active: bool = False
category = Category(
name='clothing',
is_active=True,
)
to_dict(category)
You can convert models instances to dictionary using the method .as_dict(), you must run .validate() before conversion. This way is more recommended because you don’t need to import to_dict to convert the instance
from simple_model import Model
class Category(Model):
name: str
is_active: bool = False
category = Category(
name='clothing',
is_active=True,
)
category.validate()
category.as_dict()
Creating models instances and classes from dicts¶
TBD
Model inheritance¶
TBD
Field conversion and customizing model initialization¶
TBD (__post_init__
)
Building models and model classes dynamically¶
TBD
FAQ¶
TBD