{ "info": { "author": "Jamie Matthews", "author_email": "jamie.matthews@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Developers", "License :: Public Domain", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "# micromodels\n\nA simple library for building model classes based on dictionaries of data.\n\nPerfect for (amongst other things) wrapping Python objects around JSON data returned from web-based APIs.\n\n**Authors**: Jamie Matthews () and Eric Martin ().\n\n## Installation\n\n pip install micromodels\n\n## Really simple example\n\n import micromodels\n\n class Author(micromodels.Model):\n first_name = micromodels.CharField()\n last_name = micromodels.CharField()\n date_of_birth = micromodels.DateField(format=\"%Y-%m-%d\")\n\n @property\n def full_name(self):\n return \"%s %s\" % (self.first_name, self.last_name)\n\n\n douglas_data = {\n \"first_name\": \"Douglas\",\n \"last_name\": \"Adams\",\n \"date_of_birth\": \"1952-03-11\",\n }\n\n douglas = Author.from_dict(douglas_data)\n print \"%s was born in %s\" % (douglas.full_name, douglas.date_of_birth.strftime(\"%Y\"))\n\n## Slightly more complex example\n\n import json\n from urllib2 import urlopen\n\n import micromodels\n\n class TwitterUser(micromodels.Model):\n id = micromodels.IntegerField()\n screen_name = micromodels.CharField()\n name = micromodels.CharField()\n description = micromodels.CharField()\n\n def get_profile_url(self):\n return 'http://twitter.com/%s' % self.screen_name\n\n\n class Tweet(micromodels.Model):\n id = micromodels.IntegerField()\n text = micromodels.CharField()\n created_at = micromodels.DateTimeField(format=\"%a %b %d %H:%M:%S +0000 %Y\")\n user = micromodels.ModelField(TwitterUser)\n\n\n json_data = urlopen('http://api.twitter.com/1/statuses/show/20.json').read()\n tweet = Tweet.from_dict(json_data, is_json=True)\n\n print tweet.user.name\n print tweet.user.get_profile_url()\n print tweet.id\n print tweet.created_at.strftime('%A')\n\n #new fields can also be added to the model instance\n #a method needs to be used to do this to handle serialization\n\n tweet.add_field('retweet_count', 44, micromodels.IntegerField())\n print tweet.retweet_count\n\n #the data can be cast to a dict (still containing time object)\n print tweet.to_dict()\n\n #it can also be cast to JSON (fields handle their own serialization)\n print tweet.to_json()\n\n #tweet.to_json() is equivalent to this call\n json.dumps(tweet.to_dict(serial=True))\n\n\n## Field reference\n\n### Field options\n\nThe following optional argument is available for all field types.\n\n#### `source`\n\nBy default, a model class will look for a key in its source data with the same name as each of its fields. For example:\n\n class ExampleModel(micromodels.Model):\n myfield = micromodels.CharField()\n\n >>> e = ExampleModel({'myfield': 'Some Value'})\n >>> e.myfield\n u'Some Value'\n\nIf you wish to change this, you can pass the 'source' argument to each field instance:\n\n class ExampleModel(micromodels.Model):\n myfield = micromodels.CharField()\n anotherfield = micromodels.CharField(source='some_other_field')\n\n >>> e = ExampleModel({'myfield': 'Some Value', 'some_other_field': 'Another Value'})\n >>> e.anotherfield\n u'Another Value'\n\n### Field types\n\n#### BaseField\n\nThe simplest type of field - this simply passes through whatever is in the data dictionary without changing it at all.\n\n#### CharField\n\nA field for string data. **Will attempt to convert its supplied data to Unicode.**\n\n#### IntegerField\n\nAttempts to convert its supplied data to an integer.\n\n#### FloatField\n\nAttempts to convert its supplied data to a floating point value.\n\n#### BooleanField\n\nAttempts to convert its supplied data to a boolean. If the data is a string, `\"true\"` (case insensitive) will be converted to `True` and all other strings will be converted to `False`. If the supplied data is an integer, positive numbers will become `True` and negative numbers or zero will become `False`.\n\n#### DateTimeField\n\nConverts its supplied data to a Python `datetime.datetime` object as `ISO8601`. \n\n class MyModel(micromodels.Model):\n created_at = micromodels.DateTimeField()\n\nAn optional format may be provided. \n\n class MyModel(micromodels.Model):\n created_at = micromodels.DateTimeField(format=\"%a %b %d %H:%M:%S +0000 %Y\")\n\nSee [the Python\ndocumentation](http://docs.python.org/library/datetime.html#strftime-strptime-behavior)\nfor details of the format string. For example:\n\n#### DateField\n\nConverts its supplied data to a Python `datetime.date` object as\n`ISO8601` or using an option `format` argument (see `DateTimeField`\nfor details)\n\n#### TimeField\n\nConverts its supplied data to a Python `datetime.time` object as\n`ISO8601` or using an option `format` argument (see `DateTimeField` for details).\n\n#### FieldCollectionField\n\nUse this field when your source data dictionary contains a list of items of the same type. It takes a single required argument, which is the field type that should be used to convert each item in the list. For example:\n\n some_data = {\n 'first_list': [0, 34, 42],\n 'second_list': ['first_item', 'second_item', 'third_item'],\n }\n\n class MyModel(micromodels.Model):\n first_list = micromodels.FieldCollectionField(micromodels.IntegerField)\n second_list = micromodels.FieldCollectionField(micromodels.CharField)\n\n >>> m = MyModel(some_data)\n >>> m.first_list\n [0, 34, 42]\n >>> m.second_list\n [u'first_item', u'second_item', u'third_item']\n\n#### ModelField\n\nUse this field when you wish to nest one object inside another. It takes a single required argument, which is the nested class. For example, given the following dictionary:\n\n some_data = {\n 'first_item': 'Some value',\n 'second_item': {\n 'nested_item': 'Some nested value',\n },\n }\n\nYou could build the following classes (note that you have to define the inner nested models first):\n\n class MyNestedModel(micromodels.Model):\n nested_item = micromodels.CharField()\n\n class MyMainModel(micromodels.Model):\n first_item = micromodels.CharField()\n second_item = micromodels.ModelField(MyNestedModel) # pass the class of the nested model\n\nThen you can access the data as follows:\n\n >>> m = MyMainModel(some_data)\n >>> m.first_item\n u'Some value'\n >>> m.second_item.__class__.__name__\n 'MyNestedModel'\n >>> m.second_item.nested_item\n u'Some nested value'\n\n#### ModelCollectionField\n\nUse this field when your source data dictionary contains a list of dictionaries. It takes a single required argument, which is the name of the nested class that each item in the list should be converted to. For example:\n\n some_data = {\n 'list': [\n {'value': 'First value'},\n {'value': 'Second value'},\n {'value': 'Third value'},\n ]\n }\n\n class MyNestedModel(micromodels.Model):\n value = micromodels.CharField()\n\n class MyMainModel(micromodels.Model):\n list = micromodels.ModelCollectionField(MyNestedModel)\n\n >>> m = MyMainModel(some_data)\n >>> len(m.list)\n 3\n >>> m.list[0].__class__.__name__\n 'MyNestedModel'\n >>> m.list[0].value\n u'First value'\n >>> [item.value for item in m.list]\n [u'First value', u'Second value', u'Third value']\n\n\n## (Un)license\n\nThis is free and unencumbered software released into the public domain.\n\nAnyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.\n\nIn jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\nFor more information, please refer to ", "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/j4mie/micromodels/", "keywords": null, "license": "Public Domain", "maintainer": null, "maintainer_email": null, "name": "micromodels", "package_url": "https://pypi.org/project/micromodels/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/micromodels/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/j4mie/micromodels/" }, "release_url": "https://pypi.org/project/micromodels/0.5.1/", "requires_dist": null, "requires_python": null, "summary": "Declarative dictionary-based model classes for Python", "version": "0.5.1" }, "last_serial": 794772, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "5d6ea900edff5cd9d7d5f7cde8fe0431", "sha256": "749e240f2f6187eaa00b8945ad7c49c9cafbad4686e6bdda7e1ba400facc063c" }, "downloads": -1, "filename": "micromodels-0.1.0.tar.gz", "has_sig": false, "md5_digest": "5d6ea900edff5cd9d7d5f7cde8fe0431", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6664, "upload_time": "2010-12-28T16:56:10", "url": "https://files.pythonhosted.org/packages/18/e3/c22473d6d960df227d023a60b135b7eb29e0e790912034fe05b508ee0525/micromodels-0.1.0.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "c66fdd0d7d6b966b4509e89f1e5c696c", "sha256": "ea464db13d8f4f33e9b4a0d3fc34ce8c89bdfdf583f77f1b054b5a86ddda1a62" }, "downloads": -1, "filename": "micromodels-0.2.0.tar.gz", "has_sig": false, "md5_digest": "c66fdd0d7d6b966b4509e89f1e5c696c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9061, "upload_time": "2011-01-10T10:09:08", "url": "https://files.pythonhosted.org/packages/6e/71/969d88ec4bef75eca849006024c2def6e82caec6f4fed194975ef78b48c0/micromodels-0.2.0.tar.gz" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "9bfafe2f3d25b22b522aafbc7bfb7d62", "sha256": "1ffa026b174796e66f7ffa69e75cf04f462c59d5f298f9df32795a63feda7120" }, "downloads": -1, "filename": "micromodels-0.3.0.tar.gz", "has_sig": false, "md5_digest": "9bfafe2f3d25b22b522aafbc7bfb7d62", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9554, "upload_time": "2011-01-19T21:33:07", "url": "https://files.pythonhosted.org/packages/79/b9/8699e659d1f534ddfc1fe20149d37d44c7c153e60bd3e162090c43c5c204/micromodels-0.3.0.tar.gz" } ], "0.4.0": [ { "comment_text": "", "digests": { "md5": "ad941a29d4eb623414f42826c6b5f6c7", "sha256": "c97d3ccfef1465f3a3769dc5441e0d0292ae0249a3b8ec1b2cec606844748c30" }, "downloads": -1, "filename": "micromodels-0.4.0.tar.gz", "has_sig": false, "md5_digest": "ad941a29d4eb623414f42826c6b5f6c7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10034, "upload_time": "2011-02-24T09:46:39", "url": "https://files.pythonhosted.org/packages/84/7a/b7f31db366501a20feaa3a471231e322ec900058cdba3fcb9ec29eedb2d4/micromodels-0.4.0.tar.gz" } ], "0.5.0": [ { "comment_text": "", "digests": { "md5": "cc17238483658ad8ebc3fa8a3013ea47", "sha256": "4c5deaaf2d516ebf8887dcf714dafbc296c6f4e86296e60f8ea3ed7259d97b9c" }, "downloads": -1, "filename": "micromodels-0.5.0.tar.gz", "has_sig": false, "md5_digest": "cc17238483658ad8ebc3fa8a3013ea47", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 10280, "upload_time": "2011-11-18T10:34:49", "url": "https://files.pythonhosted.org/packages/bd/9e/453cc09f243605bc490f8f0bc0a9f9665504f623ac2f72dee0354e4ef33e/micromodels-0.5.0.tar.gz" } ], "0.5.1": [ { "comment_text": "", "digests": { "md5": "9e96d424212015a6952737be7cb8d512", "sha256": "e2a6f3388c9583ea04da6c1072906227017d3d221bf75fd182b6cf9557d440a7" }, "downloads": -1, "filename": "micromodels-0.5.1.tar.gz", "has_sig": false, "md5_digest": "9e96d424212015a6952737be7cb8d512", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14426, "upload_time": "2012-08-01T20:37:28", "url": "https://files.pythonhosted.org/packages/49/29/68076e9abf6fe28e8202a467c5a217687999fda57dac51817cb0ae1aef7a/micromodels-0.5.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "9e96d424212015a6952737be7cb8d512", "sha256": "e2a6f3388c9583ea04da6c1072906227017d3d221bf75fd182b6cf9557d440a7" }, "downloads": -1, "filename": "micromodels-0.5.1.tar.gz", "has_sig": false, "md5_digest": "9e96d424212015a6952737be7cb8d512", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 14426, "upload_time": "2012-08-01T20:37:28", "url": "https://files.pythonhosted.org/packages/49/29/68076e9abf6fe28e8202a467c5a217687999fda57dac51817cb0ae1aef7a/micromodels-0.5.1.tar.gz" } ] }