📄 basic.cpp
字号:
#include <Python.h>
int
main(int argc, char *argv[])
{
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
char *cstr;
PyObject *pstr, *pmod, *pdict;
PyObject *pfunc, *pargs;
/* get mymod.message */
pmod = PyImport_ImportModule("mymod");
pdict = PyModule_GetDict(pmod);
pstr = PyRun_String("message", Py_eval_input, pdict, pdict);
/* convert to C */
PyArg_Parse(pstr, "s", &cstr);
printf("%s\n", cstr);
pstr = PyRun_String("message", Py_eval_input, pdict, pdict);
PyArg_Parse(pstr, "s", &cstr);
printf("%s\n", cstr);
pfunc = PyObject_GetAttrString(pmod, "change_msg");
pargs = Py_BuildValue("(s)", "nul");
pstr = PyEval_CallObject(pfunc, pargs);
PyArg_Parse(pstr, "s", &cstr);
printf("%s\n", cstr);
pfunc = PyObject_GetAttrString(pmod, "get_msg");
pargs = Py_BuildValue("(s)", "nul");
pstr = PyEval_CallObject(pfunc, pargs);
PyArg_Parse(pstr, "s", &cstr);
printf("%s\n", cstr);
pfunc = PyObject_GetAttrString(pmod, "r_file");
pargs = Py_BuildValue("(s)", "nul");
pstr = PyEval_CallObject(pfunc, pargs);
PyArg_Parse(pstr, "s", &cstr);
printf("%s\n", cstr);
//PyRun_String("change_msg()", Py_file_input, pdict, pdict);
pstr = PyRun_String("message", Py_eval_input, pdict, pdict);
PyArg_Parse(pstr, "s", &cstr);
printf("%s\n", cstr);
/* call mymod.transform(mymod.message) */
pfunc = PyObject_GetAttrString(pmod, "transform");
cstr="this is hjs's test, to uppercase";
pargs = Py_BuildValue("(s)", cstr);
pstr = PyEval_CallObject(pfunc, pargs);
PyArg_Parse(pstr, "s", &cstr);
printf("%s\n", cstr);
Py_Finalize();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -