[Tutor] New Tutorial topic
Hi gang, I've just uploaded two new files to my tutorial. The first explains a new section in the tutor aimed at highlighting practical applications of the language, the second is the first of these and introduces databases, SQL and using the Python DBI interface. Enjoy, and as ever feedback is welcomed. Note that neither of these appears in the zip bundle as 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] Importingf a List from Module
Tom Strickland wrote: >>Here are the modules in question: >> >> > This is the main.py module > > #!/usr/bin/python2.4 > import enterData > import movAvg > smavg=[] > xy=enterData.close > print xy[0] > smavg = movAvg.sma(20,enterData.close) > emavg=[] > emavg=movAvg.ema(20,enterData.close) > import stoResults > stoResults.store(enterData.date, enterData.close,smavg,emavg) > print "Finished" > > > ##This is the enterData.py module > ##!/usr/bin/python2.4 > input = open('/home/tom/Python/Input/SPY2.csv', 'r') > s = input > date =[] > open = [] > close = [] > hi = [] > lo = [] > vol = [] > for s in input: > s = s[:-2] > y =[] > y = s.split(',') > date.append(y[0]) > open.append(float(y[1])) > hi.append(float(y[2])) > lo.append(float(y[3])) > close.append(float(y[4])) > vol.append(float(y[5])) > input.close() > for i in range(5): > print close[i] > print 'enterData.py' > > > > > *** > > > >>-- >> >>Message: 7 >>Date: Sat, 27 Aug 2005 22:27:23 -0500 >>From: Tom Strickland <[EMAIL PROTECTED]> >>Subject: [Tutor] Importing a List from Module >>To: tutor@python.org >>Message-ID: <[EMAIL PROTECTED]> >>Content-Type: text/plain; charset=ISO-8859-1; format=flowed >> >>I have a module called "enterData" which generates a list, "close" from >>a data file. "close" is a list of floats. When I put a print statement >>in that module it will print out an individual member of the list. For >>example, >> >>print close[0] >> >> >>prints the first member of the list. >> >>In my "main" module I import "enterData" and try to read the first >>element of "close" as follows: >> >>import enterData >>xy=enterData.close >>print xy[0] >> >> >>When I do this it prints out the entire "close" list, not just the first >>term. >> >>What's my mistake and how do I correct it? >> >>Thank you! >> >> >>-- >> >>Message: 8 >>Date: Sun, 28 Aug 2005 00:15:15 -0400 >>From: Kent Johnson <[EMAIL PROTECTED]> >>Subject: Re: [Tutor] Importing a List from Module >>Cc: tutor@python.org >>Message-ID: <[EMAIL PROTECTED]> >>Content-Type: text/plain; charset=ISO-8859-1; format=flowed >> >>Tom Strickland wrote: >> >> >>>I have a module called "enterData" which generates a list, "close" from >>>a data file. "close" is a list of floats. When I put a print statement >>>in that module it will print out an individual member of the list. For >>>example, >>> >>>print close[0] >>> >>> >>>prints the first member of the list. >>> >>>In my "main" module I import "enterData" and try to read the first >>>element of "close" as follows: >>> >>>import enterData >>>xy=enterData.close >>>print xy[0] >>> >>> >>>When I do this it prints out the entire "close" list, not just the first >>>term. >>> >>>What's my mistake and how do I correct it? >>> >>> >> >>What you have shown here looks fine to me. Can you show some more of >>enterData? >> >>Kent >> >> >> >>-- >> >>Message: 9 >>Date: Sat, 27 Aug 2005 21:25:36 -0700 >>From: Byron <[EMAIL PROTECTED]> >>Subject: Re: [Tutor] Importing a List from Module >>To: Tom Strickland <[EMAIL PROTECTED]>, tutor@python.org >>Message-ID: <[EMAIL PROTECTED]> >>Content-Type: text/plain; charset=ISO-8859-1; format=flowed >> >>Tom Strickland wrote: >> >> >>>In my "main" module I import "enterData" and try to read the first >>>element of "close" as follows: >>> >>>import enterData >>>xy=enterData.close >>>print xy[0] >>> >>> >>>When I do this it prints out the entire "close" list, not just the first >>>term. >>> >>> >> >> >>Hi Tom, >> >>I would create a function in your module that returns the list. Here's >>a quick, simplified example: >> >>def returnList(): >> newList = [] >> newList += [123.45] >> newList += [529.59] >> newList += [259.92] >> return newList >> >>aList = returnList() >>print aList >> >> >>Note the return statement... This enables assignment, as you have done >>in "xy=enterData.returnList()" >> >>Hope this helps, >> >>Byron >>--- >> >> >> >>-- >> >>___ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor >> >> >>End of Tutor Digest, Vol 18, Issue 106 >>** >> >> >> >> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing a List from Module
Byron, I'm confused (as usual). In "def returnList():" that you write below, should the items in the newList list be close[i] and looped to fill "newList" with the contents of "close"? If so, how is "returnLost" different from "close"? Thanks! Tom Byron wrote: > Tom Strickland wrote: > >> In my "main" module I import "enterData" and try to read the first >> element of "close" as follows: >> >> import enterData >> xy=enterData.close >> print xy[0] >> >> When I do this it prints out the entire "close" list, not just the >> first term. > > > > Hi Tom, > > I would create a function in your module that returns the list. > Here's a quick, simplified example: > > def returnList(): > newList = [] > newList += [123.45] > newList += [529.59] > newList += [259.92] > return newList > > aList = returnList() > print aList > > > Note the return statement... This enables assignment, as you have > done in "xy=enterData.returnList()" > > Hope this helps, > > Byron > --- > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importing a List from Module
Tom Strickland wrote: > Eric, > > No, "xy" isn't used anywhere else in the program. It's just a dummy > variable I used to print out "enterData.close". I could easily have > left it out. > > Tom > > > Eric Walker wrote: > >>I am a newbie but do you have anything else named xy >>in your main module. >> >>Eric.. >> >>--- Tom Strickland <[EMAIL PROTECTED]> wrote: >> >> >> >>>I have a module called "enterData" which generates a >>>list, "close" from >>>a data file. "close" is a list of floats. When I put >>>a print statement >>>in that module it will print out an individual >>>member of the list. For >>>example, >>> >>>print close[0] >>> >>> >>>prints the first member of the list. >>> >>>In my "main" module I import "enterData" and try to >>>read the first >>>element of "close" as follows: >>> >>>import enterData >>>xy=enterData.close >>>print xy[0] >>> >>> >>>When I do this it prints out the entire "close" >>>list, not just the first >>>term. >>> >>>What's my mistake and how do I correct it? >>> >>>Thank you! >>>___ >>>Tutor maillist - Tutor@python.org >>>http://mail.python.org/mailman/listinfo/tutor >>> >>> >>> >> >> >> >> >>__ >>Yahoo! Mail for Mobile >>Take Yahoo! Mail with you! Check email on your mobile phone. >>http://mobile.yahoo.com/learn/mail >> >> >> >> > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importingf a List from Module
On 8/28/05, Tom Strickland <[EMAIL PROTECTED]> wrote: Tom Strickland wrote:>>Here are the modules in question:>> > ##This is the enterData.py module> ##!/usr/bin/python2.4> input = open('/home/tom/Python/Input/SPY2.csv', 'r')> s = input> date =[]> open = []> close = []> hi = [] > lo = []> vol = []> for s in input:> s = s[:-2]> y =[]> y = s.split(',')> date.append(y[0])> open.append(float(y[1]))> hi.append(float(y[2])) > lo.append(float(y[3]))> close.append(float(y[4]))> vol.append(float(y[5]))> input.close()> for i in range(5):> print close[i]> print 'enterData.py' I don't know if this would affect anything, but one of your lists is named "open" which isn't a good idea because that overrides the builtin "open()" (the one you use to open a new file). -- Email: singingxduck AT gmail DOT comAIM: singingxduckProgramming Python for the fun of it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importingf a List from Module
I'm still stumped by this one. Here is a stripped-down version of your code that works fine for me: # test.py #!/usr/bin/python2.4 import enterData xy=enterData.close print xy[0] print "Finished" ##This is the enterData.py module ##!/usr/bin/python2.4 input = open('SPY2.csv', 'r') close = [] for s in input: close.append(float(s)) input.close() for i in range(5): print close[i] print 'enterData.py' # SPY2.csv 1.2 3.4 1.2 4.5 6.7 The output is 1.2 3.4 1.2 4.5 6.7 enterData.py 1.2 Finished Do you get the same results? What is in your SPY2.csv? What output do you get? Kent Tom Strickland wrote: > Tom Strickland wrote: > > >>>Here are the modules in question: >>> >>> >> >>This is the main.py module >> >>#!/usr/bin/python2.4 >>import enterData >>import movAvg >>smavg=[] >>xy=enterData.close >>print xy[0] >>smavg = movAvg.sma(20,enterData.close) >>emavg=[] >>emavg=movAvg.ema(20,enterData.close) >>import stoResults >>stoResults.store(enterData.date, enterData.close,smavg,emavg) >>print "Finished" >> >> >>##This is the enterData.py module >>##!/usr/bin/python2.4 >>input = open('/home/tom/Python/Input/SPY2.csv', 'r') >>s = input >>date =[] >>open = [] >>close = [] >>hi = [] >>lo = [] >>vol = [] >>for s in input: >>s = s[:-2] >>y =[] >>y = s.split(',') >>date.append(y[0]) >>open.append(float(y[1])) >>hi.append(float(y[2])) >>lo.append(float(y[3])) >>close.append(float(y[4])) >>vol.append(float(y[5])) >>input.close() >>for i in range(5): >>print close[i] >>print 'enterData.py' >> >> >> >> >>*** >> >> >> >> >>>-- >>> >>>Message: 7 >>>Date: Sat, 27 Aug 2005 22:27:23 -0500 >>>From: Tom Strickland <[EMAIL PROTECTED]> >>>Subject: [Tutor] Importing a List from Module >>>To: tutor@python.org >>>Message-ID: <[EMAIL PROTECTED]> >>>Content-Type: text/plain; charset=ISO-8859-1; format=flowed >>> >>>I have a module called "enterData" which generates a list, "close" from >>>a data file. "close" is a list of floats. When I put a print statement >>>in that module it will print out an individual member of the list. For >>>example, >>> >>> print close[0] >>> >>> >>>prints the first member of the list. >>> >>>In my "main" module I import "enterData" and try to read the first >>>element of "close" as follows: >>> >>> import enterData >>> xy=enterData.close >>> print xy[0] >>> >>> >>>When I do this it prints out the entire "close" list, not just the first >>>term. >>> >>>What's my mistake and how do I correct it? >>> >>>Thank you! >>> >>> >>>-- >>> >>>Message: 8 >>>Date: Sun, 28 Aug 2005 00:15:15 -0400 >>>From: Kent Johnson <[EMAIL PROTECTED]> >>>Subject: Re: [Tutor] Importing a List from Module >>>Cc: tutor@python.org >>>Message-ID: <[EMAIL PROTECTED]> >>>Content-Type: text/plain; charset=ISO-8859-1; format=flowed >>> >>>Tom Strickland wrote: >>> >>> >>> I have a module called "enterData" which generates a list, "close" from a data file. "close" is a list of floats. When I put a print statement in that module it will print out an individual member of the list. For example, print close[0] prints the first member of the list. In my "main" module I import "enterData" and try to read the first element of "close" as follows: import enterData xy=enterData.close print xy[0] When I do this it prints out the entire "close" list, not just the first term. What's my mistake and how do I correct it? >>> >>>What you have shown here looks fine to me. Can you show some more of >>>enterData? >>> >>>Kent >>> >>> >>> >>>-- >>> >>>Message: 9 >>>Date: Sat, 27 Aug 2005 21:25:36 -0700 >>>From: Byron <[EMAIL PROTECTED]> >>>Subject: Re: [Tutor] Importing a List from Module >>>To: Tom Strickland <[EMAIL PROTECTED]>, tutor@python.org >>>Message-ID: <[EMAIL PROTECTED]> >>>Content-Type: text/plain; charset=ISO-8859-1; format=flowed >>> >>>Tom Strickland wrote: >>> >>> >>> In my "main" module I import "enterData" and try to read the first element of "close" as follows: import enterData xy=enterData.close print xy[0] When I do this it prints out the entire "close" list, not just the first term. >>> >>> >>>Hi Tom, >>> >>>I would create a function in your module that returns the list. Here's >>>a quick, simplified example: >>> >>>def returnList(): >>> newList = [] >>> newList += [123.45] >>> newList += [529.59] >>> newList += [259.92] >>> return newList >>> >>>aList = returnList() >>>print aList >>> >>> >>>Note the return statement... This enables assignment, as you have done >>>in "xy=enterData.returnList()" >>> >>>Hope this helps, >>> >
Re: [Tutor] Exceptions and its error messages
Hello! Don't know if someone wrote this already. >But how knowing all error messages from some module? >Is there any way of knowing from python interactive line? >>> for i in dir(__builtins__): >>> if i.endswith("Error"): print i HTH and Greets, J"o! -- Wir sind jetzt ein Imperium und wir schaffen uns unsere eigene Realita:t. Wir sind die Akteure der Geschichte, und Ihnen, Ihnen allen bleibt nichts, als die Realita:t zu studieren, die wir geschaffen haben. -- Karl Rove zu Ron Suskind (NYT) GMX DSL = Maximale Leistung zum minimalen Preis! 2000 MB nur 2,99, Flatrate ab 4,99 Euro/Monat: http://www.gmx.net/de/go/dsl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Importingf a List from Module
Hello! >>When I do this it prints out the entire "close" list, not just the first >>term. In your module: >for i in range(5): >print close[i] Here you tell it to do so. It does it when it gets imported. HTH and Greets, J"o! -- Wir sind jetzt ein Imperium und wir schaffen uns unsere eigene Realita:t. Wir sind die Akteure der Geschichte, und Ihnen, Ihnen allen bleibt nichts, als die Realita:t zu studieren, die wir geschaffen haben. -- Karl Rove zu Ron Suskind (NYT) Lust, ein paar Euro nebenbei zu verdienen? Ohne Kosten, ohne Risiko! Satte Provisionen für GMX Partner: http://www.gmx.net/de/go/partner ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] [Fwd: Re: Importing a List from Module]
Original Message Subject:Re: Importing a List from Module Date: Sun, 28 Aug 2005 16:37:26 -0500 From: Tom Strickland <[EMAIL PROTECTED]> To: tutor@python.org 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
[Tutor] "Lock"ing threads
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? 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 size = get_num: 761 get_num: Que size = 0 0 Closing down Press enter to Quit: ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor