MNT: Use tempfile

This commit is contained in:
Eric Dill 2015-07-17 13:31:14 -04:00
parent 0e4fd90ca2
commit e6c1c54a6b
3 changed files with 18 additions and 64 deletions

View File

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

View File

@ -1,11 +1,9 @@
""" """
SVG export test SVG export test
""" """
from __future__ import (division, print_function, absolute_import) from __future__ import division, print_function, absolute_import
import pyqtgraph as pg import pyqtgraph as pg
import pyqtgraph.exporters
import tempfile import tempfile
from . import utils
import os import os
@ -13,7 +11,8 @@ app = pg.mkQApp()
def test_plotscene(): def test_plotscene():
tempfile = utils.gentempfilename(suffix='.svg') tempfilename = tempfile.NamedTemporaryFile(suffix='.svg').name
print("using %s as a temporary file" % tempfilename)
pg.setConfigOption('foreground', (0,0,0)) pg.setConfigOption('foreground', (0,0,0))
w = pg.GraphicsWindow() w = pg.GraphicsWindow()
w.show() w.show()
@ -26,12 +25,13 @@ def test_plotscene():
app.processEvents() app.processEvents()
ex = pg.exporters.SVGExporter(w.scene()) ex = pg.exporters.SVGExporter(w.scene())
ex.export(fileName=tempfile) ex.export(fileName=tempfilename)
# clean up after the test is done # clean up after the test is done
os.unlink(tempfile) os.unlink(tempfilename)
def test_simple(): def test_simple():
tempfile = utils.gentempfilename(suffix='.svg') tempfilename = tempfile.NamedTemporaryFile(suffix='.svg').name
print("using %s as a temporary file" % tempfilename)
scene = pg.QtGui.QGraphicsScene() scene = pg.QtGui.QGraphicsScene()
#rect = pg.QtGui.QGraphicsRectItem(0, 0, 100, 100) #rect = pg.QtGui.QGraphicsRectItem(0, 0, 100, 100)
#scene.addItem(rect) #scene.addItem(rect)
@ -73,5 +73,5 @@ def test_simple():
grp2.addItem(rect3) grp2.addItem(rect3)
ex = pg.exporters.SVGExporter(scene) ex = pg.exporters.SVGExporter(scene)
ex.export(fileName=tempfile) ex.export(fileName=tempfilename)
os.unlink(tempfile) os.unlink(tempfilename)

View File

@ -1,47 +0,0 @@
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)