Compare recursively objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nicolas Fleury

    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
  • Paul McGuire

    #2
    Re: Compare recursively objects

    "Nicolas Fleury" <nid_oizo@yahoo .com_remove_the _> wrote in message
    news:LcZ5c.6697 $kc2.157952@nnr p1.uunet.ca...[color=blue]
    > 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[/color]
    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(ls t1, 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,l ist):
    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,l ist) 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(ls t1,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])


    Comment

    • Nicolas Fleury

      #3
      Re: Compare recursively objects

      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

      Comment

      • Paul McGuire

        #4
        Re: Compare recursively objects

        "Nicolas Fleury" <nid_oizo@yahoo .com_remove_the _> wrote in message
        news:gF_5c.6730 $kc2.158843@nnr p1.uunet.ca...[color=blue]
        > 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[/color]

        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


        Comment

        • Nicolas Fleury

          #5
          Re: Compare recursively objects

          Paul McGuire wrote:[color=blue]
          > 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.[/color]

          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

          Comment

          Working...