简体   繁体   English

在Python中解析人类可读的重复日期

[英]Parsing human-readable recurring dates in Python

The problem. 问题。 In my Django application, users create tasks for scheduled execution. 在我的Django应用程序中,用户为计划执行创建任务。 The users are quite non-technical, and it would be great if they can write conventional human-readable expressions to define when to execute certain task, such as: 用户是非技术性的,如果他们可以编写传统的人类可读表达式来定义何时执行某些任务,那将会很棒,例如:

  • every monday 每逢星期一
  • every fri, wed 每个朋友,星期三
  • daily 日常
  • 1, 14, 20 of each month 每月1,14,20
  • every fri; 每个朋友; every end of month 每个月末

This is inspired by Todoist . 这是受Todoist的启发。 For now, only dates are necessary; 目前,只需要日期; no times. 没有时间。 I've spent a couple of hours googling for a library to do that, but with no luck. 我花了几个小时用谷歌搜索图书馆来做这件事,但没有运气。 I am expecting a function, say, in_range(expression, date) , such that: 我期待一个函数,比如in_range(expression, date) ,这样:

>>> in_range('every monday, wednesday', date(2014, 4, 28))
True

>>> in_range('every end of month', date(2014, 5, 12))
False

>>> in_range('every millenium', date(2014, 5, 8))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unknown token "millenium".

Variants. 变种。 That's what I've looked through. 这就是我所看到的。

  • Standard datetime library does date parsing, but not date range parsing as per above. 标准datetime库执行日期解析,但不按日期解析日期范围。
  • Python-dateutil - supports recurring dates via rrule , very functional, but still does not support parsing. Python-dateutil - 通过rrule支持重复日期,非常实用,但仍不支持解析。
  • Python-crontab and Python-croniter accept standard Unix crontab syntax (and allow to specify weekdays, etc), but still such syntax is a way too technical and I'd like to avoid it if possible. Python-crontabPython-croniter接受标准的Unix crontab语法(并允许指定工作日等),但是这种语法仍然是一种技术性的方式,如果可能的话我想避免使用它。
  • Arrow and Parsedatetime do not support the feature. ArrowParsedatetime不支持该功能。

So, is there a Python code snippet, or a library that I missed, to do the thing? 那么,有没有Python代码片段或我错过的库来做这件事? If not, I'm going to write the parser myself. 如果没有,我将自己编写解析器。 Would like to release it in open source if it appears to be not too bad. 如果它看起来不是太糟糕,想在开源中发布它。

Recurrent is a library that will do natural language date parsing with support for recurring dates. Recurrent是一个库,它将进行自然语言日期解析,并支持重复日期。 It doesn't match the API you provided, but allows you to create rules that can be used with Python's datetime library. 它与您提供的API不匹配,但允许您创建可与Python的datetime库一起使用的规则。

From their Github page: 从他们的Github页面:

Natural language parsing of dates and recurring events 自然语言解析日期和重复事件

Examples 例子

Date times 日期时间

  • next tuesday 下个星期二
  • tomorrow 明天
  • in an hour 一个小时内

Recurring events 重复发生的事件

  • on weekdays 在工作日
  • every fourth of the month from jan 1 2010 to dec 25th 2020 从2010年1月1日到2020年12月25日,每月四分之一
  • each thurs until next month 每个人一直到下个月
  • once a year on the fourth thursday in november 11月的第四个星期四,每年一次
  • tuesdays and thursdays at 3:15 星期二和星期四3点15分

Messy strings 凌乱的弦乐

  • Please schedule the meeting for every other tuesday at noon 请在周二中午每隔一天安排一次会议
  • Set an alarm for next tuesday at 11pm 下周二晚上11点设置闹钟

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

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