dimanche 6 mai 2018

Python time.sleep command in a loop with if/else condition

I have a question about the time.sleepcommand in a if/elseloop. I make an api request for different places with the latitude and longitude. All in all, I need the request for 5 years. I use two forloops over the latitude and longitude and over the requested five years. The problem is that I can only send 6 requests per minute. Because of that, I use the time.sleepcommand as follow:

 for lat, lon in zip(lats, lons):
     for year in years:
         args = {
             'interpolate': False,
             'lat': lat,
             'lon': lon,
             'date_from': year + '-01-01',
             'date_to': year + '-12-31',
             'capacity': 500,
             'height': 44,
             'turbine': 'Enercon E40 500',
             'format': 'json',
             'metadata': False,
             'raw': True,
         }
         r = s.get(url, params=args)
         data = pd.read_json(r.text, orient='index')
         df = df.append(data)
     time.sleep(61)

The function works good and the time.sleepcommand too! But in this form, the function waits 61 seconds after the last passing. I wanna make this a litte bit "smoother" with an if/elseloop in that way, if the loop uses the last latitude/longitude, the time.sleepcommand can be ignored. The years are a normal list and the latitude/longitude are in a seperate excel table. Has someone a idea, how I can write this? The ways I tried it doesn't work and I need some new inspiration...Thank you!

Aucun commentaire:

Enregistrer un commentaire