0

I'm looking for the library which allows to track invocation of methods and functions. Think of it as of Mock providing called and call_count properties.

Example of end-result needed:

s = MagicProxyLib()

@s
class MyClass:

    def not_called(self):
        print("This is not called")

    def first_method(self):
        print("First is called")

    def second_method(self):
        print("Second is called")


mc = MyClass()
mc.first_method()
mc.second_method()
mc.second_method()

I can implement such a decorator myself, but do not want reinvent the wheel if there is already some library with similar functionality.

I expect to be able to use this library is a such way

assert not s.called(mc.not_called)
assert s.called(mc.first_method)
assert s.call_count(mc.second_method) == 2

I have checked this answer but profiling/tracing does not quite serve the same purpose as here. Thanks for you package suggestions.

GopherM
  • 352
  • 2
  • 8
  • P.S. I'm aware this can be implemented as a decorator like https://stackoverflow.com/questions/21716940/is-there-a-way-to-track-the-number-of-times-a-function-is-called, but I'm looking for existing libraries with broader functionality and providing extensive statistic (if exists) – GopherM Dec 23 '22 at 17:49

1 Answers1

1

You likely want a profiler - they can collect all this usage statistics. A nice one is shipped along with Python - check: https://docs.python.org/3/library/profile.html

jsbueno
  • 99,910
  • 10
  • 151
  • 209