{ "info": { "author": "Kun Fang", "author_email": "kfang_anqing@yahoo.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "Avro Data Model\n=====\n\n## Introduction\n[Apache Avro](http://avro.apache.org/) is a data serialization framework. It is used in data serialization (especially in Hadoop ecosystem) and RPC protocols. It has libraries to support many languages. The library supports code generation with static languages like Java, while for dynamic languages for example python, code generation is not necessary.\n\nWhen avro data is deserialized in Python environment, it was stored as a dictionary in memory. As a dictionary, it looses all the interesting features provided by the avro schema. For example, you can modify an integer field with a string without getting any errors. As a dictionary, it also doesn't provide any nice features from a normal class, for example, if an avro schema has `firstName` and `lastName` fields, it is not easy to define a `fullName` function to generate the full name.\n\n## Use Cases of the Library\nIn stream processing and RPC protocols, strict data types are required to make sure the system runs correctly. In Python, avro data is converted to a dictionary, which doesn't guarantee types and also doesn't provide a custom class hierarchy. I am looking to develop a way so that a class can be build on top of an avro schema, so that it can keep correct data type and also has a class structure.\n\nMy solution is similar to what [SQLAlchemy ORM](https://www.sqlalchemy.org) does. You need to manually create classes corresponding to avro schemas. However, fields of the avro schemas are all extracted from `avsc` file instead of being manually defined like SQLAlchemy. The classes allow defining methods to introduce new properties or new validations. Please check the following examples for how to use the library.\n\nThe purpose of the library is to bridge the gap between dynamical typed python and the use cases that requires strong types. This library should be restricted to places where static types are required. Otherwise, you will loose all the happiness playing with Python if applying this library everywhere.\n\n\n## Example\n### A Simple Example\n**User.avsc**\n```\n{\n \"type\": \"record\",\n \"name\": \"User\",\n \"fields\": [\n {\n \"name\": \"lastName\",\n \"type\": \"string\"\n },\n {\n \"name\": \"firstName\",\n \"type\": \"string\"\n }\n ]\n}\n```\nThe following code defined a User class associated with the schema\n```\n@avro_schema(AvroDataNames(default_namespace=\"example.avro\"), schema_file=\"User.avsc\")\nclass User(object):\n def fullname(self):\n return \"{} {}\".format(self.firstName, self.lastName)\n```\nWith this class definition, the full name can be obtained with the function call.\n```\nuser = User({\"firstName\": \"Alyssa\", \"lastName\": \"Yssa\"})\nprint(user.fullname())\n# Alyssa Yssa\n```\n\n### Avro Schema with Extra Validation\nIn some use cases, some extra validations are required, for example:\n**Date.avsc**\n```\n{\n \"name\": \"Date\",\n \"type\": \"record\",\n \"fields\": [\n {\n \"name\": \"year\",\n \"type\": \"int\"\n },\n {\n \"name\": \"month\",\n \"type\": \"int\"\n },\n {\n \"name\": \"day\",\n \"type\": \"int\"\n }\n ]\n}\n```\nThe `month` and `day` of a date cannot be arbitrary integers. A extra validation can be done as following:\n```\n@avro_schema(AvroDataNames(default_namespace=\"example.avro\"), schema_file=\"Date.avsc\")\nclass Date(object):\n def __init__(self, value):\n if isinstance(value, datetime.date):\n value = {\n 'year': value.year,\n 'month': value.month,\n 'day': value.day\n }\n super().__init__(value)\n\n def date(self):\n return datetime.date(self.year, self.month, self.day)\n\n def validate(self, data):\n return super().validate(data) \\\n and datetime.date(data['year'], data['month'], data['day'])\n```\nThe `Date` class can validate the input before assign it to then underlying avro schema\n```\ndate = Date({\"year\": 2018, \"month\": 12, \"date\": 99})\n# ValueError: day is out of range for month\ndate = Date(datetime.date(2018, 12, 12))\n# No Error\n```\n\n### Extract an avro schema defined in an outer schema\nSometimes an avro schema is defined in another schema\n**Employee.avsc**\n```\n{\n \"type\": \"record\",\n \"name\": \"Employee\",\n \"namespace\": \"com.test\",\n \"fields\": [\n {\n \"name\": \"id\"\n \"type\": \"string\"\n },\n {\n \"name\": \"name\",\n \"type\": {\n \"type\": \"record\",\n \"name\": \"Name\",\n \"namespace\": \"com.test\",\n \"fields\": [\n {\n \"name\": \"lastName\",\n \"type\": \"string\"\n },\n {\n \"name\": \"firstName\",\n \"type\": \"string\"\n }\n ]\n }\n }\n ]\n}\n```\nThe schema `com.test.Name` is defined in `com.test.Employee`. There is no `Name.avsc`, but you can still define a class for it the schema:\n```\n# Parent schema must be define first.\n@avro_schema(\n EXAMPLE_NAMES,\n schema_file=os.path.join(DIRNAME, \"Employee.avsc\"))\nclass Employee(object):\n pass\n\n\n# Full name is required\n@avro_schema(EXAMPLE_NAMES, full_name=\"com.test.Name\")\nclass Name(object):\n pass\n\n\nname = Name({{\"firstName\": \"Alyssa\", \"lastName\": \"Yssa\"})\nprint(name)\n# {'firstName': 'Alyssa', 'lastName': 'Yssa'}\n```\n\n## Contributing\nAfter cloning/forking the repo, navigate to the directory and run\n```\nsource init.sh\n```\nThe python environment should be ready for you.\n\n## Authors\n\n* **Kun Fang** - (https://github.com/kun-fang)\n\nSee also the list of [contributors](https://github.com/your/project/contributors) who participated in this project.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details\n\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/kun-fang/avro-data-model", "keywords": "avro avro-schema orm python3 avro-data data-models", "license": "", "maintainer": "", "maintainer_email": "", "name": "avro-models", "package_url": "https://pypi.org/project/avro-models/", "platform": "", "project_url": "https://pypi.org/project/avro-models/", "project_urls": { "Homepage": "https://github.com/kun-fang/avro-data-model" }, "release_url": "https://pypi.org/project/avro-models/0.0.4/", "requires_dist": [ "avro-python3 (>=1.8.2)", "six (>=1.11.0)" ], "requires_python": "", "summary": "Object-relational mapping for AVRO schema", "version": "0.0.4" }, "last_serial": 5629742, "releases": { "0.0.2": [ { "comment_text": "", "digests": { "md5": "b7b1b8fcff0a53a051cfa45bdacb4dc1", "sha256": "ce1cde53ef1c002f1868232d465a8b72f26344c3007cfee10146d3d652ef1c03" }, "downloads": -1, "filename": "avro_models-0.0.2-py3-none-any.whl", "has_sig": false, "md5_digest": "b7b1b8fcff0a53a051cfa45bdacb4dc1", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7660, "upload_time": "2019-02-23T23:23:20", "url": "https://files.pythonhosted.org/packages/c5/7d/aae8e30a02b1fd8b8d91070a32c761cf8d36ee42d6f03fa5ba6dac785488/avro_models-0.0.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4d7222d09939a49c4b861a3b49478129", "sha256": "0efa185e677cf7b1d2d2bbdcc0aa31327645388dbaf6861cb6a7e97f1e959515" }, "downloads": -1, "filename": "avro_models-0.0.2.tar.gz", "has_sig": false, "md5_digest": "4d7222d09939a49c4b861a3b49478129", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5636, "upload_time": "2019-02-23T23:23:21", "url": "https://files.pythonhosted.org/packages/ec/66/cc526b7fac59e96c991c3c8c2189b986551d419904cd31cdc7e1ee260622/avro_models-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "e9f28f0c3c39a0705175bb47a842e353", "sha256": "6b64a45a67e6ccdb65da3ab42a2a28576fc739a0205048455d35a1d4cb72856e" }, "downloads": -1, "filename": "avro_models-0.0.3-py3-none-any.whl", "has_sig": false, "md5_digest": "e9f28f0c3c39a0705175bb47a842e353", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 7677, "upload_time": "2019-02-23T23:32:40", "url": "https://files.pythonhosted.org/packages/da/14/e15da0c3ce2cc799bbdf42221279c51740f854763a143dc9174733eb55eb/avro_models-0.0.3-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "2344faae4765871e024e603984bb154d", "sha256": "a32cc3c94b3e0adcf92ee52d498ed60c660406c1e8e7c6312a93e8cab37abdde" }, "downloads": -1, "filename": "avro_models-0.0.3.tar.gz", "has_sig": false, "md5_digest": "2344faae4765871e024e603984bb154d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5669, "upload_time": "2019-02-23T23:32:41", "url": "https://files.pythonhosted.org/packages/34/37/d361eb57e04f99a853652abea0b83acc93beed0299175656717458f6003c/avro_models-0.0.3.tar.gz" } ], "0.0.4": [ { "comment_text": "", "digests": { "md5": "360a3e015c5b4c767b49f77963e9db6a", "sha256": "1bbe70202bcce3d26421b63f71474b6fbc9fa80e9738a520eec9c8363242c2c0" }, "downloads": -1, "filename": "avro_models-0.0.4-py3-none-any.whl", "has_sig": false, "md5_digest": "360a3e015c5b4c767b49f77963e9db6a", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8415, "upload_time": "2019-08-04T04:06:36", "url": "https://files.pythonhosted.org/packages/75/aa/b67bc12c59b7ee3841848c112830b6ff525ddb6cde9bad84dbe6bd0bf4b6/avro_models-0.0.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "bbd882f04d28934570cc0ac496dc552a", "sha256": "0b2b80dd0ff0cf4648d47a729ef2a56a536d0fa08d9f1ad871753813d4877cca" }, "downloads": -1, "filename": "avro_models-0.0.4.tar.gz", "has_sig": false, "md5_digest": "bbd882f04d28934570cc0ac496dc552a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5836, "upload_time": "2019-08-04T04:06:37", "url": "https://files.pythonhosted.org/packages/03/25/6e9379f42bd99bdfc8e4ae65630bcc3508ee3b7aa9befbd821a123b72f12/avro_models-0.0.4.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "360a3e015c5b4c767b49f77963e9db6a", "sha256": "1bbe70202bcce3d26421b63f71474b6fbc9fa80e9738a520eec9c8363242c2c0" }, "downloads": -1, "filename": "avro_models-0.0.4-py3-none-any.whl", "has_sig": false, "md5_digest": "360a3e015c5b4c767b49f77963e9db6a", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 8415, "upload_time": "2019-08-04T04:06:36", "url": "https://files.pythonhosted.org/packages/75/aa/b67bc12c59b7ee3841848c112830b6ff525ddb6cde9bad84dbe6bd0bf4b6/avro_models-0.0.4-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "bbd882f04d28934570cc0ac496dc552a", "sha256": "0b2b80dd0ff0cf4648d47a729ef2a56a536d0fa08d9f1ad871753813d4877cca" }, "downloads": -1, "filename": "avro_models-0.0.4.tar.gz", "has_sig": false, "md5_digest": "bbd882f04d28934570cc0ac496dc552a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5836, "upload_time": "2019-08-04T04:06:37", "url": "https://files.pythonhosted.org/packages/03/25/6e9379f42bd99bdfc8e4ae65630bcc3508ee3b7aa9befbd821a123b72f12/avro_models-0.0.4.tar.gz" } ] }