Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
utils.enum.Enum Class Reference

A primitive enum class (Python has none). More...

Inheritance diagram for utils.enum.Enum:

Public Member Functions

def __init__
 Constructor. More...
 
def __getattr__
 This is a Python hook method for retreival by direct key name (eg. More...
 
def __iter__
 This is a Python hook method for iteration. More...
 
def __len__
 This is a Python hook method for for returning length. More...
 
def __getitem__
 This is a Python hook method for indexing. More...
 
def get_ordered_vals
 Returns a list of the values contained in this Enum, in the order in which they were specified when passed to the constructor. More...
 
def get_ordered_keys
 Returns a list of the keys contained in this Enum, in the order in which they were specified when passed to the constructor. More...
 

Static Public Member Functions

def from_dict
 Static method to create an Enum from a dictionary of key-value pairs. More...
 

Public Attributes

 elements
 

Detailed Description

A primitive enum class (Python has none).

Sample usage:

1  Animals = Enum(['COW', 'GOAT', 'ELEPHANT'])
2  Animals.COW #this gives 0
3  Animals.GOAT #this gives 1
4  Animals.ELEPHANT #this gives 2
5  Animals.TURKEY #this gives AttributeError
6 
7  #Supports ordered iteration:
8  for creature in Animals:
9  print creature
10  #This prints 0, 1, 2
11 
12  #Also supports indexing:
13  print Animals[1] #this prints 1
14 
15 Can grab length like this:
16  len(Animals) #this gives 3

Note: Names should not override any Python internal class varibles (i.e. don't pass in names starting with '__'), or bad things will happen.

Definition at line 28 of file enum.py.

Constructor & Destructor Documentation

def utils.enum.Enum.__init__ (   self,
  names,
  vals = None 
)

Constructor.

Parameters
self
names(list) list of attribute names to assign to this enum
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).

Definition at line 33 of file enum.py.

Member Function Documentation

def utils.enum.Enum.__getattr__ (   self,
  name 
)

This is a Python hook method for retreival by direct key name (eg.

Animals.COW).

Parameters
self
name(string) name of the attribute to retrieve from this instance
Returns
(anything) the requested attribute - or raises an AttributeError if an attribute with that name doesn't exist

Definition at line 43 of file enum.py.

def utils.enum.Enum.__getitem__ (   self,
  index 
)

This is a Python hook method for indexing.

Parameters
self
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
Returns
(anything) the value at the requested index, or throws an exception if the index is out of range

Definition at line 64 of file enum.py.

def utils.enum.Enum.__iter__ (   self)

This is a Python hook method for iteration.

Parameters
self
Returns
(iterable object) an object that can be used to traverse the data structure

Definition at line 51 of file enum.py.

def utils.enum.Enum.__len__ (   self)

This is a Python hook method for for returning length.

Parameters
self
Returns
(int) the number of keys in this Enum.

Definition at line 57 of file enum.py.

def utils.enum.Enum.from_dict (   src_dict)
static

Static method to create an Enum from a dictionary of key-value pairs.

If the Enum is created with an unordered dictionary, the get_ordered_vals() and get_ordered_keys() methods will return values in an unspecified order.

Parameters
src_dict(Dictionary) a dictionary of key-value pairs to insert into the Enum
Returns
(Enum) a new Enum containing the dictionary data

Definition at line 84 of file enum.py.

def utils.enum.Enum.get_ordered_keys (   self)

Returns a list of the keys contained in this Enum, in the order in which they were specified when passed to the constructor.

Parameters
self
Returns
(list) list of keys

Definition at line 76 of file enum.py.

def utils.enum.Enum.get_ordered_vals (   self)

Returns a list of the values contained in this Enum, in the order in which they were specified when passed to the constructor.

Parameters
self
Returns
(list) list of values

Definition at line 70 of file enum.py.

Member Data Documentation

utils.enum.Enum.elements

Definition at line 37 of file enum.py.


The documentation for this class was generated from the following file: