Created
October 23, 2016 18:02
-
-
Save CyclingNinja/9e8477277d14272785371324c8ab9a38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# distutils: language = c++ | |
# distutils: sources = squaremod.cpp | |
# call to the c++ header, `except +` allows a python error to | |
# be raised in the case there is an allocation error | |
# includes the variable and call to the method inc. what kind of variable you are passing it | |
cdef extern from "squaremod.h" namespace "ops": | |
cdef cppclass Powers: | |
Powers() except + | |
double x | |
double getSqr(double) | |
double getCube(double) | |
# call the inctance of the c class | |
cdef class PySquare: | |
cdef Powers c_powers # hold a C++ instance of what you're wrapping | |
# initialise the C class | |
def __cinit__(self): | |
self.c_powers = Powers() | |
# actually call the function we're wanting | |
def get_sqr(self, double x): | |
return self.c_powers.getSqr(x) | |
# call another function from the class | |
def get_cube(self, double x): | |
return self.c_powers.getCube(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from distutils.core import setup | |
from Cython.Build import cythonize | |
ext_modules=cythonize( | |
"PySquare.pyx", | |
sources=['squaremod.cpp'], | |
language="c++", | |
) | |
setup( | |
name = "squaremod", | |
ext_modules = ext_modules | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "squaremod.h" | |
namespace ops { | |
Powers :: Powers() {} | |
Powers::~Powers() {} | |
double Powers::getSqr(double x) { | |
return x*x; | |
} | |
double Powers::getCube(double x) { | |
return x*x*x; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace ops { | |
class Powers{ | |
public: | |
double x; | |
Powers(); | |
~Powers(); | |
double getSqr(double x); | |
double getCube(double x); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment