Cron parser and validation in python

I am currently running a django web application in python, where I store cron entries entered by the user into the database. I was wondering if there are any python libraries / packages that will check these entries before I store them in the database. I confirm that I have in mind the correct syntax, as well as the correct range (for example: a month cannot be 15). Anyone have any suggestions? Thanks!

+4
source share
1 answer

The Croniter package seems like it can get what you need. Example from the docs:

>>> from croniter import croniter >>> from datetime import datetime >>> base = datetime(2010, 1, 25, 4, 46) >>> iter = croniter('*/5 * * * *', base) # every 5 minites >>> print iter.get_next(datetime) # 2010-01-25 04:50:00 >>> print iter.get_next(datetime) # 2010-01-25 04:55:00 >>> print iter.get_next(datetime) # 2010-01-25 05:00:00 >>> >>> iter = croniter('2 4 * * mon,fri', base) # 04:02 on every Monday and Friday >>> print iter.get_next(datetime) # 2010-01-26 04:02:00 >>> print iter.get_next(datetime) # 2010-01-30 04:02:00 >>> print iter.get_next(datetime) # 2010-02-02 04:02:00 

In code, it also performs validation on the entered format. You probably came across this already, but just in case :)

+8
source

Source: https://habr.com/ru/post/1443065/


All Articles