@@ -606,8 +606,8 @@ class Index(JSONPath):
606
606
NOTE: For the concrete syntax of `[*]`, the abstract syntax is a Slice() with no parameters (equiv to `[:]`
607
607
"""
608
608
609
- def __init__ (self , index ):
610
- self .index = index
609
+ def __init__ (self , * indices ):
610
+ self .indices = indices
611
611
612
612
def find (self , datum ):
613
613
return self ._find_base (datum , create = False )
@@ -621,10 +621,12 @@ def _find_base(self, datum, create):
621
621
if datum .value == {}:
622
622
datum .value = _create_list_key (datum .value )
623
623
self ._pad_value (datum .value )
624
- if datum .value and len (datum .value ) > self .index :
625
- return [DatumInContext (datum .value [self .index ], path = self , context = datum )]
626
- else :
627
- return []
624
+ rv = []
625
+ for index in self .indices :
626
+ # invalid indices do not crash, return [] instead
627
+ if datum .value and len (datum .value ) > index :
628
+ rv += [DatumInContext (datum .value [index ], path = self , context = datum )]
629
+ return rv
628
630
629
631
def update (self , data , val ):
630
632
return self ._update_base (data , val , create = False )
@@ -638,31 +640,38 @@ def _update_base(self, data, val, create):
638
640
data = _create_list_key (data )
639
641
self ._pad_value (data )
640
642
if hasattr (val , '__call__' ):
641
- val .__call__ (data [self .index ], data , self .index )
642
- elif len (data ) > self .index :
643
- data [self .index ] = val
643
+ for index in self .indices :
644
+ val .__call__ (data [index ], data , index )
645
+ else :
646
+ if not isinstance (val , list ):
647
+ val = [val ]
648
+ # allows somelist[5,1,2] = [some_value, another_value, third_value]
649
+ for index in self .indices :
650
+ if len (data ) > index :
651
+ data [index ] = val .pop (0 )
644
652
return data
645
653
646
654
def filter (self , fn , data ):
647
- if fn (data [self .index ]):
648
- data .pop (self .index ) # relies on mutation :(
655
+ for index in self .indices :
656
+ if fn (data [index ]):
657
+ data .pop (index ) # relies on mutation :(
649
658
return data
650
659
651
660
def __eq__ (self , other ):
652
- return isinstance (other , Index ) and self .index == other .index
661
+ return isinstance (other , Index ) and sorted ( self .indices ) == sorted ( other .indices )
653
662
654
663
def __str__ (self ):
655
- return '[%i]' % self .index
664
+ return '[%i]' % self .indices
656
665
657
666
def __repr__ (self ):
658
- return '%s(index =%r)' % (self .__class__ .__name__ , self .index )
667
+ return '%s(indices =%r)' % (self .__class__ .__name__ , self .indices )
659
668
660
669
def _pad_value (self , value ):
661
- if len (value ) <= self .index :
662
- pad = self .index - len (value ) + 1
670
+ _max = max (self .indices )
671
+ if len (value ) <= _max :
672
+ pad = _max - len (value ) + 1
663
673
value += [{} for __ in range (pad )]
664
674
665
-
666
675
class Slice (JSONPath ):
667
676
"""
668
677
JSONPath matching a slice of an array.
0 commit comments