python-decorators

How can I extend a library's decorator?

本秂侑毒 提交于 2020-08-24 10:25:46
问题 I would like to extend a library's decorator. I know that I can just call both decorators: @my_decorator @lib_decorator def func(): pass But I would like to avoid having to pass @lib_decorator to each function each time. I would like my decorator to automatically decorate func() with lib_decorator . How can I do this? Can they be nested? 回答1: You can incorporate the lib's decorator within yours. For simple, argument-less decorators, it's rather straight-forward: def my_decorator(): @lib

How can I extend a library's decorator?

[亡魂溺海] 提交于 2020-08-24 10:24:10
问题 I would like to extend a library's decorator. I know that I can just call both decorators: @my_decorator @lib_decorator def func(): pass But I would like to avoid having to pass @lib_decorator to each function each time. I would like my decorator to automatically decorate func() with lib_decorator . How can I do this? Can they be nested? 回答1: You can incorporate the lib's decorator within yours. For simple, argument-less decorators, it's rather straight-forward: def my_decorator(): @lib

How can I extend a library's decorator?

可紊 提交于 2020-08-24 10:23:11
问题 I would like to extend a library's decorator. I know that I can just call both decorators: @my_decorator @lib_decorator def func(): pass But I would like to avoid having to pass @lib_decorator to each function each time. I would like my decorator to automatically decorate func() with lib_decorator . How can I do this? Can they be nested? 回答1: You can incorporate the lib's decorator within yours. For simple, argument-less decorators, it's rather straight-forward: def my_decorator(): @lib

Sphinx decorated classes not documented

风格不统一 提交于 2020-08-11 05:34:21
问题 I'm documenting my library with Sphinx. And I have decorator logic_object : class logic_object: """Decorator for logic object class. """ def __init__(self, cls): self.cls = cls self.__doc__ = self.cls.__doc__ And I have gravity class that decorated by logic_object : @logic_object class gravity: """Basic gravity object logic class. :param float g: pixels of acceleration :param float jf: jump force """ #There is more not important code. My Sphinx .rst file is: Mind.Existence ===================

python decorator TypeError missing 1 required positional argument

蓝咒 提交于 2020-07-18 12:50:30
问题 I'm trying to write a decorator to repeat an erroring function N times with increasingly sleeping times in between. This is my attempt so far: def exponential_backoff(seconds=10, attempts=10): def our_decorator(func): def function_wrapper(*args, **kwargs): for s in range(0, seconds*attempts, attempts): sleep(s) try: return func(*args, **kwargs) except Exception as e: print(e) return function_wrapper return our_decorator @exponential_backoff def test(): for a in range(100): if a - random

how to avoid decorators while unit testing in python flask application

旧时模样 提交于 2020-07-09 05:18:06
问题 I am new to python and flask. I wanted to create unit tests for an api written. We have used jwt for authentication. To unit test, I don't want the flow to enter @jwt_required decorator. In addition to that I have chained a few other decorators for that method. class A(): @jwt_required() @mandatory_fields_check @unlock_and_lock() def get(self, address, name): .. .. .. return jsonify( {"payload": data, "message": "data received successfully"}), 200 Unit test I am trying to write def test_get()

Using a class to operate both as decorator and decorator factory

夙愿已清 提交于 2020-07-09 03:54:57
问题 Consider the following decorator function, which either returns a decorated function, or a parametrized decorat or function: from functools import wraps, partial, update_wrapper from inspect import signature def wrapit(func=None, *, verb='calling'): if func is None: # return a decoratOR return partial(wrapit, verb=verb) else: # return a decoratED @wraps(func) def _func(*args, **kwargs): print(f'{verb} {func.__name__} with {args} and {kwargs}') return func(*args, **kwargs) return _func Demo: >

Capture scrapy spider running status using an already defined decorator

假如想象 提交于 2020-05-17 10:09:21
问题 So I have a custom decorator called task that captures the status of a function. e.g., @task(task_name='tutorial', alert_name='tutorial') def start(): raw_data = download_data() data = parse(raw_data) push_to_db(data) if if __name__ == "__main__": start() So here the task decorator monitors the status of start function and send the error message to a central monitor system using alert_name if it fails otherwise sends successful message if it succeeds. Now I want to add this decorator to

Capture scrapy spider running status using an already defined decorator

我与影子孤独终老i 提交于 2020-05-17 10:06:47
问题 So I have a custom decorator called task that captures the status of a function. e.g., @task(task_name='tutorial', alert_name='tutorial') def start(): raw_data = download_data() data = parse(raw_data) push_to_db(data) if if __name__ == "__main__": start() So here the task decorator monitors the status of start function and send the error message to a central monitor system using alert_name if it fails otherwise sends successful message if it succeeds. Now I want to add this decorator to

How to access decorator attributes?

╄→尐↘猪︶ㄣ 提交于 2020-03-22 05:51:55
问题 Is it possible to access decorator attributes in Python 3? For example: is it possible to access self.misses after the call to the decorated fibonacci method? class Cache: def __init__(self, func): self.func = func self.cache = {} self.misses = 0 def __call__(self, *args): if not (args in self.cache): self.misses += 1 self.cache[args] = self.func(*args) return self.cache[args] @Cache def fibonacci(n): return n if n in (0, 1) else fibonacci(n - 1) + fibonacci(n - 2) fibonacci(20) ### now we