comparing DateTime instances

Steve Holden sholden at holdenweb.com
Fri Feb 1 18:32:40 EST 2002


"Mark McEahern" <mark at mceahern.com> wrote in message
news:mailman.1012602663.18405.python-list at python.org...
> I think I just need a sanity check.  I'm trying to compare DateTime
> instances (from the eGenix mx.DateTime package).
>
> $ python
> Python 2.2 (#1, Dec 31 2001, 15:21:18)
> [GCC 2.95.3-5 (cygwin special)] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> expected = "2003-01-31 14:13:13.65"
> >>> actual = "2003-01-31 14:13:13.65"
> >>> from mx.DateTime import DateFrom, cmp
> >>> e = DateFrom(expected)
> >>> a = DateFrom(actual)
> >>> cmp(a, e)
> 0
>
> I think this is telling me that a and e are equal.  I'm still trying to
get
> my brain to instantly recognize the seemingly flipped around semantics of
> cmp.  I understand that __cmp__ returns 0 for equality--and that 1 and -1
> are used to indicate less-than, greater-than ordering.  So too for cmp,
eh?
>
It is indeed telling you the DateTime values are equal. Did you think of
trying the equality operator? The point about __cmp__ is that [absent rich
comparisons] implementing it tends to make objects do the Right Thing when
queried by the usual comparison operators. So you would also discover:

>>> e < a
0
>>> e == a
1
>>> e > a
0

> So that would mean, if I were comparing two DateTime instances in a unit
> test and I wanted the test to fail if the dates were NOT equal, I might
say
> something like this:
>
> self.failIf(cmp(date1, date2))
>
Well, you might. Bit you should at least consider as an alternative
something like

self.failIf(date1 != date2)

> If date1 equals date2 (I know I can use accuracy to account for
fuzziness),
> cmp will return 0, which is the same as false, and therefore failIf passes
> because it only fails if the argument is true.
>
> The documentation for mx.DateTime's cmp seems to assume the reader
> understands __cmp__.

Well, cmp is actually just an interface to __cmp__, so that's probably true.
But the foremost thing to understand about cmp() is that it often isn't
encessary to use it!

> This is all we find there:
>
> "cmp(obj1,obj2,accuracy=0.0)
> Compares two DateTime[Delta] objects.
> If accuracy is given, then equality will result in case the absolute
> difference between the two values is less than or equal to accuracy."
>
> Perhaps this could expand on the actual set of results:
>
> -1 : obj1 is less than obj2
>  0 : obj1 is equal to obj2
>  1 : obj1 is greater than obj2
>
> It wouldn't seem to hurt and it might avoid someone else wondering the
same
> silly things I'm wondering.
>
It would be even nicer if it suggested that you could simply compare
DateTime objects like numerical values, which in fact you can. Then
intelligent newcomers such as yourself wouldn't find themselves
contemplating the bowels of the language when all that's required is a
straight smack on the head! I suspect you are going to enjoy Python.

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Author, "Python Web Programming": http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list