2006年04月02日 星期日 12:38
> > 我看你们C和Python交互是都要用到头文件Python.h 这个文件哪有啊? -- 天蝎座的付出爱是狂热,却忘了学习爱是妥协! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060402/dad18279/attachment.html
2006年04月02日 星期日 16:23
安装python啊,然后设置include目录 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060402/897912e5/attachment.htm
2006年04月04日 星期二 09:42
继续讨论这个问题:
昨天晚上,确切说是今天早上翻了一下python帮助,看了extending and embeding python,
其中提到application是可以作为模块为python脚本提供编程接口的。枉我以前以为只能通过dll的方式提供接口。
现在的问题是如何将一个类提供给python操作。
我还没有开始尝试,先提出来给大家讨论一下吧。
//---------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060404/c86efdfb/attachment.htm
2006年04月04日 星期二 23:15
我想这个可以参考boost.python或者SWIG的源码吧,这个需要你对Python的对象模型非常了解才能了然于胸 :) On 4/4/06, 吴俊玉 <wujunyu at gmail.com> wrote: > > 继续讨论这个问题: > 昨天晚上,确切说是今天早上翻了一下python帮助,看了extending and embeding python, > 其中提到application是可以作为模块为python脚本提供编程接口的。枉我以前以为只能通过dll的方式提供接口。 > 现在的问题是如何将一个类提供给python操作。 > 我还没有开始尝试,先提出来给大家讨论一下吧。 > > //--------------------------------------------------------------------------------- > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- Robert Python源码剖析――http://blog.donews.com/lemur/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060404/09703fc8/attachment.htm
2006年04月05日 星期三 09:41
也许我该马上看看boost,希望不会太难! 我重新描述下我的问题: 既然python与c++要交互,必然存在一个共享对象,c++中和python脚本共同操作的! 如果使用面向过程的方式而不用对象的话,不是很难,我想我应该能够搞定了! 如果要共享对象的话,我有个不是很优雅的解决方案 宿主提供过程化的函数接口 ----------> 脚本根据宿主提供的函数写一个基类包装宿主提供的函数接口----------> 宿主程序包装基类的函数。 --------------------因为python对象在c语言中都是PyObject(没错吧) //=========================================================== 以上描述指针对我所遇到的问题 不知道大家有没有什么方式能够直接在宿主中提供对象借口给python 我昨天尝试了一下,显示错误信息是不能从__thiscall 转换,强制转换应该不行吧?没试过! 好吧,简化一下问题 怎么把一个宿主语言中的对象共享给python脚本? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060405/a1a08175/attachment.htm
2006年04月05日 星期三 18:04
要崩溃了! 今天又看了一些boost,确实看到了一些例子能够把类封装成python模块, 天哪,还是使用bjam的,以前就没有手写过makefile,等于又要学一样工具 我又晕头转向编译了一下boost, 还是使用dll的――我可不想使用dll的方式 而且boost的这种方式让我害怕 时间不多了,还有5天要完成编码了! 明天看一下boost。python源码吧!希望有帮助、 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060405/5c3192d1/attachment.htm
2006年04月05日 星期三 20:12
我建议你还是用正统的Python的C扩展方式,用一个C的dll包装你的C++的class,呵呵,这个不会耽误你太多时间 :) On 4/5/06, 吴俊玉 <wujunyu at gmail.com> wrote: > > 要崩溃了! > > 今天又看了一些boost,确实看到了一些例子能够把类封装成python模块, > 天哪,还是使用bjam的,以前就没有手写过makefile,等于又要学一样工具 > 我又晕头转向编译了一下boost, > > 还是使用dll的――我可不想使用dll的方式 > 而且boost的这种方式让我害怕 > 时间不多了,还有5天要完成编码了! > 明天看一下boost。python源码吧!希望有帮助、 > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- Robert Python源码剖析――http://blog.donews.com/lemur/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060405/973b56e4/attachment.html
2006年04月06日 星期四 01:07
从网上看到的一个例子(有C代码和PYTHON代码),应该可以解决你的问题。 https://www6.software.ibm.com/developerworks/education/l-pythonscript/index.html #include/* Create a function to handle errors when they occur */ void error(char errstring) { printf("%s\n",errstring); exit(1); } int main() { /* Set up the variables to hold methods, functions and class instances. farenheit will hold our return value */ PyObject *ret, *mymod, *class, *method, *args, *object; float farenheit; Py_Initialize(); /* Load our module */ mymod = PyImport_ImportModule("celsius"); /* If we dont get a Python object back there was a problem */ if (mymod == NULL) error("Can't open module"); /* Find the class */ class = PyObject_GetAttrString(mymod, "celsius"); /* If found the class we can dump mymod, since we won't use it again */ Py_DECREF(mymod); /* Check to make sure we got an object back */ if (class == NULL) { Py_DECREF(class); error("Can't find class"); } /* Build the argument call to our class - these are the arguments that will be supplied when the object is created */ args = Py_BuildValue("(f)", 100.0); if (args == NULL) { Py_DECREF(args); error("Can't build argument list for class instance"); } /* Create a new instance of our class by calling the class with our argument list */ object = PyEval_CallObject(class, args); if (object == NULL) { Py_DECREF(object); error("Can't create object instance"); } /* Decrement the argument counter as we'll be using this again */ Py_DECREF(args); /* Get the object method - note we use the object as the object from which we access the attribute by name, not the class */ method = PyObject_GetAttrString(object, "farenheit"); if (method == NULL) { Py_DECREF(method); error("Can't find method"); } /* Decrement the counter for our object, since we now just need the method reference */ Py_DECREF(object); /* Build our argument list - an empty tuple because there aren't any arguments */ args = Py_BuildValue("()"); if (args == NULL) { Py_DECREF(args); error("Can't build argument list for method call"); } /* Call our object method with arguments */ ret = PyEval_CallObject(method,args); if (ret == NULL) { Py_DECREF(ret); error("Couldn't call method"); } /* Convert the return value back into a C variable and display it */ PyArg_Parse(ret, "f", &farenheit;); printf("Farenheit: %f\n", farenheit); /* Kill the remaining objects we don't need */ Py_DECREF(method); Py_DECREF(ret); /* Close off the interpreter and terminate */ Py_Finalize(); return 0; } ///////////////// class celsius: def __init__(self, degrees): self.degrees = degrees def farenheit(self): return ((self.degrees*9.0)/5.0)+32.0 On 4/5/06, Robert Chen <search.pythoner at gmail.com> wrote: > > 我建议你还是用正统的Python的C扩展方式,用一个C的dll包装你的C++的class,呵呵,这个不会耽误你太多时间 :) > > On 4/5/06, 吴俊玉 <wujunyu at gmail.com> wrote: > > > 要崩溃了! > > 今天又看了一些boost,确实看到了一些例子能够把类封装成python模块, > 天哪,还是使用bjam的,以前就没有手写过makefile,等于又要学一样工具 > 我又晕头转向编译了一下boost, > > 还是使用dll的――我可不想使用dll的方式 > 而且boost的这种方式让我害怕 > 时间不多了,还有5天要完成编码了! > 明天看一下boost。python源码吧!希望有帮助、 > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > -- > Robert > Python源码剖析――http://blog.donews.com/lemur/ > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060406/cf55c8fa/attachment.html
2006年04月07日 星期五 09:02
谢谢,不过光有这些还不够。 我想能不能直接传个对象的引用给c程序呢? 经过测试似乎不行: #include "python.h" #include#include extern "C" __declspec (dllexport) PyObject * tankforward(PyObject *self,PyObject * args) { return Py_BuildValue("i",PyInstance_Check(args)); } static PyMethodDef tankmethod[]= { {"forward",tankforward,METH_VARARGS,NULL}, {NULL,NULL} }; int main(int argc,char ** argv) { Py_Initialize(); Py_InitModule("tank",tankmethod); PyRun_SimpleString("import tank\n" "class myTank:\n" "\t\"this is my tank\"\n" "\tdef test(self):\n" "\t\tprint 'its a test'\n" "c=myTank()\n" "print tank.forward(c)" ); Py_Finalize(); return 0; } //------------------------------------------------------------------------------------------------------------------------- tank.forward(c)是传了含有一个myTank对象的tuple给c函数, 而c函数PyObject * tankforward(PyObject *self,PyObject * args) 我查了下资料,需要一个tuple参数 return Py_BuildValue("i",PyInstance_Check(args)); 可是为什么返回的是0呢?或者说我传过来的不是PyInstance? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/0c662d6a/attachment-0001.html
2006年04月07日 星期五 09:12
args不是一个tuple吗?你用PyInstance_Check检查自然应该是False吧 On 4/7/06, 吴俊玉 <wujunyu at gmail.com> wrote: > > 谢谢,不过光有这些还不够。 > 我想能不能直接传个对象的引用给c程序呢? > 经过测试似乎不行: > > #include "python.h" > #include> #include > > extern "C" __declspec (dllexport) PyObject * tankforward(PyObject > *self,PyObject * args) > { > return Py_BuildValue("i",PyInstance_Check(args)); > } > > static PyMethodDef tankmethod[]= > { > {"forward",tankforward,METH_VARARGS,NULL}, > {NULL,NULL} > }; > > int main(int argc,char ** argv) > { > Py_Initialize(); > Py_InitModule("tank",tankmethod); > PyRun_SimpleString("import tank\n" > "class myTank:\n" > "\t\"this is my tank\"\n" > "\tdef test(self):\n" > "\t\tprint 'its a test'\n" > "c=myTank()\n" > "print tank.forward(c)" > ); > Py_Finalize(); > return 0; > } > //------------------------------------------------------------------------------------------------------------------------- > > > tank.forward(c)是传了含有一个myTank对象的tuple给c函数, > > 而c函数PyObject * tankforward(PyObject *self,PyObject * args) > 我查了下资料,需要一个tuple参数 > > return Py_BuildValue("i",PyInstance_Check(args)); > > 可是为什么返回的是0呢?或者说我传过来的不是PyInstance? > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- Robert Python源码剖析――http://blog.donews.com/lemur/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/dab5b3b5/attachment.html
2006年04月07日 星期五 09:28
你好!
请问,我想用python建立HTML文本的DOM树,请问有可用的模块吗?(不用Win32下COM)
谢!
Steven Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/3a8dc197/attachment.htm
2006年04月07日 星期五 10:11
把html当成xml,或者转换成xhml(tidy <http://tidy.sourceforge.net/>),直接使用xml的dom工具 在06-4-7,上海贤昌 - 王志东 <zdwang at xinces.com > 写道: > > 你好! > 请问,我想用python建立HTML文本的DOM树,请问有可用的模块吗?(不用Win32下COM) > > 谢! > > Steven Wang > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- 茫茫人海,你是我的最爱 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/6fe6d36f/attachment.html
2006年04月07日 星期五 10:44
说的没错哦 但如果我就要这个python对象该怎么办呢? 我就要从python直接传个对象过来该怎么做呢? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/98f2f29c/attachment.html
2006年04月07日 星期五 10:49
可以试试 PyObject *instance = NULL; PyArg_Parse(args, "(O)", &instance;); 然后用PyObject_CallMethod调用instance 没有试过,可能语法有些什么错误,比如说&instance;应该是instance等等... 你调试一下吧 :) On 4/7/06, 吴俊玉 <wujunyu at gmail.com> wrote: > > 说的没错哦 > 但如果我就要这个python对象该怎么办呢? > 我就要从python直接传个对象过来该怎么做呢? > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- Robert Python源码剖析――http://blog.donews.com/lemur/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/447f42c6/attachment.htm
2006年04月07日 星期五 11:43
建立HTML的DOM树, 我看到这个(http://www.boddie.org.uk/python/HTML.html)文章,有没有朋友研究过?代码好像有问题? Steven Wang -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/1a287a80/attachment.html
2006年04月07日 星期五 12:09
On 4/7/06, Steven Wang (:) <zdwang at xinces.com> wrote: > > 建立HTML的DOM树, > 我看到这个(http://www.boddie.org.uk/python/HTML.html)文章,有没有朋友研究过?代码好像有问题? > > Nevow的stan是我见过的python中最简单最直接的HTML构造方法 http://divmod.org/trac/wiki/DivmodNevow 下面是有个HTML生成的讨论, 其中有stan standalone version http://groups.google.com/group/comp.lang.python/browse_thread/thread/d89ccb88d3807d31/efbc4574f71da448%23efbc4574f71da448 -- simple is good http://brucewang.net
2006年04月07日 星期五 14:17
找到了答案,但是,处理中文好像有些问题。from xml.dom.ext.reader import HtmlLib
#Create an HTML reader object
reader = HtmlLib.Reader()
#Create a 4DOM document node parsed from HTML at a URL
doc_node = reader.fromUri("http://www.python.org")
#Get the title of the HTML document
title_elem = doc_node.documentElement.getElementsByTagName("TITLE")[0]
#title_string becomes "Python Language Website"
title_string = title_elem.firstChild.data----- Original Message -----
From: Steven Wang (:)
To: python-chinese at lists.python.cn
Sent: Friday, April 07, 2006 11:43 AM
Subject: [python-chinese] 有没有哪为用过 libxml2dom ?
建立HTML的DOM树, 我看到这个(http://www.boddie.org.uk/python/HTML.html)文章,有没有朋友研究过?代码好像有问题?
Steven Wang
------------------------------------------------------------------------------
_______________________________________________
python-chinese
Post: send python-chinese at lists.python.cn
Subscribe: send subscribe to python-chinese-request at lists.python.cn
Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/c3ecf021/attachment.html
2006年04月07日 星期五 14:19
你要的是不是这样的结果?
PyObject *pCall, *pFile;
pCall = PyImport_ImportModule("cPickle");
if (!pCall)
return false;
pFile = PyFile_FromString((char*)FileName.data(), "wb");
if (!pFile)
return false;
pResult = PyObject_CallMethod(pCall, "dump", "OOi", pScanData, pFile,
-1);
Py_DECREF(pFile);
if (!pResult)
return false;
在06-4-7,吴俊玉 <wujunyu at gmail.com> 写道:
>
> 说的没错哦
> 但如果我就要这个python对象该怎么办呢?
> 我就要从python直接传个对象过来该怎么做呢?
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
--
My Blog >> http://leejd.cndev.org
My QQ >> 9847243
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/f3b5ef6a/attachment.htm
2006年04月07日 星期五 16:15
Robert Chen已经说了,而且成功了。 现在在努力实现中, 越来越觉得软件过程重要了! 现在尝试增量法迭代开发――学习中 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/50a24312/attachment.html
2006年04月09日 星期日 10:34
[Application ]
[createTank ]---| 创建一个子类对象,并创建线程运行该子类的run函数。
|
|
[tank ] | [API ] 我的程序提供一些函数以中断线程
---------- | ------
[forward ]---------|---------------[wait] forward方法会调用
[__init__] | [wake]
^ |
| |
| |
[mytank(tank)]--|
[ run ] run函数会调用tank的forward方法,
用户从我的tank类继承,编写新tank子类的脚本
我的createTank函数代码如下:
static Tank * createTank(const std::string name,int i,int j,int h,int w)
{
/*name坦克名称和脚本文件名一致,防止混乱!*/
PyObject *TankModule = NULL;
PyObject *TankDict = NULL;
PyObject *TankClassObj = NULL;
PyObject *TankObject = NULL;
PyObject *ConstructArgs=NULL;
std::ofstream out("c:\\debug.txt");
char * mdlname=new char[name.length()+1];
for(int k=0;k<<"mdlname"<<<<"1TankModule"<< <<"2TankDict"<< <<"3TankClassObj"<< <<"4TankObject"<< <<"5Tank"<< http://lists.exoweb.net/pipermail/python-chinese/attachments/20060409/ead2ffe2/attachment.html
2006年04月09日 星期日 12:00
或许我得工程设定中改变了些什么 原来能运行的例子,也不行了,同样的错误: python.dll模块错误, 但python没有提供python.dll吧! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060409/0e6068a1/attachment.htm
Zeuux © 2025
京ICP备05028076号