Skip to content

Commit

Permalink
Improved JSON loading syntax
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aromanielloNTIA committed May 31, 2022
1 parent 7801714 commit 0756667
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/its_preselector/test/test_preselector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 0756667

Please sign in to comment.