Python Decorator Code Runs Before Original Function Code -


def raw_list(function):     @wraps(function)     def wrapper(args, producer_data):         print producer_data[2]         tenant, token, url = producer_data         body, status_code = do_request(url, token)         return function(args, producer_data)     return wrapper  @raw_list def member_list(args, producer_data):     # in argparse, consumer or producer data can used because     # consumer aliased producer.     uuid = args['uuid']     producer_data[2] = producer_data[2] + "/" + uuid + "/members" 

i have several functions take url, mutate it, , make api call url. reason, made wrapper function api call part. so, each function needs mutate url , decorated wrapper function.

but issue having mutated url code producer_data[2] = producer_data[2] + "/" + uuid + "/members" seems running after function decorator code runs, , not before. because of this, original url being used instead of mutated url.

how can fix logic flow , make api call made mutated url?

if want decorated function called before rest of wrapper's code, call before rest of wrapper's code:

def raw_list(function):     @wraps(function)     def wrapper(args, producer_data):         # call here!         retval = function(args, producer_data)         print producer_data[2]         tenant, token, url = producer_data         body, status_code = do_request(url, token)         return retval     return wrapper 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -