python-decorators

How can I decorate all functions imported from a file?

亡梦爱人 提交于 2019-11-28 05:06:35
问题 I have created many functions that are divided into different files, now I would like to apply the same decorator for all of them without modifying the files and without applying the decorators one by one. I have tried to use this explanation written by delnan, but I got no success for imported functions. About the decorator, it must update a list every time a function within a class is executexecuted with the function arguments and values, just like this other question I asked. Any

how to do a conditional decorator in python

对着背影说爱祢 提交于 2019-11-28 03:59:33
Is it possible to decorator a function conditionally. For example, I want to decorate the function foo() with a timer function ( timeit ) only doing_performance_analysis is True (see the psuedo-code below). if doing_performance_analysis: @timeit def foo(): """ do something, timeit function will return the time it takes """ time.sleep(2) else: def foo(): time.sleep(2) Decorators are simply callables that return a replacement, optionally the same function, a wrapper, or something completely different. As such, you could create a conditional decorator: def conditional_decorator(dec, condition):

How to inject variable into scope with a decorator?

ε祈祈猫儿з 提交于 2019-11-27 19:09:16
问题 [Disclaimer: there may be more pythonic ways of doing what I want to do, but I want to know how python's scoping works here] I'm trying to find a way to make a decorator that does something like injecting a name into the scope of another function (such that the name does not leak outside the decorator's scope). For example, if I have a function that says to print a variable named var that has not been defined, I would like to define it within a decorator where it is called. Here is an example

How to inject variable into scope with a decorator?

让人想犯罪 __ 提交于 2019-11-27 18:42:11
[Disclaimer: there may be more pythonic ways of doing what I want to do, but I want to know how python's scoping works here] I'm trying to find a way to make a decorator that does something like injecting a name into the scope of another function (such that the name does not leak outside the decorator's scope). For example, if I have a function that says to print a variable named var that has not been defined, I would like to define it within a decorator where it is called. Here is an example that breaks: c = 'Message' def decorator_factory(value): def msg_decorator(f): def inner_dec(*args, *

How can one attach a decorator to a function “after the fact” in python?

北城余情 提交于 2019-11-27 16:26:54
问题 The way I understand decorators of function in python (and I might be wrong), is that they are supposed to add side effects and modify the return value of a function. Now decorators are added above the function definition of the function to be decorated or by an assignment. Here is a small example: def print_args_decor(function): def wrapper(*args, **kwargs): print 'Arguments:', args, kwargs # Added side-effect return function(*args, **kwargs)*5 # Modified return value return wrapper @print

Python @property decorator not working

浪子不回头ぞ 提交于 2019-11-27 07:42:57
问题 Could anyone find a problem with this @property decorator? I cannot seem to get it to assert correctly. I'm sure I'm doing some really simple thing wrong, but can anyone point my tired eyes in the right direction please? class A: def __init__(self): self.a = 0 self._b = 0 @property def b(self): return self.b @b.getter def b(self): if self._b is None: return 0 return self._b @b.setter def b(self, val): self._b = (val * 20) def test_getter_setter(): obj = A() obj.a = 1 #obj.b = 2 print obj.a,

how to do a conditional decorator in python

北战南征 提交于 2019-11-27 05:13:07
问题 Is it possible to decorator a function conditionally. For example, I want to decorate the function foo() with a timer function ( timeit ) only doing_performance_analysis is True (see the psuedo-code below). if doing_performance_analysis: @timeit def foo(): """ do something, timeit function will return the time it takes """ time.sleep(2) else: def foo(): time.sleep(2) 回答1: Decorators are simply callables that return a replacement, optionally the same function, a wrapper, or something

Using classes as method decorators [duplicate]

冷暖自知 提交于 2019-11-27 03:39:50
问题 This question already has an answer here: How can I decorate an instance method with a decorator class? 3 answers While there are plenty of resources about using classes as decorators, I haven't been able to find any that deal with the problem of decorating methods . The goal of this question is to fix that. I will post my own solution, but of course everyone else is invited to post theirs as well. Why the "standard" implementation doesn't work The problem with the standard decorator class

Callable object decorator applied to method doesn't get self argument on input

风格不统一 提交于 2019-11-27 03:36:57
问题 import functools class Decor(object): def __init__(self, func): self.func = func def __call__(self, *args, **kwargs): def closure(*args, **kwargs): print args, kwargs return self.func(*args, **kwargs) return closure(*args, **kwargs) class Victim(object): @Decor def sum(self, a, b): return a+b v = Victim() v.sum(1, 2) Results in: (1, 2) {} Traceback (most recent call last): File "test.py", line 19, in <module> v.sum(1, 2) File "test.py", line 11, in __call__ return closure(*args, **kwargs)

How to skip a pytest using an external fixture?

北城以北 提交于 2019-11-27 03:11:28
问题 Background I am running a py.test with a fixture in a conftest file. You can see the code below(this all works fine): example_test.py import pytest @pytest.fixture def platform(): return "ios" @pytest.mark.skipif("platform == 'ios'") def test_ios(platform): if platform != 'ios': raise Exception('not ios') def test_android_external(platform_external): if platform_external != 'android': raise Exception('not android') conftest.py import pytest @pytest.fixture def platform_external(): return