In a project, I need to benchmark some things and one of those thing is to time how long it takes to have an answer. In my code, this means measuring how long it takes between two different methods of an object are called. As I’m learning Python, I thought that a beautiful to do that would be with a class decorator.
If you don’t know anything about class decorator, I advise you read chapter 38.4 “Coding Class Decorators” from the book Learning Python by Mark Lutz and if you don’t even know anything about decorators, shame on you! No, I’m kidding, then just read chapter 38.2.
In a nutshell, a decorator makes it possible to replace a function (or a class) with another by just writing @myDecorator before the function or class. In my case, by writing @TwoMethodsTimer(“requestMedia”, “startPlayback”) before the class I want to benchmark, I will gather statistics on how long it takes to start the playback once I requested a media. Here is a first version of this decorator that I did after reading chapter 38.4 and some other blog posts and stack overflow threads:
In this example, we measure how long it takes between calling methodA and then methodB. It works great and all. We’re happy, until we do something new:
Here we see that internally calling methodB (self.methodB()) does not trigger the stopTimer! That’s because it doesn’t seem to be calling __getattr__ when being already in the same object. I tried several variant by intercepting calls to __getattribute__ but it’s a mess and when it’s not doing infinite recursive calls, I just don’t see the calls to the method I’m interested in.
To solve this problem, instead of hijacking calls for the __getattr__ method and looking at the name of the method being called, I just modify the attributes of the object when being created. Here is the code:
Just after the instantiation of the object, we change the mapping of the two methods we’re interested in with our own methods and store the old ones somewhere else. This trick works for external and internal method calls.
Posted in Code, Solution	| Tagged class decorator, decorator, internal call, Python, self, solution	| Leave a reply
How does a wearable tracker on your wrist measure your daily steps, activity, and burnt calories? How does it know whether you are walking slowly or running a sprint? How
Do you have a health and fitness application installed on your smartphone? Do you monitor your sleep cycles, count calories and track your activity using an application? Or do you
"... an engineer who is not only competent at the analytics and technologies of engineering, but can bring value to clients, team well, design well, foster adoptions of new technologies, position for innovations, cope with accelerating change and mentor other engineers" -- CACM 2014/12
Proper isolation of a Linux bridge talks about Linux bridges at the networking stack level. Very information and contains some isolation mechanisms I need to implement.
alternative approach to rate limiting: rate limiting tcp requests for various services. has good technical discussion of various approaches (token bucket, window counters, sliding window log, sliding window counters) with a suggested purpose-built solution.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
Disclaimer: This site may include market analysis. All ideas, opinions, and/or forecasts, expressed or implied herein, are for informational purposes only and should not be construed as a recommendation to invest, trade, and/or speculate in the markets. Any investments, trades, and/or speculations made in light of the ideas, opinions, and/or forecasts, expressed or implied herein, are committed at your own risk, financial or otherwise.