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

Question, how to pull items into an array in a spec? #260

Open
adlmtl opened this issue Apr 28, 2023 · 1 comment
Open

Question, how to pull items into an array in a spec? #260

adlmtl opened this issue Apr 28, 2023 · 1 comment

Comments

@adlmtl
Copy link

adlmtl commented Apr 28, 2023

Suppose I have a target:
{ 'a': 1, 'b': 2, 'c': [{ 'd': 3, 'e': 4 }] }

how would I write a spec to produce the output:
{ 'my_output': { 'a_and_b': [1, 2], 'c': [{ 'new_d': 3, 'new_e': 4 }] } }

the part I am hung up on, is how to take 'a' and 'b' and get them into an array...

spec = { 'my_output': { 'a_and_b': ['a', 'b'], 'c': ('c', [{'new_d': 'd', 'new_e': 'e'}]) } }

@kurtbrose
Copy link
Collaborator

kurtbrose commented Jun 19, 2023

you want the Fill spec:

>>> from glom import glom, Fill, T
>>> target = {'a': 1, 'b': 2}
>>> glom(target, Fill([T['a'], T['b']]))
[1, 2]

Fill is for "data structure as a template" use cases like this.

in the full context:

>>> target = { 'a': 1, 'b': 2, 'c': [{ 'd': 3, 'e': 4 }] } 
>>> spec = {'my_output': {'a_and_b': Fill([T['a'], T['b']]), 
...                       'c': ('c', [{'new_d': 'd', 'new_e': 'e'}]) } }
>>> glom(target, spec)
{'my_output': {'a_and_b': [1, 2], 'c': [{'new_d': 3, 'new_e': 4}]}}

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