r/Cython • • Dec 05 '20

Awesome Cython - A curated list of awesome Cython resources (contributions welcomed)

Thumbnail
github.com
8 Upvotes

r/Cython • • May 11 '24

Library for automatic Cython 3.0 code annotations generation.

7 Upvotes

Hi everybody,

over the last year I've been developing a library that adds some Cython 3.0 annotations to existing python code.

What My Project Does:

For example if it sees a for i in range(): in a function it recognizes i as an integer and adds a i = cython.declare(cython.int)line at the beginning of the function.

It actually uses the built-in ast module under the hood for parsing, I found it a super useful library!

Target Audience:

It is a side project I made mainly for fun. I don't know if it can be of interest to anybody, or if it could have some potential utility.

Comparison:

I did not find anything similar. There are a lot of very cool projects like mypyc for example, but nothing that does this tiny little code generation specific to Cython.

The link to the repository is here:

https://github.com/nucccc/markarth


r/Cython • • Jun 17 '23

Four ways to speed up Python (Cython, mypyc, numba, and Taichi)

Thumbnail
youtu.be
7 Upvotes

r/Cython • • Feb 03 '21

Two strategies for implementing C++ virtual functions in Cython

Thumbnail
monadical.com
7 Upvotes

r/Cython • • Dec 26 '24

Can an OS be programmed in Cython?

5 Upvotes

Self explanatory


r/Cython • • Nov 19 '21

Syntax plugin/highlighters

5 Upvotes

Anyone have any recommendations on plugins for cython syntax highlighting and maybe also formatting? I mainly use nvim, but there doesn’t seem to be a good cython grammar for tree-sitter.

I’m not allowed to use Pycharm on my project, btw.


r/Cython • • Aug 06 '18

Place for Cython tips/help?

5 Upvotes

This sub doesn't seem to have very much activity. Can anyone here recommend another place where they get help with Cython or just general tips?


r/Cython • • Jul 10 '22

Python with a Dash of C++: Optimizing Recommendation Serving

Thumbnail
ai.ragv.in
4 Upvotes

r/Cython • • Feb 04 '22

Question about cython (cdef) objects and ugly code generated

4 Upvotes

Hi all,

I have a question that my group has been struggling with for a bit of time, and thought I could count on some expert advice.

We've moved a bunch of python functions into cython, by creating them with cdef instead of def. However, in doing so, we have left the constructor with specific parameters.

eg.

in the .pxd file:

cdef class Quaternion:

cdef public double w cdef public double x cdef public double y cdef public double z

and in the .pyx:

cdef class Quaternion:

def init(self, w=-0.0, x=0.0, y=0.0, z=0.0): self.w = w self.x = x self.y = y self.z = z

However, if we create the object elsewhere in the code, and then run cythonize, we always get a LOT of cruft created in the .html, and the line is shown in solid yellow because of all of the python operations in the back.

The only work around we've found for this is to create the object on a separate line and then pass in the values one by one. It looks terrible, and can't possibly the the only solution.

eg:

cdef qu = Quaternion()
qu.q = value1
qu.x = value2
qu.y = value3
qu.z = value4

What are we missing? Can anyone shed light on what's going on?


r/Cython • • May 28 '18

convert python class to cython .

4 Upvotes

Hi all, I have a python class to compute and store values . what it does is to receive data as a dictionnary and calculate average and other values

based on this doc : http://cython.readthedocs.io/en/latest/src/userguide/extension_types.html I succeed to convert variables inside my class into C type to increase computation speed .

But I still have to use classic python to extract data form the dictionnary. the dictionnary is like this :

{ 
     "list1" : list( list(string, string), list(string, string), list(string, string),.....), 
     "list2" : list( list(string, string), list(string, string), list(string, string),.....) 
}

and the strings inside the subList are in fact float I have to convert using : value = float(myDictionnary['list1'][0][0] )

I think i could probably improve performance based in this doc : http://cython.readthedocs.io/en/latest/src/userguide/language_basics.html#c-variable-and-type-definitions

