From The number of times a function gets called.
Let's say, this function is called from outside the file, and I don't have access to it. I am building the function, and I just want to be able to count how many times it has been called from some other part of the program.
I can't use the actual code that calls the function. I do not have access to it. I know normally I should, but for testing purposes, I am not supposed to alter the code that calls the function. So I need to count how many times it is being called without accessing the program that calls the function.
I have tried copy and paste into the file with the function being used like so...
def counted_calls(f):
@functools.wraps(f)
def count_wrapper(*args, **kwargs):
count_wrapper.count += 1
return f(*args, **kwargs)
count_wrapper.count = 0
return count_wrapper
def myfunction(a, b, c, d)
wrapped = counted_calls(counted_calls)
integrate.quad(wrapped, 0, 1)
print(wrapped)
****more of my code *****
return something
First I get this error: Unresolved reference 'f'
.
But I get this warning:
Expected type 'Union[function, LowLevelCallable]', got '(args: Tuple[Any, ...], kwargs: Dict[str, Any]) -> Any' instead
Then when I run it, of course, f
is not defined.
How do I get this to work so I can count how many times myfunction()
is called?
I can't call my function, another part of the program calls this function whenever it wants to. I just need to track how many times it does. I can't go put this in that part of the program and make it call the function this way. I don't have access to it.
so like this?
def counted_calls(f):
@functools.wraps(f)
def count_wrapper(*args, **kwargs):
count_wrapper.count += 1
return f(*args, **kwargs)
count_wrapper.count = 0
return count_wrapper
wrapped = counted_calls(myfunction)
integrate.quad(wrapped, 0, 1)
print(wrapped)
def myfunction(a, b, c, d)
code code
return something
This returns the error:
TypeError: myfunction() missing 3 required positional arguments:
'b', 'c', and 'd'
I tried this, but still wants more arguments or something?
import functools
def counted_calls(f):
@functools.wraps(f)
def count_wrapper(*args, **kwargs):
count_wrapper.count += 1
return f(*args, **kwargs)
count_wrapper.count = 0
return count_wrapper
@counted_calls # <-- Apply decorator.
def myfunction(a, b, c, d):
...
return something
integrate.quad(myfunction, 0, 1)
print(myfunction.count)
TypeError: myfunction() missing 3 required positional arguments: b, c, d
So I changed it to what was recommended, however, I'm assuming b
, c
, and d
should be something specific, I just don't know what.
I tried as recommended:
import functools
def counted_calls(f):
@functools.wraps(f)
def count_wrapper(*args, **kwargs):
count_wrapper.count += 1
return f(*args, **kwargs)
count_wrapper.count = 0
return count_wrapper
@counted_calls # <-- Apply decorator.
def myfunction(a, b, c, d):
...
return something
integrate.quad(myfunction, 0, 1, 2, 3) # these need to be variables of some
# kind? Not sure what to put in this
# line to make it work
print(myfunction.count)
Still missing 2 required positional arguments: 'c' and 'd'.
I hover over the 2 and it says this:
Expected type 'Union[Iterable, tuple, None]', got 'int' instead