Ńň
°Ic @ s d Z d d k l Z d d k l Z d d g Z e Z d d
d YZ d e f d YZ d e
f d YZ d
e
f d YZ d S( s
A dynamic-scope-like system, aka fluid variables.
The idea behind dynamic scoped variables is for when, at one level,
you want to change the behavior of something you call. Except you
can't pass in any new arguments (e.g., there's some function or object
inbetween you and the thing you want to change), or you can't predict
exactly what you will want to change.
You should use it like::
context = Context()
def do_stuff():
state = context.set(inside='do_stuff')
try:
do stuff...
finally:
state.restore()
Then ``context.inside`` will be set to ``'do_stuff'`` inside that try
block. If a value isn't set, you'll get an attribute error.
Note that all values are thread local; this means you cannot use a
context object to pass information to another thread. In a
single-thread environment it doesn't really matter.
Typically you will create ``Context`` instances for your application,
environment, etc. These should be global module-level variables, that
may be imported by any interested module; each instance is a namespace
of its own.
Sometimes it's nice to have default values, instead of getting
attribute errors. This makes it easier to put in new variables that
are intended to be used elsewhere, without having to use
``getattr(context, 'var', default)`` to avoid AttributeErrors. There
are two ways (that can be used together) to do this.
First, when instantiating a ``Context`` object, you can give it a
``default`` value. If given, then all variables will default to that
value. ``None`` is a typical value for that.
Another is ``context.set_default(**vars)``, which will set only those
variables to default values. This will not effect the stack of
scopes, but will only add defaults.
When Python 2.5 comes out, this syntax would certainly be useful::
with context(page='view'):
do stuff...
And ``page`` will be set to ``'view'`` only inside that ``with``
block.
i˙˙˙˙( t threadinglocal( t countt Contextt ContextRestoreErrort
_NoDefaultc B s e Z RS( ( t __name__t
__module__( ( ( s6 /usr/lib/python2.6/site-packages/formencode/context.pyR @ s c B s e Z d Z RS( s9
Raised when something is restored out-of-order.
( R R t __doc__( ( ( s6 /usr/lib/python2.6/site-packages/formencode/context.pyR C s c B sJ e Z e d Z d Z d Z d Z d Z d Z d Z RS( c C s$ t i | i d <| | i d