Package Biskit :: Module Table
[hide private]
[frames] | no frames]

Source Code for Module Biskit.Table

  1  ## 
  2  ## Biskit, a toolkit for the manipulation of macromolecular structures 
  3  ## Copyright (C) 2004-2005 Raik Gruenberg & Johan Leckner 
  4  ## 
  5  ## This program is free software; you can redistribute it and/or 
  6  ## modify it under the terms of the GNU General Public License as 
  7  ## published by the Free Software Foundation; either version 2 of the 
  8  ## License, or any later version. 
  9  ## 
 10  ## This program is distributed in the hope that it will be useful, 
 11  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 13  ## General Public License for more details. 
 14  ## 
 15  ## You find a copy of the GNU General Public License in the file 
 16  ## license.txt along with this program; if not, write to the Free 
 17  ## Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 18  ## 
 19  ## 
 20   
 21  """ 
 22  @attention: This class is only used by Prosa.py which should be replaced 
 23  by L{Prosa2003}. It will only stay for a while until all transitions are made 
 24  to the new version of Prosa. 
 25  """ 
 26   
 27  from UserList import * 
 28  import string 
 29  from copy import copy 
 30   
31 -class Table(UserList):
32 - def __init__(self, lines = None, lists = None, format = None):
33 UserList.__init__(self) 34 if lines is not None: 35 for line in lines: 36 if not format: 37 words = string.split(line) 38 else: 39 words = [] 40 for item in format: 41 words.append(string.strip(line[item[0]:item[1]])) 42 _l = [] 43 for word in words: 44 try: 45 exec 'item = ' + word 46 except: 47 try: 48 exec 'item = "' + word + '"' 49 except: 50 item = '' 51 if type(item).__name__ in ['builtin_function_or_method',\ 52 'module']: 53 item = word 54 55 _l.append(item) 56 self.data.append(_l) 57 else: 58 self.data = lists
59
60 - def joinColumns(self, left, right, fromRow = -1, toRow = -1, separator = ''):
61 if fromRow == -1: fromRow = 0 62 if toRow == -1: toRow = len(self.data) 63 if left == -1: left = 0 64 print fromRow, toRow 65 for row in range(fromRow, toRow): 66 dummy = copy(right) 67 if dummy == -1: dummy = len(self.data[row]) 68 item = '' 69 for column in range(left, dummy): 70 newItem = self.data[row][column] 71 ## if not type(newItem).__name__ in ['string','int','float']: 72 ## raise 'cannot join types other than string, int, float' 73 item = item + separator + str(newItem) 74 del self.data[row][left:dummy] 75 self.data[row].insert(left,item)
76
77 - def __getslice__(self, i,j):
78 return Table(lists = self.data[i:j])
79
80 - def __getitem__(self, index):
81 if type(index).__name__ == 'int': 82 return self.data[index] 83 else: 84 if type(index[0]).__name__ == 'slice': 85 lStart = index[0].start 86 lStop = index[0].stop 87 if lStart is None: lStart = 0 88 if lStop is None: lStop = len(self.data) 89 else: 90 lStart = index[0] 91 lStop = lStart + 1 92 93 if type(index[1]).__name__ == 'slice': 94 rStart = index[1].start 95 rStop = index[1].stop 96 if rStart is None: rStart = 0 97 if rStop is None: rStop = len(self.data) 98 else: 99 rStart = index[1] 100 rStop = rStart + 1 101 102 columns = self.columns(rStart,rStop) 103 104 return table(lists = columns[lStart:lStop])
105
106 - def __repr__(self):
107 return 'table('+UserList.__repr__(self)+')'
108
109 - def rows(self, rows):
110 try: 111 rows[0] 112 except: 113 rows = (rows,) 114 115 result = [] 116 117 for row in rows: 118 result.append(self.data[row]) 119 120 if len(rows) == 1: 121 return asTable(result[0]) 122 else: 123 return asTable(result)
124
125 - def columns(self, columns):
126 try: 127 columns[0] 128 except: 129 columns = (columns,) 130 131 result = [] 132 133 if len(columns) == 1: 134 for row in self.data: 135 for column in columns: 136 result.append(row[column]) 137 else: 138 for row in self.data: 139 col = [] 140 for column in columns: 141 col.append(row[column]) 142 result.append(col) 143 144 return asTable(result)
145
146 - def deleteRows(self, rows):
147 for row in rows: 148 del self.data[row]
149
150 - def deleteColumns(self, columns):
151 for row in range(len( self.data)): 152 for column in columns: 153 del self.data[row][column]
154
155 - def writeToFile(self, name, separator = ' '):
156 f = open(name,'w') 157 map(lambda row, ff=f, ss=separator: ff.write(string.join(\ 158 map(lambda item: str(item), row), ss) + '\n'), self.data) 159 f.close()
160 161 162 from Scientific.IO import TextFile
163 -def fromFile(fileName, format = None):
164 165 ll = TextFile.TextFile(fileName).readlines() 166 return Table(lines = ll, format = format)
167
168 -def asTable(list):
169 return Table(lists = list)
170