473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generating data types automatically

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.

What is a better (and working) method for this task?

Thank you!

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jul 18 '05 #1
5 1703
Hallöchen!

Torsten Bronger <br*****@physik.rwth-aachen.de> writes:
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:

[...]

However, this doesn't work, probably because the defined type
exist only locally within the function.


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" )
....
Not very beautiful, though.

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jul 18 '05 #2
Torsten Bronger wrote:
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 + ")"


You shouldn't need to use exec for this, and it is best to avoid its use.

If you MUST do things this way, then you can add items to the globals()
dictionary. See the library reference for more details. It's probably
best to avoid globals() as well, although it's not as bad as exec/eval.

Personally, I think it would be better to define your types like this:

ViUInt32, ViPUInt32, ViAUInt32 = generate_type_triplet(u_long)

That way you will easily be able to find the initial definition of
the object by searching and replacing. You'll also have to jump through
fewer weird hoops to get the result you want.
--
Michael Hoffman
Jul 18 '05 #3
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
Jul 18 '05 #4
Torsten Bronger <br*****@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
Jul 18 '05 #5
Hallöchen!

Thomas Heller <th*****@python.net> writes:
Torsten Bronger <br*****@physik.rwth-aachen.de> writes:
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

[...]


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.


Actually I don't think that we will use them (ViP...) at all, since
we'll make use of "byref" whereever they occur in an args list.

But they are in the spec that we try to mimic, and as long as I
don't know for sure that nobody uses them, they don't hurt.

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: J. Campbell | last post by:
I have a feeling that I'm doing things all ass-backwards (again ;-), and would like some advice. What I want to do is: put some data to memory and then access that memory space as an array of...
13
by: Shailesh Humbad | last post by:
I wrote a short page as a quick reference to c++ integer data types. Any feedback welcome: http://www.somacon.com/blog/page11.php
11
by: theshowmecanuck | last post by:
As a matter of academic interest only, is there a way to programmatically list the 'c' data types? I am not looking for detail, just if it is possible, and what function could be used to...
4
by: BrianDH | last post by:
This is new to me. I am trying to call a SP (MS SQL) and pass different data types. I know in SQL you handel these data types diffent in your string Public Shared Function GetGridDataSet(ByVal...
8
by: ramu | last post by:
Hi, I want to call a vc++ function from a c program on linux. So when I do this dosen't the VC++ datatypes differ with c datatypes. Because we don't have some vc++ data types in c. I have to...
11
by: mesut demir | last post by:
Hi All, When I create fields (in files) I need assign a data type like char, varchar, money etc. I have some questions about the data types when you create fields in a file. What is the...
15
by: Madhur | last post by:
Hi All, I would like you help me in creating an array of data types. I am interested in look at the the data type which looks like this Array...
7
by: Maximus Decimus | last post by:
HI all, I am using python v2.5 and I am an amateur working on python. I am extending python for my research work and would like some help and guidance w.r.t this matter from you experienced...
1
by: ccookson | last post by:
I have a form that prompts the user to select an excel file to be linked into the database. However, Access automatically assigns the data types from text to numbers. How can I change the data...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.