optimization - Slightly different loops in python / Minimize code -
i want minify rows of code. have 2 loops difference 2 lines. possible (functions or classes) change lines in each occasion? 2 loops are:
cursor = '' while true: data = api_like_query(id,cursor) #more code in data['data']: ids_likes += i['id']+' , ' #more code
and
cursor = '' while true: data = api_com_query(id,cursor) #more code in data['data']: ids_likes += i['from']['id']+' , ' #more code
more code same chunk of code used. difference in function call (line 3) , different dictionary object in line 6.
you can create function quite easily:
def do_stuff(api_func, get_data_func): cursor = '' while true: data = api_func(id, cursor) #more code in data['data']: ids_likes += get_data_func(i) + ', ' #more code
then first loop can reproduced with:
do_stuff(api_like_query, lambda i: i['id'])
and second one:
do_stuff(api_come_query, lambda i: i['from']['id'])
functions made divide code smaller, more testable , reusable pieces, seems appropriate in case.
Comments
Post a Comment