473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 5088
"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
# use the lambda instead
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Helmut Jarausch | last post by:
Hi, what does Python do if two objects aren't comparable (to my opinion) If I've understood "Python in a Nutschell" correctly it should raise an exception but it doesn't do for me. Here are...
3
by: Stephen | last post by:
I have to write a .Net application which can compare SQL Databases including things like: - DB structure, PK's, FK's, indexes and types of indexes i.e. should be able to detect if the same index...
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
19
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I...
4
by: Gaby | last post by:
Hi all, What is the best way to compare 2 (large) ArrayLists filled with an object. Can you please help me? Gaby
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
4
by: Lamis | last post by:
Hi, what is the best way to compare 2 haschtables contatining objects. the objects has 2 property, name & value. I need to print out the differences -- LZ
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
5
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.