contextdecorator

Create APIs that work as decorators and as context managers
Download

contextdecorator Ranking & Summary

Advertisement

  • Rating:
  • License:
  • BSD License
  • Price:
  • FREE
  • Publisher Name:
  • Michael Foord
  • Publisher web site:
  • http://www.voidspace.org.uk/python/rest2web/

contextdecorator Tags


contextdecorator Description

Create APIs that work as decorators and as context managers If you're a library or framework creator then it is nice to be able to create APIs that can be used either as decorators or context managers.You just subclasss ContextDecorator and implement before and after methods. As an added piece of goodness the after method provides the optional exception handling behaviour of __exit__ even for decorators.contextdecorator is a Python library that allows you to create APIs which work as decorators and as context managers.Here's an example of how you use it:from contextdecorator import ContextDecoratorclass mycontext(ContextDecorator): def __init__(self, *args): """Normal initialiser""" def before(self): """ Called on entering the with block or starting the decorated function. If used in a with statement whatever this method returns will be the context manager. """ def after(self, *exc): """ Called on exit. Arguments and return value of this method have the same meaning as the __exit__ method of a normal context manager. """@mycontext('some', 'args')def function(): passwith mycontext('some', 'args') as something: passBoth before and after methods are optional (but providing neither is somewhat pointless). See the tests for more usage examples.contextdecorator works with Python 2.4+ including Python 3.Repository and issue tracker: * contextdecorator on google codeThe project is available for download from PyPI so it can be easily installed: pip install -U contextdecorator easy_install -U contextdecoratorThe tests require unittest2 to run. Requirements: · Python


contextdecorator Related Software