3 ms·
Cython is amazingly productive for me. I am so happy to write typed pythonic code. If there's one thing missing it's more people showing off their Cython styl
by grinalope 11y ago
Cython is amazingly productive for me. I am so happy to write typed pythonic code.
If there's one thing missing it's more people showing off their Cython style and tricks, like how they do macros (since it's not supported in the language), when to turn off safeties, and balancing pure python with typed.
For instance, I like C macros in header files, but I've played around with using a jinja2 pipeline in my setup.py to write cleaner looking pythonic code, and have done generics this way.
more posts like the one using cymem would be welcome.
- syllogism 11y agoUsing Jinja2 for macros is an interesting idea. You could try cog instead? https://www.python.org/about/success/cog/ https://www.python.org/about/success/cog/ Currently I don't do any macros at all, although the temptation is definitely strong. I have some code that could otherwise be more generic than it is. I guess a quick tip/pattern would be to use structs as data containers, with a cdef class defined as a "shell", to provide access, but not actually hold the data. Advantages of this over standard cdef class: Data is contiguous in memory; Data can be stack allocated; Data can be stored in C arrays or C++ containers; Data can be created without GIL. Advantages of this over numpy array: More Pythonic syntax (i.e. data.attr, instead of data[column]; No fussing with memorybuffers, compiler declarations etc; Data can be created without GIL Disadvantages: More code (I wrote that cymem blog post -- I'll write up this pattern as a post when I get a chance.)