0

Is there a way to monitor which functions and methods in a directory of Python packages get called? I'm working with a large library of custom Python modules, and for code cleanup purposes I'm curious how many of the functions are actually being used.

I know I can see what code is run from a given command using Coverage.py, but I want to know how the code is used in the real world. If no one is calling the script that calls foo, I'd prefer to get rid of foo instead of maintaining it.

I would prefer a solution with low overhead, so as to not impact the runtime of the modules.

jpyams
  • 4,030
  • 9
  • 41
  • 66
  • Are you able to edit the code that you'd like to monitor? – bnaecker Oct 09 '17 at 21:19
  • Yes. It would be nice if the solution could be separate from the code, but not essential – jpyams Oct 09 '17 at 21:20
  • Not a dupe, but the answer can be found here: https://stackoverflow.com/questions/44968004/python-decorators-count-function-call. Make a decorator that counts calls and then runs the function normally. – bnaecker Oct 09 '17 at 21:44
  • Why not use coverage.py while the code is running in the real world? – Ned Batchelder Oct 10 '17 at 00:49
  • @NedBatchelder are you suggesting setting up the `python` command as a wrapper so that every Python script call actually runs the script in `coverage`? – jpyams Oct 10 '17 at 14:58
  • @jpyams I don't know how you are running your program in production. – Ned Batchelder Oct 10 '17 at 20:00
  • @NedBatchelder I guess `Coverage.py` would work if the overhead is low. I don't know what it's overhead is – jpyams Oct 10 '17 at 20:49

0 Answers0