The more I learn about python programming language, the more I like it. As a developper, I like its straightforward and sparse syntax, its philosophy made of simple statments (try import this on python cli), leading to product easy-to-read code. As a Sysadmin, I never had to care much about the installation processes, as most of python packages are available on the debian or redhat packaging system, and the standard library is really substancial. Most applications may be executed with python 2.7, and differences between versions remain minor (except maybe for the transition between python2.7 and python3).
The feature of the day is named decorators. I will not explain deep into the details how they work, as the is a lot of documentation about it (another plus of python, its documentation is amazing). I am just providing some scripts which may be useful, as a reminder or for someone else. Most of them are inspired from here.
As a timer
Simple case
It may be useful to determine how long each functions take to execute its tasks. Here is how to do so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Whether the decorator itself gets some arguments or not
We prefer if the execution time printing occurs only when debugging.
In other words, having a condition to trigger the timer process.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Finally I do want to keep track of my function property
Let’s say I want to provide some documentation around my long_function().
1 2 3 4 5 6 7 |
|
I need to modify my decorator to make use of the functools library:
1 2 3 4 5 6 7 |
|
As a logging process
Ok, I like print command for debugging purpose. Everyone does this I guess. But, when it comes to production, I prefer printing into log files instead.
Here is a simple way to redirect stdout to a logging object when a production variable is triggered:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
I used sys.stdout.write instead of print which has the habit to add extra \n while printing.
Every print, stdout.write, and other craps spitting on stdout will be logged instead.
How many time my function is getting called?
Let’s continue in the spirit of the timer decorator.
Function calls counter
This time, I want to know how many times my function is getting called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Average execution time
Let’s go further!
This let us compute the average execution time of my function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
This is all for this time! But I still haven’t covered many decorators aspects, such as caching decorators, thread synchronization or timeout decorators.
To be continued, then…