python-decorators

Python profiling using line_profiler - clever way to remove @profile statements on-the-fly?

梦想的初衷 提交于 2019-12-02 23:21:23
I want to use the excellent line_profiler , but only some of the time. To make it work I add @profile before every function call, e.g. @profile def myFunc(args): blah return and execute kernprof.py -l -v mycode.py args But I don't want to have to put the @profile decorators in by hand each time, because most of the time I want to execute the code without them, and I get an exception if I try to include them, e.g. mycode.py args Is there a happy medium where I can dynamically have the decorators removed based on some condition switch/argument, without having to do things manually and/or modify

Python Decorator for printing every line executed by a function

假如想象 提交于 2019-12-02 20:59:37
I want to, for debugging purposes, print out something pertaining to each and every line executed in a python method. For example if there was some assignment in the line, i want to print what value was assigned for that variable, and if there was a function call, i want to print out the value returned by the function, etc. So, for example if i were to use a decorator, applied on function/method such as : @some_decorator def testing() : a = 10 b = 20 c = a + b e = test_function() the function testing when called, should print the following : a = 10 b = 20 c = 30 e = some_value Is there some

A function composition operator in Python

喜欢而已 提交于 2019-12-02 07:52:13
In this question I asked about a function composition operator in Python. @Philip Tzou offered the following code, which does the job. import functools class Composable: def __init__(self, func): self.func = func functools.update_wrapper(self, func) def __matmul__(self, other): return lambda *args, **kw: self.func(other.func(*args, **kw)) def __call__(self, *args, **kw): return self.func(*args, **kw) I added the following functions. def __mul__(self, other): return lambda *args, **kw: self.func(other.func(*args, **kw)) def __gt__(self, other): return lambda *args, **kw: self.func(other.func(

Python decorator that let's method run or raise an exception

大憨熊 提交于 2019-12-02 07:29:12
问题 I need to have a decorator that takes an argument and checks (based on some simple logic) if the method should be allowed to run or raise an exception. class One(obj): trend = "trend" @myDecorator(self.trend) def click_button(self): clickable_element = self.driver.find_element_by_id(self.trend) clickable_element.click() return self class Two(obj): map = "map" @myDecorator(self.map) def click_button(self): clickable_element = self.driver.find_element_by_id(self.map) clickable_element.click()

Python decorator that let's method run or raise an exception

血红的双手。 提交于 2019-12-02 03:47:48
I need to have a decorator that takes an argument and checks (based on some simple logic) if the method should be allowed to run or raise an exception. class One(obj): trend = "trend" @myDecorator(self.trend) def click_button(self): clickable_element = self.driver.find_element_by_id(self.trend) clickable_element.click() return self class Two(obj): map = "map" @myDecorator(self.map) def click_button(self): clickable_element = self.driver.find_element_by_id(self.map) clickable_element.click() return self The logic should be something like this: def my Decorator(arg): if arg: "run the method"

How do I decorate an instance method with another class in Python 2.7?

…衆ロ難τιáo~ 提交于 2019-12-02 02:56:15
问题 In Python 2.7 I'd like to decorate an instance method test in class Foo with a decorator that is also a class called FooTestDecorator . From user Chirstop's question and the Python 2 docs' Descriptor HowTo guide I created this example. There seems to be an issue however, when I print my decorated method object, it's (inspected?) name is wrong because it is noted as a question mark like Foo.? . import types class FooTestDecorator(object): def __init__(self,func): self.func=func self.count=0 #

Decorated function returns None

最后都变了- 提交于 2019-12-02 02:45:11
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 mistake? Thanks. You don't return any value from new_function inside the decorator, therefore it

How do I decorate an instance method with another class in Python 2.7?

久未见 提交于 2019-12-02 02:02:25
In Python 2.7 I'd like to decorate an instance method test in class Foo with a decorator that is also a class called FooTestDecorator . From user Chirstop's question and the Python 2 docs' Descriptor HowTo guide I created this example. There seems to be an issue however, when I print my decorated method object, it's (inspected?) name is wrong because it is noted as a question mark like Foo.? . import types class FooTestDecorator(object): def __init__(self,func): self.func=func self.count=0 # tried self.func_name = func.func_name, but seemed to have no effect def __get__(self,obj,objtype=None):

Implementing a class property that preserves the docstring

折月煮酒 提交于 2019-12-01 19:18:41
I have a descriptor that turns a method into a property on the class level: class classproperty(object): def __init__(self, getter): self.getter = getter self.__doc__ = getter.__doc__ def __get__(self, instance, owner): return self.getter(owner) Used like this: class A(object): @classproperty def test(cls): "docstring" return "Test" However, I now can't access the __doc__ attribute (which is logical, because accessing A.test.__doc__ will fetch the __doc__ of str , because A.test already returns "Test" . My final goal is that my docstring will appear in sphinx, so it is not feasible to retrieve

Decorating Python's builtin print() function

可紊 提交于 2019-12-01 18:09:16
As we know in Python 3 print() is a function, is it possible to create a decorated version of it wrapped under json.dumps(indent=4) for ex. Calling print(mydict) should produce the same result as print(json.dumps(mydict, indent=4)) You don't need a decorator per se to do that. Just define a new function and call it print : import builtins def print(*args, **kwargs): builtins.print(json.dumps(*args, **kwargs, indent=4)) You can use the builtins module as shown to access the original print function. The thing is that doing this doesn't really gain anything over calling your new function