{ "info": { "author": "LAZR Developers", "author_email": "lazr-developers@lists.launchpad.net", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL)", "Operating System :: OS Independent", "Programming Language :: Python" ], "description": "..\r\n This file is part of lazr.enum.\r\n\r\n lazr.enum is free software: you can redistribute it and/or modify it\r\n under the terms of the GNU Lesser General Public License as published by\r\n the Free Software Foundation, version 3 of the License.\r\n\r\n lazr.enum is distributed in the hope that it will be useful, but\r\n WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r\n or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public\r\n License for more details.\r\n\r\n You should have received a copy of the GNU Lesser General Public License\r\n along with lazr.enum. If not, see .\r\n\r\nEnumerated Types\r\n****************\r\n\r\nEnumerated types are used primarily in two distinct places in the Launchpad\r\ncode: selector types; and database types.\r\n\r\nSimple enumerated types do not have values, whereas database enumerated\r\ntypes are a mapping from an integer value to something meaningful in the\r\ncode.\r\n\r\n >>> from lazr.enum import (\r\n ... EnumeratedType, DBEnumeratedType, Item, DBItem, use_template)\r\n\r\nThe `enum` values of EnumeratedTypes are instances of Item.\r\n\r\n >>> class Fruit(EnumeratedType):\r\n ... \"A choice of fruit.\"\r\n ... APPLE = Item('Apple')\r\n ... PEAR = Item('Pear')\r\n ... ORANGE = Item('Orange')\r\n\r\n===================\r\nIVocabulary support\r\n===================\r\n\r\nEnumerated types support IVocabularyTokenized.\r\n\r\n >>> from zope.interface.verify import verifyObject\r\n >>> from zope.schema.interfaces import (\r\n ... ITitledTokenizedTerm, IVocabularyTokenized)\r\n >>> verifyObject(IVocabularyTokenized, Fruit)\r\n True\r\n\r\nThe items themselves do not support any interface. Items returned\r\nby the methods for vocabularies return wrapped items that support\r\nthe ITitledTokenizedTerm interface.\r\n\r\nThe token used to identify terms in the vocabulary is the name of the\r\nItem variable.\r\n\r\n >>> item = Fruit.getTermByToken('APPLE')\r\n >>> type(item)\r\n \r\n >>> verifyObject(ITitledTokenizedTerm, item)\r\n True\r\n\r\nTokenizedItems have three attributes (in order to support\r\nITitledTokenizedTerm):\r\n\r\n >>> item.value\r\n \r\n >>> item.token\r\n 'APPLE'\r\n >>> item.title\r\n 'Apple'\r\n\r\n >>> Fruit.getTermByToken('apple').value\r\n \r\n\r\nThe length of an EnumeratedType returns the number of items it has.\r\n\r\n >>> print len(Fruit)\r\n 3\r\n\r\n===========================\r\nThe EnumeratedType registry\r\n===========================\r\n\r\nAll enumerated types that are created are added to the\r\nenumerated_type_registry.\r\n\r\n >>> from lazr.enum import enumerated_type_registry\r\n\r\nThe enumerated_type_registry maps the name of the enumerated type to the type\r\nitself.\r\n\r\n >>> 'Fruit' in enumerated_type_registry\r\n True\r\n >>> enumerated_type_registry['Fruit']\r\n \r\n\r\nYou cannot redefine an existing enumerated type, nor create another enumerated\r\ntype with the same name as an existing type.\r\n\r\n >>> class BranchType(EnumeratedType):\r\n ... BAR = Item('Bar')\r\n ...\r\n >>> BranchType.name = 'AltBranchType'\r\n >>> class BranchType(EnumeratedType):\r\n ... FOO = Item('Foo')\r\n Traceback (most recent call last):\r\n ...\r\n TypeError: An enumerated type already exists with the name BranchType\r\n (...AltBranchType).\r\n\r\n======================\r\nEnumerated Type basics\r\n======================\r\n\r\nAn EnumeratedType has a name and a description. The name is the same as the\r\nclass name, and the description is the docstring for the class.\r\n\r\n >>> print Fruit.name\r\n Fruit\r\n >>> print Fruit.description\r\n A choice of fruit.\r\n\r\nIf you do not specify an explicit sort_order for the items of the\r\nEnumeratedType one is created for you. This is tuple of the tokens.\r\n\r\n >>> print Fruit.sort_order\r\n ('APPLE', 'PEAR', 'ORANGE')\r\n\r\nThe items of an enumerated type can be iterated over. However the type that\r\nis returned by the iteration is the TokenizedItem, not the item itself.\r\n\r\n >>> for item in Fruit:\r\n ... print item.token, item.title\r\n APPLE Apple\r\n PEAR Pear\r\n ORANGE Orange\r\n\r\nItems can also optionally have a url associated with them.\r\n\r\n >>> class Guitar(EnumeratedType):\r\n ... FENDER = Item('Fender', url='http://www.fender.com')\r\n ... RICK = Item('Rickenbacker', url='http://www.rickenbacker.com')\r\n ... GIBSON = Item('Gibson', url='http://www.gibson.com')\r\n ... FRANKENBASS = Item('Home built')\r\n\r\n >>> print Guitar.FENDER.url\r\n http://www.fender.com\r\n >>> print Guitar.FRANKENBASS.url\r\n None\r\n\r\nItems in an enumerator support comparison and equality checks. Comparison\r\nis based on the sort order of the items.\r\n\r\n >>> apple = Fruit.APPLE\r\n >>> pear = Fruit.PEAR\r\n >>> orange = Fruit.ORANGE\r\n >>> apple < pear\r\n True\r\n >>> apple == pear\r\n False\r\n >>> apple == apple\r\n True\r\n >>> apple != pear\r\n True\r\n >>> apple > pear\r\n False\r\n >>> pear < orange\r\n True\r\n >>> apple < orange\r\n True\r\n\r\nThe string representation of an Item is the title, and the representation\r\nalso shows the enumeration that the Item is from.\r\n\r\n >>> print apple\r\n Apple\r\n >>> print repr(apple)\r\n \r\n\r\nThe `items` attribute of an enumerated type is not a list, but a class that\r\nprovides iteration over the items, and access to the Item attributes through\r\neither the name of the Item, or the database value if there is one.\r\n\r\nThe primary use of this is to provide a backwards compatible accessor for\r\nitems, but it also provides a suitable alternative to getattr.\r\n\r\n >>> name = 'APPLE'\r\n >>> Fruit.items[name]\r\n \r\n >>> getattr(Fruit, name)\r\n \r\n\r\n=========================\r\nDatabase Enumerated Types\r\n=========================\r\n\r\nA very common use of enumerated types are to give semantic meaning to integer\r\nvalues stored in database columns. EnumeratedType Items themselves don't have\r\nany integer values.\r\n\r\nThe DBEnumeratedType provides the semantic framework for a type that is used to\r\nmap integer values to python enumerated values.\r\n\r\n >>> # Remove the existing reference to BranchType from the registry\r\n >>> del enumerated_type_registry['BranchType']\r\n >>> class BranchType(DBEnumeratedType):\r\n ... HOSTED = DBItem(1, \"\"\"\r\n ... Hosted\r\n ...\r\n ... Hosted braches use the supermirror as the main repository\r\n ... for the branch.\"\"\")\r\n ...\r\n ... MIRRORED = DBItem(2, \"\"\"\r\n ... Mirrored\r\n ...\r\n ... Mirrored branches are \"pulled\" from a remote location.\"\"\")\r\n ...\r\n ... IMPORTED = DBItem(3, \"\"\"\r\n ... Imported\r\n ...\r\n ... Imported branches are natively maintained in CVS or SVN\"\"\")\r\n\r\nNote carefully that the value of a DBItem is the integer representation. But\r\nthe value of the TokenizedItem is the DBItem itself.\r\n\r\n >>> hosted = BranchType.HOSTED\r\n >>> hosted.value\r\n 1\r\n >>> hosted == BranchType.HOSTED\r\n True\r\n >>> tokenized_item = BranchType.getTermByToken('HOSTED')\r\n >>> tokenized_item.value\r\n \r\n\r\nDBEnumeratedTypes also support IVocabularyTokenized\r\n\r\n >>> verifyObject(IVocabularyTokenized, BranchType)\r\n True\r\n\r\nThe items attribute of DBEnumeratedTypes provide a mapping from the database\r\nvalues to the DBItems.\r\n\r\n >>> BranchType.items[3]\r\n \r\n\r\nThe items also support the url field.\r\n\r\n >>> class Bass(DBEnumeratedType):\r\n ... FENDER = DBItem(10, 'Fender', url='http://www.fender.com')\r\n ... RICK = DBItem(20, 'Rickenbacker',\r\n ... url='http://www.rickenbacker.com')\r\n ... GIBSON = DBItem(30, 'Gibson', url='http://www.gibson.com')\r\n ... FRANKENBASS = DBItem(40, 'Home built')\r\n\r\n >>> print Bass.FENDER.url\r\n http://www.fender.com\r\n >>> print Bass.FRANKENBASS.url\r\n None\r\n\r\nItems in a DBEnumeratedType class must be of type DBItem.\r\n\r\n >>> class BadItemType(DBEnumeratedType):\r\n ... TESTING = Item(\"Testing\")\r\n Traceback (most recent call last):\r\n ...\r\n TypeError: Items must be of the appropriate type for the DBEnumeratedType,\r\n __builtin__.BadItemType.TESTING\r\n\r\nYou are not able to define a DBEnumeratedType that has two different\r\nDBItems that map to the same numeric value.\r\n\r\n >>> class TwoMapping(DBEnumeratedType):\r\n ... FIRST = DBItem(42, 'First')\r\n ... SECOND = DBItem(42, 'Second')\r\n Traceback (most recent call last):\r\n ...\r\n TypeError: Two DBItems with the same value 42 (FIRST, SECOND)\r\n\r\n=========================\r\nOverriding the sort order\r\n=========================\r\n\r\nBy default the sort order of the items in an enumerated type is defined by the\r\norder in which the Items are declared. This my be overridden by specifying\r\na sort_order attribute in the class.\r\n\r\nIf a sort_order is specified, it has to specify every item in the enumeration.\r\n\r\n >>> class AnimalClassification(EnumeratedType):\r\n ... sort_order = \"REPTILE\", \"INSECT\", \"MAMMAL\"\r\n ... INSECT = Item(\"Insect\")\r\n ... MAMMAL = Item(\"Mammal\")\r\n ... FISH = Item(\"Fish\")\r\n ... REPTILE = Item(\"Reptile\")\r\n Traceback (most recent call last):\r\n ...\r\n TypeError: sort_order for EnumeratedType must contain all and only Item instances ...\r\n\r\nThe sort_order may also appear anywhere in the definition of the class,\r\nalthough convention has it that it appears first, before the Item instances.\r\n\r\n >>> class AnimalClassification(EnumeratedType):\r\n ... sort_order = \"REPTILE\", \"FISH\", \"INSECT\", \"MAMMAL\"\r\n ... INSECT = Item(\"Insect\")\r\n ... MAMMAL = Item(\"Mammal\")\r\n ... FISH = Item(\"Fish\")\r\n ... REPTILE = Item(\"Reptile\")\r\n\r\nThe items attribute of the enumerated type is ordered based on the sort_order.\r\nThe items attribute is also used to control iteration using __iter__.\r\n\r\n >>> for item in AnimalClassification.items:\r\n ... print item.title\r\n Reptile\r\n Fish\r\n Insect\r\n Mammal\r\n\r\nThe sort order also drives the comparison operations.\r\n\r\n >>> reptile, fish, insect, mammal = AnimalClassification.items\r\n >>> reptile < fish < insect < mammal\r\n True\r\n\r\n==========================\r\nExtending enumerated types\r\n==========================\r\n\r\nThe simplest way to extend a class is to derive from it.\r\n\r\n >>> class AnimalClassificationExtended(AnimalClassification):\r\n ... INVERTEBRATE = Item(\"Invertebrate\")\r\n\r\n >>> for item in AnimalClassificationExtended:\r\n ... print item.title\r\n Reptile\r\n Fish\r\n Insect\r\n Mammal\r\n Invertebrate\r\n\r\nThe use_template function inserts the items from the specified enumerated type\r\ninto the new enumerated type. The default case is to take all the enumerated\r\nitems.\r\n\r\n >>> class UIBranchType(EnumeratedType):\r\n ... use_template(BranchType)\r\n >>> for item in UIBranchType:\r\n ... print item.title\r\n Hosted\r\n Mirrored\r\n Imported\r\n\r\nYou can also specify items to be excluded by referring to the attribute name\r\nin the exclude parameter. This can be either a string referring to one name\r\nor a tuple or list that refers to multiple attribute names.\r\n\r\n >>> class UIBranchType2(EnumeratedType):\r\n ... use_template(BranchType, exclude='IMPORTED')\r\n >>> for item in UIBranchType2:\r\n ... print item.title\r\n Hosted\r\n Mirrored\r\n\r\nOr limit the items to those specified:\r\n\r\n >>> class UIBranchType3(EnumeratedType):\r\n ... use_template(BranchType, include=('HOSTED', 'MIRRORED'))\r\n >>> for item in UIBranchType3:\r\n ... print item.title\r\n Hosted\r\n Mirrored\r\n\r\n================================================\r\nGetting from an item back to the enumerated type\r\n================================================\r\n\r\nEach Item in an EnumeratedType has a reference back to the EnumeratedType.\r\n\r\n >>> print repr(apple)\r\n \r\n >>> print repr(apple.enum)\r\n \r\n >>> for item in apple.enum:\r\n ... print item.title\r\n Apple\r\n Pear\r\n Orange\r\n\r\n============\r\nItem.sortkey\r\n============\r\n\r\nThe sortkey attribute of the Items are defined by the sort_order that is\r\ndefined for the enumerated type. The value is often used as a hidden value\r\nin columns in order to ensure appropriate sorting.\r\n\r\n >>> for item in Fruit.items:\r\n ... print item.title, item.sortkey\r\n Apple 0\r\n Pear 1\r\n Orange 2\r\n\r\n >>> for item in BranchType.items:\r\n ... print item.title, item.sortkey\r\n Hosted 0\r\n Mirrored 1\r\n Imported 2\r\n\r\n\r\n==================\r\nNEWS for lazr.enum\r\n==================\r\n\r\n1.1.3 (2011-04-20)\r\n==================\r\n\r\n- added case insensitivity to getting the term by the token value (lp:154556)\r\n\r\n1.1.2 (2009-08-31)\r\n==================\r\n\r\n- removed unnecessary build dependencies\r\n\r\n1.1.1 (2009-08-06)\r\n==================\r\n\r\n- Removed sys.path hack from setup.py.\r\n\r\n1.1 (2009-06-08)\r\n================\r\n\r\n- Added `url` argument to the BaseItem and DBItem constructors.\r\n\r\n\r\n1.0 (2009-03-24)\r\n================\r\n\r\n- Initial release on PyPI", "description_content_type": null, "docs_url": "https://pythonhosted.org/lazr.enum/", "download_url": "https://launchpad.net/lazr.enum/+download", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://launchpad.net/lazr.enum", "keywords": "", "license": "LGPL v3", "maintainer": "", "maintainer_email": "", "name": "lazr.enum", "package_url": "https://pypi.org/project/lazr.enum/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/lazr.enum/", "project_urls": { "Download": "https://launchpad.net/lazr.enum/+download", "Homepage": "https://launchpad.net/lazr.enum" }, "release_url": "https://pypi.org/project/lazr.enum/1.1.4/", "requires_dist": null, "requires_python": null, "summary": "Enums with zope.schema vocabulary support and database-friendly conveniences.", "version": "1.1.4" }, "last_serial": 1497506, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "8911dd0ca36786470cce49a9b97a71e5", "sha256": "8b460a116d97de5485997a5510d164edc53f6338e878f465ee76b3490115488e" }, "downloads": -1, "filename": "lazr.enum-1.0.tar.gz", "has_sig": false, "md5_digest": "8911dd0ca36786470cce49a9b97a71e5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 22366, "upload_time": "2009-03-26T17:41:25", "url": "https://files.pythonhosted.org/packages/f1/89/4b3121675c4d47482a11e7eadfb56425dba555b71d84c976de291867ce76/lazr.enum-1.0.tar.gz" } ], "1.1": [ { "comment_text": "", "digests": { "md5": "27e814b2c3ad7649fb6aa46710e772aa", "sha256": "bc3db7e5fe73e2b1c00f52c590ebe4adca486f267885d20629db34cbbc58c07d" }, "downloads": -1, "filename": "lazr.enum-1.1.tar.gz", "has_sig": true, "md5_digest": "27e814b2c3ad7649fb6aa46710e772aa", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 24822, "upload_time": "2009-06-09T15:44:25", "url": "https://files.pythonhosted.org/packages/da/24/cda9c3e6f416cbf67b21335ab5b745c5c947ac9b58ab83e28a366e0fbffb/lazr.enum-1.1.tar.gz" } ], "1.1.1": [], "1.1.3": [ { "comment_text": "", "digests": { "md5": "b7c1db297069ddd66af0e5c46281df2b", "sha256": "a36107aaf6421fb0e36b9f403567c223d29ae45dce02affa5c7c026069ddb25d" }, "downloads": -1, "filename": "lazr.enum-1.1.3.tar.gz", "has_sig": true, "md5_digest": "b7c1db297069ddd66af0e5c46281df2b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 20231, "upload_time": "2011-04-20T04:27:25", "url": "https://files.pythonhosted.org/packages/ac/ae/fd6dde4423d198a5f6863846f245aa8ab7062df7236a959d0f5fb2861360/lazr.enum-1.1.3.tar.gz" }, { "comment_text": "", "digests": { "md5": "a8d08840ef46e416ce73084c82b098ed", "sha256": "28fd97101e0039fbf8ad3de4ed78240298d8793edc22e86838c63541ae6fffcb" }, "downloads": -1, "filename": "lazr.enum-1.1.4.tar.gz", "has_sig": true, "md5_digest": "a8d08840ef46e416ce73084c82b098ed", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 19752, "upload_time": "2012-04-19T03:30:05", "url": "https://files.pythonhosted.org/packages/93/2f/eeb735f8ea3adcfec4512927bb9fef75d9d24dc6c8d2518226b1564c9ade/lazr.enum-1.1.4.tar.gz" } ], "1.1.4": [] }, "urls": [] }