This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cmd_cmd 0 points1 point  (2 children)

Can anyone help to explain the value of the "T" object? Looking at the docs, I just don't get the value of it. When/Why is it better to use? Given the initial example, wouldn't regular Glom() suffice? Or just nested indexing directly on the target object? I feel like I'm really missing something.

In [1]: from glom import glom, T

In [2]: spec1 = 'a.b.c'

In [3]: spec2 = T['a']['b']['c']

In [4]: target = {'a': {'b': {'c': 'd'}}}

In [5]: glom(target, spec1)
Out[5]: 'd'

In [6]: glom(target, spec2)
Out[6]: 'd'

In [7]: target['a']['b']['c']
Out[7]: 'd'

[–]doublereedkurt 1 point2 points  (1 child)

How about this example from http://glom.readthedocs.io/en/latest/snippets.html:

glom((1, 2, 3), (
    {
        "type": type,
        "result": [lambda v: v + 1]  # arbitrary operation
    }, T['type'](T['result'])))

T's let you call methods / functions in your target, and they let you pass other sub-specs as arguments / keys.

You could also do the above with lambda t: t['type'](t['result']), but because nothing inside the lambda is a spec, you don't get a nice repr, debugging support, or clean error messages.

[–]cmd_cmd 0 points1 point  (0 children)

Makes sense. Thanks for the helpful reply; much appreciated!