From 0e4fd90ca2f2ae6076a04f507524fc98daf52d78 Mon Sep 17 00:00:00 2001 From: Eric Dill Date: Mon, 13 Jul 2015 13:14:46 -0500 Subject: [PATCH 1/2] ENH: Clean up temp file from test suite --- pyqtgraph/exporters/tests/__init__.py | 0 pyqtgraph/exporters/tests/test_csv.py | 15 ++++++--- pyqtgraph/exporters/tests/test_svg.py | 26 ++++++++++----- pyqtgraph/exporters/tests/utils.py | 47 +++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 pyqtgraph/exporters/tests/__init__.py create mode 100644 pyqtgraph/exporters/tests/utils.py diff --git a/pyqtgraph/exporters/tests/__init__.py b/pyqtgraph/exporters/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pyqtgraph/exporters/tests/test_csv.py b/pyqtgraph/exporters/tests/test_csv.py index a98372ec..a54c7aca 100644 --- a/pyqtgraph/exporters/tests/test_csv.py +++ b/pyqtgraph/exporters/tests/test_csv.py @@ -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() - \ No newline at end of file diff --git a/pyqtgraph/exporters/tests/test_svg.py b/pyqtgraph/exporters/tests/test_svg.py index 871f43c2..dfa6059f 100644 --- a/pyqtgraph/exporters/tests/test_svg.py +++ b/pyqtgraph/exporters/tests/test_svg.py @@ -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) diff --git a/pyqtgraph/exporters/tests/utils.py b/pyqtgraph/exporters/tests/utils.py new file mode 100644 index 00000000..f2498a3b --- /dev/null +++ b/pyqtgraph/exporters/tests/utils.py @@ -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) From e6c1c54a6b3532e032bff104f73120c1cd6636a1 Mon Sep 17 00:00:00 2001 From: Eric Dill Date: Fri, 17 Jul 2015 13:31:14 -0400 Subject: [PATCH 2/2] MNT: Use tempfile --- pyqtgraph/exporters/tests/test_csv.py | 17 +++++----- pyqtgraph/exporters/tests/test_svg.py | 18 +++++----- pyqtgraph/exporters/tests/utils.py | 47 --------------------------- 3 files changed, 18 insertions(+), 64 deletions(-) delete mode 100644 pyqtgraph/exporters/tests/utils.py diff --git a/pyqtgraph/exporters/tests/test_csv.py b/pyqtgraph/exporters/tests/test_csv.py index a54c7aca..15c6626e 100644 --- a/pyqtgraph/exporters/tests/test_csv.py +++ b/pyqtgraph/exporters/tests/test_csv.py @@ -1,22 +1,23 @@ """ 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.exporters import csv import os -import shutil -from . import utils +import tempfile app = pg.mkQApp() + def approxeq(a, b): return (a-b) <= ((a + b) * 1e-6) 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() y1 = [1,3,2,3,1,6,9,8,4,2] plt.plot(y=y1, name='myPlot') @@ -30,9 +31,9 @@ def test_CSVExporter(): plt.plot(x=x3, y=y3, stepMode=True) 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] header = lines.pop(0) 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]) i += 1 - os.unlink(tempfile) + os.unlink(tempfilename) if __name__ == '__main__': test_CSVExporter() diff --git a/pyqtgraph/exporters/tests/test_svg.py b/pyqtgraph/exporters/tests/test_svg.py index dfa6059f..2261f7df 100644 --- a/pyqtgraph/exporters/tests/test_svg.py +++ b/pyqtgraph/exporters/tests/test_svg.py @@ -1,11 +1,9 @@ """ 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.exporters import tempfile -from . import utils import os @@ -13,7 +11,8 @@ app = pg.mkQApp() 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)) w = pg.GraphicsWindow() w.show() @@ -26,12 +25,13 @@ def test_plotscene(): app.processEvents() ex = pg.exporters.SVGExporter(w.scene()) - ex.export(fileName=tempfile) + ex.export(fileName=tempfilename) # clean up after the test is done - os.unlink(tempfile) + os.unlink(tempfilename) 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() #rect = pg.QtGui.QGraphicsRectItem(0, 0, 100, 100) #scene.addItem(rect) @@ -73,5 +73,5 @@ def test_simple(): grp2.addItem(rect3) ex = pg.exporters.SVGExporter(scene) - ex.export(fileName=tempfile) - os.unlink(tempfile) + ex.export(fileName=tempfilename) + os.unlink(tempfilename) diff --git a/pyqtgraph/exporters/tests/utils.py b/pyqtgraph/exporters/tests/utils.py deleted file mode 100644 index f2498a3b..00000000 --- a/pyqtgraph/exporters/tests/utils.py +++ /dev/null @@ -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)