首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python中构建结构的ctype数组

如何在python中构建结构的ctype数组
EN

Stack Overflow用户
提问于 2018-12-29 07:21:00
回答 1查看 233关注 0票数 0

目的性

将结构数组从python模块传递到c模块。

方法

利用python调用call来传输数据。

步骤

  1. 在python中声明结构原型;(pass,第4-9行)
  2. 定义结构数组;(pass,第16-17行)
  3. 填充此数组的值;(失败,第30行)

C API声明

代码语言:javascript
复制
injectNodes(int nodeNum, struct node *pNode);

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

Python代码

代码语言:javascript
复制
#!/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)

预期结果

代码语言:javascript
复制
{'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

实际结果

代码语言:javascript
复制
{'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'
EN

回答 1

Stack Overflow用户

发布于 2018-12-30 11:02:48

Structure必须有_fields_而不是__field__的定义。

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

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

代码语言:javascript
复制
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)

输出(由于depthc_uint8定义,它是模块256个):

代码语言:javascript
复制
Node(id=3, version=180, depth=156)
Node(id=9, version=180, depth=13)
Node(id=2, version=180, depth=25)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53967546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档