contexttimer icon indicating copy to clipboard operation
contexttimer copied to clipboard

[feature request] function decorator `timer` should support direct call

Open MagicFrogSJTU opened this issue 5 years ago • 1 comments

Currently, it can only be

@timer()
def my_func():
    pass

But, the following code is expected to run but fail:

@timer
def my_func():
    pass

An fix can be easily done to decorator definition by

def example2(_func=None, *, kw1=val1, kw2=val2):
    if _func is None:
        return functools.partials(example2, kw1=val1, kw2=kw2) 
    
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        # do things with kw1
        return func(*args, **kwargs)
    return wrapper

I am willing to make a PR if it is needed.

MagicFrogSJTU avatar Dec 07 '20 09:12 MagicFrogSJTU

Hi!

I think it's a good idea, and if you have a PR at the ready, I'd be more than happy to merge it.

Thanks!

On Mon, Dec 7, 2020, at 10:03 AM, yzchen wrote:

Currently, it can only be

@timer() def my_func(): pass But, the following code is expected to run but fail:

@timer def my_func(): pass An fix can be easily done to decorator definition by

`def example2(_func=None, *, kw1=val1, kw2=val2): if _func is None: return functools.partials(example2, kw1=val1, kw2=kw2)

@functools.wraps(func)
def wrapper(*args, **kwargs):
    # do things with kw1
    return func(*args, **kwargs)
return wrapper

`

I am willing to make a PR if it is needed.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/brouberol/contexttimer/issues/15, or unsubscribe https://github.com/notifications/unsubscribe-auth/AADVHA3INQZIOML6HR2XSL3STSK5FANCNFSM4UQERWSQ.

brouberol avatar Dec 13 '20 18:12 brouberol