Vong Ly Vong Ly - 1 year ago 92
Python Question

Loop a function using the previous output value as input

I'm trying to query an API, but it only provides me with 100 records at a time and provides an offshoot record, which I need to use to query the next 100 records. I can write a function to query my results, but am having trouble looping my function to use the output of the previous function as the input of the following function. Here's what I want my loop to essentially do:

def query(my_offset=None):
page = at.get('Accounts',offset=my_offset)
a = page['records']
return str(page['offset'])


Jim Jim
Answer Source

I'm guessing res can have a special value indicating no more rows were returned, if so, a while loop can be deployed:

res = query()
while True:
    res = query(res)
    if not res: break

you just rebind the result of the query to res and re-use it during every iteration.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download