ANN: Python 3 enum package

E

Ethan Furman

Greetings!

There was a recent thread on Python-Ideas about adding an enumeration package
to the stdlib. One idea that seemed to be fairly popular (at least I like it a lot ;)
was the use of a metaclass to automatically assign values when a name lookup failed.

It was also fairly unpopular and so probably won't make it in (although another good one
seems poised to be accepted -- flufl.enum, I believe).

At any rate, I finished putting my prototype together, and it is available on github here:

https://bitbucket.org/stoneleaf/enum


And here is some sample code so you can see if you would like to take a closer look
(comments and critiques welcome):


8<----------------------------------------------------------------------------------
#!/usr/bin/python3

from enum import Enum, BitMaskEnum, UniqueEnum, enum

class Color(Enum):
"basic red/green/blue"
black
red
green
blue

print(Color)
print(Color.green)
print(Color(2))
print(Color('green'))
print(repr(Color.green))

class MoreColor(Color):
"and some cyan/magenta/yellow"
cyan
magenta
yellow

print(MoreColor)
print(MoreColor.red)
print(MoreColor(1))
print(MoreColor('red'))
print(repr(MoreColor.red))

class Errors(Enum):
missing
closed
corrupted
modified

print(Errors)
print(Errors.closed)
print(Errors(1))
print(Errors('closed'))
print(repr(Errors.closed))

print(Color.red in Color)
print(MoreColor.magenta in MoreColor)
print(Color.red in MoreColor)
print(Color.red == MoreColor.red)

print(not (Errors.closed in MoreColor))
print(not (Errors.closed in Color))
print(not (Errors.closed == Color.red))
print(not (Errors.closed == MoreColor.red))

class Status(BitMaskEnum):
has_memo
binary
increment
unicoded

print(Status)
for e in Status:
print(repr(e))
print(Status.binary | Status.increment)
print(Status(5))
print(Status('binary|increment'))
print(repr(Status(5)))

class Position(UniqueEnum):
LEFT
RIGHT
TOP
BOTTOM

print(Position)
print(Position.TOP)
print(Position('top'))
print(Position('TOP'))
print(repr(Position.TOP))

#return class discarded
print(Enum.create('IceCream', 'chocolate vanilla strawberry'))

#class stuffed into globals
Enum.create('IceCream', 'chocolate vanilla strawberry', namespace=globals())
print(IceCream)

# can even subclass this way
import sys
Enum.create(
'MoreIceCream',
'cherry rockyroad coconut',
bases=(IceCream,),
namespace=sys.modules)
from MoreIceCream import * # and import from it
print(cherry)
print()

# and if you don't like the magic, don't use it :)
class Color(BitMaskEnum):
black = enum('midnight', '#000', value=0)
red = enum('sunset', '#001')
green = enum('emerald', '#010')
blue = enum('sky', '#100')

def __init__(yo, desc, code):
"value is automatically saved"
yo.desc = desc
yo.code = code

def describe(yo, noun):
return "%s %s" % (yo.desc, noun)

print(Color)
print(Color.green)
print(repr(Color.green))
print(Color.green.describe('glow'))

8<----------------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,068
Latest member
MakersCBDIngredients

Latest Threads

Top