473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

datetime.iterdate

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
fu******@amor.org
Jul 18 '05 #1
5 2056
"Robert Brewer" <fu******@amor.org> wrote in message news:<ma*************************************@pyth on.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)

Robert Brewer
MIS
Amor Ministries
fu******@amor.org


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
Jul 18 '05 #2
On Sun, 11 Jul 2004, Robert Brewer wrote:
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.

Jul 18 '05 #3
On Mon, 12 Jul 2004 09:20:38 -0400, rumours say that Christopher T King
<sq******@WPI.EDU> might have written:

[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...
--
TZOTZIOY, I speak England very best,
"Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek
Jul 18 '05 #4
On Mon, 12 Jul 2004, Christos TZOTZIOY Georgiou wrote:
On Mon, 12 Jul 2004 09:20:38 -0400, rumours say that Christopher T King
<sq******@WPI.EDU> might have written:

[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_ret urned_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

Jul 18 '05 #5
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.
"Robert Brewer" <fu******@amor.org> wrote in message
news:ma*************************************@pytho n.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)

Robert Brewer
MIS
Amor Ministries
fu******@amor.org
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Max M | last post by:
# -*- coding: latin-1 -*- """ I am currently using the datetime package, but I find that the design is oddly asymmetric. I would like to know why. Or perhaps I have misunderstood how it...
16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
15
by: Fritz Switzer | last post by:
I'd like to have a string assigned the value of a DateTime.AddMinutes(amount) so that the string is formatted in "HH:MM" format. For example: DateTime.Now.AddMinutes(30) returns "00:30" ...
3
by: Andrew S. Giles | last post by:
Hello, I am importing a flat text file, and putting it into a datagrid for display on a form. Currently the users have their dates and times seperated. I have two fields, therefore in the...
6
by: Ante Perkovic | last post by:
Hi, How to declare datetime object and set it to my birthday, first or last day of this month or any other date. I can't find any examples in VS.NET help! BTW, what is the difference...
5
by: I am Sam | last post by:
I have created this DateTime object and instanced it I think correctly DateTime myClubNow1=new...
11
by: Cor Ligthert | last post by:
Hello everybody, Jay and Herfried are telling me every time when I use CDate that using the datetime.parseexact is always the best way to do String to datetime conversions. They don't tell why...
9
by: Phil B | last post by:
I am having a problem with a datetime from a web services provider The provider is sending the following SOAP response <?xml version="1.0" encoding="utf-8"?> <soap:Envelope...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.