dot

packages and services management
Log | Files | Refs | README

commit 4ef2909dbb384475af509e5b7a0d3534eb3b1cf6
parent da1bdb246c4b2ae2c91d5652f17e065da2122dba
Author: Josuah Demangeon <mail@josuah.net>
Date:   Thu, 11 May 2017 19:04:16 +0200

util.py

Diffstat:
Autil.py | 49+++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 49 insertions(+), 0 deletions(-)

diff --git a/util.py b/util.py @@ -0,0 +1,49 @@ +def full_map(li1, li2, key): + """Given a dict: + obj['key'] = 1 + obj.get('key') = 1 + obj1 == obj2 + + and two lists: + li1 = [ obj1, obj2, obj3 ] + obj3.value += 1 + li2 = [ obj1, obj3 ] + + returns : + ( + [], # added + [obj2], # missing + [(old_obj3, new_obj3), ..] # common + ) + + "key" is the sorting key for lists. + """ + def sanitize_keys(li): + i = 0 + for x in li: + if x.get(key) is None: + x[key] = i + i = x[key] + i += 1 + + def has(some_list, key, some_object): + return [ x for x in some_list + if x.get(key) == some_object.get(key) + ] + + def get(some_list, key, some_object): + for x in some_list: + if x.get(key) == some_object.get(key): + return x + + sanitize_keys(li1) + sanitize_keys(li2) + + return ( + [ x for x in li2 if + not has(li1, key, x) ], + [ x for x in li1 if + not has(li2, key, x) ], + [ (get(li1, key, x), x) for x in li2 if + has(li1, key, x) ] + )