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.3.tar.gz
(7.9 kB
view details)
Built Distribution
File details
Details for the file cron-validator-1.0.3.tar.gz
.
File metadata
- Download URL: cron-validator-1.0.3.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3dc724e895d34f21de3873cf6b206c886b9382b459214ccada64eff55a4a523e |
|
MD5 | 574aaff71efff069567b760da78fa065 |
|
BLAKE2b-256 | 5ea232a4f972a77fa2b9e0f2eef7782e0a4097194a5772c70234c11a7dc50450 |
File details
Details for the file cron_validator-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: cron_validator-1.0.3-py3-none-any.whl
- Upload date:
- Size: 6.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04f8ebc45ad146b94531a036cc0f9aa8b889f2dde727eb59c566db361a47ec0a |
|
MD5 | fe91f5454266eca06dbf0c56a9c0d035 |
|
BLAKE2b-256 | a7e2a8ffee0a6525d1cfbf371e1db6d1ce555046ea24c5ce5104e57b429ae84a |