Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
enum.py
Go to the documentation of this file.
1 ## @package utils.enum
2 
3 from collections import OrderedDict
4 
5 ## A primitive enum class (Python has none).
6 # Sample usage:
7 # \code
8 # Animals = Enum(['COW', 'GOAT', 'ELEPHANT'])
9 # Animals.COW #this gives 0
10 # Animals.GOAT #this gives 1
11 # Animals.ELEPHANT #this gives 2
12 # Animals.TURKEY #this gives AttributeError
13 #
14 # #Supports ordered iteration:
15 # for creature in Animals:
16 # print creature
17 # #This prints 0, 1, 2
18 #
19 # #Also supports indexing:
20 # print Animals[1] #this prints 1
21 #
22 #Can grab length like this:
23 # len(Animals) #this gives 3
24 #\endcode
25 #
26 # Note: Names should not override any Python internal class varibles (i.e. don't pass in names starting with '__'), or bad things will happen.
27 #
28 class Enum(object):
29  ## Constructor
30  # @param self
31  # @param names (list) list of attribute names to assign to this enum
32  # @param vals (list=None) list of values corresponding to the names. Passing None will cause the Enum to default to an integer value for each name (starting from zero and ascending).
33  def __init__(self, names, vals=None):
34  if vals == None:
35  vals = range(len(names))
36 
37  self.elements = OrderedDict( zip(names, vals) )
38 
39  ## This is a Python hook method for retreival by direct key name (eg. Animals.COW).
40  # @param self
41  # @param name (string) name of the attribute to retrieve from this instance
42  # @returns (anything) the requested attribute - or raises an AttributeError if an attribute with that name doesn't exist
43  def __getattr__(self, name):
44  if name in self.elements:
45  return self.elements[name]
46  raise AttributeError("Enum instance has no attribute '%s'" % (name))
47 
48  ## This is a Python hook method for iteration.
49  # @param self
50  # @returns (iterable object) an object that can be used to traverse the data structure
51  def __iter__(self):
52  return self.elements.__iter__()
53 
54  ## This is a Python hook method for for returning length.
55  # @param self
56  # @returns (int) the number of keys in this Enum.
57  def __len__(self):
58  return len(self.elements)
59 
60  ## This is a Python hook method for indexing.
61  # @param self
62  # @param index (int) the index of the element to look up - elements are indexed in the order the keys were in when they were passed to the constructor
63  # @returns (anything) the value at the requested index, or throws an exception if the index is out of range
64  def __getitem__(self, index):
65  return self.elements.values()[index]
66 
67  ## Returns a list of the values contained in this Enum, in the order in which they were specified when passed to the constructor.
68  # @param self
69  # @returns (list) list of values
70  def get_ordered_vals(self):
71  return self.elements.values()
72 
73  ## Returns a list of the keys contained in this Enum, in the order in which they were specified when passed to the constructor.
74  # @param self
75  # @returns (list) list of keys
76  def get_ordered_keys(self):
77  return self.elements.keys()
78 
79  ## Static method to create an Enum from a dictionary of key-value pairs. If the Enum is created with an unordered dictionary,
80  # the get_ordered_vals() and get_ordered_keys() methods will return values in an unspecified order.
81  # @param src_dict (Dictionary) a dictionary of key-value pairs to insert into the Enum
82  # @returns (Enum) a new Enum containing the dictionary data
83  @staticmethod
84  def from_dict(src_dict):
85  names = src_dict.keys()
86  vals = map(lambda key: src_dict[key], names)
87 
88  return Enum(names, vals)