dimanche 10 mai 2020

check if count is greater-than-equal a constant the day I am running a script, otherwise reset count for next day

Hello I have to implement a condition to an API based application, and I currently don't have enough knowledge with date-time implementations and needed help turning my pseudocode to python code:

daily_count = 0    

for(get_portion_of_data):         
   for(do_something from some_portion then get more):        
      for(process the some_portion):        
        daily_count = daily_count+1  

   if(daily_count >= 20 AND it is the day you ran the script):      
        sleep(until midnight of that day you're running scipt)   
        daily_count=0            
   elif (daily_count < 20 AND it is a new day):     
        daily_count=0      

I have everything done but I can't make the conditions of the if and last elif statements where I have to consider the time/day in mind. The problem is that I have limited API calls that I can make per day. And the amount of the call-backs from API varies daily, so sometimes I can process 3K sometimes it 4K sometimes it is 15K....all based on stress on the server of the college's server where I got API from.

If have this so far (I took out for loop stuff so I can keep it simple)-

daily_count = 0

for i in range(20):
    print("___hello from first loop___")
    for j in range(5):
        print("___hello from second loop__")
        for k in range(1):
            daily_count = daily_count +1
            print("_hello from third loop_")
            print("iterator value is this:", inner_itr_count)

    #--------------------------------------------------------#
    # the conditions of these if and elif that I don't know
    if (inner_itr_count >= 20 and datetime.datetime.now().day == datetime.datetime.now().day): #is this correct?
        # sleep until 12AM of that day
        # the number of seconds plus some more 
        seconds_in_a_day = 86450
        # current date with zero time (00:00:00)
        dt = datetime.datetime.now()
        midnight = datetime.datetime.combine(dt.date(), datetime.time())
        # the number of seconds since the beginning of the day
        seconds_since_midnight = (dt - midnight).seconds
        # the number of seconds remaining until the end of the day
        seconds_until_end_of_day = seconds_in_a_day - seconds_since_midnight
        print("Sleeping until the midnight. Don't disturb me.")
        daily_count = 0  # reset the daily quota
        time.sleep(seconds_until_end_of_day)

    elif (inner_itr_count < 20): # and ????? it is a new date/day):
        daily_count = 0

Very new to the topic of date-time, was wondering if anyone can help out.

Aucun commentaire:

Enregistrer un commentaire