glom icon indicating copy to clipboard operation
glom copied to clipboard

deleting doesn't support list paths

Open Granitosaurus opened this issue 4 years ago • 4 comments

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

Granitosaurus avatar Jun 02 '20 10:06 Granitosaurus

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')])?

mahmoud avatar Jun 02 '20 14:06 mahmoud

@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!

Granitosaurus avatar Jun 03 '20 04:06 Granitosaurus

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

kurtbrose avatar Jun 28 '20 21:06 kurtbrose

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

probably *-paths will help with this :-)

kurtbrose avatar Jun 28 '20 22:06 kurtbrose