Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
base_objects.py
Go to the documentation of this file.
1 ## @package data_structs.base_objects
2 
3 import types
4 
5 ## A custom object that provides some handy debugging routines to subclasses.
6 class BLLObject(object):
7  _FCN_TYPES = [types.FunctionType, types.MethodType]
8 
9  ## Checks if a particular attribute of this object is a function or method.
10  # @returns (boolean) True if the attribute with 'name' is a function/method, otherwise False
11  # @param self
12  # @param name (string) name of attribute to check
13  def _is_function(self, name):
14  found = False
15  obj = getattr(self, name)
16 
17  i = 0
18  while not found and i < len(BLLObject._FCN_TYPES):
19  found = isinstance(obj, BLLObject._FCN_TYPES[i])
20  i += 1
21 
22  return found
23 
24  ## Builds a nicely formatted string containing all attributes of this object (except those in omit_attr_names), for debugging purposes. Proceeds recursively (eg. if attribute is a object, calls __str__() on that object.
25  # @returns (string) as described above
26  # @param self
27  # @param omit_attr_names (list=[]) list of attribute names you don't want in the returned string (useful for preventing infinite recursion due to self-referencing pointers)
28  def __str__(self, omit_attr_names=[]):
29  #place the omitted names in a dictionary for quick lookup in the loop below
30  omissions = {}
31  for name in omit_attr_names:
32  omissions[name] = True
33 
34  output = self.__class__.__name__ + ':\n'
35  #go through all (at least, 'all' as defined by dir() ) attributes of this object
36  for name in dir(self):
37  #omit private (starts with underscore) attributes, methods, and names in the ommissions dictionary
38  if not name.startswith('_') and not name in omissions and not self._is_function(name):
39  val = getattr(self, name)
40  val_str = val
41 
42  #format lists nicely
43  if isinstance(val, list):
44  size = len(val)
45  val_str = '[\n' if size > 0 else '['
46  for i in range(size):
47  val_str += ' ' + str(val[i])
48  if i < size - 1:
49  val_str += ',\n'
50 
51  val_str += ' ]'
52 
53  output += ' -%s: %s\n' % (name, val_str)
54 
55  return output
56 
57 ## Represents an object that can be stored in the database.
59  ## Constructor
60  # @param self
61  def __init__(self):
62  self.db_id = None
63 
64  ## Performs operations needed to insert this object into the database. Subclasses must override this method.
65  # @param self
66  # @param db (BLLDatabase) database object to use for the insertion operation.
67  def db_insert(self, db):
68  pass
69 
70  ## Performs operations needed to delete this object from the database. Subclasses must override this method.
71  # @param self
72  # @param db (BLLDatabase) database object to use for the deletion operation.
73  def db_delete(self, db):
74  pass
75 
76  ## Selects objects of this type from the database. Subclasses must override this method.
77  # @param db (BLLDatabase) database object to use for the selection operation.
78  # @param ids (list=[]) list of ids (primary key integers) from the corresponding DB table for this object that are to be selected. If empty, all objects from the corresponding table are selected.
79  # @returns (list) list of objects of this type, constructed from a SELECT query on the corresponding DB table.
80  @staticmethod
81  def db_select(db, ids=[]):
82  pass
83 
84  ## Convenience method for subclasses. Constructs a SQL where condition clause from the specified ids.
85  # @param ids (list) list of integer primary keys
86  # @returns (string) clause like "WHERE id IN (0, 1, 2, 3, 4)" - assumes primary key column is named 'id'. Returns None if ids is empty.
87  @staticmethod
89  where_cond = None
90  if ids:
91  in_str = reduce(lambda accum, x: '%s,%d' % (str(accum), x), ids)
92  where_cond = 'id IN (%s)' % (in_str)
93 
94  return where_cond