Skip to content

Commit 95518b8

Browse files
author
Release Manager
committed
gh-40725: some fixes for ruff SIM warnings mainly about using `.get()` ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #40725 Reported by: Frédéric Chapoton Reviewer(s): Martin Rubey
2 parents 41fbe03 + 8c88e59 commit 95518b8

File tree

13 files changed

+40
-87
lines changed

13 files changed

+40
-87
lines changed

src/sage/algebras/lie_algebras/representation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ def __init__(self, L, minimal=False):
628628

629629
def as_exp(s):
630630
sm = s._monomial
631-
return tuple([sm[i] if i in sm else 0 for i in I])
631+
return tuple([sm.get(i, 0) for i in I])
632632

633633
def test_ideal(m, X):
634634
elt = self._pbw.element_class(self._pbw, {monoid(list(zip(I, m))): one})
@@ -706,13 +706,13 @@ def _project(self, elt):
706706
if self._minimal:
707707
for m, c in elt._monomial_coefficients.items():
708708
mm = m._monomial
709-
vec = tuple([mm[i] if i in mm else 0 for i in I])
709+
vec = tuple([mm.get(i, 0) for i in I])
710710
if vec in self._indices:
711711
ret[self._indices(vec)] = c
712712
else:
713713
for m, c in elt._monomial_coefficients.items():
714714
mm = m._monomial
715-
vec = [mm[i] if i in mm else 0 for i in I]
715+
vec = [mm.get(i, 0) for i in I]
716716
if sum(e * d for e, d in zip(vec, self._degrees)) <= self._step:
717717
ret[self._indices(vec)] = c
718718
return self.element_class(self, ret)

