gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:02:04 25 4
gpt4 key购买 nike

问题。在我的 Django 应用程序中,用户创建任务以按计划执行。用户是非技术人员,如果他们可以编写常规的人类可读表达式来定义何时执行某些任务,那就太好了,例如:

  • 每个星期一
  • 每周五、周三
  • 每日
  • 每月 1、14、20 日
  • 每个星期五;每月底

这是受 Todoist 的启发.现在,只需要日期;没有时间。我花了几个小时在谷歌上搜索图书馆来做到这一点,但没有运气。我期待一个函数,比如 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".

变体。这就是我看过的内容。

  • 标准 datetime 库进行日期解析,但不进行上述日期范围解析。
  • Python-dateutil - 通过rrule 支持循环日期,非常实用,但仍然不支持解析。
  • Python-crontabPython-croniter接受标准的 Unix crontab 语法(并允许指定工作日等),但这种语法仍然过于技术化,我希望尽可能避免使用它。
  • ArrowParsedatetime不支持该功能。

那么,是否有 Python 代码片段或我错过的库来完成这件事?如果没有,我将自己编写解析器。如果它看起来还不错,我想以开源方式发布它。

最佳答案

Recurrent是一个将进行自然语言日期解析并支持重复日期的库。它与您提供的 API 不匹配,但允许您创建可与 Python 的 datetime 库一起使用的规则。

来自他们的 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
  • each thurs until next month
  • once a year on the fourth thursday in november
  • tuesdays and thursdays at 3:15

Messy strings

  • Please schedule the meeting for every other tuesday at noon
  • Set an alarm for next tuesday at 11pm

关于python - 在 Python 中解析人类可读的重复日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23312829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com