i’d like to obtain the parameter from the decorated function. It’s a lil bit different from the usual one.
Here is the code
JavaScript
x
37
37
1
import functools
2
3
4
def remotable_timer(fn):
5
@functools.wraps(fn)
6
def wrapper(*args, **kwargs):
7
print('remotable_timer', fn, args, **kwargs)
8
result = fn(*args, **kwargs)
9
return result
10
wrapper.original_fn = fn
11
return wrapper
12
13
14
def remotable_cache(fn):
15
@functools.wraps(fn)
16
def wrapper(*args, **kwargs):
17
print('remotable_cache', fn, args)
18
result = fn(*args, **kwargs)
19
return result
20
21
wrapper.original_fn = fn
22
return wrapper
23
24
25
remotable = remotable_timer(remotable_cache)
26
27
28
class TestClass(object):
29
@remotable
30
def test_function(self, context, *args):
31
print('test_function', context, *args)
32
33
34
if __name__ == '__main__':
35
test = TestClass()
36
test.test_function('context', 1, 2, 3)
37
output: i need fetch: context, 1,2,3(parameters in test_function) in remotable_timer
JavaScript
1
4
1
remotable_timer <function remotable_cache at 0x000002780F7336A8> (<function TestClass.test_function at 0x000002780F733730>,)
2
remotable_cache <function TestClass.test_function at 0x000002780F733730> (<__main__.TestClass object at 0x000002780F72D898>, 'context', 1, 2, 3)
3
test_function context 1 2 3
4
i know the code below can be a potential way to achieve it, but i have to decorate remotable_cache
in line anyway. Dose anyone have any idea, i’ll be appreciate it !
JavaScript
1
6
1
class TestClass(object):
2
@remotable_timer
3
@remotable_cache
4
def test_function(self, context, *args):
5
print('test_function', context, *args)
6
Advertisement
Answer
You should change
JavaScript
1
2
1
remotable = remotable_timer(remotable_cache)
2
to
JavaScript
1
3
1
def remotable(func):
2
return remotable_timer(remotable_cache(func))
3
Here is the result
JavaScript
1
4
1
remotable_timer <function TestClass.test_function at 0x0000010A12B99280> (<__main__.TestClass object at 0x0000010A12B93F70>, 'context', 1, 2, 3)
2
remotable_cache <function TestClass.test_function at 0x0000010A12B991F0> (<__main__.TestClass object at 0x0000010A12B93F70>, 'context', 1, 2, 3)
3
test_function context 1 2 3
4