t.p.c.ContextTracker : class documentation

Part of twisted.python.context View Source 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 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..
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.
contexts =
A 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..
def __init__(self): (source)
Undocumented
def callWithContext(self, newContext, func, *args, **kw): (source)
Call func(*args, **kw) such that the contents of newContext will be available for it to retrieve using getContext.
ParametersnewContextA dict of data to push onto the context for the duration of the call to func.
funcA callable which will be called.
*argsAny additional positional arguments to pass to func.
**kwAny additional keyword arguments to pass to func.
ReturnsWhatever is returned by func
RaisesWhatever is raised by func.
def getContext(self, key, default=None): (source)
Retrieve the value for a key from the context.
ParameterskeyThe key to look up in the context.
defaultThe value to return if key is not found in the context.
ReturnsThe value most recently remembered in the context for key.
API Documentation for Twisted, generated by pydoctor at 2014-05-12 18:27:00.