Unix cron implementation by Python
Project description
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.")
License
This project is licensed under the MIT License - see the LICENSE.txt file for details
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
cron-validator-1.0.4.tar.gz
(7.8 kB
view details)
Built Distribution
File details
Details for the file cron-validator-1.0.4.tar.gz
.
File metadata
- Download URL: cron-validator-1.0.4.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using 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.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ba60dbf4d18b0b087b1f10ea985ed41dc8522bcafe67494741349c530c35852 |
|
MD5 | e6faad798088ce7f4517b6cf7b369f6b |
|
BLAKE2b-256 | 8937fe46a8acd51f6ed9dae7c1980502d71b305cc9a37f835fd278e14b3939c8 |
File details
Details for the file cron_validator-1.0.4-py3-none-any.whl
.
File metadata
- Download URL: cron_validator-1.0.4-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using 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.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 60c6a5446b84088bd627cabadbfa4d1f52bdc99fb7e3f627d0ade78ae904f68a |
|
MD5 | c205184d136da735f9c8ee730fc94294 |
|
BLAKE2b-256 | f36d71084e776de5d9f7e5627208cecf068dc39612a3170212fe03fbcdbf5f50 |