Re: [Tutor] How to make a python binding for a c library?
"tuyun" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] Hi I have a library written in C, I want to make a python binding for it. But I dont know how to get started. Is there any guide or document? Is "Python/C API Reference Manual" the right doc I need to study first? Check out the ctypes library. It comes with Python 2.5, but can be installed for older versions. Here's an example that calls the Win32 function Beep() from kernel32.dll: import ctypes ctypes.windll.kernel32.Beep(440,1000) This is a simplistic example that works for simple DLL functions that take integers and return integers. If your APIs deal with pointers and read/write buffers it is somewhat more complicated, but still easier to write your bindings in Python than using the Python C APIs. -Mark ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] a replace function using re module
As more practice with the re module I thought I'd make this function, replace2(): def replace2(astr, regex, repl, case=0, count=0): """ Replace all regex matches in a string with repl. case=0 is case insensitive. count=0 means make all possible replacements. count=n (n > 0) means make the first n possible replacements. """ import re if case == 0: p = re.compile(regex, re.IGNORECASE) else: p = re.compile(regex) return p.sub(repl, astr, count) In shell: >>> astr = "Now is the time 4 all good men." >>> regex = r"[ohen0-9]" >>> repl = "X" >>> replace2(astr, regex, repl, case=0, count=0) 'XXw is tXX timX X all gXXd mXX.' >>> replace2(astr, regex, repl) 'XXw is tXX timX X all gXXd mXX.' >>> replace2(astr, regex, repl, case=1) 'NXw is tXX timX X all gXXd mXX.' >>> replace2(astr, regex, repl, count=4) 'XXw is tXX time 4 all good men.' >>> (I've also pasted the above at < http://py77.python.pastebin.com/f18e323db>) There's a string method, replace(); I can't use that name for my function, so unimaginatively I've gone with replace2(). Got a better name? But more importantly, how can I improve replace()? Thanks, Dick Moores ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to make a python binding for a c library?
Hi I have a library written in C, I want to make a python binding for it. But I dont know how to get started. Is there any guide or document? Is "Python/C API Reference Manual" the right doc I need to study first? Thanks in advance Twomol _ 用手机MSN聊天写邮件看空间,无限沟通,分享精彩! http://mobile.msn.com.cn/___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] pyUSB linux + unknown device
I'm trying to make an interface between my weather station and my linux server however the only output the weather station has is USB. Further, there are only windows drivers for this Honeywell weather station. Thus, I'm trying to use Python and pyUSB to interface. I THINK i've gotten pretty close. It appears the device is identifying as an Unknown Product by Tenx Technology, Inc. on USB 1.10. I can gleen all the information about the device that you can get from 'configurations' but I'm stuck on the next step. . Reading. Now, I'm betting the device simply is dumping packets just like the serial device would on all other weather stations I've encountered. However, I'm not SURE. I've pasted my code below but I'm getting this error: "usb.USBError: error submitting URB: Device or resource busy" Anyone have any thoughts or suggestions on where I should go from here. . This is my first shot at PyUSB if you hadn't noticed. import re, sys, usb, string class DeviceDescriptor: def __init__(self): 1 def getDevice(self): buses = usb.busses() for bus in buses: for device in bus.devices: if hex(device.idVendor)[2:] == '1130': print 'Device Found: %s:%s'%(hex(device.idVendor),hex(device.idProduct)) return device print 'FAILED TO FIND THE WEATHER STATION!' return None class main: def __init__(self): self.device_descriptor = DeviceDescriptor() self.device = self.device_descriptor.getDevice() self.handle = self.device.open() def close(self): self.handle.releaseInterface() def getDataPacket(self, bytesToGet): #self.handle.bulkWrite(1,"v"+(7)*"Z",1000) return self.handle.interruptRead(0x81,3,1000) if __name__ == "__main__": data = main() print data.getDataPacket(10) -- View this message in context: http://www.nabble.com/pyUSB-linux-%2B-unknown-device-tp17198596p17198596.html Sent from the Python - tutor mailing list archive at Nabble.com. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Python for accurate calculations
"Gloom Demon" <[EMAIL PROTECTED]> wrote I am working on a function which is going to calculate correlation between two given sets of numbers. Example: x=[10.0, 10.8, 11.3, 10.0, 10.1, 11.1, 11.3, 10.2, 13.5, 12.3, 14.5, 11.0, y=[0.70, 0.73, 0.75, 0.70, 0.65, 0.65, 0.70, 0.61, 0.70, 0.63, 0.70, 0.65, However Python stores these numbers like this: x [10.0, 10.801, 11.301, 10.0, 10.1, 11.1, The extra digits added by Python are adding error probability The digits aren't really added by Python they are just the nearest equivalent to your decimal numbers in binary. It will happen in any computer program that uses standard float types (even a pocket calculator!) This is one reason not to use floats for finance calculations! There are several ways around it. 1) Multiply up your numbers to integer values - in your case multiplying by 100 (or 1000) would work. - Easy with a list comprehension. 2) Use the decimal module for fixed decimals. (BCD if you like) 3) Set limits and work within +/- delta of your final values. Much depends on whether the errors are affecting the results or only the display of the results. The display can be fixed via formatted printing, the actual values will need options 1-3. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Else Clause In A Loop
"kinuthia muchane" <[EMAIL PROTECTED]> wrote This has served to confuse me more. Would someone please kindly explain how all this fits into the code below which searches (and finds!) for prime numbers... It doesn't find prime numbers very well. It only finds the primes *below* the one entered. So... def prime(): number = int(raw_input("Enter a number :")) for i in range(2,number): for j in range(2,i): if i%j == 0: break else: print "is a prime number", i prime() ...especially in the instance when number is 2 in the first for statement, for then we will have for j range(2,2)! When number is 2 it doesnm't print anything. Which as you point out is as you would expect since the first range() will be empty. For number=3 the first range has one element so it enters the second loop with an empty list so goes straight to the else clause. I'm not a sure what you are confused about? The code is not very elegant or effective but it does what you have described in your experiments. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to return an error from a CGI
Hi John, It's been a long time! I believe you may be looking for the Status Code stuff that web servers use to tell browsers that something special is happening. See Section 3.3.1.3 in: http://www.unix.com.ua/orelly/linux/cgi/ch03_03.htm for an example done in Perl; the code should be easy to translate to Python. The idea is to print out a "Status" header at the beginning, so that the web server knows that it should send out a response with that particular status code type. The status codes are also specified as constants in: http://www.python.org/doc/lib/module-httplib.html near the bottom of that documentation. Wikipedia also catalogs the status codes that you can use: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Else Clause In A Loop
On Mon, 2008-05-12 at 14:08 -0400, "Simón A. Ruiz" wrote: > I'll try my hand at this: > > The outside for loop is looking through every number up to the variable > "number". No quarrel here. > > For each of those numbers, it checks to see if any number between 2 and > i is divisible into i. If it finds anything, we know it's not a prime, > and so it breaks out of that second loop without completing it, which > means the else block isn't executed. This is where I am getting lost. When the variable 'number' is 3, it means that in that instance the inner 'for' statement will be 'for j in range(2,3)', hmmm which means that we will be dividing each element by 2 in the first 'for' statement and checking whether it is true , no? But 2%2 is zero, so, in my warped logic, the inner 'for' loop should break and the else clause will not execute! > > If it can't find anything that i is divisible by, then that inside for > loop finishes without breaking, we know that i is a prime number, and > the "else" clause is executed. This is where it gets even more interesting for me. Wont 'i' in one instance be 8, so at that particular moment there will be a 7 in the inner 'for' loop which will be divisible by one of our 'prime' numbers ie 7?! I know I am wrong but for some reason I cannot see the light! :-) > > range(2,2) is is an empty list, so the loops ends uneventfully without > doing anything and the else clause is executed. > > I'm not an expert, and this seems to me to be what's happening there. > > Simón All the same, thanks... > > kinuthia muchane wrote: > > Hi, > > > > I learnt that a loop can have an else clause. And that this clause > > executes when the loop TERMINATES. In a while loop when the condition > > becomes false, and in a for loop when a sequence is exhausted. When I > > write the following code it seems to work: > > > > for n in [1,2,3,4,5]: > > print 'we are in the loop',n > > else: > > print 'we are now EXITING the loop',n > > > > which results in: > > > > we are in the loop 1 > > we are in the loop 2 > > we are in the loop 3 > > we are in the loop 4 > > we are in the loop 5 > > we are now EXITING the loop 5 > > > > Or: > > > > n = 1 > > while n <= 5: > > print 'we are in the loop',n > > n += 1 > > else: > > print 'we are now EXITING the loop',n > > > > ...which gives: > > > > we are in the loop 1 > > we are in the loop 2 > > we are in the loop 3 > > we are in the loop 4 > > we are in the loop 5 > > we are now EXITING the loop 6 (it spills over here!) > > > > This has served to confuse me more. Would someone please kindly explain > > how all this fits into the code below which searches (and finds!) for > > prime numbers... > > > > > > def prime(): > >number = int(raw_input("Enter a number :")) > >for i in range(2,number): > > for j in range(2,i): > > if i%j == 0: > > break > > else: > > print i, "is a prime number" > > prime() > > > > ...especially in the instance when number is 2 in the first for > > statement, for then we will have for j in range(2,2)! Or what is going > > on?? > > > > Thanks, > > Kinuthia... > > > > > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Else Clause In A Loop
I'll try my hand at this: The outside for loop is looking through every number up to the variable "number". For each of those numbers, it checks to see if any number between 2 and i is divisible into i. If it finds anything, we know it's not a prime, and so it breaks out of that second loop without completing it, which means the else block isn't executed. If it can't find anything that i is divisible by, then that inside for loop finishes without breaking, we know that i is a prime number, and the "else" clause is executed. range(2,2) is is an empty list, so the loops ends uneventfully without doing anything and the else clause is executed. I'm not an expert, and this seems to me to be what's happening there. Simón kinuthia muchane wrote: Hi, I learnt that a loop can have an else clause. And that this clause executes when the loop TERMINATES. In a while loop when the condition becomes false, and in a for loop when a sequence is exhausted. When I write the following code it seems to work: for n in [1,2,3,4,5]: print 'we are in the loop',n else: print 'we are now EXITING the loop',n which results in: we are in the loop 1 we are in the loop 2 we are in the loop 3 we are in the loop 4 we are in the loop 5 we are now EXITING the loop 5 Or: n = 1 while n <= 5: print 'we are in the loop',n n += 1 else: print 'we are now EXITING the loop',n ...which gives: we are in the loop 1 we are in the loop 2 we are in the loop 3 we are in the loop 4 we are in the loop 5 we are now EXITING the loop 6 (it spills over here!) This has served to confuse me more. Would someone please kindly explain how all this fits into the code below which searches (and finds!) for prime numbers... def prime(): number = int(raw_input("Enter a number :")) for i in range(2,number): for j in range(2,i): if i%j == 0: break else: print "is a prime number", i prime() ...especially in the instance when number is 2 in the first for statement, for then we will have for j range(2,2)! Or what is going on?? Thanks, Kinuthia... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to return an error from a CGI
Classification: UNCLASSIFIED Caveat (s): FOUO All, I have been writing simple cgi scripts for a long time but have never worried about the error codes. Now I have been asked to return a specific error and I have no idea how to do this. I do not even know if I should be returning an HTTP or URL error. If I understand correctly, I am supposed to return something similar to say a 500 error. I think this is a HTTP error and I tried a few things but no luck. Thanks for the ideas. John Ertl Meteorologist FNMOC 7 Grace Hopper Ave. Monterey, CA 93943 (831) 656-5704 [EMAIL PROTECTED] Classification: UNCLASSIFIED Caveat (s): FOUO ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Python for accurate calculations
Gloom Demon wrote: Hello I am working on a function which is going to calculate correlation between two given sets of numbers. Example: x=[10.0, 10.8, 11.3, 10.0, 10.1, 11.1, 11.3, 10.2, 13.5, 12.3, 14.5, 11.0, 12.0, 11.8, 13.4, 11.4, 12.0, 15.6, 13.0, 12.1] y=[0.70, 0.73, 0.75, 0.70, 0.65, 0.65, 0.70, 0.61, 0.70, 0.63, 0.70, 0.65, 0.72, 0.69, 0.78, 0.70, 0.60, 0.85, 0.80, 0.75] However Python stores these numbers like this: >>> x [10.0, 10.801, 11.301, 10.0, 10.1, 11.1, 11.301, 10.199, 13.5, 12.301, 14.5, 11.0, 12.0, 11.801, 13.4, 11.4, 12.0, 15.6, 13.0, 12.1] >>> y [0.69996, 0.72998, 0.75, 0.69996, 0.65002, 0.65002, 0.69996, 0.60999, 0.69996, 0.63, 0.69996, 0.65002, 0.71997, 0.68995, 0.78003, 0.69996, 0.59998, 0.84998, 0.80004, 0.75] The extra digits added by Python are adding error probability to my calculations. Is there a way to overcome this? Also consider using the decimal module http://docs.python.org/lib/module-decimal.html -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Else Clause In A Loop
Hi, I learnt that a loop can have an else clause. And that this clause executes when the loop TERMINATES. In a while loop when the condition becomes false, and in a for loop when a sequence is exhausted. When I write the following code it seems to work: for n in [1,2,3,4,5]: print 'we are in the loop',n else: print 'we are now EXITING the loop',n which results in: we are in the loop 1 we are in the loop 2 we are in the loop 3 we are in the loop 4 we are in the loop 5 we are now EXITING the loop 5 Or: n = 1 while n <= 5: print 'we are in the loop',n n += 1 else: print 'we are now EXITING the loop',n ...which gives: we are in the loop 1 we are in the loop 2 we are in the loop 3 we are in the loop 4 we are in the loop 5 we are now EXITING the loop 6 (it spills over here!) This has served to confuse me more. Would someone please kindly explain how all this fits into the code below which searches (and finds!) for prime numbers... def prime(): number = int(raw_input("Enter a number :")) for i in range(2,number): for j in range(2,i): if i%j == 0: break else: print "is a prime number", i prime() ...especially in the instance when number is 2 in the first for statement, for then we will have for j range(2,2)! Or what is going on?? Thanks, Kinuthia... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using Python for accurate calculations
Gloom Demon wrote: Hello I am working on a function which is going to calculate correlation between two given sets of numbers. Example: x=[10.0, 10.8, 11.3, 10.0, 10.1, 11.1, 11.3, 10.2, 13.5, 12.3, 14.5, 11.0, 12.0, 11.8, 13.4, 11.4, 12.0, 15.6, 13.0, 12.1] y=[0.70, 0.73, 0.75, 0.70, 0.65, 0.65, 0.70, 0.61, 0.70, 0.63, 0.70, 0.65, 0.72, 0.69, 0.78, 0.70, 0.60, 0.85, 0.80, 0.75] However Python stores these numbers like this: >>> x [10.0, 10.801, 11.301, 10.0, 10.1, 11.1, 11.301, 10.199, 13.5, 12.301, 14.5, 11.0, 12.0, 11.801, 13.4, 11.4, 12.0, 15.6, 13.0, 12.1] >>> y [0.69996, 0.72998, 0.75, 0.69996, 0.65002, 0.65002, 0.69996, 0.60999, 0.69996, 0.63, 0.69996, 0.65002, 0.71997, 0.68995, 0.78003, 0.69996, 0.59998, 0.84998, 0.80004, 0.75] The extra digits added by Python are adding error probability to my calculations. Is there a way to overcome this? See http://docs.python.org/tut/node16.html. -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Buffering in file.read()
Kent Johnson tds.net> writes: > > Footnote 2.3 says, "Specifying a buffer size currently has no effect > on systems that don't have setvbuf(). The interface to specify the > buffer size is not done using a method that calls setvbuf(), because > that may dump core when called after any I/O has been performed, and > there's no reliable way to determine whether this is the case." > > So whether that works will depend on what OS the OP is using. And so, the next question is: Which OS's support setvbuf()? I'm on Ubuntu GNU/Linux. I do the following and I see: ~ [6] cd /usr/include/ /usr/include [7] grep setvbuf *.h stdio.h:/* The possibilities for the third argument to `setvbuf'. */ stdio.h:extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf, So, some versions of Linux apparently have it. And, for MS Windows, you might look here: http://msdn.microsoft.com/en-us/library/86cebhfs(VS.80).aspx So, MS Windows seems to support it. - Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Using Python for accurate calculations
Hello I am working on a function which is going to calculate correlation between two given sets of numbers. Example: x=[10.0, 10.8, 11.3, 10.0, 10.1, 11.1, 11.3, 10.2, 13.5, 12.3, 14.5, 11.0, 12.0, 11.8, 13.4, 11.4, 12.0, 15.6, 13.0, 12.1] y=[0.70, 0.73, 0.75, 0.70, 0.65, 0.65, 0.70, 0.61, 0.70, 0.63, 0.70, 0.65, 0.72, 0.69, 0.78, 0.70, 0.60, 0.85, 0.80, 0.75] However Python stores these numbers like this: >>> x [10.0, 10.801, 11.301, 10.0, 10.1, 11.1, 11.301, 10.199, 13.5, 12.301, 14.5, 11.0, 12.0, 11.801, 13.4, 11.4, 12.0, 15.6, 13.0, 12.1] >>> y [0.69996, 0.72998, 0.75, 0.69996, 0.65002, 0.65002, 0.69996, 0.60999, 0.69996, 0.63, 0.69996, 0.65002, 0.71997, 0.68995, 0.78003, 0.69996, 0.59998, 0.84998, 0.80004, 0.75] The extra digits added by Python are adding error probability to my calculations. Is there a way to overcome this? Thank You :-) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor