Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
error_collector.py
Go to the documentation of this file.
1 ## @package data_structs.error_collector
2 
3 ## This class maintains a collection of errors and warnings, providing various lookup methods to retreive them.
4 class ErrorCollector(object):
5  ## Constructor
6  # @param self
7  def __init__(self):
8  self.collection = {}
9 
10  ## Adds a warning/error to this collector.
11  # @param self
12  # @param error (instance of any subclass of BLLAppError)
13  def add(self, error):
14  #The internal dictionary is keyed by error object.
15  #Each key's value is a sub-dictionary.
16  #Each sub-dictionary is keyed by the error's class name.
17  #Each key's value is an array of the error instances themselves.
18  #This structure provides a reasonably efficient layout for performing all of the lookups we need.
19 
20  #If there is nothing stored for this object yet, add the object as a key.
21  if not error.obj in self.collection:
22  self.collection[error.obj] = {}
23 
24  #if the sub-dictionary has no entry for this error's class, add one.
25  if not error.__class__ in self.collection[error.obj]:
26  self.collection[error.obj][error.__class__] = []
27 
28  #drop the error object into the appropraite location
29  self.collection[error.obj][error.__class__].append(error)
30 
31  ## Retreives a list of all of the objects assocated with errors that have been added to this collector.
32  # Currently, these objects are always Utterances.
33  # @param self
34  # @returns (list) list of objects - currently Utterances - obtained from the 'obj' attribute of the errors added to this collector.
35  def get_all_utters(self):
36  return self.collection.keys()
37 
38  ## Retreives a list of all errors associated with a specific utterance
39  # @param self
40  # @param utter (object) an object - currently always an Utterance - to search for within the errors that have been added to this collector.
41  # @returns (list) a list of all errors whose 'obj' attribute is equal to the specified utterance
42  def get_errors_by_utter(self, utter):
43  error_list = []
44  if utter in self.collection:
45  for cls in self.collection[utter]:
46  error_list.extend(self.collection[utter][cls])
47 
48  return error_list
49 
50  ## This method is for debugging - it prints out all of the Utterances currently in this collector, grouped by the error/warning class in which they were inserted.
51  # @param self
52  # @param utter (Utterance=None) if 'utter' is present in this collector the method will print it. Otherwise if will print nothing. If this is set to 'None', all utterances in the collector will be printed.
53  # @param error_cls (class=None) specify a subclass of BLLAppError to force the method to print out only utterances associated with errors of this type. Use 'None' to print utterances from all error classes.
54  def print_errors(self, utter=None, error_cls=None):
55  utter_list = []
56  if utter and utter in self.collection.keys():
57  utter_list.append(utter)
58  elif not utter:
59  utter_list = self.collection.keys()
60 
61  for u in utter_list:
62  cls_list = []
63  if error_cls and error_cls in self.collection[u].keys():
64  cls_list.append(error_cls)
65  elif not error_cls:
66  cls_list = self.collection[u].keys()
67 
68  for cls in cls_list:
69  if self.collection[u][cls]:
70  print u
71  for err in self.collection[u][cls]:
72  print err
73