Module Utils
[hide private]
[frames] | no frames]

Source Code for Module Utils

  1  '''
 
  2  Created on 01/08/2010
 
  3  
 
  4  @author: Luis
 
  5  ''' 
  6  
 
  7  from zlib import decompress 
  8  #import base64
 
  9  import csv 
 10  import os 
 11  import zlib 
 12  
 
13 -class utils():
14 ''' 15 This class groups a set of useful methods used in other classes to manipulate data. 16 ''' 17 18
19 - def __init__(self):
20 ''' 21 Constructor, nothing to setup 22 '''
23 24
25 - def fixFloatingPoint(self,number):
26 ''' 27 This function addresses the floating point representation in Python 28 It takes a float or double and turns it into its fixed representation. 29 i.e. 0.01 is represented as 1.0000000475 or 1.00000001 depending on the hardware 30 The function will return 0.01; the precision used is 1/100 000 31 ''' 32 num_str=str(number) 33 float_pos=num_str.find(".") 34 trunc_str="" 35 fixed_str="" 36 37 if num_str.find("99999",float_pos)!=-1:# finds the representation error for x.*999999* 38 trunc_until=num_str.find("99999",float_pos) 39 trunc_str=num_str[0:trunc_until] 40 fixed_str=trunc_str[0:-1] + str(eval(trunc_str[-1])+1)# add 1 to A, .*A99999 and truncates the number 41 #print fixed_str 42 elif num_str.find("00000",float_pos)!=-1:# finds the representation error for x.*00000* 43 trunc_until=num_str.find("00000",float_pos) # truncate the number until A like x.*A00000* 44 fixed_str=num_str[0:trunc_until] 45 #print fixed_str 46 else: 47 fixed_str=num_str 48 return fixed_str
49
50 - def getRelativePath(self,filename):
51 path_split=filename.split("/") 52 platform="L" 53 if len(path_split)==1: 54 platform="W" 55 path_split=path_split[0].split("\\") 56 57 directories=path_split[0:-1] 58 #print directories 59 relative_path="" 60 for dir in directories: 61 if platform=="L": 62 relative_path=relative_path + dir + "/" # Python will translate this to the native separator, so this selection is kind of useless. just in case. 63 else: 64 relative_path=relative_path+ dir + "\\" 65 return relative_path
66 67
68 - def getPythonFormat(self,mapped_type,mapped_endianness):
69 ''' 70 In the XML map schema the data types are named after C conventions i.e. 2 byte integers are 'int16' 71 This function returns the Python data type. 72 ''' 73 74 dataType="not supported" 75 #Python does not have a 'switch' statement so we need to compare the values using 'if' 76 if mapped_type=="uint8": 77 dataType="B" 78 typeBytes=1 79 if mapped_type=="int8": 80 dataType="b" 81 typeBytes=1 82 if mapped_type=="char8": 83 dataType="c" 84 typeBytes=1 85 if mapped_type=="uint16": 86 dataType="H" 87 typeBytes=2 88 if mapped_type=="int16": 89 dataType="h" 90 typeBytes=2 91 if mapped_type=="uint32": 92 dataType="I" 93 typeBytes=4 94 if mapped_type=="int32": 95 dataType="i" 96 typeBytes=4 97 if mapped_type=="uint64": 98 dataType="Q" 99 typeBytes=8 100 if mapped_type=="int64": 101 dataType="q" 102 typeBytes=8 103 if mapped_type=="float32": 104 dataType="f" 105 typeBytes=4 106 if mapped_type=="float64": 107 dataType="d" 108 typeBytes=8 109 110 if dataType=="not supported": 111 return dataType 112 else: 113 if mapped_endianness=="bigEndian": 114 python_endianess = ">" 115 elif mapped_endianness=="littleEndian": 116 python_endianess = "<" 117 118 #bytes=unpack_from(dataType,self.hdf_buffer,current_offset) 119 return dataType,typeBytes,python_endianess
120
121 - def inflate(self, compressed_bytes ):
122 ''' 123 Decompress streams of bytes using the zlib library 124 zip,inflate compression supported 125 ''' 126 try: 127 decompressed_bytes= decompress(compressed_bytes) 128 #SZIP not implemented yet 129 return decompressed_bytes 130 except: 131 print "Some error occurred while decompresing data"
132
133 - def getXMLattribute(self,node,attribute):
134 ''' 135 This function return the value of an attribute in a XML node; 136 also handles "key error" in case the node does not contain that attribute 137 ''' 138 try: 139 attribute_value= node.attrib[attribute] 140 return attribute_value 141 except: 142 #print "Attribute '" + attribute + "' not found in " + node.tag 143 return False
144
145 - def createCSVfromTable(self,table,file_name):
146 ''' 147 Creates a CSV file from a python table. 148 ''' 149 try: 150 file_name=os.path.normpath(file_name) 151 csvWriter =csv.writer(open(file_name+".csv", 'wb'), delimiter=',', quotechar=' ', quoting=csv.QUOTE_MINIMAL) 152 except: 153 print "can't open the file : " + file_name 154 return 155 #for rows in table: 156 # if len(rows)>0: 157 csvWriter.writerows(table)
158
159 - def createPlainDatFile(self,buffer,file_name):
160 ''' 161 Writes the content of a buffer in a .dat file 162 ''' 163 try: 164 file_name=os.path.normpath(file_name) 165 output =open(file_name,"wb") 166 output.write(buffer) 167 buffer=None 168 output.close() 169 except: 170 print "can't open the file : " + file_name 171 return
172 173 174
175 - def inflate64(self, b64string ):
176 #decoded_data = base64.b64decode( b64string ) 177 return zlib.decompress( b64string )
178