python-decorators

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

Threading Decorator [Python]

浪尽此生 提交于 2020-12-13 03:37:55
问题 i'm trying to create a simple program using python socket and threading library. I would like to automatize the following procedure using a decorator: t = threading.Thread(target=function, args=(arg1, arg2)) t.start() the program is structured using OOP so I defined a subclass inside the main one to contain all the decorators (I've read about this method in this article: https://medium.com/@vadimpushtaev/decorator-inside-python-class-1e74d23107f6). Therefore I have a situation like this:

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

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

﹥>﹥吖頭↗ 提交于 2020-11-26 19:35:01
问题 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