RE: datetime.iterdate

From: Robert Brewer (fumanchu_at_amor.org)
Date: 07/12/04


Date: Mon, 12 Jul 2004 11:47:04 -0700
To: "Larry Bates" <lbates@swamisoft.com>, <python-list@python.org>

Larry Bates wrote:
> List comprehension to the rescue:
>
> day_range=[first+datetime.timedelta(x) for x in
> range((last-first).days+1))]
> for day in day_range:
> do_something_with(day)
>
> I'm not entirely sure the syntax is correct (I just
> copied yours for the example) , but you get the idea.
> I think it clearly defines the list of items you are
> iterating over and keeps the definition close to the
> loop where you do something (rather in a function
> that may be defined far away in the code).
>
> "Robert Brewer" <fumanchu@amor.org> wrote in message
> news:mailman.236.1089594533.5135.python-list@python.org...
> Anyone else tired of typing date-addition logic when
> iterating? It would
> be nice if the datetime package had something like:
>
> def iterdates(first, last):
> for day in range((last - first).days + 1):
> yield first + datetime.timedelta(day)
>
> ...notice the inclusive boundaries (i.e. last gets returned). This
> simple construct would make ugly date loops a lot cleaner:
>
> for day in datetime.iterdates(first_date, last_date):
> do_something_with(day)

Listcomps are nice tools, but don't address the "problem" I was trying
to "solve", which is ugliness. Listcomps just shuffle syntax; I'm trying
to hide some of it. By "hide", I mean, "encapsulate logic", in the sense
of building more powerful expressions out of intermediate
domain-appropriate components; "iterdates" is a candidate for that IMO.
All of which is to say that I _don't_ want the "definition close to the
loop".

I think I'd settle for Tim's suggestion to make the endpoint exclusive.
More like:

def daterange(start, end):
    for day in xrange((end - start).days):
        yield start + datetime.timedelta(day)

...which starts to lead toward questions of generating sequences in
general, which I think is worthy of some serious study, and probably a
PEP. Maybe someday we'll have a __range__ classmethod, or a "class"
argument to range(), or something. For now, I think datetime.daterange()
would be worth including, but if Michele and I are the only ones, I'll
stop pursuing it.

Robert Brewer
MIS
Amor Ministries
fumanchu@amor.org



Relevant Pages

  • Re: datetime.iterdate
    ... List comprehension to the rescue: ... I'm not entirely sure the syntax is correct (I just ... loop where you do something (rather in a function ... Anyone else tired of typing date-addition logic when iterating? ...
    (comp.lang.python)
  • Preview chapter about the structured syntax definition of Seed7
    ... I am writing a chapter about the structured syntax definition ... will be used as base to explain the S7SSD. ... that a new statement, the 'loop' statement, should be defined. ... Priority and assoziativity ...
    (comp.compilers)
  • Re: Am I the only one who would love these extentions? - Python 3.0 proposals (long)
    ... Why is it nice enough to make it be a syntax addition, ... to point an image viewer/editor at a chunk of Python code. ... You argue consistancy to other keywords. ... Why is this new loop construct of yours useful enough ...
    (comp.lang.python)
  • Re: How about this syntactic candy?
    ... I can think of that's worse than adding a rarely needed, rarely beneficial syntax is adding one that actually allows one to lie to the compiler in a way that _breaks_ the code. ... But more significantly, IMHO, the reason that syntax has survived so many generations of this family of languages is that it's so commonly useful. ... simple for loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: PEP-315 ("do" loop)
    ... But it's syntax seems like an acquired taste to me. ... Would be defined to work exactly like a while loop except the test is not ... and my news client breaks the formatting. ... "improving" the basic while loop, none of which seem to be a natural ...
    (comp.lang.python)