Generating data types automatically
Thomas Heller
theller at python.net
Mon Mar 14 03:11:54 EST 2005
Torsten Bronger <bronger at physik.rwth-aachen.de> writes:
> Hallöchen!
>
> I have to generate a lot of data types (for ctypes by the way). An
> example is
>
> ViUInt32 = u_long
> ViPUInt32 = POINTER(ViUInt32)
> ViAUInt32 = ViPUInt32
>
> Therefore, I defined functions that should make my life easier:
>
> def generate_type_dublett(visa_type, ctypes_type):
> visa_type_name = visa_type.__name__
> exec visa_type_name + "=" + ctypes_type.__name__
> exec "ViP" + visa_type_name[2:] + "=POINTER(" + visa_type_name + ")"
>
> def generate_type_triplett(visa_type, ctypes_type):
> generate_type_dublett(visa_type, ctypes_type)
> visa_type_name = visa_type.__name__
> exec "ViA" + visa_type_name[2:] + "=" + "ViP" + visa_type_name[2:]
>
> generate_type_triplett(ViUInt32, c_ulong)
>
>
> However, this doesn't work, probably because the defined type exist
> only locally within the function.
Others have answered your question already, but I would like to note one
thing: The POINTER() function caches its results, so you could (and
should, imo) write 'POINTER(ViUInt32)' instead everywhere in your code.
Calling POINTER(ViUInt32) *allways* returns the same type.
Thomas
More information about the Python-list
mailing list