Как построить массив структуры ctypes в Python - PullRequest
0 голосов
/ 29 декабря 2018

Назначение

Доставка массива структуры из модуля python в модуль c.

Метод

Использование типов Pythonвызывать c api для передачи данных.

Steps

  1. Объявить прототип структуры в python;(проход, строка 4-9)
  2. Определить массив структур;(передача, строка 16-17)
  3. Заполнить значения в этом массиве;(ошибка, строка 30)

C Декларация API

injectNodes(int nodeNum, struct node *pNode);

struct node {
    uint16_t id;
    uint8_t version;
    uint8_t depth;
};

Код Python

#!/bin/python3
import pdb
from ctypes import *
class Node(Structure):
    _field_ = [
        ("id",    c_uint16),
        ("version", c_uint8),
        ("depth",    c_uint8)
    ]

dics = [{'ID': '3', 'VERSION': '180', 'DEPTH': '924'},
        {'ID': '9', 'VERSION': '180', 'DEPTH': '269'},
        {'ID': '2', 'VERSION': '180', 'DEPTH': '537'}]

nodeNum = len(dics)
NODES = Node * nodeNum
nodes = NODES()
for j in range(nodeNum):
    print(dics[j])
    node = Node()
    node.id = int(dics[j]["ID"])
    node.version = int(dics[j]["VERSION"])
    node.depth = int(dics[j]["DEPTH"])
    print("id", node.id)
    print("version", node.version)
    print("depth", node.depth)
    nodes[j] = node
    print("id", nodes[j].id)
    print("version", nodes[j].version)
    print("depth", nodes[j].depth)
print(nodes)

Ожидаемые результаты

{'ID': '3', 'DEPTH': '924', 'VERSION': '180'}
id 3
version 180
depth 924
id 3
version 180
depth 924
{'ID': '9', 'DEPTH': '269', 'VERSION': '180'}
id 9
version 180
depth 269
id 9
version 180
depth 269
{'ID': '2', 'DEPTH': '537', 'VERSION': '180'}
id 2
version 180
depth 537
id 2
version 180
depth 537

Фактические результаты

{'ID': '3', 'DEPTH': '924', 'VERSION': '180'}
id 3
version 180
depth 924
Traceback (most recent call last):
  File "array_test.py", line 28, in <module>
    print("id", nodes[j].id)
AttributeError: 'Node' object has no attribute 'id'

1 Ответ

0 голосов
/ 30 декабря 2018

Structure должно иметь определение _fields_, а не __field__.

Также существует другая ошибка.depth объявлен как c_uint8, который имеет диапазон 0-255, но все значения в словаре больше 255.

Я предлагаю определить __repr__ в структуре, чтобы Node могпечатать самому, используя более «питонический» цикл:

from ctypes import *

class Node(Structure):

    _fields_ = [("id", c_uint16),
                ("version", c_uint8),
                ("depth", c_uint8)]

    def __repr__(self):
        return f'Node(id={self.id}, version={self.version}, depth={self.depth})'

dics = [{'ID': '3', 'VERSION': '180', 'DEPTH': '924'},
        {'ID': '9', 'VERSION': '180', 'DEPTH': '269'},
        {'ID': '2', 'VERSION': '180', 'DEPTH': '537'}]

nodeNum = len(dics)
NODES = Node * nodeNum
nodes = NODES()

for node,dic in zip(nodes,dics):
    node.id = int(dic['ID'])
    node.version = int(dic['VERSION'])
    node.depth = int(dic['DEPTH'])

for node in nodes:
    print(node)

Вывод (depth - это модуль 256 из-за его определения c_uint8):

Node(id=3, version=180, depth=156)
Node(id=9, version=180, depth=13)
Node(id=2, version=180, depth=25)
...