Skip to main content

Simple cron-like parser, which determines if current datetime matches conditions.

Project description

pycron

Test Status Coverage Status

Simple cron-like parser for Python, which determines if current datetime matches conditions.

Installation

pip install pycron

Usage

import pycron
pycron.is_now('*/5 * * * *')  # True every 5 minutes
pycron.is_now('0 * * * *')    # True every hour, on minute 0

Help

The formats currently supported are

  • */5 (for "every X" function),
  • 4-10 (for time ranges),
  • 6,8,23 (for a list of values),
  • * (for wildcard),
  • and of course a single number.

The module includes is_now(s, dt=None), where s is the cron-style string and dt is the datetime to use (defaults to current datetime, if not set). The function returns True, if dt matches the format.

It also includes has_been(s, since, dt=None), where s is the cron-style string, since is a datetime in the past and dt is the datetime to use (defaults to current datetime, if not set). The function returns True, if dt would have matched the format at some point during the period. This behaves much like like anacron and is useful for applications which do not run continuously.

All functions are compatible with both timezone aware and naive datetimes.

There are couple of helpers available, mainly for use with Django. They give out list of tuples, as required by Django field choices.

The available helpers are

  • pycron.MINUTE_CHOICES,
  • pycron.HOUR_CHOICES,
  • pycron.DOM_CHOICES, for day of month
  • pycron.MONTH_CHOICES, for month names
  • pycron.DOW_CHOICES, for day names

Support for alternative datetime -libraries

Currently supported "alternative" datetime libraries are:

Notes

This was done, as I personally needed something like this to implement proper timers for my Django-project and every available library felt too complicated for my use-case. Also, this was a good coding exercise...

As the Django -helper choices are quite limited, I've expanded them in my own project by adding values like ('*/5', 'every 5 minutes'), ('1-5', 'on weekdays'), and ('0,6', 'on weekends'). I haven't included them in the code, as every use-case is different, this was just to give an idea on how to use this ;)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycron-3.1.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

pycron-3.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file pycron-3.1.0.tar.gz.

File metadata

  • Download URL: pycron-3.1.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.8.0-1014-azure

File hashes

Hashes for pycron-3.1.0.tar.gz
Algorithm Hash digest
SHA256 22590aa84e14d518fd9939cb3dd3720220e9fb006dd9861fea790c30a87de2be
MD5 151b6ebef4a9a750fc513596f7878704
BLAKE2b-256 5cadfe59ac3ef75f47bd1dc3ed601e7735587154d2958c26bcc59d7ef6d68159

See more details on using hashes here.

File details

Details for the file pycron-3.1.0-py3-none-any.whl.

File metadata

  • Download URL: pycron-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.8.0-1014-azure

File hashes

Hashes for pycron-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c7f4bb0d7f69c999add5fff3e6f6e56b4b8d6e2cfe6db6509ee9917cc7dff4b
MD5 62ff71457b9fe0cd1224722404d81b65
BLAKE2b-256 30a5b76e35e7fbaf1ec38ec4daab648e7f4f50f482cc3ba67fb3994e6da75cf5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page