Skip to content

Commit

Permalink
Trac #26989: some cleanup around if statements
Browse files Browse the repository at this point in the history
There are a few places with code like
{{{
if(something):
}}}
This is turned into more pythonic shape.

URL: https://trac.sagemath.org/26989
Reported by: chapoton
Ticket author(s): Frédéric Chapoton
Reviewer(s): Jeroen Demeyer
  • Loading branch information
Release Manager authored and vbraun committed Jan 26, 2019
2 parents 3715387 + 14b42ef commit d75becd
Show file tree
Hide file tree
Showing 26 changed files with 164 additions and 164 deletions.
12 changes: 6 additions & 6 deletions src/sage/calculus/ode.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,13 @@ class ode_solver(object):
self.params = params
self.solution = []

def __setattr__(self,name,value):
if(hasattr(self,'solution')):
object.__setattr__(self,'solution',[])
object.__setattr__(self,name,value)
def __setattr__(self, name, value):
if hasattr(self, 'solution'):
object.__setattr__(self, 'solution', [])
object.__setattr__(self, name, value)

def interpolate_solution(self,i=0):
pts = [(t,y[i]) for t,y in self.solution]
def interpolate_solution(self, i=0):
pts = [(t, y[i]) for t, y in self.solution]
return sage.calculus.interpolation.spline(pts)

def plot_solution(self, i=0, filename=None, interpolate=False, **kwds):
Expand Down
4 changes: 2 additions & 2 deletions src/sage/combinat/binary_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,11 +1314,11 @@ def _to_ordered_tree(self, bijection="left", root=None):
[[[[]], [[]]], [[]], []]
"""
close_root = False
if(root is None):
if root is None:
from sage.combinat.ordered_tree import OrderedTree
root = OrderedTree().clone()
close_root = True
if(self):
if self:
left, right = self[0], self[1]
if bijection == "left":
root = left._to_ordered_tree(bijection=bijection, root=root)
Expand Down
6 changes: 3 additions & 3 deletions src/sage/combinat/parallelogram_polyomino.py
Original file line number Diff line number Diff line change
Expand Up @@ -1020,10 +1020,10 @@ def check(self):
for i in range(len(upper_path)-1):
p_up[1-upper_path[i]] += 1
p_down[1-lower_path[i]] += 1
if(p_up[0] <= p_down[0] or p_down[1] <= p_up[1]):
if (p_up[0] <= p_down[0] or p_down[1] <= p_up[1]):
raise ValueError("the lower and upper paths are crossing")
p_up[1-upper_path[-1]] += 1
p_down[1-lower_path[-1]] += 1
p_up[1 - upper_path[-1]] += 1
p_down[1 - lower_path[-1]] += 1
if (p_up[0] != p_down[0] or p_up[1] != p_down[1]):
raise ValueError("the two paths have distinct ends")

Expand Down
4 changes: 2 additions & 2 deletions src/sage/combinat/posets/hasse_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,14 +733,14 @@ def rank_function(self):
sage: f = H.rank_function()
sage: s = dumps(H)
"""
if(self._rank is None):
if self._rank is None:
return None
return self._rank.__getitem__ # the rank function is just the getitem of the list

