From 7e121a1ba9373af3739f5c8b51dceb93ad872928 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 18 Apr 2024 13:24:44 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../grib_phenom_translation/_gribcode.py | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/iris_grib/grib_phenom_translation/_gribcode.py b/iris_grib/grib_phenom_translation/_gribcode.py index 19c2df72..47606a11 100644 --- a/iris_grib/grib_phenom_translation/_gribcode.py +++ b/iris_grib/grib_phenom_translation/_gribcode.py @@ -2,21 +2,19 @@ # # This file is part of iris-grib and is released under the BSD license. # See LICENSE in the root of the repository for full licensing details. -''' +""" Provide object to represent grib phenomena For use as cube attributes, freely convertible to+from strings -''' +""" + from dataclasses import dataclass import re from typing import Optional def _invalid_edition(edition): - msg = ( - f"Invalid grib edition, {edition!r}, for GRIBcode : " - "can only be 1 or 2." - ) + msg = f"Invalid grib edition, {edition!r}, for GRIBcode : " "can only be 1 or 2." raise ValueError(msg) @@ -33,7 +31,7 @@ def _invalid_nargs(args): # - for four times ... # - match any non-digits (including none) and discard # - then match any digits (including none), and return as a "group" -_RE_PARSE_FOURNUMS = re.compile(4 * r'[^\d]*(\d*)') +_RE_PARSE_FOURNUMS = re.compile(4 * r"[^\d]*(\d*)") def _fournums_from_gribcode_string(grib_param_string): @@ -47,9 +45,11 @@ def _fournums_from_gribcode_string(grib_param_string): parsed_ok = False if not parsed_ok: - msg = ('Invalid argument for GRIBCode creation, ' - '"GRIBCode({!r})" : ' - 'requires 4 numbers, separated by non-numerals.') + msg = ( + "Invalid argument for GRIBCode creation, " + '"GRIBCode({!r})" : ' + "requires 4 numbers, separated by non-numerals." + ) raise ValueError(msg.format(grib_param_string)) return nums @@ -79,8 +79,7 @@ def GRIBCode(edition, *args, **kwargs): # Convert to a string and extract 4 integers. # NOTE: this also allows input from a GRIBCode, or a plain tuple. edition_string = str(edition) - edition, arg2, arg3, arg4 = \ - _fournums_from_gribcode_string(edition_string) + edition, arg2, arg3, arg4 = _fournums_from_gribcode_string(edition_string) args = [arg2, arg3, arg4] # Check edition + select the relevant keywords for the edition @@ -94,7 +93,7 @@ def GRIBCode(edition, *args, **kwargs): # Convert all of (edition, *args) into **kwargs if not args: # Ignore that edition= is a required arg -- make it a kwarg - kwargs['edition'] = edition + kwargs["edition"] = edition else: # Include edition, which just makes it simpler args = tuple([edition] + list(args)) @@ -124,18 +123,18 @@ class GenericConcreteGRIBCode: GRIBCode1 and GRIBCode2 inherit this, making both dataclasses. They contain different data properties. """ + def __init__(self, **kwargs): # Note : only support creation with kargs. In GRIBCode(), any args # get translated into kwargs # Check against "_edition", defined by the specific subclass. - assert kwargs['edition'] == self._edition + assert kwargs["edition"] == self._edition for key, value in kwargs.items(): setattr(self, key, value) def _broken_repr(self): result = ( - f"<{self.__class__.__name__} with invalid content: " - f"{self.__dict__}>" + f"<{self.__class__.__name__} with invalid content: " f"{self.__dict__}>" ) return result @@ -145,12 +144,9 @@ def __str__(self): # NB fallback to "invalid" if edition not one of (1, 2) format = { 1: "GRIB{:1d}:t{:03d}c{:03d}n{:03d}", - 2: "GRIB{:1d}:d{:03d}c{:03d}n{:03d}" + 2: "GRIB{:1d}:d{:03d}c{:03d}n{:03d}", }[edition] - arg_values = [ - getattr(self, argname) - for argname in self.argnames - ] + arg_values = [getattr(self, argname) for argname in self.argnames] # NB fallback to "invalid" if format fails result = format.format(*arg_values) except Exception: @@ -168,7 +164,7 @@ def __repr__(self): value = getattr(self, argname, None) assert isinstance(value, int) key_value_strings.append(f"{argname}={value}") - inner_text = ', '.join(key_value_strings) + inner_text = ", ".join(key_value_strings) result = f"GRIBCode({inner_text})" except Exception: # Invalid content somewhere : fall back on a default repr