易截截图软件、单文件、免安装、纯绿色、仅160KB

vc中调用python代码

运行一句python命令
对vc设置路径
include:D:\PYTHON31\INCLUDE
lib:D:\PYTHON31\LIBS
#include "stdafx.h"
#include "python.h"
int main(int argc, char* argv[])
{
 Py_Initialize() ;
 PyRun_SimpleString("print('Hello')");
 //PyRun_SimpleString("print(dir())");
 Py_Finalize();    
 return 0;
}
编译、连接
拷贝D:\Python31\python31.dll到exe文件目录
运行,输出到控制台
可以运行多行命令
Py_Initialize() ;
PyRun_SimpleString("x=100");
PyRun_SimpleString("print(x)");
PyRun_SimpleString("a=x*x\nprint(a)\n");
Py_Finalize();   
相当于在python下运行下列代码
x=100
print(X)
a=x*x
print(a)
控制台输入 
以下代码解释控制台输入
#include "stdafx.h"
#include "python.h"
int main(int argc, char* argv[])
{
 Py_Initialize() ;
 PyRun_AnyFile(stdin,NULL);
 Py_Finalize();    
 return 0;
}
读取python变量值
将python运行结果读取到vc中
char *cstr;
PyObject *pstr;
PyObject *main_dict;
  
Py_Initialize() ;
PyObject* main_module = PyImport_AddModule("__main__");
main_dict = PyModule_GetDict(main_module);
PyRun_SimpleString("x='abc'");
pstr = PyRun_String("x", Py_eval_input, main_dict, main_dict);
PyArg_Parse(pstr,"s",&cstr); //转换
printf(cstr);
Py_Finalize();


相关文档:

UBUNTU10.04安装stackless python运行高性能服务器

以下"#"开头是Ubuntu终端命令
1。首先安装Ubuntu10.04
参考 http://wiki.ubuntu.org.cn/
2。修改root用户密码
3。使用root登陆系统
 
4。Ubuntu默认已经安装python2.6.5
 
5。下载stackless
查看网址 http://zope.stackless.com/download/sdocument_view
# cd /usr/src
# wget http://www.sta ......

Python为类定义“拷贝构造函数”

初学Python,这么做好像有点2,凑合能用:
class MyClass():
def __init__(self, n = 10):
self._Field = n
def __getitem__(self, range):
return MyClass(self._Field)
obj1 = MyClass()
obj2 = obj1
obj3 = obj1[:]
obj1._Field = 100
obj4 = MyClass(123)
print obj1._Field, obj2. ......

Python中的OS模块

os模块提供了多数操作系统的功能接口函数.当os模块被导入后,它会自适应于不同的操作系统平台,如posix或NT系统平台,os模块会根据不同的平台进行相应的操作.本节内容将对os模块提供的函数进行详细的解读.
1.1 文件操作函数
1.1.1 open()函数提供创建、打开、修改文件的功能。
Example 1-1. Using the os Module to Rename ......

Python标准库的threading.Thread类

这个类表示在单独的控制线程中运行的活动。有两种方法可以指定这种活动,给构造函数传递回调对象,或者在子类中重写run() 方法。其他方法(除了构造函数)都不应在子类中被重写。换句话说,在子类中只有__init__()和run()方法被重写。
一旦线程对象被创建,它的活动需要通过调用线程的start()方法来启动。这方法再调用控制 ......

Python内部的线程实现

在Python中的线程运行实际是受到Interpreter的控制或者说牵制的。在Interpreter的核心函数
PyObject * PyEval_EvalFrameEx
(PyFrameObject *f, int
throwflag)
我们可以看到有一个全局变量_Py_Ticker来控制着线程对Interpreter的占有的,默认是Interpreter每执行一百条指令就会释放另一个全局变量interpreter_lock.
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号