Recently I decided to start encapsulating my analysis into an object which has methods but also holds data from a data analysis.
This is conveninent since I’ve implemented classes to handle pipeline objects (projects, samples, annotation sheets).
Still, nothing particularly exciting:
It would be nice if each time I run a certain functions of the
Analysis class the class itself would be saved as a Python
Enter Python decorators.
If you’re new to Python or Python decorators (I’ve known them for a while but seldomly use them) here’s a really nice introduction to nested functions, clojures and decorators.
In this case I write a decorator which calls the function and then performs its action, in this case, pickling the
To apply the decorator the some methods of the class, just add
@pickle_me to the method:
This works however, for functions accepting only
self as argument as in the example. To allow an arbitrary number of arguments passed to each function, we will make the decorator function accept one argument which will be the
Analysis (I keep calling it
obj) object and any number of arguments (by using
*args), which will be passed to the function that is decorated.
If I give the
Analysis class an attribute holding where it should be pickled (
pickle_file), then I can tell the decorator function to get it from the class itself: