Message226468
Right. We can still use the alias machinery to accomplish this task for us, and avoid the metaclass hacking:
-- python2 sample code ------------------------------------
# -*- coding: utf-8 -*-
from enum import Enum
class MultiValueEnum(Enum):
def __new__(cls, *values):
obj = object.__new__(cls)
obj._value_ = values[0]
obj._all_values = values
for alias in values[1:]:
cls._value2member_map_[alias] = obj
return obj
def __repr__(self):
return "<%s.%s: %s>" % (
self.__class__.__name__,
self._name_,
', '.join(["'%s'" % v for v in self._all_values])
)
class Suits(MultiValueEnum):
CLUBS = '♣', 'c', 'C', 'clubs', 'club'
DIAMONDS = '♦', 'd', 'D', 'diamonds', 'diamond'
HEARTS = '♥', 'h', 'H', 'hearts', 'heart'
SPADES = '♠', 's', 'S', 'spades', 'spade'
print(Suits.HEARTS)
print(repr(Suits.HEARTS))
print(Suits('d'))
print(Suits('club'))
print(Suits('S'))
print(Suits('hearts'))
----------------------------------------------------------------
And the output:
Suits.HEARTS
<Suits.HEARTS: '♥', 'h', 'H', 'hearts', 'heart'>
Suits.DIAMONDS
Suits.CLUBS
Suits.SPADES
Suits.HEARTS
----------------------------------------------------------------
I'm still going to fix the bug, though. :)
Oh, the above does not fix the IncorrectAliasBehavior class, but honestly I'm not sure what you are trying to accomplish there. |
|
Date |
User |
Action |
Args |
2014-09-06 02:54:19 | ethan.furman | set | recipients:
+ ethan.furman, barry, eli.bendersky, kissgyorgy |
2014-09-06 02:54:19 | ethan.furman | set | messageid: <1409972059.24.0.618017657573.issue22339@psf.upfronthosting.co.za> |
2014-09-06 02:54:19 | ethan.furman | link | issue22339 messages |
2014-09-06 02:54:18 | ethan.furman | create | |
|