• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

docs/H04-Apr-2018-2,0141,253

examples/H04-Apr-2018-549359

schematics/H04-Apr-2018-3,1652,381

tests/H04-Apr-2018-3,8322,690

.gitignoreH A D04-Apr-2018176 2423

.pylintrcH A D04-Apr-20189.8 KiB337226

.travis.ymlH A D04-Apr-2018161 107

HISTORY.rstH A D04-Apr-20189.1 KiB120102

LICENSEH A D04-Apr-20181.5 KiB3123

MANIFEST.inH A D04-Apr-2018116 87

README.rstH A D04-Apr-20183 KiB9365

setup.cfgH A D04-Apr-2018170 75

setup.pyH A D04-Apr-20181.7 KiB5745

tox.iniH A D04-Apr-2018341 1714

README.rst

1==========
2Schematics
3==========
4
5.. rubric:: Python Data Structures for Humans™.
6
7.. image:: https://secure.travis-ci.org/schematics/schematics.png?branch=master
8  :target: https://secure.travis-ci.org/schematics/schematics
9  :alt: Build Status
10
11.. image:: https://coveralls.io/repos/schematics/schematics/badge.png
12  :target: https://coveralls.io/r/schematics/schematics
13  :alt: Coverage
14
15**For more information, please see our documentation:** http://schematics.readthedocs.org/en/latest/
16
17
18About
19=====
20
21Schematics is a Python library to combine types into structures, validate them,
22and transform the shapes of your data based on simple descriptions.
23
24The internals are similar to ORM type systems, but there is no database layer
25in Schematics.  Instead, we believe that building a database
26layer is made significantly easier when Schematics handles everything but
27writing the query.
28
29Further, it can be used for a range of tasks where having a database involved
30may not make sense.
31
32Some common use cases:
33
34+ Design and document specific `data structures <https://schematics.readthedocs.org/en/latest/usage/models.html>`_
35+ `Convert structures <https://schematics.readthedocs.org/en/latest/usage/exporting.html#converting-data>`_ to and from different formats such as JSON or MsgPack
36+ `Validate <https://schematics.readthedocs.org/en/latest/usage/validation.html>`_ API inputs
37+ `Remove fields based on access rights <https://schematics.readthedocs.org/en/latest/usage/exporting.html>`_ of some data's recipient
38+ Define message formats for communications protocols, like an RPC
39+ Custom `persistence layers <https://schematics.readthedocs.org/en/latest/usage/models.html#model-configuration>`_
40
41
42Example
43=======
44
45This is a simple Model. ::
46
47  >>> from schematics.models import Model
48  >>> from schematics.types import StringType, URLType
49  >>> class Person(Model):
50  ...     name = StringType(required=True)
51  ...     website = URLType()
52  ...
53  >>> person = Person({'name': u'Joe Strummer',
54  ...                  'website': 'http://soundcloud.com/joestrummer'})
55  >>> person.name
56  u'Joe Strummer'
57
58Serializing the data to JSON. ::
59
60  >>> import json
61  >>> json.dumps(person.to_primitive())
62  {"name": "Joe Strummer", "website": "http://soundcloud.com/joestrummer"}
63
64Let's try validating without a name value, since it's required. ::
65
66  >>> person = Person()
67  >>> person.website = 'http://www.amontobin.com/'
68  >>> person.validate()
69  Traceback (most recent call last):
70    File "<stdin>", line 1, in <module>
71    File "schematics/models.py", line 231, in validate
72      raise ModelValidationError(e.messages)
73  schematics.exceptions.ModelValidationError: {'name': [u'This field is required.']}
74
75Add the field and validation passes::
76
77  >>> person = Person()
78  >>> person.name = 'Amon Tobin'
79  >>> person.website = 'http://www.amontobin.com/'
80  >>> person.validate()
81  >>>
82
83
84.. _coverage:
85
86Testing & Coverage support
87==========================
88
89Run coverage and check the missing statements. ::
90
91  $ `coverage run --source schematics -m py.test && coverage report`
92
93