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
c3e07660
Commit
c3e07660
authored
Oct 08, 2012
by
Victor Shnayder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
formatting cleanup in calc.py
parent
242dd4f7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
9 deletions
+19
-9
common/lib/capa/capa/calc.py
+19
-9
No files found.
common/lib/capa/capa/calc.py
View file @
c3e07660
...
...
@@ -48,7 +48,7 @@ general_whitespace = re.compile('[^\w]+')
def
check_variables
(
string
,
variables
):
'''
Confirm the only variables in string are defined.
'''Confirm the only variables in string are defined.
Pyparsing uses a left-to-right parser, which makes the more
elegant approach pretty hopeless.
...
...
@@ -56,7 +56,8 @@ def check_variables(string, variables):
achar = reduce(lambda a,b:a|b ,map(Literal,alphas)) # Any alphabetic character
undefined_variable = achar + Word(alphanums)
undefined_variable.setParseAction(lambda x:UndefinedVariable("".join(x)).raiseself())
varnames = varnames | undefined_variable'''
varnames = varnames | undefined_variable
'''
possible_variables
=
re
.
split
(
general_whitespace
,
string
)
# List of all alnums in string
bad_variables
=
list
()
for
v
in
possible_variables
:
...
...
@@ -71,7 +72,8 @@ def check_variables(string, variables):
def
evaluator
(
variables
,
functions
,
string
,
cs
=
False
):
''' Evaluate an expression. Variables are passed as a dictionary
'''
Evaluate an expression. Variables are passed as a dictionary
from string to value. Unary functions are passed as a dictionary
from string to function. Variables must be floats.
cs: Case sensitive
...
...
@@ -108,6 +110,7 @@ def evaluator(variables, functions, string, cs=False):
if
string
.
strip
()
==
""
:
return
float
(
'nan'
)
ops
=
{
"^"
:
operator
.
pow
,
"*"
:
operator
.
mul
,
"/"
:
operator
.
truediv
,
...
...
@@ -169,14 +172,19 @@ def evaluator(variables, functions, string, cs=False):
def
func_parse_action
(
x
):
return
[
all_functions
[
x
[
0
]](
x
[
1
])]
number_suffix
=
reduce
(
lambda
a
,
b
:
a
|
b
,
map
(
Literal
,
suffixes
.
keys
()),
NoMatch
())
# SI suffixes and percent
# SI suffixes and percent
number_suffix
=
reduce
(
lambda
a
,
b
:
a
|
b
,
map
(
Literal
,
suffixes
.
keys
()),
NoMatch
())
(
dot
,
minus
,
plus
,
times
,
div
,
lpar
,
rpar
,
exp
)
=
map
(
Literal
,
".-+*/()^"
)
number_part
=
Word
(
nums
)
inner_number
=
(
number_part
+
Optional
(
"."
+
number_part
))
|
(
"."
+
number_part
)
# 0.33 or 7 or .34
number
=
Optional
(
minus
|
plus
)
+
inner_number
+
\
Optional
(
CaselessLiteral
(
"E"
)
+
Optional
(
"-"
)
+
number_part
)
+
\
Optional
(
number_suffix
)
# 0.33k or -17
# 0.33 or 7 or .34
inner_number
=
(
number_part
+
Optional
(
"."
+
number_part
))
|
(
"."
+
number_part
)
# 0.33k or -17
number
=
(
Optional
(
minus
|
plus
)
+
inner_number
+
Optional
(
CaselessLiteral
(
"E"
)
+
Optional
(
"-"
)
+
number_part
)
+
Optional
(
number_suffix
))
number
=
number
.
setParseAction
(
number_parse_action
)
# Convert to number
# Predefine recursive variables
...
...
@@ -201,9 +209,11 @@ def evaluator(variables, functions, string, cs=False):
varnames
.
setParseAction
(
lambda
x
:
map
(
lambda
y
:
all_variables
[
y
],
x
))
else
:
varnames
=
NoMatch
()
# Same thing for functions.
if
len
(
all_functions
)
>
0
:
funcnames
=
sreduce
(
lambda
x
,
y
:
x
|
y
,
map
(
lambda
x
:
CasedLiteral
(
x
),
all_functions
.
keys
()))
funcnames
=
sreduce
(
lambda
x
,
y
:
x
|
y
,
map
(
lambda
x
:
CasedLiteral
(
x
),
all_functions
.
keys
()))
function
=
funcnames
+
lpar
.
suppress
()
+
expr
+
rpar
.
suppress
()
function
.
setParseAction
(
func_parse_action
)
else
:
...
...
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