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.92k stars 63 forks source link

deleting doesn't support list paths #145

Open Granitosaurus opened 4 years ago

Granitosaurus commented 4 years ago

when trying to delete path with square brackets like ('products', ['id']) glom throws:

path argument must be a .-delimited string, Path, T, or S

example:

import glom
foo = {
    'items': [
        {'foo': '1', 'bar': '2'},
        {'foo': '1', 'bar': '2'},
        {'foo': '1', 'bar': '2'},
    ]
}
glom.glom(foo, ('items', ['foo']))
# ['1', '1', '1']

glom.delete(foo, ('items', ['foo']))
# TypeError: path argument must be a .-delimited string, Path, T, or S
glom.glom(foo, glom.Delete(('items', ['foo']))) 
# TypeError: path argument must be a .-delimited string, Path, T, or S
mahmoud commented 4 years ago

Hi @Granitosaurus, usually in cases like this, I like to ask for the intended output. But thanks to your fairly thorough illustration, I assume the intention here is a multi-delete?

If so, then I should mention (and maybe update the docs to say) that glom's deep mutation spec types (both Assign and Delete) are only for single mutations at this time. PRs certainly considered, though mutation can be tricky.

Technically calling it in a loop might work, but have you tried glom.glom(foo, (items, [Delete('foo')])?

Granitosaurus commented 4 years ago

@mahmoud very interesting, your suggestion does work! But it's a bit awkward to use:

>>> glom.glom(foo, ("items", [glom.Delete('foo')]))                                                                   
[{'bar': '2'}, {'bar': '2'}, {'bar': '2'}]

I think coming up to this usage as glom user would be very difficult but it works! I think it's worth documenting that and whole delete function as I think it can be a killer feature of glom!

Anyway for my use case this is good enough, thanks!

kurtbrose commented 4 years ago

possibly we could extend delete to support slices? definitely kind of tricky

kurtbrose commented 4 years ago

oh I misunderstood, this is talking about multiple parallel deletes -- not deleting multiple items from the same list

probably *-paths will help with this :-)