Automatically fill up missing values in an python `Enum`

Table of Contents

Python 3.4 introduce the missing Enumerations, which I already used multiple times since they have some nice practical features.

In an python Enum you have an name-value pairs. Well in most cases the your Enumeration you defined contains all possible values for your scope.

But in some cases you only define those name-values from which you know that they exists. Then if you want to get the Enum object from an value like YourEnum(3) an ValueError will be thrown if 3 is not defined.

For example your Enum EnumType represents an type field in an protocol. Over the years more and more types are maybe invented ans specified in many different documents. Or someone just do some experiments and simply set an not standardised value. So your EnumType can be incomplete but still want to support all possible values.

Maybe you want to make that more fault robust and don't want that an Error is thrown. Well sadly python enum are singletons and therefore you can't change them after initialization.

I solved that problem with an python decorator which fills up the enum with every possible value, where the name is auto generated. Well "every possible value" can't be infinity; in this case the decorator takes a list of values as input and he generate a new enum member if it not already exist in the class. What the input list contains depends on your needs; in case of the example above you can set it to the size of the type field in the protocol (range(2**4) if the type field has 4 bit).

Get the class here

Example

import enum
from fill_enum import FillEnum

@enum.unique
@FillEnum(range(2**4), enum.Enum, prefix='val', module=__name__)
class TypeEnum(object):
    """This doc will be copied to the new Enum class."""
    reverse = 1
    direct = 3
    upstream = 4
    downstream = 7

And the example in action (ignore the last line. He seems to detect <> as html tags and automatically closes them at the end. I don't know why he does that, I do not write them and python is set as language)

>>> TypeEnum(3)

>>> TypeEnum(6)

>>> TypeEnum.direct

>>> TypeEnum['val6']

>>> help(TypeEnum)
class TypeEnum(enum.Enum)
 |  This doc will be copied to the new Enum class.
 |  
 |  Method resolution order:
 |      TypeEnum
 |      enum.Enum
 |      builtins.object
 |  
 |  Data and other attributes defined here:
 |  
 |  direct = 
 |  
 |  downstream = 
 |  
 |  reverse = 
 |  
 |  upstream = 
 |  
 |  val0 = 
 |  
 |  val10 = 
 |  
 |  val11 = 
 |  
 |  val12 = 
 |  
 |  val13 = 
 |  
 |  val14 = 
 |  
 |  val15 = 
 |  
 |  val2 = 
 |  
 |  val5 = 
 |  
 |  val6 = 
 |  
 |  val8 = 
 |  
 |  val9 = 
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from enum.Enum:
 |  
 |  name
 |      The name of the Enum member.
 |  
 |  value
 |      The value of the Enum member.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from enum.EnumMeta:
 |  
 |  __members__
 |      Returns a mapping of member name->value.
 |      
 |      This mapping lists all enum members, including aliases. Note that this
 |      is a read-only view of the internal mapping.

Tags: 

 
 
 

User login