glom icon indicating copy to clipboard operation
glom copied to clipboard

REQ: Add a "Data-Driven Assignment" example like this to the tutorial

Open mborus opened this issue 5 years ago • 3 comments

Hi,

When I started using glom I quickly encountered a roadblock. The APIs I wanted to glom used structures that had dictionaries with non-constant values. (The nested data examples in the tutorials don't cover a case, where the planet names are keys.)

After several re-reads of the whole documentation over the course of a few weeks, I found the hint to a solution in the "Snippets"-section. The example there does not show how to solve the problem with variable keys in dictionaries. But is explains what the problem is. (That example takes a dict, but the result is still a dict. It offers no help on how to access parts of it to transform it.)

Will you consider to add a "data driven assignment" example to the nested data part of the tutorial or to mention this problem early on?

An example like this would have made it easier to get started:

from glom import glom, T, merge, Iter, Coalesce

target = {
    "pluto": {"moons": 6, "population": None},
    "venus": {"population": {"aliens": 5}},
    "earth": {"moons": 1, "population": {"humans": 7_700_000_000, "aliens": 1}},
}

spec = {
    "moons": (
        T.items(),
        Iter({T[0]: (T[1], Coalesce("moons", default=0))}).all(),
        merge,
    )
}

glom(target, spec)`

Result:

{'moons': {'pluto': 6, 'venus': 0, 'earth': 1}}

mborus avatar Feb 26 '20 21:02 mborus

@mborus Thanks for the great suggestion (and your patience in my reply!). It's great to hear that the snippets page came in handy, but you're right, we should lift something like this into the rest of the docs. Would you mind working what you've got above into the tutorial?

mahmoud avatar Mar 17 '20 08:03 mahmoud

Hi @mborus just thought I'd check in on your interest in adding this to the tutorial. Say the word and I'll get it in there, but I figured I'd give you the right of first refusal :)

mahmoud avatar Apr 11 '20 22:04 mahmoud

Thanks for the reminder. Yes, still interested, I'll try to get it in in the easter holiday week.

mborus avatar Apr 12 '20 07:04 mborus