Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
edf6730c
Commit
edf6730c
authored
Dec 08, 2015
by
Sarina Canelake
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Pylint fixes to symmath module
parent
8caad151
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
44 deletions
+37
-44
common/lib/symmath/symmath/formula.py
+36
-43
scripts/all-tests.sh
+1
-1
No files found.
common/lib/symmath/symmath/formula.py
View file @
edf6730c
...
...
@@ -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
([
parse
PresentationMathMLS
ymbol
(
y
)
for
y
in
xml
])
return
'_'
.
join
([
parse
_presentation_s
ymbol
(
y
)
for
y
in
xml
])
elif
tag
==
'msup'
:
return
'^'
.
join
([
parse
PresentationMathMLS
ymbol
(
y
)
for
y
in
xml
])
raise
Exception
(
'[parse
PresentationMathMLS
ymbol] unknown tag
%
s'
%
tag
)
return
'^'
.
join
([
parse
_presentation_s
ymbol
(
y
)
for
y
in
xml
])
raise
Exception
(
'[parse
_presentation_s
ymbol] 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
=
parse
PresentationMathMLS
ymbol
(
xml
[
0
])
usym
=
parse
_presentation_s
ymbol
(
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
...
...
scripts/all-tests.sh
View file @
edf6730c
...
...
@@ -11,7 +11,7 @@ set -e
###############################################################################
# Violations thresholds for failing the build
export
PYLINT_THRESHOLD
=
5
555
export
PYLINT_THRESHOLD
=
5
400
export
JSHINT_THRESHOLD
=
9080
doCheckVars
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment