Generating data types automatically

Steven Bethard steven.bethard at gmail.com
Sat Mar 12 13:26:05 EST 2005


Torsten Bronger wrote:
> Okay this works:
> 
> def generate_type_dublett(visa_type, ctypes_type):
>     return visa_type + "=" + ctypes_type + ";" + \
> 	"ViP" + visa_type[2:] + "=POINTER(" + visa_type + ")"
> 
> def generate_type_triplett(visa_type, ctypes_type):
>     return generate_type_dublett(visa_type, ctypes_type) + ";" + \
> 	"ViA" + visa_type[2:] + "=" + "ViP" + visa_type[2:]
> 
> exec generate_type_triplett("ViUInt32",  "c_ulong"  )
> ...

Exec is nasty.  Can you create a dict and use an update to globals instead?

py> def get_types(visa_type_name, ctypes_type):
...     pointer = ctypes.POINTER(ctypes_type)
...     return {'Vi%s' % visa_type_name:ctypes_type,
...             'ViP%s' % visa_type_name:pointer,
...             'ViA%s' % visa_type_name:pointer}
...
py> globals().update(get_types("UInt32", ctypes.c_ulong))
py> ViUInt32
<class 'ctypes.c_ulong'>
py> ViPUInt32
<class 'ctypes.LP_c_ulong'>
py> ViAUInt32
<class 'ctypes.LP_c_ulong'>

STeVe



More information about the Python-list mailing list