[Python-Dev] Re: dateutil

Michael Chermside mcherm at mcherm.com
Tue Mar 16 08:23:16 EST 2004


Greg Ewing writes:
> Paul Moore <pf_moore at yahoo.co.uk>:
> >     dateutil.adjust(d, day=1)
> >     dateutil.adjust(d, day=-1)
> >     dateutil.adjust(d, day=1, months=+1, days=-1)
> > 
> > Does this look any better?
> 
> Yes, I think it does. At least it avoids giving any spurious
> impression that there might be an algebra lurking around
> somewhere.

+0.6  I do not feel as strongly as Greg does that use of +
      implies an alegbra (because it's glaringly obvious to
      me that the whole POINT of this library is to deal
      with the fact that date manipulations do NOT follow
      any sane algebra). However, there's some point to
      what he says and I like the function approach better.

Greg continues:
> It might be better to have separate functions for absolute
> and relative adjustments, e.g.
> 
>   dateutil.set(d, day=1)
>   dateutil.add(d, months=+1, days=-1)
> 
> since the difference between the singular and plural keywords
> is rather subtle and easy to miss.

+1.0  ABSOLUTELY! The singular vs plural keywords is downright
      misleading, while the use of "set()" vs "add()" adds
      greatly to clarity of intent.

I still feel that there is a danger of great confusion when we
use keyword parameters to specify various transforms that are
to be performed, but the order in which they are applied matters.

  EXAMPLE (NOT tested code, I hope I've got this right):
    >>> mar1 = datetime.datetime(2004,3,1)
    >>> mar1 + relativedelta(months=1) + relativedelta(days=-1)
    datetime.datetime(2004,3,31)
    >>> mar1 + relativedelta(day=-1) + relativedelta(month=1)
    datetime.datetime(2004,3,29)
    >>> mar1 + relativedelta(month=1, day=-1)  # I'm not sure of this one
    datetime.datetime(2004,3,31)

I probably would have tried a somewhat different design. Perhaps
individual transform objects which do just one thing at a time but
can be combined into a single object which applies them in series:

  HYPOTHETICAL SYNTAX:
    >>> mar1 = datetime.datetime(2004,3,1)
    >>> nextMonth = relativeDateDelta(months, 1)
    >>> prevDay = relativeDateDelta(days, -1)
    >>> mar1.addDelta(nextMonth).addDelta(prevDay)
    datetime.datetime(2004,3,31)
    >>> mar1.addDelta(prevDay).addDelta(nextMonth)
    datetime.datetime(2004,3,29)
    >>> firstCombo = relativeDateDelta([nextMonth, prevDay])
    >>> secondCombo = relativeDateDelta([prevDay, nextMonth])
    >>> # or, alternative syntax:
    >>> firstCombo = nextMonth.andThen(prevDay)

Anyway, although I do see some problems with the current syntax, and
I'm having fun playing around with ways that I think I might make
it better, I realize that my ideas haven't been tried out in real
code like Gustavo's. And what we're arguing here is syntax, not
functionality... I think it is clear that the functionality would be
a nice thing to have in the standard library if the api is acceptable
and the documentation is sufficient. I would urge Gustavo to listen
carefully to the ideas here (this discussion thread, not my post) and
see if there aren't some ideas which would improve his package.

-- Michael Chermside




More information about the Python-Dev mailing list