src/sage/combinat/designs/orthogonal_arrays_build_recursive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,10 +743,10 @@ def thwart_lemma_3_5(k, n, m, a, b, c, d=0, complement=False, explain_constructi
743743
last_sets_dict = [{v:i for i,v in enumerate(s)} for s in last_sets]
744744

745745
# Truncating the OA
746-
for i,D in enumerate(last_sets_dict):
746+
for i, D in enumerate(last_sets_dict):
747747
kk = len(OA[0])-3+i
748748
for R in OA:
749-
R[kk] = D[R[kk]] if R[kk] in D else None
749+
R[kk] = D.get(R[kk], None)
750750

751751
if d:
752752
for R in OA:

src/sage/combinat/diagram_algebras.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,9 +1328,8 @@ def __contains__(self, obj):
13281328
return False
13291329
if obj.base_diagram():
13301330
tst = sorted(flatten(obj.base_diagram()))
1331-
if len(tst) % 2 or tst != list(range(-len(tst)//2,0)) + list(range(1,len(tst)//2+1)):
1332-
return False
1333-
return True
1331+
ell = len(tst)
1332+
return not ell % 2 and tst == list(range(-ell // 2, 0)) + list(range(1, ell // 2 + 1))
13341333
return self.order == 0
13351334

13361335
def _element_constructor_(self, d):

src/sage/combinat/sine_gordon.py

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -472,41 +472,21 @@ def plot(self, **kwds):
472472
else:
473473
radius = ceil(self.r() / (2 * pi))
474474
points_opts = {}
475-
if 'points_color' in kwds:
476-
points_opts['color'] = kwds['points_color']
477-
else:
478-
points_opts['color'] = 'black'
479-
if 'points_size' in kwds:
480-
points_opts['size'] = kwds['points_size']
481-
else:
482-
points_opts['size'] = 7
475+
points_opts['color'] = kwds.get('points_color', 'black')
476+
points_opts['size'] = kwds.get('points_size', 7)
483477
triangulation_opts = {}
484-
if 'triangulation_color' in kwds:
485-
triangulation_opts['color'] = kwds['triangulation_color']
486-
else:
487-
triangulation_opts['color'] = 'black'
488-
if 'triangulation_thickness' in kwds:
489-
triangulation_opts['thickness'] = kwds['triangulation_thickness']
490-
else:
491-
triangulation_opts['thickness'] = 0.5
478+
triangulation_opts['color'] = kwds.get('triangulation_color', 'black')
479+
triangulation_opts['thickness'] = kwds.get('triangulation_thickness',
480+
0.5)
492481
shading_opts = {}
493-
if 'shading_color' in kwds:
494-
shading_opts['color'] = kwds['shading_color']
495-
else:
496-
shading_opts['color'] = 'lightgray'
482+
shading_opts['color'] = kwds.get('shading_color', 'lightgray')
497483
reflections_opts = {}
498-
if 'reflections_color' in kwds:
499-
reflections_opts['color'] = kwds['reflections_color']
500-
else:
501-
reflections_opts['color'] = 'blue'
502-
if 'reflections_thickness' in kwds:
503-
reflections_opts['thickness'] = kwds['reflections_thickness']
504-
else:
505-
reflections_opts['thickness'] = 1
484+
reflections_opts['color'] = kwds.get('reflections_color', 'blue')
485+
reflections_opts['thickness'] = kwds.get('reflections_thickness', 1)
506486
# Helper functions
507487

508488
def triangle(x):
509-
(a, b) = sorted(x[:2])
489+
a, b = sorted(x[:2])
510490
for p in self.vertices():
511491
if (p, a) in self.triangulation() or (a, p) in self.triangulation():
512492
if (p, b) in self.triangulation() or (b, p) in self.triangulation():
@@ -522,7 +502,7 @@ def plot_arc(radius, p, q, **opts):
522502
if p - q in [1, -1]:
523503
def f(t):
524504
return (radius * cos(t), radius * sin(t))
525-
(p, q) = sorted([p, q])
505+
p, q = sorted([p, q])
526506
angle_p = vertex_to_angle(p)
527507
angle_q = vertex_to_angle(q)
528508
return parametric_plot(f(t), (t, angle_q, angle_p), **opts)
@@ -533,7 +513,7 @@ def f(t):
533513
angle_p += 2 * pi
534514
internal_angle = angle_p - angle_q
535515
if internal_angle > pi:
536-
(angle_p, angle_q) = (angle_q + 2 * pi, angle_p)
516+
angle_p, angle_q = (angle_q + 2 * pi, angle_p)
537517
internal_angle = angle_p - angle_q
538518
angle_center = (angle_p + angle_q) / 2
539519
hypotenuse = radius / cos(internal_angle / 2)
@@ -607,8 +587,8 @@ def vertex_to_angle(v):
607587
P += line([(0, 1.1 * radius), (0, -1.1 * radius)],
608588
zorder=len(P), **reflections_opts)
609589
axis_angle = vertex_to_angle(-0.5 * (self.rk() + (1, 1))[1])
610-
(a, b) = (1.1 * radius * cos(axis_angle),
611-
1.1 * radius * sin(axis_angle))
590+
a, b = (1.1 * radius * cos(axis_angle),
591+
1.1 * radius * sin(axis_angle))
612592
P += line([(a, b), (-a, -b)], zorder=len(P), **reflections_opts)
613593
# Wrap up
614594
P.set_aspect_ratio(1)

src/sage/databases/sql_db.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,9 @@ def _create_print_table(cur, col_titles, **kwds):
335335
pcol_map.append(kwds['plot_cols'][col])
336336
pcol_index.append(col_titles.index(col))
337337

338-
max_field_size = kwds['max_field_size'] if 'max_field_size' in kwds \
339-
else 20
338+
max_field_size = kwds.get('max_field_size', 20)
340339
id_col_index = col_titles.index(kwds['id_col']) if 'id_col' in kwds \
341-
else None
340+
else None
342341

343342
if 'relabel_cols' in kwds:
344343
relabel_cols = kwds['relabel_cols']

src/sage/functions/bessel.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,10 +1224,7 @@ def Bessel(*args, **kwds):
12241224
raise ValueError("inconsistent types given")
12251225
# record the function type
12261226
if _type is None:
1227-
if 'typ' in kwds:
1228-
_type = kwds['typ']
1229-
else:
1230-
_type = 'J'
1227+
_type = kwds.get('typ', 'J')
12311228
if _type not in ['I', 'J', 'K', 'Y']:
12321229
raise ValueError("type must be one of I, J, K, Y")
12331230

src/sage/homology/chain_complex.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,10 +1486,7 @@ def torsion_list(self, max_prime, min_prime=2):
14861486
temp_diff[i] = mod_p_betti.get(i, 0) - torsion_free[i]
14871487
for i in temp_diff:
14881488
if temp_diff[i] > 0:
1489-
if i+D in diff_dict:
1490-
lower = diff_dict[i+D]
1491-
else:
1492-
lower = 0
1489+
lower = diff_dict.get(i + D, 0)
14931490
current = temp_diff[i]
14941491
if current > lower:
14951492
diff_dict[i] = current - lower

src/sage/homology/graded_resolution.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,12 @@ def betti(self, i, a=None):
262262
else:
263263
degrees = [a]
264264

265-
betti = {}
266-
for s in degrees:
267-
betti[s] = len([d for d in shifts if d == s])
265+
betti = {s: len([d for d in shifts if d == s]) for s in degrees}
268266

269267
if a is None:
270268
return betti
271269
else:
272-
return betti[a] if a in betti else 0
270+
return betti.get(a, 0)
273271

274272
def K_polynomial(self, names=None):
275273
r"""

src/sage/manifolds/chart.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -913,10 +913,7 @@ def valid_coordinates(self, *coordinates, **kwds):
913913
"""
914914
if len(coordinates) != self.domain()._dim:
915915
return False
916-
if 'parameters' in kwds:
917-
parameters = kwds['parameters']
918-
else:
919-
parameters = None
916+
parameters = kwds.get('parameters', None)
920917
# Check of restrictions:
921918
if self._restrictions:
922919
substitutions = dict(zip(self._xx, coordinates))
@@ -2556,14 +2553,8 @@ def valid_coordinates(self, *coordinates, **kwds):
25562553
n = len(coordinates)
25572554
if n != self._manifold._dim:
25582555
return False
2559-
if 'tolerance' in kwds:
2560-
tolerance = kwds['tolerance']
2561-
else:
2562-
tolerance = 0
2563-
if 'parameters' in kwds:
2564-
parameters = kwds['parameters']
2565-
else:
2566-
parameters = None
2556+
tolerance = kwds.get('tolerance', 0)
2557+
parameters = kwds.get('parameters', None)
25672558
# Check of the coordinate ranges:
25682559
for x, bounds in zip(coordinates, self._bounds):
25692560
xmin = bounds[0][0] - tolerance

src/sage/plot/plot3d/list_plot3d.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,8 +618,8 @@ def g(x, y):
618618
return G
619619

620620
if interpolation_type == 'spline':
621-
kx = kwds['kx'] if 'kx' in kwds else 3
622-
ky = kwds['ky'] if 'ky' in kwds else 3
621+
kx = kwds.get('kx', 3)
622+
ky = kwds.get('ky', 3)
623623
if 'degree' in kwds:
624624
kx = kwds['degree']
625625
ky = kwds['degree']

0 commit comments

Comments
 (0)