Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Apr 18, 2024
1 parent d6155d7 commit 7e121a1
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions iris_grib/grib_phenom_translation/_gribcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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):
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit 7e121a1

Please sign in to comment.