Framework context
FrameworkContext
¶
A context class that provides a thread level variable dictionary.
Source code in agentuniverse/base/context/framework_context.py
Python
class FrameworkContext:
"""A context class that provides a thread level variable dictionary."""
def __init__(self, context: Dict[str, Any]):
"""Save context dict and init an empty dict to save prior context.
Args:
context (`dict`):
A dict contains all kv pairs
which will be added to current context.
"""
self.context = context
self.old_state = {}
def __enter__(self):
"""Preserve the prior context and set a new one."""
for key, value in self.context.items():
if FrameworkContextManager().is_context_exist(key):
self.old_state[key] = FrameworkContextManager().get_context(key)
FrameworkContextManager().set_context(key, value)
def __exit__(self, exc_type, exc_value, traceback):
"""Clear the current context and revert to the prior context."""
for key, value in self.context.items():
if key in self.old_state:
FrameworkContextManager().set_context(key, self.old_state[key])
else:
FrameworkContextManager().del_context(key)
__enter__()
¶
Preserve the prior context and set a new one.
Source code in agentuniverse/base/context/framework_context.py
__exit__(exc_type, exc_value, traceback)
¶
Clear the current context and revert to the prior context.
Source code in agentuniverse/base/context/framework_context.py
Python
def __exit__(self, exc_type, exc_value, traceback):
"""Clear the current context and revert to the prior context."""
for key, value in self.context.items():
if key in self.old_state:
FrameworkContextManager().set_context(key, self.old_state[key])
else:
FrameworkContextManager().del_context(key)
__init__(context)
¶
Save context dict and init an empty dict to save prior context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context |
`dict`
|
A dict contains all kv pairs which will be added to current context. |
required |