Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding class support #42

Closed
wants to merge 33 commits into from
Closed

Adding class support #42

wants to merge 33 commits into from

Conversation

qcoumes
Copy link

@qcoumes qcoumes commented Nov 10, 2017

Hello,

I've added a use_class_attribute=False optional flag which, if set to True, override seconds with self.timeout if it exists (raising AttributeError if not).

I've also added a test for this and updated version, changes and README accordingly.

@bitranox
Copy link

bitranox commented Nov 10, 2017

I personally dont like that the wrapped part is duplicated over and over.
Look at my fork with WRAPT, its a more elegant approach I think.
Also if You want to use that decorator with classes or class methods, You should choose the WRAPT version over functools - it behaves correctly when debugging with pycharm or others preserving traceback information on methods , static methods and class methods.

If You want to access custom (class or other) attributes, You can use smt. like :
...

def timeout(seconds=None, use_signals=True, timeout_exception=TimeoutError, exception_message=None, custom_timeout_attribute=False):
    ...
    if custom_attribute is not None: 
       new_seconds=eval(custom_timeout_attribute)

Its just a markup - didnt look at the old code that accurately, since my code already derived a lot from the old version. I will probably set up a new project, because I wrote a very nice retry decorator also, that will need my new fork of the timeout decorator ...

Now You can do (almost) anything You want.
When You use my fork, You would be able to write :

class Foo(object):
    def __init__(self, x):
        self.x=x

    @timeout(custom_timout_attribute='instance.x*2')
    def foo2(self):
        do something interesting

what You need to think of is - who will "win" - the custom_timeout_attribute or the kwarg possibly passed to the method ? :

class Foo(object):
    def __init__(self, x):
        self.x=x

    @timeout(custom_timout_attribute='instance.x*2')
    def foo2(self):
        do something interesting

>>> my_foo = Foo(3)
>>> my_foo.foo2(timeout=5)    # who should win here ? the popped kwarg or the custom attribute ? 
                              # and is it really neccessary to access special attributes 
                              # if You can pass new timeout by kwarg ? 

@bitranox
Copy link

bitranox commented Nov 10, 2017

hey, I added the eval idea into my project. give it a try (and if You want, make some tests for it) ...
You can now access attributes of the instance, but also args, kwargs or propertys of the wrapped function.
Check it out, its cool !
https://github.com/bitranox/wrapt-timeout-decorator

yours sincerely
Robert

@qcoumes qcoumes closed this Feb 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants