Skip to content

Commit b32b467

Browse files
authored
Merge pull request #134 from ocefpaf/ruff_autofix
Ruff autofix
2 parents 8762b51 + 90ec869 commit b32b467

37 files changed

+1796
-1841
lines changed

.pre-commit-config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ repos:
2121
rev: v0.9.4
2222
hooks:
2323
- id: ruff
24+
args: ["--fix", "--show-fixes"]
25+
- id: ruff-format
2426

2527
- repo: https://github.com/tox-dev/pyproject-fmt
2628
rev: "v2.5.0"

pocean/cf.py

+71-76
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111

1212
class CFDataset(EnhancedDataset):
13-
1413
default_fill_value = False
15-
default_time_unit = 'seconds since 1990-01-01 00:00:00Z'
14+
default_time_unit = "seconds since 1990-01-01 00:00:00Z"
1615

1716
@classmethod
1817
def load(cls, path):
@@ -45,38 +44,38 @@ def load(cls, path):
4544
try:
4645
dsg = cls(path)
4746
for klass in subs:
48-
logger.debug(f'Trying {klass.__name__}...')
49-
if hasattr(klass, 'is_mine'):
47+
logger.debug(f"Trying {klass.__name__}...")
48+
if hasattr(klass, "is_mine"):
5049
if klass.is_mine(dsg):
5150
return klass(path)
5251
except OSError:
5352
raise
5453
finally:
55-
if hasattr(dsg, 'close'):
54+
if hasattr(dsg, "close"):
5655
dsg.close()
5756

58-
subnames = ', '.join([ s.__name__ for s in subs ])
59-
raise ValueError(
60-
'Could not open {} as any type of CF Dataset. Tried: {}.'.format(
61-
path,
62-
subnames
63-
)
64-
)
57+
subnames = ", ".join([s.__name__ for s in subs])
58+
raise ValueError(f"Could not open {path} as any type of CF Dataset. Tried: {subnames}.")
6559

6660
def axes(self, name):
67-
return getattr(self, f'{name.lower()}_axes')()
61+
return getattr(self, f"{name.lower()}_axes")()
6862

6963
def t_axes(self):
70-
7164
# If there is only one variable with the axis parameter, return it
72-
hasaxis = self.filter_by_attrs(axis=lambda x: x and str(x).lower() == 't')
65+
hasaxis = self.filter_by_attrs(axis=lambda x: x and str(x).lower() == "t")
7366
if len(hasaxis) == 1:
7467
return hasaxis
7568

76-
tvars = list(set(itertools.chain(
77-
hasaxis,
78-
self.filter_by_attrs(standard_name=lambda x: x in ['time', 'forecast_reference_time'])
79-
)))
69+
tvars = list(
70+
set(
71+
itertools.chain(
72+
hasaxis,
73+
self.filter_by_attrs(
74+
standard_name=lambda x: x in ["time", "forecast_reference_time"]
75+
),
76+
)
77+
)
78+
)
8079
return tvars
8180

