python-c-api

Convert Python dictionary into C like structure

烈酒焚心 提交于 2019-12-12 02:57:00
问题 I am a newbie in Python and C and I would like to know how to put dictionary elements into a C like structure (struct). For example, here is my structure: typedef struct { int dim; float *Tab1; float *Tab2; } Tableaux; Here is my dictionary in Python: Tableaux = {} Tableaux["dim"]=None Tableaux["Tab1"]=[] Tableaux["Tab2"]=[] Here is my interface function: static PyObject* py_initTab(PyObject* self, PyObject* args) { PyObject* dict; Tableaux Tab; if (!PyArg_ParseTuple(args, "O!", &dict))

Passing a List and numpy.matrix to a python function from a C++ application

怎甘沉沦 提交于 2019-12-12 01:19:42
问题 I have a bunch of functions written in python (for rapid prototyping). My main project is in C++ and I wanna call these functions from my C++ program.These functions use some specialized python modules like numpy, pyside etc. To start with, I have one function that takes in 4 arguments. The first one is a numpy.matrix object and the other three are simple python lists. The function is returning a numpy.matrix object. I know I'm supposed to use a combination of Python/C API and Numpy/C API,

Python SyntaxError when embedding in C

烈酒焚心 提交于 2019-12-11 20:42:36
问题 I have a python file which runs fine when I execute it against my python interpreter. I'm trying to call the same file from a C program using the python C API: #include <Python.h> #include <stdio.h> int main(int argc, char* argv[]){ FILE* fp; Py_SetProgramName(argv[0]); Py_Initialize(); PySys_SetArgv(argc, argv); fp = fopen("floatcli.py", "r"); PyRun_SimpleFile(fp, "floatcli.py"); Py_Finalize(); } However, when I run this I get a python syntax error: File "floatcli.py", line 1 üBa ^

Pass command line arguments to python 2.7.6 package application using C API

混江龙づ霸主 提交于 2019-12-11 13:01:42
问题 I'm new to python and now I need to call a python 2.7.6 program using its C API. The python program is in the form of a python package and takes several command line options. You can run it like this: python my_py_app input.txt --option1="value1" --option2="value2" Here's what I've been doing: 1, Setup python API using Py_Initialize() ; 2, Load that package using PyImport_ImportModule("my_py_app") and it returns a valid PyObject 3, I don't know how to proceed ... The python C API document

embedding python module error

﹥>﹥吖頭↗ 提交于 2019-12-11 11:22:05
问题 So i have a c to python wrapper that takes input strings and pass them to a python function. the error im getting is that the python API is not recognizing my python file... PyObject *pName, *pModule, *pFunc; QString pyFile="Test.py"; Py_Initialize(); pName = PyUnicode_FromString(pyFile.toAscii().data()); pModule = PyImport_Import(pName); error is "ImportError: No module named Test.py" This is when i have my Test.py in the same directory as my project when i placed my Test.py up one level in

Python C API: WindowsError after creating some number of PyObjects

一世执手 提交于 2019-12-11 07:56:20
问题 I've been having an issue getting the Python C API to not give me errors. Background: I've been using ctypes to run native code (C++) for a while, but until now I had never actually done anything specific with the Python C API. I had mostly just been passing in structs from Python and filling them from C++. The way I was using structs was becoming cumbersome, so I decided I would try creating Python objects directly in C++ and just pass them back to my Python script. Code: I have a DLL ( Foo

Numpy/CAPI error with import_array() when compiling multiple modules

天大地大妈咪最大 提交于 2019-12-11 04:04:39
问题 I am trying to compile a C++ module to use in scipy.weave that is composed of several headers and source C++ files. These files contain classes and methods that extensively use the Numpy/C-API interface. But I am failing to figure out how to include import_array() successfully. I have been struggling on this for the past week and I am going nuts. I hope you could help me with it because the weave help is not very explanatory. In practice I have first a module called pycapi_utils that contains

Python ctypes: how to pass row outputs from a C function into a pandas DataFrame?

冷暖自知 提交于 2019-12-11 02:11:30
问题 My question is how to parse tab-delimited output from a C function into a pandas DataFrame via ctypes: I am writing a Python wrapper in Python3.x around a C library using ctypes. The C library currently does database queries. The C function I am accessing return_query() returns tab-delimited rows from a query, given the path to a file, an index, and a query-string: int return_query(structname **output, const char *input_file, const char *index, const char *query_string); As you can see, I'm

Return list of new custom-class objects in python C API

允我心安 提交于 2019-12-11 01:53:16
问题 I need to create a new list via the python C API containing new copies of objects of a Quaternion class I've written (in C++). [Actually, I'd really like a numpy array, but any sort of sequence would do.] But I'm getting seg faults with everything I've tried. I'm terrible with pointers, so that's not a big surprise. I'm thinking maybe I need to give python ownership of the objects I create with new . The best I've gotten so far appears below. Am I not supposed to copy-construct the Quaternion

PyArg_ParseTuple SegFaults in CApi

穿精又带淫゛_ 提交于 2019-12-11 00:18:08
问题 I am writing a code, trying to get used to the C API of NumPy arrays. #include <Python.h> #include "numpy/arrayobject.h" #include <stdio.h> #include <stdbool.h> static char doc[] = "Document"; static PyArrayObject * trace(PyObject *self, PyObject *args){ PyArrayObject *matin; if (!PyArg_ParseTuple(args, "O!",&PyArray_Type, &matin)) return NULL; printf("a"); return matin; } static PyMethodDef TraceMethods[] = { {"trace", trace, METH_VARARGS, doc}, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC