DocList
docarray.array.doc_list.doc_list.DocList
Bases: ListAdvancedIndexing[T_doc]
, PushPullMixin
, IOMixinDocList
, AnyDocArray[T_doc]
DocList is a container of Documents.
A DocList is a list of Documents of any schema. However, many
DocList features are only available if these Documents are
homogeneous and follow the same schema. To precise this schema you can use
the DocList[MyDocument]
syntax where MyDocument is a Document class
(i.e. schema). This creates a DocList that can only contains Documents of
the type MyDocument
.
from docarray import BaseDoc, DocList
from docarray.typing import NdArray, ImageUrl
from typing import Optional
class Image(BaseDoc):
tensor: Optional[NdArray[100]] = None
url: ImageUrl
docs = DocList[Image](
Image(url='http://url.com/foo.png') for _ in range(10)
) # noqa: E510
# If your DocList is homogeneous (i.e. follows the same schema), you can access
# fields at the DocList level (for example `docs.tensor` or `docs.url`).
print(docs.url)
# [ImageUrl('http://url.com/foo.png', host_type='domain'), ...]
# You can also set fields, with `docs.tensor = np.random.random([10, 100])`:
import numpy as np
docs.tensor = np.random.random([10, 100])
print(docs.tensor)
# [NdArray([0.11299577, 0.47206767, 0.481723 , 0.34754724, 0.15016037,
# 0.88861321, 0.88317666, 0.93845579, 0.60486676, ... ]), ...]
# You can index into a DocList like a numpy doc_list or torch tensor:
docs[0] # index by position
docs[0:5:2] # index by slice
docs[[0, 2, 3]] # index by list of indices
docs[True, False, True, True, ...] # index by boolean mask
# You can delete items from a DocList like a Python List
del docs[0] # remove first element from DocList
del docs[0:5] # remove elements for 0 to 5 from DocList
Note
If the DocList is homogeneous and its schema contains nested BaseDoc
(i.e, BaseDoc inside a BaseDoc) where the nested Document is Optional
, calling
docs.nested_doc
will return a List of the nested BaseDoc instead of DocList.
This is because the nested field could be None and therefore could not fit into
a DocList.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
Optional[Iterable[T_doc]]
|
iterable of Document |
None
|
Source code in docarray/array/doc_list/doc_list.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
|
append(doc)
Append a Document to the DocList
. The Document must be from the same class
as the .doc_type
of this DocList
otherwise it will fail.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
doc |
T_doc
|
A Document |
required |
Source code in docarray/array/doc_list/doc_list.py
construct(docs)
classmethod
Create a DocList
without validation any data. The data must come from a
trusted source
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
Sequence[T_doc]
|
a Sequence (list) of Document with the same schema |
required |
Returns:
Type | Description |
---|---|
T
|
a |
Source code in docarray/array/doc_list/doc_list.py
extend(docs)
Extend a DocList
with an Iterable of Document. The Documents must be from
the same class as the .doc_type
of this DocList
otherwise it will
fail.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
Iterable[T_doc]
|
Iterable of Documents |
required |
Source code in docarray/array/doc_list/doc_list.py
from_base64(data, protocol='protobuf-array', compress=None, show_progress=False)
classmethod
Deserialize base64 strings into a DocList
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
str
|
Base64 string to deserialize |
required |
protocol |
ProtocolType
|
protocol that was used to serialize |
'protobuf-array'
|
compress |
Optional[str]
|
compress algorithm that was used to serialize between |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
Returns:
Type | Description |
---|---|
T
|
the deserialized |
Source code in docarray/array/doc_list/io.py
from_bytes(data, protocol='protobuf-array', compress=None, show_progress=False)
classmethod
Deserialize bytes into a DocList
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes
|
Bytes from which to deserialize |
required |
protocol |
ProtocolType
|
protocol that was used to serialize |
'protobuf-array'
|
compress |
Optional[str]
|
compression algorithm that was used to serialize between |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
Returns:
Type | Description |
---|---|
T
|
the deserialized |
Source code in docarray/array/doc_list/io.py
from_csv(file_path, encoding='utf-8', dialect='excel')
classmethod
Load a DocList from a csv file following the schema defined in the
.doc_type
attribute.
Every row of the csv file will be mapped to one document in the doc_list.
The column names (defined in the first row) have to match the field names
of the Document type.
For nested fields use "__"-separated access paths, such as 'image__url'
.
List-like fields (including field of type DocList) are not supported.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
path to csv file to load DocList from. |
required |
encoding |
str
|
encoding used to read the csv file. Defaults to 'utf-8'. |
'utf-8'
|
dialect |
Union[str, Dialect]
|
defines separator and how to handle whitespaces etc. Can be a |
'excel'
|
Returns:
Type | Description |
---|---|
T
|
|
Source code in docarray/array/doc_list/io.py
from_dataframe(df)
classmethod
Load a DocList
from a pandas.DataFrame
following the schema
defined in the .doc_type
attribute.
Every row of the dataframe will be mapped to one Document in the doc_list.
The column names of the dataframe have to match the field names of the
Document type.
For nested fields use "__"-separated access paths as column names,
such as 'image__url'
.
List-like fields (including field of type DocList) are not supported.
import pandas as pd
from docarray import BaseDoc, DocList
class Person(BaseDoc):
name: str
follower: int
df = pd.DataFrame(
data=[['Maria', 12345], ['Jake', 54321]], columns=['name', 'follower']
)
docs = DocList[Person].from_dataframe(df)
assert docs.name == ['Maria', 'Jake']
assert docs.follower == [12345, 54321]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
|
required |
Returns:
Type | Description |
---|---|
T
|
|
Source code in docarray/array/doc_list/io.py
from_json(file)
classmethod
Deserialize JSON strings or bytes into a DocList
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
Union[str, bytes, bytearray]
|
JSON object from where to deserialize a |
required |
Returns:
Type | Description |
---|---|
T
|
the deserialized |
Source code in docarray/array/doc_list/io.py
from_protobuf(pb_msg)
classmethod
create a Document from a protobuf message
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pb_msg |
DocListProto
|
The protobuf message from where to construct the |
required |
Source code in docarray/array/doc_list/doc_list.py
get_pushpull_backend(protocol)
classmethod
Get the backend for the given protocol.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol |
PUSH_PULL_PROTOCOL
|
the protocol to use, e.g. 'file', 's3' |
required |
Returns:
Type | Description |
---|---|
Type[AbstractDocStore]
|
the backend class |
Source code in docarray/array/doc_list/pushpull.py
insert(i, doc)
Insert a Document to the DocList
. The Document must be from the same
class as the doc_type of this DocList
otherwise it will fail.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
i |
SupportsIndex
|
index to insert |
required |
doc |
T_doc
|
A Document |
required |
Source code in docarray/array/doc_list/doc_list.py
load_binary(file, protocol='protobuf-array', compress=None, show_progress=False, streaming=False)
classmethod
Load doc_list elements from a compressed binary file.
In case protocol is pickle the Documents
are streamed from disk to save memory usage
Note
If file
is str
it can specify protocol
and compress
as file extensions.
This functionality assumes file=file_name.$protocol.$compress
where $protocol
and $compress
refer to a
string interpolation of the respective protocol
and compress
methods.
For example if file=my_docarray.protobuf.lz4
then the binary data will be loaded assuming protocol=protobuf
and compress=lz4
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
Union[str, bytes, Path, BufferedReader, _LazyRequestReader]
|
File or filename or serialized bytes where the data is stored. |
required |
protocol |
ProtocolType
|
protocol to use. It can be 'pickle-array', 'protobuf-array', 'pickle' or 'protobuf' |
'protobuf-array'
|
compress |
Optional[str]
|
compress algorithm to use between |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
streaming |
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
Union[T, Generator[T_doc, None, None]]
|
a |
Source code in docarray/array/doc_list/io.py
pull(url, show_progress=False, local_cache=True)
classmethod
Pull a DocList
from the specified url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
url specifying the protocol and save name of the |
required |
show_progress |
bool
|
if true, display a progress bar. |
False
|
local_cache |
bool
|
store the downloaded |
True
|
Returns:
Type | Description |
---|---|
DocList
|
a |
Source code in docarray/array/doc_list/pushpull.py
pull_stream(url, show_progress=False, local_cache=False)
classmethod
Pull a stream of Documents from the specified url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
url specifying the protocol and save name of the |
required |
show_progress |
bool
|
if true, display a progress bar. |
False
|
local_cache |
bool
|
store the downloaded |
False
|
Returns:
Type | Description |
---|---|
Iterator[BaseDoc]
|
Iterator of Documents |
Source code in docarray/array/doc_list/pushpull.py
push(url, show_progress=False, **kwargs)
Push this DocList
object to the specified url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
url specifying the protocol and save name of the |
required |
show_progress |
bool
|
If true, a progress bar will be displayed. |
False
|
Source code in docarray/array/doc_list/pushpull.py
push_stream(docs, url, show_progress=False)
classmethod
Push a stream of documents to the specified url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
Iterator[BaseDoc]
|
a stream of documents |
required |
url |
str
|
url specifying the protocol and save name of the |
required |
show_progress |
bool
|
If true, a progress bar will be displayed. |
False
|
Source code in docarray/array/doc_list/pushpull.py
resolve_url(url)
staticmethod
Resolve the URL to the correct protocol and name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
url to resolve |
required |
Source code in docarray/array/doc_list/pushpull.py
save_binary(file, protocol='protobuf-array', compress=None, show_progress=False)
Save DocList into a binary file.
It will use the protocol to pick how to save the DocList.
If used picke-doc_list
and protobuf-array
the DocList will be stored
and compressed at complete level using pickle
or protobuf
.
When using protobuf
or pickle
as protocol each Document in DocList
will be stored individually and this would make it available for streaming.
!!! note
If file
is str
it can specify protocol
and compress
as file extensions.
This functionality assumes file=file_name.$protocol.$compress
where $protocol
and $compress
refer to a
string interpolation of the respective protocol
and compress
methods.
For example if file=my_docarray.protobuf.lz4
then the binary data will be created using protocol=protobuf
and compress=lz4
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
Union[str, Path]
|
File or filename to which the data is saved. |
required |
protocol |
ProtocolType
|
protocol to use. It can be 'pickle-array', 'protobuf-array', 'pickle' or 'protobuf' |
'protobuf-array'
|
compress |
Optional[str]
|
compress algorithm to use between |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
Source code in docarray/array/doc_list/io.py
summary()
Print a summary of this DocList
object and a summary of the schema of its
Document type.
to_base64(protocol='protobuf-array', compress=None, show_progress=False)
Serialize itself into base64 encoded string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol |
ProtocolType
|
protocol to use. It can be 'pickle-array', 'protobuf-array', 'pickle' or 'protobuf' |
'protobuf-array'
|
compress |
Optional[str]
|
compress algorithm to use between |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
Returns:
Type | Description |
---|---|
str
|
the binary serialization in bytes or None if file_ctx is passed where to store |
Source code in docarray/array/doc_list/io.py
to_bytes(protocol='protobuf-array', compress=None, file_ctx=None, show_progress=False)
Serialize itself into bytes
.
For more Pythonic code, please use bytes(...)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol |
ProtocolType
|
protocol to use. It can be 'pickle-array', 'protobuf-array', 'pickle' or 'protobuf' |
'protobuf-array'
|
compress |
Optional[str]
|
compress algorithm to use between : |
None
|
file_ctx |
Optional[BinaryIO]
|
File or filename or serialized bytes where the data is stored. |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
Returns:
Type | Description |
---|---|
Optional[bytes]
|
the binary serialization in bytes or None if file_ctx is passed where to store |
Source code in docarray/array/doc_list/io.py
to_csv(file_path, dialect='excel')
Save a DocList
to a csv file.
The field names will be stored in the first row. Each row corresponds to the
information of one Document.
Columns for nested fields will be named after the "__"-seperated access paths,
such as 'image__url'
for image.url
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
path to a csv file. |
required |
dialect |
Union[str, Dialect]
|
defines separator and how to handle whitespaces etc. Can be a |
'excel'
|
Source code in docarray/array/doc_list/io.py
to_dataframe()
Save a DocList to a pandas.DataFrame
.
The field names will be stored as column names. Each row of the dataframe corresponds
to the information of one Document.
Columns for nested fields will be named after the "__"-seperated access paths,
such as 'image__url'
for image.url
.
Returns:
Type | Description |
---|---|
DataFrame
|
|
Source code in docarray/array/doc_list/io.py
to_doc_vec(tensor_type=NdArray)
Convert the DocList
into a DocVec
. Self
cannot be used
afterward
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tensor_type |
Type[AbstractTensor]
|
Tensor Class used to wrap the doc_vec tensors. This is useful if the BaseDoc has some undefined tensor type like AnyTensor or Union of NdArray and TorchTensor |
NdArray
|
Returns:
Type | Description |
---|---|
DocVec
|
A |
Source code in docarray/array/doc_list/doc_list.py
to_json()
Convert the object into JSON bytes. Can be loaded via .from_json
.
Returns:
Type | Description |
---|---|
str
|
JSON serialization of |
to_protobuf()
Convert DocList
into a Protobuf message
Source code in docarray/array/doc_list/io.py
docarray.array.doc_list.io.IOMixinDocList
Bases: Iterable[T_doc]
Source code in docarray/array/doc_list/io.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 |
|
from_base64(data, protocol='protobuf-array', compress=None, show_progress=False)
classmethod
Deserialize base64 strings into a DocList
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
str
|
Base64 string to deserialize |
required |
protocol |
ProtocolType
|
protocol that was used to serialize |
'protobuf-array'
|
compress |
Optional[str]
|
compress algorithm that was used to serialize between |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
Returns:
Type | Description |
---|---|
T
|
the deserialized |
Source code in docarray/array/doc_list/io.py
from_bytes(data, protocol='protobuf-array', compress=None, show_progress=False)
classmethod
Deserialize bytes into a DocList
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
bytes
|
Bytes from which to deserialize |
required |
protocol |
ProtocolType
|
protocol that was used to serialize |
'protobuf-array'
|
compress |
Optional[str]
|
compression algorithm that was used to serialize between |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
Returns:
Type | Description |
---|---|
T
|
the deserialized |
Source code in docarray/array/doc_list/io.py
from_csv(file_path, encoding='utf-8', dialect='excel')
classmethod
Load a DocList from a csv file following the schema defined in the
.doc_type
attribute.
Every row of the csv file will be mapped to one document in the doc_list.
The column names (defined in the first row) have to match the field names
of the Document type.
For nested fields use "__"-separated access paths, such as 'image__url'
.
List-like fields (including field of type DocList) are not supported.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
path to csv file to load DocList from. |
required |
encoding |
str
|
encoding used to read the csv file. Defaults to 'utf-8'. |
'utf-8'
|
dialect |
Union[str, Dialect]
|
defines separator and how to handle whitespaces etc. Can be a |
'excel'
|
Returns:
Type | Description |
---|---|
T
|
|
Source code in docarray/array/doc_list/io.py
from_dataframe(df)
classmethod
Load a DocList
from a pandas.DataFrame
following the schema
defined in the .doc_type
attribute.
Every row of the dataframe will be mapped to one Document in the doc_list.
The column names of the dataframe have to match the field names of the
Document type.
For nested fields use "__"-separated access paths as column names,
such as 'image__url'
.
List-like fields (including field of type DocList) are not supported.
import pandas as pd
from docarray import BaseDoc, DocList
class Person(BaseDoc):
name: str
follower: int
df = pd.DataFrame(
data=[['Maria', 12345], ['Jake', 54321]], columns=['name', 'follower']
)
docs = DocList[Person].from_dataframe(df)
assert docs.name == ['Maria', 'Jake']
assert docs.follower == [12345, 54321]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
|
required |
Returns:
Type | Description |
---|---|
T
|
|
Source code in docarray/array/doc_list/io.py
from_json(file)
classmethod
Deserialize JSON strings or bytes into a DocList
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
Union[str, bytes, bytearray]
|
JSON object from where to deserialize a |
required |
Returns:
Type | Description |
---|---|
T
|
the deserialized |
Source code in docarray/array/doc_list/io.py
from_protobuf(pb_msg)
classmethod
create a Document from a protobuf message
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pb_msg |
DocListProto
|
The protobuf message from where to construct the DocList |
required |
Source code in docarray/array/doc_list/io.py
load_binary(file, protocol='protobuf-array', compress=None, show_progress=False, streaming=False)
classmethod
Load doc_list elements from a compressed binary file.
In case protocol is pickle the Documents
are streamed from disk to save memory usage
Note
If file
is str
it can specify protocol
and compress
as file extensions.
This functionality assumes file=file_name.$protocol.$compress
where $protocol
and $compress
refer to a
string interpolation of the respective protocol
and compress
methods.
For example if file=my_docarray.protobuf.lz4
then the binary data will be loaded assuming protocol=protobuf
and compress=lz4
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
Union[str, bytes, Path, BufferedReader, _LazyRequestReader]
|
File or filename or serialized bytes where the data is stored. |
required |
protocol |
ProtocolType
|
protocol to use. It can be 'pickle-array', 'protobuf-array', 'pickle' or 'protobuf' |
'protobuf-array'
|
compress |
Optional[str]
|
compress algorithm to use between |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
streaming |
bool
|
if |
False
|
Returns:
Type | Description |
---|---|
Union[T, Generator[T_doc, None, None]]
|
a |
Source code in docarray/array/doc_list/io.py
save_binary(file, protocol='protobuf-array', compress=None, show_progress=False)
Save DocList into a binary file.
It will use the protocol to pick how to save the DocList.
If used picke-doc_list
and protobuf-array
the DocList will be stored
and compressed at complete level using pickle
or protobuf
.
When using protobuf
or pickle
as protocol each Document in DocList
will be stored individually and this would make it available for streaming.
!!! note
If file
is str
it can specify protocol
and compress
as file extensions.
This functionality assumes file=file_name.$protocol.$compress
where $protocol
and $compress
refer to a
string interpolation of the respective protocol
and compress
methods.
For example if file=my_docarray.protobuf.lz4
then the binary data will be created using protocol=protobuf
and compress=lz4
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file |
Union[str, Path]
|
File or filename to which the data is saved. |
required |
protocol |
ProtocolType
|
protocol to use. It can be 'pickle-array', 'protobuf-array', 'pickle' or 'protobuf' |
'protobuf-array'
|
compress |
Optional[str]
|
compress algorithm to use between |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
Source code in docarray/array/doc_list/io.py
to_base64(protocol='protobuf-array', compress=None, show_progress=False)
Serialize itself into base64 encoded string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol |
ProtocolType
|
protocol to use. It can be 'pickle-array', 'protobuf-array', 'pickle' or 'protobuf' |
'protobuf-array'
|
compress |
Optional[str]
|
compress algorithm to use between |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
Returns:
Type | Description |
---|---|
str
|
the binary serialization in bytes or None if file_ctx is passed where to store |
Source code in docarray/array/doc_list/io.py
to_bytes(protocol='protobuf-array', compress=None, file_ctx=None, show_progress=False)
Serialize itself into bytes
.
For more Pythonic code, please use bytes(...)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol |
ProtocolType
|
protocol to use. It can be 'pickle-array', 'protobuf-array', 'pickle' or 'protobuf' |
'protobuf-array'
|
compress |
Optional[str]
|
compress algorithm to use between : |
None
|
file_ctx |
Optional[BinaryIO]
|
File or filename or serialized bytes where the data is stored. |
None
|
show_progress |
bool
|
show progress bar, only works when protocol is |
False
|
Returns:
Type | Description |
---|---|
Optional[bytes]
|
the binary serialization in bytes or None if file_ctx is passed where to store |
Source code in docarray/array/doc_list/io.py
to_csv(file_path, dialect='excel')
Save a DocList
to a csv file.
The field names will be stored in the first row. Each row corresponds to the
information of one Document.
Columns for nested fields will be named after the "__"-seperated access paths,
such as 'image__url'
for image.url
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_path |
str
|
path to a csv file. |
required |
dialect |
Union[str, Dialect]
|
defines separator and how to handle whitespaces etc. Can be a |
'excel'
|
Source code in docarray/array/doc_list/io.py
to_dataframe()
Save a DocList to a pandas.DataFrame
.
The field names will be stored as column names. Each row of the dataframe corresponds
to the information of one Document.
Columns for nested fields will be named after the "__"-seperated access paths,
such as 'image__url'
for image.url
.
Returns:
Type | Description |
---|---|
DataFrame
|
|
Source code in docarray/array/doc_list/io.py
to_json()
Convert the object into JSON bytes. Can be loaded via .from_json
.
Returns:
Type | Description |
---|---|
str
|
JSON serialization of |
to_protobuf()
Convert DocList
into a Protobuf message
Source code in docarray/array/doc_list/io.py
docarray.array.doc_list.pushpull.PushPullMixin
Bases: Iterable['BaseDoc']
Mixin class for push/pull functionality.
Source code in docarray/array/doc_list/pushpull.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
|
get_pushpull_backend(protocol)
classmethod
Get the backend for the given protocol.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol |
PUSH_PULL_PROTOCOL
|
the protocol to use, e.g. 'file', 's3' |
required |
Returns:
Type | Description |
---|---|
Type[AbstractDocStore]
|
the backend class |
Source code in docarray/array/doc_list/pushpull.py
pull(url, show_progress=False, local_cache=True)
classmethod
Pull a DocList
from the specified url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
url specifying the protocol and save name of the |
required |
show_progress |
bool
|
if true, display a progress bar. |
False
|
local_cache |
bool
|
store the downloaded |
True
|
Returns:
Type | Description |
---|---|
DocList
|
a |
Source code in docarray/array/doc_list/pushpull.py
pull_stream(url, show_progress=False, local_cache=False)
classmethod
Pull a stream of Documents from the specified url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
url specifying the protocol and save name of the |
required |
show_progress |
bool
|
if true, display a progress bar. |
False
|
local_cache |
bool
|
store the downloaded |
False
|
Returns:
Type | Description |
---|---|
Iterator[BaseDoc]
|
Iterator of Documents |
Source code in docarray/array/doc_list/pushpull.py
push(url, show_progress=False, **kwargs)
Push this DocList
object to the specified url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
url specifying the protocol and save name of the |
required |
show_progress |
bool
|
If true, a progress bar will be displayed. |
False
|
Source code in docarray/array/doc_list/pushpull.py
push_stream(docs, url, show_progress=False)
classmethod
Push a stream of documents to the specified url.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
docs |
Iterator[BaseDoc]
|
a stream of documents |
required |
url |
str
|
url specifying the protocol and save name of the |
required |
show_progress |
bool
|
If true, a progress bar will be displayed. |
False
|
Source code in docarray/array/doc_list/pushpull.py
resolve_url(url)
staticmethod
Resolve the URL to the correct protocol and name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url |
str
|
url to resolve |
required |