Python: 我们能否将 ctypes 结构转换为字典?

14

我有一个ctypes结构。

class S1 (ctypes.Structure):
    _fields_ = [
    ('A',     ctypes.c_uint16 * 10),
    ('B',     ctypes.c_uint32),
    ('C',     ctypes.c_uint32) ]
如果我有X=S1(),我想从这个对象中返回一个字典:例如,如果我做类似这样的事情:Y=X.getdict()或者Y=getdict(X),那么Y可能看起来像:
{ 'A': [1,2,3,4,5,6,7,8,9,0], 
  'B': 56,
  'C': 8986 }

有什么帮助吗?


1
顺便问一下,你需要一个真正的dict对象而不能只是将__getitem__()等方法添加到你的结构类中吗? - llasram
在我的情况下,我想要一个包含有关系统进程数据的字典,因此将其放入字典中更有意义。 - RobotHumans
3个回答

12

可能是这样:

def getdict(struct):
    return dict((field, getattr(struct, field)) for field, _ in struct._fields_)

>>> x = S1()
>>> getdict(x)
{'A': <__main__.c_ushort_Array_10 object at 0x100490680>, 'C': 0L, 'B': 0L}

正如你所看到的,它可以处理数字,但是处理数组时就不那么顺畅了——你必须自己负责将数组转换为列表。一个更复杂的版本尝试着将数组转换为列表,代码如下:

def getdict(struct):
    result = {}
    for field, _ in struct._fields_:
         value = getattr(struct, field)
         # if the type is not a primitive and it evaluates to False ...
         if (type(value) not in [int, long, float, bool]) and not bool(value):
             # it's a null pointer
             value = None
         elif hasattr(value, "_length_") and hasattr(value, "_type_"):
             # Probably an array
             value = list(value)
         elif hasattr(value, "_fields_"):
             # Probably another struct
             value = getdict(value)
         result[field] = value
    return result

如果你拥有numpy并且想要处理多维C数组,你应该添加import numpy as np并修改:

 value = list(value)

需要翻译的内容:

to:

 value = np.ctypeslib.as_array(value).tolist()

这将为您提供一个嵌套列表。


来到这里是为了寻找非常相似的东西。@G.A.,你是否曾经让反向工作?我认为我已经得到了一个更通用的getdict(嵌套结构,带有内置或结构体数组)和一个通用的getstruct,但我不确定它们是否完全优化或最佳方法。 - Mark
@马克:不,我没有把反向转换做好。但是现在我已经转到其他事情上了。可能我不能尝试反向转换了。 - G.A.
@G.A. 好的,我会找出我的函数并发布为WIP答案。 - Mark
请注意,在Python2.7及更早版本中,第一个“if”子句将无法检测到“long”类型。因此,为了使其正常工作,您应该将“long”添加到类型列表中。 - jrast
谢谢,我已经将“long”添加到类型列表中。 - Tamás
显示剩余3条评论

2
更通用的处理双重数组、结构体数组和位域的方法。
def getdict(struct):
    result = {}
    #print struct
    def get_value(value):
         if (type(value) not in [int, long, float, bool]) and not bool(value):
             # it's a null pointer
             value = None
         elif hasattr(value, "_length_") and hasattr(value, "_type_"):
             # Probably an array
             #print value
             value = get_array(value)
         elif hasattr(value, "_fields_"):
             # Probably another struct
             value = getdict(value)
         return value
    def get_array(array):
        ar = []
        for value in array:
            value = get_value(value)
            ar.append(value)
        return ar
    for f  in struct._fields_:
         field = f[0]
         value = getattr(struct, field)
         # if the type is not a primitive and it evaluates to False ...
         value = get_value(value)
         result[field] = value
    return result

2
如何尝试这样的方案:

class S1(ctypes.Structure):
    _fields_ = [ ... ]

    def getdict(self):
        dict((f, getattr(self, f)) for f, _ in self._fields_)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接