adriank / ObjectPath

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

python: is and in comparison operators in selector behavior #72

Closed bitsofinfo closed 5 years ago

bitsofinfo commented 5 years ago

Hi, cool library!

I have a scenario where I want to get all of the "animals" below where the id attribute contains the word test.

How can I do this? Nothing below seems to work. I can get them explicitly as below, but not from a match this prefix kind of way. The in and is comparison operators seems like they should work, but it doesn't appear to.

Python 3.6.4 (default, Mar 15 2018, 16:45:14)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from objectpath import *
>>> x = {
...   "a": "test",
...   "b": [{
...     "animals": [{
...       "id": "test AA1",
...       "value": "something"
...     }, {
...       "id": "test AA2",
...       "value": "whatever"
...     }]
...   }]
... }
>>> tree = Tree(x)
>>> result = tree.execute("$.b[0].animals[@.id in 'test AA1']")
>>> next(result)
{'id': 'test AA1', 'value': 'something'}
>>> result = tree.execute("$.b[0].animals[@.id in 'test AA2']")
>>> next(result)
{'id': 'test AA2', 'value': 'whatever'}
>>> result = tree.execute("$.b[0].animals['test' in @.id]")
>>> next(result)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> result = tree.execute("$.b[0].animals[split(@.id,'')[0] is 'test']")
>>> next(result)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

There seems to be no way to utilize the in or is syntax in this way? Perhaps I am doing it wrong?

bitsofinfo commented 5 years ago

Answered my own question

>>> result = tree.execute("$.b[0].animals[split(@.id,' ')[0] is 'test']")
>>> next(result)
{'id': 'test AA1', 'value': 'something'}
>>> next(result)
{'id': 'test AA2', 'value': 'whatever'}

works!