What is a ".pxi.in" file and how to use it?

Viewed 71
1 Answers

Cython comes with tempita. The idea behind it is more or less to have a kind of preprocessor-language for Cython, because otherwise Cython is somewhat lacking this functionality compared to C or C++-templates.

*.pxi.in from pandas uses tempita, to create e.g. classes for int8, int16 usw. from a template, so one doesn't have to repeat the code per hand. *.pxi.in files are converted to *.pxi-file in the setup.py-file.

As example

{{py:

# name
complex_types = ['complex64',
                 'complex128']
}}

{{for name in complex_types}}
cdef kh{{name}}_t to_kh{{name}}_t({{name}}_t val) nogil:
    cdef kh{{name}}_t res
    res.real = val.real
    res.imag = val.imag
    return res

{{endfor}}

will be converted, once tempita step was performed, to

cdef khcomplex64_t to_khcomplex64_t(complex64_t val) nogil:
    cdef khcomplex64_t res
    res.real = val.real
    res.imag = val.imag
    return res

cdef khcomplex128_t to_khcomplex128_t(complex128_t val) nogil:
    cdef khcomplex128_t res
    res.real = val.real
    res.imag = val.imag
    return res

and the resulting pxi-file will be used while cythonizing pyx-files.

Related