简体   繁体   English

是否有用于解析 AWS cron 字符串并将其转换为日历日期的 python 库?

[英]Is there a python library for parsing AWS cron strings and converting them to calendar dates?

I would like to take a list of all the cron expressions that the get back from calling a boto3 api call.我想列出从调用 boto3 api 调用返回的所有 cron 表达式。 Take take that list and convert it into calendar dates for the current month.获取该列表并将其转换为当月的日历日期。 This way I can use the dates for the backend to look at the events on a calendar.这样我就可以使用后端的日期来查看日历上的事件。

The current problem I am facing is that I can't find a library in python for parsing AWS cron strings and definitely not a library which does both parsing and cron to date conversion.我目前面临的问题是我在 python 中找不到用于解析 AWS cron 字符串的库,而且绝对不是同时进行解析和 cron 到日期转换的库。

Does anyone know of any python libraries for such work?有谁知道用于此类工作的任何 python 库吗?

AWS cron strings are different than cron/crontab. AWS cron 字符串不同于 cron/crontab。

https://docs.aws.amazon.com/glue/latest/dg/monitor-data-warehouse-schedule.html https://docs.aws.amazon.com/glue/latest/dg/monitor-data-warehouse-schedule.html

cron(Minutes Hours Day-of-month Month Day-of-week Year)

在此处输入图像描述

Wildcards通配符

The, (comma) wildcard includes additional values. ,(逗号)通配符包含附加值。 In the Month field, JAN,FEB,MAR would include January, February, and March.在月份字段中,JAN、FEB、MAR 将包括一月、二月和三月。

The - (dash) wildcard specifies ranges. -(破折号)通配符指定范围。 In the Day field, 1–15 would include days 1 through 15 of the specified month.在 Day 字段中,1–15 将包括指定月份的第 1 天到第 15 天。

The * (asterisk) wildcard includes all values in the field. *(星号)通配符包括字段中的所有值。 In the Hours field, * would include every hour.在小时字段中,* 将包括每小时。

The / (forward slash) wildcard specifies increments. /(正斜杠)通配符指定增量。 In the Minutes field, you could enter 1/10 to specify every 10th minute, starting from the first minute of the hour (for example, the 11th, 21st, and 31st minute).在分钟字段中,您可以输入 1/10 以指定每 10 分钟,从该小时的第一分钟开始(例如,第 11、21 和 31 分钟)。

The?这? (question mark) wildcard specifies one or another. (问号)通配符指定一个或另一个。 In the Day-of-month field you could enter 7, and if you didn't care what day of the week the seventh was, you could enter?在 Day-of-month 字段中可以输入 7,如果不关心第七天是星期几,可以输入? in the Day-of-week field.在“星期几”字段中。

The L wildcard in the Day-of-month or Day-of-week fields specifies the last day of the month or week. Day-of-month 或 Day-of-week 字段中的 L 通配符指定该月或该周的最后一天。

The W wildcard in the Day-of-month field specifies a weekday. Day-of-month 字段中的 W 通配符指定工作日。 In the Day-of-month field, 3W specifies the day closest to the third weekday of the month.在 Day-of-month 字段中,3W 指定最接近该月第三个工作日的日期。

Limits限制

You can't specify the Day-of-month and Day-of-week fields in the same cron expression.您不能在同一个 cron 表达式中指定 Day-of-month 和 Day-of-week 字段。 If you specify a value in one of the fields, you must use a?如果您在其中一个字段中指定一个值,则必须使用? (question mark) in the other. (问号)在另一个。

Cron expressions that lead to rates faster than 5 minutes are not supported.不支持导致速率快于 5 分钟的 Cron 表达式。

Work maybe in progress, porting one over to python工作可能正在进行中,将一个移植到 python

pyawscron-pitchblack408: pyawscron-pitchblack408:

https://github.com/pitchblack408/pyawscron https://github.com/pitchblack408/pyawscron

https://pypi.org/project/pyawscron/ https://pypi.org/project/pyawscron/

A python port from a typescript project:来自 typescript 项目的 python 端口:

https://github.com/beemhq/aws-cron-parser https://github.com/beemhq/aws-cron-parser

cron = '23,24,25 17,18 25 MAR/4 ? 2020,2021,2023,2028'
cron = AWSCron(cron)
dt = datetime.datetime(2020, 5, 9, 22, 30, 57, tzinfo=datetime.timezone.utc)
dt = cron.occurance(dt).next()

@pitchblack408, Kindly merge the pull request that I have raised after reviewing. @pitchblack408,请合并我在审查后提出的拉取请求。

pitchblack408 has done a great job porting over typescript project to Python. It is really helpful. pitchblack408在将 typescript 项目移植到 Python 上做得很好。这真的很有帮助。 However there is small typo in the README because of which I was scratching my head while using it.然而,README 中有一个小错字,因此我在使用它时摸不着头脑。 I had to read the whole source code and then finally got it.我不得不阅读整个源代码,然后终于明白了。

I have raised a pull request to fix the typo in the README as well as added few methods that will be really helpful to people using that.我提出了一个拉取请求来修复 README 中的拼写错误,并添加了一些对使用它的人真正有帮助的方法。 While that pull request is merged request everyone to try this below piece of code.虽然该拉取请求已合并,但请大家尝试下面的这段代码。

NOTE: The typo is in the occurrence method.注意:拼写错误在出现方法中。

    dt = cron.occurrence(dt).next()

Along with that here are the details of the methods I have added...除此之外,还有我添加的方法的详细信息......

Additional methods added get_all_schedule_bw_dates and get_next_n_schedule .添加了其他方法get_all_schedule_bw_datesget_next_n_schedule

These methods are very useful while using AWS cron schedule.这些方法在使用 AWS cron schedule 时非常有用。

For example...例如...

  1. When you need to get all possible execution dates between two dates, you can use get_all_schedule_bw_dates and compare whether the execution was done or not.当您需要获取两个日期之间所有可能的执行日期时,您可以使用get_all_schedule_bw_dates并比较执行是否完成。

     from_dt = datetime.datetime(2021, 8, 7, 8, 30, 57, tzinfo=datetime.timezone.utc) to_date = datetime.datetime(2021, 8, 7, 11, 30, 57, tzinfo=datetime.timezone.utc) AWSCron.get_all_schedule_bw_dates(from_dt, to_date, '0/23 * * *? *') # Outputs [datetime.datetime(2021, 8, 7, 8, 46, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 9, 0, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 9, 23, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 9, 46, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 10, 0, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 10, 23, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 10, 46, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 11, 0, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 11, 23, tzinfo=datetime.timezone.utc)]
  2. Sometimes you have to predict next few(say 5 or 10) executions based on a cron pattern.有时你必须根据 cron 模式预测接下来的几次(比如 5 或 10)执行。 In this case you can use get_next_n_schedule and pass n according to our requirements.在这种情况下,您可以使用get_next_n_schedule并根据我们的要求传递 n。

     from_dt = datetime.datetime(2021, 8, 7, 8, 30, 57, tzinfo=datetime.timezone.utc) AWSCron.get_next_n_schedule(10, from_dt, '0/23 * * *? *') #Outputs [datetime.datetime(2021, 8, 7, 8, 46, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 9, 0, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 9, 23, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 9, 46, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 10, 0, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 10, 23, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 10, 46, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 11, 0, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 11, 23, tzinfo=datetime.timezone.utc), datetime.datetime(2021, 8, 7, 11, 46, tzinfo=datetime.timezone.utc)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM