-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add CSV export capability to DataFrameClient (#45)
- Loading branch information
Showing
20 changed files
with
848 additions
and
419 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from shutil import copyfileobj | ||
|
||
from nisystemlink.clients.dataframe import DataFrameClient | ||
from nisystemlink.clients.dataframe.models import ( | ||
ColumnFilter, | ||
ColumnOrderBy, | ||
ExportFormat, | ||
ExportTableDataRequest, | ||
FilterOperation, | ||
) | ||
|
||
client = DataFrameClient() | ||
|
||
# List a table | ||
response = client.list_tables(take=1) | ||
table = response.tables[0] | ||
|
||
# Export table data with query options | ||
request = ExportTableDataRequest( | ||
columns=["col1"], | ||
order_by=[ColumnOrderBy(column="col2", descending=True)], | ||
filters=[ | ||
ColumnFilter(column="col1", operation=FilterOperation.NotEquals, value="0") | ||
], | ||
response_format=ExportFormat.CSV, | ||
) | ||
|
||
data = client.export_table_data(id=table.id, query=request) | ||
|
||
# Write the export data to a file | ||
with open(f"{table.name}.csv", "wb") as f: | ||
copyfileobj(data, f) | ||
|
||
# Alternatively, load the export data into a pandas dataframe | ||
# import pandas as pd | ||
# df = pd.read_csv(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from ._iterator_file_like import IteratorFileLike | ||
|
||
# flake8: noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import Any, Iterator | ||
|
||
|
||
class IteratorFileLike: | ||
"""A file-like object adapter that wraps a python iterator, providing a way to | ||
read from the iterator as if it was a file. | ||
""" | ||
|
||
def __init__(self, iterator: Iterator[Any]): | ||
self._iterator = iterator | ||
self._buffer = b"" | ||
|
||
def read(self, size: int = -1) -> bytes: | ||
"""Read at most `size` bytes from the file-like object. If `size` is not | ||
specified or is negative, read until the iterator is exhausted and | ||
returns all bytes or characters read. | ||
""" | ||
while size < 0 or len(self._buffer) < size: | ||
try: | ||
chunk = next(self._iterator) | ||
self._buffer += chunk | ||
except StopIteration: | ||
break | ||
if size < 0: | ||
data = self._buffer | ||
self._buffer = b"" | ||
else: | ||
data = self._buffer[:size] | ||
self._buffer = self._buffer[size:] | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.