Skip to content

Commit

Permalink
MNT: Use zip's strict keyword argument available with 3.10
Browse files Browse the repository at this point in the history
Identified by ruff.
  • Loading branch information
dopplershift committed Aug 28, 2024
1 parent 695e6a2 commit eb36c3d
Show file tree
Hide file tree
Showing 26 changed files with 74 additions and 68 deletions.
3 changes: 2 additions & 1 deletion examples/formats/NEXRAD_Level_2_File.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
fig = plt.figure(figsize=(15, 8))
add_metpy_logo(fig, 190, 85, size='large')

for var_data, var_range, ax_rect in zip((ref, rho), (ref_range, rho_range), spec):
for var_data, var_range, ax_rect in zip((ref, rho), (ref_range, rho_range), spec,
strict=False):
# Turn into an array, then mask
data = np.ma.array(var_data)
data[np.isnan(data)] = np.ma.masked
Expand Down
2 changes: 1 addition & 1 deletion examples/formats/NEXRAD_Level_3_File.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
ctables = (('NWSStormClearReflectivity', -20, 0.5), # dBZ
('NWS8bitVel', -100, 1.0)) # m/s

for v, ctable, ax_rect in zip(('N0Q', 'N0U'), ctables, spec):
for v, ctable, ax_rect in zip(('N0Q', 'N0U'), ctables, spec, strict=False):
# Open the file
name = get_test_data(f'nids/KOUN_SDUS54_{v}TLX_201305202016', as_file_obj=False)
f = Level3File(name)
Expand Down
10 changes: 5 additions & 5 deletions examples/gridding/Inverse_Distance_Verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ def draw_circle(ax, x, y, r, m, label):
#
# The variable ``indices`` represents the index of each matched coordinate within the
# cKDTree's ``data`` list.
grid_points = np.array(list(zip(sim_gridx, sim_gridy)))
grid_points = np.array(list(zip(sim_gridx, sim_gridy, strict=False)))

radius = 40
obs_tree = cKDTree(list(zip(xp, yp)))
obs_tree = cKDTree(list(zip(xp, yp, strict=False)))
indices = obs_tree.query_ball_point(grid_points, r=radius)

###########################################
Expand All @@ -83,7 +83,7 @@ def draw_circle(ax, x, y, r, m, label):
barnes_dist = dist_2(sim_gridx[1], sim_gridy[1], x2, y2)
barnes_obs = zp[indices[1]]

kappa = calc_kappa(average_spacing(list(zip(xp, yp))))
kappa = calc_kappa(average_spacing(list(zip(xp, yp, strict=False))))

barnes_val = barnes_point(barnes_dist, barnes_obs, kappa)

Expand Down Expand Up @@ -121,7 +121,7 @@ def draw_circle(ax, x, y, r, m, label):
mx, my = obs_tree.data[indices[0]].T
mz = zp[indices[0]]

for x, y, z in zip(mx, my, mz):
for x, y, z in zip(mx, my, mz, strict=False):
d = np.sqrt((sim_gridx[0] - x)**2 + (y - sim_gridy[0])**2)
ax.plot([sim_gridx[0], x], [sim_gridy[0], y], '--')

Expand Down Expand Up @@ -160,7 +160,7 @@ def draw_circle(ax, x, y, r, m, label):
mx, my = obs_tree.data[indices[1]].T
mz = zp[indices[1]]

for x, y, z in zip(mx, my, mz):
for x, y, z in zip(mx, my, mz, strict=False):
d = np.sqrt((sim_gridx[1] - x)**2 + (y - sim_gridy[1])**2)
ax.plot([sim_gridx[1], x], [sim_gridy[1], y], '--')

Expand Down
7 changes: 4 additions & 3 deletions examples/gridding/Natural_Neighbor_Verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
ax.set_title('Triangulation of observations and test grid cell '
'natural neighbor interpolation values')

members, circumcenters = geometry.find_natural_neighbors(tri, list(zip(sim_gridx, sim_gridy)))
members, circumcenters = geometry.find_natural_neighbors(tri, list(zip(sim_gridx, sim_gridy,
strict=False)))

