Package: scheduled-task
Released under MIT licenseDescriptionThis package provides functionality to work with scheduled tasks (cron-like) in Python.
The main intention is to let you use planned scheduled tasks in lazy environments,
such as web server, by providing functions to check previous and next execution time of the task (get_previous_timeandget_next_time).This package doesn't parse cron string and is not fully compatible with cron.
It currently doesn't support last day of month and last weekday functionality,
however it supports providing the day of week number (#) or providing a week number.
Rules can be provided in a form of list of integers, range object (start, stop, and step will be used), or None.Installationpip install scheduled_taskQuick startTask that executes twice a day: at 00:00 and 00:30, get next execution timefromshceduled_taskimportScheduledTasktask=ScheduledTask(minutes=[0,30],hours=[0],days=None,months=None,years=None)print(task.get_next_time())Task that executes every 1st day of Monthfromshceduled_taskimportScheduledTaskfromdatetimeimportdatetimetask=ScheduledTask(minutes=[0],hours=[0],days=[1],months=None,years=None)print(task.get_previous_time(current_datetime=datetime(2016,11,19))# Prints datetime(2016, 12, 1, 0, 0))More complex example:Get next and previous USA presidential election day by getting the next day after first monday of November every 4rth yearfromscheduled_taskimportScheduledTasktask=ScheduledTask(minutes=[0],hours=[0],days_of_week=[0],days_of_week_num=[0],months=[11],years=range(1848,9999,4))print(task.get_next_time()+timedelta(days=1))print(task.get_previous_time()+timedelta(days=1))RulesRule typesWhen creating a ScheduledTask object, you can provide rules of when this task must be executed.
Every rule can be of 3 types:list: List of values. List can contain 1 value.range: Range of values, might contain valid step. For example, day=range(2, 31, 2) means "every even day of month".None: None means every valid value (* in cron).Rule fieldsFieldValueStrategiesDescriptionminutes0-59*Minuteshours0-23*Hoursdays1-31days_of_monthDaysdays_of_week0-6days_of_week,  days_of_week_numDays of week - Monday to Sundaydays_of_week_num0-4days_of_week_numNumber of day of week. For example, 0 and Friday means every 1st Friday of a monthweeks0-5days_of_weekWeek number. 0 and Friday means every Friday that happens in the first week of a monthmonths1-12*Monthsyears0-9999*YearsStrategiesWhen creating a ScheduledTask, not all fields are compatible with each other.
Generally, there are 3 strategies that will be used:days_of_month- default strategy. Used ifdaysrule is provided and non of week-related rules are provided.days_of_week- this strategy is chosen whendays_of_weekand/orweeksrules are provided. If that strategy is chosen,daysordays_of_week_numrules are ignored.days_of_week_num- this strategy is chosen whendays_of_weekanddays_of_week_numrules are provided. This is used to set up rules like "2nd Monday of July".Providing current timeWhen callingget_previous_timeorget_next_time, you can providecurrent_datetimeto check against.
If no current datetime is provided, datetime.utcnow() will be used.current_datetimedoesn't have to be in UTC-format. This library is timezone-agnostic and will return result using the same timezone as current_datetime.ContributingIf you find a bug in the library, please feel free to contribute by opening an issue or creating a pull request.