scheduled tasks for Python 3
Released under MIT license
This 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_time and get_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.
pip install scheduled_task
Task that executes twice a day: at 00:00 and 00:30, get next execution time
from shceduled_task import ScheduledTask task = ScheduledTask(minutes=[0, 30], hours=, days=None, months=None, years=None) print(task.get_next_time())
Task that executes every 1st day of Month
from shceduled_task import ScheduledTask from datetime import datetime task = ScheduledTask(minutes=, hours=, days=, 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 year
from scheduled_task import ScheduledTask task = ScheduledTask(minutes=, hours=, days_of_week=, days_of_week_num=, months=, years=range(1848, 9999, 4)) print(task.get_next_time() + timedelta(days=1)) print(task.get_previous_time() + timedelta(days=1))
When 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).
|days_of_week||0-6||days_of_week, days_of_week_num||Days of week - Monday to Sunday|
|days_of_week_num||0-4||days_of_week_num||Number of day of week. For example, 0 and Friday means every 1st Friday of a month|
|weeks||0-5||days_of_week||Week number. 0 and Friday means every Friday that happens in the first week of a month|
When 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 if days rule is provided and non of week-related rules are provided.
- days_of_week - this strategy is chosen when days_of_week and/or weeks rules are provided. If that strategy is chosen, days or days_of_week_num rules are ignored.
- days_of_week_num - this strategy is chosen when days_of_week and days_of_week_num rules are provided. This is used to set up rules like "2nd Monday of July".
Providing current time
When calling get_previous_time or get_next_time, you can provide current_datetime to check against. If no current datetime is provided, datetime.utcnow() will be used. current_datetime doesn't have to be in UTC-format. This library is timezone-agnostic and will return result using the same timezone as current_datetime.
If you find a bug in the library, please feel free to contribute by opening an issue or creating a pull request.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size scheduled_task-1.0.1-py3-none-any.whl (7.9 kB)||File type Wheel||Python version py3||Upload date||Hashes View hashes|
|Filename, size scheduled_task-1.0.1.tar.gz (10.8 kB)||File type Source||Python version None||Upload date||Hashes View hashes|
Hashes for scheduled_task-1.0.1-py3-none-any.whl