@@ -643,8 +643,8 @@ class Index(JSONPath):
643
643
NOTE: For the concrete syntax of `[*]`, the abstract syntax is a Slice() with no parameters (equiv to `[:]`
644
644
"""
645
645
646
- def __init__ (self , index ):
647
- self .index = index
646
+ def __init__ (self , * indices ):
647
+ self .indices = indices
648
648
649
649
def find (self , datum ):
650
650
return self ._find_base (datum , create = False )
@@ -658,10 +658,12 @@ def _find_base(self, datum, create):
658
658
if datum .value == {}:
659
659
datum .value = _create_list_key (datum .value )
660
660
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
665
667
666
668
def update (self , data , val ):
667
669
return self ._update_base (data , val , create = False )
@@ -675,28 +677,40 @@ def _update_base(self, data, val, create):
675
677
data = _create_list_key (data )
676
678
self ._pad_value (data )
677
679
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
681
693
return data
682
694
683
695
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 :(
686
699
return data
687
700
688
701
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 )
690
703
691
704
def __str__ (self ):
692
- return '[%i]' % self .index
705
+ return '[%i]' % self .indices
693
706
694
707
def __repr__ (self ):
695
- return '%s(index =%r)' % (self .__class__ .__name__ , self .index )
708
+ return '%s(indices =%r)' % (self .__class__ .__name__ , self .indices )
696
709
697
710
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
700
714
value += [{} for __ in range (pad )]
701
715
702
716
def __hash__ (self ):
0 commit comments