Package: extenum
extenumExtended Enum classes for Python 3 enum module.Theenummodule was added
since 3.4. That's good enough for simple use.
The extenum is strongly inspired by Java Enum style described inEffective Javaand provides additional feature.How to install$pipinstallextenumConstantSpecificEnumConstantSpecificEnumclass is inherited the standard Enum class and
provides the feature of constant specific method and function overloading
for Enum members.ReadEffective Javafor more detail.Constant specific method implementationLet's try to create Enum class withConstantSpecificEnum.
To use a method as function overloading, add@overload(CONSTANT)decorator
on that method. Theoverloaddecorator is implicitly defined as well as
you'll see later inImplicitEnumsection.>>>fromextenumimportConstantSpecificEnum>>>classOperation(ConstantSpecificEnum):...PLUS='+'...MINUS='-'...TIMES='*'...DIVIDE='/'......@overload(PLUS)...defapply(self,x,y):...returnx+y......@overload(MINUS)...defapply(self,x,y):...returnx-y......@overload(TIMES)...defapply(self,x,y):...returnx*y......@overload(DIVIDE)...defapply(self,x,y):...returnx/y...>>>forname,constinOperation.__members__.items():...print(name,':',const.apply(2,4))...PLUS:6MINUS:-2TIMES:8DIVIDE:0.5Strategy enum patternThe strategy enum is more complex pattern based on constant specific method.>>>fromextenumimportConstantSpecificEnum>>>classPayrollDay(ConstantSpecificEnum):......classPayType(ConstantSpecificEnum):...WEEKDAY=1...WEEKEND=2......@overload(WEEKDAY)...defovertime_pay(self,hours,pay_rate):...return0ifhours<=8else(hours-8)*pay_rate/2......@overload(WEEKEND)...defovertime_pay(self,hours,pay_rate):...returnhours*pay_rate/2......defpay(self,hours_worked,pay_rate):...base_pay=hours_worked*pay_rate...overtime_pay=self.overtime_pay(hours_worked,pay_rate)...returnbase_pay+overtime_pay......MONDAY=PayType.WEEKDAY...TUESDAY=PayType.WEEKDAY...WEDNESDAY=PayType.WEEKDAY...THURSDAY=PayType.WEEKDAY...FRIDAY=PayType.WEEKDAY...SATURDAY=PayType.WEEKEND...SUNDAY=PayType.WEEKEND......defpay(self,hours_worked,pay_rate):...returnself.value.pay(hours_worked,pay_rate)...>>>PayrollDay.MONDAY.pay(10,1000.0)11000.0>>>PayrollDay.WEDNESDAY.pay(8,1000.0)8000.0>>>PayrollDay.SATURDAY.pay(10,1000.0)15000.0>>>PayrollDay.SUNDAY.pay(8,1000.0)12000.0ImplicitEnumBefore describing whatImplicitEnumclass is, read good article written by
Nick Coghlan as below.Support for alternate declaration syntaxesOK. I guess you've already understood why the standard enum module haven't
support implicit declaration syntax.Put aside its needs for now, Nick indicates how to implementImplicitEnum.
So, let's try to implement it experimentally using the special method,__missing__in defaultdict and__prepare__in Metaclass.>>>fromextenumimportImplicitEnum>>>classColor(ImplicitEnum):...RED...GREEN...BLUE...>>>forname,constinColor.__members__.items():...print(name,':',const.value)...RED:1GREEN:2BLUE:3It works well if some constants are explicit and the rest are implicit.>>>classNumbers(ImplicitEnum):...ONE=1...TWO=2...THREE...>>>Numbers.THREE.value3However, it depends on the declaration order.>>>classDuplicatedValues(ImplicitEnum):...ONE...TWO=1...THREE=1...>>>DuplicatedValues.ONE.value1>>>DuplicatedValues.TWO.value1>>>DuplicatedValues.THREE.value1EnumSetEnumSet is one of the specialized implementation of Set interface for enumeration type,
inspired byJava EnumSet.It provides utility functions to handle multiple Enum constants.>>>fromenumimportEnum>>>fromextenumimportEnumSet>>>classMode(Enum):...READ=4...WRITE=2...EXECUTE=1......@classmethod...defset_of(cls,values):...opts=EnumSet.none_of(cls)...forvalueinvalues:...opts.add(cls(value))...returnopts...>>>Mode.set_of([4,2])# doctest: +SKIPEnumSet({<Mode.READ:4>,<Mode.WRITE:2>})To create EnumSet with all Enum members:>>>EnumSet.all_of(Mode)# doctest: +SKIPEnumSet({<Mode.READ:4>,<Mode.WRITE:2>,<Mode.EXECUTE:1>})Or, to create EnumSet with arbitrary Enum members:>>>enumset=EnumSet.of(Mode.READ,Mode.EXECUTE)>>>enumset# doctest: +SKIPEnumSet({<Mode.READ:4>,<Mode.EXECUTE:1>})>>>enumset.update(EnumSet.of(Mode.READ,Mode.WRITE))>>>enumset# doctest: +SKIPEnumSet({<Mode.READ:4>,<Mode.WRITE:2>,<Mode.EXECUTE:1>})ChangeLog0.8.1 (2020-11-07)change to use pytest-flake8 for testingsupport python 3.6+0.8.0 (2015-03-15)add EnumSet0.7.0 (2015-03-06)remove RegisterFactory for simplicity0.6.0 (2015-03-05)add ImplicitEnum0.5.0 (2015-03-01)first release