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
a8bcd38b
Commit
a8bcd38b
authored
Feb 14, 2012
by
Piotr Mitros
Browse files
Options
Browse Files
Download
Plain Diff
merge
parents
d9466219
a5b9372e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
3 deletions
+17
-3
courseware/capa/calc.py
+17
-3
No files found.
courseware/capa/calc.py
View file @
a8bcd38b
import
copy
import
logging
import
math
import
operator
...
...
@@ -26,11 +27,19 @@ default_variables = {'j':numpy.complex(0,1),
'e'
:
numpy
.
complex
(
numpy
.
e
)
}
log
=
logging
.
getLogger
(
"mitx.courseware.capa"
)
def
evaluator
(
variables
,
functions
,
string
):
''' Evaluate an expression. Variables are passed as a dictionary
from string to value. Unary functions are passed as a dictionary
from string to function '''
from string to function. Variables must be floats.
TODO: Fix it so we can pass integers and complex numbers in variables dict
'''
# log.debug("variables: {0}".format(variables))
# log.debug("functions: {0}".format(functions))
# log.debug("string: {0}".format(string))
all_variables
=
copy
.
copy
(
default_variables
)
all_variables
.
update
(
variables
)
all_functions
=
copy
.
copy
(
default_functions
)
...
...
@@ -119,7 +128,10 @@ def evaluator(variables, functions, string):
# Handle variables passed in. E.g. if we have {'R':0.5}, we make the substitution.
# Special case for no variables because of how we understand PyParsing is put together
if
len
(
all_variables
)
>
0
:
varnames
=
sreduce
(
lambda
x
,
y
:
x
|
y
,
map
(
lambda
x
:
CaselessLiteral
(
x
),
all_variables
.
keys
()))
# We sort the list so that var names (like "e2") match before
# mathematical constants (like "e"). This is kind of a hack.
all_variables_keys
=
sorted
(
all_variables
.
keys
(),
key
=
len
,
reverse
=
True
)
varnames
=
sreduce
(
lambda
x
,
y
:
x
|
y
,
map
(
lambda
x
:
CaselessLiteral
(
x
),
all_variables_keys
))
varnames
.
setParseAction
(
lambda
x
:
map
(
lambda
y
:
all_variables
[
y
],
x
))
else
:
varnames
=
NoMatch
()
...
...
@@ -147,7 +159,9 @@ if __name__=='__main__':
print
"X"
,
evaluator
(
variables
,
functions
,
"10000||sin(7+5)-6k"
)
print
"X"
,
evaluator
(
variables
,
functions
,
"13"
)
print
evaluator
({
'R1'
:
2.0
,
'R3'
:
4.0
},
{},
"13"
)
#
print
evaluator
({
'e1'
:
1
,
'e2'
:
1.0
,
'R3'
:
7
,
'V0'
:
5
,
'R5'
:
15
,
'I1'
:
1
,
'R4'
:
6
},
{},
"e2"
)
print
evaluator
({
'a'
:
2.2997471478310274
,
'k'
:
9
,
'm'
:
8
,
'x'
:
0.66009498411213041
},
{},
"5"
)
print
evaluator
({},{},
"-1"
)
print
evaluator
({},{},
"-(7+5)"
)
...
...
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