Add strings_to_ctypes_array to convert a sequence of strings into a ctypes array#3137
Merged
Add strings_to_ctypes_array to convert a sequence of strings into a ctypes array#3137
Conversation
…gs into a ctypes array
32c97d5 to
2fe50a9
Compare
This was referenced Mar 23, 2024
michaelgrund
approved these changes
Mar 23, 2024
michaelgrund
approved these changes
Mar 25, 2024
10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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_arrayfunction, which can hide the technical details.The above two lines codes can be shortened into a single line of code:
Actually
np.char.encodecalls thestr.encodeelement-wise, so it can be further written as:The list comprehension version is faster than the
np.char.encodeversion:This PR adds the
strings_to_ctypes_arrayfunction to do the conversion work. It just contains one line of code and doesn't check if thestringsis 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.