val = natural_neighbor_point(xp, yp, zp, (sim_gridx[0], sim_gridy[0]), tri, members[0],
circumcenters)
Expand Down Expand Up @@ -164,7 +165,7 @@ def draw_circle(ax, x, y, r, m, label):
# spatial data structure that we use here simply to show areal ratios.
# Notice that the two natural neighbor triangle circumcenters are also vertices
# in the Voronoi plot (green dots), and the observations are in the polygons (blue dots).
vort = Voronoi(list(zip(xp, yp)))
vort = Voronoi(list(zip(xp, yp, strict=False)))

fig, ax = plt.subplots(1, 1, figsize=(15, 10))
ax.ishold = lambda: True # Work-around for Matplotlib 3.0.0 incompatibility
Expand All @@ -175,7 +176,7 @@ def draw_circle(ax, x, y, r, m, label):
x_0 = xp[nn_ind]
y_0 = yp[nn_ind]

for x, y, z in zip(x_0, y_0, z_0):
for x, y, z in zip(x_0, y_0, z_0, strict=False):
ax.annotate(f'{x}, {y}: {z:.3f} F', xy=(x, y))

ax.plot(sim_gridx[0], sim_gridy[0], 'k+', markersize=10)
Expand Down
2 changes: 1 addition & 1 deletion examples/plots/Plotting_Surface_Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def plot_bulletin(ax, data):
# Handle H/L points using MetPy's StationPlot class
for field in ('HIGH', 'LOW'):
rows = data[data.feature == field]
x, y = zip(*((pt.x, pt.y) for pt in rows.geometry))
x, y = zip(*((pt.x, pt.y) for pt in rows.geometry), strict=False)
sp = StationPlot(ax, x, y, transform=ccrs.PlateCarree(), clip_on=True)
sp.plot_text('C', [field[0]] * len(x), **complete_style[field])
sp.plot_parameter('S', rows.strength, **complete_style[field])
Expand Down
2 changes: 1 addition & 1 deletion examples/plots/US_Counties.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
ax2 = fig.add_subplot(1, 3, 2, projection=proj)
ax3 = fig.add_subplot(1, 3, 3, projection=proj)

