{ "info": { "author": "Tetsuya Morimoto", "author_email": "tetsuya dot morimoto at gmail dot com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Operating System :: MacOS :: MacOS X", "Operating System :: Microsoft :: Windows", "Operating System :: POSIX", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.2", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", "Topic :: Software Development :: Libraries" ], "description": "extenum\r\n=======\r\n\r\n|Build Status| |Latest Version| |Downloads| |License|\r\n\r\nExtended Enum classes for the Python 3 enum module.\r\n\r\nThe `enum `__ module was\r\nadded since 3.4. That's good enough for simple use. The extenum is\r\nstrongly inspired by Java Enum style described in `Effective\r\nJava `__ and\r\nprovides additional feature.\r\n\r\nHow to install\r\n--------------\r\n\r\nNOTE: extenum supports Python 3 only.\r\n\r\n::\r\n\r\n $ pip install extenum\r\n\r\nConstantSpecificEnum\r\n--------------------\r\n\r\n*ConstantSpecificEnum* class is inherited the standard Enum class and\r\nprovides the feature of constant specific method and function\r\noverloading for Enum members.\r\n\r\nRead `Effective\r\nJava `__ for\r\nmore detail.\r\n\r\nConstant specific method implementation\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n\r\nLet's try to create Enum class with *ConstantSpecificEnum*. To use a\r\nmethod as function overloading, add *@overload(CONSTANT)* decorator on\r\nthat method. The *overload* decorator is implicitly defined as well as\r\nyou'll see later in *ImplicitEnum* section.\r\n\r\n.. code:: python\r\n\r\n >>> from extenum import ConstantSpecificEnum\r\n >>> class Operation(ConstantSpecificEnum):\r\n ... PLUS = '+'\r\n ... MINUS = '-'\r\n ... TIMES = '*'\r\n ... DIVIDE = '/'\r\n ...\r\n ... @overload(PLUS)\r\n ... def apply(self, x, y):\r\n ... return x + y\r\n ...\r\n ... @overload(MINUS)\r\n ... def apply(self, x, y):\r\n ... return x - y\r\n ...\r\n ... @overload(TIMES)\r\n ... def apply(self, x, y):\r\n ... return x * y\r\n ...\r\n ... @overload(DIVIDE)\r\n ... def apply(self, x, y):\r\n ... return x / y\r\n ...\r\n >>> for name, const in Operation.__members__.items():\r\n ... print(name, ':', const.apply(2, 4))\r\n ...\r\n PLUS : 6\r\n MINUS : -2\r\n TIMES : 8\r\n DIVIDE : 0.5\r\n\r\nStrategy enum pattern\r\n~~~~~~~~~~~~~~~~~~~~~\r\n\r\nThe strategy enum is more complex pattern based on constant specific\r\nmethod.\r\n\r\n.. code:: python\r\n\r\n >>> from extenum import ConstantSpecificEnum\r\n >>> class PayrollDay(ConstantSpecificEnum):\r\n ...\r\n ... class PayType(ConstantSpecificEnum):\r\n ... WEEKDAY = 1\r\n ... WEEKEND = 2\r\n ...\r\n ... @overload(WEEKDAY)\r\n ... def overtime_pay(self, hours, pay_rate):\r\n ... return 0 if hours <= 8 else (hours - 8) * pay_rate / 2\r\n ...\r\n ... @overload(WEEKEND)\r\n ... def overtime_pay(self, hours, pay_rate):\r\n ... return hours * pay_rate / 2\r\n ...\r\n ... def pay(self, hours_worked, pay_rate):\r\n ... base_pay = hours_worked * pay_rate\r\n ... overtime_pay = self.overtime_pay(hours_worked, pay_rate)\r\n ... return base_pay + overtime_pay\r\n ...\r\n ... MONDAY = PayType.WEEKDAY\r\n ... TUESDAY = PayType.WEEKDAY\r\n ... WEDNESDAY = PayType.WEEKDAY\r\n ... THURSDAY = PayType.WEEKDAY\r\n ... FRIDAY = PayType.WEEKDAY\r\n ... SATURDAY = PayType.WEEKEND\r\n ... SUNDAY = PayType.WEEKEND\r\n ...\r\n ... def pay(self, hours_worked, pay_rate):\r\n ... return self.value.pay(hours_worked, pay_rate)\r\n ...\r\n >>> PayrollDay.MONDAY.pay(10, 1000.0)\r\n 11000.0\r\n >>> PayrollDay.WEDNESDAY.pay(8, 1000.0)\r\n 8000.0\r\n >>> PayrollDay.SATURDAY.pay(10, 1000.0)\r\n 15000.0\r\n >>> PayrollDay.SUNDAY.pay(8, 1000.0)\r\n 12000.0\r\n\r\nImplicitEnum\r\n------------\r\n\r\nBefore describing what *ImplicitEnum* class is, read good article\r\nwritten by Nick Coghlan as below.\r\n\r\n- `Support for alternate declaration\r\n syntaxes `__\r\n\r\nOK. I guess you've already understood why the standard enum module\r\nhaven't supported implicit declaration syntax.\r\n\r\nPut aside its needs for now, Nick indicates how to implement\r\n*ImplicitEnum*. So, let's try to implement it experimentally using the\r\nspecial method, ``__missing__`` in defaultdict and ``__prepare__`` in\r\nMetaclass.\r\n\r\n.. code:: python\r\n\r\n >>> from extenum import ImplicitEnum\r\n >>> class Color(ImplicitEnum):\r\n ... RED\r\n ... GREEN\r\n ... BLUE\r\n ...\r\n >>> for name, const in Color.__members__.items():\r\n ... print(name, ':', const.value)\r\n ...\r\n RED : 1\r\n GREEN : 2\r\n BLUE : 3\r\n\r\nIt works well if some constants are explicit and the rest are implicit.\r\n\r\n.. code:: python\r\n\r\n >>> class Numbers(ImplicitEnum):\r\n ... ONE = 1\r\n ... TWO = 2\r\n ... THREE\r\n ...\r\n >>> Numbers.THREE.value\r\n 3\r\n\r\nHowever, it depends on the declaration order.\r\n\r\n.. code:: python\r\n\r\n >>> class DuplicatedValues(ImplicitEnum):\r\n ... ONE\r\n ... TWO = 1\r\n ... THREE = 1\r\n ...\r\n >>> DuplicatedValues.ONE.value\r\n 1\r\n >>> DuplicatedValues.TWO.value\r\n 1\r\n >>> DuplicatedValues.THREE.value\r\n 1\r\n\r\nEnumSet\r\n-------\r\n\r\nEnumSet is one of the specialized implementation of Set interface for\r\nenumeration type, inspired by `Java\r\nEnumSet `__.\r\n\r\nIt provides utility functions to handle multiple Enum constants.\r\n\r\n.. code:: python\r\n\r\n >>> from enum import Enum\r\n >>> from extenum import EnumSet\r\n >>> class Mode(Enum):\r\n ... READ = 4\r\n ... WRITE = 2\r\n ... EXECUTE = 1\r\n ...\r\n ... @classmethod\r\n ... def set_of(cls, values):\r\n ... opts = EnumSet.none_of(cls)\r\n ... for value in values:\r\n ... opts.add(cls(value))\r\n ... return opts\r\n ...\r\n >>> Mode.set_of([4, 2]) # doctest: +SKIP\r\n EnumSet({, })\r\n\r\nTo create EnumSet with all Enum members:\r\n\r\n.. code:: python\r\n\r\n >>> EnumSet.all_of(Mode) # doctest: +SKIP\r\n EnumSet({, , })\r\n\r\nOr, to create EnumSet with arbitrary Enum members:\r\n\r\n.. code:: python\r\n\r\n >>> enumset = EnumSet.of(Mode.READ, Mode.EXECUTE)\r\n >>> enumset # doctest: +SKIP\r\n EnumSet({, })\r\n >>> enumset.update(EnumSet.of(Mode.READ, Mode.WRITE))\r\n >>> enumset # doctest: +SKIP\r\n EnumSet({, , })\r\n\r\n.. |Build Status| image:: https://travis-ci.org/t2y/extenum.svg?branch=master\r\n :target: https://travis-ci.org/t2y/extenum/\r\n.. |Latest Version| image:: https://pypip.in/version/extenum/badge.svg\r\n :target: https://pypi.python.org/pypi/extenum/\r\n.. |Downloads| image:: https://pypip.in/download/extenum/badge.svg\r\n :target: https://pypi.python.org/pypi/extenum/\r\n.. |License| image:: https://pypip.in/license/extenum/badge.svg\r\n :target: https://pypi.python.org/pypi/extenum/\r\n\r\nChangeLog\r\n---------\r\n\r\n0.8.0 (2015-03-15)\r\n~~~~~~~~~~~~~~~~~~\r\n\r\n- added EnumSet\r\n\r\n0.7.0 (2015-03-06)\r\n~~~~~~~~~~~~~~~~~~\r\n\r\n- removed RegisterFactory for simplicity\r\n\r\n0.6.0 (2015-03-05)\r\n~~~~~~~~~~~~~~~~~~\r\n\r\n- added ImplicitEnum\r\n\r\n0.5.0 (2015-03-01)\r\n~~~~~~~~~~~~~~~~~~\r\n\r\n- first release", "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/t2y/extenum", "keywords": "enum", "license": "Apache License 2.0", "maintainer": "", "maintainer_email": "", "name": "extenum", "package_url": "https://pypi.org/project/extenum/", "platform": "unix,linux,osx,windows", "project_url": "https://pypi.org/project/extenum/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/t2y/extenum" }, "release_url": "https://pypi.org/project/extenum/0.8.0/", "requires_dist": null, "requires_python": null, "summary": "Extended Enum classes for the Python 3 enum module", "version": "0.8.0" }, "last_serial": 1463177, "releases": { "0.5.0": [ { "comment_text": "", "digests": { "md5": "f29869b20dc74777e733002bc6bbd74c", "sha256": "4d4233dbcd33817e53cad7101a6100953b31e831116cf65b890977c506f631b4" }, "downloads": -1, "filename": "extenum-0.5.0.tar.gz", "has_sig": false, "md5_digest": "f29869b20dc74777e733002bc6bbd74c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 9348, "upload_time": "2015-02-28T16:50:03", "url": "https://files.pythonhosted.org/packages/f8/e4/1754287788003f43402c56b8f00c5cc4f93091194b539d660dd88262d08a/extenum-0.5.0.tar.gz" } ], "0.6.0": [ { "comment_text": "", "digests": { "md5": "f771b1cb1d5d2d1e97016fedfc766718", "sha256": "41372ccb72a77b58988373f17911b6fbe7a023124da1c69bacfc252e0c12c6ed" }, "downloads": -1, "filename": "extenum-0.6.0.tar.gz", "has_sig": false, "md5_digest": "f771b1cb1d5d2d1e97016fedfc766718", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11022, "upload_time": "2015-03-04T22:26:14", "url": "https://files.pythonhosted.org/packages/ce/f5/557735c0298e0ce06b0b3acb4e3ddbe8eca56d13bf6415bdbcc7ec64898d/extenum-0.6.0.tar.gz" } ], "0.7.0": [ { "comment_text": "", "digests": { "md5": "92add3c1e947cfb05aa112430c2f0a5e", "sha256": "28c9014bacfa95011a2a231a7f4998cebdd51f353dff3a61dd28739f0faac900" }, "downloads": -1, "filename": "extenum-0.7.0.tar.gz", "has_sig": false, "md5_digest": "92add3c1e947cfb05aa112430c2f0a5e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11155, "upload_time": "2015-03-05T17:36:20", "url": "https://files.pythonhosted.org/packages/3e/64/422425b2e4cfc90a154b620ddefadf39e49b9398339e021172cdfa9c4437/extenum-0.7.0.tar.gz" } ], "0.8.0": [ { "comment_text": "", "digests": { "md5": "bd2e56d98d0e46f44701efd04bdda6f8", "sha256": "020dbc2273cf9f4d53ef730bf6c990870be58ba68428d17ad3a98008863e46c7" }, "downloads": -1, "filename": "extenum-0.8.0.tar.gz", "has_sig": false, "md5_digest": "bd2e56d98d0e46f44701efd04bdda6f8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13052, "upload_time": "2015-03-15T01:16:38", "url": "https://files.pythonhosted.org/packages/60/25/792b693119e08b09e8b502eb22e774646adfe49d51a7f3381f34d6cdcd37/extenum-0.8.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "bd2e56d98d0e46f44701efd04bdda6f8", "sha256": "020dbc2273cf9f4d53ef730bf6c990870be58ba68428d17ad3a98008863e46c7" }, "downloads": -1, "filename": "extenum-0.8.0.tar.gz", "has_sig": false, "md5_digest": "bd2e56d98d0e46f44701efd04bdda6f8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 13052, "upload_time": "2015-03-15T01:16:38", "url": "https://files.pythonhosted.org/packages/60/25/792b693119e08b09e8b502eb22e774646adfe49d51a7f3381f34d6cdcd37/extenum-0.8.0.tar.gz" } ] }