[Tutor] How do use c/c++ methods in Python?
Function Test(Struct A *a,int b). I want to use this function in Python,What should be the firt parameter? _ Dont just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do use c/c++ methods in Python?
> Subject: [Tutor] How do use c/c++ methods in Python? > > Function Test(Struct A *a,int b). > I want to use this function in Python,What > should be the firt parameter? I'm not sure what you are asking here. To use a C++ function/method from Python you will need to write a wrapper around the C++ code, probably using SWIG or some similar tool. Is this what you are trying to do? And if so are you asking how to map the C struct to a Python type within the wrapper? Or are you trying to call the function directly? That won't be possible, although if its a Windows function the ctypes library may help, and may provide guidance on how to translate the struct. Can you clarify what you are trying to achieve? Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "Lock"ing threads
Hans Dushanthakumar wrote: > Hi, >In a multithreaded program, how do I ensure that a block of code in a > thread is always executed fully before passing control to another > thread. Does "Lock" serve this purpose? No, not exactly. It is used to prevent threading errors but not in the way you think. Acquiring a Lock, by itself, doesn't prevent another thread from running. It just prevents another thread from acquiring *the same* lock. So to prevent two bits of code from running "at the same time" you can use a *single* Lock. Change your code to use a global Lock or pass the Lock to the class constructors. You will also have to handle waiting on a full Queue; if you just use a shared lock you will deadlock if you put_num() on a full Queue. (put_num.run() will acquire the lock, then it will block waiting for room in the Queue. get_num.run() will not be able to acquire the lock so the Queue will never empty.) Take a look at Queue.py to see one way to handle this. Note that Queue is already thread-safe for insertion and removal. If you really need to read the size as you do in your code then you need another Lock but the basic Queue does not need this. You might be interested in "The Little Book of Semaphores" which is an introduction to threading using Python. http://greenteapress.com/semaphores/ Kent > > The foll program is a dummy one, with 2 threads. One put a number onto a > queue (of max size 1) and the other thread reads from the que. > > However, on running this program (Win XP, NOT via IDLE - it hangs when I > run it thru IDLE) the output that I see on screen indicates that the > block of code within the lock aquire and release was not run completely > before the other thread started running. Note that the print messages > from the 2 threads seem to be interspersed together: > > > import threading > import Queue > > class put_num(threading.Thread): > stop_thread = 0 > > def __init__(self, num, que): > threading.Thread.__init__(self) > self.que = que > self.num = num > self.lock = threading.Lock() > > def run(self): > global stop_thread > for k in range (20): > self.lock.acquire() > print "put_num: ", self.num > self.que.put(str(self.num)) > print "put_num: Que size = ", self.que.qsize() > self.num = self.num + 1 > self.lock.release() > > class get_num(threading.Thread): > stop_thread = 0 > > def __init__(self, que): > threading.Thread.__init__(self) > self.que = que > self.lock = threading.Lock() > > def run(self): > global stop_thread > for k in range (20): > self.lock.acquire() > mynum = self.que.get() > print "get_num: ", mynum > print "get_num: Que size = ", self.que.qsize() > self.lock.release() > > my_que = Queue.Queue(1) > > put_num_thread = put_num(742, my_que) > get_num_thread = get_num(my_que) > > print "Starting threads" > put_num_thread.start() > get_num_thread.start() > > print "Waiting for threads to finish" > put_num_thread.join() > get_num_thread.join() > > print "Closing down" > raw_input("\n\nPress enter to Quit: ") > > > > > This is the out put of the above program: > > > > Starting threads > put_num: 742 > Waiting for threads to finish > put_num: Que size = 1 > get_num: 742 > get_num: Que size = 0 > put_num: 743 > put_num: Que size = 1 > get_num: 743 > get_num: Que size = 0 > put_num: 744 > put_num: Que size = 1 > get_num: 744 > get_num: Que size = 0 > put_num: 745 > put_num: Que size = 1 > get_num: 745 > get_num: Que size = 0 > put_num: 746 > put_num: Que size = 1 > get_num: 746 > get_num: Que size = 0 > put_num: 747 > put_num: Que size = get_num: 747 > get_num: Que size = 0 > 0 > put_num: 748 > put_num: Que size = 1 > get_num: 748 > get_num: Que size = 0 > put_num: 749 > put_num: Que size = get_num: 749 > get_num: Que size = 0 > 0 > put_num: 750 > put_num: Que size = 1 > get_num: 750 > get_num: Que size = 0 > put_num: 751 > put_num: Que size = 1 > get_num: 751 > get_num: Que size = 0 > put_num: 752 > put_num: Que size = get_num: 752 > get_num: Que size = 0 > 0 > put_num: 753 > put_num: Que size = 1 > get_num: 753 > get_num: Que size = 0 > put_num: 754 > put_num: Que size = 1 > get_num: 754 > get_num: Que size = 0 > put_num: 755 > put_num: Que size = get_num: 755 > get_num: Que size = 0 > 0 > put_num: 756 > put_num: Que size = get_num: 756 > get_num: Que size = 0 > 0 > put_num: 757 > put_num: Que size = get_num: 757 > get_num: Que size = 0 > 0 > put_num: 758 > put_num: Que size = 1 > get_num: 758 > get_num: Que size = 0 > put_num: 759 > put_num: Que size = get_num: 759 > get_num: Que size = 0 > 0 > put_num: 760 > put_num: Que size = 1 > get_num: 760 > get_num: Que size = 0 > put_num: 761 > put_num: Que siz
[Tutor] hi
hello i am new to python very new as in my first time ever using it and no experience in programming at all and i woul dliek some help to understand how th eprogram works and how to use pythin to thelimits of its capabilitys and i woul dlike some help my name is bobby and i am serious about learning python so any help would be much apreciated thank u __Do You Yahoo!?Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing a List from Module
The problem has been solved. It turned out that I made a newbie mistake that had nothing to do with importing lists. I have a function, sma, which calculates the moving average for a list of prices. I passed the "close" (subsequently changed to "cloze") list to the function as an argument. There is a "for" loop in the function that appends close to a new list.I had written this as: p.append(close) when it should have been p.append(close[i]) This mistake caused the function to append the entire "close" list to "p" instead of just "close[i]" each time through the loop which was more than 4000 times.. This mistake caused a print statement to fill the output screen with numbers. It was difficult to determine that my problem wasn't importing "close", but in how I was using it. Thanks to all who offered suggestions. While my use of "open" and "close" as list names apparently didn't cause any problems, it's bad form and I've changed those two names. Tom ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New Tutorial topic
On Sun, 28 Aug 2005, Alan G wrote: > I've just uploaded two new files to my tutorial the second is the > first of these and introduces databases, SQL and using the Python DBI > interface. Alan, thanks for this. I was just casting around for a tutorial on SQLite, and its use in Python. I've just briefly looked at your tutorial, but it looks like it's going to be invaluable. I'm also finding http://souptonuts.sourceforge.net/readme_sqlite_tutorial.html useful, but since I've never played with SQL at all, I expect your tutorial will be the best intro for me. Thanks. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] hi
On Sat, 27 Aug 2005, bobby arocha wrote: > hello i am new to python very new as in my first time ever using it and > no experience in programming at all and i woul dliek some help to > understand how th eprogram works and how to use pythin to thelimits of > its capabilitys and i woul dlike some help my name is bobby and i am > serious about learning python so any help would be much apreciated Hi Bobby, Welcome aboard! You may want to take a look at: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers and: http://wiki.python.org/moin/BeginnersGuide which should help you get started: there are a lot of good resources, like tutorials, that you can grab for free. If you have questions as you're going through a tutorial, feel free to ask here on Tutor. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] hi
Hi Bobby, > hello i am new to python very new as in my first time ever > using > it and no experience in programming at all In that case go to the Non Programmers web page on the Python site and follow one of the non programmers tutorials there, possibly mine! :-) http://wiki.python.org/moin/BeginnersGuide/NonProgrammers > help to understand how th eprogram works and The above tutorials will do that. If you get stuck just ask for more help here. Be sure to tell us which tutor you are using, what the problem is and any error messages you get, no matter how cryptic they may seem to you. > how to use pythin to thelimits of its capabilitys I'm not sure anyone has really got that far yet! ;-) -- Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How do use c/c++ methods in Python?
Sorry for puzzling you! I want to know how to map the C struct to a Python type within the wrapper or other methods? From: "Alan G" <[EMAIL PROTECTED]> To: "terence zhu" <[EMAIL PROTECTED]>, Subject: Re: [Tutor] How do use c/c++ methods in Python? Date: Mon, 29 Aug 2005 11:24:37 +0100 Subject: [Tutor] How do use c/c++ methods in Python? Function Test(Struct A *a,int b). I want to use this function in Python,What should be the firt parameter? I'm not sure what you are asking here. To use a C++ function/method from Python you will need to write a wrapper around the C++ code, probably using SWIG or some similar tool. Is this what you are trying to do? And if so are you asking how to map the C struct to a Python type within the wrapper? Or are you trying to call the function directly? That won't be possible, although if its a Windows function the ctypes library may help, and may provide guidance on how to translate the struct. Can you clarify what you are trying to achieve? Alan G _ Dont just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Killing a thread from main - was RE: "Lock"ing threads
Thanks Kent How do I send a signal from the main thread to stop execution of a child thread? I tried the foll:, but got an error: Other than by this method, is there any other mechanism to stop a thread? import threading import time class shownum(threading.Thread): def __init__(self, start_num): threading.Thread.__init__(self) self.num = start_num self.stop = 0 def run(self): for i in range(12): time.sleep(1) print "shownum: ", self.num self.num = self.num + 1 if self.stop == 1: break def stop(self): self.stop = 1 def chng(self): self.num = 1 incr_num_thread = shownum1(201) incr_num_thread.start() time.sleep(3) incr_num_thread.chng() time.sleep(3) incr_num_thread.stop() Output: shownum: 201 shownum: 202 shownum: 1 shownum: 2 shownum: 3 Traceback (most recent call last): File "H:\Docs\PyScripts\test_threads.py", line 31, in ? incr_num_thread.stop() TypeError: 'int' object is not callable shownum: 4 shownum: 5 shownum: 6 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Killing a thread from main - was RE: "Lock"ing threads
Oops - the error was probably due to using 'stop' as a method name. :) -Original Message- From: Hans Dushanthakumar Sent: Tuesday, 30 August 2005 2:18 p.m. To: tutor@python.org Subject: Killing a thread from main - was RE: [Tutor] "Lock"ing threads Thanks Kent How do I send a signal from the main thread to stop execution of a child thread? I tried the foll:, but got an error: Other than by this method, is there any other mechanism to stop a thread? import threading import time class shownum(threading.Thread): def __init__(self, start_num): threading.Thread.__init__(self) self.num = start_num self.stop = 0 def run(self): for i in range(12): time.sleep(1) print "shownum: ", self.num self.num = self.num + 1 if self.stop == 1: break def stop(self): self.stop = 1 def chng(self): self.num = 1 incr_num_thread = shownum1(201) incr_num_thread.start() time.sleep(3) incr_num_thread.chng() time.sleep(3) incr_num_thread.stop() Output: shownum: 201 shownum: 202 shownum: 1 shownum: 2 shownum: 3 Traceback (most recent call last): File "H:\Docs\PyScripts\test_threads.py", line 31, in ? incr_num_thread.stop() TypeError: 'int' object is not callable shownum: 4 shownum: 5 shownum: 6 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Killing a thread from main - was RE: "Lock"ing threads
Hans Dushanthakumar wrote: > Thanks Kent > > How do I send a signal from the main thread to stop execution of a child > thread? You have the right idea - set some kind of flag that the thread checks - but you reused the name 'stop' so when you execute incr_num_thread.stop() you are retrieving the 'stop' attribute of incr_num_thread (which is the integer 0) rather than the 'stop' attribute of class shownum (which is the method you want). Then you try to call the integer (by the ()) and get an error. This recipe has a more sophisticated version of the same idea: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65448 Kent > > I tried the foll:, but got an error: > Other than by this method, is there any other mechanism to stop a > thread? > > import threading > import time > > class shownum(threading.Thread): > > def __init__(self, start_num): > threading.Thread.__init__(self) > self.num = start_num > self.stop = 0 > > def run(self): > for i in range(12): > time.sleep(1) > print "shownum: ", self.num > self.num = self.num + 1 > if self.stop == 1: > break > > def stop(self): > self.stop = 1 > > def chng(self): > self.num = 1 > > incr_num_thread = shownum1(201) > incr_num_thread.start() > > time.sleep(3) > incr_num_thread.chng() > time.sleep(3) > incr_num_thread.stop() > > > Output: > > shownum: 201 > shownum: 202 > shownum: 1 > shownum: 2 > shownum: 3 > Traceback (most recent call last): > File "H:\Docs\PyScripts\test_threads.py", line 31, in ? > incr_num_thread.stop() > TypeError: 'int' object is not callable > shownum: 4 > shownum: 5 > shownum: 6 > > ___ > 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] MS Access......
I want to know what modules are available to connect and talk with MSAccess databases... Thanks __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor