Skip to content

Commit

Permalink
docs(types): add queryset docstrings (#1948)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yongxuanzhang committed Feb 16, 2021
1 parent 5bc4955 commit 92960e7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 9 additions & 0 deletions jina/types/querylang/queryset/dunderkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ def undunder_keys(_dict: Dict) -> Dict:
"""

def f(keys, value):
"""Recursively undunder the keys."""
return {keys[0]: f(keys[1:], value)} if keys else value

def merge(dict1, dict2):
"""Merge two dictionaries."""
key, val = list(dict2.items())[0]

if key in dict1:
Expand Down Expand Up @@ -191,6 +193,13 @@ def dunder_truncate(_dict: Dict) -> Dict:
keylist = list(_dict.keys())

def decide_key(k, klist):
"""
Get the truncated key.
:param k: One element of key list.
:param klist: List of current keys.
:return: Original k if truncated key is not unique else return truncated key.
"""
newkey = dunder_last(k)
return newkey if list(map(dunder_last, klist)).count(newkey) == 1 else k

Expand Down
7 changes: 7 additions & 0 deletions jina/types/querylang/queryset/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def iff(precond: Callable, val: Union[int, str], f: Callable) -> bool:


def guard_type(classinfo: Union[Type[str], Type[Iterable]], val: Union[str, List[int]]) -> Union[str, List[int]]:
"""
Make sure the type of :param:`val` is :param:`classinfo`.
:param classinfo: Guard type.
:param val: Target object.
:return: :param:`val` if it has correct type.
"""
if not isinstance(val, classinfo):
raise LookupyError(f'Value not a {classinfo}')
return val
Expand Down
16 changes: 14 additions & 2 deletions jina/types/querylang/queryset/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class QuerySet:
"""

def __init__(self, data):
"""Set constructor method."""
self.data = data

def filter(self, *args, **kwargs) -> 'QuerySet':
Expand Down Expand Up @@ -209,9 +210,15 @@ class LookupTreeElem:
"""Base class for a child in the _lookup expression tree"""

def __init__(self):
"""Set constructor method."""
self.negate = False

def evaluate(self, item: Dict) -> bool:
"""Evaluates the expression represented by the object for the item, needs to be implemented in subclass.
:param item : (dict) item
:return: (boolean) whether _lookup passed or failed
"""
raise NotImplementedError

def __or__(self, other):
Expand Down Expand Up @@ -239,19 +246,24 @@ class LookupNode(LookupTreeElem):
"""

def __init__(self):
"""Set constructor method."""
super().__init__()
self.children = []
self.op = 'and'

def add_child(self, child):
"""
Add child node into `self.children` list.
:param child: Node needs to be added.
"""
self.children.append(child)

def evaluate(self, item: Dict) -> bool:
"""Evaluates the expression represented by the object for the item
:param item : (dict) item
:return: (boolean) whether _lookup passed or failed
"""
results = map(lambda x: x.evaluate(item), self.children)
result = any(results) if self.op == 'or' else all(results)
Expand All @@ -269,6 +281,7 @@ class LookupLeaf(LookupTreeElem):
"""Class for a leaf in the _lookup expression tree"""

def __init__(self, **kwargs):
"""Set constructor method."""
super().__init__()
self.lookups = kwargs

Expand All @@ -277,7 +290,6 @@ def evaluate(self, item: Dict) -> bool:
:param item : (dict) item
:return: (boolean) whether _lookup passed or failed
"""
result = all(_lookup(k, v, item) for k, v in self.lookups.items())
return not result if self.negate else result
Expand Down

0 comments on commit 92960e7

Please sign in to comment.