[CentralOH] Python Puzzle: Day generator
Eric Floehr
eric at intellovations.com
Thu Feb 5 02:55:14 CET 2015
Here is a group puzzle. I am interested in everyone's solutions. I will
post a link to my solution, which can also be refactored if you'd like a
refactoring challenge.
My timelapse code has a lot of places where I need to iterate through days.
So I thought to make a function that works like range() only with dates.
Here is the spec:
1. Takes an optional start date, end date, and step.
2. Start date and end date can either be python date objects or strings
of form "YYYY-MM-DD"
3. Step is number of days between steps, negative means step backwards
4. If end date is greater than start date and step is not specified,
step will be 1
5. If start date is greater than end date and step is not specified,
step will be -1
6. If end date is not specified, it will be the same as start date
7. If start date in not specified, it will be yesterday
Examples:
>>> for day in day_generator('2014-03-01', '2014-03-10'):
... print day
2014-03-01
2014-03-02
2014-03-03
2014-03-04
2014-03-05
2014-03-06
2014-03-07
2014-03-08
2014-03-09
2014-03-10
>>> for day in day_generator('2014-11-16', '2014-10-31', -4):
... print day
2014-11-16
2014-11-12
2014-11-08
2014-11-04
2014-10-31
And my solution (open to refactoring):
https://gist.github.com/efloehr/3e1dd639f3c5c1b064b7
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20150204/1808d1f2/attachment.html>
More information about the CentralOH
mailing list