twisted.python.context.ContextTracker class documentationtwisted.python.context
(View In Hierarchy)
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 listofdicts tracking the context state.  Each
newContextTracker.callWithContextpushes a newdictonto 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 ofnewContextwill be available for it to retrieve usinggetContext. | 
| Method | getContext | Retrieve the value for a key from the context. | 
list of dicts 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 dictof data to push onto the context for the duration of 
the call tofunc. | 
| 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 | Whatever is raised by func. | |