Salamek / cron-descriptor

A Python library that converts cron expressions into human readable strings.
MIT License
160 stars 38 forks source link

issue with bad cron entries #51

Closed amir-khamis closed 2 years ago

amir-khamis commented 3 years ago

Hello, I was wondering if you can possibly help me figure out the best way to avoid bad cron entries like this one:"3-59/10 * 1-7" the code bombs out and it does not continue processing. is there a way for me to improve the code on my side to avoid this issue? perhaps I can improve the for loop to avoid processing a bad entry like the example I provided? any help is greatly appreciated and thank you so much for this amazing library, it is so important!

here is the code I using:

!/usr/bin/env python

import csv import sys

from cron_descriptor import ExpressionDescriptor, Options, DescriptionTypeEnum, CasingTypeEnum

expressionField = "cron_expression" humanReadableField = "cron_human_readable"

def main(): infile = sys.stdin outfile = sys.stdout

r = csv.DictReader(infile)
header = r.fieldnames

w = csv.DictWriter(outfile, r.fieldnames)
w.writeheader()

options = Options()
options.verbose = True
options.throw_exception_on_parse_error = True
options.casing_type = CasingTypeEnum.Sentence

for result in r:
    cron_expression = result[expressionField]
    descripter = ExpressionDescriptor(cron_expression, options)
    result[humanReadableField] = str(descripter.get_description(DescriptionTypeEnum.FULL))
    w.writerow(result)

main()

Salamek commented 2 years ago

Just handle raised exceptions in your code to allow it to continue

for result in r:
    cron_expression = result[expressionField]
    try:
        descripter = ExpressionDescriptor(cron_expression, options)
        result[humanReadableField] = str(descripter.get_description(DescriptionTypeEnum.FULL))
    except Exception:
        result[humanReadableField] = '???'
    w.writerow(result)