IMP  2.0.0
The Integrative Modeling Platform
Entry.py
1 #!/usr/bin/env python
2 
3 class Entry:
4  """The entry class represents a column in the statistics file.
5  Its components are a title, a format and an additional object.
6  It's what gets written to the statistics file in a column.
7  - title: the title of the column
8  - format: a wisely chosen format string (see below)
9  - something: either something that can be formattable, a string, a number
10  etc. This is a static entry. In that case all remaining arguments are
11  discarded and get_value returns the formatted string : format % something.
12  If something is a function, this is a dynamic entry, and the format
13  string is used on the result of the function call
14  something(*args,**kwargs).
15  """
16  def __init__(self, title, fmt, something, *args, **kwargs):
17  self.title = title
18  self.format = fmt
19  self.is_function = callable(something)
20  if self.is_function:
21  self.function = something
22  self.args = args
23  self.kwargs = kwargs
24  else:
25  self.value = something
26  self.was_updated_since_last_get = False
27 
28  def __repr__(self):
29  if self.is_function:
30  return "Entry('%s', '%s', f(...))" % (self.title, self.format)
31  else:
32  return "Entry('%s', '%s', %s)" % (self.title, self.format,
33  self.value)
34 
35 
36  def get_title(self):
37  return self.title
38 
39  def get_raw_value(self):
40  if self.is_function:
41  return self.function(*self.args, **self.kwargs)
42  else:
43  self.was_updated_since_last_get = False
44  return self.value
45 
46  def get_value(self):
47  try:
48  return self.format % self.get_raw_value()
49  except TypeError:
50  return "N/A"
51 
52  def set_value(self, val):
53  if self.is_function:
54  raise RuntimeError, \
55  "Can only set_value on static entries."
56  self.value = val
57  self.was_updated_since_last_get = True
58 
59  def get_was_updated(self):
60  return self.is_function or self.was_updated_since_last_get