datetime.iterdate

R

Robert Brewer

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)



Robert Brewer
MIS
Amor Ministries
(e-mail address removed)
 
M

Michele Simionato

Robert Brewer said:
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)



Robert Brewer
MIS
Amor Ministries
(e-mail address removed)

I actually asked the same four weeks ago (thread "datetime, calendar,
time intervals") since it is one of the first things I would have
expected to
find in the datetime module.

I don't see as a problem to add functions to modules (as opposed to
add new builtins which requires a lot of care). Is good to have even
simple functions in modules, if they are common, since you have the
advantage to have a stardard names for standard things, such as
interating
on time intervals. Of course, there should be the possibily of passing
a generic time step.


Michele Simionato
 
C

Christopher T King

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)

This is something that could be made simpler with a previous proposal I
had made to replace xrange():

for day in first_date<=datetime.days<=last_date:
do_something_with(day)

where datetime.days is an object representing the set of all days, and its
comparison functions return a subset of that set. To work perfectly, my
proposal needs only a slight change in the parser (to compile a<b<c as
(a<b)&(b<c) rather than a<b and b<c); I'm not sure how much this would
affect existing code though.
 
C

Christos TZOTZIOY Georgiou

[snip of a<=x<=b proposition to replace xrange]
To work perfectly, my
proposal needs only a slight change in the parser (to compile a<b<c as
(a<b)&(b<c) rather than a<b and b<c); I'm not sure how much this would
affect existing code though.

A lot of code is based on "x and y" evaluating y only if x is true...
 
C

Christopher T King

[snip of a<=x<=b proposition to replace xrange]
To work perfectly, my
proposal needs only a slight change in the parser (to compile a<b<c as
(a<b)&(b<c) rather than a<b and b<c); I'm not sure how much this would
affect existing code though.

A lot of code is based on "x and y" evaluating y only if x is true...

Oh no, I don't mean to replace "x and y" with "x&y", I mean to replace the
expansion of "x<y<z" (which is currently "x<y and y<z") with
"(x<y)&(y<z)". This /would/ slightly change the semantics of the x<y<z
construct, but I pity the developer that relies on the short-circuitting
of the evaluation of 'z' if y<x is False (even though it is guaranteed by
the docs); any code that would be broken would have to look something like
this:

if do_A()==True==do_something_requiring_A_to_have_returned_True():
print 'Success!'

Nevertheless, perhaps a better expansion of x<y<z that would preserve
the original semantics would be:

e=x<y
if e:
e&=y<z
return e
 
L

Larry Bates

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).

Larry Bates
Syscon, Inc.


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)



Robert Brewer
MIS
Amor Ministries
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top