Reputation: 6968
I have a function which looks like this:
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)
I need to record the exact line of code which is about to be executed. In a perfect world, I am looking for something like this:
print func(*args, **kwargs).toString or str(func(*args, **kwargs)
Does anybody know if what I am trying to do is possible or should I focus on just building a string myself?
Cheers!
Upvotes: 0
Views: 93
Reputation: 239473
For your case, you can simply do
print func.func_name, args, kwargs
I would write a generic decorator, if I am defining the functions myself, like this
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)
Output
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
Upvotes: 3