Package mrv :: Module enum
[hide private]
[frames] | no frames]

Module enum

source code

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.

Example Code:
>>>     # 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

Contact: garret at bgb dot cc

License: freeware

Classes [hide private]
  Element
Internal helper class used to represent an ordered abstract value.
  Enumeration
This class represents an enumeration.
Functions [hide private]
 
create(*elements, **kwargs)
Factory method for Enumerations.
source code
Variables [hide private]
  __package__ = 'mrv'
Function Details [hide private]

create(*elements, **kwargs)

source code 

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), ...)

Parameters:
  • kwargs -
    • cls: The class to create an enumeration with, must be an instance of Enumeration

    • elmcls: The class to create elements from, must be instance of Element

    • bitflag: if True, default False, the values created will be suitable as bitflags.

      This will fail if you passed more items in than supported by the OS ( 32 , 64, etc ) or if you pass in tuples and thus define the values yourself.

Raises:
  • TypeError,ValueError - if bitflags cannot be supported in your case