Skip to content

Commit

Permalink
Enable ruff pylint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
markbandstra committed Jun 5, 2024
1 parent 9bb294b commit a06daf2
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 54 deletions.
21 changes: 10 additions & 11 deletions becquerel/core/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 7 additions & 8 deletions becquerel/core/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'")
Expand Down
48 changes: 22 additions & 26 deletions becquerel/tools/isotope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
4 changes: 2 additions & 2 deletions becquerel/tools/nndc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 = ""
Expand Down
2 changes: 1 addition & 1 deletion examples/nndc_chart_of_nuclides.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions tests/materials_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/wallet_cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit a06daf2

Please sign in to comment.