使用现有的 rrule 生成更多事件集

发布于 2024-07-10 03:36:59 字数 653 浏览 11 评论 0原文

我有一个 rrule 实例,例如

     r = rrule(WEEKLY, byweekday=SA, count=10, dtstart=parse('20081001'))

dtstart 和 byweekday 可能会改变。

如果我想生成接下来的十个日期 rrule,最好的方法是什么? 我可以分配一个新值吗 r 的 _dtstart 成员? 这似乎有效,但我不确定。

例如

     r._dtstart = list(r)[-1] or something like that

,否则我想我会创建一个新的 rrule 并访问原始实例的 _dtstart、_count、_byweekday 等。

编辑:

我已经考虑了更多,我认为我应该做的是在创建第一个 rrule 实例时省略“count”参数。 第一次使用 rrule 时,我仍然可以出现十次

instances = list(r[0:10]) 

,然后我可以得到更多,

more = list(r[10:20]) 

我认为这解决了我的问题,没有任何丑陋

I have an rrule instance e.g.

     r = rrule(WEEKLY, byweekday=SA, count=10, dtstart=parse('20081001'))

where dtstart and byweekday may change.

If I then want to generate the ten dates that follow on from this
rrule, what's the best way of doing it? Can I assign a new value to
the _dtstart member of r? That seems to work but I'm not sure.

e.g.

     r._dtstart = list(r)[-1] or something like that

Otherwise I guess I'll create a new rrule and access _dtstart, _count, _byweekday etc. of the original instance.

EDIT:

I've thought about it more, and I think what I should be doing is omitting the 'count' argument when I create the first rrule instance. I can still get ten occurrences the first time I use the rrule

instances = list(r[0:10]) 

and then afterwards I can get more

more = list(r[10:20]) 

I think that solves my problem without any ugliness

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就此别过 2024-07-17 03:36:59

首先,r._dtstart = list(r)[-1] 将为您提供原始日期序列中的最后一个日期。 如果您在不进行修改的情况下将其用于新序列的开头,则最终会得到重复的日期,即第一个序列的最后一个日期将与新序列的第一个日期相同,这可能不是你想要什么:

>>> from dateutil.rrule import *
>>> import datetime

>>> r = rrule(WEEKLY, byweekday=SA, count=10, dtstart=datetime.datetime(2008,10,01))
>>> print list(r)
[datetime.datetime(2008, 10, 4, 0, 0), datetime.datetime(2008, 10, 11, 0, 0), datetime.datetime(2008, 10, 18, 0, 0), datetime.datetime(2008, 10, 25, 0, 0), datetime.datetime(2008, 11, 1, 0, 0), datetime.datetime(2008, 11, 8, 0, 0), datetime.datetime(2008, 11, 15, 0, 0), datetime.datetime(2008, 11, 22, 0, 0), datetime.datetime(2008, 11, 29, 0, 0), datetime.datetime(2008, 12, 6, 0, 0)]
>>> r._dtstart = r[-1]
>>> print list(r)
[datetime.datetime(2008, 12, 6, 0, 0), datetime.datetime(2008, 12, 13, 0, 0), datetime.datetime(2008, 12, 20, 0, 0), datetime.datetime(2008, 12, 27, 0, 0), datetime.datetime(2009, 1, 3, 0, 0), datetime.datetime(2009, 1, 10, 0, 0), datetime.datetime(2009, 1, 17, 0, 0), datetime.datetime(2009, 1, 24, 0, 0), datetime.datetime(2009, 1, 31, 0, 0), datetime.datetime(2009, 2, 7, 0, 0)]

此外,操作 r._dtstart 被认为是很糟糕的形式,因为它显然是一个私有属性。

相反,执行如下操作:

