Python: Можем ли мы преобразовать структуру ctypes в словарь? - PullRequest
10 голосов
/ 24 сентября 2010

У меня есть структура 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 }

Любая помощь?

Ответы [ 3 ]

11 голосов
/ 24 сентября 2010

Возможно что-то вроде этого:

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)

до:

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

Это даст вам вложенный список.

2 голосов
/ 24 сентября 2010

Как насчет чего-то вроде:

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

    def getdict(self):
        dict((f, getattr(self, f)) for f, _ in self._fields_)
1 голос
/ 16 декабря 2015

Немного более общего назначения для работы с двойными массивами, массивами структур и битовыми полями.

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
...