Who knew that drinking a smoothie, could spark a simple idea for a weekend project. :)
The idea is pretty simple, a way to allow decorated python functions to callback to a certain handler based on the exception thrown by the wrapped function.
A decorator can establish that. Combine that with a class and you can now save the original function before it was decorated, and call that if need be.
In [1]: from smoothie.king import Dispenser
In [2]: def err_callback(*args, **kwargs):
...: print("Error handled")
...:
In [3]: juice = Dispenser()
In [4]: @juice.attach(exception=IndexError,
...: callback=err_callback)
...: def vending_machine():
...: drinks = ['Tea','Coffee', 'Water']
...: return drinks[4]
...:
In [5]: vending_machine()
Error handled
In [6]: juice.original('vending_machine')
Out[6]: <function __main__.vending_machine>
In [8]: juice.original('vending_machine')()
-------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-8-94b31cd25051> in <module>()
----> 1 juice.original('vending_machine')()
<ipython-input-4-10a9cb6127cc> in vending_machine()
3 def vending_machine():
4 drinks = ['Tea','Coffee', 'Water']
----> 5 return drinks[4]
6
IndexError: list index out of range
Extremely simple to use.
Code: https://github.com/sriram-mv/smoothie
Pull requests are welcome!
P.S Travis CI runs on every pull request as well.