我的功能如下:
function_dictionary = {'get_user_from_id': (requests.get, #Function
(url + "/users/" + str(user_id),), #Tuple of Arguments
{'headers': headers}), #Dictionary of keyword args
'get_user_from_username': (requests.get,
(url + "/users?username=" + str(user_id),),
{'headers': headers}),
'get_user_from_email': (requests.get,
(url + "/users?email=" + str(user_id),),
{'headers': headers}),
'delete': (requests.delete,
(url + "/users/" + str(user_id),),
{'headers': headers}),
'patch_user':(requests.patch,
(url + "/users/" + str(user["ID"]),),
{'headers': headers, 'data':json.dumps(user)}),
'post': (requests.post,
(url + "/users" ,),
{'headers': headers,'data':json.dumps(user)})
}
func, args, kwargs = function_dictionary[request]
func(*args, **kwargs)
result = func(*args, **kwargs)
我需要记录即将执行的确切代码行。在一个完美的世界里,我正在寻找这样的东西:
print func(*args, **kwargs).toString or str(func(*args, **kwargs)
有人知道我想做什么是可能的还是我应该专注于自己构建一个字符串?
干杯!
答案 0 :(得分:3)
对于您的情况,您可以简单地执行
print func.func_name, args, kwargs
如果我自己定义函数,我会写一个通用装饰器,就像这个
def decorate_logger(func):
def inner(*args, **kwargs):
print "Entering", func.func_name, "with Parameters :", args, kwargs
temp = func(*args, **kwargs)
print "Leaving ", func.func_name, "with :", temp, "for :", args, kwargs
return temp
return inner
@decorate_logger
def sum(a, b, c):
return a + b + c
print sum(1, 2, 3)
print sum(1, 2, c = 3)
<强>输出强>
Entering sum with Parameters : (1, 2, 3) {}
Leaving sum with : 6 for : (1, 2, 3) {}
6
Entering sum with Parameters : (1, 2) {'c': 3}
Leaving sum with : 6 for : (1, 2) {'c': 3}
6