From 07566672ebf4fb611528c243918b6a478d6795d0 Mon Sep 17 00:00:00 2001 From: Anthony Romaniello <66272872+aromanielloNTIA@users.noreply.github.com> Date: Tue, 31 May 2022 09:46:56 -0600 Subject: [PATCH] Improved JSON loading syntax Minor improvement to file loading: see [PEP 343](https://peps.python.org/pep-0343/) - this syntax adds a built-in `try:`/`finally:` statement around the `json.load()` call, which ensures the file is closed. This is a more robust way to implement the file load, especially valuable for open source code which may be subject to more modifications in the future. Also see [this](https://thispointer.com/python-open-a-file-using-open-with-statement-benefits-explained-with-examples/) page for more practical examples motivating this syntax. --- src/its_preselector/test/test_preselector.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/its_preselector/test/test_preselector.py b/src/its_preselector/test/test_preselector.py index 84679a6..ca812f7 100644 --- a/src/its_preselector/test/test_preselector.py +++ b/src/its_preselector/test/test_preselector.py @@ -15,13 +15,10 @@ def setUpClass(cls): null_def = json.load(null_file) null_file.close() cls.empty_preselector = WebRelayPreselector(null_def, {}) - file = open('sensor_definition.json') - sensor_def = json.load(file) - file.close() + with open('sensor_definition.json', 'r') as f: + sensor_def = json.load(f) cls.scos_preselector = WebRelayPreselector(sensor_def, {}) - - def test_valid_preselector(self): self.assertIsNotNone(self.preselector)