{ "info": { "author": "Atsuo Ishimoto", "author_email": "UNKNOWN", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "============================\nemitjson\n============================\n\nHelp composing objects to emit JSON.\n\n``emitjson.repository()`` creates a repository of functions to convert various type of objects to JSON-friendly objects. The repository is a `single-dispatch generic function `_ that returns a converted object if the converter for the type is registered.\n\nThe repository contains some default converters for types that are not supported by `json module` such as ``set()`` or ``datetime.datetime()``. Also, default repository returns (shallow) copies of standard container types such as ``collections.abc.Mapping`` or ``collection.abc.Sequence``. Items in the container object are also converted recursively.\n\n::\n\n >>> import datetime\n >>> import emitjson\n >>> my_repo = emitjson.repository() # create emitjson repository\n >>> src = [1, 2, datetime.datetime.now(), {4, 5, 6}]\n >>> new = my_repo(src)\n >>> print(new)\n (1, 2, '2016-04-20T22:09:18.731157', (4, 5, 6))\n\n\nYou can add your own converters as `singledispatch function `_. Following example add a converter to generate Base64 strings from ``bytes`` objects.\n\n::\n\n >>> import emitjson\n >>> import base64\n >>> my_repo = emitjson.repository() # create emitjson repository\n >>> @my_repo.register(bytes)\n >>> def conv_bytes(obj):\n ... # encode bytes object in Base64 format\n ... return base64.b64encode(obj).decode('ascii')\n ...\n >>> print(my_repo([b'abcd']))\n ('YWJjZA==', )\n\nYou can also define mapper class to convert objects to dictionary.\n\n::\n\n from emitjson repository, attr, ObjConverter\n\n class Class1:\n def __init__(self):\n self.prop1 = 'spam'\n self.prop2 = 'ham'\n\n class Class2:\n def __init__(self):\n self.prop3 = [Class1()]\n\n\n my_repo = emitjson.repository() # create emitjson repository\n\n @my_repo.register(Class1)\n class Class1Converter(ObjConverter):\n prop1 = attr # get obj.prop1\n prop2 = attr # get obj.prop2\n\n @my_repo.register(Class2)\n class Class2Converter(ObjConverter):\n prop_x = attr('prop3') # get obj.prop3 but store as 'prop_x'\n\nRequirements\n============\n\n* Python 3.4 or later\n\n\nFunctions\n=============\n\n\n@repository()\n------------------------\n\nCreate a repository of object converters. The repository is an instance of the `single-dispatch generic function `_.\n\nThe repository overrides following types by default.\n\n- ``collections.abc.Sequence`` objects are converted to ``tuple``. Elements in the sequence are converted recursively.\n\n- Keys and values of the ``collections.abc.Mapping`` are converted recursively.\n\n- ``datetime.date`` and ``datetime.datetime`` objects are converted to ``isoformat()`` string.\n\n\n\nObjConverter class\n----------------------------------\n\n``ObjConverter`` class create a converter from an arbitrary object to a dictionary. Values obtained from objects are also converted recursively.\n\nAttribute names to be converted are defined as ``attr`` class members of ``ObjConverter`` class.\n\nIf an attribute of the ``ObjConverter`` is an instance of ``attr``, `attrname` arguments specifies attribute name to be converted.\n\nResulting dict objects are also converted recursively.\n\nObjConverter.on_convert(obj, values)\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n``on_convert`` method is called to obtain attribute of ``obj`` and store the value to the ``values`` dictionary. You can override ``on_convert()`` method to customize conversion.\n\n::\n\n class Class3:\n prop = 1\n\n @myrepo.register(Class3)\n class Class3Converter(ObjConverter):\n def on_convert(self, obj, values):\n super().on_convert(obj, values)\n values['dynamic_prop'] = obj.prop\n\n print(myrepo(Class3()) # prints {'dynamic_prop': 1}\n\n\nattr(attrname=None, map=None) class\n---------------------------------------------\n\nConverts an attribute of object. \n\n``attrname`` overrides the attribute name if not omitted.\n\n``map`` argument is a functon to convert a target object to arbitrary objects.\n\n\nSAModelConverter class\n---------------------------------\n\n``SAModelConverter`` class create a converter from a SQLAlchemy model. Columns of the model are converted unless specified in ``IGNORES`` member of the class.\n\n::\n\n class Test(Base):\n __tablename__ = 'test'\n a = Column(Integer, primary_key=True)\n b = Column(Integer)\n c = Column(Integer)\n d = Column(Integer)\n\n @myrepo.register(Test)\n class TestModelConverter(SAModelConverter):\n IGNORES = ('c', 'd') # ommit Test.c and Test.d\n\n\nrepository.fromSQLAlchemyModel(model, attrs=None, ignores=None)\n------------------------------------------------------------------\n\nAnother way to register a converter of SQLAlchemy model. Columns of the model are converted unless the name of column is listed in ``ignore`` argument. ``attrs`` is a dictionary of key name and ``attr`` object. \n\n::\n\n # Converts Test model\n myrepo.fromSQLAlchemyModel(Test,\n attrs={\n 'fld1': attr, # Emits Test.fld1 as value of 'fld1'\n 'X_VALUE':attr('fld_X') # Emits Test.fld_X as value of 'X_VALUE'\n },\n ignores=('fld3', 'fld4')) # ignore Test.fld3 and Test.fld2\n\n\nDjangoModelConverter class\n---------------------------------\n\n``DjangoModelConverter`` class create a converter from a Django model. Columns of the model are converted unless specified in ``IGNORES`` member of the class.\n\n::\n\n class DjangoModel(models.Model):\n charattr = models.CharField(max_length=10)\n intattr = models.IntegerField()\n\n @myrepo.register(DjangoModel)\n class TestModelConverter(DjangoModelConverter):\n pass\n\nrepository.fromSQLAlchemyModel(model, attrs=None, ignores=None)\n------------------------------------------------------------------\n\nAnother way to register a converter of Django model. Columns of the model are converted unless the name of column is listed in ``ignore`` argument. ``attrs`` is a dictionary of key name and ``attr`` object. \n\n::\n\n # Converts Test model\n myrepo.fromDjangoModel(Test,\n attrs={\n 'fld1': attr, # Emits Test.fld1 as value of 'fld1'\n 'X_VALUE':attr('fld_X') # Emits Test.fld_X as value of 'X_VALUE'\n },\n ignores=('fld3', 'fld4')) # ignore Test.fld3 and Test.fld2\n\n\nCopyright \n=========================\n\nCopyright (c) 2016 Atsuo Ishimoto\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.", "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/atsuoishimoto/emitjson", "keywords": "json", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "emitjson", "package_url": "https://pypi.org/project/emitjson/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/emitjson/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/atsuoishimoto/emitjson" }, "release_url": "https://pypi.org/project/emitjson/0.0.5/", "requires_dist": null, "requires_python": null, "summary": "Help composing objects to build JSON.", "version": "0.0.5" }, "last_serial": 2308433, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "ef2bf35591f64f95bb1865b06177a939", "sha256": "5851aa8b11c439682cd83c836559e2a8fe297ee5af6420201fac21e5f86857b0" }, "downloads": -1, "filename": "emitjson-0.0.1.tar.gz", "has_sig": false, "md5_digest": "ef2bf35591f64f95bb1865b06177a939", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3473, "upload_time": "2016-04-16T13:38:51", "url": "https://files.pythonhosted.org/packages/71/1f/d0acb1aa974717e4a0938f8b61eafac5cfc92d177bdea3a0c7090c1d0bf9/emitjson-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "f47ee3dc62b86f60e639d029e95e9a9d", "sha256": "e28761ab74a0ef6ab9a4940fcbd22313cb6391ba1b0152e62f36d856541de540" }, "downloads": -1, "filename": "emitjson-0.0.2.tar.gz", "has_sig": false, "md5_digest": "f47ee3dc62b86f60e639d029e95e9a9d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4161, "upload_time": "2016-04-20T15:56:28", "url": "https://files.pythonhosted.org/packages/5f/4d/f1e139c050fd447fb7c9820734e673d5988df72685837c865b80505d17b3/emitjson-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "44c6831a64a86029aa68f0744f20a525", "sha256": "83ed49400034ec63087b8f03247f15749a1f390249e5dbf9efb5ad8bf64d8657" }, "downloads": -1, "filename": "emitjson-0.0.3.tar.gz", "has_sig": false, "md5_digest": "44c6831a64a86029aa68f0744f20a525", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4348, "upload_time": "2016-04-21T15:49:33", "url": "https://files.pythonhosted.org/packages/9b/04/2b93c2f9ba2834c55cbcd3eb405b5e5a81b0a81d613f45c458e59b7159c2/emitjson-0.0.3.tar.gz" } ], "0.0.4": [ { "comment_text": "", "digests": { "md5": "d13a76154750b077087481cdf9d53546", "sha256": "813d25ad0761b2ee0a57273984d5a885216210ca2339d3023742c7de0d84ec46" }, "downloads": -1, "filename": "emitjson-0.0.4.tar.gz", "has_sig": false, "md5_digest": "d13a76154750b077087481cdf9d53546", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4897, "upload_time": "2016-05-06T12:39:35", "url": "https://files.pythonhosted.org/packages/7e/ce/1144b30b277ffa8415617c8bc9971aa070c4d9c2c802fde46bc6d16d2f7a/emitjson-0.0.4.tar.gz" } ], "0.0.5": [ { "comment_text": "", "digests": { "md5": "2d4a6cf765e4db45d6cc151685a3fffe", "sha256": "30fcffda956823842bad28d87015562489443d8401f9afc7076264cefbf39ac7" }, "downloads": -1, "filename": "emitjson-0.0.5.tar.gz", "has_sig": false, "md5_digest": "2d4a6cf765e4db45d6cc151685a3fffe", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5248, "upload_time": "2016-08-27T16:27:39", "url": "https://files.pythonhosted.org/packages/de/a7/e571900b4ae5c22d7c48132bea06f8ccc2fa1ba16e4131fa0be146d4888c/emitjson-0.0.5.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "2d4a6cf765e4db45d6cc151685a3fffe", "sha256": "30fcffda956823842bad28d87015562489443d8401f9afc7076264cefbf39ac7" }, "downloads": -1, "filename": "emitjson-0.0.5.tar.gz", "has_sig": false, "md5_digest": "2d4a6cf765e4db45d6cc151685a3fffe", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5248, "upload_time": "2016-08-27T16:27:39", "url": "https://files.pythonhosted.org/packages/de/a7/e571900b4ae5c22d7c48132bea06f8ccc2fa1ba16e4131fa0be146d4888c/emitjson-0.0.5.tar.gz" } ] }