Skip to content

Commit

Permalink
Add string.wrap APIs to pylibcudf (#16935)
Browse files Browse the repository at this point in the history
Contributes to #15162

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)
  - Vyas Ramasubramani (https://github.com/vyasr)

Approvers:
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #16935
  • Loading branch information
mroeschke authored Oct 2, 2024
1 parent a6ca0f0 commit 63a5d2e
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ strings
slice
split
strip
wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
====
wrap
====

.. automodule:: pylibcudf.strings.wrap
:members:
24 changes: 7 additions & 17 deletions python/cudf/cudf/_lib/strings/wrap.pyx
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
# Copyright (c) 2020-2024, NVIDIA CORPORATION.

from libcpp.memory cimport unique_ptr
from libcpp.utility cimport move

from cudf.core.buffer import acquire_spill_lock

from pylibcudf.libcudf.column.column cimport column
from pylibcudf.libcudf.column.column_view cimport column_view
from pylibcudf.libcudf.strings.wrap cimport wrap as cpp_wrap
from pylibcudf.libcudf.types cimport size_type

from cudf._lib.column cimport Column

import pylibcudf as plc


@acquire_spill_lock()
def wrap(Column source_strings,
Expand All @@ -21,14 +17,8 @@ def wrap(Column source_strings,
in the Column to be formatted in paragraphs
with length less than a given `width`.
"""

cdef unique_ptr[column] c_result
cdef column_view source_view = source_strings.view()

with nogil:
c_result = move(cpp_wrap(
source_view,
width
))

return Column.from_unique_ptr(move(c_result))
plc_result = plc.strings.wrap.wrap(
source_strings.to_pylibcudf(mode="read"),
width
)
return Column.from_pylibcudf(plc_result)
2 changes: 1 addition & 1 deletion python/pylibcudf/pylibcudf/libcudf/strings/wrap.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ from pylibcudf.libcudf.types cimport size_type
cdef extern from "cudf/strings/wrap.hpp" namespace "cudf::strings" nogil:

cdef unique_ptr[column] wrap(
column_view source_strings,
column_view input,
size_type width) except +
1 change: 1 addition & 0 deletions python/pylibcudf/pylibcudf/strings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(cython_sources
slice.pyx
strip.pyx
translate.pyx
wrap.pyx
)

set(linked_libraries cudf::cudf)
Expand Down
2 changes: 2 additions & 0 deletions python/pylibcudf/pylibcudf/strings/__init__.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ from . cimport (
split,
strip,
translate,
wrap,
)
from .side_type cimport side_type

Expand All @@ -39,4 +40,5 @@ __all__ = [
"split",
"side_type",
"translate",
"wrap",
]
2 changes: 2 additions & 0 deletions python/pylibcudf/pylibcudf/strings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
split,
strip,
translate,
wrap,
)
from .side_type import SideType

Expand All @@ -40,4 +41,5 @@
"split",
"SideType",
"translate",
"wrap",
]
7 changes: 7 additions & 0 deletions python/pylibcudf/pylibcudf/strings/wrap.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copyright (c) 2024, NVIDIA CORPORATION.

from pylibcudf.column cimport Column
from pylibcudf.libcudf.types cimport size_type


cpdef Column wrap(Column input, size_type width)
42 changes: 42 additions & 0 deletions python/pylibcudf/pylibcudf/strings/wrap.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright (c) 2024, NVIDIA CORPORATION.

from libcpp.memory cimport unique_ptr
from libcpp.utility cimport move
from pylibcudf.column cimport Column
from pylibcudf.libcudf.column.column cimport column
from pylibcudf.libcudf.strings cimport wrap as cpp_wrap
from pylibcudf.libcudf.types cimport size_type


cpdef Column wrap(Column input, size_type width):
"""
Wraps strings onto multiple lines shorter than `width` by
replacing appropriate white space with
new-line characters (ASCII 0x0A).
For details, see :cpp:func:`cudf::strings::wrap`.
Parameters
----------
input : Column
String column
width : int
Maximum character width of a line within each string
Returns
-------
Column
Column of wrapped strings
"""
cdef unique_ptr[column] c_result

with nogil:
c_result = move(
cpp_wrap.wrap(
input.view(),
width,
)
)

return Column.from_libcudf(move(c_result))
24 changes: 24 additions & 0 deletions python/pylibcudf/pylibcudf/tests/test_string_wrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright (c) 2024, NVIDIA CORPORATION.
import textwrap

import pyarrow as pa
import pylibcudf as plc
from utils import assert_column_eq


def test_wrap():
pa_array = pa.array(
[
"the quick brown fox jumped over the lazy brown dog",
"hello, world",
None,
]
)
result = plc.strings.wrap.wrap(plc.interop.from_arrow(pa_array), 12)
expected = pa.array(
[
textwrap.fill(val, 12) if isinstance(val, str) else val
for val in pa_array.to_pylist()
]
)
assert_column_eq(expected, result)

0 comments on commit 63a5d2e

Please sign in to comment.