39
39
import functools
40
40
import inspect
41
41
import json
42
- import operator
43
42
import sys
44
43
45
44
from jsonpointer import JsonPointer , JsonPointerException
52
51
53
52
if sys .version_info >= (3 , 0 ):
54
53
basestring = (bytes , str ) # pylint: disable=C0103
54
+ # pylint: disable=E0611
55
+ from itertools import zip_longest
56
+ else :
57
+ # pylint: disable=E0611,W0404
58
+ from itertools import izip_longest as zip_longest
55
59
56
60
57
61
class JsonPatchException (Exception ):
@@ -78,11 +82,11 @@ def multidict(ordered_pairs):
78
82
for key , value in ordered_pairs :
79
83
mdict [key ].append (value )
80
84
81
- # unpack lists that have only 1 item
82
- for key , values in mdict . items ():
83
- if len (values ) == 1 :
84
- mdict [ key ] = values [ 0 ]
85
- return dict ( mdict )
85
+ return dict (
86
+ # unpack lists that have only 1 item
87
+ ( key , values [ 0 ] if len (values ) == 1 else values )
88
+ for key , values in mdict . items ()
89
+ )
86
90
87
91
88
92
def get_loadjson ():
@@ -237,8 +241,12 @@ def __eq__(self, other):
237
241
if not isinstance (other , JsonPatch ):
238
242
return False
239
243
240
- return len (list (self ._ops )) == len (list (other ._ops )) and \
241
- all (map (operator .eq , self ._ops , other ._ops ))
244
+ for lop , rop in zip_longest (self ._ops , other ._ops ):
245
+ if lop is None or rop is None :
246
+ return False
247
+ if lop != rop :
248
+ return False
249
+ return True
242
250
243
251
@classmethod
244
252
def from_string (cls , patch_str ):
@@ -312,7 +320,8 @@ def compare_list(path, src, dst):
312
320
'path' : '/' .join (current ),
313
321
'value' : dst [idx ]}
314
322
elif lsrc > ldst :
315
- for idx in reversed (range (ldst , lsrc )):
323
+ # more effective than reversed(range(ldst, lsrc))
324
+ for idx in range (lsrc - 1 , ldst - 1 , - 1 ):
316
325
yield {'op' : 'remove' , 'path' : '/' .join (path + [str (idx )])}
317
326
318
327
return cls (list (compare_dict (['' ], src , dst )))
0 commit comments