Diff a nested dictionary, returning only key/values that differ.
174474
def _diff_dict(orig, new):
    
    final = {}
    for k, v in new.items():
        if isinstance(v, dict):
            v = _diff_dict(orig.get(k, {}), v)
            if len(v) > 0:
                final[k] = v
        elif v != orig.get(k):
            final[k] = v
    for k, v in orig.items():
        if k not in new:
            final[k] = None
    return final
1084751
def _create_diff_action(diff, diff_key, key, value):

    '''
    
    '''

    if diff_key not in diff.keys():
        diff[diff_key] = {}
    diff[diff_key][key] = value
1026532
def compare(dicts):
    """"""

    common_members = {}
    common_keys = reduce(lambda x, y: x & y, map(dict.keys, dicts))
    for k in common_keys:
        common_members[k] = list(
            reduce(lambda x, y: x & y, [set(d[k]) for d in dicts]))

    return common_members
551329
def dict_match(a, b):
    
    res = []
    for k, v in a.items():
        try:
            res.append(b[k] == v)
        except KeyError:
            pass
    return all(res)
926185
def _get_recursive_difference(self, type):
        ''''''
        if type == 'intersect':
            return [recursive_diff(item['old'], item['new']) for item in self._intersect]
        elif type == 'added':
            return [recursive_diff({}, item) for item in self._added]
        elif type == 'removed':
            return [recursive_diff(item, {}, ignore_missing_keys=False)
                    for item in self._removed]
        elif type == 'all':
            recursive_list = []
            recursive_list.extend([recursive_diff(item['old'], item['new']) for item in self._intersect])
            recursive_list.extend([recursive_diff({}, item) for item in self._added])
            recursive_list.extend([recursive_diff(item, {},
                                                  ignore_missing_keys=False)
                                   for item in self._removed])
            return recursive_list
        else:
            raise ValueError('The given type for recursive list matching '
                             'is not supported.')
1366225
def compare_dicts(old=None, new=None):
    '''
    
    '''
    ret = {}
    for key in set((new or {})).union((old or {})):
        if key not in old:
            # New key
            ret[key] = {'old': '',
                        'new': new[key]}
        elif key not in new:
            # Key removed
            ret[key] = {'new': '',
                        'old': old[key]}
        elif new[key] != old[key]:
            # Key modified
            ret[key] = {'old': old[key],
                        'new': new[key]}
    return ret
422394
def diff(self, *args):
        """"""
        arg_dicts = self._parse_args_forward_mode(*args)
        val, diff = self._forward_mode(*arg_dicts)
        return diff
747649
def _recursive_compare(v1, v2):
    '''
    
    '''
    if isinstance(v1, list):
        if v2 is None:
            v2 = []
        if len(v1) != len(v2):
            return False
        v1.sort(key=_id_or_key)
        v2.sort(key=_id_or_key)
        for x, y in zip(v1, v2):
            if not _recursive_compare(x, y):
                return False
        return True
    elif isinstance(v1, dict):
        if v2 is None:
            v2 = {}
        v1 = dict(v1)
        v2 = dict(v2)
        if sorted(v1) != sorted(v2):
            return False
        for k in v1:
            if not _recursive_compare(v1[k], v2[k]):
                return False
        return True
    else:
        return v1 == v2
1297990
def diff(*args)
      args.unshift(parents.first) if args.size == 1 && args.first.is_a?(Hash)
      self.tree.diff(*args)
    end
294833
function equalDiff(a, b, diff) {
  diff.a = {
    line: a.line,
    column: a.column
  };
  diff.b = {
    line: b.line,
    column: b.column
  };
  return false;
}
1114819
def _get_diff_dict(dict1, dict2):
    '''
    
    '''
    ret_dict = {}
    for p in dict2.keys():
        if p not in dict1:
            ret_dict.update({p: {'val1': None, 'val2': dict2[p]}})
        elif dict1[p] != dict2[p]:
            if isinstance(dict1[p], dict) and isinstance(dict2[p], dict):
                sub_diff_dict = _get_diff_dict(dict1[p], dict2[p])
                if sub_diff_dict:
                    ret_dict.update({p: sub_diff_dict})
            else:
                ret_dict.update({p: {'val1': dict1[p], 'val2': dict2[p]}})
    return ret_dict
606459
function diff( a, b ) {
  let i, j, found = false, result = [];

  for ( i = 0; i < a.length; i++ ) {
    found = false;
    for ( j = 0; j < b.length; j++ ) {
      if ( a[ i ] === b[ j ] ) {
        found = true;
        break;
      }
    }
    if ( !found ) {
      result.push( a[ i ] );
    }
  }

  return result;
}
329626
def get_diff(a, b, *args, **kwargs):
    '''
    
    '''
    encoding = ('utf-8', 'latin-1', __salt_system_encoding__)
    # Late import to avoid circular import
    import salt.utils.data
    return ''.join(
        difflib.unified_diff(
            salt.utils.data.decode_list(a, encoding=encoding),
            salt.utils.data.decode_list(b, encoding=encoding),
            *args, **kwargs
        )
    )
1014801
def diff_safe(cls, value):
        """"""
        if isinstance(value, Frame):
            return {'_str': str(value), '_id': value._id}
        elif isinstance(value, (list, tuple)):
            return [cls.diff_safe(v) for v in value]
        return value
662878
def _get_changes_from_diff_dict(diff_dict):
    '''
    
    '''
    changes_strings = []
    for p in diff_dict.keys():
        if not isinstance(diff_dict[p], dict):
            raise ValueError('Unexpected diff difct \'{0}\''.format(diff_dict))
        if sorted(diff_dict[p].keys()) == ['val1', 'val2']:
            # Some string formatting
            from_str = diff_dict[p]['val1']
            if isinstance(diff_dict[p]['val1'], six.string_types):
                from_str = '\'{0}\''.format(diff_dict[p]['val1'])
            elif isinstance(diff_dict[p]['val1'], list):
                from_str = '\'{0}\''.format(', '.join(diff_dict[p]['val1']))
            to_str = diff_dict[p]['val2']
            if isinstance(diff_dict[p]['val2'], six.string_types):
                to_str = '\'{0}\''.format(diff_dict[p]['val2'])
            elif isinstance(diff_dict[p]['val2'], list):
                to_str = '\'{0}\''.format(', '.join(diff_dict[p]['val2']))
            changes_strings.append('{0} from {1} to {2}'.format(
                p, from_str, to_str))
        else:
            sub_changes = _get_changes_from_diff_dict(diff_dict[p])
            if sub_changes:
                changes_strings.append('{0}:'.format(p))
                changes_strings.extend(['\t{0}'.format(c)
                                       for c in sub_changes])
    return changes_strings
79907
def _diff(d1,d2):
    '''
        
    '''
    d = {}
    ds = _diff_internal(d1,d2)
    for key in ds['vdiff']:
        d[key] = d1[key]
    for key in ds['kdiff']:
        d[key] = d1[key]
    return(d)
1245081
def compare_snapshots(snapshot_a, snapshot_b, raw=False):
    
    if not all(map(lambda x: isinstance(x, dict),
                   [snapshot_a, snapshot_b])):
        return {}

    diffs = {}
    for key_a, value_a in snapshot_a.iteritems():
        # skip fieds starting with _ or __
        if key_a.startswith("_"):
            continue
        # get the value of the second snapshot
        value_b = snapshot_b.get(key_a)
        # get the diff between the two values
        diff = diff_values(value_a, value_b, raw=raw)
        if diff is not None:
            diffs[key_a] = diff
    return diffs