{ "info": { "author": "asduj", "author_email": "asduj@ya.ru", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": ".. highlight:: python\n\nAd hoc polymorphism for Python classes!\n=====================================================\n\nInstallation\n------------\n::\n\n pip install polymorphism\n\n``polymorphism`` support python 3.4+\n\nUsage\n-----\nTo use the ``polymorphism`` simply inherit from the ``Polymorphism`` class::\n\n\n from polymorphism import Polymorphism\n\n\n class Simple(Polymorphism):\n def calc(self, x: int, y: int) -> None:\n pass\n\n def calc(self, x: float, y: float) -> None:\n pass\n\nOr use it as metaclass::\n\n from polymorphism import PolymorphismMeta\n\n class Simple(metaclass=PolymorphismMeta):\n ...\n\n\nSometimes adding another class to the inheritance is undesirable, then you can use the ``overload`` decorator::\n\n from polymorphism import overload\n\n\n class Simple(Polymorphism):\n @overload\n def calc(self, x: int, y: int) -> None:\n pass\n\n @calc.overload\n def calc(self, x: float, y: float) -> None:\n pass\n\nThe only difference between using a decorator and inheriting it is checking for method shading. With ``overload`` the next example will not raise exception::\n\n from polymorphism import overload\n\n\n class Simple(Polymorphism):\n @overload\n def calc(self, x: int, y: int) -> None:\n pass\n\n calc = 5\n\nAnd ``calc`` would be only the attribute.\n\nWhy?\n----\nThe idea to implement polymorphism is not new. Many libraries `implement `_ this idea. Even the `standard library `_ has an implementation.\n\nBut they do not support use with classes or standard type annotation.\n\nThe basic idea of the implementation was inspired by the great book `Python Cookbook 3rd Edition `_ by David Beazley and Brian K. Jones. But the implementation in the book did not support usage of keyword arguments!\n\nAdvantages\n----------\n* Use standard and custom descriptors\n* Use naming (keyword) arguments\n* Checks for:\n\n * Arguments for variable length\n * Missed argument annotation\n * Name of wrapped function of descriptor\n * Shading method by attribute or data descriptor (like ``property``)\n * Redefining the method with the same types\n\n* Using any name for instance, not only ``self``\n\nFor all checks is raised ``TypeError`` exception.\n\nLimitations\n-----------\n\n* Simple types for dispatching\n* ``overload`` should be top of decorators\n* Custom descriptor should save wrapped function under \"__wrapped__\" name\n* Obvious, method argument can't be variable length (\\* and \\*\\*)\n\n\nExamples\n--------\nThere are no restrictions on the use of the number of decorators, you only need to comply the naming convention.\n\nFor example::\n\n class Simple(Polymorphism):\n def calc(self, x: int, y: int) -> None:\n pass\n\n @classmethod\n def calc(cls, x: float, y: float) -> None:\n pass\n\n @staticmethod\n def calc(x: str, y: str) -> None:\n pass\n\n Simple().calc(1.0, y=2.0)\n\nWhile use ``overload`` decorator place it on top::\n\n class Simple:\n @overload\n def calc(self, x: int, y: int) -> None:\n pass\n\n @calc.overload\n @classmethod\n def calc_float(cls, x: float, y: float) -> None:\n pass\n\n @calc.overload\n @staticmethod\n def calc_str(x: str, y: str) -> None:\n pass\n\nWith ``overload`` only first method name matter. Other methods can have any other names.\n\n``polymorphism`` checks the class at the creation time::\n\n class Simple(Polymorphism):\n def calc(self, x: int, y: int) -> None:\n pass\n\n def calc(self, x: int, y: int, z: int = 3) -> None:\n pass\n\nThe above example will raise ``TypeError`` exception because ``calc`` method overloaded with ``z`` parameter with default value and it is impossible distinct last method from first.\n\n``polymorphism`` will raise ``TypeError`` exception on any wrong overloading, so you don't need worry about correctness of it.\n\nSee more examples in `tests.py `_.\n\nTo-do\n-----\n\n* Complex and builtin types for dispatching like ``List[int]`` or ``Number``", "description_content_type": "text/x-rst", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/asduj/polymorphism", "keywords": "polymorphism,multiple dispatch,ad hoc", "license": "MIT", "maintainer": "asduj", "maintainer_email": "asduj@ya.ru", "name": "polymorphism", "package_url": "https://pypi.org/project/polymorphism/", "platform": "", "project_url": "https://pypi.org/project/polymorphism/", "project_urls": { "Homepage": "https://github.com/asduj/polymorphism", "Repository": "https://github.com/asduj/polymorphism" }, "release_url": "https://pypi.org/project/polymorphism/0.1.2/", "requires_dist": [ "pytest-cov (>=2.6,<3.0)" ], "requires_python": ">=3.4", "summary": "Ad hoc polymorphism for Python classes!", "version": "0.1.2" }, "last_serial": 4657441, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "ab67df75b383672fc2e532be9331fdaf", "sha256": "4f8a4e56cfbd04ce4f912270485af383743533a6bdbc07bbc53ddfc1b9333a19" }, "downloads": -1, "filename": "polymorphism-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "ab67df75b383672fc2e532be9331fdaf", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4", "size": 8777, "upload_time": "2019-01-02T18:48:08", "url": "https://files.pythonhosted.org/packages/02/a4/f7e42c2329eb769a45cdceae7a1fd946327817fbca790835b1e7c942f12b/polymorphism-0.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "cb1a9368dd604a23b9bb69e048f0a950", "sha256": "83546554178af77fe2c38af1635dce1f2d0f079ba808799f8927b122a7650e95" }, "downloads": -1, "filename": "polymorphism-0.1.0.tar.gz", "has_sig": false, "md5_digest": "cb1a9368dd604a23b9bb69e048f0a950", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4", "size": 3475, "upload_time": "2019-01-02T18:48:09", "url": "https://files.pythonhosted.org/packages/91/c5/0580372200625d8b6513fb2ed3ed1b1f08c42da3bb30d632501c0c6f6b9f/polymorphism-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "517337b7e90623895e84e31575882c04", "sha256": "ad2da24e40fcb5b5e5683ab9b002d292af15450aba4ceb329a6e4fab9799c0fa" }, "downloads": -1, "filename": "polymorphism-0.1.1-py3-none-any.whl", "has_sig": false, "md5_digest": "517337b7e90623895e84e31575882c04", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4", "size": 10222, "upload_time": "2019-01-03T18:40:00", "url": "https://files.pythonhosted.org/packages/2e/6e/6b7759545d4a299d271784d3d178dd82d2b90022a894f3b74d52ab65d470/polymorphism-0.1.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "615e46108373b7e71e2db045f0acfc42", "sha256": "713d478a1ab7ba5f0a3f21fac294a5fe6ca697dc725cc105bc9a5842b0b0058a" }, "downloads": -1, "filename": "polymorphism-0.1.1.tar.gz", "has_sig": false, "md5_digest": "615e46108373b7e71e2db045f0acfc42", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4", "size": 5255, "upload_time": "2019-01-03T18:40:02", "url": "https://files.pythonhosted.org/packages/ca/a7/5f5fd2a9cf866dab4e2e262204e88b5740f96c6cf35b4e4aa37a7f7d37d6/polymorphism-0.1.1.tar.gz" } ], "0.1.2": [ { "comment_text": "", "digests": { "md5": "36afb9d72ffd1a09e78e8529d19cd083", "sha256": "80609930a34f51839f5ef7c7473a88028e5d0e74876f3bebb7a3dc9afebf6f4c" }, "downloads": -1, "filename": "polymorphism-0.1.2-py3-none-any.whl", "has_sig": false, "md5_digest": "36afb9d72ffd1a09e78e8529d19cd083", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4", "size": 10231, "upload_time": "2019-01-03T19:31:41", "url": "https://files.pythonhosted.org/packages/8f/ef/71d0ae9232e03fb79962ebace230c677f33bf301311d655c13752a2f9044/polymorphism-0.1.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "da63bfcff6b9e6dd558b854f8448f15c", "sha256": "946d929f3033b060bb02de00590c95d57ac9e61d004488aab55016591e333851" }, "downloads": -1, "filename": "polymorphism-0.1.2.tar.gz", "has_sig": false, "md5_digest": "da63bfcff6b9e6dd558b854f8448f15c", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4", "size": 5263, "upload_time": "2019-01-03T19:31:43", "url": "https://files.pythonhosted.org/packages/54/58/10a0f905196472f03fbf05d9bed76304bc874b38843319cdef37cc4572ec/polymorphism-0.1.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "36afb9d72ffd1a09e78e8529d19cd083", "sha256": "80609930a34f51839f5ef7c7473a88028e5d0e74876f3bebb7a3dc9afebf6f4c" }, "downloads": -1, "filename": "polymorphism-0.1.2-py3-none-any.whl", "has_sig": false, "md5_digest": "36afb9d72ffd1a09e78e8529d19cd083", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.4", "size": 10231, "upload_time": "2019-01-03T19:31:41", "url": "https://files.pythonhosted.org/packages/8f/ef/71d0ae9232e03fb79962ebace230c677f33bf301311d655c13752a2f9044/polymorphism-0.1.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "da63bfcff6b9e6dd558b854f8448f15c", "sha256": "946d929f3033b060bb02de00590c95d57ac9e61d004488aab55016591e333851" }, "downloads": -1, "filename": "polymorphism-0.1.2.tar.gz", "has_sig": false, "md5_digest": "da63bfcff6b9e6dd558b854f8448f15c", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.4", "size": 5263, "upload_time": "2019-01-03T19:31:43", "url": "https://files.pythonhosted.org/packages/54/58/10a0f905196472f03fbf05d9bed76304bc874b38843319cdef37cc4572ec/polymorphism-0.1.2.tar.gz" } ] }