I want to make the code written in C ++ a library that can be called in Python. However, there was a big wall called Cython.
This is "Cython" tutorial to make Python explosive: How to parse Enum class defined in C ++ code to Python Enum. This is a continuation of.
The code is listed in "github here", so please take a look.
Last time, I explained how to pass the Enum class declared in C ++ to the Enum class on the Python side when converting a library written in C ++ to Cython so that it can be used from Python.
This time, it will be the explanation of Cythonization when the function on the C ++ side is overloaded (when the configuration has the same function name but different arguments).
Create the following function on the C ++ side of the library you have written so far.
Here, the print_vector
function is a function that can take `vector <int>`
and `vector <double>`
as arguments, and in such a case, the function Is said to be overloaded.
cpp_library/TestClass1.h
class TestClass1{
private:
TestClass2 property_test_class2;
EnumForTestClass1 group;
public:
//~abridgement
static void print_vector(vector<int> x);
static void print_vector(vector<double> x);
The contents are just a function that prints the contents of the vector as shown below.
cpp_library/TestClass1.cpp
void TestClass1::print_vector(vector<int> x){
for(int i=0; i<x.size(); i++){
cout << x[i] << " ";
}
cout << endl;
}
void TestClass1::print_vector(vector<double> x){
for(int i=0; i<x.size(); i++){
cout << x[i] << " ";
}
cout << endl;
}
Consider calling this print_vector
function from Cython as in the example.
cython/my_library.pxd
cdef extern from "../cpp_library/TestClass1.h" namespace "my_library":
cdef cppclass TestClass1:
#~abridgement
void print_vector(vector[int] x)
void print_vector(vector[double] x)
cython/test_class1.pxd
cdef extern from "../cpp_library/TestClass1.h" namespace "my_library":
cdef cppclass TestClass1:
#~abridgement
void print_vector(vector[int] x)
void print_vector(vector[double] x)
Here, we will implement the contents in `` `cython / test_class1.pyx```.
cython/test_class1.pyx
@staticmethod
def print_vector(list x):
cdef TestClass1 testclass1
return testclass1.print_vector(x)
When I try to run `python setup.py install`
as
Error compiling Cython file:
------------------------------------------------------------
...
return testclass1.test_vector_int_ref(x,y)
@staticmethod
def print_vector(list x):
cdef TestClass1 testclass1
return testclass1.print_vector(x)
^
------------------------------------------------------------
cython/test_class1.pyx:51:38: no suitable method found
I get an error and get angry. .. It's natural when you think about it, but because the `` `print_vector``` function on the C ++ side is overloaded, You don't know which function you're referring to. To solve this, you need to cast and specify which function you are calling.
So, rewrite `cython / test_class1.pyx`
as follows.
cython/test_class1.pyx
@staticmethod
def print_vector(list x):
cdef TestClass1 testclass1
if isinstance(x[0], int):
return testclass1.print_vector(<vector[int]>x)
elif isinstance(x[0], float):
return testclass1.print_vector(<vector[double]>x)
else:
raise Exception('TypeError')
This is done by first specifying the argument x
as `` `list``` and then
Which function on the C ++ side is called is specified by the type of its contents.
If you actually write it like this, you can build it with `python setup.py install`
,
test.py
import my_library as myl
if __name__ == "__main__":
cl1 = myl.test()
x = [1,2,3]
y = [1.1, 2.2, 3.3]
cl1.print_vector(x)
cl1.print_vector(y)
When you run and test,
(myenv) root@e96f489c2395:/from_local/cython_practice# python test.py
1 2 3
1.1 2.2 3.3
You can see that the function on the C ++ side can be called successfully.
This time, I explained Cythonization when the function on the C ++ side is overloaded (when the configuration has the same function name but different arguments).
I think it's almost covered, and what else do you often write in C ++? I haven't decided what to write next, so I'll write the next one as soon as the material is decided.
This time around.
end.
Recommended Posts