decorator

How can I extend a library's decorator?

本秂侑毒 提交于 2020-08-24 10:25:46
问题 I would like to extend a library's decorator. I know that I can just call both decorators: @my_decorator @lib_decorator def func(): pass But I would like to avoid having to pass @lib_decorator to each function each time. I would like my decorator to automatically decorate func() with lib_decorator . How can I do this? Can they be nested? 回答1: You can incorporate the lib's decorator within yours. For simple, argument-less decorators, it's rather straight-forward: def my_decorator(): @lib

How can I extend a library's decorator?

[亡魂溺海] 提交于 2020-08-24 10:24:10
问题 I would like to extend a library's decorator. I know that I can just call both decorators: @my_decorator @lib_decorator def func(): pass But I would like to avoid having to pass @lib_decorator to each function each time. I would like my decorator to automatically decorate func() with lib_decorator . How can I do this? Can they be nested? 回答1: You can incorporate the lib's decorator within yours. For simple, argument-less decorators, it's rather straight-forward: def my_decorator(): @lib

How can I extend a library's decorator?

可紊 提交于 2020-08-24 10:23:11
问题 I would like to extend a library's decorator. I know that I can just call both decorators: @my_decorator @lib_decorator def func(): pass But I would like to avoid having to pass @lib_decorator to each function each time. I would like my decorator to automatically decorate func() with lib_decorator . How can I do this? Can they be nested? 回答1: You can incorporate the lib's decorator within yours. For simple, argument-less decorators, it's rather straight-forward: def my_decorator(): @lib

How to mock a decorated function

╄→尐↘猪︶ㄣ 提交于 2020-07-14 18:22:30
问题 For testing reasons, I need to be able to mock the inner/original function of a decorated one which is used somewhere else: In mydecorator.py: def my_decorator(f): def wrapped_f(): print "decorated" f() return wrapped_f @my_decorator def function_to_be_mocked(): print 'original' def function_to_be_mocked_undecorated(): print 'original' def run_decorated(): function_to_be_mocked() def run_undecorated(): decorated_funtion = my_decorator(function_to_be_mocked_undecorated) decorated_funtion() As

How to mock a decorated function

倖福魔咒の 提交于 2020-07-14 18:20:15
问题 For testing reasons, I need to be able to mock the inner/original function of a decorated one which is used somewhere else: In mydecorator.py: def my_decorator(f): def wrapped_f(): print "decorated" f() return wrapped_f @my_decorator def function_to_be_mocked(): print 'original' def function_to_be_mocked_undecorated(): print 'original' def run_decorated(): function_to_be_mocked() def run_undecorated(): decorated_funtion = my_decorator(function_to_be_mocked_undecorated) decorated_funtion() As

How to mock a decorated function

孤街浪徒 提交于 2020-07-14 18:19:36
问题 For testing reasons, I need to be able to mock the inner/original function of a decorated one which is used somewhere else: In mydecorator.py: def my_decorator(f): def wrapped_f(): print "decorated" f() return wrapped_f @my_decorator def function_to_be_mocked(): print 'original' def function_to_be_mocked_undecorated(): print 'original' def run_decorated(): function_to_be_mocked() def run_undecorated(): decorated_funtion = my_decorator(function_to_be_mocked_undecorated) decorated_funtion() As

Typescript, decorate async function

冷暖自知 提交于 2020-07-05 04:22:10
问题 I'm trying to decorate async function#1 with some async function#2. E.g. function func2(param) { return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => { //make async operations and then return descriptor } @func2(param) async function func1() { await .... //some async operation await .... //some async operation } So, the main idea is to perform some async operation in decorator, and then perform other async calls in the main function. Is it possible to make this withing

JS TS apply decorator to all methods / enumerate class methods

让人想犯罪 __ 提交于 2020-06-25 09:11:06
问题 I would like to apply a decorator function to all methods within a class so I can replace: class User { @log delete() {} @log create() {} @log update() {} } with @log class User { delete() {} create() {} update() {} } 回答1: Create a class decorator and enumerate the properties on the target's prototype. For each property: Get the property descriptor. Ensure it's for a method. Wrap the descriptor value in a new function that logs the information about the method call. Redefine the modified

Repeating decorators for instance variables

故事扮演 提交于 2020-05-30 08:04:00
问题 I am writing a class where I need to check whether the instance variables are of a certain type. I noticed that there is a lot of repeating code. Is there a better way to do similar checks on the instance variables? Or is this the right way to do it? class Variable(): __type = 'Variable' def __init__(self, id = None, updateable = True, name = 'Variable', value=None): if id is not None: self.id = id if value is not None: self.value = value self.updateable = updateable self.name = name

Repeating decorators for instance variables

梦想与她 提交于 2020-05-30 08:03:44
问题 I am writing a class where I need to check whether the instance variables are of a certain type. I noticed that there is a lot of repeating code. Is there a better way to do similar checks on the instance variables? Or is this the right way to do it? class Variable(): __type = 'Variable' def __init__(self, id = None, updateable = True, name = 'Variable', value=None): if id is not None: self.id = id if value is not None: self.value = value self.updateable = updateable self.name = name