Questions tagged [cython]

Cython is a superset of the Python language for quickly generating Python C extensions.

Cython is a superset of the Python language for quickly generating Python C/C++ extensions. Cython is a pidgin language of Python and C/C++. Unlike pure Python, Cython code is not directly interpreted by the Python interpreter, but is instead used to generate C/C++ code. The generated C/C++ code can then be compiled into a C/C++-extension, which then can be imported by Python code.

Cython syntax was originally based on Python 2, with added type declarations à la C/C++, however, its syntax now supports both Python 2 and 3 language features. Additionally, Cython is capable of generating C/C++ extension code compatible with either Python 2 or Python 3. Its syntax now allows the use of advanced C++ constructs such as template and stl container. Finally, thanks to static typing, Cython code generally executes much faster than Python code.

Cython is freely available under the open source Apache License.

The latest release of Cython is 3.0 alpha 5 (released 2020-05-19). Cython is available from the PyPI package index repository.

5220 questions
14
votes
2 answers

Cython: Nesting a union within a struct

In Cython glue declarations, how do I represent a C struct type containing an anonymous union? For example, if I have a C header file mystruct.h containing struct mystruct { union { double da; uint64_t ia; }; }; then, in…
polerto
  • 1,750
  • 5
  • 29
  • 50
14
votes
2 answers

Can I override a C++ virtual function within Python with Cython?

I have a C++ class with a virtual method: //C++ class A { public: A() {}; virtual int override_me(int a) {return 2*a;}; int calculate(int a) { return this->override_me(a) ;} }; What I would like to do is to expose this…
ascobol
  • 7,554
  • 7
  • 49
  • 70
13
votes
5 answers

Wrap C struct with array member for access in python: SWIG? cython? ctypes?

I want to access a C function that returns a struct containing double arrays (where the lengths of these arrays is given by other int members of the struct) from python. The declaration is typedef struct { int dim; int vertices; int…
kynan
  • 13,235
  • 6
  • 79
  • 81
13
votes
3 answers

How can I construct a Pandas DataFrame from individual 1D Numpy arrays without copying

Unlike every other question I can find, I do not want to create a DataFrame from a homogeneous Numpy array, nor do I want to convert a structured array into a DataFrame. What I want is to create a DataFrame from individual 1D Numpy arrays for each…
Pepijn
  • 4,145
  • 5
  • 36
  • 64
13
votes
1 answer

Cython - iterate through map

I was wondering if this was possible to iterate through a map directly in Cython code, ie, in the .pyx. Here is my example: import cython cimport cython from licpp.map import map as mapcpp def it_through_map(dict mymap_of_int_int): # python dict…
boris
  • 378
  • 1
  • 4
  • 16
13
votes
2 answers

Cython: when should I define a string as char*, str, or bytes?

When defining a variable type that will hold a string in Cython + Python 3, I can use (at least): cdef char* mystring = "foo" cdef str mystring = "foo" cdef bytes mystring = "foo" The documentation page on strings is unclear on this -- it mostly…
right2clicky
  • 785
  • 7
  • 14
13
votes
2 answers

Cython float division PyExc_ZeroDivisionError checking

I'm doing some loop-intensive calculations and converted the code into Cython. I did profiling with cython -a option, and inspected the .html file, and it seems whenever I do the float division, there is somewhat yellow line and it does something…
joon
  • 3,899
  • 1
  • 40
  • 53
13
votes
2 answers

spaCy Documentation for [ orth , pos , tag, lema and text ]

I am new to spaCy. I added this post for documentation and make it simple for new starters as me. import spacy nlp = spacy.load('en') doc = nlp(u'KEEP CALM because TOGETHER We Rock !') for word in doc: print(word.text, word.lemma, word.lemma_,…
ahmed osama
  • 297
  • 4
  • 15
13
votes
2 answers

Mixing cdef and regular python attributes in cdef class

i am learning Cython and now experimenting with it. I tried the basic cdef class sample program and it works perfectly. Now what i want to do is have a mix of cdef and non cdef mix of attributes in the cdef class type, something like this cdef class…
vk-code
  • 950
  • 12
  • 22
13
votes
3 answers

Compiling an optional cython extension only when possible in setup.py

I have a python module fully implemented in python. (For portability reasons.) The implementation of a small part has been duplicated in a cython module. To improve perfomance where possible. I know how to install the .c modules created by cython…
ARF
  • 7,420
  • 8
  • 45
  • 72
13
votes
1 answer

cython: how do you create an array of cdef class

I would like to have a cython array of a cdef class: cdef class Child: cdef int i def do(self): self.i += 1 cdef class Mother: cdef Child[:] array_of_child def __init__(self): for i in range(100): …
Davoud Taghawi-Nejad
  • 16,142
  • 12
  • 62
  • 82
13
votes
2 answers

running pep8 or pylint on cython code

Is there any way to use pep8 with cython files? pep8 does not work with operators for example. getline(& line) produces error: E225 missing whitespace around operator Now if i try to fix it and run this: getline( & line) produces error: E201…
Michael WS
  • 2,450
  • 4
  • 24
  • 46
13
votes
2 answers

Cython C++ static methods in a template class

Problem I have a template class in C++ that has a static method. It looks more or less like this: template class Foo { static std::shared_ptr> doSth(); } so in C++ you would call it like: Foo::doSth();. In Cython…
piotrMocz
  • 429
  • 4
  • 12
13
votes
1 answer

Cython: working with C++ streams

The Problem How does one use c++ streams (like std::ifstream or ostream) from Cython? In c++, you could do the following: std::ofstream output { filename, std::ios::binary }; output.write(...); How would you achieve the same in Cython? Current…
piotrMocz
  • 429
  • 4
  • 12
13
votes
1 answer

cython boundscheck=True faster than boundscheck=False

Consider the following minimal example: #cython: language_level=3, boundscheck=False, wraparound=False, initializedcheck=False, cdivision=True cimport cython from libc.stdlib cimport malloc def main(size_t ni, size_t nt, size_t nx): cdef: …
antony
  • 2,877
  • 4
  • 31
  • 43