Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > no more comparisons

Reply
Thread Tools

no more comparisons

 
 
Carl Banks
Guest
Posts: n/a
 
      03-13-2008
On Mar 13, 12:38 pm, Alan Isaac <(E-Mail Removed)> wrote:
> Dan Bishop wrote:
> > def cmp_key(cmp_fn):
> > class CmpWrapper(object):
> > def __init__(self, obj):
> > self.obj = obj
> > def __cmp__(self, other):
> > return cmp_fn(self.obj, other.obj)
> > return CmpWrapper

>
> Apparently I'm overlooking something obvious ...
>
> how is this supposed to work if __cmp__ is no longer
>
> being called? (Which was my understanding.)



It won't. In Python 3.0 you'd have to write this class in terms of
rich comparisons (__lt__, __gt__, etc.).


Carl Banks
 
Reply With Quote
 
 
 
 
Alan Isaac
Guest
Posts: n/a
 
      03-13-2008
>> Dan Bishop wrote:

>>> def cmp_key(cmp_fn):


>>> class CmpWrapper(object):


>>> def __init__(self, obj):


>>> self.obj = obj


>>> def __cmp__(self, other):


>>> return cmp_fn(self.obj, other.obj)


>>> return CmpWrapper








> On Mar 13, 12:38 pm, Alan Isaac wrote:


>> how is this supposed to work if __cmp__ is no longer


>> being called? (Which was my understanding.)






Carl Banks wrote:

> It won't. In Python 3.0 you'd have to write this class in terms of


> rich comparisons (__lt__, __gt__, etc.).






Exactly. So something simple (define an anonymous function)

has become a bit of a pain.



On the other hand, I've looked through my extant code and

have not found a use of ``cmp`` that I cannot work around.

So maybe this is not as bad as I feared. What are some use

cases that will clearly be harder (i.e., at least require

a slightly elaborate wrapper) after this change?



Cheers,

Alan Isaac


 
Reply With Quote
 
 
 
 
Mark Dickinson
Guest
Posts: n/a
 
      03-13-2008
On Mar 13, 3:48*pm, Alan Isaac <(E-Mail Removed)> wrote:
> So maybe this is not as bad as I feared. *What are some use
>
> cases that will clearly be harder (i.e., at least require
>
> a slightly elaborate wrapper) after this change?


Sorting tuples, where the second item in the tuple should
have the opposite ordering to the first is going to be a bit
of a pain. Or even worse, where the ordering of the second
item depends on the value of the first item in the tuple.

For example, suppose that (for whatever contrived reason)
you're representing integers in (sign, magnitude) format
by tuples (s, i), where s = 0 or 1 (0 for positive, 1 for
negative) and i is a string representing the absolute
value of the integer. So

(0, '3') represents 3
(1, '14') represents 14

and you want to sort according to numeric value. One
can certainly come up with a key that works, but it's
not quite as straightforward as writing a custom __cmp__.

This isn't a totally contrived example: the internal
Decimal format isn't so different from this.

Mark
 
Reply With Quote
 
Mark Dickinson
Guest
Posts: n/a
 
      03-13-2008
On Mar 13, 5:10*pm, Mark Dickinson <(E-Mail Removed)> wrote:

> (1, '14') represents 14


should be -14, of course.
 
Reply With Quote
 
Alan Isaac
Guest
Posts: n/a
 
      03-14-2008
Mark Dickinson wrote:

> Sorting tuples, where the second item in the tuple should


> have the opposite ordering to the first is going to be


> a bit of a pain. Or even worse, where the ordering of the


> second item depends on the value of the first item in the


> tuple.




This is like some examples where I had used cmp,

but if I understand correctly I think it is not a problem.



> For example, suppose that (for whatever contrived reason)


> you're representing integers in (sign, magnitude) format


> by tuples (s, i), where s = 0 or 1 (0 for positive, 1 for


> negative) and i is a string representing the absolute


> value of the integer. So




Does this do it? ::



key= lambda x: (-x[1],int(x2))



Here I am depending on the lexicographic sorting of tuples.
Without that there would be real trouble.



Cheers,

Alan Isaac


 
Reply With Quote
 
Dan Bishop
Guest
Posts: n/a
 
      03-14-2008
On Mar 13, 7:38 pm, Alan Isaac <(E-Mail Removed)> wrote:
> Mark Dickinson wrote:
> > Sorting tuples, where the second item in the tuple should
> > have the opposite ordering to the first is going to be
> > a bit of a pain. Or even worse, where the ordering of the
> > second item depends on the value of the first item in the
> > tuple.

>
> This is like some examples where I had used cmp,
>
> but if I understand correctly I think it is not a problem.
>
> > For example, suppose that (for whatever contrived reason)
> > you're representing integers in (sign, magnitude) format
> > by tuples (s, i), where s = 0 or 1 (0 for positive, 1 for
> > negative) and i is a string representing the absolute
> > value of the integer. So

>
> Does this do it? ::
>
> key= lambda x: (-x[1],int(x2))
>
> Here I am depending on the lexicographic sorting of tuples.
> Without that there would be real trouble.


Even assuming you meant (-x[0], int(x[1])), that sorts negative
numbers in the wrong order. You want

key = lambda x: (-1 if x[0] else 1) * int(x[1])
 
Reply With Quote
 
Mark Dickinson
Guest
Posts: n/a
 
      03-14-2008
On Mar 13, 8:38*pm, Alan Isaac <(E-Mail Removed)> wrote:
> Does this do it? ::
>
> * * key= lambda x: (-x[1],int(x2))
>
> Here I am depending on the lexicographic sorting of tuples.
> Without that there would be real trouble.


Close.

I think it needs to be something like:

key = lambda x: (-x[0], int(x[1]) if x[0] == 0 else -int(x[1]))

Relying on lexicographic sorting of tuples seems okay to me.

What bugs me more about this example is that one has to rely
on the existence of a way to negate the usual ordering. This
is fine for numbers (where you can use the unary - operator)
or for numeric strings (where you can convert to int and then
use -), but it's not too much of a stretch to imagine cases
where neither of those options is available (e.g. non-numeric
strings), and where one ends up moving further and further
from readable code and into the realm of arcane trickery...

Mark
 
Reply With Quote
 
Alan Isaac
Guest
Posts: n/a
 
      03-14-2008
Dan Bishop wrote:

> Even assuming you meant (-x[0], int(x[1])), that sorts negative


> numbers in the wrong order. You want


> key = lambda x: (-1 if x[0] else 1) * int(x[1])






Sorry, was speed typing (very badly) and the question

actually had two different problem descriptions.

As you suggest, what I meant was::



key= lambda x: (-x[0], int(x[1]))



which was meant to address:



"Sorting tuples, where the second item in the tuple

should have the opposite ordering to the first is going

to be a bit of a pain."



But you are quite right, the problem was then specified to

numerical order, for which I like your solution. Or even::



key= lambda x: -int(x[1]) if x[0] else int(x[1])





The key point being, if you will, that this use case does

not support the idea that relying on ``key`` will be so bad.

So, what is a case that is really uncomfortable?



Thanks,

Alan Isaac


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Proposal: More flexible comparisons Ioannis Vranos C++ 22 06-29-2009 06:50 PM
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
With a Ruby Yell: more, more more! Robert Klemme Ruby 5 09-29-2005 06:37 AM
Server Comparisons Linuxserverguru Cisco 0 11-22-2003 03:22 PM
More comparisons of DSLRs Mike Engles Digital Photography 23 11-14-2003 07:11 AM



Advertisments