#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0

    Get list of differences


    Hi Guys,

    I want to compare atrib values for two instances of my object and return a list of the atribs that differ.

    so assume a class object as show below, If I create two instances of this class for two different individuals, is there any way I can easily compare the instances and get the a list of attributes that differ without having to comapre each attribute manually and changing this method everytime a new attribute is added

    Code:
    class myClass(object):
       def __init__(self, fname, lname, dob,phn, street, city, state)
            FName           = fname
            LName           = lname
            Dob               = dob
            Phone            = phn
            StreetAddress =street
            City               = city
            State             = state
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481

    Yes, use attributes in __dict__


    Code:
    class PeskyError(Exception):
        pass
    
    def set_of_attributes(a):
        '''
            argument is a dictionary,
            return set of the keys in argument for which the values are not callable
    
            >>> set_of_attributes({1:int,2:2}) == set([2])
            True
        '''
        return set(k for (k, v,) in a.items() if not callable(v))
    
    def diff(*args):
        '''
            report difference between attributes of the two objecs in the arguments
    
            >>> class C:
            ...     def __init__(self, **kwargs):
            ...         self.__dict__.update(kwargs)
            ... 
            >>> o = C(a=1,b=2,c=3)
            >>> p = C(a=1,b=3,d=4)
            >>> diff(o, p)
            common attributes  with different values: ['b']
            attributes of first object not in second: ['c']
            attributes of second object not in first: ['d']
        '''
        if len(args) != 2:
            raise PeskyError('diff: requires 2 arguments')
        (a, b,) = (o.__dict__ for o in args)
        sa = set_of_attributes(a)
        sb = set_of_attributes(b)
        common_keys = []
        for key in sa.intersection(sb):
            if a[key] != b[key]:
                common_keys.append(key)
        if common_keys:
            common_keys.sort()
            print('common attributes  with different values: {}'.format(common_keys))
        amb = list(sa - sb)
        if amb:
            amb.sort()
            print('attributes of first object not in second: {}'.format(amb))
        bma = list(sb - sa)
        if bma:
            bma.sort()
            print('attributes of second object not in first: {}'.format(bma))
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Thanks a million, exactly what I was looking for

    Originally Posted by b49P23TIvg
    Code:
    class PeskyError(Exception):
        pass
    
    def set_of_attributes(a):
        '''
            argument is a dictionary,
            return set of the keys in argument for which the values are not callable
    
            >>> set_of_attributes({1:int,2:2}) == set([2])
            True
        '''
        return set(k for (k, v,) in a.items() if not callable(v))
    
    def diff(*args):
        '''
            report difference between attributes of the two objecs in the arguments
    
            >>> class C:
            ...     def __init__(self, **kwargs):
            ...         self.__dict__.update(kwargs)
            ... 
            >>> o = C(a=1,b=2,c=3)
            >>> p = C(a=1,b=3,d=4)
            >>> diff(o, p)
            common attributes  with different values: ['b']
            attributes of first object not in second: ['c']
            attributes of second object not in first: ['d']
        '''
        if len(args) != 2:
            raise PeskyError('diff: requires 2 arguments')
        (a, b,) = (o.__dict__ for o in args)
        sa = set_of_attributes(a)
        sb = set_of_attributes(b)
        common_keys = []
        for key in sa.intersection(sb):
            if a[key] != b[key]:
                common_keys.append(key)
        if common_keys:
            common_keys.sort()
            print('common attributes  with different values: {}'.format(common_keys))
        amb = list(sa - sb)
        if amb:
            amb.sort()
            print('attributes of first object not in second: {}'.format(amb))
        bma = list(sb - sa)
        if bma:
            bma.sort()
            print('attributes of second object not in first: {}'.format(bma))

IMN logo majestic logo threadwatch logo seochat tools logo