Skip to content

Instantly share code, notes, and snippets.

@CyclingNinja
Created October 23, 2016 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CyclingNinja/9e8477277d14272785371324c8ab9a38 to your computer and use it in GitHub Desktop.
Save CyclingNinja/9e8477277d14272785371324c8ab9a38 to your computer and use it in GitHub Desktop.
# 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)
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
)
#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;
}
}
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