Open tuukkamustonen opened 3 years ago
That's an interesting idea!
One of the key features of glom is it is very easy to extend; so here's my first thought:
class SubDict:
def __init__(self, include=None, exclude=None):
self.include, self.exclude = include, exclude
def glomit(self, target, scope):
if self.include:
result = {key: target[key] for key in self.include}
else:
result = dict(target)
if self.exclude:
for key in self.exclude:
del result[key]
return result
hmmm.. that's not quite right; because you want to be able to use 'a.b'
to refer to nested structures, but its a starting point
it's in a similar vein to this:
https://github.com/mahmoud/boltons/issues/266
maybe that suggests an API?
Yes, it should work on nested data structures.
I think the API should resemble current glom-API, so maybe it should be declarable via spec (instead of glom.collect()
or glom.omit()
like the examples above).
I think this should be mentioned early in the tutorial, right after the nested get syntax glom(target, ['some.nested.key'])
. I personally expected something this simple to be easily done with such a powerful library.
I don't think there's need for defaults - wouldn't hurt, but those could be added in a subsequent call if needed. If a key doesn't exist, it's not included.
It should consider include/exclude while building, instead of first copying and then deleting attributes. Because, it's more performance effective, and the-right-way(tm).
It would appear that
glom
excels at grabbing (and manipulating) specific data out of data structures.1) However, the syntax for simply extracting a subset feels a bit verbose:
I don't need to manipulate anything here, just extract a sub-dict. Instead of what's above, I'd like to write something like:
Is there less verbose syntax, like above, for this (grab subset without mutation)?
2) Sometimes, the need is in reverse - to grab a copy of data structure, omitting certain (potentially nested) keys. I can do that at least by:
But that's again too verbose for this simple task. Plus, there's need to copy the data first, as glom manipulates it in-place.
What I would like write instead:
Is there less verbose syntax for this (grab subset, omitting certain keys)?
(Newcomer to glom... seems such a beast!)