-
Notifications
You must be signed in to change notification settings - Fork 65
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
Recursive Delete-If-Empty? #273
Comments
The easiest way to do that would be as a custom Mode -- maybe ~ 15 LOC I can whip something up real quick |
Hmm... I guess delete is an in-place mutation rather than returning a copy. I think this would be easier to do as a follow-on step rather than mixing it into the guts of delete. val = {"a": {"b": 1} }
glom(val, (Delete("a.b"), Trim()))
# val is now {} |
Let me see if this reasoning makes sense: Recursive deleting "along a path" is probably not so much what is intended as recursive deletion of a subtree. That is, you just need to get the glom target to the right place and then let it go. The interesting bits:
|
A switch would almost work -- except this requires post-evaluation not pre-evaluation. I wonder if there's something general there. Kind of like It's trivial with a custom spec -- the return value is passing through If there was such a thing as post-evaluation |
Of course, plain python recursion would have better performance than bouncing each item through glom. |
For this case, a copy was fine, and I think a recursive path trim makes sense as a separate step/spec. Even if it's not "context-free" for the whole tree like (additional context: this was a pre-step for db persistence, to minimize the size of a record) |
Chatting with @hynek tonight, seems like there's a market for a
Delete
/delete
flag to remove the value at a path if it's empty, and its parent if it's empty, and so on.Kind of like
rm -r
(orrmdir
, in that it fails/stops if the container isn't empty).(Tangentially related, remap can do this for a whole object, but it's not very convenient for recursively deleting a specific path.)
The text was updated successfully, but these errors were encountered: