There are many things why Pyhton is my standard go-to language if it comes to implement something. It’s either a website, automation, data-mining or complex calculation Python excels in most of it. I decided to write some of my favourite things which just makes things cleaner and easier to implement.
1. Class based decorators and context managers
I’ve already wrote a post where I mentioned the power of decorators. Now I want to take it to a step further. In spite of the most example can be found online decorators can be written as classes simply because everything in Python is an object. You just need to specify the __call__ method as you would do it with your function.
However sometimes you already have one (or more) decorator around your functions and it’s not really good to overdecorate your functions. Also you might not want to create a separate function because only small part of your method needs to be wrapped with the decorator’s behaviour. For example timeout. This is where context managers come into play.
Just like for examples Django’s transaction contexts your decorators can be written in a way that it can be used as a
with context as well.
Here is a simple example:
from utils import timeout
from time import sleep
# As decorator
# As context
In both cases we’re going to get timeout.TimeoutException.
So how to implement this? The only trick is you need the __enter__ and __exit__ for the context manager behaviour and __call__ for the decorator. Here’s a simple example:
import logging, signal
def __init__(self, seconds, msg=None):
self.timeout = seconds
self.timed_out = False
self.msg = msg
self.sig_handler = signal.signal(signal.SIGALRM, self.__handler)
logging.warning('Timeout after %d seconds (%s)' % (self.timeout, self.msg))
logging.warning('Function timed out after %d seconds.' % self.timeout)
logging.debug('Timeout context started with %d seconds.' % self.timeout)
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_type == timeout.TimeoutException:
self.timed_out = True
def __call__(self, fn):
def wrapped(*args, **kwargs):
retval = None
exception = None
retval = fn(*args, **kwargs)
except timeout.TimeoutException as e:
exception = e
def __handler(self, signum, frame):