'local(**kwargs) -> context manager
Create a context manager for use with the ``with`` statement.
For more information, see the example below or PEP 343.
Arguments:
kwargs: Variables to be assigned in the new environment.
Returns:
ContextType manager for managing the old and new environment.
Examples:
>>> context.clear()
>>> context.timeout = 1
>>> context.timeout == 1
True
>>> print context.timeout
1.0
>>> with context.local(timeout = 2):
...     print context.timeout
...     context.timeout = 3
...     print context.timeout
2.0
3.0
>>> print context.timeout
1.0'

def local(self, function=None, **kwargs):

    class LocalContext(object, ):
        def __enter__(a):
            self._tls.push()
            self.update(**{k: v for (k, v) in kwargs.items() if (v is not None)})
            return self
        def __exit__(a, *b, **c):
            self._tls.pop()
        def __call__(self, function, *a, **kw):
            @functools.wraps(function)
            def inner(*a, **kw):
                with self:
                    return function(*a, **kw)
            return inner
    return LocalContext()
