如何在python中构建结构的ctypes数组

2024-04-25 21:57:49 发布

您现在位置:Python中文网/ 问答频道 /正文

目的

将一个结构数组从python模块传递到c模块。你知道吗

方法

利用python ctypes调用c api来传输数据。你知道吗

步骤

  1. 用python声明结构原型;(pass,第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'

Tags: idnodeversion结构intnodesprintdepth
1条回答
网友
1楼 · 发布于 2024-04-25 21:57:49

Structure必须具有_fields_而不是__field__的定义。你知道吗

还有一个错误。depth声明为c_uint8,其范围为0-255,但字典中的值都大于255。你知道吗

我建议在结构中定义__repr__,以便Node可以打印自己,并使用更多的“pythonic”循环:

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)

相关问题 更多 >