-
Notifications
You must be signed in to change notification settings - Fork 64
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
assign with coalesce [question] #226
Comments
Hey Radu! Interesting question. So the goal is to assign the If so, I can't think of a way to do this as a single spec using |
Hi Mahmoud, thanks for replying. Indeed, I only needed the overwrite use case so far, but the functionality could be extended to work something like:
|
Hi, I'm not sure your exact use case, but the first thing that comes to mind for me: glom.assign(obj, glom.Coalesce(*specs), value)
# instead of assign(coalesce), coalesce(assign)
glom.glom(obj, Coalesce(*[Assign(T, spec, value) for spec in specs]))
# if the only desired behavior is "try these in order, return the first one that doesn't fail", then Or can also work
glom.glom(obj, Or(*[Assign(T, spec, value) for spec in specs])) |
Hi Kurt, your suggestions are close to what I want, but both bail out after the first success, and they are too readily successful (even if the path doesn't actually exist - which would correspond to what I described as non-strict behaviour in my implementation above). Anyway, thanks for the idea, it's pretty cool, I'll keep that in mind. |
Oh, just re-reading this -- you want to ensure that EACH branch gets exercised, not stop executing on the first success? strict = And(*[Assign(T, spec, value) for spec in specs]) if you want to try to execute each, but ignore failures: best_effort = And(*[Or(Assign(T, spec, value), Val(None)) for spec in specs]) |
Somewhat surprisingly,
glom.assign(obj, glom.Coalesce(*specs), value)
doesn't work.Is there an easier way to get the result, e.g. something like:
Thanks.
The text was updated successfully, but these errors were encountered: