1 '''
2 Created on 01/08/2010
3
4 @author: Luis
5 '''
6
7 from zlib import decompress
8
9 import csv
10 import os
11 import zlib
12
14 '''
15 This class groups a set of useful methods used in other classes to manipulate data.
16 '''
17
18
20 '''
21 Constructor, nothing to setup
22 '''
23
24
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:
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)
41
42 elif num_str.find("00000",float_pos)!=-1:
43 trunc_until=num_str.find("00000",float_pos)
44 fixed_str=num_str[0:trunc_until]
45
46 else:
47 fixed_str=num_str
48 return fixed_str
49
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
59 relative_path=""
60 for dir in directories:
61 if platform=="L":
62 relative_path=relative_path + dir + "/"
63 else:
64 relative_path=relative_path+ dir + "\\"
65 return relative_path
66
67
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
129 return decompressed_bytes
130 except:
131 print "Some error occurred while decompresing data"
132
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
143 return False
144
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
156
157 csvWriter.writerows(table)
158
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
176
177 return zlib.decompress( b64string )
178