{ "info": { "author": "UNKNOWN", "author_email": "UNKNOWN", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python", "Programming Language :: Python :: Implementation :: CPython" ], "description": "tinyschema\n========================================\n\nfeatures\n\n- schema definition\n- schema validation\n- data validation\n\nschema definition\n----------------------------------------\n\nThe way of schema definition is like a below sample.\n\n.. code:: python\n\n import tinyschema as t\n\n class Point(t.Schema):\n x = t.column(t.IntegerField)\n y = t.column(t.IntegerField)\n z = t.column(t.IntegerField, required=False)\n\nAccessing field with dot-access, like a plain python object. But a\nreturned object is wrapped by Field object.\n\nField object has these members.\n\n- name -- name of field (system value)\n- value -- value of field\n\nSo this Point schema accessing a field like a below.\n\n.. code::\n\n pt = Point(x=10, y=20)\n\n print(pt.x.name) # => x\n print(pt.x.value) # => 10\n\n\naddition\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nA column of Schema can store your favirote value. below example is\nstored a value about css-class \"hidden\". and adding label option\nthat display expression for human (display value).\n\n.. code:: python\n\n class Point(t.Schema):\n x = t.column(t.IntegerField, label=u\"x-coordinate\", class_=\"hidden\")\n\n pt = Point(x=10, y=20)\n\n print(pt.x.label) # => x-coordinate\n print(pt.x.class_) # => \"hidden\"\n\n\nschema validation\n----------------------------------------\n\nSchema has a behavior of schema-validation. schema-validation is format checking.\n\n- filtering expected values only.\n- checking type of value.\n- converting value if need.\n\n.. code:: python\n\n params = {\"x\": \"10\", \"y\": \"20\", \"foo\": \"foo\"}\n pt = Point.fromdict(params)\n print(pt.validate()) # => OrderedDict([('x', 10), ('y', 20), ('z', None)])\n\nschema-validation is run by calling validate() method. In above code,\n\"foo\" value is not member of Point schema, so validated value does not\ninclude a value name of foo. And a column-z has required=False option,\nbecause of this, a passed value that doesn't have a value name of z,\nconverted value is None.\n\nwhen schema error is found.\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nwhen schema validation is failure, then, Failure exception is raised.\n\n.. code:: python\n\n params = {\"x\": \"aa\"}\n pt = Point.fromdict(params)\n pt.validate()\n # tinyschema.Failure: , {'y': ['required'], 'x': ['aa is not int']})>\n\n\nAdding field validation\n----------------------------------------\n\nAdding field validation example is here.(using oneOf validator)\n\n.. code:: python\n\n class Signal(t.Schema):\n color = t.column(t.TextField, t.OneOf([\"red\", \"blue\", \"yellow\"]))\n\n # success version\n signal = Signal(color=\"red\")\n data = signal.validate()\n print(data[\"color\"]) # => \"red\"\n\n # failure version\n try:\n signal2 = Signal(color=\"green\")\n data = signal2.validate()\n except t.Failure as e:\n print(e)\n # , {'color': ['green is not in red, blue, yellow']})>\n\ndefault validator are below.\n\n- Any, Regex, Email, Range, Length, OneOf, Subset, URL\n\ndefault type of field.\n\n- IntegerField, FloatField, BooleanField, TextField, ChoicesField, PositiveIntegerField\n\n\nmore complex structure\n----------------------------------------\n\ntinyschema support more complex structure like a dict-tree, sequence,\nor combination of one.\n\ndict-tree(using Container)\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nA field of schema is also schema. below example, Pair Schema has two\nmembers, l and r. And l and r is a Point Schema.\n\n.. code:: python\n\n class Pair(t.Schema):\n l = t.column(t.Container(Point), class_=\"left\")\n r = t.column(t.Container(Point), class_=\"right\")\n\n params = {\n \"l\": {\"x\": \"10\", \"y\": \"20\", \"foo\": \"foo\"},\n \"r\": {\"x\": \"100\", \"y\": \"20\"},\n }\n\n pair = Pair.fromdict(params)\n\n import pprint\n pprint.pprint(pair.validate())\n # {'l': OrderedDict([('x', 10), ('y', 20), ('z', None)]),\n # 'r': OrderedDict([('x', 100), ('y', 20), ('z', None)])}\n\n pair.l.value.x.name # => x\n pair.l.value.x.value # => 10\n\n\nsequence(using Collection)\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\nPointList is a sequence of Point.\n\n.. code:: python\n\n class PointList(t.Schema):\n points = t.column(t.Collection(Point))\n\n params = {\n \"points\": [{\"x\": \"10\", \"y\": \"20\"}, {\"x\": \"20\", \"y\": \"20\"}, {\"x\": \"30\", \"y\": \"20\"}, ]\n }\n\n plist = PointList.fromdict(params)\n\n import pprint\n pprint.pprint(plist.validate())\n # {'points': [OrderedDict([('x', 10), ('y', 20), ('z', None)]),\n # OrderedDict([('x', 20), ('y', 20), ('z', None)]),\n # OrderedDict([('x', 30), ('y', 20), ('z', None)])]}\n\n\ndata validation\n----------------------------------------\n\ndata-validation is a checking about a relation of each data.\n\n(TODO: gentle example)\n\n.. code:: python\n\n from tinyschema.datavalidation import ValidationObject, multi, Invalid, single, share\n\n\n class PointValidation(ValidationObject):\n def __init__(self, limit):\n self.limit = limit\n\n @multi([\"x\", \"z\"])\n def equals(self, x, z):\n if x != z:\n raise Invalid(\"not equal\")\n\n @share(single(\"x\"), single(\"y\"), single(\"z\"))\n def limit(self, value):\n if value > self.limit:\n raise Invalid(\"too large\")\n\n validate = PointValidation(limit=100)\n\n print(validate(Point(x=10, y=20))) # => OrderedDict([('x', 10), ('y', 20), ('z', None)])\n\n print(validate(Point(x=10, y=20, z=10))) # => OrderedDict([('x', 10), ('y', 20), ('z', 10)])\n\n print(validate(Point(x=10, y=20, z=1000)))\n # tinyschema.Failure: , {'z': ['too large'], 'x': ['not equal']})>\n\n print(validate(Point(x=\"aa\")))\n # tinyschema.Failure: , {'x': ['aa is not int'], 'y': ['required']})>", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/podhmo/tinyschema", "keywords": "", "license": "UNKNOWN", "maintainer": null, "maintainer_email": null, "name": "tinyschema", "package_url": "https://pypi.org/project/tinyschema/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/tinyschema/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/podhmo/tinyschema" }, "release_url": "https://pypi.org/project/tinyschema/0.2.1/", "requires_dist": null, "requires_python": null, "summary": "tiny schema implementation", "version": "0.2.1" }, "last_serial": 1346050, "releases": { "0.2": [ { "comment_text": "", "digests": { "md5": "1e1ab9308bbf43bc1c4fe16b162e6b28", "sha256": "60fbb4ecf74771711e2e03e8904d7aa697582a13b0558f6e13c87ffe986ff277" }, "downloads": -1, "filename": "tinyschema-0.2.tar.gz", "has_sig": false, "md5_digest": "1e1ab9308bbf43bc1c4fe16b162e6b28", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15170, "upload_time": "2014-12-14T05:29:09", "url": "https://files.pythonhosted.org/packages/1e/ac/4a36b77957dc4f1514ee6e6264e45b7d9abd1ab86056929d88450f49965f/tinyschema-0.2.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "83cb59ab8a46cd48f7e891307add7169", "sha256": "ab70b083c68e1f1f62b61904252ef3f6903f5e109d0663cdc855aeddcea8038d" }, "downloads": -1, "filename": "tinyschema-0.2.1.tar.gz", "has_sig": false, "md5_digest": "83cb59ab8a46cd48f7e891307add7169", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15223, "upload_time": "2014-12-16T13:04:19", "url": "https://files.pythonhosted.org/packages/e1/11/8770ed503db8da139e9690cfb0f4769be627181e3d96db14936c19caf726/tinyschema-0.2.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "83cb59ab8a46cd48f7e891307add7169", "sha256": "ab70b083c68e1f1f62b61904252ef3f6903f5e109d0663cdc855aeddcea8038d" }, "downloads": -1, "filename": "tinyschema-0.2.1.tar.gz", "has_sig": false, "md5_digest": "83cb59ab8a46cd48f7e891307add7169", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15223, "upload_time": "2014-12-16T13:04:19", "url": "https://files.pythonhosted.org/packages/e1/11/8770ed503db8da139e9690cfb0f4769be627181e3d96db14936c19caf726/tinyschema-0.2.1.tar.gz" } ] }