@@ -673,8 +673,8 @@ class Index(JSONPath):
673
673
NOTE: For the concrete syntax of `[*]`, the abstract syntax is a Slice() with no parameters (equiv to `[:]`
674
674
"""
675
675
676
- def __init__ (self , index ):
677
- self .index = index
676
+ def __init__ (self , * indices ):
677
+ self .indices = indices
678
678
679
679
def find (self , datum ):
680
680
return self ._find_base (datum , create = False )
@@ -688,10 +688,12 @@ def _find_base(self, datum, create):
688
688
if datum .value == {}:
689
689
datum .value = _create_list_key (datum .value )
690
690
self ._pad_value (datum .value )
691
- if datum .value and len (datum .value ) > self .index :
692
- return [DatumInContext (datum .value [self .index ], path = self , context = datum )]
693
- else :
694
- return []
691
+ rv = []
692
+ for index in self .indices :
693
+ # invalid indices do not crash, return [] instead
694
+ if datum .value and len (datum .value ) > index :
695
+ rv += [DatumInContext (datum .value [index ], path = self , context = datum )]
696
+ return rv
695
697
696
698
def update (self , data , val ):
697
699
return self ._update_base (data , val , create = False )
@@ -705,28 +707,40 @@ def _update_base(self, data, val, create):
705
707
data = _create_list_key (data )
706
708
self ._pad_value (data )
707
709
if hasattr (val , '__call__' ):
708
- data [self .index ] = val .__call__ (data [self .index ], data , self .index )
709
- elif len (data ) > self .index :
710
- data [self .index ] = val
710
+ for index in self .indices :
711
+ val .__call__ (data [index ], data , index )
712
+ else :
713
+ for index in self .indices :
714
+ if len (data ) > index :
715
+ try :
716
+ if isinstance (val , list ):
717
+ # allows somelist[5,1,2] = [some_value, another_value, third_value]
718
+ data [index ] = val .pop (0 )
719
+ else :
720
+ data [index ] = val
721
+ except Exception as e :
722
+ raise e
711
723
return data
712
724
713
725
def filter (self , fn , data ):
714
- if fn (data [self .index ]):
715
- data .pop (self .index ) # relies on mutation :(
726
+ for index in self .indices :
727
+ if fn (data [index ]):
728
+ data .pop (index ) # relies on mutation :(
716
729
return data
717
730
718
731
def __eq__ (self , other ):
719
- return isinstance (other , Index ) and self .index == other .index
732
+ return isinstance (other , Index ) and sorted ( self .indices ) == sorted ( other .indices )
720
733
721
734
def __str__ (self ):
722
- return '[%i]' % self .index
735
+ return '[%i]' % self .indices
723
736
724
737
def __repr__ (self ):
725
- return '%s(index =%r)' % (self .__class__ .__name__ , self .index )
738
+ return '%s(indices =%r)' % (self .__class__ .__name__ , self .indices )
726
739
727
740
def _pad_value (self , value ):
728
- if len (value ) <= self .index :
729
- pad = self .index - len (value ) + 1
741
+ _max = max (self .indices )
742
+ if len (value ) <= _max :
743
+ pad = _max - len (value ) + 1
730
744
value += [{} for __ in range (pad )]
731
745
732
746
def __hash__ (self ):
0 commit comments