Source code for text

"""Module with utility classes and functions related
to data tables and text.

import PsiMod
import sys
import physconst
from psiexceptions import *

[docs]class Table(object): """Class defining a flexible Table object for storing data.""" def __init__(self, rows=(), row_label_width=10, row_label_precision=4, cols=(), width=16, precision=10): self.row_label_width = row_label_width self.row_label_precision = row_label_precision self.width = width self.precision = precision self.rows = rows if isinstance(cols, str): self.cols = (cols,) else: self.cols = cols self.labels = [] = []
[docs] def format_label(self): """Function to pad the width of Table object labels.""" #str = lambda x: (('%%%d.%df' % (self.row_label_width, self.row_label_precision)) % x) str = lambda x: (('%%%ds' % (self.row_label_width)) % x) return " ".join(map(str, self.labels))
[docs] def format_values(self, values): """Function to pad the width of Table object data cells.""" str = lambda x: (('%%%d.%df' % (self.width, self.precision)) % x) return " ".join(map(str, values))
def __getitem__(self, value): self.labels.append(value) return self def __setitem__(self, name, value): self.labels.append(name) label = self.format_label() self.labels = [] if isinstance(value, list):, value)) else:, [value]))
[docs] def save(self, file): """Function to save string of the Table object to *file*.""" import pickle pickle_str = pickle.dumps(self) fileobj = open(file, "w") fileobj.write(str(self)) fileobj.close()
def __str__(self): rowstr = lambda x: '%%%ds' % self.row_label_width % x colstr = lambda x: '%%%ds' % self.width % x lines = [] table_header = "" if isinstance(self.rows, str): table_header += "%%%ds" % self.row_label_width % self.rows else: table_header += " ".join(map(rowstr, self.rows)) table_header += " ".join(map(colstr, self.cols)) lines.append(table_header) for datarow in #print datarow row_data = datarow[0] row_data += self.format_values(datarow[1]) lines.append(row_data) return "\n".join(lines) + "\n"
[docs] def copy(self): """Function to return a copy of the Table object.""" import copy return copy.deepcopy(self)
[docs] def absolute_to_relative(self, Factor=physconst.psi_hartree2kcalmol): """Function to shift the data of each column of the Table object such that the lowest value is zero. A scaling factor of *Factor* is applied. """ import copy if len( == 0: return current_min = list(copy.deepcopy([0][1])) for datarow in for col in range(0, len(datarow[1])): if current_min[col] > datarow[1][col]: current_min[col] = datarow[1][col] for datarow in for col in range(0, len(datarow[1])): #print datarow[1][col] datarow[1][col] = (datarow[1][col] - current_min[col]) * Factor
[docs] def scale(self, Factor=physconst.psi_hartree2kcalmol): """Function to apply a scaling factor *Factor* to the data of the Table object. """ if len( == 0: return for datarow in for col in range(0, len(datarow[1])): #print datarow[1][col] datarow[1][col] = datarow[1][col] * Factor