From 05364237ab5975a62288bcaa7f4e4dbd95eb1097 Mon Sep 17 00:00:00 2001 From: Ben Elliston Date: Sat, 29 Jun 2024 15:57:54 +1000 Subject: [PATCH] Even more ruff fixes. --- setup.py | 3 ++- tests/test_generators.py | 7 ++++--- tests/test_storage.py | 1 - tests/test_utils.py | 23 ++++++++--------------- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/setup.py b/setup.py index 13f11405..c3940e29 100644 --- a/setup.py +++ b/setup.py @@ -2,9 +2,10 @@ import time +from pathlib import Path from setuptools import setup -with open('README.md', encoding='utf-8') as f: +with Path('README.md').open() as f: long_description = f.read() setup(name='nemopt', diff --git a/tests/test_generators.py b/tests/test_generators.py index 31db0ce5..5b2afabc 100644 --- a/tests/test_generators.py +++ b/tests/test_generators.py @@ -8,8 +8,8 @@ """A testsuite for the generators module.""" import inspect -import os import unittest +from pathlib import Path import numpy as np import pandas as pd @@ -77,7 +77,7 @@ class TestGenerators(unittest.TestCase): def setUp(self): """Test harness setup.""" self.tracefile = 'tracedata.csv' - with open(self.tracefile, 'w', encoding='utf-8') as tracefile: + with Path(self.tracefile).open('w') as tracefile: for i in range(100): print(f'{0.01 * i:.2f}, 0', file=tracefile) @@ -107,7 +107,8 @@ def setUp(self): def tearDown(self): """Remove tracefile on teardown.""" - os.unlink(self.tracefile) + path = Path(self.tracefile) + path.unlink() def test_series(self): """Test series() method.""" diff --git a/tests/test_storage.py b/tests/test_storage.py index 4a4a0063..006c36af 100644 --- a/tests/test_storage.py +++ b/tests/test_storage.py @@ -67,7 +67,6 @@ class TestPumpedHydro(unittest.TestCase): def setUp(self): """Test harness setup.""" - # Attach the reservroir to the pump and the turbine. # In this case, the pump and turbine are symmetric at 100 MW. self.reservoir = storage.PumpedHydroStorage(1000) diff --git a/tests/test_utils.py b/tests/test_utils.py index e3c39d6f..b8193ce6 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -10,9 +10,9 @@ # Some protected members (eg _figure) are accessed to facilitate testing. # pylint: disable=protected-access -import os import unittest from datetime import timedelta +from pathlib import Path import pytest @@ -24,18 +24,11 @@ class TestUtils(unittest.TestCase): def unlink(self, filename): """Unlink filename without error.""" - try: - os.unlink(filename) - except FileNotFoundError: - pass + Path(filename).unlink(missing_ok=True) def exists(self, filename): """Return True if filename exists.""" - try: - os.stat(filename) - return True - except FileNotFoundError: - return False + return Path(filename).exists() def setUp(self): """Test harness setup.""" @@ -43,13 +36,13 @@ def setUp(self): scenarios.ccgt(self.context) sim.run(self.context) - @pytest.mark.mpl_image_compare + @pytest.mark.mpl_image_compare() def test_figure_1(self): """Test simple supply/demand plot.""" utils._figure(self.context, spills=True, showlegend=True, xlim=None) return utils.plt.gcf() - @pytest.mark.mpl_image_compare + @pytest.mark.mpl_image_compare() def test_figure_2(self): """Test supply/demand plot with many generators.""" ngens = len(self.context.generators) @@ -68,7 +61,7 @@ def test_plot_1(self): self.unlink(fname) utils.plot(self.context, filename=fname) self.assertTrue(self.exists(fname)) - os.unlink(fname) + self.unlink(fname) def test_plot_2(self): """Test plot with only 7 days of data.""" @@ -78,7 +71,7 @@ def test_plot_2(self): self.unlink(fname) utils.plot(self.context, filename=fname, xlim=(start, end)) self.assertTrue(self.exists(fname)) - os.unlink(fname) + self.unlink(fname) def test_plot_3(self): """Test plot with only 7 days of data.""" @@ -88,4 +81,4 @@ def test_plot_3(self): self.context.timesteps = lambda: 7 * 24 utils.plot(self.context, filename=fname) self.assertTrue(self.exists(fname)) - os.unlink(fname) + self.unlink(fname)