pybind11
While pybind11 is mainly focused on extending Python using C++, it’s also possible to do the reverse: embed the Python interpreter into a C++ program
sample
- layout
demo/
|-- fs.py
|-- main.cpp
`-- build.sh
- python
#!/usr/bin/env python
# -*- coding=utf-8 -*-
import sys
if sys.version_info.major == 2:
import commands
else:
import subprocess as commands
import os
def ls(dir):
cmd = 'ls ' + dir
s, o = commands.getstatusoutput(cmd)
if s == 0:
return [ l.strip() for l in o.split('\n')]
else:
raise Exception(o)
def meta(entry):
cmd = 'ls -l ' + entry
s, o = commands.getstatusoutput(cmd)
if s == 0:
return o.split('\n')[0]
else:
raise Exception(o)
def size_scan(dir):
cmd = 'ls -l ' + dir + " | awk '{print $5, $9}'"
s, o = commands.getstatusoutput(cmd)
if s == 0:
info = {}
for l in o.split('\n'):
l = l.strip()
if l:
size, name = l.split(' ', 1)
info[name.strip()] = int(size.strip())
return info
else:
raise Exception(o)
def tmax(k1, k2, k3):
return max(max(k1, k2), k3)
if __name__ == "__main__":
dir = sys.argv[1] if len(sys.argv) > 1 else '.'
for each in ls(dir):
print(meta(each))
print(size_scan(dir))
- cplusplus
#include <string>
#include <iostream>
#include <map>
#include <pybind11/embed.h> // everything needed for embedding
#include <pybind11/stl.h> // py::dict
namespace py = pybind11;
using namespace std;
int main(int argc, char** argv)
{
string dir = argc > 1 ? argv[1] : ".";
py::scoped_interpreter guard{}; // start the interpreter and keep it alive
cout << "-------" << endl;
py::module fs = py::module::import("fs");
py::object result = fs.attr("ls")(dir); // or py::list result = fs.attr("ls")(dir);
for (auto each : result)
{
cout<< fs.attr("meta")(each).cast<string>() << endl;
}
cout << "-------" << endl;
py::dict dict = fs.attr("size_scan")(dir);
for (auto each : dict)
{
cout<< each.first << ":" << each.second << endl;
}
cout << "-------" << endl;
try
{
fs.attr("ls")("/a/b/c");
}
catch(const exception& e)
{
cout << e.what() << endl;
}
cout << "-------" << endl;
py::dict args;
args["k1"] = 0;
args["k2"] = 1;
args["k3"] = 2;
count << fs.attr("tmax")(**args) << endl;
return 0;
}
- build
PYINCLUDES=`python-config --includes`
g++ -std=c++14 \
-I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 \
-L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -lpython2.7 \
main.cpp \
-o main
ref:
- https://pybind11.readthedocs.io/en/stable/advanced/embedding.html
- https://stackoverflow.com/questions/57021752/how-to-manipulate-pybind11dict-in-c
nanobind
- benchmark:https://nanobind.readthedocs.io/en/latest/benchmark.html