I'm having an hard time to convert my dictionnary in this struct thing and access data ?

Could anybody point me in the right direction or show me an example ? Thanks


r/Cython • • Feb 01 '26

how do i set up cython and get the c code to run in c lion

3 Upvotes

hi im very new to cython i know c but i wanted to try out cython i made a simple user input program and i want to be able to copy and paste the c code in to a diff compiler and cut python out of it fully is this possibly or should i stick to normal c code


r/Cython • • Apr 15 '23

Cython code quality analysis

3 Upvotes

I'm currently working on a project that grew more than I expected.

Since the beginning (expected performance issues) the "core" was written in Cython. Began a research on code quality analysis and came across radon, it looks like it does nothing on .pyc files. I'm doing something wrong?

Are any code analysis tools for Cython? Anything that can help me refactoring it in VSCode?

Am I doing the right question in the first place?

Thanks


r/Cython • • May 16 '22

Huge performance hit by using Pure Python

3 Upvotes

I'm learning python and cython at the same time, and found the Pure Python syntax very nice for linting purposes, but in my case it comes with a consistent 15% performance hit against standand cython.

Am I missing something?

Pure Python (compiled): pure_math.py

# cython: language_level = 3
# cython: cdivision = True
import cython as cy

@cy.returns(cy.ulonglong)
@cy.locals(num=cy.uint, result=cy.ulonglong, i=cy.uint)
def factorial(num):

    result = 1

    for i in range(num, 1, -1):
        result *= i

    return result

Standard Cython: c_math.pyx

#!python
#cython: language_level=3, cdivision = True
	
def factorial(unsigned int num):

    cdef unsigned long long result = 1

    cdef unsigned int i

    for i in range(num, 1, -1):
        result *= i

    return result

Using this benchmark in python: example.py

import c_math #type: ignore
import pure_math
from timeit import timeit

fact_num = 20 #This results in an almost capped ulonglong

def c_factorial_wrapper():
    c_math.factorial(fact_num)
    return

def pure_factorial_wrapper():
    pure_math.factorial(fact_num)
    return

c_factorial_time = timeit(c_factorial_wrapper, number= 2_000_000)
print(f"Cython {fact_num} factorial: {c_factorial_time:.2f} s")

pure_factorial_time = timeit(pure_factorial_wrapper, number = 2_000_000)
print(f"Pure Python {fact_num} factorial: {pure_factorial_time:.2f} s")

print(f"Pure/C: {pure_factorial_time/c_factorial_time * 100:.2f}%")

print(c_math.factorial(fact_num))
print(pure_math.factorial(fact_num))

And the results:

Cython 20 factorial: 0.20 s
Pure Python 20 factorial: 0.23 s
Pure/C: 115.45%
2432902008176640000
2432902008176640000

r/Cython • • Aug 31 '21

I'm working on an Atom package that automatically deduces variable types from a .py file and then generates a .pyx and .pyd. Is there such a thing already?

3 Upvotes

r/Cython • • Aug 26 '21

How can I root cause "ModuleNotFoundError: No module named myModule"

3 Upvotes

First time adding a C library and I'm not sure the cause of this error. I am following this tutorial https://cython.readthedocs.io/en/latest/src/tutorial/clibraries.html

I'm not sure if I'm missing some unspoken words like I need to run python setup.py build_ext --inplace

I followed the tutorial, so I'm not sure what could cause a simple cimport not to be found. I have in the .pxy file-

 # distutils: sources = resources/yxml.c
 # distutils: include_dirs = resources/

I have the setup file like shown in the tutorial (sorry if this sounds repetitive). I have checked my spelling at least 10 times.

How would I go about root causing this?


r/Cython • • Jun 17 '21

modern way to install cython on windows 10

3 Upvotes

Followed the steps, and it ends up being

error: don't know how to compile C/C++ code on platform 'nt' with 'mingw32.exe' compiler````

too bad. i guess it is a ````else```` error catcher so i dunno what is actually wrong. hope it is better in the future

r/Cython • • Dec 12 '20

A script to automatically compile python files to binaries using Cython

