[Python-ideas] str-type enumerations

Eli Bendersky eliben at gmail.com
Thu Apr 4 19:22:35 CEST 2013


On Thu, Apr 4, 2013 at 9:23 AM, Ethan Furman <ethan at stoneleaf.us> wrote:

> On 04/04/2013 09:06 AM, Eli Bendersky wrote:
>
>
>> On Thu, Apr 4, 2013 at 8:11 AM, Ethan Furman wrote:
>>
>>     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?
>>
>>
>> I'm not entirely sure what you mean here, Ethan. What I do know is that
>> enumeration values are hashable, so they can be
>> used as keys in dictionaries.
>>
>
> In the example above 'Mn$' is the field, and '(1,6)' are the first six
> charecters in the field.  So the custom mapping has to parse the key passed
> to in in order to return the proper value; it looks something like this:
>
>     def __getitem__(self, key):
>         real_key = key[:3]
>         field = self.dict[real_key]
>         first, length = key[3:][1:-1].split(',')
>         first = first - 1
>         last = first + length
>         data = field[first:last]
>         return data
>
> If key is a str-based enum, will this work?


No. Enum allows having string values, that's it. The object itself is a
EnumValue, though, it's not "isinstance(str)".

You can easily use "key.value" there, though, as key.value *is* the actual
value assigned to the enum value.

That's a strange use case, though :-)

Eli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130404/f6b32363/attachment.html>


More information about the Python-ideas mailing list