@@ -309,7 +309,7 @@ def set(
309
309
) -> MutableMapping :
310
310
"""
311
311
Set the value in obj at the place indicated by segments. If creator is not
312
- None (default __default_creator__ ), then call the creator function to
312
+ None (default _default_creator ), then call the creator function to
313
313
create any missing path components.
314
314
315
315
set(obj, segments, value) -> obj
@@ -320,13 +320,18 @@ def set(
320
320
# For everything except the last value, walk down the path and
321
321
# create if creator is set.
322
322
for (i , segment ) in enumerate (segments [:- 1 ]):
323
+
324
+ # If segment is non-int but supposed to be a sequence index
325
+ if isinstance (segment , str ) and isinstance (current , Sequence ) and segment .isdigit ():
326
+ segment = int (segment )
327
+
323
328
try :
324
329
# Optimistically try to get the next value. This makes the
325
330
# code agnostic to whether current is a list or a dict.
326
331
# Unfortunately, for our use, 'x in thing' for lists checks
327
332
# values, not keys whereas dicts check keys.
328
333
current [segment ]
329
- except ( KeyError , IndexError ) :
334
+ except :
330
335
if creator is not None :
331
336
creator (current , segments , i , hints )
332
337
else :
@@ -336,10 +341,16 @@ def set(
336
341
if i != length - 1 and leaf (current ):
337
342
raise PathNotFound (f"Path: { segments } [{ i } ]" )
338
343
339
- if isinstance (segments [- 1 ], int ):
340
- extend (current , segments [- 1 ])
344
+ last_segment = segments [- 1 ]
345
+
346
+ # Resolve ambiguity of last segment
347
+ if isinstance (last_segment , str ) and isinstance (current , Sequence ) and last_segment .isdigit ():
348
+ last_segment = int (last_segment )
341
349
342
- current [segments [- 1 ]] = value
350
+ if isinstance (last_segment , int ):
351
+ extend (current , last_segment )
352
+
353
+ current [last_segment ] = value
343
354
344
355
return obj
345
356
@@ -388,9 +399,11 @@ def view(obj, glob):
388
399
389
400
view(obj, glob) -> obj'
390
401
"""
402
+
391
403
def f (obj , pair , result ):
392
404
(segments , value ) = pair
393
405
if match (segments , glob ):
394
406
if not has (result , segments ):
395
407
set (result , segments , deepcopy (value ), hints = types (obj , segments ))
408
+
396
409
return fold (obj , f , type (obj )())
0 commit comments