@@ -2164,14 +2164,14 @@ def xkcd(n=""):
2164
2164
# default to last comic
2165
2165
url = "https://xkcd.com/info.0.json"
2166
2166
else :
2167
- url = "https://xkcd.com/{}/info.0.json" . format ( n )
2167
+ url = f "https://xkcd.com/{ n } /info.0.json"
2168
2168
2169
2169
try :
2170
2170
with contextlib .closing (urlopen (url , context = default_context ())) as f :
2171
2171
data = f .read ()
2172
2172
except HTTPError as error :
2173
2173
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 } " )
2175
2175
except URLError :
2176
2176
pass
2177
2177
@@ -2183,8 +2183,8 @@ def xkcd(n=""):
2183
2183
img = data ['img' ]
2184
2184
alt = data ['alt' ]
2185
2185
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 } ">'
2188
2188
+ '<div>Source: <a href="{0}" target="_blank">{0}</a></div>' .format (link ))
2189
2189
2190
2190
# 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):
3685
3685
3686
3686
A polynomial example::
3687
3687
3688
- sage: x= polygen(QQ)
3688
+ sage: x = polygen(QQ)
3689
3689
sage: mods = [x,x^2+1,2*x-3]
3690
3690
sage: b = CRT_basis(mods)
3691
3691
sage: b
@@ -3723,8 +3723,7 @@ def CRT_basis(moduli, *, require_coprime_moduli=True):
3723
3723
partial_prod_table = [1 ]
3724
3724
for i in range (1 , n ):
3725
3725
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 ))
3728
3727
# also return a boolean flag to report that the moduli are not coprime
3729
3728
return [cs , False ]
3730
3729
@@ -4038,7 +4037,7 @@ def binomial(x, m, **kwds):
4038
4037
pass
4039
4038
else :
4040
4039
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 } " )
4042
4041
return P (x .binomial (m , ** kwds ))
4043
4042
4044
4043
# case 3: rational, real numbers, complex numbers -> use pari
@@ -4119,7 +4118,7 @@ def multinomial(*ks):
4119
4118
return c
4120
4119
4121
4120
4122
- def binomial_coefficients (n ):
4121
+ def binomial_coefficients (n ) -> dict :
4123
4122
r"""
4124
4123
Return a dictionary containing pairs
4125
4124
`\{(k_1,k_2) : C_{k,n}\}` where `C_{k_n}` are
@@ -4619,7 +4618,7 @@ def quadratic_residues(n):
4619
4618
[0, 1, 3, 4, 5, 9]
4620
4619
"""
4621
4620
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 )} )
4623
4622
4624
4623
4625
4624
class Moebius :
@@ -5076,14 +5075,14 @@ def hilbert_conductor(a, b):
5076
5075
- Gonzalo Tornaria (2009-03-02)
5077
5076
"""
5078
5077
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 ())
5081
5080
if hilbert_symbol (a , b , p ) == - 1 )
5082
5081
5083
5082
5084
5083
def hilbert_conductor_inverse (d ):
5085
5084
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``.
5087
5086
5088
5087
The quaternion algebra `(a,b)` over `\QQ` will then have (reduced)
5089
5088
discriminant `d`.
@@ -5123,7 +5122,7 @@ def hilbert_conductor_inverse(d):
5123
5122
sage: for i in range(100): # needs sage.libs.pari
5124
5123
....: d = ZZ.random_element(2**32).squarefree_part()
5125
5124
....: 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}" )
5127
5126
5128
5127
Tests with numpy and gmpy2 numbers::
5129
5128
@@ -5891,15 +5890,15 @@ def sum_of_k_squares(k, n):
5891
5890
x , r = n .sqrtrem ()
5892
5891
if not r :
5893
5892
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" )
5895
5894
if k == 0 :
5896
5895
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" )
5900
5899
5901
5900
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" )
5903
5902
5904
5903
# Recursively subtract the largest square
5905
5904
t : list [int ] = []
0 commit comments