Skip to content

Commit

Permalink
Added the 'placeholder' write_csv func, fixes deephaven#1542
Browse files Browse the repository at this point in the history
A write_csv func with miniaml number of params as a temporary fillgap
for future full-featured implementation
  • Loading branch information
jmao-denver committed Nov 23, 2021
1 parent fec97ad commit b4bff45
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pyintegration/deephaven2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .dherror import DHError
from .constants import SortDirection
from .csv import read as read_csv
from .csv import write as write_csv
from .table import empty_table, time_table

__all__ = ["read_csv", "DHError", "time_table", "empty_table", "SortDirection"]
__all__ = ["read_csv", "write_csv", "DHError", "time_table", "empty_table", "SortDirection"]
2 changes: 1 addition & 1 deletion pyintegration/deephaven2/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from dataclasses import dataclass


@dataclass
@dataclass(frozen=True)
class Column:
""" A Column object represents a column in a Deephaven Table. """
name: str
Expand Down
18 changes: 16 additions & 2 deletions pyintegration/deephaven2/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Deephaven table out as a CSV file.
"""
from enum import Enum
from typing import Dict, Any
from typing import Dict, Any, List

import jpy

Expand Down Expand Up @@ -112,4 +112,18 @@ def read(path: str,

return Table(j_table=j_table)
except Exception as e:
raise DHError(e, "read_csv failed") from e
raise DHError(e, "read csv failed") from e


def write(table: Table, path: str, cols: List[str] = []) -> None:
""" WriteC a table to a standard CSV file.
Args:
table (Table): the source table
path (str): the path of the CSV file
cols (List[str]): the names of the columns to be written out
Raises:
DHError
"""
table.write_csv(path, cols)
6 changes: 6 additions & 0 deletions pyintegration/deephaven2/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ def __repr__(self):
def get_dh_table(self):
return self._j_table

def write_csv(self: Table, path: str, cols: List[str] = []) -> None:
try:
_JTableTools.writeCsv(self._j_table, path, *cols)
except Exception as e:
raise DHError("write csv failed.") from e

@property
def size(self) -> int:
""" The current number of rows in the table. """
Expand Down
20 changes: 19 additions & 1 deletion pyintegration/tests/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import unittest

from deephaven2 import dtypes, DHError
from deephaven2 import read_csv
from deephaven2 import read_csv, write_csv
from tests.testbase import BaseTestCase


Expand Down Expand Up @@ -49,6 +49,24 @@ def test_read_error_quote(self):

self.assertIsNotNone(cm.exception.compact_traceback)

def test_write(self):
t = read_csv("tests/data/small_sample.csv")
write_csv(t, "./test_write.csv")
t_cols = [col.name for col in t.columns]
t = read_csv("./test_write.csv")
self.assertEqual(t_cols, [col.name for col in t.columns])

col_names = ["Strings", "Longs", "Floats"]
col_types = [dtypes.string, dtypes.long, dtypes.float_]
table_header = {k: v for k, v in zip(col_names, col_types)}
t = read_csv('tests/data/test_csv.csv', header=table_header)
write_csv(t, "./test_write.csv", cols=col_names)
t = read_csv('./test_write.csv')
self.assertEqual(col_names, [c.name for c in t.columns])

import os
os.remove("./test_write.csv")


if __name__ == '__main__':
unittest.main()

0 comments on commit b4bff45

Please sign in to comment.