String manipulation in Cython

I voted up the ‘profile it’ answer, but wanted to add this: where possible the best optimisation you can make is to use Python standard libraries or built-in functions to perform the tasks you want. These are typically implemented in C and will provide performance broadly equivalent to any extension, including extensions written in Cython. … Read more

setup_requires with Cython?

Starting from 18.0 release of setuptools (released on 2015-06-23) it is possible to specify Cython in setup_requires and pass *.pyx modules sources for regular setuptools.Extension: from setuptools import setup, Extension setup( # … setup_requires=[ # Setuptools 18.0 properly handles Cython extensions. ‘setuptools>=18.0’, ‘cython’, ], ext_modules=[ Extension( ‘mylib’, sources=[‘src/mylib.pyx’], ), ], )

Speeding up pairing of strings into objects in Python

When dealing with the creating of large numbers of objects, often the single biggest performance enhancement you can use is to turn the garbage collector off. Every “generation” of objects, the garbage collector traverses all the live objects in memory, looking for objects that are a part of cycles but are not pointed at by … Read more

How can a #defined C value be exposed to Python in a Cython module?

Here’s one way, which, while seemingly tedious, could feasibly be automated for any given .h file as input: Step 1. Get all the constants you want into a file, say bulletdefs.h, which has the #defines but with leading underscores, e.g: #define _ACTIVE_TAG 1 #define _ISLAND_SLEEPING 2 #define _WANTS_DEACTIVATION 3 #define _DISABLE_DEACTIVATION 4 #define _DISABLE_SIMULATION 5 … Read more

Docker-compose no longer building image (AttributeError: cython_sources)

Important security disclaimer: I would strongly discourage anyone downgrading PyYAML to 5.3.1, as versions before 5.4 all have a known CVE — CVE-2020-14343. This issue was caused by an incompatibility of PyYAML<6.0.1 compiled under the version of Cython>=3.0, released on July 17, 2023. This has now been fixed by a new release of PyYAML, tagged … Read more

Distributing a shared library and some C code with a Cython extension module

1) Distributing libbig.so This is a problem that python isn’t going to help you with. Who are you targeting? If it’s linux, can you request that they install it with their package manager? If libbig isn’t distributed through a package manager or it’s not linux and you’re targeting multiple architectures, you might have to distribute … Read more

Use Cython as Python to C Converter

Yes, at its core this is what Cython does. But … You don’t need Cython, however, you do need libpython. You may feel like it doesn’t use that many Python features, but I think if you try this you’ll find it’s not true — you won’t be able to separate your program from its dependence … Read more

Calling C functions in Python

You should call C from Python by writing a ctypes wrapper. Cython is for making python-like code run faster, ctypes is for making C functions callable from python. What you need to do is the following: Write the C functions you want to use. (You probably did this already) Create a shared object (.so, for … Read more

tech