Cron Validator
Features
- Validate unix cron expression
- Match unit cron expression with specific datetime
- Generate match datetime between two datetime
- Schedule tasks
Install
pip install cron-validator
Run Tests
1. Install test requirements
pip install -r requirements/test.txt
2. Run tests (with coverage if wished)
pytest --cov=. test/
Sample
1. Validate unix cron expression
from cron_validator import CronValidator
assert CronValidator.parse('* * * * *') is not None # valid
assert CronValidator.parse('*/3 * * * *') is not None # valid
assert CronValidator.parse('*/61 * * * *') is None # invalid
2. Match unit cron expression with specific datetime
from cron_validator import CronValidator
from cron_validator.util import str_to_datetime
dt_str = '2019-04-23 1:00'
dt = str_to_datetime(dt_str)
assert CronValidator.match_datetime("* * * * *", dt)
assert CronValidator.match_datetime("* * * 4 *", dt)
assert CronValidator.match_datetime("* * * 5 *", dt) is False
assert CronValidator.match_datetime("* * * 1-5 *", dt)
assert CronValidator.match_datetime("* * * 1-3 *", dt) is False
assert CronValidator.match_datetime("* * * 1/5 *", dt) is False
assert CronValidator.match_datetime("* * * * *", dt)
assert CronValidator.match_datetime("0 * * * *", dt)
assert CronValidator.match_datetime("0-30 * * * *", dt)
assert CronValidator.match_datetime("0/30 * * * *", dt)
3. Generate match datetime between two datetime
from cron_validator import CronValidator
from cron_validator.util import str_to_datetime
from_str = '2019-04-22 00:00'
to_str = '2019-04-23 23:59'
for dt in CronValidator.get_execution_time("0 0 * * *",
from_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)):
print(dt)
# Output:
# 2019-04-22 00:00:00+00:00
# 2019-04-23 00:00:00+00:00
4. Use scheduler for repetitive task
from cron_validator import CronScheduler
cron_string = "*/1 * * * *"
scheduler = CronScheduler(cron_string)
while True:
if scheduler.time_for_execution():
# Get's called every full minute (excluding first iteration)
print("Now is the next scheduled time.")
5. Use extended cron rules based on AWS EventBridge rules (from v1.0.6)
The cron validator supports partially extended rules based on the Amazon EvenBridge rule set. More info.
Currently we support:
- 'L' for day of the month and day of the week
- 'W' for day of the week.
from cron_validator import CronValidator
from cron_validator.util import str_to_datetime
from cron_validator.regexes import Version
dt_str = '2023-04-28 1:00'
dt = str_to_datetime(dt_str)
assert CronValidator.match_datetime("* * * * 30W", dt, version=Version.EB)
assert CronValidator.match_datetime("* * * * 5L", dt, version=Version.EB)
dt_str = "2022-02-28 1:00"
dt = str_to_datetime(dt_str)
assert CronValidator.match_datetime("* * L * *", dt, version=Version.EB)
License
This project is licensed under the MIT License - see the LICENSE.txt file for details
Metadata
Release files for cron-validator 1.0.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cron-validator-1.0.8.tar.gz | 9.7 kB | Without provenance | Details for cron-validator-1.0.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cron_validator-1.0.8-py3-none-any.whl | Python 3 | none | any | Details for cron_validator-1.0.8-py3-none-any.whl |
Total release size: 17.5 kB
Release files / cron-validator-1.0.8.tar.gz
| Download URL | cron-validator-1.0.8.tar.gz |
|---|---|
| Size | 9.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dd485257adb6f590b3e9433f641440c801d307015259c1ee3eb6e21c964c8026
|
|
BLAKE2b-256 checksum How to use checksums |
3155d3d8e7acad9dc3f54133df0972c79d38f2c6fc4be41f281a396c3afe4411
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.13
|
Release files / cron_validator-1.0.8-py3-none-any.whl
| Download URL | cron_validator-1.0.8-py3-none-any.whl |
|---|---|
| Size | 7.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6477fcc3d60bfbd1ec00a708f0b8b5136c1fef8140c10effea1f45b79d778653
|
|
BLAKE2b-256 checksum How to use checksums |
490cd6bf9d572fb2ce3404fe37b794cf10dd44566368f7084c71b6028d3818ff
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.13
|
Security for cron-validator 1.0.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.