class DictDiff(object): """A wrapper around json_diff's dict comparisons. :param dict1: The first dict. Can be a :class:`GitDict <GitDict>`. :type dict1: dict :param dict2: The second dict. Can be a :class:`GitDict <GitDict>`. :type dict2: dict """ def __init__(self, dict1, dict2): self._diff = Comparator().compare_dicts(dict1, dict2) @property def removed(self): """A dict of removed keys and their values. """ return self._diff.get('_remove', {}) @property def updated(self): """A dict of updated keys and their values. """ return self._diff.get('_update', {}) @property def appended(self): """A dict of appended keys and their values. """ return self._diff.get('_append', {}) def conflict(self, other): """Determine whether this :class:`DictDiff <DictDiff>`s conflicts with another. :param other: The diff to compare. :type other: :class:`DictDiff <DictDiff>` :return: the conflicts :rtype: :class:`DictConflict <DictConflict>` """ conflicts = {} for other_mod_type, other_mods in other._diff.items(): for self_mod_type, self_mods in self._diff.items(): for other_mod_key, other_mod_value in other_mods.items(): if other_mod_key in self_mods: self_mod_value = self_mods[other_mod_key] # if the mod type is the same, it's OK if the actual # modification was the same. if other_mod_type == self_mod_type: if other_mod_value == self_mods[other_mod_key]: pass else: conflicts[other_mod_key] = { 'other': { other_mod_type: other_mod_value }, 'self' : { self_mod_type: self_mod_value } } # if the mod type was not the same, it's a conflict no # matter what else: conflicts[other_mod_key] = { 'other': { other_mod_type: other_mod_value }, 'self' : { self_mod_type: self_mod_value } } return conflicts if len(conflicts) else None
# text[16] is "PROPOSITION DE LOI ..." for i in parse_two_columns_text(text[17]): for j in i: print SEPARATOR print j for i in parse_two_columns_text(text[18]): for j in i: print SEPARATOR print j if __name__ == "__main__": for i in ("53K1961001", "53K1098001", "53K1595001"): try: assert not Comparator( StringIO(dumps(intelligent_parse("%s.pdf" % i))), open("%s.json" % i, "r")).compare_dicts() except AssertionError: print dumps(Comparator( StringIO(dumps(intelligent_parse("%s.pdf" % i))), open("%s.json" % i, "r")).compare_dicts(), indent=4) print dumps(Comparator( open("%s.json" % i, "r"), StringIO(dumps(intelligent_parse("%s.pdf" % i)))).compare_dicts(), indent=4) raise dumps(intelligent_parse("53K0785001.pdf"), indent=4)
def __init__(self, dict1, dict2): self._diff = Comparator().compare_dicts(dict1, dict2)