Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Ruff linting rules (Bugbear, Pyupgrade, Ruff...) #214

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pyglider/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
# -*- coding: utf-8 -*-
2 changes: 1 addition & 1 deletion pyglider/ncprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def extract_timeseries_profiles(inname, outdir, deploymentyaml, force=False):

# add traj_strlen using bare ntcdf to make IOOS happy
with netCDF4.Dataset(outname, 'r+') as nc:
nc.renameDimension('string%d' % trajlen, 'traj_strlen')
nc.renameDimension(f'string{trajlen}', 'traj_strlen')


def make_gridfiles(
Expand Down
9 changes: 4 additions & 5 deletions pyglider/seaexplorer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
SeaExplorer-specific processing routines.
"""
Expand All @@ -21,8 +20,8 @@ def _outputname(f, outdir):
fnout = os.path.basename(f)
fns = fnout.split('.')
fns = fns[:5]
fns[4] = '%04d' % int(fns[4])
fns[1] = '%04d' % int(fns[1])
fns[4] = f'{int(fns[4]):04d}'
fns[1] = f'{int(fns[1]):04d}'
fnout = ''
for ff in fns:
fnout += ff.lower() + '.'
Expand Down Expand Up @@ -128,7 +127,7 @@ def raw_to_rawnc(
# If no files of this type found, try the next type
continue

for ind, f in enumerate(files):
for f in files:
# output name:
fnout, filenum = _outputname(f, outdir)
_log.info(f'{f} to {fnout}')
Expand Down Expand Up @@ -573,4 +572,4 @@ def raw_to_timeseries(
raw_to_L1timeseries = raw_to_L0timeseries = raw_to_timeseries
merge_rawnc = merge_parquet

__all__ = ['raw_to_rawnc', 'merge_parquet', 'raw_to_timeseries']
__all__ = ['merge_parquet', 'raw_to_rawnc', 'raw_to_timeseries']
18 changes: 9 additions & 9 deletions pyglider/slocum.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@
activeSensorList = [{} for i in range(nsensors_used)]
outlines = []
sensorInfo = {}
for i in range(nsensors_total):
for _ in range(nsensors_total):

Check warning on line 163 in pyglider/slocum.py

View check run for this annotation

Codecov / codecov/patch

pyglider/slocum.py#L163

Added line #L163 was not covered by tests
line = dfh.readline().decode('utf-8')
if line.split(':')[0] != 's':
raise ValueError('Failed to parse sensor info')
Expand Down Expand Up @@ -264,7 +264,7 @@
# read the cache first. If its not there, try to make one....
try:
activeSensorList, sensorInfo = _get_cached_sensorlist(cachedir, meta)
except FileNotFoundError:
except FileNotFoundError as e:

Check warning on line 267 in pyglider/slocum.py

View check run for this annotation

Codecov / codecov/patch

pyglider/slocum.py#L267

Added line #L267 was not covered by tests
if localcache:
_log.info('No cache file found; trying to create one')
_make_cache(outlines, cachedir, meta)
Expand All @@ -275,7 +275,7 @@
'offloaddir/Science/STATE/CACHE/ or ',
'offloaddir/Main_board/STATE/CACHE/. ',
f'Copy those locally into {cachedir}',
)
) from e
meta['activeSensorList'] = activeSensorList
# get the file's timestamp...
meta['_dbdfiletimestamp'] = os.path.getmtime(filename)
Expand Down Expand Up @@ -441,8 +441,8 @@

proctimeend = time.time()
_log.info(
('%s lines of data read from %s, data rate of %s rows ' 'per second')
% (len(data), dinkum_file, len(data) / (proctimeend - proctimestart))
f'{len(data)} lines of data read from {dinkum_file}, data rate of {len(data) / (proctimeend - proctimestart)} rows '
'per second'
)
dfh.close()

Expand Down Expand Up @@ -605,7 +605,7 @@
# make a long string for activeSensorList:
listst = ''
for sensor in meta['activeSensorList']:
listst += '%s' % sensor
listst += str(sensor)

Check warning on line 608 in pyglider/slocum.py

View check run for this annotation

Codecov / codecov/patch

pyglider/slocum.py#L608

Added line #L608 was not covered by tests
listst += '\n'
ds.attrs['activeSensorList'] = listst

Expand Down Expand Up @@ -1128,7 +1128,7 @@
for fn in files:
found_time = False

with open(fn, 'r') as fin:
with open(fn) as fin:

Check warning on line 1131 in pyglider/slocum.py

View check run for this annotation

Codecov / codecov/patch

pyglider/slocum.py#L1131

Added line #L1131 was not covered by tests
for ll in fin:
if 'Curr Time:' in ll:
times[ntimes] = ll
Expand Down Expand Up @@ -1208,7 +1208,7 @@
for fn in files:
found_time = False

with open(fn, 'r') as fin:
with open(fn) as fin:

Check warning on line 1211 in pyglider/slocum.py

View check run for this annotation

Codecov / codecov/patch

pyglider/slocum.py#L1211

Added line #L1211 was not covered by tests
for l in fin:
if 'Curr Time:' in l:
times[ntimes] = l
Expand Down Expand Up @@ -1273,7 +1273,7 @@
__all__ = [
'binary_to_rawnc',
'merge_rawnc',
'raw_to_timeseries',
'parse_gliderState',
'parse_logfiles',
'raw_to_timeseries',
]
22 changes: 11 additions & 11 deletions pyglider/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
)
dpall = np.diff(p)
inflect = np.where(dpall[:-1] * dpall[1:] < 0)[0]
for n, i in enumerate(inflect[:-1]):
for n, _ in enumerate(inflect[:-1]):

Check warning on line 127 in pyglider/utils.py

View check run for this annotation

Codecov / codecov/patch

pyglider/utils.py#L127

Added line #L127 was not covered by tests
nprofile = inflect[n + 1] - inflect[n]
inds = np.arange(good[inflect[n]], good[inflect[n + 1]] + 1) + 1
dp = np.diff(ds.pressure[inds[[-1, 0]]])
Expand Down Expand Up @@ -553,8 +553,8 @@
ds.attrs['title'] = ds.attrs['id']

dt = ds.time.values
ds.attrs['time_coverage_start'] = '%s' % dt[0]
ds.attrs['time_coverage_end'] = '%s' % dt[-1]
ds.attrs['time_coverage_start'] = str(dt[0])
ds.attrs['time_coverage_end'] = str(dt[-1])

ds.attrs['processing_level'] = (
'Level 0 (L0) processed data timeseries; ' 'no corrections or data screening'
Expand Down Expand Up @@ -701,7 +701,7 @@
xmln = fname
from bs4 import BeautifulSoup

with open(xmln, 'r') as fin:
with open(xmln) as fin:

Check warning on line 704 in pyglider/utils.py

View check run for this annotation

Codecov / codecov/patch

pyglider/utils.py#L704

Added line #L704 was not covered by tests
y = BeautifulSoup(fin, features='xml')
time = None
lon = None
Expand Down Expand Up @@ -735,7 +735,7 @@
xmln = fname
from bs4 import BeautifulSoup

with open(xmln, 'r') as fin:
with open(xmln) as fin:

Check warning on line 738 in pyglider/utils.py

View check run for this annotation

Codecov / codecov/patch

pyglider/utils.py#L738

Added line #L738 was not covered by tests
y = BeautifulSoup(fin, features='xml')
time = None
for a in y.find_all('report'):
Expand All @@ -751,8 +751,8 @@
def example_gridplot(
filename,
outname,
toplot=['potential_temperature', 'salinity', 'oxygen_concentration'],
pdenlevels=np.arange(10, 30, 0.5),
toplot=['potential_temperature', 'salinity', 'oxygen_concentration'], # noqa: B006
pdenlevels=np.arange(10, 30, 0.5), # noqa: B008
dpi=200,
ylim=None,
):
Expand Down Expand Up @@ -790,7 +790,7 @@
deploymentyaml,
]
deployment = {}
for nn, d in enumerate(deploymentyaml):
for d in deploymentyaml:
with open(d) as fin:
deployment_ = yaml.safe_load(fin)
for k in deployment_:
Expand Down Expand Up @@ -834,12 +834,12 @@


__all__ = [
'fill_metadata',
'gappy_fill_vertical',
'get_derived_eos_raw',
'get_distance_over_ground',
'get_glider_depth',
'get_profiles_new',
'get_derived_eos_raw',
'fill_metadata',
'nmea2deg',
'gappy_fill_vertical',
'oxygen_concentration_correction',
]
22 changes: 11 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ filterwarnings = [
]

[tool.ruff.lint]
# See https://docs.astral.sh/ruff/rules/ for all rules
select = [
"F", # pyflakes
"I", # isort
"E", # Error
"F", # pyflakes
"I", # isort
"E", # Error
"B", # Bugbear
"UP", # pyupgrade
"LOG", # logging
"ICN", # import conventions
"G", # logging-format
"RUF", # ruff
# "D", # pydocstyle
# "B", # Bugbear
# "UP", # pyupgrade
# "LOG", # logging
# "ICN", # import conventions
# "G", # logging-format
# "RUF", # ruff
]

ignore = [
# TODO: Remove
"E402", # Module level import not at top of file
"RUF005", # Consider `[*outlines, line]` instead of concatenation

"F841", # Local variable `...` is assigned to but never used
"E722", # Do not use bare `except`
Expand Down
4 changes: 2 additions & 2 deletions tests/test_seaexplorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import pytest
import yaml

import pyglider.seaexplorer as seaexplorer

os.system('rm tests/data/realtime_rawnc/*')
library_dir = Path(__file__).parent.parent.absolute()
example_dir = library_dir / 'tests/example-data/'

import pyglider.seaexplorer as seaexplorer


def test__outputname():
fnout, filenum = seaexplorer._outputname(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_slocum.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def test_profiles_compliant():
output_format=output_format,
)
# Open the JSON output and get the compliance scores
with open(output_filename, 'r') as fp:
with open(output_filename) as fp:
cc_data = json.load(fp)
test = cc_data['gliderdac']
assert test['high_count'] == 0
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_timeseries_compliant():
output_format=output_format,
)
# Open the JSON output and get the compliance scores
with open(output_filename, 'r') as fp:
with open(output_filename) as fp:
cc_data = json.load(fp)
test = cc_data['cf:1.8']
assert test['high_count'] == 0
Expand Down
Loading