Skip to content
Snippets Groups Projects
Commit ae7af941 authored by Dominic Kempf's avatar Dominic Kempf
Browse files

Implement a generic context manager that globally defines some value

parent 3b85a515
No related branches found
No related tags found
No related merge requests found
......@@ -25,4 +25,6 @@ from dune.perftool.generation.loopy import (domain,
)
from dune.perftool.generation.context import (cache_context,
)
\ No newline at end of file
generic_context,
get_generic_context_value,
)
......@@ -29,3 +29,27 @@ def get_context_tags():
for items in _cache_context_stack:
result = result + items
return result
_generic_context_cache = {}
class _GenericContext(object):
def __init__(self, key, value):
self.key = key
self.value = value
assert key not in _generic_context_cache
def __enter__(self):
_generic_context_cache[self.key] = self.value
def __exit__(self, exc_type, exc_value, traceback):
del _generic_context_cache[self.key]
def generic_context(key, value):
return _GenericContext(key, value)
def get_generic_context_value(key):
return _generic_context_cache[key]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment