Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add context manager for parameters #748

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading