Skip to content

Commit

Permalink
Revert "feat: add from to dict io (#160)" (#167)
Browse files Browse the repository at this point in the history
This reverts commit 3b169d4.
  • Loading branch information
hanxiao authored Mar 3, 2022
1 parent b15ae10 commit df59998
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 91 deletions.
2 changes: 0 additions & 2 deletions docarray/array/mixins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from .io.common import CommonIOMixin
from .io.csv import CsvIOMixin
from .io.dataframe import DataframeIOMixin
from .io.dict import DictIOMixin
from .io.from_gen import FromGeneratorMixin
from .io.json import JsonIOMixin
from .io.pushpull import PushPullMixin
Expand Down Expand Up @@ -42,7 +41,6 @@ class AllMixins(
CsvIOMixin,
JsonIOMixin,
BinaryIOMixin,
DictIOMixin,
CommonIOMixin,
EmbedMixin,
PushPullMixin,
Expand Down
33 changes: 0 additions & 33 deletions docarray/array/mixins/io/dict.py

This file was deleted.

4 changes: 4 additions & 0 deletions docarray/array/mixins/io/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ def to_json(self, protocol: str = 'jsonschema', **kwargs) -> str:
:return: a Python list
"""
return json.dumps(self.to_list(protocol=protocol, **kwargs))

# to comply with Document interfaces but less semantically accurate
to_dict = to_list
from_dict = from_list
20 changes: 0 additions & 20 deletions docs/fundamentals/documentarray/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Moreover, there is the ability to store/load `DocumentArray` objects to/from dis
- Base64 (compressed): `.from_base64()`/`.to_base64()`
- Protobuf Message: `.from_protobuf()`/`.to_protobuf()`
- Python List: `.from_list()`/`.to_list()`
- Python Dict: `.from_dict()`/`.to_dict()`
- Pandas Dataframe: `.from_dataframe()`/`.to_dataframe()`
- Cloud: `.push()`/`.pull()`

Expand Down Expand Up @@ -325,25 +324,6 @@ da.to_list()
More parameters and usages can be found in the Document-level {ref}`doc-dict`.
```


## From/to dict


Serializing to/from Python dict is less frequently used for the same reason as `Document.to_dict()`: it is often an intermediate step of serializing to JSON. You can do:

```python
from docarray import DocumentArray, Document

da = DocumentArray([Document(text='hello'), Document(text='world')])
da.to_dict()
```

```text
{0: {'id': '3b31cb4c993f11ec8d12787b8ab3f5de', 'mime_type': 'text/plain', 'text': 'hello', 1: {'id': '3b31cca0993f11ec8d12787b8ab3f5de', 'mime_type': 'text/plain', 'text': 'world'}}```
```
## From/to dataframe

```{important}
Expand Down
28 changes: 0 additions & 28 deletions tests/unit/array/mixins/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,34 +172,6 @@ def test_from_to_pd_dataframe(da_cls, config, start_storage):
assert da2[1].tags == {}


@pytest.mark.parametrize(
'da_cls, config',
[
(DocumentArrayInMemory, lambda: None),
(DocumentArraySqlite, lambda: None),
(DocumentArrayPqlite, lambda: PqliteConfig(n_dim=3)),
(DocumentArrayWeaviate, lambda: WeaviateConfig(n_dim=3)),
(DocumentArrayQdrant, lambda: QdrantConfig(n_dim=3)),
],
)
def test_from_to_dict(da_cls, config, start_storage):
da_dict = da_cls.empty(2, config=config()).to_dict()
assert len(da_cls.from_dict(da_dict, config=config())) == 2

# more complicated
da = da_cls.empty(2, config=config())

da[:, 'embedding'] = [[1, 2, 3], [4, 5, 6]]
da[:, 'tensor'] = [[1, 2], [2, 1]]
da[0, 'tags'] = {'hello': 'world'}
da_dict = da.to_dict()

da2 = da_cls.from_dict(da_dict, config=config())

assert da2[0].tags == {'hello': 'world'}
assert da2[1].tags == {}


@pytest.mark.parametrize(
'da_cls, config',
[
Expand Down
9 changes: 1 addition & 8 deletions tests/unit/array/test_from_to_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,14 @@ def test_from_to_protobuf(target_da):

@pytest.mark.parametrize('target', [DocumentArray.empty(10), random_docs(10)])
@pytest.mark.parametrize('protocol', ['jsonschema', 'protobuf'])
@pytest.mark.parametrize('to_fn', ['list', 'json'])
@pytest.mark.parametrize('to_fn', ['dict', 'json'])
def test_from_to_safe_list(target, protocol, to_fn):
da_r = getattr(DocumentArray, f'from_{to_fn}')(
getattr(target, f'to_{to_fn}')(protocol=protocol), protocol=protocol
)
assert da_r == target


@pytest.mark.parametrize('target', [DocumentArray.empty(10), random_docs(10)])
def test_from_to_safe_dict(target):
target_dict = getattr(target, f'to_dict')(target)
da_r = getattr(DocumentArray, f'from_dict')(target_dict)
assert da_r == target


@pytest.mark.parametrize('protocol', ['protobuf', 'pickle'])
@pytest.mark.parametrize('show_progress', [True, False])
def test_push_pull_show_progress(show_progress, protocol):
Expand Down

0 comments on commit df59998

Please sign in to comment.