Py11  v0.2
This is a minimalist Python wrapper for C++11.
Py11
Authors
Lenx Tao Wei (lenx.wei at gmail.com)

Introduction

Py11 is a minimalist Python wrapper for C++11, and makes it easier to call Python libraries from C++11.

The git repository is at https://github.com/LenxWei/py11.

Install

Py11 is a pure header library. To install it, just copy the directory include/py11 to system include directory.

Compile

Need a compiler supporting C++11, such as gcc 4.7, clang 3.2 or higher.
Make sure to include correct Python header files and link correct Python libraries. For example:

export CFLAGS="-I/usr/include/python2.7 -std=c++11"
export LDFLAGS=-lpython2.7

Documents

You can find documents about wrapper classes here, and Python utils here.

Examples

Here is the py11 implementation of the original call example.

#include <py11/py.hpp>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
py::obj module, func, value;
py::list args;
try{
if (argc < 3) {
cerr << "Usage: call pythonfile funcname [args]\n";
return 1;
}
// for setting sys.path
py::set_arg(argc, argv);
module = py::import(argv[1]); // throw exception if failed
func = module.attr(argv[2]); // throw exception if not found
args = {};
for (int i = 0; i < argc - 3; ++i) {
args.append(atoi(argv[i + 3]));
}
value = func.call(args.to_tuple()); // throw exception if failed
printf("Result of call: %ld\n", value.as_long());
return 0;
}
catch(const py::err& e){
cerr << e.what() << endl;
return 1;
}
}

Todo:

add tutorial

add exception handling