decorator

Decorator-like syntax for a specific line of code

喜夏-厌秋 提交于 2021-01-24 07:42:44
问题 Linked topic (but not duplicate): Decorator to time specific lines of the code instead of whole method? I know how decorators are usually used for Python functions. Is there a similar concept/syntax for single lines of code? Example: with def measuretime(lineofcode): start = time.time() lineofcode() print time.time() - start then @measuretime im = Image.open(BytesIO(base64.b64decode(data))) would be interpreted like start = time.time() im = Image.open(BytesIO(base64.b64decode(data))) print

Decorator-like syntax for a specific line of code

纵饮孤独 提交于 2021-01-24 07:40:40
问题 Linked topic (but not duplicate): Decorator to time specific lines of the code instead of whole method? I know how decorators are usually used for Python functions. Is there a similar concept/syntax for single lines of code? Example: with def measuretime(lineofcode): start = time.time() lineofcode() print time.time() - start then @measuretime im = Image.open(BytesIO(base64.b64decode(data))) would be interpreted like start = time.time() im = Image.open(BytesIO(base64.b64decode(data))) print

What is the correct way to decorate an external (library) function?

一笑奈何 提交于 2021-01-24 07:30:52
问题 I'm using a library function several times in my code which tests for a pass/fail condition and executes different code accordingly, but for some reason does not have a return value for the result it finds; I'd like to add this with a decorator so that I can call it in my code. What is the correct way to do this given that I cannot edit the source file? Should I do something like: def test_pass(param1, param2): external_function(param1, param2) if(...): return False else: return True Or is

What is the correct way to decorate an external (library) function?

淺唱寂寞╮ 提交于 2021-01-24 07:30:30
问题 I'm using a library function several times in my code which tests for a pass/fail condition and executes different code accordingly, but for some reason does not have a return value for the result it finds; I'd like to add this with a decorator so that I can call it in my code. What is the correct way to do this given that I cannot edit the source file? Should I do something like: def test_pass(param1, param2): external_function(param1, param2) if(...): return False else: return True Or is

Order of decorations in Decorator Pattern

徘徊边缘 提交于 2020-12-29 09:46:11
问题 Most of you know the pizza / cofee example for the decorator pattern. Pizza* pizza1 = BigPizzaDecorator(MushromDecorator(SimplePizza())); Pizza* pizza2 = MushromDecorator(BigPizzaDecorator(SimplePizza())); the two object behave in a similar way, but not completely, in particular if you have non-commutative operation, for example: BigPizzaDecorator::price() { return 10 + PizzaDecorator::price(); } // this is commutative BigPizzaDecorator::name() { return "big " + PizzaDecorator::name(); } //

Mixin to wrap every method of a Scala trait

时光总嘲笑我的痴心妄想 提交于 2020-12-29 06:58:07
问题 Suppose I have a trait Foo with several methods. I want to create a new trait which extends Foo but "wraps" each method call, for example with some print statement (in reality this will be something more complicated / I have a couple of distinct use cases in mind). trait Foo { def bar(x: Int) = 2 * x def baz(y: Int) = 3 * y } I can do this manually, by overriding each method. But this seems unnecessarily verbose (and all too easy to call the wrong super method): object FooWrapped extends

Decorator for timeit.timeit method?

China☆狼群 提交于 2020-12-01 07:45:25
问题 I'm trying to write a simple time decorator to measure time taken by functions. However the code below is giving our recursion error. What's wrong with it? import timeit def measure(func): def wrapper(): func_name = func.__name__ setup="from __main__ import {}".format(func_name) op_time = timeit.timeit('{}()'.format(func_name), number = 2, setup=setup) print(ot) return wrapper @measure def sample(): return 10 sample() Output RecursionError Traceback (most recent call last) <ipython-input-61

Decorator for timeit.timeit method?

谁都会走 提交于 2020-12-01 07:45:06
问题 I'm trying to write a simple time decorator to measure time taken by functions. However the code below is giving our recursion error. What's wrong with it? import timeit def measure(func): def wrapper(): func_name = func.__name__ setup="from __main__ import {}".format(func_name) op_time = timeit.timeit('{}()'.format(func_name), number = 2, setup=setup) print(ot) return wrapper @measure def sample(): return 10 sample() Output RecursionError Traceback (most recent call last) <ipython-input-61

Possible to create a @synchronized decorator that's aware of a method's object?

旧城冷巷雨未停 提交于 2020-11-26 19:40:57
问题 I'm trying to create a @synchronized wrapper that creates one Lock per object and makes method calls thread safe. I can only do this if I can access method.im_self of the method in the wrapped method. class B: def f(self): pass assert inspect.ismethod( B.f ) # OK assert inspect.ismethod( B().f ) # OK print B.f # <unbound method B.f> print B().f # <bound method B.f of <__main__.B instance at 0x7fa2055e67e8>> def synchronized(func): # func is not bound or unbound! print func # <function f at

Possible to create a @synchronized decorator that's aware of a method's object?

僤鯓⒐⒋嵵緔 提交于 2020-11-26 19:36:14
问题 I'm trying to create a @synchronized wrapper that creates one Lock per object and makes method calls thread safe. I can only do this if I can access method.im_self of the method in the wrapped method. class B: def f(self): pass assert inspect.ismethod( B.f ) # OK assert inspect.ismethod( B().f ) # OK print B.f # <unbound method B.f> print B().f # <bound method B.f of <__main__.B instance at 0x7fa2055e67e8>> def synchronized(func): # func is not bound or unbound! print func # <function f at