Skip to content

Commit fca35a0

Browse files
authored
refactor(array): use extend in add improve docs (#51)
1 parent 03fa120 commit fca35a0

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

docarray/array/document.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,10 @@ def __bool__(self):
388388
def __repr__(self):
389389
return f'<{self.__class__.__name__} (length={len(self)}) at {id(self)}>'
390390

391-
def __add__(self, other: 'Document'):
391+
def __add__(self, other: Union['Document', Sequence['Document']]):
392392
v = type(self)()
393-
for doc in self:
394-
v.append(doc)
395-
for doc in other:
396-
v.append(doc)
393+
v.extend(self)
394+
v.extend(other)
397395
return v
398396

399397
def extend(self, values: Iterable['Document']) -> None:

docs/fundamentals/document/serialization.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ DocArray is designed to be "ready-to-wire": it assumes you always want to send/r
77
One should use {ref}`DocumentArray for serializing multiple Documents<docarray-serialization>`, instead of looping over Documents one by one. The former is much faster and yield more compact serialization.
88
```
99

10+
```{hint}
11+
Some readers may wonder: why aren't serialization a part of constructor? They do have similarity. Nonetheless, serialization often contains elements that do not really fit into constructor: input & output model, data schema, compression, extra-dependencies. DocArray made a decision to separate the constructor and serialization for the sake of clarity and maintainability.
12+
```
1013

1114
## From/to JSON
1215

docs/fundamentals/documentarray/construct.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,29 @@ da = DocumentArray(...)
9797
da1 = DocumentArray(da)
9898
```
9999

100+
101+
## Construct from multiple DocumentArray
102+
103+
You can use `+` or `+=` to concatenate DocumentArrays together:
104+
105+
```python
106+
from docarray import DocumentArray
107+
108+
da1 = DocumentArray.empty(3)
109+
da2 = DocumentArray.empty(4)
110+
da3 = DocumentArray.empty(5)
111+
print(da1 + da2 + da3)
112+
113+
da1 += da2
114+
print(da1)
115+
```
116+
117+
```text
118+
<DocumentArray (length=12) at 5024988176>
119+
<DocumentArray (length=7) at 4525853328>
120+
```
121+
122+
100123
## Construct from a single Document
101124

102125
```python
@@ -180,6 +203,7 @@ This will scan all filenames that match the expression and construct Documents w
180203

181204

182205

206+
183207
## What's next?
184208

185209
In the next chapter, we will see how to construct DocumentArray from binary bytes, JSON, CSV, dataframe, Protobuf message.

docs/fundamentals/documentarray/serialization.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ DocArray is designed to be "ready-to-wire" at anytime. Serialization is importan
1212
- Pandas Dataframe: `.from_dataframe()`/`.to_dataframe()`
1313
- Cloud: `.push()`/`.pull()`
1414

15+
16+
17+
18+
1519
## From/to JSON
1620

1721

0 commit comments

Comments
 (0)