Jacob Smullyan avatar Jacob Smullyan committed 8469a05

initial import

Comments (0)

Files changed (4)

+README
+setup.py
+djenum/__init__.py
+This package contains a tiny convenience class, ChoiceEnum, I find
+myself always adding to Django projects.  If you need it, it is
+self-explanatory; otherwise, it is of no conceivable interest.

djenum/__init__.py

+import re
+import itertools
+
+
+class ChoiceEnum(object):
+    """
+    A class to simplify the creation of Django choice enumerations.
+
+    To generate an integer enum, starting at 1, it suffices to give a
+    list of strings, or even a single string that will be split on
+    whitespace:
+
+    >>> color_choices = ChoiceEnum('red green blue yellow brown')
+    >>> color_choices.RED
+    1
+    >>> color_choices.GREEN
+    2
+
+    The enum tuples can be accessed by index as well (as Django
+    requires):
+
+    >>> color_choices[3]
+    (4, 'yellow')
+
+    You can also pass in a list of value / name tuples, with value of
+    any type:
+
+    >>> cc_types = ChoiceEnum((('AMEX', 'American Express'), ('VISA', 'Visa')))
+    >>> cc_types.AMERICAN_EXPRESS
+    'AMEX'
+
+    To facilitate making string enumerations where the value and name
+    are identical, you can set the constructor parameter 'enum_is_int'
+    to a false value, and avoid having to specify tuples:
+
+    >>> cc_types = ChoiceEnum('AMEX VISA DISCOVER', 0)
+    >>> cc_types.DISCOVER
+    'DISCOVER'
+
+    Or, equivalently, use StringChoiceEnum, which does this for you.
+
+    """
+
+    def __init__(self, choices, enum_is_int=True):
+
+        self._enum_is_int = enum_is_int
+        if isinstance(choices, basestring):
+            choices = choices.split()
+        if isinstance(choices, (list, tuple)) and \
+               all(isinstance(x, tuple) and len(x) == 2 for x in choices):
+            values = choices
+        else:
+            if enum_is_int:
+                values = zip(itertools.count(1), choices)
+            else:
+                values = [(x, x) for x in choices]
+
+        for v, n in values:
+            # some useful cleanup
+            attr = n.upper().replace(' ', '_').replace('-', '_').replace('_&_', '_AND_')
+            attr = re.sub('[^A-Za-z0-9_-]', '', attr)
+            setattr(self, attr, v)
+        self._choices = values
+
+    def __getitem__(self, idx):
+        return self._choices[idx]
+
+    def get_name_for_value(self, value):
+        for v, n in self._choices:
+            if v == value:
+                return n
+
+
+class StringChoiceEnum(ChoiceEnum):
+    """
+    Like a ChoiceEnum, but with string values by default.
+
+    >>> color_choices = StringChoiceEnum('red green blue yellow brown')
+    >>> color_choices.RED
+    'red'
+    >>> color_choices.GREEN
+    'green'
+    
+    """
+    def __init__(self, choices):
+        super(StringChoiceEnum, self).__init__(choices, enum_is_int=False)
+
+
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()
+from distutils.core import setup
+
+VERSION='0.1'
+
+DESCRIPTION="A utility for writing Django choice enumerations."
+
+setup(name="django-enum",
+      url="http://bitbucket.org/smulloni/django-enum/",
+      version=VERSION,
+      description=DESCRIPTION,
+      author="Jacob Smullyan",
+      author_email="smulloni@smullyan.org",
+      license='BSD',
+      classifiers=['Framework :: Django',
+                   'License :: OSI Approved :: BSD License',
+                   'Topic :: Utilities'],
+      packages=['djenum'])
+      
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.