@@ -643,8 +643,8 @@ class Index(JSONPath):
643643 NOTE: For the concrete syntax of `[*]`, the abstract syntax is a Slice() with no parameters (equiv to `[:]`
644644 """
645645
646- def __init__ (self , index ):
647- self .index = index
646+ def __init__ (self , * indices ):
647+ self .indices = indices
648648
649649 def find (self , datum ):
650650 return self ._find_base (datum , create = False )
@@ -658,10 +658,12 @@ def _find_base(self, datum, create):
658658 if datum .value == {}:
659659 datum .value = _create_list_key (datum .value )
660660 self ._pad_value (datum .value )
661- if datum .value and len (datum .value ) > self .index :
662- return [DatumInContext (datum .value [self .index ], path = self , context = datum )]
663- else :
664- return []
661+ rv = []
662+ for index in self .indices :
663+ # invalid indices do not crash, return [] instead
664+ if datum .value and len (datum .value ) > index :
665+ rv += [DatumInContext (datum .value [index ], path = self , context = datum )]
666+ return rv
665667
666668 def update (self , data , val ):
667669 return self ._update_base (data , val , create = False )
@@ -675,28 +677,40 @@ def _update_base(self, data, val, create):
675677 data = _create_list_key (data )
676678 self ._pad_value (data )
677679 if hasattr (val , '__call__' ):
678- data [self .index ] = val .__call__ (data [self .index ], data , self .index )
679- elif len (data ) > self .index :
680- data [self .index ] = val
680+ for index in self .indices :
681+ val .__call__ (data [index ], data , index )
682+ else :
683+ for index in self .indices :
684+ if len (data ) > index :
685+ try :
686+ if isinstance (val , list ):
687+ # allows somelist[5,1,2] = [some_value, another_value, third_value]
688+ data [index ] = val .pop (0 )
689+ else :
690+ data [index ] = val
691+ except Exception as e :
692+ raise e
681693 return data
682694
683695 def filter (self , fn , data ):
684- if fn (data [self .index ]):
685- data .pop (self .index ) # relies on mutation :(
696+ for index in self .indices :
697+ if fn (data [index ]):
698+ data .pop (index ) # relies on mutation :(
686699 return data
687700
688701 def __eq__ (self , other ):
689- return isinstance (other , Index ) and self .index == other .index
702+ return isinstance (other , Index ) and sorted ( self .indices ) == sorted ( other .indices )
690703
691704 def __str__ (self ):
692- return '[%i]' % self .index
705+ return '[%i]' % self .indices
693706
694707 def __repr__ (self ):
695- return '%s(index =%r)' % (self .__class__ .__name__ , self .index )
708+ return '%s(indices =%r)' % (self .__class__ .__name__ , self .indices )
696709
697710 def _pad_value (self , value ):
698- if len (value ) <= self .index :
699- pad = self .index - len (value ) + 1
711+ _max = max (self .indices )
712+ if len (value ) <= _max :
713+ pad = _max - len (value ) + 1
700714 value += [{} for __ in range (pad )]
701715
702716 def __hash__ (self ):
0 commit comments