e1v1s
e1v1s

Reputation: 385

Pythonic way to write a series of non-cascading if statements

So I have this code I wrote to read from information from twitters REST API using tweepy and I'd like to collect a lot of information without exceeding the rate limit. This question is more of a conceptual one than tweepy-related. So far this is the only way i've thought of doing it:

for i, tweet in enumerate(tweepy.Cursor(api.user_timeline, screen_name = "@twitter").items(1400)):
    print(i, tweet.author.name)
    if i == 200:
        time.sleep(180)
    if i == 400:
        time.sleep(180)
    if i == 600:
        time.sleep(180)
    if i == 800:
        time.sleep(180)
    if i == 1000:
        time.sleep(180)
    if i == 1200:
        time.sleep(180)
    if i == 1400:
        sys.exit()

But rather than writing a bunch of if statements, is there a more pythonic way this can be written? Or if it isn't broken don't fix it?

Upvotes: 0

Views: 35

Answers (1)

John Kugelman
John Kugelman

Reputation: 361585

for i, tweet in enumerate(...):
    if i % 200 == 0 and i > 0:
        time.sleep(180)

This will fire every multiple of 200 except for 0.

Upvotes: 1

Related Questions