8000 refactor: rename _docs_proto from DocumentArray by JoanFM · Pull Request #2605 · jina-ai/serve · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: rename _docs_proto from DocumentArray #2605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
8 changes: 4 additions & 4 deletions jina/types/arrays/chunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ class ChunkArray(DocumentArray):
:class:`ChunkArray` inherits from :class:`DocumentArray`.
It's a subset of Documents.

:param docs_proto: Set of sub-documents (i.e chunks) of `reference_doc`
:param doc_views: Set of sub-documents (i.e chunks) of `reference_doc`
:param reference_doc: Reference :class:`Document` for the sub-documents
"""

def __init__(self, docs_proto, reference_doc: 'Document'):
def __init__(self, doc_views, reference_doc: 'Document'):
"""
Set constructor method.

:param docs_proto: protobuf representation of the chunks
:param doc_views: protobuf representation of the chunks
:param reference_doc: parent document
"""
self._ref_doc = reference_doc
super().__init__(docs_proto)
super().__init__(doc_views)

def append(self, document: 'Document', **kwargs) -> 'Document':
"""Add a sub-document (i.e chunk) to the current Document.
Expand Down
74 changes: 37 additions & 37 deletions jina/types/arrays/document.py
10000
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,24 @@ class DocumentArray(
It gives an efficient view of a list of Document. One can iterate over it like
a generator but ALSO modify it, count it, get item, or union two 'DocumentArray's using the '+' and '+=' operators.

:param docs_proto: A list of :class:`Document`
:type docs_proto: Optional[Union['RepeatedContainer', Iterable['Document']]]
:param doc_views: A list of :class:`Document`
:type doc_views: Optional[Union['RepeatedContainer', Iterable['Document']]]
"""

def __init__(
self,
docs_proto: Optional[Union['RepeatedContainer', Iterable['Document']]] = None,
doc_views: Optional[Union['RepeatedContainer', Iterable['Document']]] = None,
):
super().__init__()
if docs_proto is not None:
if doc_views is not None:
from .memmap import DocumentArrayMemmap

if isinstance(docs_proto, (Generator, DocumentArrayMemmap)):
self._docs_proto = list(docs_proto)
if isinstance(doc_views, (Generator, DocumentArrayMemmap)):
self._doc_views = list(doc_views)
else:
self._docs_proto = docs_proto
self._doc_views = doc_views
else:
self._docs_proto = []
self._doc_views = []

def insert(self, index: int, doc: 'Document') -> None:
"""
Expand All @@ -124,7 +124,7 @@ def insert(self, index: int, doc: 'Document') -> None:
:param index: Position of the insertion.
:param doc: The doc needs to be inserted.
"""
self._docs_proto.insert(index, doc.proto)
self._doc_views.insert(index, doc.proto)

def __setitem__(self, key, value: 'Document'):
if isinstance(key, int):
Expand All @@ -136,29 +136,29 @@ def __setitem__(self, key, value: 'Document'):

def __delitem__(self, index: Union[int, str, slice]):
if isinstance(index, int):
del self._docs_proto[index]
del self._doc_views[index]
elif isinstance(index, str):
del self[self._id_to_index[index]]
elif isinstance(index, slice):
del self._docs_proto[index]
del self._doc_views[index]
else:
raise IndexError(
f'do not support this index type {typename(index)}: {index}'
)

def __eq__(self, other):
return (
type(self._docs_proto) is type(other._docs_proto)
and self._docs_proto == other._docs_proto
type(self._doc_views) is type(other._doc_views)
and self._doc_views == other._doc_views
)

def __len__(self):
return len(self._docs_proto)
return len(self._doc_views)

def __iter__(self) -> Iterator['Document']:
from ..document import Document

for d in self._docs_proto:
for d in self._doc_views:
yield Document(d)

def __contains__(self, item: str):
Expand All @@ -168,11 +168,11 @@ def __getitem__(self, item: Union[int, str, slice]):
from ..document import Document

if isinstance(item, int):
return Document(self._docs_proto[item])
return Document(self._doc_views[item])
elif isinstance(item, str):
return self[self._id_to_index[item]]
elif isinstance(item, slice):
return DocumentArray(self._docs_proto[item])
return DocumentArray(self._doc_views[item])
else:
raise IndexError(f'do not support this index type {typename(item)}: {item}')

Expand All @@ -195,7 +195,7 @@ def append(self, doc: 'Document'):

:param doc: The doc needs to be appended.
"""
self._docs_proto.append(doc.proto)
self._doc_views.append(doc.proto)

def extend(self, iterable: Iterable['Document']) -> None:
"""
Expand All @@ -208,28 +208,28 @@ def extend(self, iterable: Iterable['Document']) -> None:

def clear(self):
"""Clear the data of :class:`DocumentArray`"""
del self._docs_proto[:]
del self._doc_views[:]

def reverse(self):
"""In-place reverse the sequence."""
if isinstance(self._docs_proto, RepeatedContainer):
size = len(self._docs_proto)
if isinstance(self._doc_views, RepeatedContainer):
size = len(self._doc_views)
hi_idx = size - 1
for i in range(int(size / 2)):
tmp = DocumentProto()
tmp.CopyFrom(self._docs_proto[hi_idx])
self._docs_proto[hi_idx].CopyFrom(self._docs_proto[i])
self._docs_proto[i].CopyFrom(tmp)
tmp.CopyFrom(self._doc_views[hi_idx])
self._doc_views[hi_idx].CopyFrom(self._doc_views[i])
self._doc_views[i].CopyFrom(tmp)
hi_idx -= 1
elif isinstance(self._docs_proto, list):
self._docs_proto.reverse()
elif isinstance(self._doc_views, list):
self._doc_views.reverse()

@cached_property
def _id_to_index(self):
"""Returns a doc_id to index in list

.. # noqa: DAR201"""
return {d.id: i for i, d in enumerate(self._docs_proto)}
return {d.id: i for i, d in enumerate(self._doc_views)}

def sort(self, *args, **kwargs):
"""
Expand All @@ -238,7 +238,7 @@ def sort(self, *args, **kwargs):
:param args: variable set of arguments to pass to the sorting underlying function
:param kwargs: keyword arguments to pass to the sorting underlying function
"""
self._docs_proto.sort(*args, **kwargs)
self._doc_views.sort(*args, **kwargs)

def __bool__(self):
"""To simulate ```l = []; if l: ...```
Expand All @@ -250,22 +250,22 @@ def __bool__(self):
def __str__(self):
from ..document import Document

if hasattr(self._docs_proto, '__len__'):
content = f'{self.__class__.__name__} has {len(self._docs_proto)} items'
if hasattr(self._doc_views, '__len__'):
content = f'{self.__class__.__name__} has {len(self._doc_views)} items'

if len(self._docs_proto) > 3:
if len(self._doc_views) > 3:
content += ' (showing first three)'
else:
content = 'unknown length array'

content += ':\n'
content += ',\n'.join(str(D 8000 ocument(d)) for d in self._docs_proto[:3])
content += ',\n'.join(str(Document(d)) for d in self._doc_views[:3])

return content

def __repr__(self):
content = ' '.join(
f'{k}={v}' for k, v in {'length': len(self._docs_proto)}.items()
f'{k}={v}' for k, v in {'length': len(self._doc_views)}.items()
)
content += f' at {id(self)}'
content = content.strip()
Expand Down Expand Up @@ -320,11 +320,11 @@ def save_binary(self, file: Union[str, BinaryIO]) -> None:

with file_ctx as fp:
dap = DocumentArrayProto()
if self._docs_proto:
if isinstance(self._docs_proto[0], DocumentProto):
dap.docs.extend(self._docs_proto)
if self._doc_views:
if isinstance(self._doc_views[0], DocumentProto):
dap.docs.extend(self._doc_views)
else:
dap.docs.extend([d.proto for d in self._docs_proto])
dap.docs.extend([d.proto for d in self._doc_views])
fp.write(dap.SerializeToString())

def save_json(self, file: Union[str, TextIO]) -> None:
Expand Down
6 changes: 3 additions & 3 deletions jina/types/arrays/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class MatchArray(DocumentArray):
:class:`MatchArray` inherits from :class:`DocumentArray`.
It's a subset of Documents that represents the matches

:param docs_proto: Set of matches of the `reference_doc`
:param doc_views: Set of matches of the `reference_doc`
:param reference_doc: Reference :class:`Document` for the sub-documents
"""

def __init__(self, docs_proto, reference_doc: 'Document'):
def __init__(self, doc_views, reference_doc: 'Document'):
self._ref_doc = reference_doc
super().__init__(docs_proto)
super().__init__(doc_views)

def append(self, document: 'Document', **kwargs) -> 'Document':
"""Add a matched document to the current Document.
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/types/arrays/test_chunkarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def chunks(document_factory):

@pytest.fixture
def chunkarray(chunks, reference_doc):
return ChunkArray(docs_proto=chunks, reference_doc=reference_doc)
return ChunkArray(doc_views=chunks, reference_doc=reference_doc)


def test_append_from_documents(chunkarray, document_factory, reference_doc):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/types/arrays/test_matcharray.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def matches(document_factory):

@pytest.fixture
def matcharray(matches, reference_doc):
return MatchArray(docs_proto=matches, reference_doc=reference_doc)
return MatchArray(doc_views=matches, reference_doc=reference_doc)


def test_append_from_documents(matcharray, document_factory, reference_doc):
Expand Down
0