Skip to main content

A simple Python Cron Expression lib

Project description

Python Cron Expression

This is a simple library for making cron job expression. User need to pass timestamp or datetime object to create cron job expression.

Installation

pip install PyCronExpression
Feature List
Name status
Timestamp or datetime object done
Minute done
Hourly done
Daily Done
Weekly Comming

Example

Timestamp or datetime object

from datetime import datetime
from py_cron_expression import CronJobExpression
cron_job = CronJobExpression()

timestamp = int(datetime.now().timestamp())
res = cron_job.cron_expression(time=timestamp)
print(res)
# Output 
# 10 55 13 2 5 ? 2020

# If you want to remove second from in your cron job expression. 
# Then you need to add cancel='second'
# example
cron_job.cron_expression(time=timestamp, cancel='second')
# Output 
# 55 13 2 5 ? 2020

If you need to convert the cron job expression to other timezone. Please follow the example. By default your will get the cron job expression as your local timezone.

from datetime import datetime
from py_cron_expression import CronJobExpression
cron_job = CronJobExpression()

timestamp = int(datetime.now().timestamp())
res = cron_job.cron_expression(time=timestamp, timezone='Asia/Dhaka')
print(res)
# Output 
# 10 55 13 2 5 ? 2020

Below features will give you two types of cron expression 1. AWS platform and 2. Linux. Both platforms are not supported second. So by default removed the second from the expression

Minute

from py_cron_expression import CronJobExpression
cron = CronJobExpression()
minute = cron.minute(platform="aws", minutes=10)
print(minute)
# */10 * * * ? * 
# Every 10 minutes starting at :00 minute after the hour
minute = cron.minute(platform="linux", minutes=10)
print(minute)
# */10 * * * *
# Every 10 minutes starting at :00 minute after the hour

Hourly

from py_cron_expression import CronJobExpression
cron = CronJobExpression()
hourly = cron.hourly(platform="aws", minutes=1)
print(hourly)
# * */1 * * ? *
# Every hour starting at 00am
hourly = cron.hourly(platform="linux", minutes=1)
print(hourly)
# * */1 * * *
# Every hour starting at 00am

Daily

from py_cron_expression import CronJobExpression
cron = CronJobExpression()
daily = cron.daily(platform="aws", minutes=1)
print(daily)
# 0 1 * * ? *
# At 01:00:00am every day
daily = cron.daily(platform="linux", minutes=1)
print(daily)
# 0 1 * * *
# At 01:00:00am every day

Explanation:

Linux platform Cron Expressions Definition (crontab)

# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
Linux platform cron job definition
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *

AWS Platform Cron Expressions Definition

# .---------------------- minute (0 - 59)
# |  .------------------- hour (0 - 23)
# |  |   .---------------- day of month (1 - 31)
# |  |  |   .------------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |   .---------- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |  .-------- year
# |  |  |  |  | |
# *  *  *  *  ? *

AWS Docs

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

PyCronExpression-0.0.6.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

PyCronExpression-0.0.6-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file PyCronExpression-0.0.6.tar.gz.

File metadata

  • Download URL: PyCronExpression-0.0.6.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for PyCronExpression-0.0.6.tar.gz
Algorithm Hash digest
SHA256 f61da53ba88a7057984d8c27b0cdd4ee7ab63aff5a65d5e5021ebfd8c154f2d8
MD5 e047f4ef8e583d2b4542d9c2ebbef183
BLAKE2b-256 7227fbc0b0290dc1737e9b0cec955cce4cd6de0e8ce4a9502765b6282ef9b0f9

See more details on using hashes here.

File details

Details for the file PyCronExpression-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: PyCronExpression-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for PyCronExpression-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0312276bf8affdaba5da25593f669577c6096d9da245ee3adc11741f69262699
MD5 90a694ed7f1428ad4029f0c141c42a48
BLAKE2b-256 ca36146fd1397a98facec839ff8a0c898830c68af8adc00fbe35fe0feb877785

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