Skip to content

Commit

Permalink
Bugfix: include expr in variable definitions when writing to file (#87)
Browse files Browse the repository at this point in the history
* correct release date for v0.4.2

* Fix equation generator and activate test.

* Fix and activate second test.

* Add get_parents to utils.

* Rename writer.eq and writer.var to neweq and newvar.

* Fix VariableWriter and add test.

* Replace value by default in VariableWriter

* Add VariableWriter.var and test

* Add EquationWriter.eq and failing test.

* Add imports of functions.

* Remove eval from utils.

* fix utils.

* Clean up test

* Replace domain by assumptions

* Fix to _generator.py

* Fix examples in _generator.

* Fix examples in _generator.

* Update jupyter examples

* update docu

* Fix spaces.

* Update jupyter examples

* Fix pydocstyle

* Add failing test of VariableWriter with expr in variable.

* Fix VariableWriter and move test to test_variable_writer.py

* Remove unused function.

* Update CHANGES.rst
  • Loading branch information
schymans authored Jun 19, 2020
1 parent 08eb362 commit 2bba27a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 56 deletions.
20 changes: 20 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
Changes
=======

``v0.4.3``
----------
*released 2020-06-18*

Bug Fixes
~~~~~~~~~
- **utils:** Include expr in variable definitions when writing to file
(`PR #87 <https://github.com/environmentalscience/essm/pull/87>`__)


Features
~~~~~~~~

- **documentation:** Add use examples as Jupyter notebooks and integrate in documentation
(`PR #83 <https://github.com/environmentalscience/essm/pull/83>`__)

- **utils:** Enable writers of .py files for re-import of variable and equation definitions
(`PR #84 <https://github.com/environmentalscience/essm/pull/84>`__)


``v0.4.2``
----------
*released 2020-04-28*
Expand Down
50 changes: 14 additions & 36 deletions essm/_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class {name}(Variable):
assumptions = {assumptions!r}
latex_name = {latex_name!r}
{default}
{expr}
"""

# CONSTANTS = re.compile(r'\b(e|pi)\b')
Expand All @@ -96,38 +97,6 @@ def _lint_content(content):
return content


def create_module(name, doc=None, folder=None, overwrite=False):
"""Create folder with init file."""
name_path = (name.replace('.', os.path.sep), ) \
if not isinstance(name, (tuple, list)) \
else name
folder = folder or pkg_resources.resource_filename('essm', '')
path = os.path.join(folder, *name_path)
try:
os.makedirs(path)
logger.info('Created new folder: {0}'.format(path))
except OSError as e1:
logger.error('Could not create new folder: {0}'.format(path))

init_path = os.path.join(path, '__init__.py')

if os.path.isfile(init_path):
logger.info(
'{0} already exists. Use `overwrite=True` to overwrite.'.
format(init_path)
)

if overwrite or not os.path.isfile(init_path):
with open(init_path, 'w') as file_out:
file_out.write(
LICENSE_TPL.format(year=datetime.datetime.now().year)
)
file_out.write('"""{0}"""\n'.format(doc))
logger.debug('Created file {0}.'.format(init_path))

return path


def extract_functions(expr):
"""Traverse through expression and return set of functions."""
return {
Expand Down Expand Up @@ -207,22 +176,30 @@ def newvar(
units=None,
assumptions={'real': True},
latex_name=None,
default=None
default=None,
expr=None
):
"""Add new variable."""
if not latex_name:
latex_name = name
if default is None:
default = ''
default = 'default = None'
else:
default = 'default = ' + str(default)
if expr is None:
expr = ''
else:
expr = 'expr = ' + str(expr)

context = {
"name": name,
"doc": doc,
"units": str(units).replace('^', '**') if units else '1/1',
"assumptions": assumptions,
"latex_name": latex_name,
"default": default
"default": default,
"expr": expr

}
self.vars.append(context)

Expand Down Expand Up @@ -252,7 +229,8 @@ def var(self, var1):
assumptions = dict_attr.get('assumptions')
latex_name = dict_attr.get('latex_name')
value = dict_attr.get('default')
self.newvar(name, doc, units, assumptions, latex_name, value)
expr = dict_attr.get('expr')
self.newvar(name, doc, units, assumptions, latex_name, value, expr)

def write(self, filename):
"""Serialize itself to a filename."""
Expand Down
20 changes: 0 additions & 20 deletions tests/test_equations.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,26 +303,6 @@ def test_solve():
) == [0.031 * demo_d - 1.68e-7 * demo_d2]


def test_variable_writer(tmpdir):
"""VariableWriter creates importable file with variable definitions."""
from essm.variables.physics.thermodynamics import c_pa
g = {}
writer_td = VariableWriter(docstring='Test of Variable_writer.')
writer_td.newvar(
'g',
'meter / second ^ 2',
'default = 9.81'
)
writer_td.var(c_pa)
eq_file = tmpdir.mkdir('test').join('test_variables.py')
writer_td.write(eq_file.strpath)
with open(eq_file, "rb") as source_file:
code = compile(eq_file.read(), eq_file, "exec")
exec(code, g)
assert g['g'].definition.default == 9.81
assert g['c_pa'].definition.unit == c_pa.definition.unit


def test_equation_writer(tmpdir):
"""EquationWriter creates importable file with internal variables."""
from sympy import var
Expand Down
25 changes: 25 additions & 0 deletions tests/test_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from essm.variables import Variable
from essm._generator import VariableWriter
from essm.variables.units import (derive_baseunit, derive_unit, joule,
kilogram, markdown, meter, second)
from essm.variables.utils import generate_metadata_table
Expand Down Expand Up @@ -179,3 +180,27 @@ def test_generate_metadata_table():
'Test expression variable.', '$2 demo_variable$', '-', 'm'),
('$E_l$', 'E_l',
'Latent heat flux from leaf.', '', '-', 'J s$^{-1}$ m$^{-2}$')]


def test_variable_writer(tmpdir):
"""VariableWriter creates importable file with variable definitions."""
from essm.variables.physics.thermodynamics import c_pa
g = {}
writer_td = VariableWriter(docstring='Test of Variable_writer.')
writer_td.newvar(
'g',
units=meter / second ** 2,
default=9.81
)
writer_td.var(c_pa)
writer_td.var(demo_variable)
writer_td.var(demo_expression_variable)
eq_file = tmpdir.mkdir('test').join('test_variables.py')
writer_td.write(eq_file.strpath)
with open(eq_file, "rb") as source_file:
code = compile(eq_file.read(), eq_file, "exec")
exec(code, g)
assert g['g'].definition.default == 9.81
assert g['c_pa'].definition.unit == c_pa.definition.unit
assert g['demo_expression_variable'].definition.expr \
== demo_expression_variable.definition.expr

0 comments on commit 2bba27a

Please sign in to comment.