python-decorators

list @property decorated methods in a python class

怎甘沉沦 提交于 2019-12-17 20:39:08
问题 Is it possible to obtain a list of all @property decorated methods in a class? If so how? Example: class MyClass(object): @property def foo(self): pass @property def bar(self): pass How would I obtain ['foo', 'bar'] from this class? 回答1: Anything decorated with property leaves a dedicated object in your class namespace. Look at the __dict__ of the class, or use the vars() function to obtain the same, and any value that is an instance of the property type is a match: [name for name, value in

Decorator execution order

无人久伴 提交于 2019-12-17 06:33:46
问题 def make_bold(fn): return lambda : "<b>" + fn() + "</b>" def make_italic(fn): return lambda : "<i>" + fn() + "</i>" @make_bold @make_italic def hello(): return "hello world" helloHTML = hello() Output: "<b><i>hello world</i></b>" I roughly understand about decorators and how it works with one of it in most examples. In this example, there are 2 of it. From the output, it seems that @make_italic executes first, then @make_bold . Does this mean that for decorated functions, it will first run

Real world example about how to use property feature in python?

♀尐吖头ヾ 提交于 2019-12-17 04:11:25
问题 I am interested in how to use @property in Python. I've read the python docs and the example there, in my opinion, is just a toy code: class C(object): def __init__(self): self._x = None @property def x(self): """I'm the 'x' property.""" return self._x @x.setter def x(self, value): self._x = value @x.deleter def x(self): del self._x I do not know what benefit(s) I can get from wrapping the _x filled with the property decorator. Why not just implement as: class C(object): def __init__(self):

How to use Python decorators to check function arguments?

廉价感情. 提交于 2019-12-13 22:25:32
问题 I would like to define some generic decorators to check arguments before calling some functions. Something like: @checkArguments(types = ['int', 'float']) def myFunction(thisVarIsAnInt, thisVarIsAFloat) ''' Here my code ''' pass Side notes: Type checking is just here to show an example I'm using Python 2.7 but Python 3.0 whould be interesting too 回答1: From the Decorators for Functions and Methods: Python 2 def accepts(*types): def check_accepts(f): assert len(types) == f.func_code.co_argcount

What difficulties might arise from using mutable arguments to an `lru_cache` decorated function?

三世轮回 提交于 2019-12-13 15:25:50
问题 In the comment: Is there a decorator to simply cache function return values? @gerrit points out a problem with using mutable, but hashable, objects to to a function with the functools.lru_cache decorator: If I pass a hashable, mutable argument, and change the value of the object after the first call of the function, the second call will return the changed, not the original, object. That is almost certainly not what the user wants. From my understanding, assuming the __hash__() function of the

Adding test methods dynamically using decorator

爷,独闯天下 提交于 2019-12-13 05:16:59
问题 I've been using DDT to parameterize my tests with great success for the past few months. My issue now is that I can't seem to inject a list variable as my data source. Doing so seems to confuse DDT causing it to not parameterize my tests. I started to create my own solution, but I can't seem to figure this last part out. Here is what I have thus far as decorators - def data(*values): def aaa(func): def wrapper(self, *args, **kwargs): pass # return func(self, *args, **kwargs) wrapper.func_name

Python - Metaclass decorator - How to use @classmethod

旧街凉风 提交于 2019-12-13 04:22:02
问题 I have the following Python metaclass that adds a deco_with_args decorator to each class: def deco_with_args(baz): def decorator(func): ... return func return decorator class Foo(type): def __prepare__(name, bases): return {'deco_with_args': deco_with_args} This allows me to use the decorator like this: class Bar(metaclass=Foo): @deco_with_args('baz') def some_function(self): ... How do I make the deco_with_args decorator behave like an @classmethod so that I can access the Bar class (or

How can attributes on functions survive wrapping?

走远了吗. 提交于 2019-12-13 02:38:07
问题 Let's say I have the following function which has an attribute which marks it for special handling within a callback subsystem: def my_func(msg): print msg my_func.my_marker = SPECIAL_CONSTANT The problem is that if other bits of code wrap my_func with functools.partial or another decorator, then my_marker will be lost. my_partial = partial(my_func, 'hello world') print my_partial.my_marker >>> AttributeError... Is there a way to protect attributes on functions when wrapping? Is there a

Contract of a decorator

泪湿孤枕 提交于 2019-12-12 18:42:01
问题 Contract: A function that takes function as argument and returns a function[i.e., modified(or same) version of passed function]. Passed function, here is, square , for example. @floatify def square(n): return n*n Is decorator suppose to return only decorated version of passed function, but nothing else? 回答1: It is supposed to only return a function, but nothing stops you from returning anything you'd want. >>> def d(x): ... return "hello" ... >>> @d ... def f(): ... return "world" ... >>> f

python flask how to pass a dynamic parameter to a decorator

社会主义新天地 提交于 2019-12-12 17:29:11
问题 I am using python flask framework. I write a decorator which will be need a parameter, and this parameter will be dynamic. my decorator like below, will be get a key ,and using the key fetch data from redis. def redis_hash_shop_style(key): def fn_wrapper(f): @wraps(f) def decorated_function(*args, **kwargs): data = redis_hash(key) return data return decorated_function return fn_wrapper and I have a class to using this decorater, code like this class ShopAreaAndStyleListAPI(Resource): @redis