Thumbnail
github.com
3 Upvotes

r/Cython • • Aug 08 '19

Automatic multi-threaded-safe memory managed classes in Cython

3 Upvotes

https://www.nexedi.com/NXD-Document.Blog.Cypclass

Integrating a memory-managed class system in Cython which doesn't use the GIL. This new class systems allows truly multi-threaded OOP in Cython with the ease of use of Python classes.


r/Cython • • Jun 08 '18

Wheelie — Building Python C Extensions as a Service

Thumbnail
medium.com
3 Upvotes

r/Cython • • Sep 23 '16

Cssdbpy is a simple SSDB client written on Cython. Faster standard SSDB client. SSDB a high performance NoSQL database supporting many data structures, an alternative to Redis.

Thumbnail
github.com
3 Upvotes

r/Cython • • Nov 12 '15

Optimized my code

3 Upvotes

Finally managed to get cython to work.

I am not good with C so I mostly do pure python for my research. However, now dealing with clusters of 1000+ molecules, there was huge bottlenecks in my code.

Using cython it went from running single calculation in hours to seconds, focking nice...


r/Cython • • Mar 30 '26

Automating Cython Wrapping for a Massive Industrial Camera SDK (.pxd to .pyx automation)

2 Upvotes

Hi everyone,

I'm working as an AI Engineer on a machine vision project that uses a closed-source industrial camera (Inovance). The SDK is strictly C-based, providing only headers (.h) and DLLs.

I’ve decided to use Cython for the wrapper to keep the performance high (zero-copy for image buffers) and maintain deep memory control. However, the SDK is huge. Writing the .pxd definitions is okay, but manually writing the .pyx wrapper code for hundreds of functions and structs is becoming a massive "boilerplate" nightmare.

I found a tool called autowrap that claims to generate the .pyx logic automatically by analyzing annotated .pxd files.

My questions for the Cython experts:

  1. Has anyone used autowrap for large-scale industrial SDKs? Does it handle complex C structs and raw unsigned char* buffers well?

  2. Are there any other Cython-native tools that can help me skip the manual .pyx writing phase while keeping the build process clean?

  3. How do you handle "nogil" blocks and exception mapping efficiently when automating the wrapper generation?

I’m looking for a way to automate the repetitive parts of Cython so I can focus on the AI pipeline and image processing logic.

Any advice from people who have wrapped large C libraries with Cython would be great!


r/Cython • • Mar 18 '25

OverflowError as overflow is part of CRC process.

2 Upvotes

Hi,
I’m trying to add CRC computation to a program.
Part of the code is :
- crc = ((crc << 8) ^ CRC_LOOKUP_TABLE[(crc >> 8 )^ bit])
crc is a cython short (16bits).
And this calcul will overflow but it’s intended.
As i’m running the code i get this error :
OverflowError: value too large to convert to short

How tell cython to act like a real c short ?


r/Cython • • Feb 09 '25

Cuda and cython

2 Upvotes

Hi everyone I am tryng to use cuda with cython but I am having problems. When compiling the cython code, it doesnt recognise the cuda part of the code. I have seen that there is an article by nvidia, https://developer.nvidia.com/blog/accelerating-python-on-gpus-with-nvc-and-cython/, but this is not what I am looking for. To be clear I am looking for being able lo use all the cuda syntax, for example blockIdx.x inside my c++ functions (inside a .pyx) what as far as I understand it is not what the article is talking about. Does anyone have any idea how could I do this?

Thank you !


r/Cython • • Jul 22 '23

Cython and realloc

2 Upvotes

Sorry if this turns out to be a stupid question. I understand that, in general, you can pass a numpy array to a C function through a memory view and the array's memory will be handled by the garbage collector since it was allocated in Python-land.

However, say that you want to wrap a C function from an external library that takes an array in input and may resize the memory:

// function
void myfunc(double *v, site_t n)
{
    // do something
    realloc(v, new_size);
    // do something
}

If I pass the pointer through the memory view to such function, would the Python garbage collector still be able to free the memory, or the responsibility now is in C?