Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract tuples from complex objects? #77

Closed
antoine-gallix opened this issue Jan 18, 2019 · 3 comments
Closed

Extract tuples from complex objects? #77

antoine-gallix opened this issue Jan 18, 2019 · 3 comments

Comments

@antoine-gallix
Copy link
Contributor

Is there a spec that can do the following ?

d = {'a': 1, 'b': 2}
glom(d, <some_spec_here>)
>> (1,2)

My intended use case is to extract tuples of selected data from complex items, that could be then used for sorting or grouping. Glom API creates easily dict outputs, but in the case the output needs to be an immutable tuple. It also differs from the nested list example of the tutorial because each element of the tuple is reached through a different path.

@mahmoud
Copy link
Owner

mahmoud commented Jan 18, 2019

For more powerful extractions, you can use the T specifier type to get exactly what you need. For this particular case I would do:

>>> from glom import glom, T
>>> d = {'a': 1, 'b': 2}
>>> glom(d, (T.items(), sorted, tuple))
(1, 2)

Read more about T here. :)

@antoine-gallix
Copy link
Contributor Author

Thank you! (Sorry, I forgot to close the issue)

@mahmoud
Copy link
Owner

mahmoud commented Sep 16, 2019

No worries! We're actually sketching out an explicit tuple builder, too. Keep an eye on the changelog! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants