Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add string.wrap APIs to pylibcudf #16935

Merged
merged 5 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ strings
replace
slice
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 @@ -29,6 +29,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 @@ -16,6 +16,7 @@ from . cimport (
slice,
strip,
translate,
wrap,
)
from .side_type cimport side_type

Expand All @@ -36,4 +37,5 @@ __all__ = [
"strip",
"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 @@ -17,6 +17,7 @@
slice,
strip,
translate,
wrap,
)
from .side_type import SideType

Expand All @@ -37,4 +38,5 @@
"strip",
"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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit: saving 12 as a variable would be better than hardcoding it twice.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will include this change in #16971

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)
Loading