How to check something is in a list with rich-comparison objects?

J

Jason

Comparing a string to the enumerations in pysvn gives me an attribute
error, because they've overloaded the rich compare methods:
import pysvn
"string" in [pysvn.wc_notify_action.status_completed, "string"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: expecting wc_notify_action object for rich compare

Is there a simple way around this?

Thanks,
Jason Heeris
 
G

Gabriel Genellina

Comparing a string to the enumerations in pysvn gives me an attribute
error, because they've overloaded the rich compare methods:
import pysvn
"string" in [pysvn.wc_notify_action.status_completed, "string"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: expecting wc_notify_action object for rich compare

Is there a simple way around this?

Looks like a bug in pysvn. Some class (whatever
pysvn.wc_notify_action.status_completed is) is not well written. When
compared against something unknown, it should return NotImplemented
instead of raising AttributeError.

py> class BadBoy(object):
.... foo = 1
.... #
.... def __eq__(self, other):
.... if not isinstance(other, BadBoy):
.... raise TypeError, "expecting BadBoy object for rich compare"
.... return self.foo==other.foo
....
py> "hello" in [BadBoy(), "hello"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 6, in __eq__
TypeError: expecting BadBoy object for rich compare
py>
py> class GoodBoy(object):
.... foo = 1
.... #
.... def __eq__(self, other):
.... if not isinstance(other, GoodBoy):
.... return NotImplemented
.... return self.foo==other.foo
....
py> "hello" in [GoodBoy(), "hello"]
True
 
J

Jason

I will raise this with pysvn, if I can ever find their issue reporting
system.

In the meantime, I suppose I can only do this by traversing the list
with a loop and catching exceptions. Ugly, but seems to be the only
way.
 
G

Gabriel Genellina

(top posting corrected)
I will raise this with pysvn, if I can ever find their issue reporting
system.

In the meantime, I suppose I can only do this by traversing the list
with a loop and catching exceptions. Ugly, but seems to be the only
way.

You might use a list subclass and override its __contains__ method (and
perhaps index() too). It's the same ugly code, but at least expressions
like:

'foo' in some_list

would still work.
 
S

Steven D'Aprano

(top posting corrected)


You might use a list subclass and override its __contains__ method (and
perhaps index() too). It's the same ugly code, but at least expressions
like:

'foo' in some_list

would still work.


Or you can wrap each item in an object that does the right thing.

# untested
class Wrapper:
# Thin wrapper to make equals comparisons work correctly
def __init__(self, payload):
self.payload = payload
def __eq__(self, other):
payload = self.payload
try:
return payload.__eq__(other)
except AttributeError:
return other.__eq__(payload)

some_list = map(Wrapper, some_list)
 
T

Terry Reedy

Jason said:
I will raise this with pysvn, if I can ever find their issue reporting
system.

In the meantime, I suppose I can only do this by traversing the list
with a loop and catching exceptions. Ugly, but seems to be the only
way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top