打印python的ctype定义的结构中的数据
来源:互联网 发布:什么是java技术 编辑:程序博客网 时间:2024/05/01 01:15
使用dpkt的包,发现__repr__可以生成所有字段的打印值,而ctype生成的数据结构则只能给出<ctypes.c_byte_Array_4 object at 0x00BAB760>这种提示,不能给出详细的值,这在调试的时候很不方便,所以写了一段代码打印这些值。下面是代码:
from ctypes import *
class myStructure(Structure):
def __str__(self):
'''
l = [ '%s=%s' % (k, getattr(self, k)) for k,v in self._fields_]
return '%s(%s)' % (self.__class__.__name__, ', '.join(l))
'''
s=[]
for k,v in self._fields_:
if type(v)==type(c_int) or type(v)==type(Structure): #SimpleType
s.append("%s=%s"%(k,getattr(self,k)))
elif type(v)==type(Array):
s.append('%s=%s'%(k,'['+','.join(["%s" % getattr(self,k)[i] for i in range(v._length_)])+']'))
else:
pass
return '%s(%s)'%(self.__class__.__name__,','.join([i for i in s]))
class point(myStructure):
_fields_=[('x',c_int),('y',c_int)]
class rect(myStructure):
_fields_=[('p1',point),('p2',point)]
class rectlist(myStructure):
_fields_=[('num',c_int),('list',point*3)]
p1=point(1,1)
print p1
p2=point(2,2)
p3=point(3,3)
r=rect(p1,p2)
print r
rl=rectlist(3,(p1,p2,p3))
print rl
输出为:
point(x=1,y=1)
rect(p1=point(x=1,y=1),p2=point(x=2,y=2))
rectlist(num=3,list=[point(x=1,y=1),point(x=2,y=2),point(x=3,y=3)])
- 打印python的ctype定义的结构中的数据
- Python 2.7.6的bug:ctype = ctype.encode(default_encoding) # omit in 3.x!
- CType函数的使用
- PHP的ctype函数
- 强大的ctype
- ctype库函数的使用
- Ctype函数的认识
- python利用h5py模块读取matlab中的mat文件并打印矩阵的数据类型,尺寸,数据
- python中的模块,包,库的定义
- 关于结构体运用中的一些细节问题(输入的数据与打印出来的对不齐)
- 结构体的打印
- 数据集的结构定义与实例的关系(转)
- c写个linux的小程序,从bin文件中读取数据,打印结构体中的成员变量。
- DirectCast和CType的区别
- ctype.h里的函数
- ctype.h里的函数
- php的ctype函数函数
- ctype.h里的函数
- .net 委托和事件
- wo
- 19本书,选5本,任两本不邻-粗解
- 猫捉老鼠
- 少林功夫
- 打印python的ctype定义的结构中的数据
- 部分shell命令详解
- MFC下多线程UpdateData的使用方法
- javascript函数arguments对象
- 学了8年JAVA为何还是个门外汉
- 各品牌数码相机的特点
- ni
- 今天开始让自己过得充实起来
- 临近大学毕业有感