From 8d40c38720e9a2ec6da26594f1458725ca1128d8 Mon Sep 17 00:00:00 2001 From: ZanSara Date: Wed, 25 Oct 2023 10:13:01 +0200 Subject: [PATCH 1/3] add path field to bytestream and use it in to_file() --- haystack/preview/dataclasses/byte_stream.py | 7 ++++--- test/preview/dataclasses/test_byte_stream.py | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/haystack/preview/dataclasses/byte_stream.py b/haystack/preview/dataclasses/byte_stream.py index fe006fcf85f..f6cce44e2ba 100644 --- a/haystack/preview/dataclasses/byte_stream.py +++ b/haystack/preview/dataclasses/byte_stream.py @@ -1,6 +1,6 @@ from dataclasses import dataclass, field from pathlib import Path -from typing import Dict, Any +from typing import Dict, Any, Optional, Union @dataclass(frozen=True) @@ -11,9 +11,10 @@ class ByteStream: data: bytes metadata: Dict[str, Any] = field(default_factory=dict, hash=False) + path: Optional[Union[str, Path]] = None - def to_file(self, destination_path: Path): - with open(destination_path, "wb") as fd: + def to_file(self): + with open(self.path, "wb") as fd: fd.write(self.data) @classmethod diff --git a/test/preview/dataclasses/test_byte_stream.py b/test/preview/dataclasses/test_byte_stream.py index 05d40eb79e8..2af05cf9a7b 100644 --- a/test/preview/dataclasses/test_byte_stream.py +++ b/test/preview/dataclasses/test_byte_stream.py @@ -28,6 +28,6 @@ def test_to_file(tmp_path, request): test_str = "Hello, world!\n" test_path = tmp_path / request.node.name - ByteStream(test_str.encode()).to_file(test_path) + ByteStream(test_str.encode(), path=test_path).to_file() with open(test_path, "rb") as fd: assert fd.read().decode() == test_str From 3258cb6c202b35d1b34cd6a20aeb58e7baaf59d9 Mon Sep 17 00:00:00 2001 From: ZanSara Date: Wed, 25 Oct 2023 10:19:43 +0200 Subject: [PATCH 2/3] leave to_file untouched --- haystack/preview/dataclasses/byte_stream.py | 4 ++-- test/preview/dataclasses/test_byte_stream.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/haystack/preview/dataclasses/byte_stream.py b/haystack/preview/dataclasses/byte_stream.py index f6cce44e2ba..41d761619a3 100644 --- a/haystack/preview/dataclasses/byte_stream.py +++ b/haystack/preview/dataclasses/byte_stream.py @@ -13,8 +13,8 @@ class ByteStream: metadata: Dict[str, Any] = field(default_factory=dict, hash=False) path: Optional[Union[str, Path]] = None - def to_file(self): - with open(self.path, "wb") as fd: + def to_file(self, destination_path: Path): + with open(destination_path, "wb") as fd: fd.write(self.data) @classmethod diff --git a/test/preview/dataclasses/test_byte_stream.py b/test/preview/dataclasses/test_byte_stream.py index 2af05cf9a7b..05d40eb79e8 100644 --- a/test/preview/dataclasses/test_byte_stream.py +++ b/test/preview/dataclasses/test_byte_stream.py @@ -28,6 +28,6 @@ def test_to_file(tmp_path, request): test_str = "Hello, world!\n" test_path = tmp_path / request.node.name - ByteStream(test_str.encode(), path=test_path).to_file() + ByteStream(test_str.encode()).to_file(test_path) with open(test_path, "rb") as fd: assert fd.read().decode() == test_str From 3a6d6d58dedfb9745c001c4c8a6fb018e311eab2 Mon Sep 17 00:00:00 2001 From: ZanSara Date: Wed, 25 Oct 2023 10:23:00 +0200 Subject: [PATCH 3/3] reno --- releasenotes/notes/path-for-bytestream-7d9b768254707f1f.yaml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 releasenotes/notes/path-for-bytestream-7d9b768254707f1f.yaml diff --git a/releasenotes/notes/path-for-bytestream-7d9b768254707f1f.yaml b/releasenotes/notes/path-for-bytestream-7d9b768254707f1f.yaml new file mode 100644 index 00000000000..542b03f1155 --- /dev/null +++ b/releasenotes/notes/path-for-bytestream-7d9b768254707f1f.yaml @@ -0,0 +1,2 @@ +preview: + - Introduces a `path` optional field to ByteStream.