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