[Python-ideas] str-type enumerations
Eli Bendersky
eliben at gmail.com
Thu Apr 4 18:06:13 CEST 2013
On Thu, Apr 4, 2013 at 8:11 AM, Ethan Furman <ethan at stoneleaf.us> 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.
Actually, __hash__ is object.__hash__ for enum values. You can look at the
full code here:
http://bazaar.launchpad.net/~barry/flufl.enum/trunk/view/head:/flufl/enum/_enum.py
Eli
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130404/e6bf7902/attachment.html>
More information about the Python-ideas
mailing list