decorator

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

Prevent a function from being called twice in a row

邮差的信 提交于 2020-04-16 05:54:12
问题 I'm learning decorators and I have a task asking me to create a decorator to prevent a function from being called twice in a row. If the same function is called again, it should return None I can't seem to understand how it really works, so far I've achieved this: def dont_run_twice(f): global counter def wrapper(*args, **kwargs): counter += 1 if counter == 2: return None else: result = f(*args, **kwargs) return result counter = 0 (I know that it's a really bad attempt, but I just don't know

Python decorators just syntactic sugar? [duplicate]

你说的曾经没有我的故事 提交于 2020-04-09 02:33:43
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Understanding Python decorators I am quite new on using Python decorators and from what I understand on my first impression that they are just syntactic sugar. Is there a more profound use of them for more complex uses ? 回答1: Yes it is syntactic sugar. Everything can be achieved without them, but with a few more lines of code. But it helps you write more concise code. Examples: from functools import wraps def

Python decorators just syntactic sugar? [duplicate]

泪湿孤枕 提交于 2020-04-09 02:26:29
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Understanding Python decorators I am quite new on using Python decorators and from what I understand on my first impression that they are just syntactic sugar. Is there a more profound use of them for more complex uses ? 回答1: Yes it is syntactic sugar. Everything can be achieved without them, but with a few more lines of code. But it helps you write more concise code. Examples: from functools import wraps def

Python decorators just syntactic sugar? [duplicate]

雨燕双飞 提交于 2020-04-09 02:20:11
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Understanding Python decorators I am quite new on using Python decorators and from what I understand on my first impression that they are just syntactic sugar. Is there a more profound use of them for more complex uses ? 回答1: Yes it is syntactic sugar. Everything can be achieved without them, but with a few more lines of code. But it helps you write more concise code. Examples: from functools import wraps def

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

How to access decorator attributes?

亡梦爱人 提交于 2020-03-22 05:51:29
问题 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

Decorator pattern mess

狂风中的少年 提交于 2020-03-16 05:59:25
问题 I'm having a problem figuring out if I'm using the decorator pattern the right way. Let's suppose I'm working on a console application. In this application I have defined a simple IConfigPathProvider interface, which will provide a configuration file path to some class. public interface IConfigPathProvider { string GetConfigPath(); } The path is stored in the appSettings section of the console application's app.config file. public class AppSettingsConfigPathProvider : IConfigPathProvider {

Why does Angular 2 use decorators?

元气小坏坏 提交于 2020-03-13 06:02:35
问题 I just started using Angular 2 and was wondering why some properties like selector and template are put in components decorators and not in components classes. What's the point of using all these decorators in Angular 2? 回答1: To make it easy for tools to provide all kinds of support in templates like: error checking auto-completion graphical GUI designers To generate code from decorators which allows: to define some things more declaratively or generate different code depending on some