public
Last active

Choices without metaclass

  • Download Gist
choices.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
import inspect
#y
 
class Choice(object):
def __init__(self):
self._data = []
for name, value in inspect.getmembers(self):
if not name.startswith("_") and not inspect.isfunction(value):
if isinstance(value,tuple) and len(value) > 1:
data = value
else:
data = (value, " ".join([x.capitalize() for x in name.split("_")]),)
self._data.append(data)
setattr(self, name, data[0])
 
def __iter__(self):
for value, data in self._data:
yield value, data
 
class Foo(Choice):
A = 1
B = 2
C = 3, 'CEEEE'
 
print 'list(Foo()) :', list(Foo())
# [(1, 'A'), (2, 'B'), (3, 'CEEEE')]
 
print 'Foo().C :', Foo().C
# 3

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.