@lazy_attribute
def _rank(self):
r"""
Builds the rank function of the poset, if it exists, i.e.
Build the rank function of the poset, if it exists, i.e.
an array ``d`` where ``d[object] = self.rank_function()(object)``
A *rank function* of a poset `P` is a function `r`
Expand Down
31 changes: 16 additions & 15 deletions src/sage/combinat/posets/poset_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,13 +1195,14 @@ def TetrahedralPoset(n, *colors, **labels):
try:
n = Integer(n)
except TypeError:
raise TypeError("n must be an integer.")
raise TypeError("n must be an integer")
if n < 2:
raise ValueError("n must be greater than 2.")
raise ValueError("n must be greater than 2")
for c in colors:
if(c not in ('green', 'red', 'yellow', 'orange', 'silver', 'blue')):
if c not in ('green', 'red', 'yellow', 'orange', 'silver', 'blue'):
raise ValueError("Color input must be from the following: 'green', 'red', 'yellow', 'orange', 'silver', and 'blue'.")
elem=[(i,j,k) for i in range (n) for j in range (n-i) for k in range (n-i-j)]
elem = [(i, j, k) for i in range(n)
for j in range(n-i) for k in range(n-i-j)]
rels = []
elem_labels = {}
if 'labels' in labels:
Expand All @@ -1212,23 +1213,23 @@ def TetrahedralPoset(n, *colors, **labels):
labelcount += 1
for c in colors:
for (i,j,k) in elem:
if(i+j+k < n-1):
if(c=='green'):
if i+j+k < n-1:
if c == 'green':
rels.append([(i,j,k),(i+1,j,k)])
if(c=='red'):
if c == 'red':
rels.append([(i,j,k),(i,j,k+1)])
if(c=='yellow'):
if c == 'yellow':
rels.append([(i,j,k),(i,j+1,k)])
if(j<n-1 and k>0):
if(c=='orange'):
if j < n-1 and k > 0:
if c == 'orange':
rels.append([(i,j,k),(i,j+1,k-1)])
if(i<n-1 and j>0):
if(c=='silver'):
if i < n-1 and j > 0:
if c == 'silver':
rels.append([(i,j,k),(i+1,j-1,k)])
if(i<n-1 and k>0):
if(c=='blue'):
if i < n-1 and k > 0:
if c == 'blue':
rels.append([(i,j,k),(i+1,j,k-1)])
return Poset([elem,rels], elem_labels)
return Poset([elem, rels], elem_labels)

# shard intersection order
import sage.combinat.shard_order
Expand Down
61 changes: 31 additions & 30 deletions src/sage/dynamics/arithmetic_dynamics/wehlerK3.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ def random_WehlerK3Surface(PP):
Q += BR.random_element() * CR.gen(a[0]) * CR.gen(a[1]) * CR.gen(3+b[0]) * CR.gen(3+b[1])
#We can always change coordinates to make L diagonal
L = CR.gen(0) * CR.gen(3) + CR.gen(1) * CR.gen(4) + CR.gen(2) * CR.gen(5)
return(WehlerK3Surface([L,Q]))
return WehlerK3Surface([L, Q])


class WehlerK3Surface_ring(AlgebraicScheme_subscheme_product_projective):
r"""
Expand Down Expand Up @@ -458,7 +459,7 @@ def Lxa(self, a):
PSYC = PSY.coordinate_ring()
#Define projection homomorphism
p = ASC.hom([a[0],a[1],a[2]] + list(PSY.gens()), PSYC)
return((p(self.L)))
return p(self.L)

def Qxa(self, a):
r"""
Expand Down Expand Up @@ -495,7 +496,7 @@ def Qxa(self, a):
PSYC = PSY.coordinate_ring()
#Define projection homomorphism
p = ASC.hom([a[0], a[1], a[2]] + list(PSY.gens()), PSYC)
return(p(self.Q))
return p(self.Q)

def Sxa(self, a):
r"""
Expand Down Expand Up @@ -979,7 +980,7 @@ def degenerate_primes(self,check = True):
X = self.change_ring(GF(p))
if not X.is_degenerate():
bad_primes.remove(p)
return(bad_primes)
return bad_primes

def is_smooth(self):
r"""
Expand Down Expand Up @@ -1115,11 +1116,11 @@ def sigmaX(self, P, **kwds):
except (TypeError, NotImplementedError, AttributeError):
raise TypeError("%s fails to convert into the map's domain %s, but a `pushforward` method is not properly implemented"%(P, self))
pt = list(P[0]) + [0, 0, 0]
if(P[1][0] != 0):
if P[1][0] != 0:
[a,b,c] = [P[1][0]*self.Gpoly(1, 0)(*pt),\
-1*P[1][0]*self.Hpoly(1, 0, 1)(*pt) - P[1][1]*self.Gpoly(1, 0)(*pt),\
-P[1][0]*self.Hpoly(1, 0, 2)(*pt) - P[1][2]*self.Gpoly(1, 0)(*pt)]
elif(P[1][1] != 0):
elif P[1][1] != 0:
[a,b,c] = [-1*P[1][1]*self.Hpoly(1, 0, 1)(*pt)-P[1][0]*self.Gpoly(1, 1)(*pt),\
P[1][1]*self.Gpoly(1, 1)(*pt),\
-P[1][1]*self.Hpoly(1, 1, 2)(*pt)-P[1][2]*self.Gpoly(1, 1)(*pt)]
Expand Down Expand Up @@ -1357,11 +1358,11 @@ def sigmaY(self,P, **kwds):
except (TypeError, NotImplementedError, AttributeError):
raise TypeError("%s fails to convert into the map's domain %s, but a `pushforward` method is not properly implemented"%(P, self))
pt = [0, 0, 0] + list(P[1])
if(P[0][0] != 0):
if P[0][0] != 0:
[a, b, c] = [P[0][0]*self.Gpoly(0, 0)(*pt), \
-1*P[0][0]*self.Hpoly(0, 0, 1)(*pt) - P[0][1]*self.Gpoly(0, 0)(*pt), \
-P[0][0]*self.Hpoly(0, 0, 2)(*pt) - P[0][2]*self.Gpoly(0, 0)(*pt)]
elif(P[0][1] != 0):
elif P[0][1] != 0:
[a, b, c] = [-1*P[0][1]*self.Hpoly(0, 0, 1)(*pt) - P[0][0]*self.Gpoly(0, 1)(*pt),\
P[0][1]*self.Gpoly(0, 1)(*pt), \
-P[0][1]*self.Hpoly(0, 1, 2)(*pt) - P[0][2]*self.Gpoly(0, 1)(*pt)]
Expand Down Expand Up @@ -1848,7 +1849,7 @@ def canonical_height_plus(self, P, N, badprimes=None, prec=100):
h = self.lambda_plus(P, 0, N, m, n, prec)
for p in badprimes:
h += self.lambda_plus(P, p, N, m, n, prec)
return(h)
return h

def canonical_height_minus(self, P, N, badprimes=None, prec=100):
r"""
Expand Down Expand Up @@ -1912,7 +1913,7 @@ def canonical_height_minus(self, P, N, badprimes=None, prec=100):
h = self.lambda_minus(P, 0, N, m, n, prec)
for p in badprimes:
h += self.lambda_minus(P, p, N, m, n, prec)
return(h)
return h

