Last active
October 8, 2015 21:58
-
-
Save Fak3/3394633 to your computer and use it in GitHub Desktop.
Choices without metaclass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment