[Python-ideas] str-type enumerations

Ethan Furman ethan at stoneleaf.us
Thu Apr 4 17:11:52 CEST 2013


On 04/04/2013 06:56 AM, Eli Bendersky wrote:
> On Wed, Apr 3, 2013 at 8:23 PM, Eli Bendersky wrote:
>> On Tue, Apr 2, 2013 at 12:25 PM, Ethan Furman wrote:
>>
>>> I'm not trying to beat this dead horse (really!) but back when the enumeration discussions were going fast and
>>> furious one of the things asked for was enumerations of type str, to which others replied that strings named
>>> themselves.
>>>
>>> Well, I now have a good case where I don't want to use the string that names itself: 'Mn$(1,6)'  I would match
>>> rather write item_code!  :)
>>
>> The latest incarnation of flufl.enum that went through the round of discussions during PyCon allows string values in
>> enumerations, if you want them:
>>
>> >>> from flufl.enum import Enum
>> >>> class WeirdNames(Enum):
>> ...   item_code = 'Mn$(1,6)'
>> ...   other_code = '#$%#$^'
>> ...
>> >>> WeirdNames.item_code
>> <EnumValue: WeirdNames.item_code [value=Mn$(1,6)]>
>> >>> i = WeirdNames.item_code
>> >>> i
>> <EnumValue: WeirdNames.item_code [value=Mn$(1,6)]>
>> >>> i.value
>> 'Mn$(1,6)'
>> >>> print(i)
>> WeirdNames.item_code
>>
>> I haven't updated PEP 435 to reflect this yet, hope to do so in the next day or two.
>
> The PEP is up-to-date now, and mentions string-valued enums as well.

Wow -- looks like flufl.enum has come a long way!  Cool.

My use case for the str enum is to use it as a dict key for a custom mapping to a Business Basic file; this means that 
the str value will be pulled apart and disected to see exactly where in a fixed-length field it needs to pull data from 
(in the example above it would be the first six characters as BB is 1-based).

Will a str-based enum handle that, or will the custom mapping have to be updated to check for a str or an enum, and if 
an enum use the .value?

--
~Ethan~



More information about the Python-ideas mailing list