Package: basicenum
basicenumSimple(r) enums.basicenum.compatA (mostly) API-compatible re-implementation ofenum.Enumfrom the
stdlib (plus related code).The goal for this module was to try and re-implement as much of the API ofenum.Enumas possible while using modern Python features. While this does lead
to some API breakage (e.g.type(enum.member) == type(enum)is no longer true),
it mostly revolves around metaclass-level details. If you rely on the surface
API forenum.Enum, then this module should be compatible.API CompatibilityUsing the example enum:classColour(Enum):RED=auto()GREEN=auto()BLUE=auto()The compatibility withenum.Enumis:FeatureSupported?repr(Colour.RED)✅str(Colour.RED)✅type(Colour.RED)❌ (Memberinstead)isinstance(Colour.RED, Colour)✅iter(Colour)✅hash(Colour.RED)✅Colour(1)✅Colour["RED"]✅Colour.RED in Colour✅Colour.RED.name✅Colour.RED.value✅auto()✅_generate_next_value_()✅Colour.__members__✅Colour.RED == Colour.RED✅Restricted subclassing❌Pickling✅Functional API✅ (viacreate())Type CheckingUnfortunately, type checkers hard-code their support forenum.Enum. That means
they do not recognize members ofbasicenum.compat.Enumas being instances ofMemberor matching the API of members ofenum.Enum.Luckily, you can lie to the type checkers. You can tell them to type check as if
you're usingenumwhile usingbasicenum.compatduring execution.fromtypingimportTYPE_CHECKINGifTYPE_CHECKING:fromenumimportEnum,autoelse:frombasicenum.compatimportEnum,autoBenchmarkingIf you install the[benchmark]extra, you can userichbenchto see a performance
comparison betweenenumandbasicenum.compat.ResultsOn a MacBook Pro (Retina, 13-inch, Early 2015):richbench--repeat5--times5--benchmarkcompatbenchmarks/BenchmarkMinMaxMeanMin (+)Max (+)Mean (+)import0.3740.5030.4220.117 (3.2x)0.120 (4.2x)0.119 (3.5x)creation w/auto()0.4220.4290.4250.075 (5.7x)0.077 (5.5x)0.076 (5.6x)_generate_next_value_()0.3730.3830.3760.077 (4.8x)0.078 (4.9x)0.078 (4.8x)creation w/ constants0.3330.3510.3390.060 (5.5x)0.066 (5.4x)0.063 (5.4x)functional API0.3920.3970.3940.070 (5.6x)0.074 (5.4x)0.072 (5.5x)isinstance(..., Enum)0.1890.1910.1900.279 (-1.5x)0.282 (-1.5x)0.280 (-1.5x)iter(Enum)0.9120.9220.9170.197 (4.6x)0.199 (4.6x)0.198 (4.6x)Enum(...)0.2970.2970.2970.078 (3.8x)0.079 (3.8x)0.078 (3.8x)Enum[...]0.1210.1220.1210.075 (1.6x)0.139 (-1.1x)0.093 (1.3x)... in Enum0.2670.3750.3080.162 (1.6x)0.164 (2.3x)0.163 (1.9x)member access0.3330.3370.3340.118 (2.8x)0.121 (2.8x)0.119 (2.8x)value access0.7350.7610.7440.141 (5.2x)0.143 (5.3x)0.142 (5.2x)equality0.3430.3450.3440.103 (3.3x)0.107 (3.2x)0.105 (3.3x)repr0.3790.3860.3820.236 (1.6x)0.238 (1.6x)0.237 (1.6x)hashing0.3450.3550.3480.245 (1.4x)0.250 (1.4x)0.247 (1.4x)pickling0.2700.2720.2710.270 (-1.0x)0.274 (-1.0x)0.272 (-1.0x)unpickling0.2290.2320.2310.230 (-1.0x)0.232 (-1.0x)0.231 (-1.0x)__members__0.3730.3810.3760.046 (8.1x)0.047 (8.1x)0.046 (8.1x)@unique0.5180.5230.5200.104 (5.0x)0.109 (4.8x)0.106 (4.9x)Module ContentsEnumThe class to inherit from to create an enum.MemberThe class which all enum members are instances of.auto()Function for automatic, incrementing integer member values.@uniqueGuarantee that all enum members have unique values.RaisesValueErrorif values are not all unique.create()A re-implementation of the functional API ofenum.Enum.defcreate(enum_name,member_names,/,*,module=None,qualname=None,type=None,start=1):...