I have a Python application in which I would like to monitor the number of calls to functions in the standard random module, at runtime; is there any nice way to do this, or do I have to “manually” do it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It seems to me that Python profiler should be able to do it “nicely”. Look at a post about Python profiling on SO.
At runtime, it seems that the decorators are the way to go. Of course, the default random module does not have the decorators, so perhaps you need to define a monitoredrandom module with decorators that proxys the random module and counts the calls. If you keep the function names and signatures the same as in random, you only have to modify the imports in your code.