Skip to content

Commit

Permalink
some details in cython/core as suggested by cython-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Aug 2, 2023
1 parent f55b620 commit fcdf09c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 57 deletions.
4 changes: 2 additions & 2 deletions cython/SnapPy.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ELIF REAL_TYPE == 'double':
double default_vertex_epsilon
double det_error_epsilon
cdef real_to_string(Real x):
return '%.18f'%x
return '%.18f' % x

# SnapPea declarations

Expand Down Expand Up @@ -746,7 +746,7 @@ cdef extern from "addl_code.h":
extern void free_gluing_equations(int** equations, int num_rows)
extern int* get_cusp_equation(c_Triangulation* manifold, int cusp_num, int m, int l, int* num_rows)
extern void free_cusp_equation(int* equation)
extern c_Triangulation* triangulate_link_complement_from_file(char* file_name, char *path) except *
extern c_Triangulation* triangulate_link_complement_from_file(char* file_name, char *path) except *
extern c_Triangulation* fibered_manifold_associated_to_braid(int numStrands, int braidLength, int* word) except *
extern void set_tet_shapes(c_Triangulation* manifold, Complex* filled_shapes, Complex* complete_shapes)
extern void set_target_holonomy(c_Triangulation* manifold, int theCuspIndex, Complex theTarget, int theRecomputeFlag)
Expand Down
7 changes: 4 additions & 3 deletions cython/core/abelian_group.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ cdef class AbelianGroup():
def __repr__(self):
if len(self.divisors) == 0:
return '0'
factors = ( ['Z/%d'%n for n in self.divisors if n != 0] +
['Z' for n in self.divisors if n == 0] )
factors = (['Z/%d' % n for n in self.divisors if n] +
['Z' for n in self.divisors if not n])
return ' + '.join(factors)

