Commits

funkybob  committed 1a19df0

Added ChoicesList class

  • Participants
  • Parent commits 187e441

Comments (0)

Files changed (1)

File gnocchi/tools/utils.py

             import_app(settings.PROJECT_NAME)
 
     return autodiscover
+
+class ChoicesList(object):
+    '''
+    A list-like object to make Choices easier to use.
+    Construct by passing a list of two-tuples, and get free access to attributes
+    on the object for names of the values
+
+    >>> choices = ChoicesList((0, 'First'), (1, 'Second'), (5, 'Something else')
+    >>> choices.SECOND
+    1
+    >>> choice.SOMETHING_ELSE
+    5
+    >>> list(choices)
+    [(0, 'First'), (1, 'Second')]
+    '''
+    def __init__(self, *items):
+        self.items = items
+        for val, label in items:
+            setattr(self, label.upper().replace(' ', '_'), val)
+
+    def __iter__(self):
+        return self.items.__iter__()
+
+    def __getitem__(self, key):
+        return self.items[key]