Skip to content

Commit

Permalink
some minor changes in the code, including some about exceptions (#48)
Browse files Browse the repository at this point in the history
fchapoton authored May 10, 2023
1 parent 9351887 commit 64e9219
Showing 7 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion spherogram_src/links/invariants.py
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@
from sage.groups.free_group import FreeGroup
import sage.graphs.graph as graph
from sage.groups.braid import Braid, BraidGroup
from sage.all import QQ
from sage.rings.rational_field import QQ
from sage.rings.polynomial.laurent_polynomial_ring import LaurentPolynomialRing
from sage.quadratic_forms.quadratic_form import QuadraticForm
try:
6 changes: 3 additions & 3 deletions spherogram_src/links/jones_old.py
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ def cut(G, T, e):
Cutting T along e separates T into two components.
Returns: The list of edges in G - e connecting the two different components of T-e."""
if not T.has_edge(*e):
raise Exception("e must be an edge of T.")
raise ValueError("e must be an edge of T.")
S = T.copy()
S.delete_edge(e)
(C1, C2) = S.connected_components()
@@ -58,9 +58,9 @@ def cyc(G, T, e):
Adjoining e to T creates a cycle.
Returns: this cycle."""
if not G.has_edge(*e):
raise Exception("e must be an edge of G.")
raise ValueError("e must be an edge of G.")
if T.has_edge(*e):
raise Exception("e must not be an edge of T.")
raise ValueError("e must not be an edge of T.")
# First thing: catch exceptional case that e is a multiple for an edge in T (giving a 2-cycle).
try:
l = T.edge_label(e[0], e[1])
2 changes: 1 addition & 1 deletion spherogram_src/links/orthogonal.py
Original file line number Diff line number Diff line change
@@ -663,7 +663,7 @@ def random_link():
def check_faces(link):
faces = link.faces()
assert len(link.vertices) - len(link.edges) + len(faces) == 2
assert set(Counter(sum(faces, [])).values()) == set([1])
assert all(val == 1 for val in Counter(sum(faces, [])).values())
assert link.is_planar()

def test_face_method(N):
2 changes: 1 addition & 1 deletion spherogram_src/links/simplify.py
Original file line number Diff line number Diff line change
@@ -701,7 +701,7 @@ def backtrack(link, num_steps=10, prob_type_1=.3, prob_type_2=.3):

n = 0
if prob_type_1 + prob_type_2 > 1:
raise Exception("Probabilities add to more than 1")
raise ValueError("Probabilities add to more than 1")
p1 = prob_type_1
p2 = p1 + prob_type_2
for i in range(num_steps):
8 changes: 4 additions & 4 deletions spherogram_src/links/tangles.py
Original file line number Diff line number Diff line change
@@ -253,7 +253,7 @@ def denominator_closure(self):
if m != n:
raise ValueError("To do braid closure, both the top and bottom numbers of strands must be equal")
T = self.copy()
for i in range(0, n):
for i in range(n):
join_strands(T.adjacent[i], T.adjacent[m + i])
return Link(T.crossings, check_planarity=False)

@@ -305,7 +305,7 @@ def circular_sum(self, other, n=0):
Am, An = self.boundary
Bm, Bn = self.boundary
if (Am, An) != (Bn, Bm):
raise Exception("Tangles must have compatible boundary shapes")
raise ValueError("Tangles must have compatible boundary shapes")
return (self * (other.circular_rotate(n))).denominator_closure()

def isosig(self, root=None, over_or_under=False):
@@ -379,7 +379,7 @@ def arc_id(c, i):
a fresh one if needed."""
return arc_ids.setdefault(arc_key(c, i), len(arc_ids) + 1)
m, n = T.boundary
lower = "{" + ",".join(str(arc_id(T, i)) for i in range(0, m)) + "}"
lower = "{" + ",".join(str(arc_id(T, i)) for i in range(m)) + "}"
upper = "{" + ",".join(str(arc_id(T, i)) for i in range(m, m + n)) + "}"
parts = []
for c in T.crossings:
@@ -392,7 +392,7 @@ def arc_id(c, i):
else:
parts.append(f"P[{arcs[0]},{arcs[1]}]")
else:
raise Exception("Unexpected entity")
raise TypeError("Unexpected entity")
return f"Tangle[{lower}, {upper}{''.join(', ' + p for p in parts)}]"


2 changes: 1 addition & 1 deletion spherogram_src/links/test/__init__.py
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
from ...sage_helper import _within_sage
if _within_sage:
from sage.rings.polynomial.laurent_polynomial_ring import LaurentPolynomialRing
from sage.all import QQ
from sage.rings.rational_field import QQ


class TestLinkFunctions(unittest.TestCase):
2 changes: 1 addition & 1 deletion spherogram_src/links/torus.py
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ def torus_knot(name: str) -> Link:
p, q = map(int, name[2:-1].split(','))

if p == 0 or q == 0:
raise Exception("torus_knot(p,q) requires non zero p and q")
raise ValueError("torus_knot(p,q) requires non zero p and q")
to_mirror = p * q < 0
p, q = abs(p), abs(q)
if p == 2:

0 comments on commit 64e9219

Please sign in to comment.