Public Access
cleanup of write_to_csv in MeasurementResult (fixes #30) Leaking file handle when exception occurs during write
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import threading
|
import threading
|
||||||
import math
|
import math
|
||||||
import types
|
|
||||||
import sys
|
import sys
|
||||||
import tables
|
import tables
|
||||||
import numpy
|
import numpy
|
||||||
@@ -45,7 +44,7 @@ class AccumulatedValue:
|
|||||||
|
|
||||||
def __add__(self,y):
|
def __add__(self,y):
|
||||||
new_one=AccumulatedValue()
|
new_one=AccumulatedValue()
|
||||||
if (type(y) is types.InstanceType and isinstance(y, AccumulatedValue)):
|
if isinstance(y, AccumulatedValue):
|
||||||
new_one.y=self.y+y.y
|
new_one.y=self.y+y.y
|
||||||
new_one.y2=self.y2+y.y2
|
new_one.y2=self.y2+y.y2
|
||||||
new_one.n=self.n+y.n
|
new_one.n=self.n+y.n
|
||||||
@@ -56,7 +55,7 @@ class AccumulatedValue:
|
|||||||
return new_one
|
return new_one
|
||||||
|
|
||||||
def __iadd__(self,y):
|
def __iadd__(self,y):
|
||||||
if (type(y) is types.InstanceType and isinstance(y, AccumulatedValue)):
|
if isinstance(y, AccumulatedValue):
|
||||||
self.y+=y.y
|
self.y+=y.y
|
||||||
self.y2+=y.y2
|
self.y2+=y.y2
|
||||||
self.n+=y.n
|
self.n+=y.n
|
||||||
@@ -203,28 +202,27 @@ class MeasurementResult(Drawable.Drawable, collections.UserDict):
|
|||||||
destination can be a file or a filename
|
destination can be a file or a filename
|
||||||
suitable for further processing
|
suitable for further processing
|
||||||
"""
|
"""
|
||||||
the_destination=destination
|
if isinstance(destination, str):
|
||||||
file_opened = False
|
with open(destination, "w") as f:
|
||||||
if type(destination) in (str,):
|
self._write_to_csv_dest(f, delimiter)
|
||||||
the_destination=open(destination, "w")
|
else:
|
||||||
file_opened = True
|
self._write_to_csv_dest(destination, delimiter)
|
||||||
|
|
||||||
the_destination.write("# quantity:"+str(self.quantity_name)+"\n")
|
def _write_to_csv_dest(self, dest, delimiter):
|
||||||
the_destination.write("# x y ysigma n\n")
|
dest.write("# quantity:"+str(self.quantity_name)+"\n")
|
||||||
|
dest.write("# x y ysigma n\n")
|
||||||
for x in self.get_xdata():
|
for x in self.get_xdata():
|
||||||
y=self.data[x]
|
y = self.data[x]
|
||||||
if type(y) in [float, int, int]:
|
if isinstance(y, (float, int)):
|
||||||
the_destination.write("%e%s%e%s0%s1\n"%(x, delimiter, y, delimiter, delimiter))
|
dest.write("%e%s%e%s0%s1\n" % (x, delimiter, y, delimiter, delimiter))
|
||||||
else:
|
else:
|
||||||
the_destination.write("%e%s%e%s%e%s%d\n"%(x,
|
dest.write("%e%s%e%s%e%s%d\n" % (x,
|
||||||
delimiter,
|
delimiter,
|
||||||
y.mean(),
|
y.mean(),
|
||||||
delimiter,
|
delimiter,
|
||||||
y.mean_error(),
|
y.mean_error(),
|
||||||
delimiter,
|
delimiter,
|
||||||
y.n))
|
y.n))
|
||||||
if file_opened:
|
|
||||||
the_destination.close()
|
|
||||||
|
|
||||||
|
|
||||||
def write_to_hdf(self, hdffile, where, name, title, complib=None, complevel=None):
|
def write_to_hdf(self, hdffile, where, name, title, complib=None, complevel=None):
|
||||||
|
|||||||
Reference in New Issue
Block a user