python-decorators

Optionally use decorators on class methods

允我心安 提交于 2020-01-05 08:20:53
问题 Im new to Python, and im building a wrapper for an api. I would want to let the user decide if he/she wants to use a decorator on methods I expose from my module. For example: # create a new instance api = MyApi() # return a simple json response with all the data related to a timetable print api.time_table Now the user has all the data, and can do whatever he/she wants. I would want to add some kind of 'convenience' methods, for example; to let the user get a small part of the json instead of

Python, reference class instance/method in decorated function

我的梦境 提交于 2020-01-05 04:27:05
问题 I'm having a hard time finding a way to reference class instances in a decorator function. import json import time import sys from PyQt5.QtCore import * from PyQt5.QtWidgets import * from main_UI import Ui_ApplicationWindow from slack import RTMClient class WorkerThread(QThread): finished = pyqtSignal(str) def __init__(self): QThread.__init__(self) self.rtm_client = RTMClient(token="xoxp...") def run(self): self.rtm_client.start() @RTMClient.run_on(event="message") def say_hello(**payload):

How can I use an operator to compose functions in Python?

强颜欢笑 提交于 2020-01-05 03:43:29
问题 It's fairly straightforward to write a function that composes two other functions. (For simplicity, assume they are one parameter each.) def compose(f, g): fg = lambda x: f(g(x)) return fg def add1(x): return x + 1 def add2(x): return x + 2 print(compose(add1, add2)(5)) # => 8 I would like to do composition using an operator, e.g., (add1 . add2)(5) . Is there a way to do that? I tried various decorator formulations, but I couldn't get any of them to work. def composable(f): """ Nothing I

Reassigning parameters within decorators in Python

走远了吗. 提交于 2020-01-04 04:46:08
问题 Consider a simple Python decorator with parameters: def decorator_factory(a=None): def decorator(func): def wrapper(*args, **kws): return func(*args, **kws) + a return wrapper return decorator Sometimes, it is useful to reassign the value of a parameter based on its actual value. This is a common design pattern in Python, especially given the issue with default parameter mutability, but it can be used in other situations, e.g.: def foo(a, b=None): if b is None: b = a return a + b However,

Decorate a function after it is defined?

六眼飞鱼酱① 提交于 2020-01-03 11:04:11
问题 I think the answer is no, but I can't seem to find a definitive claim. I have the following situation; def decorated_function(function): @functools.wraps(function) def my_function(): print "Hello %s" % function.__name__ return my_function for attr, value in dct.iteritems(): dct[attr] = decorated_function(value) And what I really want is something like; def my_function(function): print "Hello %s" % function.__name__ for attr, value in dct.iteritems(): dct[attr] = functools.wraps(my_function,

Decorate a function after it is defined?

无人久伴 提交于 2020-01-03 11:03:05
问题 I think the answer is no, but I can't seem to find a definitive claim. I have the following situation; def decorated_function(function): @functools.wraps(function) def my_function(): print "Hello %s" % function.__name__ return my_function for attr, value in dct.iteritems(): dct[attr] = decorated_function(value) And what I really want is something like; def my_function(function): print "Hello %s" % function.__name__ for attr, value in dct.iteritems(): dct[attr] = functools.wraps(my_function,

How to group decorators in Python

 ̄綄美尐妖づ 提交于 2020-01-02 04:48:28
问题 In Flask I'm using a set of decorators for each route, but the code is "ugly": @app.route("/first") @auth.login_required @crossdomain(origin='*') @nocache def first_page: .... @app.route("/second") @auth.login_required @crossdomain(origin='*') @nocache def second_page: .... I would prefer to have a declaration that groups all of them with a single decorator: @nice_decorator("/first") def first_page: .... @nice_decorator("/second") def second_page: .... I tried to follow the answer at Can I

Decorator and closures

眉间皱痕 提交于 2020-01-01 22:15:51
问题 I am going through the How to make a chain of function decorators? to understand decorator. In the following example, we see that "method_to_decorate" is accessible to wrapper function because of closures. But, I didn't understand how arguments self and lie are accessible to the wrapper function. def method_friendly_decorator(method_to_decorate): def wrapper(self, lie): lie = lie - 3 # very friendly, decrease age even more :-) return method_to_decorate(self, lie) return wrapper class Lucy

Why do we need wrapper function in decorators?

安稳与你 提交于 2020-01-01 02:53:06
问题 If I create a decorator like following: def my_decorator(some_fun): def wrapper(): print("before some_function() is called.") some_fun() print("after some_function() is called.") return wrapper @my_decorator def just_some_function(): print("Wheee!") Another decorator can be defined as: def my_decorator(some_fun): print("before some_function() is called.") some_fun() print("after some_function() is called.") @my_decorator def just_some_fun(): print("some fun") Both decorators will work the

Decorated function returns None

落爺英雄遲暮 提交于 2019-12-31 03:04:58
问题 I have a decorator that checks a function's argument for int type. def check_type_int(old_function): def new_function(arg): if not isinstance(arg, int): print 'Bad Type' # raise TypeError('Bad Type') else: old_function(arg) return new_function When I run a decorated function, it returns None instead of an int value. @check_type_int def times2(num): return num*2 times2('Not A Number') # prints "Bad Type" print times2(2) # prints "None" The last line should print 4 . Can someone please spot my