# Cron Validator
[![Build Status](https://travis-ci.org/vcoder4c/cron-validator.svg?branch=master)](https://travis-ci.org/vcoder4c/cron-validator)
[![Coverage Status](https://coveralls.io/repos/github/vcoder4c/cron-validator/badge.svg?branch=master)](https://coveralls.io/github/vcoder4c/cron-validator?branch=master)
### **Features**
- Validate unix cron expression
- Match unit cron expression with specific datetime
- Generate match datetime between two datetime
- Schedule tasks
### **Install**
```shell script
pip install cron-validator
```
### **Run Tests**
**1. Install test requirements**
```shell script
pip install -r requirements/test.txt
```
**2. Run tests (with coverage if wished)**
```shell script
pytest --cov=. test/
```
### Sample
**1. Validate unix cron expression**
```python
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**
```python
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**
```python
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**
```python
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.](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html#eb-cron-expressions)
Currently we support:
- 'L' for day of the month and day of the week
- 'W' for day of the week.
```python
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](LICENSE.txt) file for details
Raw data
{
"_id": null,
"home_page": "https://github.com/vcoder4c/cron-validator",
"name": "cron-validator",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "cron,python,cron expression validator,cron expression iterator,cron scheduler",
"author": "vcoder",
"author_email": "doanngocbao@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/31/55/d3d8e7acad9dc3f54133df0972c79d38f2c6fc4be41f281a396c3afe4411/cron-validator-1.0.8.tar.gz",
"platform": null,
"description": "# Cron Validator\n\n[![Build Status](https://travis-ci.org/vcoder4c/cron-validator.svg?branch=master)](https://travis-ci.org/vcoder4c/cron-validator)\n[![Coverage Status](https://coveralls.io/repos/github/vcoder4c/cron-validator/badge.svg?branch=master)](https://coveralls.io/github/vcoder4c/cron-validator?branch=master)\n\n### **Features**\n\n- Validate unix cron expression\n- Match unit cron expression with specific datetime\n- Generate match datetime between two datetime\n- Schedule tasks\n\n### **Install**\n\n```shell script\npip install cron-validator\n```\n\n### **Run Tests**\n\n**1. Install test requirements**\n\n```shell script\npip install -r requirements/test.txt\n```\n\n**2. Run tests (with coverage if wished)**\n\n```shell script\npytest --cov=. test/\n```\n\n### Sample\n\n**1. Validate unix cron expression**\n\n```python\nfrom cron_validator import CronValidator\n\nassert CronValidator.parse('* * * * *') is not None # valid\nassert CronValidator.parse('*/3 * * * *') is not None # valid\nassert CronValidator.parse('*/61 * * * *') is None # invalid\n```\n\n**2. Match unit cron expression with specific datetime**\n\n```python\nfrom cron_validator import CronValidator\nfrom cron_validator.util import str_to_datetime\n\ndt_str = '2019-04-23 1:00'\ndt = str_to_datetime(dt_str)\n\nassert CronValidator.match_datetime(\"* * * * *\", dt)\nassert CronValidator.match_datetime(\"* * * 4 *\", dt)\nassert CronValidator.match_datetime(\"* * * 5 *\", dt) is False\nassert CronValidator.match_datetime(\"* * * 1-5 *\", dt)\nassert CronValidator.match_datetime(\"* * * 1-3 *\", dt) is False\nassert CronValidator.match_datetime(\"* * * 1/5 *\", dt) is False\nassert CronValidator.match_datetime(\"* * * * *\", dt)\nassert CronValidator.match_datetime(\"0 * * * *\", dt)\nassert CronValidator.match_datetime(\"0-30 * * * *\", dt)\nassert CronValidator.match_datetime(\"0/30 * * * *\", dt)\n```\n\n**3. Generate match datetime between two datetime**\n\n```python\nfrom cron_validator import CronValidator\nfrom cron_validator.util import str_to_datetime\n\n\nfrom_str = '2019-04-22 00:00'\nto_str = '2019-04-23 23:59'\n\nfor dt in CronValidator.get_execution_time(\"0 0 * * *\",\nfrom_dt=str_to_datetime(from_str), to_dt=str_to_datetime(to_str)):\n print(dt)\n\n# Output:\n# 2019-04-22 00:00:00+00:00\n# 2019-04-23 00:00:00+00:00\n```\n\n**4. Use scheduler for repetitive task**\n\n```python\nfrom cron_validator import CronScheduler\n\ncron_string = \"*/1 * * * *\"\nscheduler = CronScheduler(cron_string)\n\nwhile True:\n if scheduler.time_for_execution():\n # Get's called every full minute (excluding first iteration)\n print(\"Now is the next scheduled time.\")\n```\n\n**5. Use extended cron rules based on AWS EventBridge rules** (from v1.0.6)\n\nThe cron validator supports partially extended rules based on the Amazon EvenBridge rule set. [More info.](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html#eb-cron-expressions)\n\nCurrently we support:\n- 'L' for day of the month and day of the week\n- 'W' for day of the week.\n\n```python\nfrom cron_validator import CronValidator\nfrom cron_validator.util import str_to_datetime\nfrom cron_validator.regexes import Version\n\ndt_str = '2023-04-28 1:00'\ndt = str_to_datetime(dt_str)\n\nassert CronValidator.match_datetime(\"* * * * 30W\", dt, version=Version.EB)\nassert CronValidator.match_datetime(\"* * * * 5L\", dt, version=Version.EB)\n\ndt_str = \"2022-02-28 1:00\"\ndt = str_to_datetime(dt_str)\nassert CronValidator.match_datetime(\"* * L * *\", dt, version=Version.EB)\n```\n\n### License\n\nThis project is licensed under the MIT License - see the [LICENSE.txt](LICENSE.txt) file for details\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Unix cron implementation by Python",
"version": "1.0.8",
"project_urls": {
"Download": "https://github.com/vcoder4c/cron-validator/archive/v1.0.8.tar.gz",
"Homepage": "https://github.com/vcoder4c/cron-validator"
},
"split_keywords": [
"cron",
"python",
"cron expression validator",
"cron expression iterator",
"cron scheduler"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "490cd6bf9d572fb2ce3404fe37b794cf10dd44566368f7084c71b6028d3818ff",
"md5": "95233c86dfef07119bee113bf499686a",
"sha256": "6477fcc3d60bfbd1ec00a708f0b8b5136c1fef8140c10effea1f45b79d778653"
},
"downloads": -1,
"filename": "cron_validator-1.0.8-py3-none-any.whl",
"has_sig": false,
"md5_digest": "95233c86dfef07119bee113bf499686a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 7830,
"upload_time": "2023-06-19T10:09:44",
"upload_time_iso_8601": "2023-06-19T10:09:44.881569Z",
"url": "https://files.pythonhosted.org/packages/49/0c/d6bf9d572fb2ce3404fe37b794cf10dd44566368f7084c71b6028d3818ff/cron_validator-1.0.8-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3155d3d8e7acad9dc3f54133df0972c79d38f2c6fc4be41f281a396c3afe4411",
"md5": "e3621046c81cf611f49d3137915a6b7e",
"sha256": "dd485257adb6f590b3e9433f641440c801d307015259c1ee3eb6e21c964c8026"
},
"downloads": -1,
"filename": "cron-validator-1.0.8.tar.gz",
"has_sig": false,
"md5_digest": "e3621046c81cf611f49d3137915a6b7e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 9657,
"upload_time": "2023-06-19T10:09:48",
"upload_time_iso_8601": "2023-06-19T10:09:48.434267Z",
"url": "https://files.pythonhosted.org/packages/31/55/d3d8e7acad9dc3f54133df0972c79d38f2c6fc4be41f281a396c3afe4411/cron-validator-1.0.8.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-19 10:09:48",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "vcoder4c",
"github_project": "cron-validator",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "cron-validator"
}