datetime.iterdate
Robert Brewer
fumanchu at amor.org
Mon Jul 12 14:47:04 EDT 2004
More information about the Python-list mailing list
Mon Jul 12 14:47:04 EDT 2004
- Previous message (by thread): datetime.iterdate
- Next message (by thread): datetime.iterdate
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 at amor.org> wrote in message > news:mailman.236.1089594533.5135.python-list at 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 at amor.org
- Previous message (by thread): datetime.iterdate
- Next message (by thread): datetime.iterdate
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list