python-decorators

Passing 'self' parameter during methods decorating in Python [duplicate]

和自甴很熟 提交于 2019-12-31 02:19:13
问题 This question already has answers here : Decorating Python class methods - how do I pass the instance to the decorator? (4 answers) Python decorator, self is mixed up [duplicate] (3 answers) Closed 4 months ago . I want to create decorator that shows which parameters were passed to function and methods. I have already written the code for functions, but methods are giving me a headaches. This is function decorator that works as intended: from functools import update_wrapper class

Decorator error: NoneType object is not callable

自古美人都是妖i 提交于 2019-12-30 11:55:11
问题 I wrote a function decorator like this: def tsfunc(func): def wrappedFunc(): print '%s() called' % func.__name__ return func() return wrappedFunc() @tsfunc def foo(): pass foo() # to get it work, use foo instead of foo() foo() I got following error message: foo() called Traceback (most recent call last): File "decorator.py", line 11, in <module> foo() TypeError: 'NoneType' object is not callable I get it work by replacing "foo()" with "foo". but I still didn't get the result I expected: foo()

How can I implement a custom error handler for all HTTP errors in Flask?

我怕爱的太早我们不能终老 提交于 2019-12-29 06:18:15
问题 In my Flask app, I can easily expand the list of errors handled by a single custom error handler by adding errorhandler decorators for each error code as with @application.errorhandler(404) @application.errorhandler(401) @application.errorhandler(500) def http_error_handler(error): return flask.render_template('error.html', error=error), error.code However this approach requires an explicit decorator for each error code. Is there a way decorate my (single) http_error_handler function so that

How can I get the same/to pass parameters between decorator and decorated function?

99封情书 提交于 2019-12-25 17:18:54
问题 In this case I want to pass _source_dir_abs:str into decorator. I tried to mimic the same process that Flask has for routing to pass parameter from decorator to function it is decorated. But this makes the parameter interpreted as a literal string and not as a variable. @dec_check_abs("<_source_dir_abs>") def walk_return_dir_nofolder(_source_dir_abs:str) -> list: w = walk(_source_dir_abs) d = [d for d, fol, fil in w if len(fol) == 0] return d I tried with @dec_check_abs(_source_dir_abs) it

How can I get the same/to pass parameters between decorator and decorated function?

被刻印的时光 ゝ 提交于 2019-12-25 17:18:24
问题 In this case I want to pass _source_dir_abs:str into decorator. I tried to mimic the same process that Flask has for routing to pass parameter from decorator to function it is decorated. But this makes the parameter interpreted as a literal string and not as a variable. @dec_check_abs("<_source_dir_abs>") def walk_return_dir_nofolder(_source_dir_abs:str) -> list: w = walk(_source_dir_abs) d = [d for d, fol, fil in w if len(fol) == 0] return d I tried with @dec_check_abs(_source_dir_abs) it

Preserve Signature in Decorator python 2

隐身守侯 提交于 2019-12-25 09:26:38
问题 I am writing a decorator which will catch TypeError for incorrect number of arguments in a function call and will print a customised message. The code is here: import inspect def inspect_signature(f): def decorate(*args, **kwargs): try: f(*args, **kwargs) except TypeError as e: print('Failed to call "{}" with signature {}. Provided args={} and kwargs={}.'.format( f.__name__, inspect.getargspec(f).args, args, kwargs)) return f return decorate @inspect_signature def func(foo, bar): pass func('a

Python decorator for attribute and method?

柔情痞子 提交于 2019-12-25 06:37:37
问题 Is it possible to have a decorator that makes a method work like an attribute if values are assigned to it using class.something = 2 and work like a method if it is called like class.something(2, True) ? What I currently have To be more concrete, I currently have the following class attributeSetter(object): ''' Makes functions appear as attributes. Takes care of autologging.''' def __init__(self, func): self.func = func def __set__(self, obj, value): return self.func(obj, value) def __repr__

Decorated class looses acces to its attributes

孤人 提交于 2019-12-25 05:35:24
问题 I implemented a decorator that worked like a charm until I added attributes to the decorated class. When I instantiate the class, it cannot acces the calss attributes. Take the following minimal working example : from module import specialfunction class NumericalMathFunctionDecorator: def __init__(self, enableCache=True): self.enableCache = enableCache def __call__(self, wrapper): def numericalmathfunction(*args, **kwargs): func = specialfunction(wrapper(*args, **kwargs)) """ Do some setup to

Use decorator with same name as an instance method

假装没事ソ 提交于 2019-12-25 01:47:08
问题 I have the following snippet: from decorators import before_save class User(Model): def before_save(self): pass @before_save def meth(self): pass It seems that when I try to decorate meth , it "uses" the instance method before_save , not the imported decorator. How can I still declare an instance method with the same name as a decorator and still be able to properly use both of them? Shouldn't the decorator work as expected? How is it possible to refer to the before_save instance method just

retry decorator implementation, retries not defined

拜拜、爱过 提交于 2019-12-24 22:33:29
问题 I have implemented the following retry decorator. def retry(delay=10, retries=4): def retry_decorator(f): @wraps(f) def f_retry(*args, **kwargs): while retries > 1: try: return f(*args, **kwargs) except Exception as e: msg = "Exception: {}, Retrying in {} seconds...'.format(e, delay)" print(msg) time.sleep(delay) retries -= 1 return f(*args, **kwargs) return f_retry return retry_decorator I get the error that retries is not defined. However, retries is mentioned in the function definition. I