def __len__(self):
Expand Down Expand Up @@ -231,7 +231,8 @@ cdef class PresentationMatrix():
"""
cdef temp, m, i, j, k, l
while len(self._units) > 0:
for i,j in self._units: break
for i, j in self._units:
break
col_support = [k for k in self._col_support[j] if k != i] + [i]
row_entries = [(l, self._entries.get((i,l), 0))
for l in self._row_support[i]]
Expand Down
6 changes: 3 additions & 3 deletions cython/core/basic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def to_byte_str(s):


# Types of covering spaces
cover_types = {1:"irregular", 2:"regular", 3:"cyclic"}
cover_types = {1: "irregular", 2: "regular", 3: "cyclic"}

# Paths
manifold_path = manifold_paths[0] + os.sep
Expand Down Expand Up @@ -179,7 +179,7 @@ cdef public void uFatalError(const_char_ptr function,
# uFatalError which is usually the root cause of the problem.
if not PyErr_Occurred():
raise SnapPeaFatalError('SnapPea crashed in function %s(), '
'defined in %s.c.'%(function, file))
'defined in %s.c.' % (function, file))

# Global variables used for interrupt processing
cdef public Boolean gLongComputationInProgress
Expand Down Expand Up @@ -257,7 +257,7 @@ cdef public int uQuery(const_char_ptr message,
# from the user, but for now it is set up to return the default
# response, to facilitate batch computations.
cdef char *default = <char *> responses[<int> default_response]
sys.stderr.write('Q: %s\nA: %s\n'%(<char *> message, default))
sys.stderr.write('Q: %s\nA: %s\n' % (<char *> message, default))
return <int> default_response


Expand Down
15 changes: 8 additions & 7 deletions cython/core/dirichlet.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cdef (WEPolyhedron*, int) get_generators_from_bytes(
'"% Generators"')
nums = []
for line in data[1:]:
nums += line.split()
nums += line.split()
num_gens = int(nums[0])
nums.pop(0)

Expand All @@ -26,7 +26,7 @@ cdef (WEPolyhedron*, int) get_generators_from_bytes(
for j in range(4):
for k in range(4):
num_string = nums.pop(0) # save a reference
generators[i][j][k] = <Real_struct>Real_from_string(
generators[i][j][k] = <Real_struct>Real_from_string(
<char*>num_string)
elif len(nums) == 8*num_gens:
temp_gens = <MoebiusTransformation *>malloc(
Expand Down Expand Up @@ -171,7 +171,7 @@ cdef class CDirichletDomain():
free_Dirichlet_domain(self.c_dirichlet_domain)

def __repr__(self):
return '%d finite vertices, %d ideal vertices; %d edges; %d faces'%(
return '%d finite vertices, %d ideal vertices; %d edges; %d faces' % (
self.num_finite_vertices(),
self.num_ideal_vertices(),
self.num_edges(),
Expand Down Expand Up @@ -500,7 +500,7 @@ cdef class CDirichletDomain():
def view(self):
if PolyhedronViewer:
return ViewerWindow(PolyhedronViewer, facedicts=self.face_list(),
title='Dirichlet Domain of %s'%self.manifold_name)
title='Dirichlet Domain of %s' % self.manifold_name)
else:
raise RuntimeError('The PolyhedronViewer class '
'was not imported.')
Expand Down Expand Up @@ -629,10 +629,10 @@ cdef class CDirichletDomain():

def _to_string(self):
matrices = self.pairing_matrices()
result = '%% Generators\n%s\n'%len(matrices)
result = '%% Generators\n%s\n' % len(matrices)
for matrix in matrices:
for row in matrix:
result += ' %s\n'%' '.join([str(x) for x in row])
result += ' %s\n' % ' '.join(str(x) for x in row)
result += '\n'
return result

Expand Down Expand Up @@ -694,7 +694,8 @@ cdef class CDirichletDomain():
output = stl(self.face_list(), model, cutout, num_subdivisions, shrink_factor, cutoff_radius)
with open(filename, 'w') as output_file:
for line in output:
if UI_callback is not None: UI_callback()
if UI_callback is not None:
UI_callback()
output_file.write(line)


Expand Down
2 changes: 1 addition & 1 deletion cython/core/fundamental_group.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ cdef class CFundamentalGroup():
free_group_presentation(self.c_group_presentation)

def __repr__(self):
return 'Generators:\n %s\nRelators:\n %s'%(
return 'Generators:\n %s\nRelators:\n %s' % (
','.join(self.generators()),
'\n '.join(self.relators()))

Expand Down
60 changes: 32 additions & 28 deletions cython/core/manifold.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ cdef class Manifold(Triangulation):
cdef result
cdef Triangulation new_tri

if self.c_triangulation is NULL: return ""
if self.c_triangulation is NULL:
return ""

copy_triangulation(self.c_triangulation,
&c_retriangulated_triangulation)
Expand Down Expand Up @@ -649,10 +650,11 @@ cdef class Manifold(Triangulation):
"""
cdef int acc
cdef solution_type
if self.c_triangulation is NULL: return 0
if self.c_triangulation is NULL:
return 0
solution_type = self.solution_type()
if solution_type in ('not attempted', 'no solution found'):
raise ValueError('Solution type is: %s'%solution_type)
raise ValueError('Solution type is: %s' % solution_type)
IF HIGH_PRECISION is True:
# must provide a start value to get the correct precision
result = sum(
Expand Down Expand Up @@ -760,7 +762,8 @@ cdef class Manifold(Triangulation):
cdef Real CS
cdef int accuracy

if self.c_triangulation is NULL: return 0
if self.c_triangulation is NULL:
return 0
get_CS_value(self.c_triangulation,
&is_known,
&CS,
Expand Down Expand Up @@ -888,7 +891,8 @@ cdef class Manifold(Triangulation):
cdef int acc_rec_re, acc_rec_im, acc_log_re, acc_log_im
cdef Boolean is_geometric

if self.c_triangulation is NULL: return []
if self.c_triangulation is NULL:
return []

# The SnapPea kernel itself supports non-orientable manifolds,
# but the extensions in snap and verify don't.
Expand Down Expand Up @@ -1173,9 +1177,8 @@ cdef class Manifold(Triangulation):
if data_spec is None:
return ListOnePerLine([self.cusp_info(i)
for i in range(self.num_cusps())])
# Need to check that data_spec has string type or unicode type
# for backwards compatibility with python 2.7.
if type(data_spec) == type('') or type(data_spec) == type(u''):
# Need to check that data_spec has string type
if isinstance(data_spec, str):
return [c[data_spec] for c in self.cusp_info()]
cusp_index = valid_index(
data_spec, self.num_cusps(),
Expand Down Expand Up @@ -1204,8 +1207,8 @@ cdef class Manifold(Triangulation):
'shape': self._number_(shape),
'shape_accuracy': current_shape_accuracy,
'modulus': self._number_(modulus),
'holonomies':(self._number_(meridian), self._number_(longitude)),
'holonomy_accuracy':min(meridian_accuracy,longitude_accuracy)
'holonomies': (self._number_(meridian), self._number_(longitude)),
'holonomy_accuracy': min(meridian_accuracy,longitude_accuracy)
}

core_geodesic(self.c_triangulation, cusp_index,
Expand All @@ -1215,8 +1218,8 @@ cdef class Manifold(Triangulation):
core_length = Complex2Number(c_core_length)
core_length.accuracy = accuracy
info.update({
'core_length' : self._number_(core_length),
'singularity_index':singularity_index
'core_length': self._number_(core_length),
'singularity_index': singularity_index
})

return CuspInfo(**info)
Expand Down Expand Up @@ -1498,7 +1501,7 @@ cdef class Manifold(Triangulation):
max_segments = which_curve.max_segments
which_curve = which_curve.index

new_name = to_byte_str(self.name() + '-%d'%which_curve)
new_name = to_byte_str(self.name() + '-%d' % which_curve)
c_new_name = new_name

dual_curves(self.c_triangulation,
Expand Down Expand Up @@ -1627,7 +1630,8 @@ cdef class Manifold(Triangulation):
cdef long int p, q
cdef c_Triangulation *c_canonized_triangulation

if self.c_triangulation is NULL: return False
if self.c_triangulation is NULL:
return False

copy_triangulation(self.c_triangulation, &c_canonized_triangulation)
proto_canonize(c_canonized_triangulation)
Expand Down Expand Up @@ -1666,18 +1670,18 @@ cdef class Manifold(Triangulation):
&perm0, &perm1, &perm2, &perm3)
ans.append(
{'index':i,
'generators':(face0_gen, face1_gen, face2_gen, face3_gen),
'neighbors':(neighbor0_idx, neighbor1_idx,
neighbor2_idx, neighbor3_idx),
'gluings': ( tuple([ perm0>>(2 * i) & 3 for i in range(4)]),
tuple([ perm1>>(2 * i) & 3 for i in range(4)]),
tuple([ perm2>>(2 * i) & 3 for i in range(4)]),
tuple([ perm3>>(2 * i) & 3 for i in range(4)])),
'corners': ( self._number_(Complex2Number(c0)),
self._number_(Complex2Number(c1)),
self._number_(Complex2Number(c2)),
self._number_(Complex2Number(c3)) ),
'generator_path':generator_path}
'generators': (face0_gen, face1_gen, face2_gen, face3_gen),
'neighbors': (neighbor0_idx, neighbor1_idx,
neighbor2_idx, neighbor3_idx),
'gluings': (tuple([ perm0>>(2 * i) & 3 for i in range(4)]),
tuple([ perm1>>(2 * i) & 3 for i in range(4)]),
tuple([ perm2>>(2 * i) & 3 for i in range(4)]),
tuple([ perm3>>(2 * i) & 3 for i in range(4)])),
'corners': (self._number_(Complex2Number(c0)),
self._number_(Complex2Number(c1)),
self._number_(Complex2Number(c2)),
self._number_(Complex2Number(c3)) ),
'generator_path': generator_path}
)
return ans

Expand Down Expand Up @@ -1754,7 +1758,7 @@ cdef class Manifold(Triangulation):
cdef NormalSurfaceList *surfaces
cdef c_FuncResult result
cdef int num_surfaces
cdef c_Triangulation *pieces[2]
cdef c_Triangulation *pieces[2]
cdef Manifold M0, M1

if self.c_triangulation is NULL:
Expand All @@ -1771,7 +1775,7 @@ cdef class Manifold(Triangulation):
if not (0 <= which_surface < num_surfaces):
raise ValueError('SnapPea only found %d surfaces, but you asked for surface number %s.' % (num_surfaces, which_surface))

result = split_along_normal_surface(surfaces, which_surface, pieces)
result = split_along_normal_surface(surfaces, which_surface, pieces)
if result != func_OK:
raise RuntimeError('SnapPea kernel failed when splitting open along the given surface.')

Expand Down
30 changes: 17 additions & 13 deletions cython/core/triangulation.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ cdef class Triangulation():
knot = spherogram.DTcodec(DT)
c_triangulation = get_triangulation_from_PythonKLP(
knot.KLPProjection(), remove_finite_vertices)
name = to_byte_str('%d'%crossings + alternation + '%d'%index)
name = to_byte_str('%d' % crossings + alternation + '%d' % index)
set_triangulation_name(c_triangulation, name)
self._set_DTcode(knot)
self.set_c_triangulation(c_triangulation)
Expand Down Expand Up @@ -432,9 +432,11 @@ cdef class Triangulation():
False
"""
orientability = Orientability[get_orientability(self.c_triangulation)]
if orientability == 'orientable': return True
elif orientability == 'nonorientable': return False
else: return None
if orientability == 'orientable':
return True
if orientability == 'nonorientable':
return False
return None

def copy(self):
"""
Expand Down Expand Up @@ -685,7 +687,7 @@ cdef class Triangulation():
_to_string.
"""
cdef c_Triangulation* c_triangulation = NULL
if not self.c_triangulation is NULL:
if self.c_triangulation is not NULL:
raise ValueError('The Triangulation must be empty.')
b_string = to_byte_str(string)
c_triangulation = read_triangulation_from_string(b_string)
Expand Down Expand Up @@ -719,7 +721,7 @@ cdef class Triangulation():
if c_terse.glues_to_old_tet[n]:
byte |= 1 << bit
bit += 1
if bit%8 == 0:
if bit % 8 == 0:
byteseq.append(byte)
byte, bit = 0, 0
if bit:
Expand All @@ -745,7 +747,7 @@ cdef class Triangulation():
True
"""
cdef c_Triangulation* c_triangulation = NULL
if not self.c_triangulation is NULL:
if self.c_triangulation is not NULL:
raise ValueError('The Triangulation must be empty.')
c_triangulation = triangulation_from_bytes(bytestring)
self.set_c_triangulation(c_triangulation)
Expand All @@ -754,7 +756,7 @@ cdef class Triangulation():
"""
"""
cdef c_Triangulation* c_triangulation = NULL
if not self.c_triangulation is NULL:
if self.c_triangulation is not NULL:
raise ValueError('The Triangulation must be empty.')
c_triangulation = unpickle_triangulation(bytestring)
self.set_c_triangulation(c_triangulation)
Expand Down Expand Up @@ -804,7 +806,7 @@ cdef class Triangulation():
True
"""
cdef c_Triangulation* c_triangulation = NULL
if not self.c_triangulation is NULL:
if self.c_triangulation is not NULL:
raise ValueError('The Triangulation must be empty.')
c_triangulation = listlike_to_triangulation(tetrahedra_data,
num_or_cusps,
Expand All @@ -831,7 +833,7 @@ cdef class Triangulation():
triangulation_isosig.
"""

if not self.c_triangulation is NULL:
if self.c_triangulation is not NULL:
raise ValueError('The Triangulation must be empty.')

match = is_decorated_isosig.match(isosig)
Expand Down Expand Up @@ -1051,7 +1053,8 @@ cdef class Triangulation():
>>> M.name()
'4_1'
"""
if self.c_triangulation is NULL: return
if self.c_triangulation is NULL:
return
return to_str(get_triangulation_name(self.c_triangulation))

def set_name(self, new_name):
Expand Down Expand Up @@ -1117,7 +1120,8 @@ cdef class Triangulation():
>>> M.num_tetrahedra()
2
"""
if self.c_triangulation is NULL: return 0
if self.c_triangulation is NULL:
return 0
return get_num_tetrahedra(self.c_triangulation)

def dehn_fill(self, filling_data, which_cusp=None):
Expand Down Expand Up @@ -1226,7 +1230,7 @@ cdef class Triangulation():
if data_spec is None:
return ListOnePerLine([self.cusp_info(i)
for i in range(self.num_cusps())])
if type(data_spec) == type(''):
if isinstance(data_spec, str):
return [c[data_spec] for c in self.cusp_info()]
cusp_index = valid_index(
data_spec, self.num_cusps(),
Expand Down

0 comments on commit fcdf09c

Please sign in to comment.