diff --git a/README.md b/README.md index 9bb988f37b2a8..36dea7af5f308 100644 --- a/README.md +++ b/README.md @@ -135,8 +135,7 @@ with f: ```python # retrieve top-3 neighbours of 🐲, this print 🐲🐦🐢 with score 0, 1, 1 respectively with f: - f.search(docs[0], top_k=3, - on_done=lambda x: [print(m.dict()) for m in x.docs[0].matches]) + f.search(docs[0], top_k=3, on_done=lambda x: print(x.docs[0].matches)) ``` diff --git a/jina/types/sets/document.py b/jina/types/sets/document.py index b2ff9dd9d5301..2850f55aea164 100644 --- a/jina/types/sets/document.py +++ b/jina/types/sets/document.py @@ -4,6 +4,8 @@ import numpy as np +from ...helper import typename + try: # when protobuf using Cpp backend from google.protobuf.pyext._message import RepeatedCompositeContainer as RepeatedContainer @@ -167,3 +169,16 @@ def new(self) -> 'Document': """Create a new empty document appended to the end of the set""" from ..document import Document return self.append(Document()) + + def __str__(self): + from ..document import Document + content = ',\n'.join(str(Document(d)) for d in self._docs_proto[:3]) + if len(self._docs_proto) > 3: + content += f'in total {len(self._docs_proto)} items' + return content + + def __repr__(self): + content = ' '.join(f'{k}={v}' for k, v in {'length': len(self._docs_proto)}.items()) + content += f' at {id(self)}' + content = content.strip() + return f'<{typename(self)} {content}>' diff --git a/jina/types/sets/match.py b/jina/types/sets/match.py index 53ff8b43f739a..78ca9c2d24089 100644 --- a/jina/types/sets/match.py +++ b/jina/types/sets/match.py @@ -40,4 +40,4 @@ def granularity(self) -> int: @property def adjacency(self) -> int: """The adjacency of all document in this set """ - return self._ref_doc.adjacency + 1 + return self._ref_doc.adjacency + 1 \ No newline at end of file diff --git a/tests/unit/types/test_repr_str.py b/tests/unit/types/test_repr_str.py index d3dfbaff6e9b9..7fb98bbfeb583 100644 --- a/tests/unit/types/test_repr_str.py +++ b/tests/unit/types/test_repr_str.py @@ -3,13 +3,17 @@ from jina import Document, Request, QueryLang, NdArray from jina.types.score import NamedScore +from jina.types.sets.chunk import ChunkSet +from jina.types.sets.match import MatchSet @pytest.mark.parametrize('obj', [Document(), Request(), QueryLang(), NamedScore(), - NdArray()]) + NdArray(), + MatchSet([Document()], Document()), + ChunkSet([Document()], Document())]) def test_builtin_str_repr_no_content(obj): print(obj) print(f'{obj!r}') @@ -24,6 +28,6 @@ def test_builtin_str_repr_no_content(obj): ref_id='10' * 16, description='score description'), NdArray(np.random.random([3, 5]))]) -def test_builtin_str_repr_no_content(obj): +def test_builtin_str_repr_has_content(obj): print(obj) print(f'{obj!r}')