diff --git a/becquerel/core/plotting.py b/becquerel/core/plotting.py index df626e99..11a2652f 100644 --- a/becquerel/core/plotting.py +++ b/becquerel/core/plotting.py @@ -94,18 +94,17 @@ def xmode(self, mode): self._xmode = "energy" else: self._xmode = "channel" + elif mode.lower() in ("kev", "energy"): + if not self.spec.is_calibrated: + raise PlottingError( + "Spectrum is not calibrated, however " + "x axis was requested as energy" + ) + self._xmode = "energy" + elif mode.lower() in ("channel", "channels", "chn", "chns"): + self._xmode = "channel" else: - if mode.lower() in ("kev", "energy"): - if not self.spec.is_calibrated: - raise PlottingError( - "Spectrum is not calibrated, however " - "x axis was requested as energy" - ) - self._xmode = "energy" - elif mode.lower() in ("channel", "channels", "chn", "chns"): - self._xmode = "channel" - else: - raise PlottingError(f"Unknown x data mode: {mode}") + raise PlottingError(f"Unknown x data mode: {mode}") # Then, set the _xedges and _xlabel based on the _xmode xedges, xlabel = self.spec.parse_xmode(self._xmode) diff --git a/becquerel/core/spectrum.py b/becquerel/core/spectrum.py index 0ed2dde3..1e1181ff 100644 --- a/becquerel/core/spectrum.py +++ b/becquerel/core/spectrum.py @@ -985,13 +985,12 @@ def _mul_div(self, scaling_factor: float, div=False): or np.isnan(scaling_factor) ): raise ValueError("Scaling factor must be nonzero and finite") - else: - if ( - scaling_factor.nominal_value == 0 - or np.isinf(scaling_factor.nominal_value) - or np.isnan(scaling_factor.nominal_value) - ): - raise ValueError("Scaling factor must be nonzero and finite") + elif ( + scaling_factor.nominal_value == 0 + or np.isinf(scaling_factor.nominal_value) + or np.isnan(scaling_factor.nominal_value) + ): + raise ValueError("Scaling factor must be nonzero and finite") if div: multiplier = 1 / scaling_factor else: @@ -1481,7 +1480,7 @@ def plot(self, *fmt, **kwargs): color = ax.get_lines()[-1].get_color() if emode == "band": plotter.errorband(color=color, alpha=alpha * 0.5, label="_nolegend_") - elif emode == "bars" or emode == "bar": + elif emode in ("bars", "bar"): plotter.errorbar(color=color, label="_nolegend_") elif emode != "none": raise SpectrumError(f"Unknown error mode '{emode}', use 'bars' or 'band'") diff --git a/becquerel/tools/isotope.py b/becquerel/tools/isotope.py index 3b595e60..6b041397 100644 --- a/becquerel/tools/isotope.py +++ b/becquerel/tools/isotope.py @@ -235,35 +235,31 @@ def _init_m(self, arg): if arg == "" or arg is None or arg == 0: self.m = "" self.M = 0 - else: - if isinstance(arg, int): - if arg == 1: - self.m = "m" - self.M = 1 - elif arg >= 2: - self.M = arg - self.m = f"m{self.M}" - else: - raise IsotopeError(f"Metastable level must be >= 0: {arg}") - elif isinstance(arg, str): - self.m = arg.lower() - if self.m[0] != "m": + elif isinstance(arg, int): + if arg == 1: + self.m = "m" + self.M = 1 + elif arg >= 2: + self.M = arg + self.m = f"m{self.M}" + else: + raise IsotopeError(f"Metastable level must be >= 0: {arg}") + elif isinstance(arg, str): + self.m = arg.lower() + if self.m[0] != "m": + raise IsotopeError(f'Metastable level must start with "m": {self.m}') + if len(self.m) > 1: + if not self.m[1:].isdigit(): raise IsotopeError( - f'Metastable level must start with "m": {self.m}' + f"Metastable level must be numeric: {self.m[0]} {self.m[1:]}" ) - if len(self.m) > 1: - if not self.m[1:].isdigit(): - raise IsotopeError( - "Metastable level must be numeric: " - f"{self.m[0]} {self.m[1:]}" - ) - self.M = int(self.m[1:]) - else: - self.M = 1 + self.M = int(self.m[1:]) else: - raise IsotopeError( - f"Metastable level must be integer or string: {arg} {type(arg)}" - ) + self.M = 1 + else: + raise IsotopeError( + f"Metastable level must be integer or string: {arg} {type(arg)}" + ) def __str__(self): """Define behavior of str() on Isotope.""" diff --git a/becquerel/tools/nndc.py b/becquerel/tools/nndc.py index 570b7885..52d5f7c2 100644 --- a/becquerel/tools/nndc.py +++ b/becquerel/tools/nndc.py @@ -122,7 +122,7 @@ def _parse_headers(headers): # reformat column headers if needed for j, hd in enumerate(headers): # rename so always have T1/2 (s) - if hd == "T1/2 (num)" or hd == "T1/2 (seconds)": + if hd in ("T1/2 (num)", "T1/2 (seconds)"): hd = "T1/2 (s)" # for uncertainties, add previous column header to it if j > 0 and "Unc" in hd: @@ -260,7 +260,7 @@ def _parse_float_uncertainty(x, dx): if "8 .0E-E5" in x: x = x.replace("8 .0E-E5", "8.0E-5") # handle blank or missing data - if x == "" or x == " ": + if x in ("", " "): return None if "****" in dx or dx in ["LT", "GT", "LE", "GE", "AP", "CA", "SY"]: dx = "" diff --git a/examples/nndc_chart_of_nuclides.ipynb b/examples/nndc_chart_of_nuclides.ipynb index 1787a409..7a31d805 100644 --- a/examples/nndc_chart_of_nuclides.ipynb +++ b/examples/nndc_chart_of_nuclides.ipynb @@ -17,9 +17,9 @@ "metadata": {}, "outputs": [], "source": [ - "import matplotlib.patches as patches\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", + "from matplotlib import patches\n", "\n", "import becquerel as bq" ] diff --git a/pyproject.toml b/pyproject.toml index 384bc0cf..a8e69fdd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -69,7 +69,7 @@ select = [ # "ERA", # eradicate "PD", # pandas-vet "PGH", # pygrep-hooks - # "PL", # pylint + "PL", # pylint "TRY", # tryceratops "FLY", # flynt "NPY", # NumPy-specific rules @@ -87,6 +87,8 @@ ignore = [ "SIM108", # Use ternary operator instead of `if`-`else`-block "SIM300", # Yoda conditions are discouraged "PD901", # Avoid using the generic variable name `df` for DataFrames + "PLW2901", # `for` loop variable overwritten by assignment target + "PLR2004", # Magic value used in comparison, consider replacing with a constant variable "TRY003", # Avoid specifying long messages outside the exception class "NPY002", # Replace legacy `np.random.poisson` call with `np.random.Generator` "PERF203", # `try`-`except` within a loop incurs performance overhead @@ -114,7 +116,8 @@ convention = "google" [tool.ruff.lint.pylint] max-args = 15 -max-branches = 15 +max-branches = 60 max-locals = 25 max-nested-blocks = 15 -max-statements = 100 +max-returns = 20 +max-statements = 150 diff --git a/tests/materials_test.py b/tests/materials_test.py index e595b14b..a99166a7 100644 --- a/tests/materials_test.py +++ b/tests/materials_test.py @@ -7,12 +7,12 @@ import pytest from utils import xcom_is_up -import becquerel.tools.materials as materials -import becquerel.tools.materials_compendium as materials_compendium from becquerel.tools import ( MaterialsError, MaterialsWarning, fetch_materials, + materials, + materials_compendium, remove_materials_csv, ) from becquerel.tools.materials_nist import convert_composition diff --git a/tests/wallet_cache_test.py b/tests/wallet_cache_test.py index 1b4ece39..501e8c79 100644 --- a/tests/wallet_cache_test.py +++ b/tests/wallet_cache_test.py @@ -5,7 +5,7 @@ import uncertainties from utils import nndc_is_up -import becquerel.tools.wallet_cache as wallet_cache +from becquerel.tools import wallet_cache @pytest.mark.parametrize(