ENH: Clean up temp file from test suite

This commit is contained in:
Eric Dill 2015-07-13 13:14:46 -05:00
parent a6d5e28642
commit 0e4fd90ca2
4 changed files with 76 additions and 12 deletions

View File

View File

@ -1,16 +1,22 @@
"""
SVG export test
"""
from __future__ import (division, print_function, absolute_import)
import pyqtgraph as pg
import pyqtgraph.exporters
import csv
import os
import shutil
from . import utils
app = pg.mkQApp()
def approxeq(a, b):
return (a-b) <= ((a + b) * 1e-6)
def test_CSVExporter():
tempfile = utils.gentempfilename(suffix='.csv')
plt = pg.plot()
y1 = [1,3,2,3,1,6,9,8,4,2]
plt.plot(y=y1, name='myPlot')
@ -24,9 +30,9 @@ def test_CSVExporter():
plt.plot(x=x3, y=y3, stepMode=True)
ex = pg.exporters.CSVExporter(plt.plotItem)
ex.export(fileName='test.csv')
ex.export(fileName=tempfile)
r = csv.reader(open('test.csv', 'r'))
r = csv.reader(open(tempfile, 'r'))
lines = [line for line in r]
header = lines.pop(0)
assert header == ['myPlot_x', 'myPlot_y', 'x0001', 'y0001', 'x0002', 'y0002']
@ -43,7 +49,8 @@ def test_CSVExporter():
assert (i >= len(x3) and vals[4] == '') or approxeq(float(vals[4]), x3[i])
assert (i >= len(y3) and vals[5] == '') or approxeq(float(vals[5]), y3[i])
i += 1
os.unlink(tempfile)
if __name__ == '__main__':
test_CSVExporter()

View File

@ -1,11 +1,19 @@
"""
SVG export test
"""
from __future__ import (division, print_function, absolute_import)
import pyqtgraph as pg
import pyqtgraph.exporters
import tempfile
from . import utils
import os
app = pg.mkQApp()
def test_plotscene():
tempfile = utils.gentempfilename(suffix='.svg')
pg.setConfigOption('foreground', (0,0,0))
w = pg.GraphicsWindow()
w.show()
@ -18,10 +26,12 @@ def test_plotscene():
app.processEvents()
ex = pg.exporters.SVGExporter(w.scene())
ex.export(fileName='test.svg')
ex.export(fileName=tempfile)
# clean up after the test is done
os.unlink(tempfile)
def test_simple():
tempfile = utils.gentempfilename(suffix='.svg')
scene = pg.QtGui.QGraphicsScene()
#rect = pg.QtGui.QGraphicsRectItem(0, 0, 100, 100)
#scene.addItem(rect)
@ -51,17 +61,17 @@ def test_simple():
#el = pg.QtGui.QGraphicsEllipseItem(0, 0, 100, 50)
#el.translate(10,-5)
#el.scale(0.5,2)
#el.setParentItem(rect2)
grp2 = pg.ItemGroup()
scene.addItem(grp2)
grp2.scale(100,100)
rect3 = pg.QtGui.QGraphicsRectItem(0,0,2,2)
rect3.setPen(pg.mkPen(width=1, cosmetic=False))
grp2.addItem(rect3)
ex = pg.exporters.SVGExporter(scene)
ex.export(fileName='test.svg')
ex = pg.exporters.SVGExporter(scene)
ex.export(fileName=tempfile)
os.unlink(tempfile)

View File

@ -0,0 +1,47 @@
import tempfile
import uuid
import os
def gentempfilename(dir=None, suffix=None):
"""Generate a temporary file with a random name
Defaults to whatever the system thinks is a temporary location
Parameters
----------
suffix : str, optional
The suffix of the file name (The thing after the last dot).
If 'suffix' does not begin with a dot then one will be prepended
Returns
-------
str
The filename of a unique file in the temporary directory
"""
if dir is None:
dir = tempfile.gettempdir()
if suffix is None:
suffix = ''
elif not suffix.startswith('.'):
suffix = '.' + suffix
print('tempfile.tempdir = %s' % tempfile.tempdir)
print('suffix = %s' % suffix)
return os.path.join(dir, str(uuid.uuid4()) + suffix)
def gentempdir(dir=None):
"""Generate a temporary directory
Parameters
----------
dir : str, optional
The directory to create a temporary directory im. If None, defaults
to the place on disk that the system thinks is a temporary location
Returns
-------
str
The path to the temporary directory
"""
return tempfile.mkdtemp(dir=dir)