2017-07-21 2 views
4

私のような機能を備えた装置のためのCライブラリがあります。CでのPython拡張 - パッシングリストを前後

この場合
int GetDevInfo(int *devices); 

を、デバイスは次のように定義されている可能性があります整数の配列であります:

int devices[10] 

この関数は、ハードウェアバスをループしてアクティブデバイスを探します。それらが見つかると、次の使用可能な場所にデバイス番号が入れられます(devices[])。たとえば、スキャンの前に、

devices = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}とグローバル変数DeviceCountには、アクティブなものがあることが分かります。この機能は魔法を発揮し、デバイス5と8がアクティブであると判断します。それでは:DeviceCountが2に設定されることがあります

devices = list(range(64)) 
for i in range(0,len(devices)): # Set them all to 0 
    devices[i] = 0 
DeviceCount = 0 

# Now we'll update it 
DeviceCount = myModule.GetDevInfo(DeviceCount, devices) 

それが戻ったとき、およびデバイスのルックス:

devices = {5, 8, 0, 0, 0, 0, 0, 0, 0, 0}DeviceCount

2.私はPythonの関数は何かのようになりたいです知っていますlike:

整数で渡された文字列を渡す他のラッパーのコードを記述しましたが、それを私に助けてくれるニューヨークの知恵。

def f(a, data): 
    a.append(data) 

l = [1, 2, 3] 
f(l, 4) 

私はl=[1, 2, 3, 4]を得るが、それは事前にC.

ThannksへのPythonラッパーとその効果を得るためにどのように明白ではない、とピザお金がためにそれにあります:私はこのような何かを行うかどうかを知ります助けることができる人!

答えて

0

オーケーは、拡張の一環として、ウェブと試行錯誤の多くがそのコードでは、後で...

static PyObject* LMS_module_timesTwo(PyObject *self, PyObject *args) 
{ 
    int i, numLines; 
    PyObject *listObj, *numObj; 
    unsigned int n; 

    n = PyTuple_Size(args); 
    if (1 == n) { 
    /* argument 1 should be an list of integers */ 
    listObj = PyTuple_GetItem(args, 0); 
    /* get the number of elements in the list */ 
    numLines = PyList_Size(listObj); 

    /* should raise an error here. */ 
    if (numLines < 0) return NULL; /* Not a list */ 

    for (i=0; i<numLines; i++) { 
     numObj = PyList_GetItem(listObj, i); 
     n = PyLong_AsUnsignedLong(PyNumber_Long(numObj)); 
     // This is the action - multiply by 2 for the test case 
     n = n * 2; 
     PyList_SetItem(listObj, i, Py_BuildValue("i", n)); 
    } 
    } else { 
     PyErr_SetString(PyExc_ValueError, "Function expects one argument"); 
     return NULL; 
    } 
    return PyLong_FromLong(1); 
} 

を読んだ(私は他の構造のビットを残したが、誰場合は全部を投稿することができますそれらを必要とします)、これはPython 3のテストプログラムとしてはうねりがあります。

import my_module 

    testlist = [1, 2, 3, 4, 5] 
    print("List before multiplying = ",testlist) 
    my_module.timesTwo(testlist) 
    print("List after multiplying = ",testlist) 

これを読んで回答をお寄せいただきありがとうございます。私の実際のコードでは、エラーチェックとその内容もあります。プログラムが迷惑メールになってしまうと、これは非常に特殊な機能なので何が起こるのか心配していません。

問題は、もともとC関数への配列としてリストを渡すようにそれを提起し、これは最終的に近い:

static PyObject* LMS_module_timesTwo(PyObject *self, PyObject *args) 
{ 
    int i, numLines; 
    PyObject *listObj, *numObj; 
    unsigned int n; 
    unsigned int carray[64]; // this has to be big enough! 

    n = PyTuple_Size(args); 
    if (1 == n) { 
    /* argument 1 should be an list of integers */ 
    listObj = PyTuple_GetItem(args, 0); 
    /* get the number of elements in the list */ 
    numLines = PyList_Size(listObj); 

    /* should raise an error here. */ 
    if (numLines < 0) return NULL; /* Not a list */ 

    for (i=0; i<numLines; i++) { 
     numObj = PyList_GetItem(listObj, i); 
     carray[i] = PyLong_AsUnsignedLong(PyNumber_Long(numObj)); 
    } 
    /* Do the actual work on the array */ 
    someCfunction(carry); // this modifies the array somehow 
    for (i=0; i<numLines; i++) { 
     PyList_SetItem(listObj, i, Py_BuildValue("i", carray[i])); 
    } 
    } else { 
     PyErr_SetString(PyExc_ValueError, "Function expects one argument"); 
     return NULL; 
    } 
    return PyLong_FromLong(1); 
}