Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative for Weave in Python 3

I want to find an alternative for weave in Python 2 since weave is not available anymore in Python 3.

More specifically I need to have an alternative way of writing:

from scipy import weave
from scipy.weave import converters

code = """ C-code1 """
support_code = """ C-code2 """
weave.inline(code, ['a', 'b', 'c'], support_code=support_code, type_converters=converters.blitz, compiler='gcc', verbose=0)
like image 688
Jeroen Bertels Avatar asked Oct 15 '25 16:10

Jeroen Bertels


2 Answers

You can use the Cython library as it's recommended by weave developers here. It's a little more complex in using but increases the performance of your code too. You can find some example here.

Another alternative can be Numba. It's more user-friendly but doesn't cache the compiled code.

like image 121
George Avatar answered Oct 17 '25 05:10

George


Have a look at numba. Chances are, you can migrate all your codebase into plain python, and still preserve the same speed that you're used to from C code. You even gain some features like throwing clear python errors right from your inner loops, which was to my knowledge not easily possible from weave. As an example how fast you get with numba, you might check the benchmarks of numpy_groupies, which offers implementations as well in numba as with weave. If you got rid of your C code once, you'll never look back.

like image 44
Michael Avatar answered Oct 17 '25 06:10

Michael