@contextmanager<EOL>def patch(namespace, **values):
originals = {}<EOL>if isinstance(namespace, LazyObject):<EOL><INDENT>if namespace._wrapped is None:<EOL><INDENT>namespace._setup()<EOL><DEDENT>namespace = namespace._wrapped<EOL><DEDENT>for (name, value) in values.iteritems():<EOL><INDENT>try:<EOL><INDENT>originals[name] = getattr(namespace, name)<EOL><DEDENT>except AttributeError:<EOL><INDENT>originals[name] = NotImplemented<EOL><DEDENT>if value is NotImplemented:<EOL><INDENT>if originals[name] is not NotImplemented:<EOL><INDENT>delattr(namespace, name)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>setattr(namespace, name, value)<EOL><DEDENT><DEDENT>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>for (name, original_value) in originals.iteritems():<EOL><INDENT>if original_value is NotImplemented:<EOL><INDENT>if values[name] is not NotImplemented:<EOL><INDENT>delattr(namespace, name)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>setattr(namespace, name, original_value)<EOL><DEDENT><DEDENT><DEDENT>
Patches `namespace`.`name` with `value` for (name, value) in values