leopd / timebudget

Stupidly-simple speed measurements for Python.
Apache License 2.0
156 stars 13 forks source link

quiet=false doesn't work in annotation #11

Open leopd opened 4 years ago

leopd commented 4 years ago

It looks like you should be able to annotate a funciton like @timebudget(quiet=False) but this doesn't work.

akash-suresh commented 4 years ago

I was looking for exactly this. I want to enable profiling, based on a boolean flag.

leopd commented 4 years ago

Do you plan to change the flag in code or at runtime? That is, do you want to go into the code and change the flag for profiling on/off vs have a command-line argument that does this? Because the implementations are different.

If you want to do it at runtime, then an argument in the decorator won't work. Because the decorator is evaluated (executed) when the code is loaded, on import, which generally happens before your code runs and starts looking at argparse etc.

Which is all to say that something like this won't worok:

@timebudget(quiet=is_profiling_enabled)
def my_func():
    pass

if __name__ == "__main__":
    is_profiling_enabled = parse_setup_arguments()
akash-suresh commented 4 years ago

Yes. I want to do something like this.

Is there an alternative way of achieving the same?