diff --git a/cid/test/python/test_isolated_parameters.py b/cid/test/python/test_isolated_parameters.py new file mode 100644 index 00000000..958b2097 --- /dev/null +++ b/cid/test/python/test_isolated_parameters.py @@ -0,0 +1,12 @@ +from cid.utils import IsolatedParameters, get_parameters, set_parameters + +def test_isolated_parameter_context(): + """ make sure the isolated_parameter works + """ + set_parameters({'param': 'a'}) + + with IsolatedParameters(): + 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 f5303d84..43ca3d99 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,15 @@ def ago(time): return 'just now' +class IsolatedParameters: + """A context manager to run something in isolated set of parameters""" + 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