Skip to content

Commit

Permalink
simplify again
Browse files Browse the repository at this point in the history
  • Loading branch information
lithomas1 committed Jun 29, 2024
1 parent e57a677 commit 7806ce4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
3 changes: 1 addition & 2 deletions python/cudf/cudf/_lib/pylibcudf/io/json.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from libcpp cimport bool
from libcpp.limits cimport numeric_limits
from libcpp.string cimport string
from libcpp.utility cimport move

from cudf._lib.pylibcudf.io.types cimport SinkInfo, TableWithMetadata
from cudf._lib.pylibcudf.libcudf.io.json cimport (
Expand Down Expand Up @@ -49,7 +48,7 @@ cpdef void write_json(
cdef table_metadata tbl_meta = table_w_meta.metadata
cdef string na_rep_c = na_rep.encode()

cdef json_writer_options options = move(
cdef json_writer_options options = (
json_writer_options.builder(sink_info.c_obj, table_w_meta.tbl.view())
.metadata(tbl_meta)
.na_rep(na_rep_c)
Expand Down
53 changes: 25 additions & 28 deletions python/cudf/cudf/_lib/pylibcudf/io/types.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cdef class TableWithMetadata:
def __init__(self, Table tbl, list column_names):
self.tbl = tbl

self.metadata.schema_info = move(self._make_column_info(column_names))
self.metadata.schema_info = self._make_column_info(column_names)

cdef vector[column_name_info] _make_column_info(self, list column_names):
cdef vector[column_name_info] col_name_infos
Expand All @@ -56,7 +56,7 @@ cdef class TableWithMetadata:
raise ValueError("Column name must be a string!")

info.name = <string> name.encode()
info.children = move(self._make_column_info(child_names))
info.children = self._make_column_info(child_names)

col_name_infos.push_back(info)

Expand Down Expand Up @@ -207,38 +207,35 @@ cdef class SinkInfo:

cdef object initial_sink_cls = type(sinks[0])

for s in sinks:
if not isinstance(s, initial_sink_cls):
raise ValueError("All sinks must be of the same type!")
if isinstance(s, str):
paths.reserve(len(sinks))
paths.push_back(<string> s.encode())
elif isinstance(s, os.PathLike):
paths.reserve(len(sinks))
paths.push_back(<string> os.path.expanduser(s).encode())
else:
data_sinks.reserve(len(sinks))
if isinstance(s, (io.StringIO, io.BytesIO)):
if not all(isinstance(s, initial_sink_cls) for s in sinks):
raise ValueError("All sinks must be of the same type!")

if isinstance(sinks[0], os.PathLike):
sinks = [os.path.expanduser(s) for s in sinks]

if isinstance(sinks[0], (io.StringIO, io.BytesIO, io.TextIOBase)):
data_sinks.reserve(len(sinks))
if isinstance(sinks[0], (io.StringIO, io.BytesIO)):
for s in sinks:
self.sink_storage.push_back(
unique_ptr[data_sink](new iobase_data_sink(s))
)
elif isinstance(s, io.TextIOBase):
if codecs.lookup(s.encoding).name not in {
"utf-8",
"ascii",
}:
raise NotImplementedError(
f"Unsupported encoding {s.encoding}"
)
elif isinstance(sinks[0], io.TextIOBase):
for s in sinks:
if codecs.lookup(s).name not in ('utf-8', 'ascii'):
raise NotImplementedError(f"Unsupported encoding {s.encoding}")
self.sink_storage.push_back(
unique_ptr[data_sink](new iobase_data_sink(s.buffer))
)
else:
raise TypeError(
"Unrecognized input type: {}".format(type(sinks[0]))
)

data_sinks.push_back(self.sink_storage.back().get())
data_sinks.push_back(self.sink_storage.back().get())
elif isinstance(sinks[0], str):
paths.reserve(len(sinks))
for s in sinks:
paths.push_back(<string> s.encode())
else:
raise TypeError(
"Unrecognized input type: {}".format(type(sinks[0]))
)

if data_sinks.size() > 0:
self.c_obj = sink_info(data_sinks)
Expand Down

0 comments on commit 7806ce4

Please sign in to comment.