Add strings_to_ctypes_array to convert a sequence of strings into a ctypes array #3137
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of proposed changes
To pass a list of strings into a ctypes function, currently, we have codes like the below, which is not readable:
So better to have a
strings_to_ctypes_array
function, which can hide the technical details.The above two lines codes can be shortened into a single line of code:
Actually
np.char.encode
calls thestr.encode
element-wise, so it can be further written as:The list comprehension version is faster than the
np.char.encode
version:This PR adds the
strings_to_ctypes_array
function to do the conversion work. It just contains one line of code and doesn't check if thestrings
is an empty list to avoid extra overheads.PR #3136 is a similar work but for converting a sequence of numbers to a ctypes array. These two functions share similar codes but I prefer not to combine them into a single function to avoid too many if-else clauses in the low-level function. Better to review these two PRs back-to-back.