热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

c给python混合编程_请教下python怎么和C或者C++混合编程

网上找来的看下。。附1text.txtthisistesttextintext.txt.附2mymod.pyimportstringmessageoriginalstringmes

网上找来的看下。。

附1 text.txt

this is test text in text.txt.

附2 mymod.py

import string

message = ''original string''

message =message+message

msg_error=""

try:

text_file = open("text.txt", "r")

whole_thing = text_file.read()

print whole_thing

text_file.close()

except IOError, (errno, strerror):

print "I/O error(%s): %s" % (errno, strerror)

def transform(input):

#input = string.replace(input, ''life'', ''Python'')

return string.upper(input)

def change_msg(nul):

global message #如果没有此行,message是函数里头的局部变量

message=''string changed''

return message

def r_file(nul):

return whole_thing

def get_msg(nul):

return message

附3 simplepy.h

using std::string;

class CSimplepy // : private noncopyable

{

public:

///constructor

CSimplepy()

{

Py_Initialize();

pstr=NULL, pmod=NULL, pdict=NULL;

pfunc=NULL, pargs=NULL;

}

virtual ~CSimplepy()

{

Py_Finalize();

}

bool ImportModule(const char* mod_name)

{

try{

pmod  = PyImport_ImportModule(const_cast(mod_name));

if(pmod==NULL)

return false;

pdict = PyModule_GetDict(pmod);

}

catch(...)

{

return false;

}

if(pmod!=NULL && pdict!=NULL)

return true;

else

return false;

}

int Run_SimpleString(const char* str)

{

return PyRun_SimpleString(const_cast(str) );

}

string Run_String(const char* str)

{

char *cstr;

pstr  = PyRun_String(str, Py_eval_input, pdict, pdict);

if(pstr==NULL)

throw ("when Run_String, there is an exception was raised by Python environment.");

PyArg_Parse(pstr, "s", &cstr);

return string(cstr);

}

string CallObject(const char* func_name, const char* parameter)

{

pfunc=NULL;

pfunc = PyObject_GetAttrString(pmod, const_cast(func_name));

if(pfunc==NULL)

throw (string("do not found in Python module for: ")

+func_name).c_str();

char* cstr;

pargs = Py_BuildValue("(s)", const_cast(parameter));

pstr  = PyEval_CallObject(pfunc, pargs);

if(pstr==NULL)

throw ("when PyEval_CallObject, there is an exception was raised by Python environment");

PyArg_Parse(pstr, "s", &cstr);

return string(cstr);

}

protected:

PyObject *pstr, *pmod, *pdict;

PyObject *pfunc, *pargs;

};



推荐阅读
  • python教程分享Pytorchmlu 实现添加逐层算子方法详解
    目录1、注册算子2、算子分发3、修改opmethods基类4、下发算子5、添加wrapper6、添加wrapper7、算子测试本教程分享了在寒武纪设备上pytorch-mlu中添加 ... [详细]
  • 实现方式1、首先设置一个Qt下的一个窗口基类;2、窗口基类继承自一个重写的QGLWidget类和一个osgViewer::Viewer类3、重新QGLWidget类& ... [详细]
  • 没有得到任何错误,所有数据似乎都有效.出于某种原因,正在调用与视图相关的方法.我确定了以下内容:getItemCount()是唯一被调用的适配器方法,并返回一个正整数值,(我知道 ... [详细]
  • ImtryingtoaccessarequestfromanHTMLform,andsenditasamail,butIgetamailwithaval ... [详细]
  • 我是Django的初学者。现在,我正在使用API​​。我面临一个问题。我无法在 ... [详细]
  • Linux多线程(2)
    线程的知识点太多,太重要,所以分成三部分进行总结学习线程安全多个线程并发同一段代码时,不会出现不同的结果。常见对全局变量或者静态变量进行操作,并且没有锁保护的情况下,会出现该问题。 ... [详细]
  • 基于bionic c分析线程的一生
    1.概述和问题进程和线程操作系统基础和重要的机制,从源码角度理解进程和线程的区别对于理解操作系统的基本原理非常有帮助,同时进程和线程的创建又是通过系统 ... [详细]
  • 实验六提交版
    1.21.3part2共用体与结构体类型的区别?答:共用体与结构体的区别在于它们的表示方法不同。结构体内,结构体的各成员顺序排列存储,每个成员都有自己独立的存储位置,而共用体的情况 ... [详细]
  • Causedby:java.nio.charset.MalformedInputException:Inputlength=1
    java.lang.IllegalStateException:Failedtoloadpropertysourcefromlocationclasspath:applicatio ... [详细]
  • 看这篇之前,如果没有看过之前的文章,移步拉到文章末尾查看之前的文章。回顾先捋一下,之前我们实现的Vue类,主要有一下的功能:属性和方法的代理proxy监听属性watcher事件对于 ... [详细]
  • #region批量插入数据批量插入收集库件级文书档案信息实体(批量)数据表publicintBulkInsert(DataTabletable){intinsertCount0;t ... [详细]
  • 篇首语:本文由编程笔记#小编为大家整理,主要介绍了高效算法求解数独相关的知识,希望对你有一定的参考价值。title:高效算法求解数独 ... [详细]
  • Signal 第一个简单Demo
    最简单的聊天室功能1.用VS2013创建一个MVC4(MVC5也类似)项目1.1选择模板为基本2.用NuGet安装SignalR3安装完成, ... [详细]
  • Vue编写添加用户的表单 ~ 不要错过哦
    在写项目的过程中遇到了添加用户信息的功能,今天写出来和大家分享。希望可以帮助到有需要的小伙伴文章目录使用element-ui实现布局和样式javascript中的数据 ... [详细]
  • 后台生产验证码code和byte[]图片引用命名空间usingSystem.Drawing;usingSystem.Drawing.Drawing2D;usingSystem.Dr ... [详细]
author-avatar
手机用户2502862793
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有