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