8281
def x_axes(self):
@@ -87,76 +86,74 @@ def x_axes(self):
8786
* The `standard_name` property is one of ``'longitude'``,
8887
``'projection_x_coordinate'`` or ``'grid_longitude'``
8988
"""
90-
xnames = ['longitude', 'grid_longitude', 'projection_x_coordinate']
91-
xunits = [
92-
'degrees_east',
93-
'degree_east',
94-
'degree_E',
95-
'degrees_E',
96-
'degreeE',
97-
'degreesE'
98-
]
89+
xnames = ["longitude", "grid_longitude", "projection_x_coordinate"]
90+
xunits = ["degrees_east", "degree_east", "degree_E", "degrees_E", "degreeE", "degreesE"]
9991

10092
# If there is only one variable with the axis parameter, return it
101-
hasaxis = self.filter_by_attrs(axis=lambda x: x and str(x).lower() == 'x')
93+
hasaxis = self.filter_by_attrs(axis=lambda x: x and str(x).lower() == "x")
10294
if len(hasaxis) == 1:
10395
return hasaxis
10496

105-
xvars = list(set(itertools.chain(
106-
hasaxis,
107-
self.filter_by_attrs(standard_name=lambda x: x and str(x).lower() in xnames),
108-
self.filter_by_attrs(units=lambda x: x and str(x).lower() in xunits)
109-
)))
97+
xvars = list(
98+
set(
99+
itertools.chain(
100+
hasaxis,
101+
self.filter_by_attrs(standard_name=lambda x: x and str(x).lower() in xnames),
102+
self.filter_by_attrs(units=lambda x: x and str(x).lower() in xunits),
103+
)
104+
)
105+
)
110106
return xvars
111107

112108
def y_axes(self):
113-
ynames = ['latitude', 'grid_latitude', 'projection_y_coordinate']
114-
yunits = [
115-
'degrees_north',
116-
'degree_north',
117-
'degree_N',
118-
'degrees_N',
119-
'degreeN',
120-
'degreesN'
121-
]
109+
ynames = ["latitude", "grid_latitude", "projection_y_coordinate"]
110+
yunits = ["degrees_north", "degree_north", "degree_N", "degrees_N", "degreeN", "degreesN"]
122111

123112
# If there is only one variable with the axis parameter, return it
124-
hasaxis = self.filter_by_attrs(axis=lambda x: x and str(x).lower() == 'y')
113+
hasaxis = self.filter_by_attrs(axis=lambda x: x and str(x).lower() == "y")
125114
if len(hasaxis) == 1:
126115
return hasaxis
127116

128-
yvars = list(set(itertools.chain(
129-
hasaxis,
130-
self.filter_by_attrs(standard_name=lambda x: x and str(x).lower() in ynames),
131-
self.filter_by_attrs(units=lambda x: x and str(x).lower() in yunits)
132-
)))
117+
yvars = list(
118+
set(
119+
itertools.chain(
120+
hasaxis,
121+
self.filter_by_attrs(standard_name=lambda x: x and str(x).lower() in ynames),
122+
self.filter_by_attrs(units=lambda x: x and str(x).lower() in yunits),
123+
)
124+
)
125+
)
133126
return yvars
134127

135128
def z_axes(self):
136129
znames = [
137-
'atmosphere_ln_pressure_coordinate',
138-
'atmosphere_sigma_coordinate',
139-
'atmosphere_hybrid_sigma_pressure_coordinate',
140-
'atmosphere_hybrid_height_coordinate',
141-
'atmosphere_sleve_coordinate',
142-
'ocean_sigma_coordinate',
143-
'ocean_s_coordinate',
144-
'ocean_s_coordinate_g1',
145-
'ocean_s_coordinate_g2',
146-
'ocean_sigma_z_coordinate',
147-
'ocean_double_sigma_coordinate'
130+
"atmosphere_ln_pressure_coordinate",
131+
"atmosphere_sigma_coordinate",
132+
"atmosphere_hybrid_sigma_pressure_coordinate",
133+
"atmosphere_hybrid_height_coordinate",
134+
"atmosphere_sleve_coordinate",
135+
"ocean_sigma_coordinate",
136+
"ocean_s_coordinate",
137+
"ocean_s_coordinate_g1",
138+
"ocean_s_coordinate_g2",
139+
"ocean_sigma_z_coordinate",
140+
"ocean_double_sigma_coordinate",
148141
]
149142

150143
# If there is only one variable with the axis parameter, return it
151-
hasaxis = self.filter_by_attrs(axis=lambda x: x and str(x).lower() == 'z')
144+
hasaxis = self.filter_by_attrs(axis=lambda x: x and str(x).lower() == "z")
152145
if len(hasaxis) == 1:
153146
return hasaxis
154147

155-
zvars = list(set(itertools.chain(
156-
hasaxis,
157-
self.filter_by_attrs(positive=lambda x: x and str(x).lower() in ['up', 'down']),
158-
self.filter_by_attrs(standard_name=lambda x: x and str(x).lower() in znames)
159-
)))
148+
zvars = list(
149+
set(
150+
itertools.chain(
151+
hasaxis,
152+
self.filter_by_attrs(positive=lambda x: x and str(x).lower() in ["up", "down"]),
153+
self.filter_by_attrs(standard_name=lambda x: x and str(x).lower() in znames),
154+
)
155+
)
156+
)
160157
return zvars
161158

162159
def is_valid(self, *args, **kwargs):
@@ -169,34 +166,32 @@ def data_vars(self):
169166
standard_name=lambda x: x is not None,
170167
flag_values=lambda x: x is None,
171168
flag_masks=lambda x: x is None,
172-
flag_meanings=lambda x: x is None
169+
flag_meanings=lambda x: x is None,
173170
)
174171

175172
def ancillary_vars(self):
176173
ancillary_variables = []
177-
for rv in self.filter_by_attrs(
178-
ancillary_variables=lambda x: x is not None
179-
):
174+
for rv in self.filter_by_attrs(ancillary_variables=lambda x: x is not None):
180175
# Space separated ancillary variables
181-
for av in rv.ancillary_variables.split(' '):
176+
for av in rv.ancillary_variables.split(" "):
182177
if av in self.variables:
183178
ancillary_variables.append(self.variables[av])
184179
return list(set(ancillary_variables))
185180

186181
def nc_attributes(self):
187182
return {
188-
'global' : {
189-
'Conventions': 'CF-1.6',
190-
'date_created': datetime.utcnow().strftime("%Y-%m-%dT%H:%M:00Z"),
183+
"global": {
184+
"Conventions": "CF-1.6",
185+
"date_created": datetime.utcnow().strftime("%Y-%m-%dT%H:%M:00Z"),
191186
}
192187
}
193188

194189

195190
def cf_safe_name(name):
196191
if isinstance(name, str):
197-
if re.match('^[0-9_]', name):
192+
if re.match("^[0-9_]", name):
198193
# Add a letter to the front
199194
name = f"v_{name}"
200-
return re.sub(r'[^_a-zA-Z0-9]', "_", name)
195+
return re.sub(r"[^_a-zA-Z0-9]", "_", name)
201196

202197
raise ValueError(f'Could not convert "{name}" to a safe name')

0 commit comments

Comments
 (0)