From 05c10d0c1ab611d29942ca450b5191217824cb2d Mon Sep 17 00:00:00 2001 From: Ross Beyer Date: Tue, 26 Dec 2023 12:35:05 -0800 Subject: [PATCH] style(many): Enforced black formatting. --- Makefile | 8 +++++++- kalasiris/Histogram.py | 10 +++------- kalasiris/PathSet.py | 4 +--- kalasiris/cube.py | 8 ++------ kalasiris/cubenormfile.py | 8 +------- kalasiris/k_funcs.py | 4 ++-- kalasiris/pysis.py | 5 +---- kalasiris/specialpixels.py | 4 +--- kalasiris/version.py | 16 ++++++---------- setup.cfg | 1 + tests/test_Histogram.py | 6 +++--- tests/test_PathSet.py | 2 +- tests/test_cube.py | 39 ++++++++++++++++---------------------- tests/test_cubenormfile.py | 10 +++------- tests/test_fromlist.py | 2 +- tests/test_k_funcs.py | 10 +++------- tests/test_pysis.py | 6 ++---- tests/test_sweetened.py | 2 +- tests/test_version.py | 16 ++++------------ tests/utils.py | 8 ++++---- 20 files changed, 63 insertions(+), 106 deletions(-) diff --git a/Makefile b/Makefile index 75d2108..34b7c33 100644 --- a/Makefile +++ b/Makefile @@ -65,9 +65,15 @@ clean-test: ## remove test and coverage artifacts rm -fr test-resources rm -fr test-ISIS3DATA -lint: ## check style with flake8 +lint/flake8: ## check style with flake8 flake8 kalasiris tests +lint/black: ## check style with black + black --check kalasiris tests + +lint: lint/flake8 lint/black + twine check dist/* + test: test-resources ## run tests quickly with the default Python python -m pytest diff --git a/kalasiris/Histogram.py b/kalasiris/Histogram.py index 04b9988..c8082bf 100644 --- a/kalasiris/Histogram.py +++ b/kalasiris/Histogram.py @@ -30,9 +30,7 @@ def __init__(self, histinfo): self.histinfo = histinfo try: - (self.dictionary, self.headers, self.hist_list) = self.parse( - histinfo - ) + (self.dictionary, self.headers, self.hist_list) = self.parse(histinfo) except StopIteration: try: (self.dictionary, self.headers, self.hist_list) = self.parse( @@ -73,7 +71,7 @@ def __contains__(self, item): def keys(self): """Gets the keys from the initial portion of the hist output file. - These will be items like 'Cube', 'Band', 'Average', etc. + These will be items like 'Cube', 'Band', 'Average', etc. """ return self.dictionary.keys() @@ -146,9 +144,7 @@ def parse(histinfo: str) -> tuple: # d[k.strip()] = v.strip() d.setdefault(k.strip(), v.strip()) - reader = csv.reader( - filter(lambda x: "," in x, str(histinfo).splitlines()) - ) + reader = csv.reader(filter(lambda x: "," in x, str(histinfo).splitlines())) fieldnames = next(reader) HistRow = collections.namedtuple("HistRow", fieldnames) diff --git a/kalasiris/PathSet.py b/kalasiris/PathSet.py index b82ad21..ec0448b 100644 --- a/kalasiris/PathSet.py +++ b/kalasiris/PathSet.py @@ -55,9 +55,7 @@ def add(self, elem) -> Path: if not isinstance(elem, Path): raise TypeError("only accepts pathlib.Path objects") if elem in self: - raise ValueError( - f"The {elem} object is already a member of the PathSet." - ) + raise ValueError(f"The {elem} object is already a member of the PathSet.") super().add(elem) return elem diff --git a/kalasiris/cube.py b/kalasiris/cube.py index 8fc0adc..088d38e 100644 --- a/kalasiris/cube.py +++ b/kalasiris/cube.py @@ -63,9 +63,7 @@ def _get_start_size(d: dict) -> Tuple[int, int]: return start, size -def get_startsize_from( - label=None, table_name=None, cube_path=None -) -> Tuple[int, int]: +def get_startsize_from(label=None, table_name=None, cube_path=None) -> Tuple[int, int]: """Returns a tuple of ints that represent the true start byte and size based on the provided *label* or combination of *table_name* and *cube_path*. @@ -136,9 +134,7 @@ def get_startsize_from( # This function is derived from this commit dated Sep 24, 2019: # https://github.com/USGS-Astrogeology/ale/commit/add5368ba46b2c911de9515afeaccc4d1c981000 -def read_table_data( - cube_path: os.PathLike, label=None, table_name=None -) -> bytes: +def read_table_data(cube_path: os.PathLike, label=None, table_name=None) -> bytes: """Returns a bytes object with the contents read from the file at *cube_path* based on the elements provided in the *label* or *table_name*. diff --git a/kalasiris/cubenormfile.py b/kalasiris/cubenormfile.py index 7f9d33d..d241159 100644 --- a/kalasiris/cubenormfile.py +++ b/kalasiris/cubenormfile.py @@ -78,13 +78,7 @@ class DictWriter(csv.DictWriter): """A DictWriter for ``cubenorm`` files.""" def __init__( - self, - f, - restval="", - extrasaction="raise", - dialect=Dialect, - *args, - **kwds + self, f, restval="", extrasaction="raise", dialect=Dialect, *args, **kwds ): self.fieldnames = fieldnames self.restval = restval diff --git a/kalasiris/k_funcs.py b/kalasiris/k_funcs.py index a689fdd..379da1b 100644 --- a/kalasiris/k_funcs.py +++ b/kalasiris/k_funcs.py @@ -53,7 +53,7 @@ def hi2isis_k(*args, **kwargs): if len(args) > 0 and not str(args[0]).endswith("__"): from_path = Path(args[0]) else: - for (k, v) in kwargs.items(): + for k, v in kwargs.items(): if k.startswith("f"): from_path = Path(v) break @@ -70,7 +70,7 @@ def hist_k(*args, **kwargs) -> str: create the file, and return its contents as a string """ to_pathlike = None - for (k, v) in kwargs.items(): + for k, v in kwargs.items(): if "to" == k or "to_" == k: to_pathlike = v diff --git a/kalasiris/pysis.py b/kalasiris/pysis.py index 4768753..0f36389 100644 --- a/kalasiris/pysis.py +++ b/kalasiris/pysis.py @@ -53,10 +53,7 @@ def __init__(self, returncode, cmd, stdout, stderr): self.stdout = stdout self.stderr = stderr - msg = ( - f"Command {self.cmd} returned non-zero exit " - f"status {self.returncode}." - ) + msg = f"Command {self.cmd} returned non-zero exit " f"status {self.returncode}." super(ProcessError, self).__init__(msg) diff --git a/kalasiris/specialpixels.py b/kalasiris/specialpixels.py index 7971390..4554be1 100644 --- a/kalasiris/specialpixels.py +++ b/kalasiris/specialpixels.py @@ -62,9 +62,7 @@ ) # 1-byte special pixel values from SpecialPixel.h -UnsignedByte = SpecialPixels( - Min=1, Null=0, Lrs=0, Lis=0, His=255, Hrs=255, Max=254 -) +UnsignedByte = SpecialPixels(Min=1, Null=0, Lrs=0, Lis=0, His=255, Hrs=255, Max=254) # 2-byte unsigned special pixel values from SpecialPixel.h UnsignedWord = SpecialPixels( diff --git a/kalasiris/version.py b/kalasiris/version.py index 9107c1c..950a14b 100644 --- a/kalasiris/version.py +++ b/kalasiris/version.py @@ -40,9 +40,7 @@ class ISISversion( version_re = re.compile(r"(?P\d+)\.(?P\d+)\.(?P\d+)") date_re = re.compile(r"(?P\d{4})-(?P\d{1,2})-(?P\d{1,2})") -date_yearlast_re = re.compile( - r"(?P\d{1,2})-(?P\d{1,2})-(?P\d{4})" -) +date_yearlast_re = re.compile(r"(?P\d{1,2})-(?P\d{1,2})-(?P\d{4})") level_re = re.compile(r"^alpha|beta|stable") @@ -57,10 +55,10 @@ def version_info() -> ISISversion: def get_from_string(s: str) -> ISISversion: """Read text and parse the contents for ISIS version information. - This should parse ISIS version text as far back as ISIS 3.5.2.0, - but possibly earlier. It will return *None* values for releaselevel - and date if it cannot parse them. It will raise a :exc:`ValueError` - if it cannot parse a version number. + This should parse ISIS version text as far back as ISIS 3.5.2.0, + but possibly earlier. It will return *None* values for releaselevel + and date if it cannot parse them. It will raise a :exc:`ValueError` + if it cannot parse a version number. """ # Version Matching @@ -68,9 +66,7 @@ def get_from_string(s: str) -> ISISversion: if match: v = match.groupdict() else: - raise ValueError( - f"{s} did not match version regex: " f"{version_re.pattern}" - ) + raise ValueError(f"{s} did not match version regex: " f"{version_re.pattern}") # Date Matching d = None diff --git a/setup.cfg b/setup.cfg index 8c62d5a..ad495c0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -2,6 +2,7 @@ # Black formatter sometimes puts space before the colon (E203) and # puts a line break before binary operators (W503) ignore = E203, W503 +max_line_length = 88 max-complexity = 10 exclude = docs diff --git a/tests/test_Histogram.py b/tests/test_Histogram.py index af766f1..f5a46e9 100644 --- a/tests/test_Histogram.py +++ b/tests/test_Histogram.py @@ -18,7 +18,7 @@ from .utils import ( resource_check as rc, real_files as run_real_files, - real_files_reason as run_real_files_reason + real_files_reason as run_real_files_reason, ) # Hardcoding this, but I sure would like a better solution. @@ -192,11 +192,11 @@ def test_repr(self): def test_iter(self): rows = 0 for x in self.h: - rows +=1 + rows += 1 self.assertEqual(107, rows) - def test_contains(self): + def test_contains_false(self): self.assertFalse("Foo" in self.h) def test_keys(self): diff --git a/tests/test_PathSet.py b/tests/test_PathSet.py index 7ebcdff..8c12028 100644 --- a/tests/test_PathSet.py +++ b/tests/test_PathSet.py @@ -16,7 +16,7 @@ import kalasiris as isis from .utils import ( real_files as run_real_files, - real_files_reason as run_real_files_reason + real_files_reason as run_real_files_reason, ) diff --git a/tests/test_cube.py b/tests/test_cube.py index 75fb7c8..063aa4a 100644 --- a/tests/test_cube.py +++ b/tests/test_cube.py @@ -17,7 +17,7 @@ from .utils import ( resource_check as rc, real_files as run_real_files, - real_files_reason as run_real_files_reason + real_files_reason as run_real_files_reason, ) # Hardcoding this, but I sure would like a better solution. @@ -106,9 +106,7 @@ def test_get_table(self): try: import pvl # noqa F401 - table = isis.cube.get_table( - self.cube, "HiRISE Calibration Ancillary" - ) + table = isis.cube.get_table(self.cube, "HiRISE Calibration Ancillary") self.assertEqual(255, table["GapFlag"][0]) self.assertEqual(9, table["LineNumber"][9]) self.assertEqual(1359, table["BufferPixels"][0][0]) @@ -124,9 +122,7 @@ def test_get_table(self): def test_overwrite_table_data(self): data = bytes(10) - self.assertRaises( - ValueError, isis.cube.overwrite_table_data, self.cube, data - ) + self.assertRaises(ValueError, isis.cube.overwrite_table_data, self.cube, data) self.assertRaises( KeyError, isis.cube.overwrite_table_data, @@ -175,9 +171,7 @@ def test_encode_table(self): bad_table = dict(table) bad_table["TooLong"] = ["a", "b", "c", "d", "e"] - self.assertRaises( - IndexError, isis.cube.encode_table, bad_table, fields - ) + self.assertRaises(IndexError, isis.cube.encode_table, bad_table, fields) bad_fields = list(fields) bad_fields.append({"Name": "TooLong", "Type": "Text", "Size": "1"}) @@ -193,18 +187,21 @@ def test_encode_table(self): long_tab["List"] = [1, 2, 3, [4, 5]] long_field = list(fields) long_field.append({"Name": "List", "Type": "Integer", "Size": "1"}) - self.assertRaises( - IndexError, isis.cube.encode_table, long_tab, long_field - ) + self.assertRaises(IndexError, isis.cube.encode_table, long_tab, long_field) two_field = list(fields[1:]) two_field.append({"Name": "Foo", "Type": "Integer", "Size": "2"}) - self.assertRaises( - ValueError, isis.cube.encode_table, table, two_field - ) + self.assertRaises(ValueError, isis.cube.encode_table, table, two_field) seq_tab = dict(table) - seq_tab["List"] = [1, 2, 3, [4, ]] + seq_tab["List"] = [ + 1, + 2, + 3, + [ + 4, + ], + ] seq_field = list(fields) seq_field.append({"Name": "List", "Type": "Integer", "Size": "1"}) seq_data = isis.cube.encode_table(seq_tab, seq_field) @@ -240,13 +237,9 @@ def test_overwrite_table(self): try: import pvl # noqa F401 - isis.cube.overwrite_table( - self.cube, "HiRISE Calibration Ancillary", table - ) + isis.cube.overwrite_table(self.cube, "HiRISE Calibration Ancillary", table) - p_tab = isis.cube.get_table( - self.cube, "HiRISE Calibration Ancillary" - ) + p_tab = isis.cube.get_table(self.cube, "HiRISE Calibration Ancillary") self.assertEqual(255, p_tab["GapFlag"][0]) self.assertEqual(27, p_tab["LineNumber"][9]) diff --git a/tests/test_cubenormfile.py b/tests/test_cubenormfile.py index bb792ec..55e9bfb 100644 --- a/tests/test_cubenormfile.py +++ b/tests/test_cubenormfile.py @@ -20,7 +20,7 @@ from .utils import ( resource_check as rc, real_files as run_real_files, - real_files_reason as run_real_files_reason + real_files_reason as run_real_files_reason, ) @@ -57,18 +57,14 @@ def setUp(self): ) def test_Dialect(self): - reader = csv.reader( - self.stats.splitlines(), dialect=isis.cubenormfile.Dialect - ) + reader = csv.reader(self.stats.splitlines(), dialect=isis.cubenormfile.Dialect) for row in reader: self.assertEqual(8, len(row)) break def test_writer(self): columns = list() - reader = csv.reader( - self.stats.splitlines(), dialect=isis.cubenormfile.Dialect - ) + reader = csv.reader(self.stats.splitlines(), dialect=isis.cubenormfile.Dialect) for row in reader: columns.append(row) diff --git a/tests/test_fromlist.py b/tests/test_fromlist.py index df1c6c8..498e103 100644 --- a/tests/test_fromlist.py +++ b/tests/test_fromlist.py @@ -18,7 +18,7 @@ import kalasiris as isis from .utils import ( real_files as run_real_files, - real_files_reason as run_real_files_reason + real_files_reason as run_real_files_reason, ) diff --git a/tests/test_k_funcs.py b/tests/test_k_funcs.py index af6dc10..637ab60 100644 --- a/tests/test_k_funcs.py +++ b/tests/test_k_funcs.py @@ -20,7 +20,7 @@ from .utils import ( resource_check as rc, real_files as run_real_files, - real_files_reason as run_real_files_reason + real_files_reason as run_real_files_reason, ) # Hardcoding these, but I sure would like a better solution. @@ -67,9 +67,7 @@ class Test_hi2isis_k(unittest.TestCase): @patch("kalasiris.k_funcs.isis.hi2isis") def test_with_to(self, m_hi2i): isis.hi2isis_k("dummy.img", to="dummy.cub") - self.assertEqual( - m_hi2i.call_args_list, [call("dummy.img", to="dummy.cub")] - ) + self.assertEqual(m_hi2i.call_args_list, [call("dummy.img", to="dummy.cub")]) @patch("kalasiris.k_funcs.isis.hi2isis") def test_without_to(self, m_hi2i): @@ -190,9 +188,7 @@ class Test_cubeit_k(unittest.TestCase): @patch("kalasiris.k_funcs.isis.cubeit") def test_cubeit_k(self, m_cubeit): from_name = "temp_fromlist.txt" - m_context = Mock( - __enter__=Mock(return_value=from_name), __exit__=Mock() - ) + m_context = Mock(__enter__=Mock(return_value=from_name), __exit__=Mock()) m_temp = MagicMock(return_value=m_context) with patch("kalasiris.k_funcs.isis.fromlist.temp", m_temp): isis.cubeit_k(["a.cub", "b.cub", "c.cub"], to="stacked.cub") diff --git a/tests/test_pysis.py b/tests/test_pysis.py index 6cb5111..ae76479 100644 --- a/tests/test_pysis.py +++ b/tests/test_pysis.py @@ -19,7 +19,7 @@ from .utils import ( resource_check as rc, real_files as run_real_files, - real_files_reason as run_real_files_reason + real_files_reason as run_real_files_reason, ) @@ -72,9 +72,7 @@ def tearDown(self): def test_getkey(self): truth = b"HIRISE\n" - key = pysis.getkey( - self.cub, grpname="Instrument", keyword="InstrumentId" - ) + key = pysis.getkey(self.cub, grpname="Instrument", keyword="InstrumentId") self.assertEqual(truth, key) def test_getkey_fail(self): diff --git a/tests/test_sweetened.py b/tests/test_sweetened.py index c0c3055..5e5d0dd 100644 --- a/tests/test_sweetened.py +++ b/tests/test_sweetened.py @@ -18,7 +18,7 @@ from .utils import ( resource_check as rc, real_files as run_real_files, - real_files_reason as run_real_files_reason + real_files_reason as run_real_files_reason, ) diff --git a/tests/test_version.py b/tests/test_version.py index e6020c6..a0ebc82 100644 --- a/tests/test_version.py +++ b/tests/test_version.py @@ -22,34 +22,26 @@ def test_get_from_string(self): """3.7.0 # Public version number 2019-04-30 # Release date stable # release stage (alpha, beta, stable)""", - version.ISISversion( - 3, 7, 0, "stable", datetime.date(2019, 4, 30) - ), + version.ISISversion(3, 7, 0, "stable", datetime.date(2019, 4, 30)), ), ( """3.6.0 # Public version number 10-26-2018 # Release date stable # release stage (alpha, beta, stable)""", - version.ISISversion( - 3, 6, 0, "stable", datetime.date(2018, 10, 26) - ), + version.ISISversion(3, 6, 0, "stable", datetime.date(2018, 10, 26)), ), ( """3.5.2.0 2018-04-06 # Version date v007 # 3rd party libraries version stable # release stage (alpha, beta, stable)""", - version.ISISversion( - 3, 5, 2, "stable", datetime.date(2018, 4, 6) - ), + version.ISISversion(3, 5, 2, "stable", datetime.date(2018, 4, 6)), ), ( """3.5.2.0 v007 # 3rd party libraries version stable # release stage (alpha, beta, stable)""", - version.ISISversion( - 3, 5, 2, "stable", None - ), + version.ISISversion(3, 5, 2, "stable", None), ), ] diff --git a/tests/utils.py b/tests/utils.py index 7a5322a..579b201 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -19,10 +19,10 @@ def resource_check(*args): """Checks to see if the files exist. And returns a tuple. - Using the first element as the truth value and the second - as the test value in a ``self.assertEqual(truth, test)`` - provides a much more useful failure message than a bunch - of ``self.assertTrue(os.path.isfile(filename))`` + Using the first element as the truth value and the second + as the test value in a ``self.assertEqual(truth, test)`` + provides a much more useful failure message than a bunch + of ``self.assertTrue(os.path.isfile(filename))`` """ CheckReturn = collections.namedtuple("CheckReturn", ["truth", "test"])