From 48fb2ee0fc29fa1a6df7ab35de35ce2b8c7671b8 Mon Sep 17 00:00:00 2001 From: Iakov Gan Date: Sat, 2 Mar 2024 11:09:56 +0100 Subject: [PATCH 1/2] add context manager for parameters --- cid/test/python/test_isolated_parameters.py | 10 ++++++++++ cid/utils.py | 13 +++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 cid/test/python/test_isolated_parameters.py diff --git a/cid/test/python/test_isolated_parameters.py b/cid/test/python/test_isolated_parameters.py new file mode 100644 index 00000000..56237aac --- /dev/null +++ b/cid/test/python/test_isolated_parameters.py @@ -0,0 +1,10 @@ +from cid.utils import IsolatedParameters, get_parameters, set_parameters + +def test_isolated_parameter_context(): + """ make sure the merge works with depth + """ + set_parameters({'a': 'a'}) + with IsolatedParameters(): + set_parameters({'a': 'b'}) + assert get_parameters().get('a') == 'b', 'parameters within context must be B' + assert get_parameters().get('a') == 'a', 'parameters within context must be A' \ No newline at end of file diff --git a/cid/utils.py b/cid/utils.py index f5303d84..09006161 100644 --- a/cid/utils.py +++ b/cid/utils.py @@ -1,5 +1,6 @@ import os import sys +import copy import math import inspect import logging @@ -279,6 +280,18 @@ def ago(time): return 'just now' +class IsolatedParameters: + """A context manager to run something in isolated set of parameters""" + def __init__(self): + self.backup = None + + def __enter__(self): + self.backup = copy.deepcopy(params) + + def __exit__(self, exc_type, exc_value, traceback): + global params + params = self.backup + def merge_objects(obj1, obj2, depth=2): """ merging objects with a depth From 3f3682f7902a9821cf796e9b757b223f3aa46641 Mon Sep 17 00:00:00 2001 From: Iakov Gan Date: Sat, 2 Mar 2024 11:16:57 +0100 Subject: [PATCH 2/2] add context manager for parameters --- cid/test/python/test_isolated_parameters.py | 12 +++++++----- cid/utils.py | 3 --- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cid/test/python/test_isolated_parameters.py b/cid/test/python/test_isolated_parameters.py index 56237aac..958b2097 100644 --- a/cid/test/python/test_isolated_parameters.py +++ b/cid/test/python/test_isolated_parameters.py @@ -1,10 +1,12 @@ from cid.utils import IsolatedParameters, get_parameters, set_parameters def test_isolated_parameter_context(): - """ make sure the merge works with depth + """ make sure the isolated_parameter works """ - set_parameters({'a': 'a'}) + set_parameters({'param': 'a'}) + with IsolatedParameters(): - set_parameters({'a': 'b'}) - assert get_parameters().get('a') == 'b', 'parameters within context must be B' - assert get_parameters().get('a') == 'a', 'parameters within context must be A' \ No newline at end of file + set_parameters({'param': 'b'}) + assert get_parameters().get('param') == 'b', 'parameters within context must be B' + + assert get_parameters().get('param') == 'a', 'parameters within context must be A' diff --git a/cid/utils.py b/cid/utils.py index 09006161..43ca3d99 100644 --- a/cid/utils.py +++ b/cid/utils.py @@ -282,9 +282,6 @@ def ago(time): class IsolatedParameters: """A context manager to run something in isolated set of parameters""" - def __init__(self): - self.backup = None - def __enter__(self): self.backup = copy.deepcopy(params)