def canonical_height(self, P, N, badprimes=None, prec=100):
r"""
Expand Down Expand Up @@ -1964,8 +1965,8 @@ def canonical_height(self, P, N, badprimes=None, prec=100):
"""
if badprimes is None:
badprimes = self.degenerate_primes()
return(self.canonical_height_plus(P, N,badprimes,prec) +
self.canonical_height_minus(P, N,badprimes,prec))
return (self.canonical_height_plus(P, N, badprimes, prec) +
self.canonical_height_minus(P, N, badprimes, prec))

def fiber(self, p, component):
r"""
Expand Down Expand Up @@ -2077,7 +2078,7 @@ def fiber(self, p, component):
Points.append([A2, One, C2] + P)
else:
return []
elif(self.Gpoly(component, 2)(P0) != 0):
elif self.Gpoly(component, 2)(P0) != 0:
T0 = (self.Hpoly(component, 0, 2)(P0)**2 - 4*self.Gpoly(component, 0)(P0)*self.Gpoly(component, 2)(P0))
T1 = (self.Hpoly(component, 1, 2)(P0)**2 - 4*self.Gpoly(component, 1)(P0)*self.Gpoly(component, 2)(P0))
if (T0.is_square() and T1.is_square()):
Expand All @@ -2099,7 +2100,7 @@ def fiber(self, p, component):
Points.append([A2, B2, One] + P)
else:
return []
elif(self.Hpoly(component, 0, 1)(P0) != 0):
elif self.Hpoly(component, 0, 1)(P0) != 0:
if component == 1:
Points.append(P+[Zero, One, Zero])
Points.append(P+[-self.Hpoly(component, 0, 1)(P0),Zero,-self.Hpoly(component, 1, 2)(P0)])
Expand All @@ -2110,14 +2111,14 @@ def fiber(self, p, component):
Points.append([-self.Hpoly(component, 0, 1)(P0),Zero,-self.Hpoly(component, 1, 2)(P0)] + P)
Points.append([One,Zero,Zero]+P)
Points.append([Zero,-self.Hpoly(component, 0, 1)(P0),-self.Hpoly(component, 0, 2)(P0)] + P)
elif(self.Hpoly(component, 0, 2)(P0) != 0):
elif self.Hpoly(component, 0, 2)(P0) != 0:
if component == 1:
Points.append(P+[Zero, Zero, One])
Points.append(P+[-self.Hpoly(component, 0, 2)(P0),-self.Hpoly(component, 1, 2)(P0), Zero])
else:
Points.append([Zero, Zero, One]+P)
Points.append([-self.Hpoly(component, 0, 2)(P0),-self.Hpoly(component, 1, 2)(P0), Zero]+ P)
elif(self.Hpoly(component, 1, 2)(P0) != 0):
elif self.Hpoly(component, 1, 2)(P0) != 0:
if component == 1:
Points.append(P + [Zero, Zero, One])
Points.append(P + [Zero, One, Zero])
Expand All @@ -2133,7 +2134,7 @@ def fiber(self, p, component):
Y = self.point(x, False)
if not Y in fiber:
fiber.append(Y)
return(fiber)
return fiber

def nth_iterate_phi(self, P, n, **kwds):
r"""
Expand Down Expand Up @@ -2191,14 +2192,14 @@ def nth_iterate_phi(self, P, n, **kwds):
raise TypeError("iterate number must be an integer")
#Since phi and psi are inverses and automorphisms
if n < 0:
return(self.nth_iterate_psi(P, abs(n), **kwds))
return self.nth_iterate_psi(P, abs(n), **kwds)
if n == 0:
return(self)
return self
else:
Q = self.phi(P, **kwds)
for i in range(2, n+1):
Q = self.phi(Q, **kwds)
return(Q)
return Q

