From f0000b09a6cb32fbd6cc6ac95f81eed1d50edea7 Mon Sep 17 00:00:00 2001 From: lambert_bruyas Date: Wed, 2 Nov 2022 21:02:07 +0100 Subject: [PATCH 1/4] Overwrite environment deepcopy --- formulae/environment.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/formulae/environment.py b/formulae/environment.py index f229465..190f49a 100644 --- a/formulae/environment.py +++ b/formulae/environment.py @@ -74,6 +74,9 @@ def capture(cls, env=0, reference=0): return cls([frame.f_locals, frame.f_globals]) finally: del frame + + def __deepcopy__(self, memo): + pass def _namespace_ids(self): return [id(n) for n in self._namespaces] From f46b57a274fae6c75f86c960ff15a66ddeb75aac Mon Sep 17 00:00:00 2001 From: lambert_bruyas Date: Fri, 4 Nov 2022 10:38:00 +0100 Subject: [PATCH 2/4] ADD : test for Environment deepcopy --- formulae/environment.py | 2 +- formulae/tests/test_environment.py | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/formulae/environment.py b/formulae/environment.py index 190f49a..fc578fa 100644 --- a/formulae/environment.py +++ b/formulae/environment.py @@ -76,7 +76,7 @@ def capture(cls, env=0, reference=0): del frame def __deepcopy__(self, memo): - pass + return Environment([]) def _namespace_ids(self): return [id(n) for n in self._namespaces] diff --git a/formulae/tests/test_environment.py b/formulae/tests/test_environment.py index 61b53ba..fca2680 100644 --- a/formulae/tests/test_environment.py +++ b/formulae/tests/test_environment.py @@ -1,6 +1,6 @@ import pytest from formulae.environment import VarLookupDict, Environment - +from copy import deepcopy def test_varlookup_get(): lookup = VarLookupDict([{"a": 1, "b": 2}, {"a": 2, "c": 3}]) @@ -85,3 +85,9 @@ def test_evalenv_equality(): env_outer = Environment.capture(1) assert env != env_outer + +def test_evalenv_deepcopy(): + a = 1 + env = Environment.capture() + clone_env = deepcopy(env) + assert isinstance(clone_env, Environment) From 667d9a2f74b944441440cee1f09c337b859f46d9 Mon Sep 17 00:00:00 2001 From: lambert_bruyas Date: Fri, 4 Nov 2022 17:36:02 +0100 Subject: [PATCH 3/4] Deepcopy environment with dill --- formulae/environment.py | 4 ++-- formulae/tests/test_environment.py | 2 +- requirements.txt | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/formulae/environment.py b/formulae/environment.py index fc578fa..bf38d7f 100644 --- a/formulae/environment.py +++ b/formulae/environment.py @@ -2,7 +2,7 @@ # For more info see: https://github.com/pydata/patsy/blob/master/patsy/eval.py import inspect import numbers - +import dill class VarLookupDict: def __init__(self, dicts): @@ -76,7 +76,7 @@ def capture(cls, env=0, reference=0): del frame def __deepcopy__(self, memo): - return Environment([]) + return dill.copy(self) def _namespace_ids(self): return [id(n) for n in self._namespaces] diff --git a/formulae/tests/test_environment.py b/formulae/tests/test_environment.py index fca2680..d77355b 100644 --- a/formulae/tests/test_environment.py +++ b/formulae/tests/test_environment.py @@ -90,4 +90,4 @@ def test_evalenv_deepcopy(): a = 1 env = Environment.capture() clone_env = deepcopy(env) - assert isinstance(clone_env, Environment) + assert clone_env.namespace["a"] == a diff --git a/requirements.txt b/requirements.txt index 93c263f..a9172bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ numpy>=1.16 pandas>=1.0.0 scipy>=1.5.4 +dill>=0.3.4 \ No newline at end of file From 716f06cca8deb7e65292f44777ef3ba5ad2e1713 Mon Sep 17 00:00:00 2001 From: lambert_bruyas Date: Fri, 4 Nov 2022 21:26:21 +0100 Subject: [PATCH 4/4] apply black (fix linting) --- formulae/environment.py | 3 ++- formulae/tests/test_environment.py | 4 +++- setup.py | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/formulae/environment.py b/formulae/environment.py index bf38d7f..4111d19 100644 --- a/formulae/environment.py +++ b/formulae/environment.py @@ -4,6 +4,7 @@ import numbers import dill + class VarLookupDict: def __init__(self, dicts): self._dicts = [{}] + list(dicts) @@ -74,7 +75,7 @@ def capture(cls, env=0, reference=0): return cls([frame.f_locals, frame.f_globals]) finally: del frame - + def __deepcopy__(self, memo): return dill.copy(self) diff --git a/formulae/tests/test_environment.py b/formulae/tests/test_environment.py index d77355b..5480728 100644 --- a/formulae/tests/test_environment.py +++ b/formulae/tests/test_environment.py @@ -2,6 +2,7 @@ from formulae.environment import VarLookupDict, Environment from copy import deepcopy + def test_varlookup_get(): lookup = VarLookupDict([{"a": 1, "b": 2}, {"a": 2, "c": 3}]) assert lookup["a"] == 1 @@ -85,7 +86,8 @@ def test_evalenv_equality(): env_outer = Environment.capture(1) assert env != env_outer - + + def test_evalenv_deepcopy(): a = 1 env = Environment.capture() diff --git a/setup.py b/setup.py index 1150d43..970409b 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ def get_requirements(): def get_version(): with open(VERSION_FILE, encoding="utf-8") as buff: - exec(buff.read()) # pylint: disable=exec-used + exec(buff.read()) # pylint: disable=exec-used return vars()["__version__"]