2015-12-24 20 views
5

Bir Python C uzantısı yazıyorum. Bir Python Sözlüğünü C işlevine geçiriyorum. Aşağıdaki kodu kullanarak, bunu ayrıştırmak mümkün duyuyorum:CPython - Bir C İşlevi içindeki Python Sözlüğünü (tuşlarını/değerlerini oku) Bağımsız değişken olarak aktarıldı

İşte
PyObject *large_dict = NULL; 
if (! PyArg_ParseTuple(args, "O!", &PyDict_Type, &large_dict)) return NULL; 
if (large_dict != NULL) 
{ 
    printf("Large Dictionary Not Null\n"); 
} 

başarıyla ayrıştırılır sözlüğü anlamı açıklamada "Büyük Sözlük boş değil" basılır. Şimdi, python'da yaptığımız gibi tuşları belirterek sözlük değerlerine erişmek istiyorum. yani diksiyon ['k1'], ve bu bir değer v1 verir.

Bu C işlevindeki sözlük anahtarlarına/değerlerine nasıl erişebilirim?

Lütfen bana bir çözüm öner?

cevap

5

Sen bağlantı yoluyla gitmeli, aşağıda verilen https://docs.python.org/2/c-api/dict.html alıntı,

PyObject* PyDict_GetItem(PyObject *p, PyObject *key) 
Return value: Borrowed reference. 
Return the object from dictionary p which has a key key. Return NULL if the key key is not present, but without setting an exception. 

PyObject* PyDict_GetItemString(PyObject *p, const char *key) 
Return value: Borrowed reference. 
This is the same as PyDict_GetItem(), but key is specified as a char*, rather than a PyObject*. 

PyObject* PyDict_Items(PyObject *p) 
Return value: New reference. 
Return a PyListObject containing all the items from the dictionary, as in the dictionary method dict.items(). 

PyObject* PyDict_Keys(PyObject *p) 
Return value: New reference. 
Return a PyListObject containing all the keys from the dictionary, as in the dictionary method dict.keys(). 

PyObject* PyDict_Values(PyObject *p) 
Return value: New reference. 
Return a PyListObject containing all the values from the dictionary p, as in the dictionary method dict.values(). 

borrowed reference/new reference bir göz atın. Python uzantıları için kodlama yaparken biraz zor.

+0

Böyle bir şey (https://github.com/aerospike/aerospike-client-python/blob/master/src/main/conversions.c#L155) –

İlgili konular