Skip to content

Instantly share code, notes, and snippets.

@galileoguzman
Last active October 31, 2018 17:19
Show Gist options
  • Save galileoguzman/570d9886f931d63cb3eb3de775e12578 to your computer and use it in GitHub Desktop.
Save galileoguzman/570d9886f931d63cb3eb3de775e12578 to your computer and use it in GitHub Desktop.
# importing python libraries
from datetime import datetime, time, timedelta
from functools import reduce
# defining the scope of tasks in time daily
START_TIME = datetime.now().replace(hour=6, minute=0, second=0, microsecond=0)
LIMIT_TIME = datetime.now().replace(hour=7, minute=0, second=0, microsecond=0)
TIME_AWAIT = 15
def is_time_between(begin_time, end_time, check_time=None):
'''
Function to verify if a time object it's inside of a range of time
'''
check_time = check_time or datetime.utcnow()
if begin_time < end_time:
return check_time > begin_time and check_time < end_time
else:
return check_time > begin_time or check_time < end_time
def get_schedule_for_accounts(account_ids):
'''
THIS FUNTION IS IN CHARGE OF THE COMPUTATION OF SCHEDULES FOR EACH ACCAOUNT RECEIVED
THE FUNCTION GET AN ARRAY OF ACCOUNTS IDS ANFO FOR EACH ONE COMPUTE THE SCHEDULES
WITH A DIFFERENCE OF 15 MINUTES
'''
account_schedules = []
account_counter = 0
for account in account_ids:
if account_counter == 0:
time_scheduled = START_TIME
else:
time_scheduled = first_schedule_from_last_account + timedelta(minutes = 1)
schedule_for_account = list()
while time_scheduled < LIMIT_TIME:
if len(schedule_for_account) == 0:
time_scheduled = time_scheduled
else:
time_scheduled = time_scheduled + timedelta(minutes = TIME_AWAIT)
schedule_for_account.append(time_scheduled.strftime("%Y-%m-%d %H:%M:%S"))
account_schedules.append((account, schedule_for_account))
first_schedule_from_last_account = datetime.strptime(schedule_for_account[0], "%Y-%m-%d %H:%M:%S")
account_counter = account_counter + 1
return account_schedules
def get_time_in_minutes_from_schedules(times):
'''
THIS FUNCTION IS IN CHARGE TO RETURN AN STRING WITH ALL THE SCHEDULES FROM AN ARRAY OF TIME OBJECTS
PARSE THE TIME OBJECT ON A STRING AND APPEND THE RESULT INSIDE AN ARRAY
AT THE END JOIN ALL THE STRINGS STORED IN THE ARRAY
'''
minutes = []
for sh in times:
min = datetime.strptime(sh.split(' ')[1],'%H:%M:%S')
minutes.append(min.strftime("%H:%M:%S"))
minutes_str = ' '.join(minutes)
return minutes_str
# mock data for account ids
accounts = [1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010, 1011, 1012, 1013]
# SCHEDULES STORE THE RESULT FOR THE ARRAY RETURNED BY get_schedule_for_accounts FUNCTION
# ARRAY OF ACCOUNTS WITH THEIR SCHEDULES
schedules = get_schedule_for_accounts(accounts)
# PRINTING THE RESULTS IN A READLY FORMAT
for account_id, schedules_account in schedules:
print('ACCOUNT ID : {} WITH SCHEDULES AT {}'.format(account_id, get_time_in_minutes_from_schedules(schedules_account)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment