Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/structure/element_verify.py
4045 views
1
"""
2
Verify that a given file probably defines a valid element class.
3
"""
4
5
import os
6
7
def element_verify(file, module_element=False, ring_element=False, monoid_element=False):
8
try:
9
r = open(file).read()
10
except OSError, msg:
11
print msg
12
print "Invalid file!"
13
return False
14
15
sagex = (file[-4:] == '.pyx')
16
17
basefile = os.path.split(file)[1]
18
19
def msg(s):
20
print "%s: %s"%(basefile, s)
21
22
if not 'class' in r:
23
return
24
25
if sagex:
26
if not "def __richcmp__" in r:
27
msg("The following method *must* be in your file.")
28
msg("def __richcmp__(left, right, int op)")
29
30
for x in ['_richcmp(self, right, int op)']:
31
if (' ' + x) in r:
32
msg("The following forbidden method is in your file but must *not* be.")
33
msg(" " + x)
34
35
if not '_cmp_c_impl(left,' in r:
36
msg("WARNING: You should define '_cmp_c_impl(left,'")
37
msg("And be sure to also define 'def __richcmp__(left, right, int op)'")
38
39
if module_element:
40
for x in ['_add_', '_sub_', '_neg_c_impl']:
41
if not (('Element ' + x) in r):
42
msg("WARNING: You should define the cdef'd method '%s'"%x)
43
44
if monoid_element or ring_element:
45
if not 'Element _mul_' in r:
46
msg("WARNING: You should define the cdef'd method '_mul_'")
47
48
if ring_element:
49
if not 'Element _div_' in r:
50
msg("WARNING: You should define the cdef'd method '_div_'")
51
52
else:
53
# pure python class
54
if not 'def __cmp__(' in r:
55
msg("WARNING: You should define 'def __cmp__(left, right)'")
56
msg("which may assume the parents of left and right are identical.")
57
58
if module_element:
59
for x in ['_add_', '_sub_', '_neg_']:
60
if not (('def ' + x) in r):
61
msg("WARNING: You should define the method '%s'"%x)
62
63
if monoid_element or ring_element:
64
if not 'def _mul_' in r:
65
msg("WARNING: You should define the method '_mul_'")
66
67
if ring_element:
68
if not 'def _div_' in r:
69
msg("WARNING: You should define the method '_div_'")
70
71
72
73
74
75
76
77