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
9bc433d2
Commit
9bc433d2
authored
Oct 11, 2012
by
Victor Shnayder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Actually implement exact vs factors-don't-matter comparisons in chemcalc
parent
5f816597
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
4 deletions
+25
-4
common/lib/capa/capa/chem/chemcalc.py
+14
-4
common/lib/capa/capa/chem/tests.py
+11
-0
No files found.
common/lib/capa/capa/chem/chemcalc.py
View file @
9bc433d2
...
...
@@ -312,12 +312,14 @@ def divide_chemical_expression(s1, s2, ignore_state=False):
return
Fraction
(
treedic
[
'1 factors'
][
0
]
/
treedic
[
'2 factors'
][
0
])
def
chemical_equations_equal
(
eq1
,
eq2
,
ignoreFactor
=
Tru
e
):
def
chemical_equations_equal
(
eq1
,
eq2
,
exact
=
Fals
e
):
"""
Check whether two chemical equations are the same. If ignoreFactor is True,
then they are considered equal if they differ by a constant factor.
Check whether two chemical equations are the same.
arrows matter: ->, and <-> are different.
If exact is False, then they are considered equal if they differ by a
constant factor.
arrows matter: -> and <-> are different.
e.g.
chemical_equations_equal('H2 + O2 -> H2O2', 'O2 + H2 -> H2O2') -> True
...
...
@@ -325,6 +327,10 @@ def chemical_equations_equal(eq1, eq2, ignoreFactor=True):
chemical_equations_equal('H2 + O2 -> H2O2', 'O2 + H2 <-> H2O2') -> False
chemical_equations_equal('H2 + O2 -> H2O2', '2 H2 + 2 O2 -> 2 H2O2') -> True
chemical_equations_equal('H2 + O2 -> H2O2', '2 H2 + 2 O2 -> 2 H2O2', exact=True) -> False
If there's a syntax error, we raise pyparsing.ParseException.
"""
# for now, we do a manual parse for the arrow.
...
...
@@ -359,4 +365,8 @@ def chemical_equations_equal(eq1, eq2, ignoreFactor=True):
# factors don't match (molecule counts to add up)
return
False
if
exact
and
factor_left
!=
1
:
# want an exact match.
return
False
return
True
common/lib/capa/capa/chem/tests.py
View file @
9bc433d2
...
...
@@ -44,6 +44,17 @@ class Test_Compare_Equations(unittest.TestCase):
self
.
assertFalse
(
chemical_equations_equal
(
'H2 + O2 -> H2O2'
,
'O2 + H2 <-> 2H2O2'
))
def
test_exact_match
(
self
):
self
.
assertTrue
(
chemical_equations_equal
(
'H2 + O2 -> H2O2'
,
'2O2 + 2H2 -> 2H2O2'
))
self
.
assertFalse
(
chemical_equations_equal
(
'H2 + O2 -> H2O2'
,
'2O2 + 2H2 -> 2H2O2'
,
exact
=
True
))
# order still doesn't matter
self
.
assertTrue
(
chemical_equations_equal
(
'H2 + O2 -> H2O2'
,
'O2 + H2 -> H2O2'
,
exact
=
True
))
def
test_syntax_errors
(
self
):
self
.
assertRaises
(
ParseException
,
chemical_equations_equal
,
...
...
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