Skip to content

Commit a914c0c

Browse files
[io] feat: add support for async write
1 parent 687c426 commit a914c0c

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ jobs:
3636
if: contains(github.event.pull_request.changed_files, 'libs/${{ matrix.library }}/**')
3737
run: |
3838
uv pip install -e libs/core/.[all]
39-
uv pip install -e libs/io/.[all]
39+
uv pip install -e libs/io/.[test,all]
4040
uv pip install -e libs/executors/.[all]
4141
cd libs/${{ matrix.library }}
4242
pytest tests/unit

docs/usage/writers.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ concrete_writer = writer.create_writer('YOUR_WRITER')
6969
concrete_writer.write(sample_report, 'query')
7070
```
7171
///
72-
72+
!!!note
73+
You can use `awrite` method to write report asynchronously.
74+
```python
75+
await concrete_writer.awrite(sample_report, 'query')
76+
```
7377

7478
### Console
7579

libs/io/garf_io/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# limitations under the License.
1414
"""Write GarfReport to anywhere."""
1515

16-
__version__ = '0.1.0'
16+
__version__ = '0.1.1'

libs/io/garf_io/writers/abs_writer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from __future__ import annotations
1717

1818
import abc
19+
import asyncio
1920
import logging
2021
from typing import Literal
2122

@@ -39,6 +40,13 @@ def __init__(
3940
"""Initializes AbsWriter."""
4041
self.array_handling = array_handling
4142
self.array_separator = array_separator
43+
self.kwargs = kwargs
44+
45+
async def awrite(self, report: GarfReport, destination: str) -> str | None:
46+
"""Writes report to destination."""
47+
return await asyncio.to_thread(
48+
self.write, report=report, destination=destination
49+
)
4250

4351
@abc.abstractmethod
4452
def write(self, report: GarfReport, destination: str) -> str | None:

libs/io/pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ dynamic=["version"]
3737
[tool.setuptools.dynamic]
3838
version = {attr = "garf_io.__version__"}
3939

40-
[options.extras_require]
40+
[project.optional-dependencies]
4141
test = [
4242
"pytest",
4343
"pytest-cov",
44+
"pytest-asyncio",
4445
"pandas"
4546
]
46-
47-
[project.optional-dependencies]
4847
bq=[
4948
"google-cloud-bigquery",
5049
"pandas",

libs/io/tests/unit/writers/test_json_writer.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,25 @@ def test_write_single_column_report_returns_correct_data(
4747

4848
assert data == expected
4949

50-
def test_write_single_column_report_returns_correct_data(
50+
@pytest.mark.asyncio
51+
async def test_awrite_single_column_report_returns_correct_data(
52+
self, json_writer, single_column_data, output_folder
53+
):
54+
output = output_folder / _TMP_FILENAME
55+
expected = [
56+
{'column_1': 1},
57+
{'column_1': 2},
58+
{'column_1': 3},
59+
]
60+
61+
await json_writer.awrite(single_column_data, _TMP_FILENAME)
62+
63+
with open(output, 'r') as f:
64+
data = json.load(f)
65+
66+
assert data == expected
67+
68+
def test_write_single_column_report_returns_correct_jsonl_data(
5169
self, single_column_data, output_folder
5270
):
5371
writer = json_writer.JsonWriter(output_folder, format='jsonl')

0 commit comments

Comments
 (0)