Commit edf6730c by Sarina Canelake

Pylint fixes to symmath module

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