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