News for hackers. Stuff that matters.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by vxxzy on Friday May 08, 2020 @05:40AM (#23112978)
Yes. Early on in my career, I needed a function that produced a range of dates, given a start and end date. After it was all said and done, it boiled out like so:

  def daterange(start,end):
      while start<=end:
          yield start
          start +=timedelta(days=1)
Though simplistic and straightforward, I admire the solution. I am sure there may be “better” ways to achieve the same result, and posting this here may result in cowboys trashing it or pointing out a fallacy - oh well... at the time, I was pretty satisfied with it and am still to this day.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by gumballhead on Friday May 08, 2020 @06:39AM (#23113280)
What's cool about this is now that you have a date generator, you can apply all kinds of fun things to your date ranges with Python's standard library.

Apply generator expressions, format them with map, filter out weekends, anything in itertools (dropwhile, takewhile), etc.

I wrote a datepicker to demonstrate some concepts like lazy evaluation with JavaScript generators and it was fun coming up with the test cases.

https://github.com/gumballhead/datepicker/blob/master/src/it...

Re:Ask HN: Do you ever go back and admire a piece of code yo... by mkl on Friday May 08, 2020 @06:38AM (#23113273)
Not trying to trash it, but doing

  dt = timedelta(days=1)
outside the loop and using it inside would make it a bit more efficient.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by the_pwner224 on Friday May 08, 2020 @09:42AM (#23114761)
I'm struggling to understand how this is special... isn't it basically just the normal 2-argument range function, but with date instead of int?

    def range(start, end):
     while start < end:
      yield start
      start += 1
Or is it more about the beauty of the algorithm than this specific code?
Re:Ask HN: Do you ever go back and admire a piece of code yo... by mywittyname on Friday May 08, 2020 @11:42AM (#23116025)
I think it's just the relative simplicity of code that handles date logic compared to other languages:

https://stackoverflow.com/questions/4345045/javascript-loop-...

I hate dealing with date math, but I have to admit that Python makes date math much more elegant. I've written the functional equivalent of the OP function several times, and it's just so nice to be able to loop over dates just like they were any other generic sequence.

Edit: plus you can enhance it pretty to make arbitrary increments with ease:

    def daterange(start, stop, increment='days', step=1):
         start = datetime.fromisoformat(start)
         stop = datetime.fromisoformat(stop)
         inc = {increment: step}
         while start <= stop:
             yield start
             start += timedelta(**inc)
         
    > list(daterange('2020-01-01', '2020-01-14'))
    [datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 2, 0, 0), datetime.datetime(2020, 1, 3, 0, 0), [...], datetime.datetime(2020, 1, 14, 0, 0)]
    > list(daterange('2020-01-01', '2020-01-14', 'weeks', 1))
    [datetime.datetime(2020, 1, 1, 0, 0), datetime.datetime(2020, 1, 8, 0, 0)]
Re:Ask HN: Do you ever go back and admire a piece of code yo... by maest on Saturday May 09, 2020 @06:40AM (#23123612)
Python's date handling is meh. Some languages have dates as first class primitives and are a dream to work with.
Re:Ask HN: Do you ever go back and admire a piece of code yo... by heavenlyblue on Friday May 08, 2020 @06:32AM (#23113245)
Are you proud of using a generator to create dates as you need? Just trying to understand the logic