GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add strings_to_ctypes_array to convert a sequence of strings into a ctypes array by seisman · Pull Request #3137 · GenericMappingTools/pygmt

Conversation

@seisman

@seisman seisman commented Mar 23, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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:

strings_pointer = (ctp.c_char_p * len(strings))()
strings_pointer[:] = np.char.encode(strings)

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:

strings_pointer = (ctp.c_char_p * len(strings))(*np.char.encode(strings))

Actually np.char.encode calls the str.encode element-wise, so it can be further written as:

strings_pointer = (ctp.c_char_p * len(strings))(*[s.encode() for s in strings])

The list comprehension version is faster than the np.char.encode version:

>>> import numpy as np
>>> import ctypes as ctp
>>> strings = ["ABC", "DEFGHI", "ABC123", "ABC1234566"]
>>> %timeit (ctp.c_char_p * 4)(*[s.encode() for s in strings])
1.37 µs ± 3.79 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

>>> %timeit (ctp.c_char_p * 4)(*np.char.encode(strings))
6.17 µs ± 22.5 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

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 the strings 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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@seisman
Add a function strings_to_ctypes_array to convert a sequence of strin…
2fe50a9
…gs into a ctypes array
@seisman
seisman force-pushed the strings_to_ctypes_array branch from 32c97d5 to 2fe50a9 Compare March 23, 2024 08:42
@seisman seisman added the maintenance Boring but important stuff for the core devs label Mar 23, 2024
@seisman seisman added this to the 0.12.0 milestone Mar 23, 2024
@seisman seisman added the needs review This PR has higher priority and needs review. label Mar 23, 2024
This was referenced Mar 23, 2024
Add sequence_to_ctypes_array to convert a sequence to a ctypes array #3136
Merged
Refactor all wrappers to pass an argument list to Session.call_module #3132
Merged
@seisman seisman added run/benchmark Trigger the benchmark workflow in PRs and removed run/benchmark Trigger the benchmark workflow in PRs labels Mar 23, 2024
michaelgrund
michaelgrund approved these changes Mar 23, 2024
View reviewed changes
@seisman seisman changed the title Add a function strings_to_ctypes_array to convert a sequence of strings into a ctypes array Add strings_to_ctypes_array to convert a sequence of strings into a ctypes array Mar 23, 2024
michaelgrund
michaelgrund approved these changes Mar 25, 2024
View reviewed changes
@michaelgrund michaelgrund added final review call This PR requires final review and approval from a second reviewer and removed needs review This PR has higher priority and needs review. labels Mar 25, 2024
@seisman
Merge branch 'main' into strings_to_ctypes_array
546b396
@seisman seisman removed the final review call This PR requires final review and approval from a second reviewer label Mar 26, 2024
seisman added 2 commits March 26, 2024 09:26
@seisman
Merge branch 'main' into strings_to_ctypes_array
5bcf8d8
@seisman
Fix import sorts
ff36ac5
@seisman
seisman merged commit 62eb5d6 into main Mar 26, 2024
@seisman
seisman deleted the strings_to_ctypes_array branch March 26, 2024 01:28
@weiji14 weiji14 mentioned this pull request Oct 11, 2024
pyarrow: Check compatibility of pyarrow.array with string type #2933
Merged
5 of 10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@michaelgrund michaelgrund michaelgrund approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

maintenance Boring but important stuff for the core devs

Projects

None yet

Milestone

0.12.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@seisman @michaelgrund
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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.