Package diffpy :: Package srrietveld :: Module utility
[frames] | no frames]

Source Code for Module diffpy.srrietveld.utility

  1  ############################################################################## 
  2  # 
  3  # diffpy.srrietveld by DANSE Diffraction group 
  4  #                   Simon J. L. Billinge 
  5  #                   (c) 2009 Trustees of the Columbia University 
  6  #                   in the City of New York.  All rights reserved. 
  7  # 
  8  # File coded by:    Peng Tian 
  9  # 
 10  # See AUTHORS.txt for a list of people who contributed. 
 11  # See LICENSE.txt for license information. 
 12  # 
 13  ################################################################################ 
 14   
 15  """Common shared functions that do not fit anywhere else. 
 16  """ 
 17   
 18  import os 
 19  import sys 
 20  import traceback 
 21  from hashlib import md5 
 22   
 23   
24 -def checkFormat(filename):
25 """ Check the format of input file """ 26 filebasename = os.path.basename(filename) 27 fileformat = os.path.splitext(filebasename)[-1][1:].lower() 28 return fileformat
29 30
31 -def importClass(modpath, modname, classname, *args):
32 """ Import certain class with certain inputs """ 33 mod = __import__(modpath, globals(), locals(), [modname], -1) 34 cls = getattr(mod, classname) 35 return cls(*args)
36
37 -def parseXYToList(filename):
38 """ Parse XY or XYSigma data to list """ 39 # 1. read file 40 ifile = open(filename, "r") 41 inlines = ifile.readlines() 42 ifile.close() 43 44 # 2. parse content 45 datalist = [] 46 for line in inlines: 47 terms = line.strip().split() 48 if len(terms) > 1 and terms[0] not in ["#", ""]: 49 datalist.append((float(terms[0]), float(terms[1]))) 50 return datalist
51 52 53 # PJ FIXME: use the Python logging module instead 54
55 -def printWarning(warningMsg):
56 '''Print a red warning message to the std output 57 warningMsg : the warning message to be printed 58 ''' 59 #print '\\033[1;31m' + 'WARNING: ' + warningMsg + '\\033[1;m' 60 print 'WARNING: ' + warningMsg 61 return
62 63
64 -def unique(seq):
65 '''Get unique items from a sequence while keeping its original order''' 66 seen = set() 67 for item in seq: 68 if item in seen: continue 69 seen.add(item) 70 yield item 71 pass
72 73
74 -def getFileid(fullpath):
75 '''get the md5 id of the file path 76 fullpath - the abs path of the file 77 return - the file path''' 78 fid = None 79 fid = md5(open(fullpath, 'rb').read()).hexdigest() 80 return fid
81 82
83 -def printDebugInfo():
84 '''Print the information for debug''' 85 exc_type, exc_value, exc_traceback = sys.exc_info() 86 traceback.print_exception(exc_type, exc_value, exc_traceback, 87 limit=100, file=sys.stdout) 88 return
89 90 ############################################################################## 91 # csv reader and writer for unicode data, an extra encoding parameter is added 92 ############################################################################## 93 94 import csv, codecs, cStringIO 95
96 -class UTF8Recoder:
97 """ 98 Iterator that reads an encoded stream and reencodes the input to UTF-8 99 """
100 - def __init__(self, f, encoding):
101 self.reader = codecs.getreader(encoding)(f)
102
103 - def __iter__(self):
104 return self
105
106 - def next(self):
107 return self.reader.next().encode("utf-8")
108
109 -class UnicodeReader:
110 """ 111 A CSV reader which will iterate over lines in the CSV file "f", 112 which is encoded in the given encoding. 113 """ 114
115 - def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
116 f = UTF8Recoder(f, encoding) 117 self.reader = csv.reader(f, dialect=dialect, **kwds)
118
119 - def next(self):
120 row = self.reader.next() 121 return [unicode(s, "utf-8") for s in row]
122
123 - def __iter__(self):
124 return self
125
126 -class UnicodeWriter:
127 """ 128 A CSV writer which will write rows to CSV file "f", 129 which is encoded in the given encoding. 130 """ 131
132 - def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
133 # Redirect output to a queue 134 self.queue = cStringIO.StringIO() 135 self.writer = csv.writer(self.queue, dialect=dialect, **kwds) 136 self.stream = f 137 self.encoder = codecs.getincrementalencoder(encoding)()
138
139 - def writerow(self, row):
140 self.writer.writerow([s.encode("utf-8") for s in row]) 141 # Fetch UTF-8 output from the queue ... 142 data = self.queue.getvalue() 143 data = data.decode("utf-8") 144 # ... and reencode it into the target encoding 145 data = self.encoder.encode(data) 146 # write to the target stream 147 self.stream.write(data) 148 # empty queue 149 self.queue.truncate(0)
150
151 - def writerows(self, rows):
152 for row in rows: 153 self.writerow(row)
154