Skip to content

Commit

Permalink
Fix Reaction.check_consistent_units
Browse files Browse the repository at this point in the history
  • Loading branch information
bjodah committed Jan 24, 2018
1 parent baa11eb commit 181c41c
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion chempy/_equilibrium.py
Expand Up @@ -3,7 +3,7 @@

try:
import numpy as np
except:
except ImportError:
np = None

from .chemistry import equilibrium_quotient
Expand Down
11 changes: 8 additions & 3 deletions chempy/chemistry.py
Expand Up @@ -16,7 +16,7 @@
formula_to_latex, formula_to_unicode, formula_to_html
)

from .units import default_units, is_quantity, unit_of
from .units import default_units, is_quantity, unit_of, to_unitless
from ._util import intdiv
from .util.pyutil import deprecated, DeferredImport, ChemPyDeprecationWarning

Expand Down Expand Up @@ -515,8 +515,13 @@ def check_all_integral(self):

def check_consistent_units(self):
if is_quantity(self.param): # This will assume mass action
return unit_of(self.param, simplified=True) == unit_of(
default_units.molar**(1-self.order())/default_units.s, simplified=True)
try:
to_unitless(self.param/(
default_units.molar**(1-self.order())/default_units.s))
except Exception:
return False
else:
return True
else:
return True # the user might not be using ``chempy.units``

Expand Down
2 changes: 1 addition & 1 deletion chempy/kinetics/arrhenius.py
Expand Up @@ -190,7 +190,7 @@ def format(self, precision, tex=False):
try:
str_A, str_A_unit = format_string(self.A, precision, tex)
str_Ea, str_Ea_unit = format_string(self.Ea, precision, tex)
except:
except Exception:
str_A, str_A_unit = precision.format(self.A), '-'
str_Ea, str_Ea_unit = precision.format(self.Ea), '-'
return (str_A, str_A_unit), (str_Ea, str_Ea_unit)
Expand Down
2 changes: 1 addition & 1 deletion chempy/kinetics/eyring.py
Expand Up @@ -147,7 +147,7 @@ def format(self, precision, tex=False):
try:
str_A, str_A_unit = format_string(self.A, precision, tex)
str_Ea, str_Ea_unit = format_string(self.Ea, precision, tex)
except:
except Exception:
str_A, str_A_unit = precision.format(self.A), '-'
str_Ea, str_Ea_unit = precision.format(self.Ea), '-'
return (str_A, str_A_unit), (str_Ea, str_Ea_unit)
Expand Down
7 changes: 7 additions & 0 deletions chempy/tests/test_chemistry.py
Expand Up @@ -118,6 +118,13 @@ def test_Reaction_parsing():
r6 = Reaction.from_string('->', checks=())
assert r6.reac == {} and r6.prod == {}

r7 = Reaction.from_string('2 A -> B; 2e-3*metre**3/mol/hour', None)
assert r7.reac == {'A': 2} and r7.prod == {'B': 1}
assert r7.param == 2e-3*default_units.metre**3/default_units.mol/default_units.hour

with pytest.raises(ValueError):
Reaction.from_string('2 A -> B; 2e-3/hour', None)


@requires(parsing_library, units_library)
def test_Substance__molar_mass():
Expand Down
4 changes: 2 additions & 2 deletions chempy/units.py
Expand Up @@ -387,13 +387,13 @@ def allclose(a, b, rtol=1e-8, atol=None):
""" Analogous to ``numpy.allclose``. """
try:
d = abs(a - b)
except:
except Exception:
try:
if len(a) == len(b):
return all(allclose(_a, _b, rtol, atol) for _a, _b in zip(a, b))
else:
return False
except:
except Exception:
return False
lim = abs(a)*rtol
if atol is not None:
Expand Down
2 changes: 1 addition & 1 deletion chempy/util/_quantities.py
Expand Up @@ -47,7 +47,7 @@ def format_units_html(udict, font='%s', mult=r'⋅', paren=False):
def _patch_pow0_py35(pq):
try:
pq.metre**0
except:
except Exception:
pq.quantity.Quantity.__pow__ = pq.quantity.check_uniform(lambda self, other: np.power(self, other))


Expand Down
2 changes: 1 addition & 1 deletion chempy/util/table.py
Expand Up @@ -176,7 +176,7 @@ def _wrap(s):
k = k_fmt % to_unitless(rxn.param, kunit)
k_unit_str = (kunit.dimensionality.latex.strip('$') if tex
else kunit.dimensionality)
except:
except Exception:
k, k_unit_str = rxn.param.equation_as_string(k_fmt, tex)
else:
k_unit_str = '-'
Expand Down
2 changes: 1 addition & 1 deletion chempy/util/tests/test_graph.py
Expand Up @@ -56,7 +56,7 @@ def test_rsys2graph():
rsys2graph(rsys, os.path.join(tempdir, 'out.ps'))
try:
subprocess.call(['dot2tex', '-v'])
except:
except Exception:
pass
else:
rsys2graph(rsys, os.path.join(tempdir, 'out.tex'))
Expand Down
1 change: 1 addition & 0 deletions environment.yml
Expand Up @@ -23,3 +23,4 @@ dependencies:
- sym >=0.3.0
- notebook
- nbconvert
- graphviz
2 changes: 1 addition & 1 deletion examples/_ipynb_progressbar.py
Expand Up @@ -44,7 +44,7 @@ def log_progress(sequence, every=None, size=None, name='Items'):
size=size
)
yield record
except:
except Exception:
progress.bar_style = 'danger'
raise
else:
Expand Down

0 comments on commit 181c41c

Please sign in to comment.