Commit edf6730c by Sarina Canelake

Pylint fixes to symmath module

parent 8caad151
......@@ -78,7 +78,8 @@ def to_latex(expr):
# substitute back into latex form for scripts
# literally something of the form
# 'scriptN' becomes '\\mathcal{N}'
# note: can't use something akin to the _print_hat method above because we sometimes get 'script(N)__B' or more complicated terms
# note: can't use something akin to the _print_hat method above because we
# sometimes get 'script(N)__B' or more complicated terms
expr_s = re.sub(
r'script([a-zA-Z0-9]+)',
'\\mathcal{\\1}',
......@@ -99,11 +100,11 @@ def my_evalf(expr, chop=False):
if isinstance(expr, list):
try:
return [x.evalf(chop=chop) for x in expr]
except:
except Exception: # pylint: disable=broad-except
return expr
try:
return expr.evalf(chop=chop)
except:
except Exception: # pylint: disable=broad-except
return expr
......@@ -115,23 +116,25 @@ def my_sympify(expr, normphase=False, matrix=False, abcsym=False, do_qubit=False
if symtab:
varset = symtab
else:
varset = {'p': sympy.Symbol('p'),
'g': sympy.Symbol('g'),
'e': sympy.E, # for exp
'i': sympy.I, # lowercase i is also sqrt(-1)
'Q': sympy.Symbol('Q'), # otherwise it is a sympy "ask key"
'I': sympy.Symbol('I'), # otherwise it is sqrt(-1)
'N': sympy.Symbol('N'), # or it is some kind of sympy function
'ZZ': sympy.Symbol('ZZ'), # otherwise it is the PythonIntegerRing
'XI': sympy.Symbol('XI'), # otherwise it is the capital \XI
'hat': sympy.Function('hat'), # for unit vectors (8.02)
}
varset = {
'p': sympy.Symbol('p'),
'g': sympy.Symbol('g'),
'e': sympy.E, # for exp
'i': sympy.I, # lowercase i is also sqrt(-1)
'Q': sympy.Symbol('Q'), # otherwise it is a sympy "ask key"
'I': sympy.Symbol('I'), # otherwise it is sqrt(-1)
'N': sympy.Symbol('N'), # or it is some kind of sympy function
'ZZ': sympy.Symbol('ZZ'), # otherwise it is the PythonIntegerRing
'XI': sympy.Symbol('XI'), # otherwise it is the capital \XI
'hat': sympy.Function('hat'), # for unit vectors (8.02)
}
if do_qubit: # turn qubit(...) into Qubit instance
varset.update({'qubit': Qubit,
'Ket': Ket,
'dot': dot,
'bit': sympy.Function('bit'),
})
varset.update({
'qubit': Qubit,
'Ket': Ket,
'dot': dot,
'bit': sympy.Function('bit'),
})
if abcsym: # consider all lowercase letters as real symbols, in the parsing
for letter in string.lowercase:
if letter in varset: # exclude those already done
......@@ -207,7 +210,7 @@ class formula(object):
usym = unicode(k.text)
try:
udata = unicodedata.name(usym)
except Exception:
except Exception: # pylint: disable=broad-except
udata = None
# print "usym = %s, udata=%s" % (usym,udata)
if udata: # eg "GREEK SMALL LETTER BETA"
......@@ -271,7 +274,8 @@ class formula(object):
newk = etree.Element('mi')
newk.text = 'hat(%s)' % k[0].text
xml.replace(k, newk)
if gettag(k[0]) == 'mrow' and gettag(k[0][0]) == 'mi' and gettag(k[1]) == 'mo' and str(k[1].text) == '^':
if gettag(k[0]) == 'mrow' and gettag(k[0][0]) == 'mi' and \
gettag(k[1]) == 'mo' and str(k[1].text) == '^':
newk = etree.Element('mi')
newk.text = 'hat(%s)' % k[0][0].text
xml.replace(k, newk)
......@@ -419,7 +423,7 @@ class formula(object):
# pre-process the presentation mathml before sending it to snuggletex to convert to content mathml
try:
xml = self.preprocess_pmathml(self.expr)
except Exception, err:
except Exception as err: # pylint: disable=broad-except
log.warning('Err %s while preprocessing; expr=%s', err, self.expr)
return "<html>Error! Cannot process pmathml</html>"
pmathml = etree.tostring(xml, pretty_print=True)
......@@ -468,13 +472,6 @@ class formula(object):
def gettag(expr):
return re.sub('{http://[^}]+}', '', expr.tag)
# simple math
def op_divide(*args):
if not len(args) == 2:
raise Exception('divide given wrong number of arguments!')
# print "divide: arg0=%s, arg1=%s" % (args[0],args[1])
return sympy.Mul(args[0], sympy.Pow(args[1], -1))
def op_plus(*args):
return args[0] if len(args) == 1 else op_plus(*args[:-1]) + args[-1]
......@@ -491,7 +488,7 @@ class formula(object):
opdict = {
'plus': op_plus,
'divide': operator.div, # should this be op_divide?
'divide': operator.div,
'times': op_times,
'minus': op_minus,
'root': sympy.sqrt,
......@@ -518,12 +515,7 @@ class formula(object):
'ln': sympy.ln,
}
# simple symbols - TODO is this code used?
nums1dict = {
'pi': sympy.pi,
}
def parsePresentationMathMLSymbol(xml):
def parse_presentation_symbol(xml):
"""
Parse <msub>, <msup>, <mi>, and <mn>
"""
......@@ -533,10 +525,10 @@ class formula(object):
elif tag == 'mi':
return xml.text
elif tag == 'msub':
return '_'.join([parsePresentationMathMLSymbol(y) for y in xml])
return '_'.join([parse_presentation_symbol(y) for y in xml])
elif tag == 'msup':
return '^'.join([parsePresentationMathMLSymbol(y) for y in xml])
raise Exception('[parsePresentationMathMLSymbol] unknown tag %s' % tag)
return '^'.join([parse_presentation_symbol(y) for y in xml])
raise Exception('[parse_presentation_symbol] unknown tag %s' % tag)
# parser tree for Content MathML
tag = gettag(xml)
......@@ -574,11 +566,10 @@ class formula(object):
elif tag == 'cn': # number
return sympy.sympify(xml.text)
# return float(xml.text)
elif tag == 'ci': # variable (symbol)
if len(xml) > 0 and (gettag(xml[0]) == 'msub' or gettag(xml[0]) == 'msup'): # subscript or superscript
usym = parsePresentationMathMLSymbol(xml[0])
usym = parse_presentation_symbol(xml[0])
sym = sympy.Symbol(str(usym))
else:
usym = unicode(xml.text)
......@@ -596,7 +587,7 @@ class formula(object):
sympy = property(make_sympy, None, None, 'sympy representation')
def GetContentMathML(self, asciimath, mathml):
def GetContentMathML(self, asciimath, mathml): # pylint: disable=invalid-name
"""
Handle requests to snuggletex API to convert the Ascii math to MathML
"""
......@@ -606,7 +597,9 @@ class formula(object):
'asciiMathInput': asciimath,
'asciiMathML': mathml,
}
headers = {'User-Agent': "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13"}
headers = {
'User-Agent': "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13"
}
request = requests.post(url, data=payload, headers=headers, verify=False)
request.encoding = 'utf-8'
ret = request.text
......
......@@ -11,7 +11,7 @@ set -e
###############################################################################
# Violations thresholds for failing the build
export PYLINT_THRESHOLD=5555
export PYLINT_THRESHOLD=5400
export JSHINT_THRESHOLD=9080
doCheckVars() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment