This module is designed to be the equivalent of the enum type in other languages. An enumeration object is created at run time, and contains named members that are the enumeration elements.
The enumeration is also a tuple of all of the values in it. You can iterate through the values, perform ‘in’ tests, slicing, etc. It also includes functions to lookup specific values by name, or names by value.
You can specify your own element values, or use the create factory method to create default Elements. These elements are unique system wide, and are ordered based on the order of the elements in the enumeration. They also are _repr_’d by the name of the element, which is convenient for testing, debugging, and generation text output.
>>> # Using Element values
>>> Colors = Enumeration.create('red', 'green', 'blue')
>>> # Using explicitly specified values
>>>Borders = Enumeration.create(('SUNKEN', 1),
>>> ('RAISED', 32),
>>> ('FLAT', 2))
>>> x = Colors.red
>>> y = Colors.blue
>>> assert x < y
>>> assert x == Colors('red')
>>> assert Borders.FLAT == 2:
>>> assert 1 in Borders
note: | slightly modified by Sebastian Thiel to be more flexible and suitable as base class |
---|
Factory method for Enumerations. Accepts of list of values that can either be strings or (name, value) tuple pairs. Strings will have an Element created for use as their value. If you provide elements, the member returned when you access the enumeration will be the element itself.
Example: Enumeration.create(‘fred’, ‘bob’, (‘joe’, 42)) Example: Enumeration.create(‘fred’, cls = EnumerationSubClass ) Example: Enumeration.create(Element(‘fred’, Marge), ...)
Parameter: | kwargs –
|
---|---|
Raises TypeError,ValueError: | |
if bitflags cannot be supported in your case |
Bases: object
Internal helper class used to represent an ordered abstract value.
The values have string representations, have strictly defined ordering (inside the set) and are never equal to anything but themselves.
They are usually created through the create factory method as values for Enumerations.
They assume that the enumeration member will be filled in before any comparisons are used. This is done by the Enumeration constructor.
Returns: | name of the element |
---|
Returns: | own value |
---|
Bases: tuple
This class represents an enumeration. You should not normally create multiple instances of the same enumeration, instead create one with however many references are convenient.
The enumeration is a tuple of all of the values in it. You can iterate through the values, perform ‘in’ tests, slicing, etc. It also includes functions to lookup specific values by name, or names by value.
You can specify your own element values, or use the create factory method to create default Elements. These elements are unique system wide, and are ordered based on the order of the elements in the enumeration. They also are _repr_’d by the name of the element, which is convenient for testing, debugging, and generation text output.
Note: | pickling this class with Elements will fail as they contain cyclic references that it cannot deal with |
---|---|
Todo: | implement proper pickle __getstate__ and __setstate__ that deal with that problem |
Look up the name of an enumeration element, given it’s value.
If there are multiple elements with the same value, you will only get a single matching name back. Which name is undefined.
Raises ValueError: | |
---|---|
if value is not a part of our enumeration |
Returns: | element following after given element |
---|---|
Parameters: |
|
Raises ValueError: | |
if wrap_around is False and there is no next element |
Returns: | element coming before the given element |
---|---|
Parameters: |
|
Raises ValueError: | |
if wrap_around is False and there is no previous element |
Look up the enumeration value for a given element name.
Raises ValueError: | |
---|---|