FacundoGFlores FacundoGFlores - 2 months ago 15
Python Question

Parsing DeepDiff result

I am working with DeepDiff. So I have results like:

local = [{1: {'age': 50, 'name': 'foo'}}, {2: {'age': 90, 'name': 'bar'}}, {3: {'age': 60, 'name': 'foobar'}}]
online = [{1: {'age': 50, 'name': 'foo'}}, {2: {'age': 40, 'name': 'bar'}}]
ddiff = DeepDiff(local, online)
added, updated = ddiff['iterable_item_added'], ddiff['values_changed']
added = {'root[2]': {3: {'age': 60, 'name': 'foobar'}}}
updated = {"root[1][2]['age']": {'new_value': 90, 'old_value': 40}}


Now, I want to take:

list_indexes_added = foo(added)
list_indexes_updated = foo(updated)


and to obtain:

list_indexes_added = [2]
list_index_updated = [(1,2,'age')]


in this way, I can manipulate the list
local
and
online
and in the future update the
online
table.

I am thinking in regexs, but maybe there is other option.

Answer
  • One solution can be regex and custom parsing of the matches.

  • Another can be using literal_eval after regex parsing on these strings, if the output format of deepdiff is consistent

    from ast import literal_eval
    import re
    
    
    def str_diff_parse(str_diff):
        return [tuple(literal_eval(y) for y in re.findall(r"\[('?\w+'?)\]", x)) for x in str_diff]
    
    added = {'root[2]': {3: {'age': 60, 'name': 'foobar'}}}
    updated = {"root[1][2]['age']": {'new_value': 90, 'old_value': 40}}
    
    list_indexes_added = str_diff_parse(added)
    list_indexes_updated = str_diff_parse(updated)
    
    print(list_indexes_added)
    print(list_indexes_updated)
    # prints
    #[(2,)]
    #[(1, 2, 'age')]
    

demo : http://ideone.com/3MhTky

  • Would also recommend dictdiffer module, it returns the diff as a consumable python diff object which can be patched to original dictionary to get the updated one or vice versa.
Comments