Skip to content

Commit

Permalink
sagemathgh-39337: a few fixes for ruff PLC
Browse files Browse the repository at this point in the history
    
this is about using `.items()` when possible

here in various folders

### 📝 Checklist

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
    
URL: sagemath#39337
Reported by: Frédéric Chapoton
Reviewer(s): David Coudert, Martin Rubey
  • Loading branch information
Release Manager committed Jan 25, 2025
2 parents 03a8e53 + f1927a4 commit 4907e05
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 36 deletions.
11 changes: 5 additions & 6 deletions src/sage/interfaces/lie.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@
# https://www.gnu.org/licenses/
#
##########################################################################
from itertools import chain
import os

from .expect import Expect, ExpectElement, ExpectFunction, FunctionElement
from sage.interfaces.interface import AsciiArtString
Expand All @@ -293,7 +295,6 @@
from sage.misc.sage_eval import sage_eval
from sage.interfaces.tab_completion import ExtraTabCompletion
from sage.misc.instancedoc import instancedoc
import os


COMMANDS_CACHE = '%s/lie_commandlist_cache.sobj' % DOT_SAGE
Expand Down Expand Up @@ -448,9 +449,7 @@ def _read_info_files(self, use_disk_cache=True):
info.close()

# Build the list of all possible command completions
l = []
for key in commands:
l += commands[key]
l = list(chain(*commands.values()))

# Save the data
self._tab_completion_dict = commands
Expand Down Expand Up @@ -574,7 +573,7 @@ def version(self) -> str:
EXAMPLES::
sage: lie.version() # optional - lie
'2.2'
'2...'
"""
return lie_version()

Expand Down Expand Up @@ -943,7 +942,7 @@ def lie_version():
sage: from sage.interfaces.lie import lie_version
sage: lie_version() # optional - lie
'2.2'
'2...'
"""
with open(os.path.join(LIE_INFO_DIR, 'INFO.0')) as f:
lines = f.readlines()
Expand Down
3 changes: 1 addition & 2 deletions src/sage/knots/free_knotinfo_monoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ def hp_mirr(hp):
if SymmetryMutant.concordance_inverse in hp_sym.keys():
hp_sym[SymmetryMutant.concordance_inverse] = hpm

for sym_mut in hp_sym.keys():
hps = hp_sym[sym_mut]
for sym_mut, hps in hp_sym.items():
if hps.divides(hpoly):
Kgen = self((K, sym_mut))
h = hpoly // hps
Expand Down
23 changes: 12 additions & 11 deletions src/sage/knots/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,25 +1181,26 @@ def _khovanov_homology_cached(self, height, ring=ZZ):
for st in states:
i, j = st[3], st[4]
if j == height:
if (i,j) in bases:
bases[i,j].append(st)
if (i, j) in bases:
bases[i, j].append(st)
else:
bases[i,j] = [st]
bases[i, j] = [st]
complexes = {}
for (i, j) in bases:
if (i+1, j) in bases:
m = matrix(ring, len(bases[(i,j)]), len(bases[(i+1,j)]))
for (i, j), bij in bases.items():
if (i + 1, j) in bases:
m = matrix(ring, len(bij), len(bases[(i + 1, j)]))
for ii in range(m.nrows()):
V1 = bases[(i,j)][ii]
V1 = bij[ii]
for jj in range(m.ncols()):
V2 = bases[(i+1, j)][jj]
V2 = bases[(i + 1, j)][jj]
V20 = V2[0]
difs = [index for index,value in enumerate(V1[0]) if value != V20[index]]
difs = [index for index, value in enumerate(V1[0])
if value != V20[index]]
if len(difs) == 1 and not (V2[2].intersection(V1[1]) or V2[1].intersection(V1[2])):
m[ii,jj] = (-1)**sum(V2[0][x] for x in range(difs[0]+1, ncross))
m[ii, jj] = (-1)**sum(V2[0][x] for x in range(difs[0]+1, ncross))
# Here we have the matrix constructed, now we have to put it in the dictionary of complexes
else:
m = matrix(ring, len(bases[(i,j)]), 0)
m = matrix(ring, len(bij), 0)
complexes[i] = m.transpose()
if (i-1, j) not in bases:
complexes[i-1] = matrix(ring, len(bases[(i,j)]), 0)
Expand Down
8 changes: 4 additions & 4 deletions src/sage/misc/sage_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -1405,10 +1405,10 @@ def __call__(self, *args, **kwargs):
sage: sie(4)
{call: {atomic:3}({atomic:4})}
"""
args = [self._sie_builder(_) for _ in args]
for k in kwargs:
kwargs[k] = self._sie_builder(kwargs[k])
return SIE_call(self._sie_builder, self, args, kwargs)
new_args = [self._sie_builder(arg) for arg in args]
new_kwargs = {key: self._sie_builder(val)
for key, val in kwargs.items()}
return SIE_call(self._sie_builder, self, new_args, new_kwargs)

def __getitem__(self, key):
r"""
Expand Down
4 changes: 2 additions & 2 deletions src/sage/modular/modsym/relation_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ def T_relation_matrix_wtk_g0(syms, mod, field, sparse):
v[j0] += s0
else:
v[j0] = s0
for j0 in v:
entries[(row, j0)] = v[j0]
for j0, vj0 in v.items():
entries[(row, j0)] = vj0
row += 1

MAT = MatrixSpace(field, row, len(syms), sparse=True)
Expand Down
9 changes: 4 additions & 5 deletions src/sage/monoids/string_monoid_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,11 @@ def coincidence_index(self, prec=0):
char_dict[i] = 1
nn = 0
ci_num = 0
for i in char_dict.keys():
ni = char_dict[i]
for ni in char_dict.values():
nn += ni
ci_num += ni*(ni-1)
ci_den = nn*(nn-1)
return RR(ci_num)/ci_den
ci_num += ni * (ni - 1)
ci_den = nn * (nn - 1)
return RR(ci_num) / ci_den

def character_count(self):
r"""
Expand Down
4 changes: 2 additions & 2 deletions src/sage/plot/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,8 +1022,8 @@ def __init__(self):
148
"""
# Convert the colors_dict defined above to Color instances.
for k in colors_dict:
self[k] = Color(colors_dict[k])
for k, col in colors_dict.items():
self[k] = Color(col)

def __getattr__(self, name):
"""
Expand Down
8 changes: 4 additions & 4 deletions src/sage/plot/primitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ def options(self):
A = self._allowed_options()
t = False
K = list(A) + ['xmin', 'xmax', 'ymin', 'ymax', 'axes']
for k in O.keys():
for k, Ok in O.items():
if k not in K:
do_verify = False
verbose(f"WARNING: Ignoring option '{k}'={O[k]}",
verbose(f"WARNING: Ignoring option '{k}'={Ok}",
level=0)
t = True
if t:
Expand All @@ -195,8 +195,8 @@ def options(self):

if 'hue' in O:
t = O['hue']
if not isinstance(t, (tuple,list)):
t = [t,1,1]
if not isinstance(t, (tuple, list)):
t = [t, 1, 1]
O['rgbcolor'] = hue(*t)
del O['hue']
return O
Expand Down

0 comments on commit 4907e05

Please sign in to comment.