for scale, axis in zip(['20m', '5m', '500k'], [ax1, ax2, ax3]):
for scale, axis in zip(['20m', '5m', '500k'], [ax1, ax2, ax3], strict=False):
axis.set_extent([270.25, 270.9, 38.15, 38.75], ccrs.Geodetic())
axis.add_feature(USCOUNTIES.with_scale(scale))
2 changes: 1 addition & 1 deletion src/metpy/calc/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ def zoom_xarray(input_field, zoom, output=None, order=3, mode='constant', cval=0
if not np.iterable(zoom):
zoom = tuple(zoom for _ in input_field.dims)
zoomed_dim_coords = {}
for dim_name, dim_zoom in zip(input_field.dims, zoom):
for dim_name, dim_zoom in zip(input_field.dims, zoom, strict=False):
if dim_name in input_field.coords:
zoomed_dim_coords[dim_name] = scipy_zoom(
input_field[dim_name].data, dim_zoom, order=order, mode=mode, cval=cval,
Expand Down
2 changes: 1 addition & 1 deletion src/metpy/calc/kinematics.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def advection(

return -sum(
wind * gradient
for wind, gradient in zip(wind_vector.values(), gradient_vector)
for wind, gradient in zip(wind_vector.values(), gradient_vector, strict=False)
)


Expand Down
2 changes: 1 addition & 1 deletion src/metpy/calc/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ def _wide_option(intersect_type, p_list, t_list, pressure, parcel_temperature_pr
lfc_p_list, _ = find_intersections(pressure, parcel_temperature_profile,
temperature, direction='increasing',
log_x=True)
diff = [lfc_p.m - el_p.m for lfc_p, el_p in zip(lfc_p_list, el_p_list)]
diff = [lfc_p.m - el_p.m for lfc_p, el_p in zip(lfc_p_list, el_p_list, strict=False)]
return (p_list[np.where(diff == np.max(diff))][0],
t_list[np.where(diff == np.max(diff))][0])

Expand Down
6 changes: 3 additions & 3 deletions src/metpy/interpolate/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def natural_neighbor_to_grid(xp, yp, variable, grid_x, grid_y):
"""
# Handle grid-to-points conversion, and use function from `interpolation`
points_obs = list(zip(xp, yp))
points_obs = list(zip(xp, yp, strict=False))
points_grid = generate_grid_coords(grid_x, grid_y)
img = natural_neighbor_to_points(points_obs, variable, points_grid)
return img.reshape(grid_x.shape)
Expand Down Expand Up @@ -214,7 +214,7 @@ def inverse_distance_to_grid(xp, yp, variable, grid_x, grid_y, r, gamma=None, ka
"""
# Handle grid-to-points conversion, and use function from `interpolation`
points_obs = list(zip(xp, yp))
points_obs = list(zip(xp, yp, strict=False))
points_grid = generate_grid_coords(grid_x, grid_y)
img = inverse_distance_to_points(points_obs, variable, points_grid, r, gamma=gamma,
kappa=kappa, min_neighbors=min_neighbors, kind=kind)
Expand Down Expand Up @@ -296,7 +296,7 @@ def interpolate_to_grid(x, y, z, interp_type='linear', hres=50000,
grid_x, grid_y = generate_grid(hres, boundary_coords)

# Handle grid-to-points conversion, and use function from `interpolation`
points_obs = np.array(list(zip(x, y)))
points_obs = np.array(list(zip(x, y, strict=False)))
points_grid = generate_grid_coords(grid_x, grid_y)
img = interpolate_to_points(points_obs, z, points_grid, interp_type=interp_type,
minimum_neighbors=minimum_neighbors, gamma=gamma,
Expand Down
6 changes: 3 additions & 3 deletions src/metpy/interpolate/points.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def cressman_point(sq_dist, values, radius):
weights = tools.cressman_weights(sq_dist, radius)
total_weights = np.sum(weights)

return sum(v * (w / total_weights) for (w, v) in zip(weights, values))
return sum(v * (w / total_weights) for (w, v) in zip(weights, values, strict=False))


def barnes_point(sq_dist, values, kappa, gamma=None):
Expand Down Expand Up @@ -82,7 +82,7 @@ def barnes_point(sq_dist, values, kappa, gamma=None):
weights = tools.barnes_weights(sq_dist, kappa, gamma)
total_weights = np.sum(weights)

return sum(v * (w / total_weights) for (w, v) in zip(weights, values))
return sum(v * (w / total_weights) for (w, v) in zip(weights, values, strict=False))


def natural_neighbor_point(xp, yp, variable, grid_loc, tri, neighbors, circumcenters):
Expand Down Expand Up @@ -271,7 +271,7 @@ def inverse_distance_to_points(points, values, xi, r, gamma=None, kappa=None, mi

img = np.asarray([interp_func(geometry.dist_2(*grid, *obs_tree.data[matches].T),
values[matches]) if len(matches) >= min_neighbors else np.nan
for matches, grid in zip(indices, xi)])
for matches, grid in zip(indices, xi, strict=False)])

if org_units:
img = units.Quantity(img, org_units)
Expand Down
2 changes: 1 addition & 1 deletion src/metpy/interpolate/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def remove_repeat_coordinates(x, y, z):
coords = []
variable = []

for (x_, y_, t_) in zip(x, y, z):
for (x_, y_, t_) in zip(x, y, z, strict=False):
if (x_, y_) not in coords:
coords.append((x_, y_))
variable.append(t_)
Expand Down
8 changes: 4 additions & 4 deletions src/metpy/io/_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(self, info, prefmt='', tuple_name=None):
"""Initialize the NamedStruct."""
if tuple_name is None:
tuple_name = 'NamedStruct'
names, fmts = zip(*info)
names, fmts = zip(*info, strict=False)
self.converters = {}
conv_off = 0
for ind, i in enumerate(info):
Expand Down Expand Up @@ -118,7 +118,7 @@ class DictStruct:

def __init__(self, info, prefmt=''):
"""Initialize the DictStruct."""
names, formats = zip(*info)
names, formats = zip(*info, strict=False)

# Remove empty names
self._names = [n for n in names if n]
Expand All @@ -131,7 +131,7 @@ def size(self):
return self._struct.size

def _create(self, items):
return dict(zip(self._names, items))
return dict(zip(self._names, items, strict=False))

def unpack(self, s):
"""Parse bytes and return a dict."""
Expand All @@ -151,7 +151,7 @@ def __init__(self, *args, **kwargs):
self.val_map = dict(enumerate(args))

# Invert the kwargs dict so that we can map from value to name
self.val_map.update(zip(kwargs.values(), kwargs.keys()))
self.val_map.update(zip(kwargs.values(), kwargs.keys(), strict=False))

def __call__(self, val):
"""Map an integer to the string representation."""
Expand Down
7 changes: 4 additions & 3 deletions src/metpy/io/gempak.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,9 @@ def __init__(self, file):
fkey_prod = product(['header_name', 'header_length', 'header_type'],
range(1, self.prod_desc.file_headers + 1))
fkey_names = ['{}{}'.format(*x) for x in fkey_prod]
fkey_info = list(zip(fkey_names, np.repeat(('4s', 'i', 'i'),
self.prod_desc.file_headers)))
fkey_info = list(zip(fkey_names,
np.repeat(('4s', 'i', 'i'), self.prod_desc.file_headers),
strict=False))
self.file_keys_format = NamedStruct(fkey_info, self.prefmt, 'FileKeys')

self._buffer.jump_to(self._start, _word_to_position(self.prod_desc.file_keys_ptr))
Expand Down Expand Up @@ -1897,7 +1898,7 @@ def _merge_sounding(self, parts):
if num_man_levels >= 1:
for mp, mt, mz in zip(parts['TTAA']['PRES'],
parts['TTAA']['TEMP'],
parts['TTAA']['HGHT']):
parts['TTAA']['HGHT'], strict=False):
if self.prod_desc.missing_float not in [
mp,
mt,
Expand Down
2 changes: 1 addition & 1 deletion src/metpy/io/gini.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _scaled_int(s):

def _name_lookup(names):
r"""Create an io helper to convert an integer to a named value."""
mapper = dict(zip(range(len(names)), names))
mapper = dict(zip(range(len(names)), names, strict=False))

def lookup(val):
return mapper.get(val, 'UnknownValue')
Expand Down
14 changes: 7 additions & 7 deletions src/metpy/io/nexrad.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def _decode_msg15(self, msg_hdr):
num_rng = data[offset]
codes = data[offset + 1:offset + 1 + 2 * num_rng:2]
ends = data[offset + 2:offset + 2 + 2 * num_rng:2]
az_data.append(list(zip(ends, codes)))
az_data.append(list(zip(ends, codes, strict=False)))
offset += 2 * num_rng + 1
self.clutter_filter_map['data'].append(az_data)

Expand Down Expand Up @@ -1963,7 +1963,7 @@ def _unpack_packet_radial_data(self, code, in_sym_block):
rads.append((start_az, end_az,
self._unpack_rle_data(
self._buffer.read_binary(2 * rad.num_hwords))))
start, end, vals = zip(*rads)
start, end, vals = zip(*rads, strict=False)
return {'start_az': list(start), 'end_az': list(end), 'data': list(vals),
'center': (hdr.i_center * self.pos_scale(in_sym_block),
hdr.j_center * self.pos_scale(in_sym_block)),
Expand All @@ -1984,7 +1984,7 @@ def _unpack_packet_digital_radial(self, code, in_sym_block):
start_az = rad.start_angle * 0.1
end_az = start_az + rad.angle_delta * 0.1
rads.append((start_az, end_az, self._buffer.read_binary(rad.num_bytes)))
start, end, vals = zip(*rads)
start, end, vals = zip(*rads, strict=False)
return {'start_az': list(start), 'end_az': list(end), 'data': list(vals),
'center': (hdr.i_center * self.pos_scale(in_sym_block),
hdr.j_center * self.pos_scale(in_sym_block)),
Expand Down Expand Up @@ -2148,7 +2148,7 @@ def _unpack_packet_digital_precipitation(self, code, in_sym_block):
row = self._unpack_rle_data(row_bytes)
else:
row = []
for run, level in zip(row_bytes[::2], row_bytes[1::2]):
for run, level in zip(row_bytes[::2], row_bytes[1::2], strict=False):
row.extend([level] * run)
assert len(row) == lfm_boxes
rows.append(row)
Expand All @@ -2164,7 +2164,7 @@ def _unpack_packet_linked_vector(self, code, in_sym_block):
value = None
scale = self.pos_scale(in_sym_block)
pos = [b * scale for b in self._buffer.read_binary(num_bytes / 2, '>h')]
vectors = list(zip(pos[::2], pos[1::2]))
vectors = list(zip(pos[::2], pos[1::2], strict=False))
return {'vectors': vectors, 'color': value}

def _unpack_packet_vector(self, code, in_sym_block):
Expand All @@ -2176,7 +2176,7 @@ def _unpack_packet_vector(self, code, in_sym_block):
value = None
scale = self.pos_scale(in_sym_block)
pos = [p * scale for p in self._buffer.read_binary(num_bytes / 2, '>h')]
vectors = list(zip(pos[::4], pos[1::4], pos[2::4], pos[3::4]))
vectors = list(zip(pos[::4], pos[1::4], pos[2::4], pos[3::4], strict=False))
return {'vectors': vectors, 'color': value}

def _unpack_packet_contour_color(self, code, in_sym_block):
Expand All @@ -2196,7 +2196,7 @@ def _unpack_packet_linked_contour(self, code, in_sym_block):
vectors = [(startx, starty)]
num_bytes = self._buffer.read_int(2, 'big', signed=False)
pos = [b * scale for b in self._buffer.read_binary(num_bytes / 2, '>h')]
vectors.extend(zip(pos[::2], pos[1::2]))
vectors.extend(zip(pos[::2], pos[1::2], strict=False))
return {'vectors': vectors}

def _unpack_packet_wind_barbs(self, code, in_sym_block):
Expand Down
2 changes: 1 addition & 1 deletion src/metpy/plots/_mpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def draw(self, renderer):

angle = self.get_rotation()

for (posx, posy), t in zip(pts, self.text):
for (posx, posy), t in zip(pts, self.text, strict=False):
# Skip empty strings--not only is this a performance gain, but it fixes
# rendering with path effects below.
if not t:
Expand Down
4 changes: 2 additions & 2 deletions src/metpy/plots/declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ def _build(self):
# The order here needs to match the order of the tuple
if self.arrowkey is not None:
key_kwargs = {'U': 100, 'X': 0.85, 'Y': 1.02, 'labelpos': 'E', 'label': ''}
for name, val in zip(key_kwargs, self.arrowkey):
for name, val in zip(key_kwargs, self.arrowkey, strict=False):
if val is not None:
key_kwargs[name] = val
self.parent.ax.quiverkey(self.handle, labelcolor=self.color, **key_kwargs)
Expand Down Expand Up @@ -2247,7 +2247,7 @@ def _build(self):
# Each Shapely object is plotted separately with its corresponding strength
# and customizable parameters
for geo_obj, strengthvalues, feature in zip(
self.geometry, strengths, self.feature):
self.geometry, strengths, self.feature, strict=True):
kwargs = self.mpl_args.copy()
# Plot the Shapely object with the appropriate method and style
if isinstance(geo_obj, (LineString)):
Expand Down
Loading

0 comments on commit eb36c3d

Please sign in to comment.