>>> r = rrule(WEEKLY, byweekday=SA, count=10, dtstart=datetime.datetime(2008,10,01))
>>> r2 = rrule(WEEKLY, byweekday=SA, count=r.count(), dtstart=r[-1] + datetime.timedelta(days=1))
>>> print list(r)
[datetime.datetime(2008, 10, 4, 0, 0), datetime.datetime(2008, 10, 11, 0, 0), datetime.datetime(2008, 10, 18, 0, 0), datetime.datetime(2008, 10, 25, 0, 0), datetime.datetime(2008, 11, 1, 0, 0), datetime.datetime(2008, 11, 8, 0, 0), datetime.datetime(2008, 11, 15, 0, 0), datetime.datetime(2008, 11, 22, 0, 0), datetime.datetime(2008, 11, 29, 0, 0), datetime.datetime(2008, 12, 6, 0, 0)]
>>> print list(r2)
[datetime.datetime(2008, 12, 13, 0, 0), datetime.datetime(2008, 12, 20, 0, 0), datetime.datetime(2008, 12, 27, 0, 0), datetime.datetime(2009, 1, 3, 0, 0), datetime.datetime(2009, 1, 10, 0, 0), datetime.datetime(2009, 1, 17, 0, 0), datetime.datetime(2009, 1, 24, 0, 0), datetime.datetime(2009, 1, 31, 0, 0), datetime.datetime(2009, 2, 7, 0, 0), datetime.datetime(2009, 2, 14, 0, 0)]

此代码不会访问 rrule 的任何私有属性(尽管您可能需要查看 _byweekday)。

Firstly, r._dtstart = list(r)[-1] will give you the last date in the original sequence of dates. If you use that, without modification, for the beginning of a new sequence, you will end up with a duplicate date, i.e. the last date of the first sequence will be the same as the first date of the new sequence, which is probably not what you want:

>>> from dateutil.rrule import *
>>> import datetime

>>> r = rrule(WEEKLY, byweekday=SA, count=10, dtstart=datetime.datetime(2008,10,01))
>>> print list(r)
[datetime.datetime(2008, 10, 4, 0, 0), datetime.datetime(2008, 10, 11, 0, 0), datetime.datetime(2008, 10, 18, 0, 0), datetime.datetime(2008, 10, 25, 0, 0), datetime.datetime(2008, 11, 1, 0, 0), datetime.datetime(2008, 11, 8, 0, 0), datetime.datetime(2008, 11, 15, 0, 0), datetime.datetime(2008, 11, 22, 0, 0), datetime.datetime(2008, 11, 29, 0, 0), datetime.datetime(2008, 12, 6, 0, 0)]
>>> r._dtstart = r[-1]
>>> print list(r)
[datetime.datetime(2008, 12, 6, 0, 0), datetime.datetime(2008, 12, 13, 0, 0), datetime.datetime(2008, 12, 20, 0, 0), datetime.datetime(2008, 12, 27, 0, 0), datetime.datetime(2009, 1, 3, 0, 0), datetime.datetime(2009, 1, 10, 0, 0), datetime.datetime(2009, 1, 17, 0, 0), datetime.datetime(2009, 1, 24, 0, 0), datetime.datetime(2009, 1, 31, 0, 0), datetime.datetime(2009, 2, 7, 0, 0)]

Furthermore, it is considered poor form to manipulate r._dtstart as it is clearly intended to be a private attribute.

Instead, do something like this:

>>> r = rrule(WEEKLY, byweekday=SA, count=10, dtstart=datetime.datetime(2008,10,01))
>>> r2 = rrule(WEEKLY, byweekday=SA, count=r.count(), dtstart=r[-1] + datetime.timedelta(days=1))
>>> print list(r)
[datetime.datetime(2008, 10, 4, 0, 0), datetime.datetime(2008, 10, 11, 0, 0), datetime.datetime(2008, 10, 18, 0, 0), datetime.datetime(2008, 10, 25, 0, 0), datetime.datetime(2008, 11, 1, 0, 0), datetime.datetime(2008, 11, 8, 0, 0), datetime.datetime(2008, 11, 15, 0, 0), datetime.datetime(2008, 11, 22, 0, 0), datetime.datetime(2008, 11, 29, 0, 0), datetime.datetime(2008, 12, 6, 0, 0)]
>>> print list(r2)
[datetime.datetime(2008, 12, 13, 0, 0), datetime.datetime(2008, 12, 20, 0, 0), datetime.datetime(2008, 12, 27, 0, 0), datetime.datetime(2009, 1, 3, 0, 0), datetime.datetime(2009, 1, 10, 0, 0), datetime.datetime(2009, 1, 17, 0, 0), datetime.datetime(2009, 1, 24, 0, 0), datetime.datetime(2009, 1, 31, 0, 0), datetime.datetime(2009, 2, 7, 0, 0), datetime.datetime(2009, 2, 14, 0, 0)]

This codes does not access any private attributes of rrule (although you might have to look at _byweekday).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文