class twisted.python.context.ContextTracker: (source)
A ContextTracker
provides a way to pass arbitrary key/value data up and down a call stack without passing them as parameters to the functions on that call stack.
This can be useful when functions on the top and bottom of the call stack need to cooperate but the functions in between them do not allow passing the necessary state. For example:
from twisted.python.context import call, get def handleRequest(request): call({'request-id': request.id}, renderRequest, request.url) def renderRequest(url): renderHeader(url) renderBody(url) def renderHeader(url): return "the header" def renderBody(url): return "the body (request id=%r)" % (get("request-id"),)
This should be used sparingly, since the lack of a clear connection between the two halves can result in code which is difficult to understand and maintain.
Instance Variable | contexts | A list of dict s tracking the context state. Each new ContextTracker.callWithContext pushes a new dict onto this stack for the duration of the call, making the data available to the function called and restoring the previous data once it is complete.. |
Method | __init__ | Undocumented |
Method | callWithContext | Call func(*args, **kw) such that the contents of newContext will be available for it to retrieve using getContext . |
Method | getContext | Retrieve the value for a key from the context. |
list
of dict
s tracking the context state. Each new ContextTracker.callWithContext
pushes a new dict
onto this stack for the duration of the call, making the data available to the function called and restoring the previous data once it is complete..Call func(*args, **kw)
such that the contents of newContext
will be available for it to retrieve using getContext
.
Parameters | newContext | A dict of data to push onto the context for the duration of the call to func . |
func | A callable which will be called. | |
args | Any additional positional arguments to pass to func . | |
kw | Any additional keyword arguments to pass to func . | |
Returns | Whatever is returned by func | |
Raises | Exception | Whatever is raised by func . |
Retrieve the value for a key from the context.
Parameters | key | The key to look up in the context. |
default | The value to return if key is not found in the context. | |
Returns | The value most recently remembered in the context for key . |