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