adriank / ObjectPath

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

int values must be converted to float for comparison #58

Closed FerriSaraber closed 5 years ago

FerriSaraber commented 6 years ago

A query looking for a specific float number will match the exact float and for some reason also the integer. E.g. looking for the number 9.891 will find 9.891 but also 9, however 9.8 will not be found.

Somehow the query does work if the value is converted into a float.

from objectpath import Tree

input = {
    "item_1": {
        "value": "foo",
        "x": 5.6,
        "y": 9
    },
    "item_2": {
        "value": "bar",
        "x": 5.6,
        "y": 9.891
    },
    "item_3": {
        "value": "foobar",
        "x": 5.6,
        "y": 9.8
    }
}

op = Tree(input)

query = "$..*[@.x is 5.6 and @.y is 9.891].value"
result = list(op.execute(query))
>>> ['foo', 'bar']

query = "$..*[float(@.x) is 5.6 and float(@.y) is 9.891].value"
result = list(op.execute(query))
>>> ['bar']
adriank commented 6 years ago

I'll have some time to dig into it around 21 Jan. I would appreciate if you could add the test case above to ObjectPath tests file. Or even better help me by checking if this is an issue with Python or a bug in my code.

adriank commented 5 years ago

Should be fixed now.