jina.types.querylang.queryset.lookup

Originally from https://github.com/naiquevin/lookupy

The library is provided as-is under the MIT License

Copyright (c) 2013 Vineet Naik (naikvin@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class jina.types.querylang.queryset.lookup.QuerySet(data)[source]

Bases: object

Provides an interface to filter data and select specific fields from the data

QuerySet is used for filtering data and also selecting only relevant fields out of it. This object is internally created which means usually you, the user wouldn’t need to create it.

Parameters

data – an iterable of dicts

Set constructor method.

filter(*args, **kwargs)[source]

Filters data using the _lookup parameters

Lookup parameters can be passed as,

  1. keyword arguments of type field__lookuptype=value where lookuptype specifies how to “query” eg:

    >>> c.items.filter(language__contains='java')
    

    above will match all items where the language field contains the substring ‘java’ such as ‘java’, ‘javascript’. Each look up is treated as a conditional clause and if multiple of them are passed, they are combined using logical the and operator

    For nested fields, double underscore can be used eg:

    >>> data = [{'a': {'b': 3}}, {'a': {'b': 10}}]
    >>> c = Collection(data)
    >>> c.items.filter(a__b__gt=5)
    

    above _lookup will match the 2nd element (b > 5)

    For the list of supported _lookup parameter, see documentation on Github

  2. pos arguments of the type field__lookuptype=Q(...). These can be useful to build conditional clauses that need to be combined using logical or or negated using not

    >>> c.items.filter(Q(language__exact='Python')
                       |
                       Q(language__exact='Ruby')
    

    above query will only filter the data where language is either ‘Python’ or ‘Ruby’

For more documentation see README on Github

:param args : Q objects :param kwargs : _lookup parameters :rtype: QuerySet :return: QuerySet of the filtered docs

select(*args, **kwargs)[source]

Selects specific fields of the data

e.g. to select just the keys ‘framework’ and ‘type’ from many keys,

>>> c.items.select('framework', 'type')

:param args : field names to select :param kwargs : optional keyword args :rtype: QuerySet :return: subset of fields

jina.types.querylang.queryset.lookup.Collection

alias of jina.types.querylang.queryset.lookup.QuerySet

jina.types.querylang.queryset.lookup.filter_items(items, *args, **kwargs)[source]

Filters an iterable using _lookup parameters

:param items : iterable :param args : Q objects :param kwargs : _lookup parameters :rtype: Iterable :return: lazy iterable (generator)

class jina.types.querylang.queryset.lookup.LookupTreeElem[source]

Bases: object

Base class for a child in the _lookup expression tree

Set constructor method.

evaluate(item)[source]

Evaluates the expression represented by the object for the item, needs to be implemented in subclass.

:param item : (dict) item :rtype: bool :return: (boolean) whether _lookup passed or failed

class jina.types.querylang.queryset.lookup.LookupNode[source]

Bases: jina.types.querylang.queryset.lookup.LookupTreeElem

A node (element having children) in the _lookup expression tree

Typically it’s any object composed of two Q objects eg:

>>> Q(language__neq='Ruby') | Q(framework__startswith='S')
>>> ~Q(language__exact='PHP')

Set constructor method.

add_child(child)[source]

Add child node into self.children list.

Parameters

child – Node needs to be added.

evaluate(item)[source]

Evaluates the expression represented by the object for the item

:param item : (dict) item :rtype: bool :return: (boolean) whether _lookup passed or failed

class jina.types.querylang.queryset.lookup.LookupLeaf(**kwargs)[source]

Bases: jina.types.querylang.queryset.lookup.LookupTreeElem

Class for a leaf in the _lookup expression tree

Set constructor method.

evaluate(item)[source]

Evaluates the expression represented by the object for the item

:param item : (dict) item :rtype: bool :return: (boolean) whether _lookup passed or failed

jina.types.querylang.queryset.lookup.Q

alias of jina.types.querylang.queryset.lookup.LookupLeaf

jina.types.querylang.queryset.lookup.include_keys(items, fields)[source]

Function to keep only specified fields in data

Returns a list of dict with only the keys mentioned in the fields param:

>>> include_keys(items, ['request__url', 'response__status'])

Note: the resulting keys are “dundered”, as they appear in fields, rather than nested as they are in items.

:param items : iterable of dicts :param fields : (iterable) fieldnames to keep :rtype: Iterable[Dict] :return: lazy iterable