Did I find the right examples for you? yes no

All Samples(10)  |  Call(9)  |  Derive(0)  |  Import(1)
Filter data for a given MongoDB query.

Only a subset of MongoDB query language is supported:

- $gt
- $gte
- $lt
- $lte
- $ne
- $in(more...)

        def query_data(data, q):
    """ Filter data for a given MongoDB query.

    Only a subset of MongoDB query language is supported:

    - $gt
    - $gte
    - $lt
    - $lte
    - $ne
    - $in
    - $nin

    You can combine multiple operator like {"val": {"$gt": 10, "$lt": 20}}

    :type data: list
    :param data: List of dict

    :type q: dict
    :param q: MongoDB Query

    """
    for k, f in q.items():
        if isinstance(f, dict):
            for _filter, val in f.items():
                _f = lambda x: True
                if _filter.startswith("$"):
                    if _filter == "$gt":
                        _f = lambda x: k in x and x[k] > val
                    elif _filter == "$gte":
                        _f = lambda x: k in x and x[k] >= val
                    elif _filter == "$lt":
                        _f = lambda x: k in x and x[k] < val
                    elif _filter == "$lte":
                        _f = lambda x: k in x and x[k] <= val
                    elif _filter == "$ne":
                        _f = lambda x: k in x and x[k] != val
                    elif _filter == "$in":
                        _f = lambda x: k in x and x[k] in val
                    elif _filter == "$nin":
                        _f = lambda x: k in x and not x[k] in val
                else:
                    _f = lambda x: k in x and x[k] == f
                data = list(ifilter(_f, data))
        else:
            _f = lambda x: k in x and x[k] == f
            data = list(ifilter(_f, data))
    return data
        


src/e/v/eve-mocker-0.1.0/test_eve_mocker.py   eve-mocker(Download)
from httpretty import HTTPretty
from sure import expect
from eve_mocker import EveMocker, query_data
from urlparse import urljoin
from functools import partial
    def testQueryData(self):
        """ Test if mongo query syntax works, testing the raw query_data """
        test_data = [{"testpk": i} for i in range(50)]
        res = query_data(test_data, {"testpk": 10})
 
        expect(res).to.have.length_of(1)
        expect(res[0]).to.equal(test_data[10])
 
        res = query_data(test_data, {"testpk": {"$gte": 10}})
        expect(res).to.have.length_of(40)
 
        res = query_data(test_data, {"testpk": {"$gte": 10, "$lte": 15}})
        expect(res).to.have.length_of(6)
 
        res = query_data(test_data, {"badkey": {"$gte": 10, "$lte": 15}})