ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ctypes-users
ctypes-users
Re: [ctypes-users] Re: Converting between ctypes data and Python data
by Thomas Heller other posts by this author
Feb 15 2006 3:30AM messages near this date
Re: [ctypes-users] Re: Converting between ctypes data and Python data | Re: [ctypes-users] Re: Converting between ctypes data and Python data
Carsten Wartmann wrote:
>  Thomas Heller schrieb:
> > Carsten Wartmann wrote:
> >> Hello,
> >>
> >> after yours help on this list I got my application working, in fact it
> >> is working like a charm.
> >>
> >> However, as I looked today on my "code" I noticed that I convert the
> >> ctypes structures (given back from a dll function) into python data with
> >> a simple for...in...: construct.
> >>
> >> I guess there must be a better way?
> >>
> >> I am converting this:
> >>
> >> SPINKIT_HANDLE = c_void_p
> >> class SPINKIT_DATA(Structure):
> >>     _fields_ = [("Device", SPINKIT_HANDLE),
> >>                 ("Spins", c_int * 6),
> >>                 ("Buttons", BOOL * 7)]
> >> SPINKIT_DATA_SIZE = sizeof(SPINKIT_DATA)
> >>
> >> into this:
> >>
> >> class SpinkitPyData:
> >>     def __init__(self):
> >>         self.Handle = -1
> >>         self.Spins = [0,0,0,0,0,0]
> >>         self.Buttons = [False,False,False,False,False,False,False]
> >
> > Sorry, but I'm not sure what you're asking.
>  
>  Hrm, that happens when you write in a hurry...
>  
>  The funtion I am using is returning SPINKIT_DATA(Structure): and I find 
>  that this is not easy to use in python and so I "convert" it into 
>  SpinkitPyData and return() this to have easier acces to the results.
>  
>  def SpinKitReadNonBlocking(self,data):
>          "Read Immediate"
>          datac=SPINKIT_DATA()
>          if windll.spinkit.SpinKitReadImmediate(self.Handle,pointer(datac)):
>              data.Device=self.Handle
>              i=0
>              for spin in datac.Spins:
>                  data.Spins[i]=spin
>                  i=i+1
>              i=0
>              for button in datac.Buttons:
>                  data.Buttons[i]=button
>                  i=i+1
>              return(True)
>          else:
>              return(False)
>  
>  
>  What I am asking is, if there is a more elegant way to convert the 
>  SPINKIT_DATA(Structure) into something more pythonesque like my 
>  SpinkitPyData which I think is a nice representation and easy to access 
>  inside python.
>  
>  Hope this makes it more clear?
Yes it does.

The SPINKIT_DATA instance should behave the same way as the SpinKitPyData instance that you 
create,
if you abstract from some details.  Can't you use it directly?  And, imo, a more pythonic wa
y to
write your SpinKitReadNonBlocking method would be this (it returns the SPINKIT_DATA instance
 or None):

def SpinkKitReadNonBlocking(self):
    data = SPINKIT_DATA()
    if windll.spinkit.SpinKitReadImmediate(self.Handle, byref(data)):
        return data
    return None


Thomas



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
ctypes-users mailing list
ctypes-users@[...].net
https://lists.sourceforge.net/lists/listinfo/ctypes-users
Thread:
Carsten Wartmann
Thomas Heller
Carsten Wartmann
Thomas Heller
Carsten Wartmann

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved