Skip to content

Commit

Permalink
add context manager for parameters (#748)
Browse files Browse the repository at this point in the history
* add context manager for parameters

* add context manager for parameters
  • Loading branch information
iakov-aws authored Mar 8, 2024
1 parent 9c090d4 commit bb42e8c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cid/test/python/test_isolated_parameters.py
Original file line number Diff line number Diff line change
@@ -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'
10 changes: 10 additions & 0 deletions cid/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
import copy
import math
import inspect
import logging
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bb42e8c

Please sign in to comment.