Skip to content

Commit 4e5f1d9

Browse files
author
Release Manager
committed
gh-40849: some details in arith/misc very minor changes, suggested by ruff ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. URL: #40849 Reported by: Frédéric Chapoton Reviewer(s): David Coudert
2 parents d87f9df + b9c0005 commit 4e5f1d9

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/sage/arith/misc.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,14 +2164,14 @@ def xkcd(n=""):
21642164
# default to last comic
21652165
url = "https://xkcd.com/info.0.json"
21662166
else:
2167-
url = "https://xkcd.com/{}/info.0.json".format(n)
2167+
url = f"https://xkcd.com/{n}/info.0.json"
21682168

21692169
try:
21702170
with contextlib.closing(urlopen(url, context=default_context())) as f:
21712171
data = f.read()
21722172
except HTTPError as error:
21732173
if error.getcode() == 400: # this error occurs when asking for a non valid comic number
2174-
raise RuntimeError("could not obtain comic data from {}".format(url))
2174+
raise RuntimeError(f"could not obtain comic data from {url}")
21752175
except URLError:
21762176
pass
21772177

@@ -2183,8 +2183,8 @@ def xkcd(n=""):
21832183
img = data['img']
21842184
alt = data['alt']
21852185
title = data['safe_title']
2186-
link = "http://xkcd.com/{}".format(data['num'])
2187-
return html('<h1>{}</h1><img src="{}" title="{}">'.format(title, img, alt)
2186+
link = f"http://xkcd.com/{data['num']}"
2187+
return html(f'<h1>{title}</h1><img src="{img}" title="{alt}">'
21882188
+ '<div>Source: <a href="{0}" target="_blank">{0}</a></div>'.format(link))
21892189

21902190
# TODO: raise this error in such a way that it's not clear that
@@ -3685,7 +3685,7 @@ def CRT_basis(moduli, *, require_coprime_moduli=True):
36853685
36863686
A polynomial example::
36873687
3688-
sage: x=polygen(QQ)
3688+
sage: x = polygen(QQ)
36893689
sage: mods = [x,x^2+1,2*x-3]
36903690
sage: b = CRT_basis(mods)
36913691
sage: b
@@ -3723,8 +3723,7 @@ def CRT_basis(moduli, *, require_coprime_moduli=True):
37233723
partial_prod_table = [1]
37243724
for i in range(1, n):
37253725
partial_prod_table.append((1 - e[-i]) * partial_prod_table[-1])
3726-
for i in range(n):
3727-
cs.append(e[i] * partial_prod_table[-i - 1])
3726+
cs.extend(e[i] * partial_prod_table[-i - 1] for i in range(n))
37283727
# also return a boolean flag to report that the moduli are not coprime
37293728
return [cs, False]
37303729

@@ -4038,7 +4037,7 @@ def binomial(x, m, **kwds):
40384037
pass
40394038
else:
40404039
if c > 0 and any(c.gcd(k) > 1 for k in range(2, m + 1)):
4041-
raise ZeroDivisionError("factorial({}) not invertible in {}".format(m, P))
4040+
raise ZeroDivisionError(f"factorial({m}) not invertible in {P}")
40424041
return P(x.binomial(m, **kwds))
40434042

40444043
# case 3: rational, real numbers, complex numbers -> use pari
@@ -4119,7 +4118,7 @@ def multinomial(*ks):
41194118
return c
41204119

41214120

4122-
def binomial_coefficients(n):
4121+
def binomial_coefficients(n) -> dict:
41234122
r"""
41244123
Return a dictionary containing pairs
41254124
`\{(k_1,k_2) : C_{k,n}\}` where `C_{k_n}` are
@@ -4619,7 +4618,7 @@ def quadratic_residues(n):
46194618
[0, 1, 3, 4, 5, 9]
46204619
"""
46214620
n = abs(int(n))
4622-
return sorted(set(ZZ((a * a) % n) for a in range(n // 2 + 1)))
4621+
return sorted({ZZ((a * a) % n) for a in range(n // 2 + 1)})
46234622

46244623

46254624
class Moebius:
@@ -5076,14 +5075,14 @@ def hilbert_conductor(a, b):
50765075
- Gonzalo Tornaria (2009-03-02)
50775076
"""
50785077
a, b = ZZ(a), ZZ(b)
5079-
return ZZ.prod(p for p in set([2]).union(prime_divisors(a),
5080-
prime_divisors(b))
5078+
return ZZ.prod(p for p in {2}.union(a.prime_divisors(),
5079+
b.prime_divisors())
50815080
if hilbert_symbol(a, b, p) == -1)
50825081

50835082

50845083
def hilbert_conductor_inverse(d):
50855084
r"""
5086-
Finds a pair of integers `(a,b)` such that ``hilbert_conductor(a,b) == d``.
5085+
Find a pair of integers `(a,b)` such that ``hilbert_conductor(a,b) == d``.
50875086
50885087
The quaternion algebra `(a,b)` over `\QQ` will then have (reduced)
50895088
discriminant `d`.
@@ -5123,7 +5122,7 @@ def hilbert_conductor_inverse(d):
51235122
sage: for i in range(100): # needs sage.libs.pari
51245123
....: d = ZZ.random_element(2**32).squarefree_part()
51255124
....: if hilbert_conductor(*hilbert_conductor_inverse(d)) != d:
5126-
....: print("hilbert_conductor_inverse failed for d = {}".format(d))
5125+
....: print(f"hilbert_conductor_inverse failed for d = {d}")
51275126
51285127
Tests with numpy and gmpy2 numbers::
51295128
@@ -5891,15 +5890,15 @@ def sum_of_k_squares(k, n):
58915890
x, r = n.sqrtrem()
58925891
if not r:
58935892
return (x,)
5894-
raise ValueError("%s is not a sum of 1 square" % n)
5893+
raise ValueError(f"{n} is not a sum of 1 square")
58955894
if k == 0:
58965895
if n == 0:
5897-
return tuple()
5898-
raise ValueError("%s is not a sum of 0 squares" % n)
5899-
raise ValueError("k = %s must be nonnegative" % k)
5896+
return ()
5897+
raise ValueError(f"{n} is not a sum of 0 squares")
5898+
raise ValueError(f"k = {k} must be nonnegative")
59005899

59015900
if n < 0:
5902-
raise ValueError("%s is not a sum of %s squares" % (n, k))
5901+
raise ValueError(f"{n} is not a sum of {k} squares")
59035902

59045903
# Recursively subtract the largest square
59055904
t: list[int] = []

0 commit comments

Comments
 (0)