c++ invoke python

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值