Someone at my job wrote a few python modules using PyQt4. I wrote a Jython application that was supposed to allow Python modules access to the Java binding of our API but I did not realize Jython couldn't use ctypes thus the entire application is basically useless.
I'm scrambling to find a solution on how I can expose our API to his Python modules. Our API is C and has a C++ binding (as well as Java and Perl) so I'm thinking the best solution is to just expose one of the C/C++ APIs to his Python modules.
Instead of doing (what I anticipate would be a lot of work) a C/C++ wrapper for Python using ctypes, sig, swig, etc. I thought maybe I could just have him use Cython. From what I've been reading you don't have to do much work to make your Python code into Cython so I could create a few test examples for him demonstrating how to convert his Python modules into Cython and show him how you would access our C API.
But is it even possible to use Cython and PyQt4 at the same time. It doesn't seem to make much sense, Qt is C or C++ and PyQt is a binding or wrapper to use Qt with Python then we're changing the Python code into C code using Cython. But despite it not making sense and possibly slowing everything down is it even possible? If it's possible I'm going to offer this as a temporary solution.
I did this as a quick and dirty solution to a problem, passing a QImage to the following function:
cpdef void export_array(object image, int[:] colorList, int[:] dim, int[:, :] arr, int color_offset):
"""
Updates the image with the values from an entire array.
:param image: A QImage object to be written to
:param colorList: List of colors, as integers
:param dim: Dimensions of the array
:param array: Array to use as new image
:param color_offset: Use primary colors (0) or secondary colors (2)?
:return: None
"""
cdef int color
cdef Py_ssize_t i, j
cdef int a = dim[0]
cdef int b = dim[1]
for i in range(a):
for j in range(b):
color = colorList[arr[i][j] + color_offset]
image.setPixel(i, j, color)
It's an order of magnitude faster:
%timeit cf.export_array(image, colorList, imageDim, arr, 2)
%timeit pf.export_array(image, colorList, arr, 2)
507 ms ± 730 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)
3.38 s ± 22.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Though another function that I tried it with slowed down by about 10%. In this case the nested for loops make it a good candidate.
(I assume your problems have evolved considerably since you posted this question, but maybe this answer can help other contemporary googlers.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With