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

Help debugging a PathAccessError #46

Closed
xiaosimao opened this issue Jul 27, 2018 · 3 comments

Comments

@xiaosimao
Copy link

commented Jul 27, 2018

@mahmoud @kurtbrose Hi, guys,there is a question i can not understand,please help me.

from glom import glom
target = {'system': {'planets': [{'name': 'earth'}, {'name': 'jupiter'}, {'name2': 'jupiters'}]}}

print glom(target, ('system.planets', ['name']))

Why this occur PathAccessError?

Does this mean the dict which key is name2 can not be in the list?

And how can i get data like this {'plants':['earth', 'jupiter', 'jupiters']} in a pythonic way use glom?

thank you.

@mahmoud

This comment has been minimized.

Copy link
Owner

commented Jul 27, 2018

Hey @xiaosimao! (little four hair?)

First of all, in your spec, the system.planets is just a destination key in the output. Only the value of the dict is used for lookup.

Now, the working code/tutorial:

import glom 
target = {'system': {'planets': [{'name': 'earth'}, {'name': 'jupiter'}, {'name2': 'jupiters'}]}}

glom.glom(target, {'planets': ('system', 'planets', [glom.Coalesce('name', 'name2')])})
# {'planets': ['earth', 'jupiter', 'jupiters']}

One step at a time:

  1. {'planets': ... } -- We set the output to be a dictionary with key 'planets', per your request.
  2. ('system', 'planets', ...) -- We use a tuple to look up 'system', then 'planets'. (note that you can also do ('system.planets', ...) with the same result)
  3. [...] -- We use a list to traverse the planets
  4. glom.Coalesce('name', 'name2') -- We use a Coalesce, because the planet with "jupiters" has its name under key 'name2' instead of 'name' like the others.

I hope that makes sense! It's a lot of power for just one line, and I'm sure you'll get the hang of it with some more practice :)

@xiaosimao

This comment has been minimized.

Copy link
Author

commented Jul 28, 2018

@mahmoud Hi, Thanks a lot for your reply. That works!! And make me know much more about how to use Coalesce.

What's more , that you know the meaning of xiaosimao impress me very much!!

But, in china, if we call somebody xiaosimao, that means 小四毛, this is the nickname of the fourth child in a family. So, we call the thrid child sanmao(三毛),the second ermao(二毛) and the first child damao(大毛) .

HAHAHA,last but not least, thanks for your replays again.

@mahmoud

This comment has been minimized.

Copy link
Owner

commented Jul 31, 2018

Haha, I knew my small Chinese knowledge would come in handy someday ;D That's a very useful lesson! You have a big family, lots of geges and jiejies! Glad to be of help!

@mahmoud mahmoud closed this Jul 31, 2018
@mahmoud mahmoud changed the title PathAccessError Help debugging a PathAccessError Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.