Skip to content

Commit

Permalink
File handling: use context managers for safer resource management
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreRaybaut committed Nov 7, 2024
1 parent 3e509ae commit f923828
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
3 changes: 2 additions & 1 deletion plotpy/builder/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ def svg(
assert shape in ("circle", "rectangle", "square")
assert isinstance(fname_or_data, (str, bytes))
if isinstance(fname_or_data, str):
data = open(fname_or_data, "rb").read()
with open(fname_or_data, "rb") as file:
data = file.read()
else:
data = fname_or_data
shapeklass = {
Expand Down
8 changes: 4 additions & 4 deletions plotpy/tests/features/test_loadsaveitems_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ class PickleTest(IOTest):

def restore_items(self) -> None:
"""Restore items"""
f = open(self.FNAME, "rb")
self.plot.restore_items(f)
with open(self.FNAME, "rb") as f:
self.plot.restore_items(f)

def save_items(self) -> None:
"""Save items"""
self.plot.select_all()
f = open(self.FNAME, "wb")
self.plot.save_items(f, selected=True)
with open(self.FNAME, "wb") as f:
self.plot.save_items(f, selected=True)


def test_pickle() -> None:
Expand Down
3 changes: 2 additions & 1 deletion plotpy/tests/tools/test_get_rectangle_with_svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class SVGToolExample(RectangularShapeTool):

def create_shape(self):
"""Create shape to be drawn"""
svg_data = open(self.SVG_FNAME, "rb").read()
with open(self.SVG_FNAME, "rb") as svg_file:
svg_data = svg_file.read()
shape = make.svg("rectangle", svg_data, 0, 0, 1, 1, "SVG")
self.set_shape_style(shape)
return shape, 0, 2
Expand Down
8 changes: 4 additions & 4 deletions plotpy/tools/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ def activate_command(self, plot: BasePlot, checked: bool) -> None:
)
if not fname:
return
itemfile = open(fname, "wb")
plot.save_items(itemfile, selected=True)
with open(fname, "wb") as itemfile:
plot.save_items(itemfile, selected=True)


class LoadItemsTool(OpenFileTool):
Expand Down Expand Up @@ -343,8 +343,8 @@ def activate_command(self, plot: BasePlot, checked: bool) -> None:
filename = self.get_filename(plot)
if not filename:
return
itemfile = open(filename, "rb")
plot.restore_items(itemfile)
with open(filename, "rb") as itemfile:
plot.restore_items(itemfile)
plot.replot()


Expand Down
13 changes: 6 additions & 7 deletions plotpy/widgets/qtdesigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def loadui(fname, replace_class="QwtPlot"):
QtDesigner plugins because they don't inheritate from a PyQt5.QtGui
object.
"""
uifile_text = open(fname).read().replace(replace_class, "QFrame")
with open(fname) as f:
uifile_text = f.read().replace(replace_class, "QFrame")
ui, base_class = uic.loadUiType(io.StringIO(uifile_text))

class Form(base_class, ui):
Expand All @@ -55,12 +56,10 @@ def compileui(fname, replace_class="QwtPlot"):
:param fname:
:param replace_class:
"""
uifile_text = open(fname).read().replace("QwtPlot", "QFrame")
uic.compileUi(
io.StringIO(uifile_text),
open(fname.replace(".ui", "_ui.py"), "w"),
pyqt3_wrapper=True,
)
with open(fname) as f:
uifile_text = f.read().replace("QwtPlot", "QFrame")
with open(fname.replace(".ui", "_ui.py"), "w") as pyfile:
uic.compileUi(io.StringIO(uifile_text), pyfile, pyqt3_wrapper=True)


def create_qtdesigner_plugin(
Expand Down

0 comments on commit f923828

Please sign in to comment.