Skip to main content

very simple model framework

Project description

https://travis-ci.org/AFriemann/simple_model.svg?branch=master https://badge.fury.io/py/simple_model.svg

As the name suggests, this is a very simple model framework. It can be used for data validation and (de-)serialization.

Installation

Install with pip:

$ pip install --user simple_model

Usage

This allows me to test the examples by taking care of sorting the dictionaries, it is not required for simple_model to work:

>>> from pprint import pprint

Examples:

>>> from simple_model import Model, Attribute

>>> class Data(Model):
...     name = Attribute(str)
...     some_value = Attribute(str, optional=True)
...     another_value = Attribute(int, fallback=0)

>>> pprint(dict(Data(name = 'test', some_value = None, another_value = 12)))
{'another_value': 12, 'name': 'test', 'some_value': None}

>>> pprint(dict(Data(name = 'test')))
{'another_value': 0, 'name': 'test', 'some_value': None}

>>> init_dict = {'name': 'test', 'some_value': 'val', 'another_value': 3}
>>> pprint(dict(Data(**init_dict)))
{'another_value': 3, 'name': 'test', 'some_value': 'val'}

Initializing with missing attributes while not specifying them as optional or providing a fallback value will result in a ValueError. Note that fallback takes precedence over optional, specifying both is unnecessary.

Unknown values will be ignored:

>>> pprint(dict(Data(name = 'test', unknown_value = True)))
{'another_value': 0, 'name': 'test', 'some_value': None}

Serialization can be achieved easily, for example:

>>> import json
>>> def serialize(model):
...     return json.dumps(dict(model))

>>> def deserialize(string):
...     return Data(**json.loads(string))

Since the Model class simply calls the Attribute class for each parameter and the Attribute class in turn calls the given ‘type’, one could easily use functions instead of types to achieve more complex results and value parsing:

>>> from datetime import datetime
>>> def parse_date(string):
...     return datetime.strptime(string, '%Y-%m-%d')

>>> class Data(Model):
...     date = Attribute(parse_date)

>>> dict(Data(date='2015-11-20'))
{'date': datetime.datetime(2015, 11, 20, 0, 0)}

Fallback values can also be given as functions

>>> def fun():
...     return "foo"

>>> class Data(Model):
...     point = Attribute(str, fallback=fun)

>>> dict(Data())
{'point': 'foo'}

If you need to verify Lists of objects, use functions:

>>> class Data(Model):
...     points = Attribute(lambda l: list(map(str, l)))

>>> dict(Data(points=['abc', 'def', 'ghi']))
{'points': ['abc', 'def', 'ghi']}

Or the included list_type helper class:

>>> from simple_model import list_type
>>> class Data(Model):
...     points = Attribute(list_type(str))

>>> dict(Data(points=['abc', 'def', 'ghi']))
{'points': ['abc', 'def', 'ghi']}

For more complex data, use Models to verify:

>>> class SubData(Model):
...     some_value = Attribute(str)
...     some_other_value = Attribute(int)

>>> class Data(Model):
...     point = Attribute(SubData)

>>> pprint(dict(Data(point={'some_value': 'abc', 'some_other_value': 12})))
{'point': {'some_other_value': 12, 'some_value': 'abc'}}

To allow uncommon names, use the Attribute name kwarg:

>>> class Data(Model):
...     point = Attribute(str, name='@point')

>>> dict(Data(point='something'))
{'@point': 'something'}

>>> dict(Data(**{ '@point': 'something' }))
{'@point': 'something'}

Tests

To run the tests use tox:

$ tox

Or run py.test manually (not recommended, needs simple_module installed):

$ py.test .

Changelog

1.0.0

  • removed the AttributeList class, use functions instead.

  • Model Attributes can now be named. To allow this we keep the Attribute object and store the value.

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

simple_model-1.0.0.post3.tar.gz (4.7 kB view details)

Uploaded Source

File details

Details for the file simple_model-1.0.0.post3.tar.gz.

File metadata

File hashes

Hashes for simple_model-1.0.0.post3.tar.gz
Algorithm Hash digest
SHA256 dcd90ceb8df9dc76b97557bae110f8217dccab8e8c3679bcafad26cadb0ff343
MD5 0bcfa7ccd9e9833f1cbbd92333ae91b1
BLAKE2b-256 22fb73654e4d22646e6c8c210bfedba8841e67ec1f9589231dfb53200a672903

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