
Security News
Security Community Slams MIT-linked Report Claiming AI Powers 80% of Ransomware
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.
contextdecorator
Advanced tools
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.
The contextdecorator module is a backport of new features added to the
contextlib module <http://docs.python.org/library/contextlib.html>_ in
Python 3.2. contextdecorator works with Python 2.4+ including Python 3.
Context managers inheriting from ContextDecorator have to implement
__enter__ and __exit__ as normal.
__exit__ <http://docs.python.org/reference/datamodel.html#object.__exit__>_
retains its optional exception handling even when used as a decorator.
Example::
from contextdecorator import ContextDecorator
class mycontext(ContextDecorator): def enter(self): print 'Starting' return self
  def __exit__(self, *exc):
     print 'Finishing'
     return False
@mycontext() ... def function(): ... print 'The bit in the middle' ... function() Starting The bit in the middle Finishing
with mycontext(): ... print 'The bit in the middle' ... Starting The bit in the middle Finishing
Existing context managers that already have a base class can be extended by
using ContextDecorator as a mixin class::
from contextdecorator import ContextDecorator
class mycontext(ContextBaseClass, ContextDecorator): def enter(self): return self
  def __exit__(self, *exc):
     return False
contextdecorator also contains an implementation of contextlib.contextmanager <http://docs.python.org/library/contextlib.html#contextlib.contextmanager>_
that uses ContextDecorator. The context managers it creates can be used as
decorators as well as in with statements. ::
from contextdecorator import contextmanager
@contextmanager def mycontext(*args): print 'Started' try: yield finally: print 'Finished!'
@mycontext('some', 'args') ... def function(): ... print 'In the middle' ... Started In the middle Finished!
with mycontext('some', 'args'): ... print 'In the middle' ... Started In the middle Finished!
Repository and issue tracker:
contextdecorator on google code <http://code.google.com/p/contextdecorator/>_The project is available for download from PyPI <http://pypi.python.org/pypi/contextdecorator>_
so it can be easily installed:
| ``pip install -U contextdecorator``
| ``easy_install -U contextdecorator``
The tests require unittest2 <http://pypi.python.org/pypi/unittest2>_
to run.
FAQs
Create APIs that work as decorators and as context managers.
We found that contextdecorator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.

Research
/Security News
Socket researchers found 10 typosquatted npm packages that auto-run on install, show fake CAPTCHAs, fingerprint by IP, and deploy a credential stealer.