Skip to content

Commit

Permalink
Fix pylint violations
Browse files Browse the repository at this point in the history
  • Loading branch information
reuterbal committed Nov 11, 2024
1 parent 6299a7f commit f3e7b3f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 26 deletions.
10 changes: 8 additions & 2 deletions ifsbench/darshanreport.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ def open_darshan_logfile(filepath):
execute(['darshan-parser', str(filepath)], stdout=logfile)
except CalledProcessError as e:
if logpath.stat().st_size > 0:
warning('darshan-parser exited with non-zero exit code. Continue with potentially incomplete file...')
warning((
'darshan-parser exited with non-zero exit code. '
'Continue with potentially incomplete file...'
))
else:
error('darshan-parser exited with non-zero exit code and did not produce an output file.')
error((
'darshan-parser exited with non-zero exit code. '
'No output file produced.'
))
raise e

filepath = logpath
Expand Down
2 changes: 1 addition & 1 deletion ifsbench/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def _sha256sum(filepath):
# memory will be a bad idea for large GRIB files).
chunk_size = 4*1024*1024
sha = sha256()

with filepath.open('rb') as f:
chunk = f.read(chunk_size)
while chunk:
Expand Down
31 changes: 17 additions & 14 deletions ifsbench/gribfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
from .logging import error, warning


CFGRIB_AVAILABLE = False
ECCODES_AVAILABLE = False
PYGRIB_AVAILABLE = False

try:
import eccodes
from pkg_resources import packaging
Expand All @@ -27,34 +31,29 @@
# pylint: enable=no-member
ECCODES_AVAILABLE = True
except (RuntimeError, ImportError):
ECCODES_AVAILABLE = False
CFGRIB_AVAILABLE = False
PYGRIB_AVAILABLE = False

class gribmessage:
pass

pass

if ECCODES_AVAILABLE:
try:
import cfgrib

CFGRIB_AVAILABLE = True
except (RuntimeError, ImportError):
CFGRIB_AVAILABLE = False
pass
try:
from pygrib import open as pgopen
from pygrib import gribmessage

PYGRIB_AVAILABLE = True
except (RuntimeError, ImportError):
PYGRIB_AVAILABLE = False
pass

# pylint: disable=function-redefined
class gribmessage:
pass
if not PYGRIB_AVAILABLE:
# pylint: disable=function-redefined
class gribmessage:
pass

# pylint: enable=function-redefined
# pylint: enable=function-redefined


__all__ = [
Expand Down Expand Up @@ -82,7 +81,7 @@ class GribFile:
def __init__(self, input_path: str):
if not CFGRIB_AVAILABLE:
raise RuntimeError(
'Cannot read GRIB files cfgrib or eccodes not available.'
'Cannot read GRIB files. cfgrib or eccodes not available.'
)

self._input_path = input_path
Expand Down Expand Up @@ -126,6 +125,9 @@ def _read_grib(cls, input_path: str) -> List[xr.Dataset]:
Returns: List of datasets containing the data from the file.
"""
if not CFGRIB_AVAILABLE:
raise RuntimeError(f'Cannot read grib file {input_path}. cfgrib is not installed.')
# pylint: disable=possibly-used-before-assignment
return cfgrib.open_datasets(input_path, backend_kwargs={'indexpath': ''})

@classmethod
Expand Down Expand Up @@ -266,6 +268,7 @@ def modify_grib_file(
)
return
with open(output_path, 'wb') as outfile:
# pylint: disable=possibly-used-before-assignment
grbs = pgopen(input_path)
for grb in grbs:
grb_mod = _handle_grib_message(grb, base_modification, parameter_config)
Expand Down
2 changes: 1 addition & 1 deletion ifsbench/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def disable():


# Set colours on true terminals
if sys.stdout.isatty:
if sys.stdout.isatty():
colors.enable()
else:
colors.disable()
Expand Down
7 changes: 3 additions & 4 deletions ifsbench/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def execute(command, **kwargs):

if logfile:
# If we're file-logging, intercept via pipe
_log_file = Path(logfile).open('w', encoding='utf-8')
_log_file = Path(logfile).open('w', encoding='utf-8') # pylint: disable=consider-using-with
cmd_args['stdout'] = PIPE
else:
_log_file = None
Expand Down Expand Up @@ -239,8 +239,7 @@ def is_iterable(o):
iter(o)
except TypeError:
return False
else:
return True
return True


def flatten(l):
Expand Down Expand Up @@ -275,7 +274,7 @@ def auto_post_mortem_debugger(type, value, tb): # pylint: disable=redefined-bui
Activate by setting ``sys.excepthook = auto_post_mortem_debugger``
Adapted from
Adapted from
https://code.activestate.com/recipes/65287-automatically-start-the-debugger-on-an-exception/
"""
is_interactive = hasattr(sys, 'ps1')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_input_file(here):
assert the_file == also_the_file

with pytest.raises(OSError):
no_exist_file = InputFile('/i_dont_exist', compute_metadata=True)
_ = InputFile('/i_dont_exist', compute_metadata=True)


def test_experiment_files(here):
Expand Down
6 changes: 4 additions & 2 deletions tests/test_gribfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ def test_modify_grib_output_exists(here, tmp_path):
no_noise = NoGribModification()

# Create the output and set its mtime and atime to the past.
open(output_path, 'w').close()
with output_path.open(mode='w'):
pass
os.utime(output_path, ns=(1000, 1000))

modify_grib_file(input_path, output_path, no_noise)
Expand All @@ -322,7 +323,8 @@ def test_modify_grib_output_exists_allow_overwrite(here, tmp_path):
no_noise = NoGribModification()

# Create the output and set its mtime and atime to the past.
open(output_path, 'w').close()
with output_path.open(mode='w'):
pass
os.utime(output_path, ns=(1000, 1000))

modify_grib_file(input_path, output_path, no_noise, overwrite_existing=True)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_nodelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ def test_grid_norms(node_path, nrows, ncolumns):
)
def test_sanitize_value(value, expected):
""" Test correct sanitisation to standard scientific format. """
assert expected == pd.to_numeric(NODEFile._sanitise_float(value))
assert expected == pd.to_numeric(NODEFile._sanitise_float(value))

0 comments on commit f3e7b3f

Please sign in to comment.