Add Choice class

This commit is contained in:
Roberto Rosario
2012-08-18 10:25:34 -04:00
parent cef6af6cca
commit 22bfb93504

View File

@@ -433,3 +433,25 @@ def copyfile(source, destination, buffer_size=1024 * 1024):
source_descriptor.close()
destination_descriptor.close()
#From: http://tomforb.es/using-python-metaclasses-to-make-awesome-django-model-field-choices?pid=0&utm_source=agiliq&utm_medium=agiliq
import inspect
class Choice(object):
class __metaclass__(type):
def __init__(self, name, type, other):
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