Hi everybody

 

Recently I needed a way to compare the contents of two elements of the
same class (that was of course generated from a schema).

The default comparison of python is to check if the instances are the
same.

The way that this is fixed is by adding a __eq__ method to the class.

Because my class structure is a bit big, I've written a function that
takes two elements and checks them recursively (Note that I've made an
assumption that the members of the class begin with a capital letter,
and that sequences hold the members in the same locations):

def assertEqual(a,b):

            if (type(a) != type(b)):

                        raise "types
differ:"+repr(type(a))+","+repr(type(b))

            if (a != b):

                        raise "values differ:"+repr(a)+","+repr(b)

 

def isSeq(seq):

            return type(seq)==type([])

                        

def compareContents(a,b):

            aMembers = [x for x in dir(a) if x < '_']

            bMembers = [x for x in dir(b) if x < '_']

            assertEqual(aMembers,bMembers)

            members = aMembers

            if (len(members)==0):

                        assertEqual(a,b)

            for member in members:

                        if (isSeq(getattr(a,member))):

 
assertEqual(len(getattr(a,member)),len(getattr(b,member)))

                                    for i in
range(len(getattr(a,member))):

 
compareContents(getattr(a,member)[i],getattr(b,member)[i])

                        else:

 
compareContents(getattr(a,member),getattr(b,member))

 

How hard will it be to add a __eq__ method to the generated classes, one
that will just compare all the members (also the value of the class, if
it exists) and return true/false accordingly?

 

 

Benny.

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
generateds-users mailing list
generateds-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/generateds-users

Reply via email to