Skip to content

Commit

Permalink
Wrap all open() calls in a context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom authored and Tom committed Jul 25, 2017
1 parent 3b38a46 commit 7a4d967
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 8 deletions.
3 changes: 2 additions & 1 deletion malcolm/modules/ADCore/parts/hdfwriterpart.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def configure(self, context, completed_steps, steps_to_do, part_info, params):
xml = self._make_layout_xml(params.generator, part_info)
self.layout_filename = os.path.join(
file_dir, "%s-layout.xml" % self.params.mri)
open(self.layout_filename, "w").write(xml)
with open(self.layout_filename, "w") as f:
f.write(xml)
# We want the HDF writer to flush this often:
flush_time = 1 # seconds
# (In particular this means that HDF files can be read cleanly by
Expand Down
3 changes: 2 additions & 1 deletion malcolm/modules/ADCore/parts/statspluginpart.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def configure(self, context, completed_steps, steps_to_do, part_info,
xml = self._make_attributes_xml()
self.attributes_filename = os.path.join(
params.fileDir, "%s-attributes.xml" % self.params.mri)
open(self.attributes_filename, "w").write(xml)
with open(self.attributes_filename, "w") as f:
f.write(xml)
fs.append(
child.attributesFile.put_value_async(self.attributes_filename))
context.wait_all_futures(fs)
Expand Down
6 changes: 4 additions & 2 deletions malcolm/modules/builtin/controllers/managercontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,8 @@ def do_save(self, design=""):
structure[part_name] = part_structure
text = json_encode(structure, indent=2)
filename = self._validated_config_filename(design)
open(filename, "w").write(text)
with open(filename, "w") as f:
f.write(text)
self._mark_clean(design)

def _set_layout_names(self, extra_name=None):
Expand Down Expand Up @@ -445,7 +446,8 @@ def set_design(self, value):

def do_load(self, design):
filename = self._validated_config_filename(design)
text = open(filename, "r").read()
with open(filename, "r") as f:
text = f.read()
structure = json_decode(text)
# Set the layout table
layout_table = Table(self.layout.meta)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_modules/test_ADCore/test_hdfwriterpart.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ def test_configure(self):
</group>
</group>
</hdf5_layout>"""
actual_xml = open(expected_xml_filename).read().replace(">", ">\n")
with open(expected_xml_filename) as f:
actual_xml = f.read().replace(">", ">\n")
assert actual_xml.splitlines() == expected_xml.splitlines()

def test_run(self):
Expand Down
3 changes: 2 additions & 1 deletion tests/test_modules/test_ADCore/test_statspluginpart.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_configure(self):
<Attributes>
<Attribute addr="0" datatype="DOUBLE" description="Sum of the array" name="STATS_TOTAL" source="TOTAL" type="PARAM" />
</Attributes>"""
actual_xml = open(expected_filename).read().replace(">", ">\n")
with open(expected_filename) as f:
actual_xml = f.read().replace(">", ">\n")
assert actual_xml.splitlines() == expected_xml.splitlines()

4 changes: 2 additions & 2 deletions tests/test_modules/test_builtin/test_managercontroller.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def check_expected_save(self, x=0.0, y=0.0, visible="true", attr="defaultv"):
"attr": "%s"
}
}""" % (x, y, visible, attr)).splitlines()]
actual = [x.strip() for x in open(
"/tmp/mainBlock/testSaveLayout.json").readlines()]
with open("/tmp/mainBlock/testSaveLayout.json") as f:
actual = [x.strip() for x in f.readlines()]
assert actual == expected

def test_save(self):
Expand Down

0 comments on commit 7a4d967

Please sign in to comment.