home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / arm / util / enum.py < prev    next >
Encoding:
Python Source  |  2012-05-18  |  3.0 KB  |  117 lines

  1. """
  2. Basic enumeration, providing ordered types for collections. These can be
  3. constructed as simple type listings, ie:
  4. >>> insects = Enum("ANT", "WASP", "LADYBUG", "FIREFLY")
  5. >>> insects.ANT
  6. 'Ant'
  7. >>> insects.values()
  8. ['Ant', 'Wasp', 'Ladybug', 'Firefly']
  9.  
  10. with overwritten string counterparts:
  11. >>> pets = Enum(("DOG", "Skippy"), "CAT", ("FISH", "Nemo"))
  12. >>> pets.DOG
  13. 'Skippy'
  14. >>> pets.CAT
  15. 'Cat'
  16.  
  17. or with entirely custom string components as an unordered enum with:
  18. >>> pets = LEnum(DOG="Skippy", CAT="Kitty", FISH="Nemo")
  19. >>> pets.CAT
  20. 'Kitty'
  21. """
  22.  
  23. def toCamelCase(label):
  24.   """
  25.   Converts the given string to camel case, ie:
  26.   >>> toCamelCase("I_LIKE_PEPPERJACK!")
  27.   'I Like Pepperjack!'
  28.   
  29.   Arguments:
  30.     label - input string to be converted
  31.   """
  32.   
  33.   words = []
  34.   for entry in label.split("_"):
  35.     if len(entry) == 0: words.append("")
  36.     elif len(entry) == 1: words.append(entry.upper())
  37.     else: words.append(entry[0].upper() + entry[1:].lower())
  38.   
  39.   return " ".join(words)
  40.  
  41. class Enum:
  42.   """
  43.   Basic enumeration.
  44.   """
  45.   
  46.   def __init__(self, *args):
  47.     self.orderedValues = []
  48.     
  49.     for entry in args:
  50.       if isinstance(entry, str):
  51.         key, val = entry, toCamelCase(entry)
  52.       elif isinstance(entry, tuple) and len(entry) == 2:
  53.         key, val = entry
  54.       else: raise ValueError("Unrecognized input: %s" % args)
  55.       
  56.       self.__dict__[key] = val
  57.       self.orderedValues.append(val)
  58.   
  59.   def values(self):
  60.     """
  61.     Provides an ordered listing of the enumerations in this set.
  62.     """
  63.     
  64.     return list(self.orderedValues)
  65.   
  66.   def indexOf(self, value):
  67.     """
  68.     Provides the index of the given value in the collection. This raises a
  69.     ValueError if no such element exists.
  70.     
  71.     Arguments:
  72.       value - entry to be looked up
  73.     """
  74.     
  75.     return self.orderedValues.index(value)
  76.   
  77.   def next(self, value):
  78.     """
  79.     Provides the next enumeration after the given value, raising a ValueError
  80.     if no such enum exists.
  81.     
  82.     Arguments:
  83.       value - enumeration for which to get the next entry
  84.     """
  85.     
  86.     if not value in self.orderedValues:
  87.       raise ValueError("No such enumeration exists: %s (options: %s)" % (value, ", ".join(self.orderedValues)))
  88.     
  89.     nextIndex = (self.orderedValues.index(value) + 1) % len(self.orderedValues)
  90.     return self.orderedValues[nextIndex]
  91.   
  92.   def previous(self, value):
  93.     """
  94.     Provides the previous enumeration before the given value, raising a
  95.     ValueError if no such enum exists.
  96.     
  97.     Arguments:
  98.       value - enumeration for which to get the previous entry
  99.     """
  100.     
  101.     if not value in self.orderedValues:
  102.       raise ValueError("No such enumeration exists: %s (options: %s)" % (value, ", ".join(self.orderedValues)))
  103.     
  104.     prevIndex = (self.orderedValues.index(value) - 1) % len(self.orderedValues)
  105.     return self.orderedValues[prevIndex]
  106.  
  107. class LEnum(Enum):
  108.   """
  109.   Enumeration that accepts custom string mappings.
  110.   """
  111.   
  112.   def __init__(self, **args):
  113.     Enum.__init__(self)
  114.     self.__dict__.update(args)
  115.     self.orderedValues = sorted(args.values())
  116.  
  117.