Baby Language Lab Scripts
A collection of data processing tools.
 All Classes Namespaces Files Functions Variables Pages
form.py
Go to the documentation of this file.
1 ## @package utils.form
2 
3 ## This class provides a way to store UI input elements by name so that they can be retrieved later on.
4 # Basically, this is a dictionary, but elements can be added or accessed using the dot notation direclty.
5 # This is simply for convenience - so we don't have to pass a bunch of UI inputs around between methods
6 # in UI code for doing things like validation/retrieving user input.
7 # For example:
8 # \code
9 # def create_form():
10 # form = Form()
11 # form.my_entrybox = gtk.EntryBox(...)
12 # form.my_spinner = gtk.SpinButton(...)
13 # return form
14 #
15 # def process_form(form):
16 # text_input = form.my_entrybox.get_text()
17 # int_input = form.my_spinner.get_int_value()
18 # ...
19 # \endcode
20 class Form(object):
21  ## Constructor
22  # @param self
23  def __init__(self):
24  self._items = {}
25 
26  ## Called by Python when an instance's data/function member is set to something.
27  # @param self
28  # @param name (string) name of the member that is being set on this instance.
29  # @param val (anything) value to set the member to.
30  def __setattr__(self, name, val):
31  #this case is needed to prevent infinite recursion when the constructor assigns to _items
32  if name == '_items':
33  #Defer to the "object" class in a special way to perform the actual set.
34  super(Form, self).__setattr__(name, val)
35  else:
36  self._items[name] = val
37 
38  ## Called by Python when an instance's data/function member is read.
39  # @param self
40  # @param name (string) name of the member being read.
41  # @returns (anything) value of the member being requested.
42  def __getattr__(self, name):
43  #This case is needed to prevent infinite recursion
44  if name == '_items':
45  return self._items
46  #this case occurs when the user has requested something they've previously stored
47  elif name in self._items:
48  return self._items[name]
49  #cause a fuss if nothing is found with the requested name
50  else:
51  raise AttributeError('Form instance has no attribute %s.' % (name))
52  ## Returns a list of all the controls that have been stored in this Form instance (in no particular order)
53  # @param self
54  # @returns (list) list of whatever's been stored in this instance
55  def get_item_list(self):
56  return self._items.values()