Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print all python Structure field values

I've trying to write a small program which could read .h file, then generate ctypes.Structure classes from struct in .h file. Then i'm reading binary file into the Structures. And then i need to print out all of the Structure field values (including arrays and subStructures). How can i do it?

like image 445
qmor Avatar asked Dec 19 '22 20:12

qmor


2 Answers

This is basic function that print all fields and sub-structures.

def f(obj):
    for k,v in obj.__dict__.items():
        print k ,v
        if hasattr(v,'__dict__'):
            f(v)   

Of course, you can add conditions to the function, to filter out unneeded data etc.

like image 61
mclafee Avatar answered Dec 30 '22 10:12

mclafee


There is already a tool that does this. (No need to re-invent the wheel).

See: https://github.com/davidjamesca/ctypesgen

like image 41
James Mills Avatar answered Dec 30 '22 11:12

James Mills