Coder Social home page Coder Social logo

py_c_py's Introduction

py_c_py

A cython wrapper from python to C++ to python again. This is only an example how to achieve things. Not a library.

Setup

First build the cpp lib:

$ cd clib
$ make

Then build the C extension with cython (maybe you have to change some flags to correct include paths in setup.py...):

$ python setup.py build_ext -i

Be sure that $LD_LIBRARY_PATH points to the clib directory that libclib.so can be found :

$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$(pwd)/clib

Run the 'tests':

$ python test.py

What is it?

We want to hand over pure python functions to a library that expects std::function objects, let it run and then use the result in python again.

How is it achieved?

First we write a wrapper for each function that expexts a std::function which expects a void* and a std::function with the same signature as the original std::function object plus a void pointer. The void* will point to the python function and the other std::function to something, which can evaluate this python function. We that give the original function a closure with the right signature. What? Here's an example:

// The original function we want to wrap
int orig_func(std::function<double(double)> callback) { // do something }

// The wrapper around that
int orig_func_wrapper(void* python_func, std::function<double(void*, double)> eval_func) {
    return orig_func([](double v){ return eval_func(python_func, v); };
}

Ok, now we need the evaluation function. That one is done in cython. We nee one for each std::function template instantiation we want to use as callback. It gets as first argument the python function as void*

# eval_func for std::fucntion<double(double)> 
cdef inline double evaluate_function(void* func_p, double value):
  # recover Python function object from void* argument and call it
  return (<object> func_p)(value)

Now we want to call the orig_func from python. We wrap that using cython and our wrappers like this:

def py_orig_func(py_func):
    orig_func_wrapper(<void*>py_func, evaluate_function)

Everything else is normal cython. Refer to its documentation for that.

py_c_py's People

Contributors

hildensia avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.