469,591 Members | 1,601 Online

# Compare recursively objects

Hi everyone,
Is there a way to compare recursively two objects (compare their
members recursively)? I'm only interested in equality or non-equality
(no need for lower-than...).

Thx and Regards,
Nicolas
Jul 18 '05 #1
4 4630
"Nicolas Fleury" <ni******@yahoo.com_remove_the_> wrote in message
news:Lc*******************@nnrp1.uunet.ca...
Hi everyone,
Is there a way to compare recursively two objects (compare their
members recursively)? I'm only interested in equality or non-equality
(no need for lower-than...).

Thx and Regards,
Nicolas

If you're comparing lists, here's an example (although list1==list2 also
works, see the verification in the test() function). If you're comparing
something else, the logic is similar.

This example also does short-circuiting, in that it stops testing for
equality once the first mismatch is found. Also, there is a slight
short-cut in isEqualElement, in comparison of two objects for equal identity
before testing for equal value.

-- Paul
def compareLists(lst1, lst2):

def isEqualElement(e1,e2):
print "Comparing",e1,"and",e2
if e1 is e2:
return True

if type(e1) is type(e2):
if isinstance(e1,list):
return compareLists(e1,e2)
else:
return e1 == e2
else:
return False

# or the same thing as a lambda, just remove '0' from name to
isEqualElement0 = (lambda e1,e2:
(e1 is e2) or
( ( type(e1) is type(e2) ) and
( ( isinstance(e1,list) and compareLists(e1,e2) ) or
(e1 == e2) )
)
)

if len(lst1) != len(lst2):
return False

for e1,e2 in zip(lst1,lst2):
if not isEqualElement(e1,e2):
return False

return True
def test(lst1, lst2):
print "Test:", lst1,"<->",lst2
print compareLists(lst1,lst2)
print lst1 == lst2
print
test([1,2,3], [1,2,3])
test([1,2,3], [1,2])
test([1,'2',3], [1,2,3])
test([1,2,[3,4,5],6], [1,2,[3,4,5],6])
test([1,2,[3,4],6], [1,2,[3,4,5],6])
test([1,2,[7,4,5],6], [1,2,[3,4,5],6])
Jul 18 '05 #2
Do you know if the use of pickle would work? If I dump two objects and
compare the files, do I have the assurance that the files are the same
if the recursive members are the same?

Regards,
Nicolas
Jul 18 '05 #3
"Nicolas Fleury" <ni******@yahoo.com_remove_the_> wrote in message
news:gF*******************@nnrp1.uunet.ca...
Do you know if the use of pickle would work? If I dump two objects and
compare the files, do I have the assurance that the files are the same
if the recursive members are the same?

Regards,
Nicolas

I am not very familiar with pickle, other than its purpose for persisting
object representations for later reconstitution. Given that, I suspect your
pickle-to-file-and-compare-the-files scheme would work.

However, I would be wary of a scheme that routinely used files for
temporary/scratch storage in this manner. There are just too many ordinary
ways for this to go wrong:
- disk has insufficient room to create files
- insufficient user privs to create files
- trying to create files in non-existent or protected directory
- program exits before scratch files get cleaned up, leaving behind growing
debris
- concurrent users end up using each others' files by mistake; accidentally
creating duplicate scratch file names
Plus, file i/o is just plain agonizingly slow.

In sum, I suspect you will get diverted to dealing with a whole bunch of
issues that are artifacts of using files, when what you really wanted to do
is compare some objects. Perhaps you could pickle to a string and compare
the strings? At least this avoids most of the file system complications.

-- Paul
Jul 18 '05 #4
Paul McGuire wrote:
In sum, I suspect you will get diverted to dealing with a whole bunch of
issues that are artifacts of using files, when what you really wanted to do
is compare some objects. Perhaps you could pickle to a string and compare
the strings? At least this avoids most of the file system complications.

Sounds like a good idea. I am only in need of that functionality for
unit testing, so I guess pickle to StringIO is a nice idea.

Thx
Nicolas
Jul 18 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.