A Python utility for AWS cron expressions. Validate and parse AWS EventBridge cron expressions seamlessly.
Project description
AWS Croniter
AWS Croniter is a Python package for parsing, validating, and calculating occurrences of AWS EventBridge cron expressions. AWS cron expressions are a powerful way to schedule events, but they differ from standard Unix cron syntax. This library makes it easy to work with AWS-specific cron schedules programmatically.
Table of Contents
Inspiration
AWS Croniter was inspired by two existing packages,
aws-cron-expression-validator and
pyawscron, which serve similar purposes. The
aws-cron-expression-validator package focuses solely on validating AWS cron expressions, while pyawscron provides
functionality for parsing and calculating schedules such as next and previous occurrences. However, both packages had
limitations, and users often needed to install and integrate both packages to work effectively with AWS cron schedules.
AWS Croniter was developed to address these issues by combining the features of both packages into a single, robust
solution that also provides an improved and comprehensive tool for working with AWS cron expressions.
Features
- Validate AWS cron expressions against AWS EventBridge syntax.
- Parse and interpret cron rules with detailed validation error messages.
- Compute:
- Next and previous occurrence times for a given schedule.
- All occurrences of a schedule between two given dates.
- Handle special AWS cron syntax (e.g.,
?,L,W,#) and aliases for months (JAN,FEB, ...) and days of the week (SUN,MON, ...).
Installation
Install the package via pip:
pip install aws-croniter
Usage
AWS Cron Expression Example
from aws_croniter import AwsCroniter
cron_expression = "0 12 15 * ? 2023" # AWS cron syntax
aws_cron = AwsCroniter(cron_expression)
Handling Invalid Cron Expressions
When an invalid AWS cron expression is passed, AwsCroniter raises specific exceptions indicating the nature of the
error:
from aws_croniter import AwsCroniter
from aws_croniter.exceptions import AwsCroniterExpressionError
try:
invalid_cron = "0 18 ? * MON-FRI" # Missing fields
AwsCroniter(invalid_cron)
except AwsCroniterExpressionError as e:
print(f"Invalid cron expression: {e}")
# Output: Invalid cron expression: Incorrect number of values in '0 18 ? * MON-FRI'. 6 required, 5 provided.
Fetching the Next Occurrence
The get_next method retrieves the next occurrence(s) of the cron schedule from a specified date. This method
always returns a list with n items and sets the items to None if no valid occurrences are found.
Basic Usage
from aws_croniter import AwsCroniter
from datetime import datetime, timezone
# AWS cron expression
cron_expression = "0 12 15 * ? 2023"
aws_cron = AwsCroniter(cron_expression)
# Starting datetime
start_date = datetime(2023, 12, 14, tzinfo=timezone.utc)
next_occurrence = aws_cron.get_next(start_date)
print(next_occurrence)
# Output: [datetime.datetime(2023, 12, 15, 12, 0, tzinfo=datetime.timezone.utc)]
Retrieving Multiple Occurrences (n > 1)
from aws_croniter import AwsCroniter
from datetime import datetime, timezone
# AWS cron expression
cron_expression = "0 12 15 * ? 2023"
aws_cron = AwsCroniter(cron_expression)
# Starting datetime
start_date = datetime(2023, 9, 14, tzinfo=timezone.utc)
# Fetch the next 3 occurrences
next_occurrences = aws_cron.get_next(start_date, n=3)
print(next_occurrences)
# Output: [datetime.datetime(2023, 9, 15, 12, 0, tzinfo=datetime.timezone.utc),
# datetime.datetime(2023, 10, 15, 12, 0, tzinfo=datetime.timezone.utc),
# datetime.datetime(2023, 11, 15, 12, 0, tzinfo=datetime.timezone.utc)]
Using the Inclusive Parameter
Setting inclusive=True includes the start date in the results if it matches the schedule. The default is False.
from aws_croniter import AwsCroniter
from datetime import datetime, timezone
# AWS cron expression
cron_expression = "0 12 15 * ? 2023"
aws_cron = AwsCroniter(cron_expression)
# Starting datetime
start_date = datetime(2023, 12, 15, 12, 0,
tzinfo=timezone.utc)
# Include the starting date in the results
next_occurrence_inclusive = aws_cron.get_next(start_date, inclusive=True)
print(next_occurrence_inclusive)
# Output: [datetime.datetime(2023, 12, 15, 12, 0, tzinfo=datetime.timezone.utc)]
Fetching the Previous Occurrence
The get_prev method retrieves the previous occurrence(s) of the cron schedule from a specified date. This method
always returns a list with n items and sets the items to None if no valid occurrences are found.
Basic Usage
from aws_croniter import AwsCroniter
from datetime import datetime, timezone
# AWS cron expression
cron_expression = "0 12 15 * ? 2023"
aws_cron = AwsCroniter(cron_expression)
# Starting datetime
start_date = datetime(2023, 12, 14, tzinfo=timezone.utc)
prev_occurrence = aws_cron.get_prev(start_date)
print(prev_occurrence)
# Output: [datetime.datetime(2023, 11, 15, 12, 0, tzinfo=datetime.timezone.utc)]
Retrieving Multiple Occurrences (n > 1)
from aws_croniter import AwsCroniter
from datetime import datetime, timezone
# AWS cron expression
cron_expression = "0 12 15 * ? 2023"
aws_cron = AwsCroniter(cron_expression)
# Starting datetime
start_date = datetime(2023, 12, 14, tzinfo=timezone.utc)
# Fetch the previous 2 occurrences
prev_occurrences = aws_cron.get_prev(start_date, n=2)
print(prev_occurrences)
# Output: [datetime.datetime(2023, 11, 15, 12, 0, tzinfo=datetime.timezone.utc),
# datetime.datetime(2023, 10, 15, 12, 0, tzinfo=datetime.timezone.utc)]
Using the Inclusive Parameter
Setting inclusive=True includes the start date in the results if it matches the schedule. The default is False.
from aws_croniter import AwsCroniter
from datetime import datetime, timezone
# AWS cron expression
cron_expression = "0 12 15 * ? 2023"
aws_cron = AwsCroniter(cron_expression)
# Starting datetime
start_date = datetime(2023, 12, 15, 12, 0,
tzinfo=timezone.utc)
# Include the starting date in the results
prev_occurrence_inclusive = aws_cron.get_prev(start_date, inclusive=True)
print(prev_occurrence_inclusive)
# Output: [datetime.datetime(2023, 12, 15, 12, 0, tzinfo=datetime.timezone.utc)]
Fetch All Schedules in Range
The get_all_schedule_bw_dates method retrieves all occurrences of the cron schedule between two specified dates.
Basic Usage
from aws_croniter import AwsCroniter
from datetime import datetime, timezone
# AWS cron expression
cron_expression = "0 12 15 * ? 2023"
aws_cron = AwsCroniter(cron_expression)
# Define the date range
from_date = datetime(2023, 11, 14, tzinfo=timezone.utc)
to_date = datetime(2023, 12, 31, tzinfo=timezone.utc)
# Fetch all occurrences in the range
all_occurrences = aws_cron.get_all_schedule_bw_dates(from_date, to_date)
print(all_occurrences)
# Output: [datetime.datetime(2023, 11, 15, 12, 0, tzinfo=datetime.timezone.utc),
# datetime.datetime(2023, 12, 15, 12, 0, tzinfo=datetime.timezone.utc)]
Excluding Start and End Dates
Setting exclude_ends=True omits occurrences that match the start and end dates. The default is False.
from aws_croniter import AwsCroniter
from datetime import datetime, timezone
# AWS cron expression
cron_expression = "0 12 15 * ? 2023"
aws_cron = AwsCroniter(cron_expression)
# Define the date range
from_date = datetime(2023, 11, 14, tzinfo=timezone.utc)
to_date = datetime(2023, 12, 31, tzinfo=timezone.utc)
# Exclude the start and end dates from the results
all_occurrences_exclude_ends = aws_cron.get_all_schedule_bw_dates(from_date, to_date, exclude_ends=True)
print(all_occurrences_exclude_ends)
# Output: [datetime.datetime(2023, 11, 15, 12, 0, tzinfo=datetime.timezone.utc),
# datetime.datetime(2023, 12, 15, 12, 0, tzinfo=datetime.timezone.utc)]
Contributing
Contributions are welcome! Please read the contributing guidelines first.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contact
For any questions or suggestions, please open an issue or contact the maintainer at hello@techwithsid.com.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aws_croniter-1.0.2.tar.gz.
File metadata
- Download URL: aws_croniter-1.0.2.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a79cf69eb5f66f45419bcfc49dc68d905a46a527cd2eb512a174942465db0da8
|
|
| MD5 |
84b2814a3d4cb585cbb240f0ab3315c0
|
|
| BLAKE2b-256 |
668a59f100cb705b723669356d6a2684561703e3474cec36eaf8ae12c7dad13b
|
Provenance
The following attestation bundles were made for aws_croniter-1.0.2.tar.gz:
Publisher:
release.yml on siddarth-patil/aws-croniter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aws_croniter-1.0.2.tar.gz -
Subject digest:
a79cf69eb5f66f45419bcfc49dc68d905a46a527cd2eb512a174942465db0da8 - Sigstore transparency entry: 172169555
- Sigstore integration time:
-
Permalink:
siddarth-patil/aws-croniter@4ea11ad24404a71a0ce1ae494fdbd634cc26d6fc -
Branch / Tag:
refs/tags/1.0.2 - Owner: https://github.com/siddarth-patil
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4ea11ad24404a71a0ce1ae494fdbd634cc26d6fc -
Trigger Event:
push
-
Statement type:
File details
Details for the file aws_croniter-1.0.2-py3-none-any.whl.
File metadata
- Download URL: aws_croniter-1.0.2-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4588936ef91fa834aaad21babc36fcf41655cb8ca0ede978b7d223926374f017
|
|
| MD5 |
c9cb22a2e9b4b9a0332c860749cadc6a
|
|
| BLAKE2b-256 |
5656a91dd535542f0f727b9ecee4773b4ba3b69b275960fd18f932dd6a5628fa
|
Provenance
The following attestation bundles were made for aws_croniter-1.0.2-py3-none-any.whl:
Publisher:
release.yml on siddarth-patil/aws-croniter
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aws_croniter-1.0.2-py3-none-any.whl -
Subject digest:
4588936ef91fa834aaad21babc36fcf41655cb8ca0ede978b7d223926374f017 - Sigstore transparency entry: 172169557
- Sigstore integration time:
-
Permalink:
siddarth-patil/aws-croniter@4ea11ad24404a71a0ce1ae494fdbd634cc26d6fc -
Branch / Tag:
refs/tags/1.0.2 - Owner: https://github.com/siddarth-patil
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4ea11ad24404a71a0ce1ae494fdbd634cc26d6fc -
Trigger Event:
push
-
Statement type: