Tag: python

从C调用python hello world函数,解析字符串参数

我使用此答案中的代码创建以下文件 callpython.c #include int main(int argc, char *argv[]) { PyObject *pName, *pModule, *pDict, *pFunc; PyObject *pArgs, *pValue; int i; if (argc < 3) { fprintf(stderr,"Usage: call pythonfile funcname [args]\n"); return 1; } Py_Initialize(); pName = PyString_FromString(argv[1]); /* Error checking of pName left out */ //fprintf(stderr,"pName is %s\n", pName); PyRun_SimpleString("import sys"); PyRun_SimpleString("sys.path.append(\".\")"); //PySys_SetArgv(argc, argv); pModule = […]

Python有条件地解决延迟微分方程

我使用dde23包的pydelay来解决延迟微分方程。 我的问题:如何有条件地编码方程式? 例如,目标方程有两个选项: when x>1, dx/dt=0.25 * x(t-tau) / (1.0 + pow(x(t-tau),10.0)) -0.1*x otherwise, dx/dt=0.25 * x 我尝试了两种方法,但似乎没有一种方法可行: 方法1没有抱怨,但if else语句没有被解释。 方法2产生了以下错误: 找到可执行文件c:\ mingw \ bin \ g ++。exe c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:在函数’double f(double,double)’中:c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ […]

如何在Swig中使用Python中的float **?

我正在为一些c函数编写swig绑定。 其中一个函数需要浮动**。 我已经在使用cpointer.i作为普通指针并查看了carrays.i ,但我没有找到一种方法来声明一个浮点数**。 您有什么推荐的吗? 接口文件: extern int read_data(const char * file,int * n_,int * m_,float ** data_,int ** classes_);

双向PyFloat转换是不正确的

我正在学习SWIG,因为在Python中使用C语言。 我写了这个函数,但我无法理解,为什么包装的myfunc返回错误的float / double值: mfuncs.c #include float myfunc(int n) { float result; result = 100 / n; return result; } mfuncs.i %module mfuncs %typemap(out) double, float “$result = PyFloat_FromDouble($1);” extern float myfunc(int n); 最后我得到1107558400.0而不是33.33333。 >>>导入mfuncs >>> mfuncs.myfunc(3) 1107558400.0 >>> 哪里出错了?

C:staticforward的Python扩展

所以我需要使用子进程模块的代码来添加我需要的一些function。 当我尝试编译_subprocess.c文件时,它会给出以下错误消息: Error 1 error C2086: ‘PyTypeObject sp_handle_type’ : redefinition 这是与_subprocess.c文件相关的代码部分: typedef struct { PyObject_HEAD HANDLE handle; } sp_handle_object; staticforward PyTypeObject sp_handle_type; static PyObject* sp_handle_new(HANDLE handle) { sp_handle_object* self; self = PyObject_NEW(sp_handle_object, &sp_handle_type); if (self == NULL) return NULL; self->handle = handle; return (PyObject*)self; } #if defined(MS_WIN32) && !defined(MS_WIN64) #define HANDLE_TO_PYNUM(handle) PyInt_FromLong((long) handle) #define PY_HANDLE_PARAM […]

在初始化时嵌入python错误

当我运行C代码来调用python函数时,Py_Initialize()上出现错误错误是ImportError:没有名为site的模块。 我试过把Py_SetProgramName(argv [0])但它不起作用。 cmd调用是cInterfacePython Test.py乘以3 2(exe是cInterfacePython)

ctypes POINTER(c_void_p)在回调函数中

我目前面临着ctypes的问题。 我有一个C函数foo,这样: void** foo(int); 我必须为foo函数定义一个回调函数。 所以: FOO_FUNC = CFUNCTYPE(POINTER(c_void_p), c_int) foo_c = lib.foo foo.argtypes = [c_int] foo.restype = POINTER(c_void_p) 不幸的是,在调用函数时将此回调函数作为参数,它会给出错误: TypeError: invalid result type for callback function 我看不出问题……任何人都可以帮助我吗? 谢谢

SWIG与python和C:参数

我有这个function: void func(int* a, int b); 我希望在python中提供这样的内容: func(list, int) 即,用户传递一个列表和一个整数(告诉函数应该在列表中存储多少条目)。 为此,我需要在“a”的初始化中知道“b”的值(因为我需要临时的原始C int数组)。 %typemap(in) ( int* a) { //need to allocate sizeof(int) * b bytes temporarily } 但我不知道“b”的值,因为它只是稍后解析! 如何访问“b”参数的值?

如何将结构列表/数组从python传递给C

我有一个C函数,必须可以从C和Python调用。 我无法弄清楚如何将c-cons结构的python列表传递给c函数,每个结构包含几个嵌套结构。 这些结构中的一个在python中看起来像这样: class STATION_MM_NODE(ctypes.Structure): _fields_ = [ (“signal”, MM_STRUCT), (“noise”, MM_STRUCT), (“signalWindowLen”, ctypes.c_double), (“metadata”, SAC_PZ) ] 在C中像这样: typedef struct stationMMnode { struct mantleMagStruct *signal; struct mantleMagStruct *noise; double signalWindowLen; SAC_PZ metadata; } stationMMnode_t; 采用stationMMnode结构数组的c函数可以调用为: double magnitudeCompute_Mw_Mm_Event(stationMMnode_t **stationMMarray, int numStations); 例如,我可以将其称为纯粹来自C,如: int testfunc() { stationMMnode_t *node1 = malloc(sizeof(struct stationMMnode)); node1->signalWindowLen = 500; stationMMnode_t *node2 = […]

在C中自定义实现“tail -f”function

编辑:我最后使用了inotify。 正如stefanB所说,inotify是可以使用的东西。 我发现了一个尾部克隆,它使用inotify来实现-f模式, inotail 。 原始问题文本: 我正在尝试在C项目中实现“tail -f”逻辑,为了进行原型设计,我在python中开发它如下: # A forever loop, each 5 seconds writes a line into file.txt from time import * while 1: sleep(5) file = open(“file.txt”, “a”) file.write(“This is a test\n”) file.close() 下一个代码遵循file.txt的eof(由上面的代码更新) # tail -f from time import * file = open(“file.txt”, “r”) file.seek(0, 2) while 1: line = file.readline() […]