4

I am learning ObjectPath for python and have found out, e.g., how to do an exact match on an attribute:

>>> import objectpath
>>>
>>> tree = objectpath.Tree({'doc': {'title': 'Purple is Best Color'}})
>>>
>>> tree.execute('$..*[@.title is "Purple is Best Color"]').next()
{'title': 'Purple is Best Color'}

This makes sense to me; I want to start from the root ($) and recursively (..) find all (*) items (@) for which title == "Purple is Best Color". And it works!

But then I try something similar with the in operator:

>>> tree.execute('$..*["Purple" in @.title]').next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

Huh? Seemed like a natural way to tweak the condition, but it's not quite right.

In the manual, I read that in checks if the result of the left side of expression is in array, object or string, and that in objects, keys are matched. (maybe that's my issue, but not sure quite what it means here). I think that my current @ is indeed a string...?

Considering the above, what could I be missing here?

4

2 回答 2

7

有趣的是,显式转换Purple为字符串有效:

$..*[str("Purple") in @.title]

ObjectPath在问题跟踪器上创建了一个问题:

于 2016-01-18T15:34:18.110 回答
4

是一个错误。现在已经修好了。该修复程序可通过 github 获得(git clone https://github.com/adriank/ObjectPath.git)。

于 2016-01-19T21:19:21.323 回答