mahmoud / glom

☄️ Python's nested data operator (and CLI), for all your declarative restructuring needs. Got data? Glom it! ☄️
https://glom.readthedocs.io
Other
1.89k stars 61 forks source link

better target-path #199

Open kurtbrose opened 3 years ago

kurtbrose commented 3 years ago

there was some prototyping of scope[Path] as a mechanism for tracking target path, but there are some flaws and also it eats a lot of performance for light specs (e.g. list-of-dicts)

here are some of the issues:

e.g. a very simple case:

glom([1, 1, 1], [T])

While evaluating the first T, the list-scope will have path=0; the T-scope will have no path

When evaluating the second T, the list-scope will have path=1; the T-scope will have no path

This really becomes a problem when something like branching gets introduced -- if you want to be able to "rewind" and generate a nice clean error message involving the target-path for why a previous branch failed it is impossible because that information got overwritten in the parent scope

kurtbrose commented 3 years ago

one approach forward would be to add a param to _glom()

def _glom(target, spec, scope, target_move=_MISSING):
   if target_move is not _MISSING:
      scope[Path] = target_move

or similar -- storing something in the scope that says "here is how the current target was derived from the previous target"

possibly something that also requires looking at the spec to interpret