Path: blob/master/src/sage/structure/element_verify.py
8814 views
"""1Verify that a given file probably defines a valid element class.2"""34import os56def element_verify(file, module_element=False, ring_element=False, monoid_element=False):7try:8r = open(file).read()9except OSError, msg:10print msg11print "Invalid file!"12return False1314sagex = (file[-4:] == '.pyx')1516basefile = os.path.split(file)[1]1718def msg(s):19print "%s: %s"%(basefile, s)2021if not 'class' in r:22return2324if sagex:25if not "def __richcmp__" in r:26msg("The following method *must* be in your file.")27msg("def __richcmp__(left, right, int op)")2829for x in ['_richcmp(self, right, int op)']:30if (' ' + x) in r:31msg("The following forbidden method is in your file but must *not* be.")32msg(" " + x)3334if not '_cmp_c_impl(left,' in r:35msg("WARNING: You should define '_cmp_c_impl(left,'")36msg("And be sure to also define 'def __richcmp__(left, right, int op)'")3738if module_element:39for x in ['_add_', '_sub_', '_neg_c_impl']:40if not (('Element ' + x) in r):41msg("WARNING: You should define the cdef'd method '%s'"%x)4243if monoid_element or ring_element:44if not 'Element _mul_' in r:45msg("WARNING: You should define the cdef'd method '_mul_'")4647if ring_element:48if not 'Element _div_' in r:49msg("WARNING: You should define the cdef'd method '_div_'")5051else:52# pure python class53if not 'def __cmp__(' in r:54msg("WARNING: You should define 'def __cmp__(left, right)'")55msg("which may assume the parents of left and right are identical.")5657if module_element:58for x in ['_add_', '_sub_', '_neg_']:59if not (('def ' + x) in r):60msg("WARNING: You should define the method '%s'"%x)6162if monoid_element or ring_element:63if not 'def _mul_' in r:64msg("WARNING: You should define the method '_mul_'")6566if ring_element:67if not 'def _div_' in r:68msg("WARNING: You should define the method '_div_'")697071727374757677