From 93e147a152ad8329ea7a94d3dfe80712d377c549 Mon Sep 17 00:00:00 2001 From: Stan Schymanski Date: Fri, 18 Jun 2021 12:15:10 +0200 Subject: [PATCH 1/5] Make automatic variable imports in EquationWriter optional. --- essm/_generator.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/essm/_generator.py b/essm/_generator.py index 028d4bb..e06f2a3 100644 --- a/essm/_generator.py +++ b/essm/_generator.py @@ -315,8 +315,11 @@ def __str__(self): reformatted_result = _lint_content(result) return reformatted_result - def neweq(self, name, expr, doc='', parents=None, variables=None): - """Add new equation.""" + def neweq(self, name, expr, doc='', parents=None, variables=None, + autovars=1): + """Add new equation, incl. any variables. + If you wish to suppress import of external variables, + pass autovars=0.""" if parents: parents = ', '.join(parent + '.definition' for parent in parents) else: @@ -365,18 +368,21 @@ def neweq(self, name, expr, doc='', parents=None, variables=None): for match in re.finditer(_IMPORTS, str(expr)) or []: self._imports['sympy'].add(match.group()) - for arg in extract_variables(expr): - if str(arg) not in internal_variables and\ - arg in Variable.__registry__: - self._imports[Variable.__registry__[arg].__module__].add( - str(arg) - ) + if autovars == 1: # Optional inclusion of external variable imports + for arg in extract_variables(expr): + if str(arg) not in internal_variables and\ + arg in Variable.__registry__: + self._imports[Variable.__registry__[arg].__module__].add( + str(arg) + ) for match in re.finditer(_IMPORTS, str(expr)) or []: self._imports['essm'].add(match.group()) - def eq(self, eq1): - """Add pre-defined equation to writer, incl. any internal variables. + def eq(self, eq1, autovars=1): + """Add pre-defined equation to writer, incl. any variables. + If you wish to suppress import of external variables, + pass autovars=0. Example: @@ -386,7 +392,7 @@ def eq(self, eq1): from essm.equations.leaf.energy_water import eq_Pwl, eq_Cwl writer = EquationWriter(docstring="Test.", supplementary_imports={'sympy': {'exp', 'sin'}}) - writer.eq(eq_Pwl) + writer.eq(eq_Pwl, autovars=0) writer.eq(eq_Cwl) print(writer) """ @@ -407,7 +413,8 @@ def eq(self, eq1): 'latex_name': d1.get('latex_name'), 'default': d1.get('default')} for d1 in int_vars_attr] - self.neweq(name, expr, doc, parents, variables=variables) + self.neweq(name, expr, doc, parents, variables=variables, + autovars=autovars) def write(self, filename): """Serialize itself to a filename.""" From bc20168853e956ef9b075e66c5af1efc00ec0ba3 Mon Sep 17 00:00:00 2001 From: Jiri Kuncar Date: Tue, 6 Jul 2021 15:27:14 +0200 Subject: [PATCH 2/5] Update essm/_generator.py --- essm/_generator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/essm/_generator.py b/essm/_generator.py index e06f2a3..83f8db5 100644 --- a/essm/_generator.py +++ b/essm/_generator.py @@ -317,7 +317,8 @@ def __str__(self): def neweq(self, name, expr, doc='', parents=None, variables=None, autovars=1): - """Add new equation, incl. any variables. + """Add new equation, including any variables. + If you wish to suppress import of external variables, pass autovars=0.""" if parents: From a30b9d4f99ac683d810c8c26a07f0a8ede8d5a28 Mon Sep 17 00:00:00 2001 From: Stan Schymanski Date: Wed, 7 Jul 2021 09:03:33 +0200 Subject: [PATCH 3/5] Update docstrings to comply with pydocstyle --- essm/_generator.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/essm/_generator.py b/essm/_generator.py index 83f8db5..6a09349 100644 --- a/essm/_generator.py +++ b/essm/_generator.py @@ -317,10 +317,12 @@ def __str__(self): def neweq(self, name, expr, doc='', parents=None, variables=None, autovars=1): - """Add new equation, including any variables. - + """ + Add new equation, including any variables. + If you wish to suppress import of external variables, - pass autovars=0.""" + pass autovars=0. + """ if parents: parents = ', '.join(parent + '.definition' for parent in parents) else: @@ -381,7 +383,8 @@ def neweq(self, name, expr, doc='', parents=None, variables=None, self._imports['essm'].add(match.group()) def eq(self, eq1, autovars=1): - """Add pre-defined equation to writer, incl. any variables. + """ + Add pre-defined equation to writer, incl. any variables. If you wish to suppress import of external variables, pass autovars=0. From 862cdfae4bd3e4a5153696e3fb645048ecd43e11 Mon Sep 17 00:00:00 2001 From: Stan Schymanski Date: Wed, 7 Jul 2021 09:08:18 +0200 Subject: [PATCH 4/5] Add blank line in docstring --- essm/_generator.py | 1 + 1 file changed, 1 insertion(+) diff --git a/essm/_generator.py b/essm/_generator.py index 6a09349..5974d0c 100644 --- a/essm/_generator.py +++ b/essm/_generator.py @@ -385,6 +385,7 @@ def neweq(self, name, expr, doc='', parents=None, variables=None, def eq(self, eq1, autovars=1): """ Add pre-defined equation to writer, incl. any variables. + If you wish to suppress import of external variables, pass autovars=0. From 3ff84e9f00318aee1803506895d4a2af120151fd Mon Sep 17 00:00:00 2001 From: Stan Schymanski Date: Wed, 7 Jul 2021 09:18:46 +0200 Subject: [PATCH 5/5] remove whitespace in docstring --- essm/_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/essm/_generator.py b/essm/_generator.py index 5974d0c..c180855 100644 --- a/essm/_generator.py +++ b/essm/_generator.py @@ -385,7 +385,7 @@ def neweq(self, name, expr, doc='', parents=None, variables=None, def eq(self, eq1, autovars=1): """ Add pre-defined equation to writer, incl. any variables. - + If you wish to suppress import of external variables, pass autovars=0.