Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"enumerate" equivalent: give the full path with the result #39

Open
treuherz opened this issue Jun 27, 2018 · 2 comments
Open

"enumerate" equivalent: give the full path with the result #39

treuherz opened this issue Jun 27, 2018 · 2 comments

Comments

@treuherz
Copy link

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?

@mahmoud
Copy link
Owner

mahmoud commented Jun 27, 2018

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).

@treuherz
Copy link
Author

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!

@mahmoud mahmoud mentioned this issue Oct 29, 2019
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants