1  """ 
  2  Just a few utilities that can be of more general use. 
  3  """ 
  4  import re 
  5   
  6  __author__    = "$Author: jurgenfd $" 
  7  ___revision__ = "$Revision: 10 $" 
  8  ___date__     = "$Date: 2007-01-23 19:08:11 +0100 (Tue, 23 Jan 2007) $" 
  9   
 10  """ 
 11  $Log$ 
 12  Revision 1.2  2007/01/23 18:08:11  jurgenfd 
 13  The below quoted value wasn't parsed correctly. 
 14  Occurs in the MR file for entry 2ihx. 
 15   
 16  '#It has very upfield-shifted H5', H5" @ 3.935,4.012 ppm' 
 17   
 18  Fixed with this update. 
 19   
 20  Revision 1.1.1.1  2007/01/09 22:10:15  jurgenfd 
 21  initial import 
 22   
 23  Revision 1.1  2007/01/08 20:49:41  jurgen 
 24  Merged improvements from Wim Vranken (EBI) back in. 
 25   
 26  Revision 1.1.1.1  2001/11/02 20:16:40  jurgen 
 27  Initial package capable of read/write access to STAR files without nested loops 
 28   
 29  """ 
 30   
 32      """Example from 'Learning Python from O'Reilly publisher'""" 
 34          return ("<Instance of %s, address %s:\n%s>" % 
 35             (self.__class__.__name__, id(self), self.attrnames())) 
  36   
 38          result='' 
 39          keys = sorted(self.__dict__.keys()) 
 40          for attr in keys: 
 41              if attr[:2] == "__": 
 42                  result = result + "\tname %s=<built-in>\n" % attr 
 43              else: 
 44                  result = result + "\tname %s=%s\n" % (attr, self.__dict__[attr]) 
 45          return result         
   46   
 47   
 48  """ 
 49  A fast transposing algorithm from the python mailing list 
 50  Used in TagTable. 
 51   
 52  This algorithm fails in Python 3, so has been replaced! 
 53  """ 
 55      if len( matrix ) < 1: 
 56          print('ERROR: trying to transpose an empty matrix') 
 57          return 1 
 58      elif len( matrix ) == 1: 
 59          if len(matrix[0]) == 0: 
 60              print('ERROR: trying to transpose an empty matrix, shape would be lost') 
 61              print('ERROR: [[]] would become []') 
 62              return 1 
 63          else: 
 64              return [(y,) for y in matrix[0]] 
 65      else: 
 66           
 67          result = [] 
 68   
 69           
 70          max_len = 0 
 71          for i in range(len(matrix)): 
 72              max_len = max(max_len, len(matrix[i])) 
 73   
 74           
 75          for j in range(max_len): 
 76               
 77              row = [] 
 78   
 79               
 80              for i in range(len(matrix)): 
 81                   
 82                  if j > len(matrix[i]) - 1: 
 83                      row.append(None) 
 84                  else: 
 85                      row.append(matrix[i][j]) 
 86   
 87               
 88              result.append(tuple(row)) 
 89   
 90          return result 
  91   
 92   
 93  """ 
 94  Collapses all whitespace to a single regular space 
 95  before comparing. Doesn't remove final eol space. 
 96  """ 
 98      pattern   = re.compile("\s+" ) 
 99      a = re.sub(pattern, ' ', a) 
100      b = re.sub(pattern, ' ', b) 
101   
102   
103      return a == b 
 104   
106      return re.sub('\r\n', '\n', text) 
 108      return re.sub('([^\r])(\n)', '\1\r\n', text) 
 110      return re.sub('\r', '\n', text) 
 111