adriank / ObjectPath

The agile query language for semi-structured data
http://objectpath.org
MIT License
380 stars 93 forks source link

Attribute name with leading digit is not parsed #10

Closed dummylabs closed 10 years ago

dummylabs commented 10 years ago

Thanks for ObjectPath, it is great! I started to play with it recently and found that number-leading atributes are not recognized by parser. This is a part of openweathermap json response:

>>> from objectpath import Tree
>>> tree=Tree({'3h':1})
>>> tree.execute("$.3h")
{'3h': 1}

I also found workaround, which gives me expected result:

>>>> tree.execute("$.*['3h'][0]")
1

But in some complex cases workaround does not work either.

adriank commented 10 years ago

Thanks for kind words!

This is indeed a bug. Should be fixed today.

adriank commented 10 years ago

Fixed, when you have attribute name that begins with number, $, @ or other operator use:

$.'3h' notation.

adriank commented 10 years ago

BTW, can I ask you what do you use OP for? I'm always curious:).

dummylabs commented 10 years ago

Thanks for quick reply, now it works like a charm!

About your question: for my pet project I was looking for a way to store weather information from different forecast services (openweathermap.com, forecast.io, etc.) in a consistent form suitable for further analysis. As json format differs from service to service, I need a way to bring their data to common format.

May be you can help me with the following: can be transformation described below done by an ObjectPath query?

source =  [{'dt': 1, 'rain':3}, {'dt':2, 'snow':5}] 
dest = [{'time':1, 'precip_type':'rain', 'precip_intencity':3}, {'time':2, 'precip_type':'snow', 'precip_intencity':5}]

I came up with two queries solution: one to extract repeating block of data, second query(-es) to transform each block's values using Python dictionary which holds either ObjectPath query or Python function if i need extra logic:

repeating_block_query = '$.*'
transform_block_query = {'time': '$.dt',
                  'precip_type': get_precip_type,
             'precip_intencity': get_precip_intencity}

Is there straight way to do it?

adriank commented 10 years ago

Sure, but can you ask this on https://groups.google.com/forum/#!forum/objectpath or StackOverflow so that others could benefit from it? This issue is closed and not visible to anyone but us:).

dummylabs commented 10 years ago

Ok, thanks!