Giter Site home page Giter Site logo

Comments (2)

brimoor avatar brimoor commented on May 21, 2024

Here's an implementation of the parser for python-like string matches:

import json

# https://docs.mongodb.com/manual/reference/operator/query-comparison
_OPERATORS = {
    ">": "$gt",
    ">=": "$gte",
    "==": "$eq",
    "!=": "$ne",
    "<": "$lt",
    "<=": "$lte",
    "in": "$in",
    "not in": "$nin",
}

def parse_query(s):
    chunks = s.split()

    if len(chunks) < 3:
        raise ValueError(
            "Invalid query string '%s'; all queries must have the syntax "
            "'<field> <operator> <value>'" % s
        )

    if (len(chunks) > 3) and (chunks[1:3] == ["not", "in"]):
        chunks = [chunks[0], "not in"] + chunks[3:]

    if len(chunks) > 3:
        chunks = chunks[:2] + [" ".join(chunks[2:])]

    field, op, val = chunks

    if op not in _OPERATORS:
        raise ValueError(
            "Unsupported comparison operation '%s'; the supported operations "
            "are {%s}" % (op, ", ".join(_OPERATORS.keys()))
        )

    try:
        return {field: {_OPERATORS[op]: json.loads(val)}}
    except json.JSONDecodeError:
        raise ValueError("Query value '%s' must be JSON parsable" % val)

Example uses:

print(parse_query("metadata.size_bytes > 1200"))
# {'metadata.size_bytes': {'$gt': 1200}}

print(parse_query('tag not in ["test", "train"]'))
# {'tag': {'$nin': ['test', 'train']}}

print(parse_query("metadata.size_bytes ~= 1200"))
# ValueError: Unsupported comparison operation '~='; the supported operations are {>, >=, ==, !=, <, <=, in, not in}

print(parse_query('tag not in {"test", "train"}'))
# ValueError: Query value '{"test", "train"}' must be JSON parsable

from fiftyone.

tylerganter avatar tylerganter commented on May 21, 2024

Resolved by #290

from fiftyone.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.