def nth_iterate_psi(self, P, n, **kwds):
r"""
Expand Down Expand Up @@ -2245,14 +2246,14 @@ def nth_iterate_psi(self, P, n, **kwds):
raise TypeError("iterate number must be an integer")
#Since phi and psi and inverses
if n < 0:
return(self.nth_iterate_phi(P, abs(n), **kwds))
return self.nth_iterate_phi(P, abs(n), **kwds)
if n == 0:
return(self)
return self
else:
Q = self.psi(P, **kwds)
for i in range(2, n+1):
Q = self.psi(Q, **kwds)
return(Q)
return Q

def orbit_phi(self,P,N, **kwds):
r"""
Expand Down Expand Up @@ -2302,7 +2303,7 @@ def orbit_phi(self,P,N, **kwds):
if N[0] < 0 or N[1] < 0:
raise TypeError("orbit bounds must be non-negative")
if N[0] > N[1]:
return([])
return []
Q = self(copy(P))
for i in range(1, N[0] + 1):
Q = self.phi(Q, **kwds)
Expand Down Expand Up @@ -2358,7 +2359,7 @@ def orbit_psi(self, P, N, **kwds):
if N[0] < 0 or N[1] < 0:
raise TypeError("orbit bounds must be non-negative")
if N[0] > N[1]:
return([])
return []
Q = self(copy(P))
for i in range(1, N[0] + 1):
Q = self.psi(Q, **kwds)
Expand Down Expand Up @@ -2452,7 +2453,7 @@ def is_symmetric_orbit(self,orbit):
if P[0] == Q[0] or P[1] == Q[1]:
sym = True
i += 1
return(sym)
return sym


class WehlerK3Surface_field( WehlerK3Surface_ring):
Expand Down Expand Up @@ -2494,20 +2495,20 @@ def getPx2():
for i in getPx1():
for j in getPx1():
A = i + j
if(self.L(A) == 0 and self.Q(A) == 0):
if self.L(A) == 0 and self.Q(A) == 0:
Count += 1
for k in getPx2():
A = i + k
if(self.L(A) == 0 and self.Q(A) == 0):
if self.L(A) == 0 and self.Q(A) == 0:
Count += 1
B = i + Ypoint
if(self.L(B) == 0 and self.Q(B) == 0):
if self.L(B) == 0 and self.Q(B) == 0:
Count += 1
#Create all possible Px2 Values
for i in getPx2():
for j in getPx1():
A = i + j
if (self.L(A) == 0 and self.Q(A) == 0):
if self.L(A) == 0 and self.Q(A) == 0:
Count += 1
for k in getPx2():
A = i + k
Expand Down
7 changes: 3 additions & 4 deletions src/sage/graphs/generators/families.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ def KneserGraph(n,k):

return g

from sage.graphs.graph import Graph

def FurerGadget(k, prefix=None):
r"""
Expand Down Expand Up @@ -204,7 +203,6 @@ def FurerGadget(k, prefix=None):
(('Prefix', (1, 2)), ('Prefix', (2, 'a')), None)]
"""
from itertools import repeat as rep, chain, combinations
from sage.graphs.graph import DiGraph
if k <= 0:
raise ValueError("The order of the Furer gadget must be greater than zero")
G = Graph()
Expand All @@ -230,6 +228,7 @@ def FurerGadget(k, prefix=None):
partition.append(powerset)
return G, partition


def CaiFurerImmermanGraph(G, twisted=False):
r"""
Return the a Cai-Furer-Immerman graph from `G`, possibly a twisted
Expand Down Expand Up @@ -318,7 +317,6 @@ def CaiFurerImmermanGraph(G, twisted=False):
newG = Graph()
total_partition = []
edge_index = {}
ps_partition = []
for v in G:
Fk, p = FurerGadget(G.degree(v), v)
total_partition += p
Expand All @@ -340,7 +338,7 @@ def CaiFurerImmermanGraph(G, twisted=False):
isConnected = False
newG.add_edge(edge_va, edge_ua)
newG.add_edge(edge_vb, edge_ub)
if(twisted and G.is_connected()):
if twisted and G.is_connected():
s = " twisted"
else:
s = ""
Expand Down Expand Up @@ -416,6 +414,7 @@ def EgawaGraph(p, s):
g.add_edge(v,u)
return g


def HammingGraph(n, q, X=None):
r"""
Returns the Hamming graph with parameters ``n``, ``q`` over ``X``.
Expand Down

0 comments on commit d75becd

Please sign in to comment.