glom
glom copied to clipboard
"enumerate" equivalent: give the full path with the result
Python has the extremely-handy enumerate
function, allowing you to iterate through a list and get the index of the current element. What's the team's view on adding similar functinality to Glom? Could be very useful for data exploration/validation (c.f. #7), so that you can find your way back to where a value came from.
Don't have a good idea of the best interface for this, but something like:
target = {
"foo": [
{"a": 1, "b": 2},
{"a": 3, "b": 4}
]
}
spec = ("foo", ["b"])
result = glom(target, spec, trace=True)
# [("foo.0.b", 2), ("foo.1.b", 4)]
So if I was using
this to power a validator ensuring that, say, no "b" was greater than 3, it could do:
for path, value in result:
if value > 3:
print(f"Warning: {path} greater than 3!")
It seems like the information needed to power this is all there already, based on the output of a recursive Inspect
. Is there already some easy way of achieving this that I'm missing?
Hey Eli! You're right that the state is all there, and that sounds like a perfectly fine application. Not sure if we want a top-level argument, or if it would be better to have it work at all levels like Inspect. We have an extension API in the works (#32), and that'll make adding these a lot easier.
It may be worth mentioning that if the structure of the data isn't well-known or highly-dynamic, remap may be better suited than glom, but in your example, it seems like you know what you're looking for :)
I'll keep this issue updated with developments, and let you know when the extension API is ready (that PR is our most active area of discussion/development atm, so it should be soon).
That's a good point about the top-level argument. I only figured out after writing this how Inspect
-style wrappers can make use of glom's recursion to implement this kind of feature (which I think is extremely neat, by the way). Thanks for being so responsive, I'm looking forward to the update!