4.1.4.2. EnumFactory

This module provides an implementation for enumerate data type.

The enumerate factory EnumFactory() builds a enumerate from a list of names and assigns to these constants a value from 0 to N-1, where N is the number of constants. For example:

enum = EnumFactory('Enum1', ('cst1', 'cst2'))

will build a enumerate with cst1 set to 0 and cst2 set to 1.

We can retrieve a constant’s value using an integer context:

int(enum.cst1)

and the constant’s name using:

str(enum.cst1)

We can test if two constants are equal using:

enum1.cst == enum2.cst

or with an object that implements the int protocol:

enum1.cst == obj
# is equivalent to
int(enum1.cst) == int(obj)

The number of constants could be retrieved with:

len(enum)

The enumerate factory :func:`ExplicitEnumFactory` is a variant that permits to specify the constant`s values::
      
enum2 = ExplicitEnumFactory('Enum2', {'cst1':1, 'cst2':3})

We can test if a value is in the enumerate using:

constant_value in enum2
class PyOpenGLng.Tools.EnumFactory.EnumConstant(name, value)[source]

Bases: object

Define an Enum Constant

PyOpenGLng.Tools.EnumFactory.EnumFactory(enum_name, enum_tuple)[source]

Return an EnumMetaClass instance, where enum_name is the class name and enum_tuple is an iterable of constant’s names.

class PyOpenGLng.Tools.EnumFactory.EnumMetaClass[source]

Bases: PyOpenGLng.Tools.EnumFactory.ReadOnlyMetaClass

This metaclass implements the len() protocol.

PyOpenGLng.Tools.EnumFactory.ExplicitEnumFactory(enum_name, enum_dict)[source]

Return an ExplicitEnumMetaClass instance, where enum_name is the class name and enum_dict is a dict of constant’s names and their values.

class PyOpenGLng.Tools.EnumFactory.ExplicitEnumMetaClass[source]

Bases: PyOpenGLng.Tools.EnumFactory.ReadOnlyMetaClass

This metaclass implements the in operator.

class PyOpenGLng.Tools.EnumFactory.ReadOnlyMetaClass[source]

Bases: type

This metaclass implements a class where attributes are read only.

Previous topic

4.1.4.1. AttributeDictionaryInterface

Next topic

4.1.4.3. Interval

This Page