From david at abbottdavid.com Wed Apr 1 01:51:04 2009 From: david at abbottdavid.com (David) Date: Tue, 31 Mar 2009 19:51:04 -0400 Subject: [Tutor] Operational Error. --HELP In-Reply-To: <358348b30903311223r27e18d5dvec410870713fd8b2@mail.gmail.com> References: <358348b30903302108u7523d956t68e60f97618610b2@mail.gmail.com> <358348b30903311115h684a62daldf8baee82a69d483@mail.gmail.com> <49D265F5.3070904@gmail.com> <358348b30903311207n1121932eh5d31609fa14eeafe@mail.gmail.com> <49D26B34.3060106@gmail.com> <358348b30903311223r27e18d5dvec410870713fd8b2@mail.gmail.com> Message-ID: <49D2AC68.1060203@abbottdavid.com> bijoy franco wrote: > I tried following query as well. > > code: > infunction_curs.execute('SELECT * FROM table_book') > > This also throws the same error > > Bijoy Why did you try that? -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From metolone+gmane at gmail.com Wed Apr 1 03:08:55 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Tue, 31 Mar 2009 18:08:55 -0700 Subject: [Tutor] how are unicode chars represented? References: <20090330093651.27bada23@o><1c2a2c590903300352t2bd3f1a7j5f37703cf1c3b0c@mail.gmail.com> <1c2a2c590903310357m682e16acr9d94b12b609939d5@mail.gmail.com> Message-ID: "Kent Johnson" wrote in message news:1c2a2c590903310357m682e16acr9d94b12b609939d5 at mail.gmail.com... On Tue, Mar 31, 2009 at 1:52 AM, Mark Tolonen wrote: >> Unicode is simply code points. How the code points are represented >> internally is another matter. The below code is from a 16-bit Unicode >> build >> of Python but should look exactly the same on a 32-bit Unicode build; >> however, the internal representation is different. >> >> Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit >> (Intel)] >> on win32 >> Type "help", "copyright", "credits" or "license" for more information. >>>>> >>>>> x=u'\U00012345' >>>>> x.encode('utf8') >> >> '\xf0\x92\x8d\x85' >> >> However, I wonder if this should be considered a bug. I would think the >> length of a Unicode string should be the number of code points in the >> string, which for my string above should be 1. Anyone have a 32-bit >> Unicode >> build of Python handy? This exposes the implementation as UTF-16. >>>>> >>>>> len(x) >> >> 2 >>>>> >>>>> x[0] >> >> u'\ud808' >>>>> >>>>> x[1] >> >> u'\udf45' > > In standard Python the representation of unicode is 16 bits, without > correct handling of surrogate pairs (which is what your string > contains). I think this is called UCS-2, not UTF-16. > > There is a a compile switch to enable 32-bit representation of > unicode. See PEP 261 and the "Internal Representation" section of the > second link below for more details. > http://www.python.org/dev/peps/pep-0261/ > http://www.cmlenz.net/archives/2008/07/the-truth-about-unicode-in-python > > Kent My string above is UTF-16 because it *does* handle surrogate pairs. See http://en.wikipedia.org/wiki/UTF-16. "UCS-2 (2-byte Universal Character Set) is an obsolete character encoding which is a predecessor to UTF-16. The UCS-2 encoding form is identical to that of UTF-16, except that it *does not* support surrogate pairs...". The single character \U00012345 was stored by Python as the surrogate pair \ud808\udf45 and was correctly encoded as the 4-byte UTF-8 '\xf0\x92\x8d\x85' in my example. Also, "Because of the technical similarities and upwards compatibility from UCS-2 to UTF-16, the two encodings are often erroneously conflated and used as if interchangeable, so that strings encoded in UTF-16 are sometimes misidentified as being encoded in UCS-2." Python isn't strictly UCS-2 anymore, but it doesn't completely implement UTF-16 either, since string functions return incorrect results for characters outside the BMP. -Mark From sierra_mtnview at sbcglobal.net Wed Apr 1 06:38:13 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Tue, 31 Mar 2009 21:38:13 -0700 Subject: [Tutor] Default Structure of a 640x480 PIL BMP File? Message-ID: <49D2EFB5.4060904@sbcglobal.net> An HTML attachment was scrubbed... URL: From dukelx2005 at gmail.com Wed Apr 1 12:06:41 2009 From: dukelx2005 at gmail.com (Jared White) Date: Wed, 1 Apr 2009 06:06:41 -0400 Subject: [Tutor] My Program stopped working, can anyone help Message-ID: <9239372b0904010306s79a904e7i47189089c946d6a9@mail.gmail.com> I am trying to produce A Caesar cipher is a simple substitution cipher , that would code For example, if the key value is 2, the word ?Sourpuss? would be encoded as ?Uqwtrwuu Here is my code: ( but i keep getting a ERROR) It works for me Last week but for some reason it isnt work for me right now. ANY REASON WHY PLEASE HELP [image: :(] # alphabet.py # Simple string processing program to generate an encryp text def main(): print "This program will encode your messages using a Caesar Cipher" print key = input("Enter the key: ") message = raw_input("Enter the message: ") codedMessage = "" for ch in message: codedMessage = codedMessage + chr(ord(ch) + key) print "The coded message is:", codedMessage main() -------------- next part -------------- An HTML attachment was scrubbed... URL: From padhu.47 at gmail.com Wed Apr 1 12:24:03 2009 From: padhu.47 at gmail.com (Padmanaban Ganesan) Date: Wed, 1 Apr 2009 15:54:03 +0530 Subject: [Tutor] My Program stopped working, can anyone help In-Reply-To: <9239372b0904010306s79a904e7i47189089c946d6a9@mail.gmail.com> References: <9239372b0904010306s79a904e7i47189089c946d6a9@mail.gmail.com> Message-ID: <8aa4df020904010324s6ed92e22l34c6b6427d7a539b@mail.gmail.com> Hi Jared, This is program is working fine for me. Please check the indentation is proper in it. def main(): print "This program will encode your messages using a Caesar Cipher" print key = input("Enter the key: ") message = raw_input("Enter the message: ") codedMessage = "" for ch in message: codedMessage = codedMessage + chr(ord(ch) + key) print "The coded message is:", codedMessage main() Sample Execution: ---------------------------- Enter the key: 123 Enter the message: Hi Paddy The coded message is: ???????? This program will encode your messages using a Caesar Cipher -- Ta, Paddy "The Secret to creativity is knowing how to hide your sources " 2009/4/1 Jared White > I am trying to produce A Caesar cipher is a simple substitution cipher , > that would code For example, if the key value is 2, the word ?Sourpuss? > would be encoded as ?Uqwtrwuu > > Here is my code: ( but i keep getting a ERROR) It works for me Last week > but for some reason it isnt work for me right now. ANY REASON WHY PLEASE > HELP [image: :(] > > # alphabet.py > # Simple string processing program to generate an encryp text > > > def main(): > print "This program will encode your messages using a Caesar Cipher" > print > key = input("Enter the key: ") > message = raw_input("Enter the message: ") > codedMessage = "" > for ch in message: > codedMessage = codedMessage + chr(ord(ch) + key) > print "The coded message is:", codedMessage > > > main() > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Apr 1 13:00:00 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 1 Apr 2009 07:00:00 -0400 Subject: [Tutor] My Program stopped working, can anyone help In-Reply-To: <9239372b0904010306s79a904e7i47189089c946d6a9@mail.gmail.com> References: <9239372b0904010306s79a904e7i47189089c946d6a9@mail.gmail.com> Message-ID: <1c2a2c590904010400i58cb3536p7e4021d2bb099e63@mail.gmail.com> On Wed, Apr 1, 2009 at 6:06 AM, Jared White wrote: > I am trying to produce A Caesar cipher is a simple substitution cipher , > that would code For example, if the key value is 2, the word ?Sourpuss? > would be encoded as ?Uqwtrwuu > > Here is my code: ( but i keep getting a ERROR) It works for me Last week but > for some reason it isnt work for me right now. ANY REASON WHY PLEASE HELP > > # alphabet.py > # Simple string processing program to generate an encryp text > > > def main(): > print "This program will encode your messages using a Caesar Cipher" > print > key = input("Enter the key: ") > message = raw_input("Enter the message: ") > codedMessage = "" > for ch in message: > codedMessage = codedMessage + chr(ord(ch) + key) > print "The coded message is:", codedMessage > > > main() Please be more specific. What input are you giving? What error do you get? What is the value of chr(ord(ch) + key) when ch is 'z' and key is 10? Is that what you want? Kent From roadierich at googlemail.com Wed Apr 1 19:14:16 2009 From: roadierich at googlemail.com (Richard Lovely) Date: Wed, 1 Apr 2009 18:14:16 +0100 Subject: [Tutor] Fwd: range() fractional increment In-Reply-To: References: <430910.53900.qm@web54302.mail.re2.yahoo.com> Message-ID: forgot to reply-all... ---------- Forwarded message ---------- From: Richard Lovely Date: 1 Apr 2009 18:13 Subject: Re: [Tutor] range() fractional increment To: Alan Gauld There's always the itertools solution: import itertools def rangeInThirds(start,end): for whole, frac in itertools.product(range(start,end), (0, 0.3, 0.6)): yield whole + frac The simplist way is to use a list comprehension: [whole + frac for whole in xrange(start, end) for frac in [0, 0.3, 0.6]] Then you just need to turn the iterable (generator in the first case, list in the second into your "array". On 31/03/2009, Alan Gauld wrote: > > "james carnell" wrote > > > example: > > x0000 row = 25 : col = 10 > > x0000 row = 26 : col = 10.3 > > x0000 row = 27 : col = 10.6 > > 0x000 row = 28 : col = 11 > > > > > for col in range(10,12, 0.3): #<- Crash Bang doesn't work 0.3 = zero = > infinite loop? > > > > If you know the limits (rather than their being variables) you can do > > for n in range(100,120,3) > n = n/10 > > But if you have variable range limits then a generator > or while loop are your best bets I think. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/l2p/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com -- Richard "Roadie Rich" Lovely, part of the JNP|UK Famile www.theJNP.com From bgailer at gmail.com Wed Apr 1 21:40:44 2009 From: bgailer at gmail.com (bob gailer) Date: Wed, 01 Apr 2009 15:40:44 -0400 Subject: [Tutor] My Program stopped working, can anyone help In-Reply-To: <9239372b0904010306s79a904e7i47189089c946d6a9@mail.gmail.com> References: <9239372b0904010306s79a904e7i47189089c946d6a9@mail.gmail.com> Message-ID: <49D3C33C.6070102@gmail.com> Jared White wrote: > I am trying to produce A Caesar cipher is a simple substitution cipher > , that would code For example, if the key value is 2, the word > ?Sourpuss? would be encoded as ?Uqwtrwuu > > Here is my code: ( but i keep getting a ERROR) 1 - I see no indentation. Please fix that and repost. 2 - Also post the traceback that is reporting the error. We are not psychic! 3 - be sure to reply to the list. > It works for me Last week but for some reason it isnt work for me > right now. ANY REASON WHY PLEASE HELP :( > > # alphabet.py > # Simple string processing program to generate an encryp text > > > def main(): > print "This program will encode your messages using a Caesar Cipher" > print > key = input("Enter the key: ") > message = raw_input("Enter the message: ") > codedMessage = "" > for ch in message: > codedMessage = codedMessage + chr(ord(ch) + key) > print "The coded message is:", codedMessage > > > main() > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer Chapel Hill NC 919-636-4239 From alan.gauld at btinternet.com Wed Apr 1 23:03:32 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 1 Apr 2009 22:03:32 +0100 Subject: [Tutor] Left Alignment -- Tkinter References: <49CD0D40.5090508@sbcglobal.net> <49CD2D23.2050707@sbcglobal.net> <333efb450903280549o1c39f6b1uec186aaf339af5c1@mail.gmail.com> Message-ID: "W W" wrote > Good news... I got it! > > Here's something that's often a good idea when debugging overlapping > layouts > (especially when you didn't design the parent!) - use different > backgrounds! Yes, or for Frames use a relief value to see where the frame bounds are. > Here's my changes (also found here: http://rafb.net/p/clKroD65.html ) Here is my stripped down version that does what I think you want:: def body(self, master): self.title("Enter Site/Misc. Data") # Heading fLocationTitle = Frame(master, height=7) fLocationTitle.pack() Label(fLocationTitle, text="Geographic Location").pack() # Latitude and Longitude fCoords = Frame(master) fCoords.pack() Label(fCoords, text='Latitude:').grid(row=0, column=0, padx=5) self.lat = Entry(fCoords, width=12) self.lat.grid(row=0, column=1, padx=5) Label(fCoords, text='Longitude:').grid(row=0, column=2, padx=5 ) self.long = Entry(fCoords, width=12) self.long.grid(row=0, column=3, padx=5) I removed quirte a lot of the redundant code that was cluttering things up and moved the frames to beside the widgets that are contained within them. HTH, Alan G. Back from vacation... From jrodriguo at hotmail.com Thu Apr 2 01:59:38 2009 From: jrodriguo at hotmail.com (j mercedes) Date: Wed, 1 Apr 2009 23:59:38 +0000 Subject: [Tutor] toggle through a list Message-ID: Hi, I have problem here...I am writing a program but I am getting stucked at how to solve the mode of a series of numbers...any ideas???following is the program:def mode(data): num=0 count=0 mx=0 mode=0 for i in data: if num!=data[i]: num=data[i] count=count+1 else : count=count+1 if count>mx: mx=count mode=num print 'The mode is: ',mode def median(lst): x=len(data) y=x/2 print 'The median is: ',data[y]def getlst(): num=[] while True: s=raw_input('please enter a series of number to evaluate') if s=='':break num.append(eval(s)) return numdef avg(y): sum(y) print'the average is: ', sum/len(y)data=getlst()median(data)avg(data) -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Thu Apr 2 02:35:52 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 01 Apr 2009 17:35:52 -0700 Subject: [Tutor] toggle through a list In-Reply-To: References: Message-ID: j mercedes wrote: > Hi, I have problem here...I am writing a program but I am getting > stucked at how to solve the mode of a series of numbers...any > ideas???following is the program: > > def mode(data): > num=0 > count=0 > mx=0 > mode=0 > for i in data: i here takes on the values of data -- it isn't the index > if num!=data[i]: here you're using i as though it were an index -- maybe use if num!=i Emile From bgailer at gmail.com Thu Apr 2 03:28:13 2009 From: bgailer at gmail.com (bob gailer) Date: Wed, 01 Apr 2009 21:28:13 -0400 Subject: [Tutor] toggle through a list In-Reply-To: References: Message-ID: <49D414AD.6090803@gmail.com> j mercedes wrote: > Hi, I have problem here...I am writing a program but I am getting > stucked at how to solve the mode of a series of numbers...any ideas??? The customary approach is to collect counts of each value in a dictionary. Untested code follows: from collections import defaultdict def mode(data): numdict = defaultdict(int) for i in data: numdict[data[i]] += 1 numlist = [(count, num) for num, count in nums.items()] numlist.sort() print 'The mode is: ', numlist[-1][1] -- Bob Gailer Chapel Hill NC 919-636-4239 From bgailer at gmail.com Thu Apr 2 03:54:54 2009 From: bgailer at gmail.com (bob gailer) Date: Wed, 01 Apr 2009 21:54:54 -0400 Subject: [Tutor] toggle through a list CORRECTION In-Reply-To: <49D414AD.6090803@gmail.com> References: <49D414AD.6090803@gmail.com> Message-ID: <49D41AEE.2000501@gmail.com> bob gailer wrote: > j mercedes wrote: >> Hi, I have problem here...I am writing a program but I am getting >> stucked at how to solve the mode of a series of numbers...any ideas??? > > The customary approach is to collect counts of each value in a > dictionary. Untested code follows: > > from collections import defaultdict > def mode(data): > numdict = defaultdict(int) > for num in data: # correction > numdict[num] += 1 # correction > numlist = [(count, num) for num, count in nums.items()] > numlist.sort() > print 'The mode is: ', numlist[-1][1] > > -- Bob Gailer Chapel Hill NC 919-636-4239 From sierra_mtnview at sbcglobal.net Thu Apr 2 06:20:40 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Wed, 01 Apr 2009 21:20:40 -0700 Subject: [Tutor] Left Alignment -- Tkinter In-Reply-To: References: <49CD0D40.5090508@sbcglobal.net> <49CD2D23.2050707@sbcglobal.net> <333efb450903280549o1c39f6b1uec186aaf339af5c1@mail.gmail.com> Message-ID: <49D43D18.7000508@sbcglobal.net> An HTML attachment was scrubbed... URL: From srilyk at gmail.com Thu Apr 2 12:40:36 2009 From: srilyk at gmail.com (W W) Date: Thu, 2 Apr 2009 05:40:36 -0500 Subject: [Tutor] Left Alignment -- Tkinter In-Reply-To: <49D43D18.7000508@sbcglobal.net> References: <49CD0D40.5090508@sbcglobal.net> <49CD2D23.2050707@sbcglobal.net> <333efb450903280549o1c39f6b1uec186aaf339af5c1@mail.gmail.com> <49D43D18.7000508@sbcglobal.net> Message-ID: <333efb450904020340o14c69ac0w4e5e5ff44bb192ee@mail.gmail.com> On Wed, Apr 1, 2009 at 11:20 PM, Wayne Watson wrote: > Alan, the game changed in the thread you branched away from to other > considerations. As stated there, I've thrown in the towel, but first let me > say what the game was about. Here's the simplest example. Say I have four > labels: small, big, house, cat, tree, and I want to put them in a column > with full left justification. That is, like this: > > small > big > house > cat > tree > > What I get is: > small > big > house > eat > tree > > More or less centered. There seems to be no way to make left justify > happen. Once an entry is placed to the right of these items, and another > label+entry to the right of them, it gets weird. > Did you get my discovery about anchor + width? Actually, it turns out to be my mistake at not clearly reading the documentation he has for the justify option. Justify is for multiple lines of text, anchor will anchor the text. Try with width=30, anchor=W and you should see what you're looking for. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Thu Apr 2 13:38:02 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Thu, 02 Apr 2009 04:38:02 -0700 Subject: [Tutor] Left Alignment -- Tkinter In-Reply-To: <333efb450904020340o14c69ac0w4e5e5ff44bb192ee@mail.gmail.com> References: <49CD0D40.5090508@sbcglobal.net> <49CD2D23.2050707@sbcglobal.net> <333efb450903280549o1c39f6b1uec186aaf339af5c1@mail.gmail.com> <49D43D18.7000508@sbcglobal.net> <333efb450904020340o14c69ac0w4e5e5ff44bb192ee@mail.gmail.com> Message-ID: <49D4A39A.7030004@sbcglobal.net> An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Thu Apr 2 20:37:34 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Thu, 02 Apr 2009 11:37:34 -0700 Subject: [Tutor] PIL Handbook in pdf Format? Message-ID: <49D505EE.5020400@sbcglobal.net> An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Apr 2 23:06:55 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 2 Apr 2009 22:06:55 +0100 Subject: [Tutor] PIL Handbook in pdf Format? References: <49D505EE.5020400@sbcglobal.net> Message-ID: "Wayne Watson" wrote > I plainly had a copy of the Subject pdf in the past. ... > . A search of my PC shows nothing. Try this: http://www.ezgoal.com/house/final.asp?software=201967 Its what I use to produce the PDF version of my website. The tool presents a web site within an app that allows you to apply your own styles/headers/footers etc and then selectively print all or part of the site. There is a v2 which is less buggy but I can't find a link to it any more. If you print to the Adobe acrobat print driver you get a pdf file... The software was a free download by HP but they have stopped support and withdrawn the download links. Shame, it was a really useful app. I still use it a lot for producing dead wood or PDF versions of chunky web sites. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From sierra_mtnview at sbcglobal.net Fri Apr 3 00:05:51 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Thu, 02 Apr 2009 15:05:51 -0700 Subject: [Tutor] PIL Handbook in pdf Format? In-Reply-To: References: <49D505EE.5020400@sbcglobal.net> Message-ID: <49D536BF.2080700@sbcglobal.net> An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Fri Apr 3 06:41:01 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Thu, 02 Apr 2009 21:41:01 -0700 Subject: [Tutor] PIL Handbook in pdf Format? In-Reply-To: <49D536BF.2080700@sbcglobal.net> References: <49D505EE.5020400@sbcglobal.net> <49D536BF.2080700@sbcglobal.net> Message-ID: <49D5935D.9060307@sbcglobal.net> An HTML attachment was scrubbed... URL: From emile at fenx.com Fri Apr 3 19:27:51 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 03 Apr 2009 10:27:51 -0700 Subject: [Tutor] ping Message-ID: ping! From eduardo.susan at gmail.com Fri Apr 3 21:53:13 2009 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Fri, 3 Apr 2009 13:53:13 -0600 Subject: [Tutor] Can't run this script while I'm logged off of Windows XP Message-ID: <9356b9f30904031253v7fd8391bvf2fb38ced7dcc234@mail.gmail.com> Hello, I'm making a script that daily grabs some information and email it to some of my coworkers. It runs all the other parts while I'm logged off, but this part doesn't. I need to be logged on for it to work. Also, when I'm logged on to my Windows XP I just can't make it send. It only sits in my mailbox. I have to manually press "send" to get it sent. here is the code that won't work: import win32com.client try: s = win32com.client.Dispatch("Mapi.Session") s.Logon("Default") Msg = s.Outbox.Messages.Add("Daily Activity Report", report) testaddresses = [ 'test1 at expresssignproducts.com', ## '1 at expresssignproducts.com', ## '2 at expresssignproducts.com', ## '3 at expresssignproducts.com', ] for address in testaddresses: Msg.Recipients.Add(address) Msg.Send() s.DeliverNow() s.Logoff() except: logfile.write("Couldn't write email") #=== So, I'm coming to a conclusion that it is not possible to send emails while logged off. From emile at fenx.com Fri Apr 3 21:59:34 2009 From: emile at fenx.com (Emile van Sebille) Date: Fri, 03 Apr 2009 12:59:34 -0700 Subject: [Tutor] Can't run this script while I'm logged off of Windows XP In-Reply-To: <9356b9f30904031253v7fd8391bvf2fb38ced7dcc234@mail.gmail.com> References: <9356b9f30904031253v7fd8391bvf2fb38ced7dcc234@mail.gmail.com> Message-ID: Eduardo Vieira wrote: > Hello, I'm making a script that daily grabs some information and email > it to some of my coworkers. It runs all the other parts while I'm > logged off, but this part doesn't. I need to be logged on for it to > work. Also, when I'm logged on to my Windows XP I just can't make it > send. It only sits in my mailbox. I have to manually press "send" to > get it sent. > > here is the code that won't work: > import win32com.client > > try: > s = win32com.client.Dispatch("Mapi.Session") Hmmm... I normally send mail with the smtplib module. There're plenty of examples out there. Maybe you'll have better luck with that. Emile From RWeidner at ea.com Sat Apr 4 00:12:08 2009 From: RWeidner at ea.com (Weidner, Ronald) Date: Fri, 3 Apr 2009 15:12:08 -0700 Subject: [Tutor] subprocess Popen Message-ID: I have a long running py script that I'm trying to kick off from another long running py script as a separate process... If either script fails or terminates, I don't want the other script to be effected as a result. In other words I need a separate process not a new thread. In any case, this code I thought would work but it doesn't. someLongRunningScript= ( "%s/someLongRunningScript.py" % ( os.getcwd() ) ) someLongRunningScriptArgs= ( '--verbose --directory=%s --link=%s %s' % ( directory_name, link_name, span_option ) ) longCommand = ( '%s %s' % ( someLongRunningScript, someLongRunningScriptArgs) ) pid = subprocess.Popen ( ["/usr/bin/python", longCommand ] ).pid print ( "/usr/bin/python %s " % ( longCommand ) ) What's interesting is that if I copy the text printed in the 5th line, paste into my shell, then run it -- the process I'm trying to start works perfectly. The problem seems to have something to do with the way arguments are being passed to the python script named someLongRunningProcess.py. Help is greatly appreciated. -- Ron -------------- next part -------------- An HTML attachment was scrubbed... URL: From RWeidner at ea.com Sat Apr 4 00:03:20 2009 From: RWeidner at ea.com (Weidner, Ronald) Date: Fri, 3 Apr 2009 15:03:20 -0700 Subject: [Tutor] subprocess Popen Message-ID: I have a long running py script that I'm trying to kick off from another long running py script as a separate process... If either script fails or terminates, I don't want the other script to be effected as a result. In other words I need a separate process not a new thread. In any case, this code I thought would work but it doesn't. someLongRunningScript= ( "%s/someLongRunningScript.py" % ( os.getcwd() ) ) someLongRunningScriptArgs= ( '--verbose --directory=%s --link=%s %s' % ( directory_name, link_name, span_option ) ) longCommand = ( '%s %s' % ( someLongRunningScript, someLongRunningScriptArgs) ) pid = subprocess.Popen ( ["/usr/bin/python", longCommand ] ).pid print ( "/usr/bin/python %s " % ( longCommand ) ) What's interesting is that if I copy the text printed in the 5th line, paste into my shell, then run it -- the process I'm trying to start works perfectly. The problem seems to have something to do with the way arguments are being passed to the python script named someLongRunningProcess.py. Help is greatly appreciated. -- Ron -------------- next part -------------- An HTML attachment was scrubbed... URL: From tiagosaboga at gmail.com Sat Apr 4 02:25:11 2009 From: tiagosaboga at gmail.com (Tiago Saboga) Date: Fri, 3 Apr 2009 21:25:11 -0300 Subject: [Tutor] subprocess Popen In-Reply-To: References: Message-ID: <20090404002511.GC4809@localdomain> On Fri, Apr 03, 2009 at 03:12:08PM -0700, Weidner, Ronald wrote: > I have a long running py script that I'm trying to kick off from > another long running py script as a separate process... If either > script fails or terminates, I don't want the other script to be > effected as a result. In other words I need a separate process not a > new thread. In any case, this code I thought would work but it > doesn't. > > someLongRunningScript= ( "%s/someLongRunningScript.py" % ( os.getcwd() ) ) > someLongRunningScriptArgs= ( '--verbose --directory=%s --link=%s %s' % ( directory_name, link_name, span_option ) ) > longCommand = ( '%s %s' % ( someLongRunningScript, someLongRunningScriptArgs) ) > pid = subprocess.Popen ( ["/usr/bin/python", longCommand ] ).pid > print ( "/usr/bin/python %s " % ( longCommand ) ) > > What's interesting is that if I copy the text printed in the 5th > line, paste into my shell, then run it -- the process I'm trying to > start works perfectly. > > The problem seems to have something to do with the way arguments are > being passed to the python script named someLongRunningProcess.py. Yes, the arguments are not passed to the shell. There is a Popen option to pass them to a shell, perhaps it would work. But the simpler way is to write: pid = subprocess.Popen(["/usr/bin/python"] + longCommand.split()).pid It will not work if some arguments are quoted strings with spaces. It would be better if longCommand was already a list. Tiago. From marc at marcd.org Sat Apr 4 03:05:22 2009 From: marc at marcd.org (marc at marcd.org) Date: Fri, 3 Apr 2009 21:05:22 -0400 (EDT) Subject: [Tutor] Trouble Parsing XML using lxml Message-ID: <49312.64.252.205.230.1238807122.squirrel@webmail1.hrnoc.net> Hi - I'm hoping to get some guidance I am trying to parse a structure that looks like: {urn:FindingImport}TOOL - GD {urn:FindingImport}TOOL_VERSION - 2.0.8.8 {urn:FindingImport}AUTHENTICATED_FINDING - TRUE {urn:FindingImport}GD_VUL_NAME - Rename Built-in Guest Account {urn:FindingImport}GD_SEVERITY - 2 {urn:FindingImport}FINDING - None {urn:FindingImport}FINDING_ID - V0001115 {urn:FindingImport}FINDING_STATUS - NF {urn:FindingImport}TOOL - GD {urn:FindingImport}TOOL_VERSION - 2.0.8.8 {urn:FindingImport}AUTHENTICATED_FINDING - TRUE {urn:FindingImport}GD_VUL_NAME - Rename Built-in Administrator Account {urn:FindingImport}GD_SEVERITY - 2 {urn:FindingImport}FINDING - None {urn:FindingImport}FINDING_ID - V0001117 This is the result when the original data is run through 'for element in root.iter():' as described in the lxml tutorial. This structure repeats many times in the document with different values after each tag. I want to take the values and place them in one csv line for each structure in the file. The closest I have come is something like (but doesn't work): for element in root.iter("{urn:FindingImport}TOOL"): print element.text print element.getnext().text print element.getnext().text The initial print element.tag and the first element.getnext().text work as I would like, but I am not finding a way to parse past that. The second element.getnext().text returns the value for the same tag as the one prior to it. I know I am missing something, but don't see it. Any assistance is appreciated. Thanks, marc From dave6502 at googlemail.com Sat Apr 4 11:34:01 2009 From: dave6502 at googlemail.com (dave selby) Date: Sat, 4 Apr 2009 10:34:01 +0100 Subject: [Tutor] os.popen3 > subprocess.Popen but nohup.out won't go Message-ID: Hi, I have upgraded to python 2.6 and was getting depreciation warnings about os.popen3 etc so I changed over to subprocess. os.popen3('nohup %s/core/kmotion_hkd2.py &> /dev/null &' % kmotion_dir) becomes .. subprocess.Popen('nohup %s/core/kmotion_hkd2.py &> /dev/null &' % kmotion_dir, shell=True) all is well except I now get nohup: appending output to `nohup.out' on the terminal which '&> /dev/null' used to ditch to /dev/null. I think this is because subprocess simulates the shell differently. Any idea how to ditch to /dev/null in subprocess ? Cheers Dave -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html From sander.sweers at gmail.com Sat Apr 4 12:24:13 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Sat, 4 Apr 2009 12:24:13 +0200 Subject: [Tutor] os.popen3 > subprocess.Popen but nohup.out won't go In-Reply-To: References: Message-ID: 2009/4/4 dave selby : > os.popen3('nohup %s/core/kmotion_hkd2.py &> /dev/null &' % kmotion_dir) > > becomes .. > > subprocess.Popen('nohup %s/core/kmotion_hkd2.py &> /dev/null &' % > kmotion_dir, shell=True) > > all is well except I now get > > nohup: appending output to `nohup.out' > > on the terminal which '&> /dev/null' used to ditch to /dev/null. I > think this is because subprocess simulates the > shell differently. Any idea how to ditch to /dev/null in subprocess ? Subprocess by default does not store stdout and stderror. Try this: proc = subprocess.Popen('nohup %s/core/kmotion_hkd2.py' % kmotion_dir, shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE) Now proc.communicate returns a tuple with (stdout, strderr). You can also make stdout/stderr go to a file, below untested! outfile = open('/tmp/myoutfile') errfile = open('/tmp/myerrfile') proc = subprocess.Popen('nohup %s/core/kmotion_hkd2.py' % kmotion_dir, shell=True, stdout=outfile,stderr=errfile) proc.communicate() Greets Sander From sander.sweers at gmail.com Sat Apr 4 12:29:31 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Sat, 4 Apr 2009 12:29:31 +0200 Subject: [Tutor] os.popen3 > subprocess.Popen but nohup.out won't go In-Reply-To: References: Message-ID: 2009/4/4 Sander Sweers : Bah, of course you need to open the files writable :-( This > outfile = open('/tmp/myoutfile') > errfile = open('/tmp/myerrfile') Should be outfile = open('/tmp/myoutfile', 'w') errfile =open('/tmp/myerrfile', 'w') Greets Sander From mydomdom at gmail.com Sat Apr 4 13:07:55 2009 From: mydomdom at gmail.com (Dominique) Date: Sat, 4 Apr 2009 11:07:55 +0000 (UTC) Subject: [Tutor] How to make the difference between binary and text files ? Message-ID: Hello, I am developing a very small multi-platform app to search for a specific word or expression in files located in a drive or directory. So I need to open files to search for the word. I read that opening a binary file as a text file (open(filename, 'r')) may corrupt the file. Consequently, before opening the file, I'd like to test it to adapt the opening mode ('r' or 'rb'). Is there a way to make the difference between binary and text files ? Thanks in advance for your help. Dominique From orsenthil at gmail.com Sat Apr 4 14:12:41 2009 From: orsenthil at gmail.com (Senthil Kumaran) Date: Sat, 4 Apr 2009 07:12:41 -0500 Subject: [Tutor] How to make the difference between binary and text files ? In-Reply-To: References: Message-ID: <7c42eba10904040512g1c496f2ag3363d4ac94f9b0cc@mail.gmail.com> Hello Dominique, On Sat, Apr 4, 2009 at 6:07 AM, Dominique wrote: > I am developing a very small multi-platform app to search for a specific word or > expression in files located in a drive or directory. > So I need to open files to search for the word. > > I read that opening a binary file as a text file (open(filename, 'r')) may > corrupt the file. First of all, I doubt if reading a file in text format would cause any harm. Writing may screw things up. Secondly, determining the type of file is based on heuristics and most often determined by reading a chunck of the file. I found the following recipe, that could be useful to you: http://code.activestate.com/recipes/173220/ -- Senthil From cspears2002 at yahoo.com Sat Apr 4 21:37:15 2009 From: cspears2002 at yahoo.com (Christopher Spears) Date: Sat, 4 Apr 2009 12:37:15 -0700 (PDT) Subject: [Tutor] renumbering a sequence Message-ID: <727194.72925.qm@web51605.mail.re2.yahoo.com> I want to write a script that takes a list of images and renumbers them with a user supplied number. Here is a solution I came up while noodling around in the interpreter: >>> alist = ["frame.0001.tif","frame.0002.tif","frame.0003.tif"] >>> new_start = 5000 >>> for x in alist: ... name, number, ext = x.split(".") ... new_frame = name + "." + str(new_start) + "." + ext ... new_start = new_start + 1 ... print new_frame ... frame.5000.tif frame.5001.tif frame.5002.tif How is that for a solution? Is there a more elegant way to solve this problem? From david at abbottdavid.com Sat Apr 4 23:17:56 2009 From: david at abbottdavid.com (David) Date: Sat, 04 Apr 2009 17:17:56 -0400 Subject: [Tutor] Append a dictionary value Message-ID: <49D7CE84.2040205@abbottdavid.com> I put together a todo program here; http://linuxcrazy.pastebin.com/f74beaf78 And I am trying to add an option to edit the values which are date and time. Below I have gotten this far but now I need some help. looks like my attempt to use setdefault will not work. Please point me in the right direction my head has started to hurt :) thanks -david #!/usr/bin/python import time import datetime todo = {'Study Python': (datetime.date(2009, 4, 1), datetime.time(12, 15)), 'Clean House': (datetime.date(2009, 4, 4), datetime.time(15, 15)), 'Fix Python Code': (datetime.date(2009, 3, 29), datetime.time(9, 1))} for k, v in todo.iteritems(): print k, v[0],v[1] answer = raw_input('\nWhich Todo do you want to edit? \nEnter >> ') for k, v in todo.iteritems(): key = todo[answer] current_date = key[0] print 'Current Date and Time for', answer,'\n' print 'Date: =', current_date current_time = key[1] print 'Time: =', current_time print """ Enter D: Edit Date Enter T: Edit Time """ new_value = raw_input('\nWhich value do you want to edit? ') new_value = new_value.lower() if new_value == 'd': print 'Next, enter date for Todo: ' curr_date = time.strftime('%Y %m %d', time.gmtime()) print 'Format as ', curr_date yr = int(raw_input('\nEnter Year: ')) mt = int(raw_input('Enter Month: ')) dy = int(raw_input('Enter Day: ')) datevalue = datetime.date(yr, mt, dy) todo[answer] = datevalue, time #todo.setdefault(answer,[current_date]).append(datevalue) elif new_value == 't': hr = int(raw_input('\nEnter Hour (24h): ')) mn = int(raw_input('Enter Minute (01-59): ')) sec = 0 hourvalue = datetime.time(hr, mn, sec) todo[answer] = date, hourvalue #todo.setdefault(answer,[current_time]).append(hourvalue) else: print 'big time error' -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From david at abbottdavid.com Sun Apr 5 00:30:29 2009 From: david at abbottdavid.com (David) Date: Sat, 04 Apr 2009 18:30:29 -0400 Subject: [Tutor] Append a dictionary value In-Reply-To: <49D7CE84.2040205@abbottdavid.com> References: <49D7CE84.2040205@abbottdavid.com> Message-ID: <49D7DF85.30300@abbottdavid.com> David wrote: > I put together a todo program here; > http://linuxcrazy.pastebin.com/f74beaf78 > > And I am trying to add an option to edit the values which are date and > time. Below I have gotten this far but now I need some help. looks like > my attempt to use setdefault will not work. Please point me in the right > direction my head has started to hurt :) > thanks > -david I guess because the value is a tuple that that can not be changed. So what I did is save the current values as variables and them del the dictionary key and make a new one with the saved variables, or new ones if edited. -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From sander.sweers at gmail.com Sun Apr 5 01:34:37 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 5 Apr 2009 01:34:37 +0200 Subject: [Tutor] Append a dictionary value In-Reply-To: <49D7CE84.2040205@abbottdavid.com> References: <49D7CE84.2040205@abbottdavid.com> Message-ID: 2009/4/4 David : > I put together a todo program here; > http://linuxcrazy.pastebin.com/f74beaf78 > > And I am trying to add an option to edit the values which are date and time. > Below I have gotten this far but now I need some help. looks like my attempt > to use setdefault will not work. Please point me in the right direction my > head has started to hurt :) I do not understand what it is you want to do. Do you want to replace the old date and time with the new? If so you already do this with todo[answer] = datevalue, time. Why do you want to use setdefault? > #!/usr/bin/python > import time > import datetime > todo = {'Study Python': > ? ? ? ?(datetime.date(2009, 4, 1), datetime.time(12, 15)), > ? ? ? ?'Clean House': > ? ? ? ?(datetime.date(2009, 4, 4), datetime.time(15, 15)), > ? ? ? ?'Fix Python Code': > ? ? ? ?(datetime.date(2009, 3, 29), datetime.time(9, 1))} Why not use a datetime object? todo = {'Study Python': (datetime.datetime(2009, 4, 1,12,15)} Greets Sander From david at abbottdavid.com Sun Apr 5 01:58:38 2009 From: david at abbottdavid.com (David) Date: Sat, 04 Apr 2009 19:58:38 -0400 Subject: [Tutor] Append a dictionary value In-Reply-To: References: <49D7CE84.2040205@abbottdavid.com> Message-ID: <49D7F42E.40901@abbottdavid.com> Sander Sweers wrote: > I do not understand what it is you want to do. Do you want to replace > the old date and time with the new? If so you already do this with > todo[answer] = datevalue, time. Why do you want to use setdefault? I was confused, I thought it would add another value to the key not change the current one. > Why not use a datetime object? > > todo = {'Study Python': > (datetime.datetime(2009, 4, 1,12,15)} > > Greets > Sander > > thank you -- Powered by Gentoo GNU/LINUX http://www.linuxcrazy.com pgp.mit.edu From kent37 at tds.net Sun Apr 5 02:02:38 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 4 Apr 2009 20:02:38 -0400 Subject: [Tutor] How to make the difference between binary and text files ? In-Reply-To: References: Message-ID: <1c2a2c590904041702x11e2b4abw80632dbbea3fe10b@mail.gmail.com> On Sat, Apr 4, 2009 at 7:07 AM, Dominique wrote: > Hello, > > I am developing a very small multi-platform app to search for a specific word or > expression in files located in a drive or directory. > So I need to open files to search for the word. > > I read that opening a binary file as a text file (open(filename, 'r')) may > corrupt the file. > Consequently, before opening the file, I'd like to test it to adapt the opening > mode ('r' or 'rb'). > > Is there a way to make the difference between binary and text files ? You might want to look at grin which has full-featured recursive search including identifying binary files: http://pypi.python.org/pypi/grin Kent From kent37 at tds.net Sun Apr 5 02:05:41 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 4 Apr 2009 20:05:41 -0400 Subject: [Tutor] renumbering a sequence In-Reply-To: <727194.72925.qm@web51605.mail.re2.yahoo.com> References: <727194.72925.qm@web51605.mail.re2.yahoo.com> Message-ID: <1c2a2c590904041705m52c5c6abld7731667f6df2414@mail.gmail.com> On Sat, Apr 4, 2009 at 3:37 PM, Christopher Spears wrote: > > I want to write a script that takes a list of images and renumbers them with a user supplied number. ?Here is a solution I came up while noodling around in the interpreter: > >>>> alist = ["frame.0001.tif","frame.0002.tif","frame.0003.tif"] >>>> new_start = 5000 You might want to sort the list, depending on where it came from: alist.sort() >>>> for x in alist: > ... ? ? name, number, ext = x.split(".") > ... ? ? new_frame = name + "." + str(new_start) + "." + ext You could use string formatting here; new_frame = '%s.%s.%s' % (name, new_start, ext) > ... ? ? new_start = new_start + 1 new_start += 1 > ... ? ? print new_frame > ... > frame.5000.tif > frame.5001.tif > frame.5002.tif > > How is that for a solution? ?Is there a more elegant way to solve this problem? It's fine. Kent From davea at ieee.org Sun Apr 5 03:09:36 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Apr 2009 20:09:36 -0500 Subject: [Tutor] renumbering a sequence In-Reply-To: References: Message-ID: <49D804D0.6070606@ieee.org> Christopher Spears wrote: > I want to write a script that takes a list of images and renumbers > them with a user supplied number. Here is a solution I came up while > noodling around in the interpreter: >>>> >>> alist = ["frame.0001.tif","frame.0002.tif","frame.0003.tif"] >>>> >>> new_start = 5000 >>>> >>> for x in alist: >>>> > alist = ["frame.0001.tif","frame.0002.tif","frame.0003.tif"] > >>> new_start = 5000 > >>> for x in alist: > > ... name, number, ext = x.split(".") > ... new_frame = name + "." + str(new_start) + "." + ext > ... new_start = new_start + 1 > ... print new_frame > ... > frame.5000.tif > frame.5001.tif > frame.5002.tif > > How is that for a solution? Is there a more elegant way to solve this problem? > > A few things you could do more elegantly. Main thing is to avoid explictly incrementing new_start. The trick for that is enumerate. I'd do something like: for num, x in enumerate(alist, start=5000) : name, dummy, ext = x.split(".") new_frame = ".".join( (name, str(num), ext) ) print new_frame Notes: enumerate turns a list of strings into a sequence of num/string pairs, where the numbers are sequential starting with 5000. And string.join concatenates all the strings in the list, with "." in between them. It is the inverse of split, so it makes perfect sense. Only catch is that you have to use double parentheses, since you have to turn those three strings into a list of strings, then pass it to join as a single argument. From sierra_mtnview at sbcglobal.net Sun Apr 5 06:03:08 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Sat, 04 Apr 2009 21:03:08 -0700 Subject: [Tutor] Learn as You Go. Tkinter, PIL, Pmw, etc. and Toolkits--Choices Message-ID: <49D82D7C.50204@sbcglobal.net> An HTML attachment was scrubbed... URL: From mydomdom at gmail.com Sun Apr 5 08:14:07 2009 From: mydomdom at gmail.com (Dominique) Date: Sun, 5 Apr 2009 06:14:07 +0000 (UTC) Subject: [Tutor] =?utf-8?q?How_to_make_the_difference_between_binary_and_t?= =?utf-8?q?ext=09files_=3F?= References: <1c2a2c590904041702x11e2b4abw80632dbbea3fe10b@mail.gmail.com> Message-ID: Kent Johnson tds.net> writes: > > You might want to look at grin which has full-featured recursive > search including identifying binary files: > http://pypi.python.org/pypi/grin > > Kent > _______________________________________________ > Tutor maillist - Tutor python.org > http://mail.python.org/mailman/listinfo/tutor > > Thanks for your responses Dominique From denis.spir at free.fr Sun Apr 5 09:36:21 2009 From: denis.spir at free.fr (spir) Date: Sun, 5 Apr 2009 09:36:21 +0200 Subject: [Tutor] renumbering a sequence In-Reply-To: <1c2a2c590904041705m52c5c6abld7731667f6df2414@mail.gmail.com> References: <727194.72925.qm@web51605.mail.re2.yahoo.com> <1c2a2c590904041705m52c5c6abld7731667f6df2414@mail.gmail.com> Message-ID: <20090405093621.47b4b561@o> Le Sat, 4 Apr 2009 20:05:41 -0400, Kent Johnson s'exprima ainsi: > On Sat, Apr 4, 2009 at 3:37 PM, Christopher Spears > wrote: > > > > I want to write a script that takes a list of images and renumbers them > > with a user supplied number. ?Here is a solution I came up while noodling > > around in the interpreter: > > > >>>> alist = ["frame.0001.tif","frame.0002.tif","frame.0003.tif"] > >>>> new_start = 5000 > > You might want to sort the list, depending on where it came from: > alist.sort() > > >>>> for x in alist: > > ... ? ? name, number, ext = x.split(".") > > ... ? ? new_frame = name + "." + str(new_start) + "." + ext > > You could use string formatting here; > new_frame = '%s.%s.%s' % (name, new_start, ext) > > > ... ? ? new_start = new_start + 1 > > new_start += 1 > > > ... ? ? print new_frame > > ... > > frame.5000.tif > > frame.5001.tif > > frame.5002.tif > > > > How is that for a solution? ?Is there a more elegant way to solve this > > problem? > > It's fine. ... and rename new_start (it's a new start number only as long as ==5000) Denis ------ la vita e estrany From alan.gauld at btinternet.com Sun Apr 5 10:26:31 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 5 Apr 2009 09:26:31 +0100 Subject: [Tutor] Learn as You Go. Tkinter, PIL, Pmw, etc. and Toolkits--Choices References: <49D82D7C.50204@sbcglobal.net> Message-ID: "Wayne Watson" wrote > I get the feeling, particularly with Tkinter and PIL, that I'm dealing > with antiquated tools, and documentation. They are both quite old and therefore quite stable. They do what they do well and don't need to change very often. That is a Good Thing in that they are reliable and well understood in the community. I'd much prefer to use software that has a large installed base and hasn't changed in the last year or so than something that is still in active development and only at version 0.9 or whatever. (Althougfh I'd still prefer the latter to writing it myself! :-) > I inquired about Pmw here, and it too looks a little old. It is old and with very little activity. It was never mainstream. > I recently bought the book Core Python and decided to look > at his section on toolkits. I see Pmw was meant as a > replacement for Tkinter. No PMW builds on top of TKinter in the same way as Tix does. It addresses weaknesses in the size of the widget set that Tk supports (remember Tkinter - and most Python toolkits - is just a wrapper around another toolkit in another langage, in this case Tk.) > After my grid experience, I hope so. The grid is a perfectly good layout manager but like all such tools it works in a particular way and has lots of options. Some things are better done with the packer or placer (and the new form) managers. The designer has to pick the best tool for the job. > Probably grid is pretty sound, but the documentation I've dug up > doesn't seem sufficient. The Python documentation for Tkinter generally is quite good to get you started but there is a real lack of in depth stuff. For that you really need to go to the Tk sources. Fortunately translating Tk to Tkinter is pretty easy. Even better the new Tk web site is starting to give example code for Perl and Ruby as well as Tk so it is even easier to see how it works in other languages. > Perhaps grid is still a foundation in Pmw. Yes, PMW builds out of Tkinter it not separate. But I suspect that with Tix now in the standard library and doing most of what PMW does that PMW will tend to wither and die. If someone produced a really good reference for Tkinter and Tix (especially based on the new Tk widget set) then things would be a whole lot better in Python GUI land. > My view is that it could be better described. Totally agree, although the Tk docs are more comprehensive. (Although oddly not so good for beginners!) > the author briefly describes wxWidgets. This is a completely separate GUI framework which is more comprehensive than Tkinter, even with PMW or Tix. But its a whole new learning curve and is not standard Python Library. > Tix, The now official extension library to Tkinter, given extra widgets. > GtK+, PyGTK, Python wrapper around yet another set of GUI widgets. I've never used it so can't comment. Some people like it. If you use it in another language then its the obvious choice. > FXpy, Never even heard of this one! > PyOpenGL. This is a wrapper around the OpenGL graphics library, closer to PIL in function than to Tkinter. Its all about drawing to screen quickly. If you need dynamic screen gtraphics you may need this. > They probably all have a place, Just so. Each does a specific job, or wraps a specific external toolkit to look at least a bit like Python. > If I had to guess, I might think Pmw might be a good choice to > carry the ball into graphics. Nope, it doesn't offer much for graphics, its really a set of widgets (a spreadsheet like table, combo boxes etc) and a framework for building other widgets. But it is not graphics oriented particularly. > I base that on looking at the chapter 10 contents I posted a few weeks > ago. Remember Grayson is about 10 years old now so there are newer tools. But most of Chapter 10 is based on the Canvas widget which is pure Tkinter and the baseline tool for drawing graphics in Tk. If you are drawing graphs for example there are other libraries that work with Tkinter that will be much easier than using the canvas. > So here I am wondering if instead of Tkinter or Python, maybe I > should take up Pmw or some other toolkits. Most of the ther toolkits will have similar issues. Even wxPython which is the Python wrapper around wxWidgets, requires you to read the C++ documentation and code from time to time. This is always going to happen when you build a wrapper around another language toolkit. PMW is built onTkinter which is built on Tk which is built on C!. > Given the sparseness of the PIL documents, I may end up doing > a lot of needless experimenting that is available some other way. I've only used PIL in very basic work. But it is your best option for graphics manipulation in Python I suspect. Displaying the images created will require interaction with the GUI and PIL does at least work with Tkinter... But experimenting interactively is core to building software in any environment. Most of these toolkits have so many options that its virtually impossible to document every possible interaction of every option. You must experiment to see how the bits you are interested in will work together. It's what software engineers do... As I say in the concepts section oof my tutorial: ------------------------------- The other thing that is useful is the right mind set to program. What I mean by that is an innate curiosity about things, coupled to a logical way of thinking. These are both essential requirements for a successful programmer. The curiosity factor comes into play in looking for answers to problems and being willing to dig around in sometimes obscure documents for ideas and information needed to complete a task. The logical thinking comes into play because computers are intrinsically stupid. They can't really do anything except add single digits together and move bytes from one place to another. Luckily for us some talented programmers have written lots of programs to hide this basic stupidity. But of course as a programmer you may well get into a new situation where you have to face that stupidity in its raw state. At that point you have to think for the computer. You have to figure out exactly what needs to be done to your data and when. ------------------------------------------ -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l From sierra_mtnview at sbcglobal.net Sun Apr 5 13:48:33 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Sun, 05 Apr 2009 04:48:33 -0700 Subject: [Tutor] Learn as You Go. Tkinter, PIL, Pmw, etc. and Toolkits--Choices In-Reply-To: References: <49D82D7C.50204@sbcglobal.net> Message-ID: <49D89A91.9020502@sbcglobal.net> An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun Apr 5 17:16:22 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 5 Apr 2009 16:16:22 +0100 Subject: [Tutor] Learn as You Go. Tkinter, PIL, Pmw, etc. and Toolkits--Choices References: <49D82D7C.50204@sbcglobal.net> <49D89A91.9020502@sbcglobal.net> Message-ID: "Wayne Watson" wrote > > The grid is a perfectly good layout manager but like all such > > tools it works in a particular way and has lots of options. Some > > To me grid looks like simple arithmetic, +, -, * and /, but someone > left out the description (in a fictitious book on arithmetic) of "/ > "(division). You really need to refer to the Tk documents. Here is the Tk reference for Grid. http://www.tcl.tk/man/tcl8.4/TkCmd/grid.htm#M18 The example at the end is, of course, in Tcl/Tk but the documentation of the options all appllies in Tkinter as well. In particular the explanation of the master/slave concept and the algorithm used by grisd is useful reading. But it is not easy stuff, it is written for professionals and assumes a fair knowledge of GUI programming concepts. > I suspect the New Mex Tech doc is among the more comprehensive, > but not quite right for beginners. This is a Tkinter document and as such is very good, but it still IMHO doesn't cover the depth of the Tk docs > The now official extension library to Tkinter, given extra widgets. > GtK+, PyGTK, > > Ah, ha. This may be places for me to explore, especially if they have > extension to canvas and anything to do with IP (img proc). I'm not sure but since the GtK was originally developed to build the GIMP there is a fair likeliehood that it does have some more advanced IP stuff > Here's a big question though. Are any of these toolkits free? So far as I know they are all free with the minor exception of Qt which is free in certain circumstances > FXpy, > > FOX binding to Python. Binding? > What does that mean with respect to the language? It means it is a Python wrapper around some other language API - usually C/C++ So you can refer to the original docs and call the functions from Python. Tkinter is a Python binding for Tk. > Likely so, how time flies. However, I'm intrigued with some of > his topics like rubber bands, and moving items on the canvas. > If Pmw is free and available, it might be worth exercising his > draw example to see how it works. Yes PMW is free and still available, at least for Python v2.x Grayson's example does not use PIL. PIL doesn't do dynamic stuff like rubber bands though, it only does the actual image manipulation - actually creating and editing GIF/JPEG images etc The interaction stuff you need to do using the GUI toolkit. > To me the documentation here is like my arithmetic analogy, > but they (say, an author of a book on the subject) left out > descriptions of both multiplication and division, Thats because the documentors assume you know the underlying toolkt and are only really documenting how to use it from Python. Its like somebody writing a new mathematical notation for addition or subtraction etc(think Matrix arithmetic say). They define the new operations b6y allegory to the original math notation but they assume you know how the basics work already. > > option. You must experiment to see how the bits you are interested > > in will work together. It's what software engineers do... > > This is fine, but I don't want to be a software engineer. But the task you are doing is software engineering. Its a bit like deciding that you will build your own house extension rather than employing a builder. Then you discover you can't understand the architect's drawings. You need to learn to think/work like a builder. Similarly to write production level code (which is what you are doing) you need to think/work like a software engineer. You are stepping beyond the normal territory of the novice/hobbyist programmer. > I have a feeling though there is a lot of loose documentation or > knowledge imbedded in Python that I'm not yet handy at. > Dir, Doc, vars, docstrings, Pydoc, help, ... There is a lot you can do with things like help() and dir() to get a clue what to look at (or google for) in the documentation. But you won't always get detail there, simply clues. Often you need to read the source code to truly understand what's going on. This is one of the penalties (some say blessings!) of using open source. The documenration may not be perfect but you can always read the source - if you have the time! HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sierra_mtnview at sbcglobal.net Sun Apr 5 21:46:42 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Sun, 05 Apr 2009 12:46:42 -0700 Subject: [Tutor] Learn as You Go. Tkinter, PIL, Pmw, etc. and Toolkits--Choices In-Reply-To: References: <49D82D7C.50204@sbcglobal.net> <49D89A91.9020502@sbcglobal.net> Message-ID: <49D90AA2.8050306@sbcglobal.net> An HTML attachment was scrubbed... URL: From ctcast at gmail.com Sun Apr 5 22:36:09 2009 From: ctcast at gmail.com (Chris Castillo) Date: Sun, 5 Apr 2009 15:36:09 -0500 Subject: [Tutor] base n fractional Message-ID: <50e459210904051336v60dfc6ddt280d3c9c8f6e035b@mail.gmail.com> I need some help converting the fractional (right side of the decimal) to base 10. I have this but it doesn't work mynum = raw_input("Please enter a number: ") myint, myfrac = mynum.split(".") base = raw_input("Please enter the base you would like to convert to: ") base = int(base) mydecfrac = 0 fraclen = len(myfrac) - 1 for digit in range(len(myfrac) -1, -1, -1): mydecfrac = mydecfrac + int(myfrac[digit]) mydecfrac = mydecfrac / base -------------- next part -------------- An HTML attachment was scrubbed... URL: From benjamin.serrato at gmail.com Sun Apr 5 22:54:49 2009 From: benjamin.serrato at gmail.com (Benjamin Serrato) Date: Sun, 5 Apr 2009 15:54:49 -0500 Subject: [Tutor] Formatting zip module arguments correctly Message-ID: Please tell me why this code fails. I am new and I don't understand why my formatting of my zip arguments is incorrect. Since I am unsure how to communicate best so I will show the code, the error message, and what I believe is happening. #!c:\python30 # Filename: backup_ver5.py import os import time import zipfile source = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list' target_dir = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir' today = target_dir + os.sep + time.strftime('%Y%m%d') now = time.strftime('%H%M%S') comment = input('Enter a comment --> ') if len(comment) == 0: target = today + os.sep + now + '.zip' else: target = today + os.sep + now + '_' + \ comment.replace(' ', '_') + '.zip' if not os.path.exists(today): os.mkdir(today) print('Successfully created directory', today) print(target) print(source) zip_command = zipfile.ZipFile(target, 'w').write(source) if os.system(zip_command) == 0: print('Successful backup to', target) else: print('Backup FAILED') I receive this error message: Enter a comment --> C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir\200904 05\154956.zip C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list Traceback (most recent call last): File "C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_ve r5.py", line 32, in zip_command = zipfile.ZipFile(target, 'w').write(source) File "c:\python30\lib\zipfile.py", line 1031, in write fp = io.open(filename, "rb") File "C:\Python30\lib\io.py", line 222, in open closefd) File "C:\Python30\lib\io.py", line 615, in __init__ _fileio._FileIO.__init__(self, name, mode, closefd) IOError: [Errno 13] Permission denied: 'C:\\Documents and Settings\\Benjamin Ser rato\\My Documents\\python\\backup_list' The two print tests before zip_command is assigned tell me that the two strings are being passed to zipfile.ZipFile() correctly. The traceback tells me I am not calling zipfile.ZipFile() correctly. The error in __init__ makes me more sure of this. Last, the problem seems to be that I am causing my path string to have double backslashes. I can't follow why the IOError shows that. I used this site to figure out how to use zipfile. zipfile is a class, I import it at the start of the program then I use it and its primary method. I pass the file I would like to write to zipfile.ZipFile('file to write', 'mode') and set the program to open an object set to be writable. Then the command writes the file to the destination folder with a sub-method like so, "".zipfile('files to write'). Where am I going wrong? -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Sun Apr 5 23:28:27 2009 From: bgailer at gmail.com (bob gailer) Date: Sun, 05 Apr 2009 17:28:27 -0400 Subject: [Tutor] Formatting zip module arguments correctly In-Reply-To: References: Message-ID: <49D9227B.9030908@gmail.com> Benjamin Serrato wrote: > Please tell me why this code fails. I am new and I don't understand why my formatting of my zip arguments is incorrect. Since I am unsure how to communicate best so I will show the code, the error message, and what I believe is happening. > > #!c:\python30 > # Filename: backup_ver5.py > > import os > import time > > import zipfile > > > source = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list' > > target_dir = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir' > > > today = target_dir + os.sep + time.strftime('%Y%m%d') > > now = time.strftime('%H%M%S') > > comment = input('Enter a comment --> ') > > if len(comment) == 0: > target = today + os.sep + now + '.zip' > > else: > target = today + os.sep + now + '_' + \ > comment.replace(' ', '_') + '.zip' > > if not os.path.exists(today): > os.mkdir(today) > print('Successfully created directory', today) > > > > print(target) > print(source) > zip_command = zipfile.ZipFile(target, 'w').write(source) > > if os.system(zip_command) == 0: > print('Successful backup to', target) > else: > print('Backup FAILED') > > > I receive this error message: > > Enter a comment --> > C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir\200904 > 05\154956.zip > C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list > Traceback (most recent call last): > File "C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_ve > r5.py", line 32, in > zip_command = zipfile.ZipFile(target, 'w').write(source) > File "c:\python30\lib\zipfile.py", line 1031, in write > fp = io.open(filename, "rb") > File "C:\Python30\lib\io.py", line 222, in open > closefd) > File "C:\Python30\lib\io.py", line 615, in __init__ > _fileio._FileIO.__init__(self, name, mode, closefd) > IOError: [Errno 13] Permission denied: 'C:\\Documents and Settings\\Benjamin Ser > rato\\My Documents\\python\\backup_list' Permission denied? How is that not clear? Why it is denied has nothing to do with Python or zipfile. It is a file system problem. Appears you don't have permission to write to C:\\Documents and Settings\\Benjamin Serrato\\My Documents\\python\\ > > The two print tests before zip_command is assigned tell me that the two strings are being passed to zipfile.ZipFile() correctly. The traceback tells me I am not calling zipfile.ZipFile() correctly. The error in __init__ makes me more sure of this. Last, the problem seems to be that I am causing my path string to have double backslashes. I can't follow why the IOError shows that. Python displays \ as \\ since \ is used as an escape character. There is really just one \ in the actual string. > > I used this site to figure out how to use zipfile. zipfile is a class, I import it at the start of the program then I use it and its primary method. I pass the file I would like to write to zipfile.ZipFile('file to write', 'mode') and set the program to open an object set to be writable. Then the command writes the file to the destination folder with a sub-method like so, "".zipfile('files to write'). > > Where am I going wrong? First problem is posting in anything other than plain text. Please always use plain text. I hate having to get out my magnifying glass. From alan.gauld at btinternet.com Mon Apr 6 01:01:39 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Apr 2009 00:01:39 +0100 Subject: [Tutor] Learn as You Go. Tkinter, PIL, Pmw, etc. and Toolkits--Choices References: <49D82D7C.50204@sbcglobal.net> <49D89A91.9020502@sbcglobal.net> <49D90AA2.8050306@sbcglobal.net> Message-ID: "Wayne Watson" wrote > manipulation - actually creating and editing GIF/JPEG images etc > The interaction stuff you need to do using the GUI toolkit. > > The GUI toolkit, Pmw, Tkinter? Yes exactly. Or wxPython or PyGtK or whatever, they will all provide a canvas type widget which you can program to display the images created by PIL, ImageMagick, OpenGL etc. It's about using each toolkit to do the thintg it is best at and then getting the various toolkits to talk to each other for a final solution. Sadly the last bit is often what determines which set of tools you can select because the pain of trying to knit together two incompatible libraries usually far outweighs any benefits a particular toolset in isolation may offer. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From kent37 at tds.net Mon Apr 6 01:19:41 2009 From: kent37 at tds.net (Kent Johnson) Date: Sun, 5 Apr 2009 19:19:41 -0400 Subject: [Tutor] Formatting zip module arguments correctly In-Reply-To: References: Message-ID: <1c2a2c590904051619k663bd7a6raa686d9c1378baf6@mail.gmail.com> On Sun, Apr 5, 2009 at 4:54 PM, Benjamin Serrato wrote: > Please tell me why this code fails. I am new and I don't understand why my > formatting of my zip arguments is incorrect. Since I am unsure how to > communicate best so I will show the code, the error message, and what I > believe is happening. > > #!c:\python30 > # Filename: backup_ver5.py > > import os > import time > > import zipfile > > > source = r'C:\Documents and Settings\Benjamin Serrato\My > Documents\python\backup_list' > > target_dir = r'C:\Documents and Settings\Benjamin Serrato\My > Documents\python\backup_dir' > > > today = target_dir + os.sep + time.strftime('%Y%m%d') > > now = time.strftime('%H%M%S') > > comment = input('Enter a comment --> ') > > if len(comment) == 0: > target = today + os.sep + now + '.zip' > > else: > target = today + os.sep + now + '_' + \ > comment.replace(' ', '_') + '.zip' > > if not os.path.exists(today): > os.mkdir(today) > print('Successfully created directory', today) > > > > print(target) > print(source) > zip_command = zipfile.ZipFile(target, 'w').write(source) The argument to write() must be a file path, are you giving it a directory path? > > if os.system(zip_command) == 0: > print('Successful backup to', target) > else: > print('Backup FAILED') > > > I receive this error message: > ? File "c:\python30\lib\zipfile.py", line 1031, in write > ??? fp = io.open(filename, "rb") > ? File "C:\Python30\lib\io.py", line 222, in open > ??? closefd) > ? File "C:\Python30\lib\io.py", line 615, in __init__ > ??? _fileio._FileIO.__init__(self, name, mode, closefd) > IOError: [Errno 13] Permission denied: 'C:\\Documents and Settings\\Benjamin > Ser > rato\\My Documents\\python\\backup_list' Notice that it is the write() call that is failing, not ZipFile(), and the error is a read error on target. > The two print tests before zip_command is assigned tell me that the two > strings are being passed to zipfile.ZipFile() correctly. The traceback tells > me I am not calling zipfile.ZipFile() correctly. The error in __init__ makes > me more sure of this. No, it is FileIO.__init__() that is failing, not ZipFile.__init__() > I used this site to figure out how to use zipfile. zipfile is a class, I > import it at the start of the program then I use it and its primary method. > I pass the file I would like to write to zipfile.ZipFile('file to write', > 'mode') and set the program to open an object set to be writable. Then the > command writes the file to the destination folder with a sub-method like so, > "".zipfile('files to write'). It should be zipfile('file to write') Kent From alan.gauld at btinternet.com Mon Apr 6 01:25:57 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Apr 2009 00:25:57 +0100 Subject: [Tutor] base n fractional References: <50e459210904051336v60dfc6ddt280d3c9c8f6e035b@mail.gmail.com> Message-ID: "Chris Castillo" wrote >I need some help converting the fractional (right side of the decimal) to > base 10. > I have this but it doesn't work Frankly, based on your algorithm, I'm not sure what exactly you want to do but taking the above statement as a starting point I'd convert the fractional part into an integer then divide by 10**(len(n)) Something like: n = raw_input('????') # say n ->1.234 d,f = n.split('.') # so d -> '1', f -> '234' result = int(f)/(10**len(f)) # result = 234/10**3 = 234/1000 = 0.234 Or you could just add a dot back on and convert to float: f = float('.'+f) # float('.234') -> 0.234 > for digit in range(len(myfrac) -1, -1, -1): > mydecfrac = mydecfrac + int(myfrac[digit]) > mydecfrac = mydecfrac / base This just confused me. I think this might do what you are trying to do: for digit in reversed(list('234')): mydecfrac = (mydecfrac + int(digit)) / base But that doesn't seem to do what I think you wanted? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From alan.gauld at btinternet.com Mon Apr 6 01:37:47 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Apr 2009 00:37:47 +0100 Subject: [Tutor] Formatting zip module arguments correctly References: Message-ID: "Benjamin Serrato" wrote > formatting of my zip arguments is incorrect. Since I am unsure how to > communicate best so I will show the code, the error message, and what I > believe is happening. Thats pretty close to communicating the best way! :-) > zip_command = zipfile.ZipFile(target, 'w').write(source) > > if os.system(zip_command) == 0: I didn't think you needed to call os.system when using the zipfile module? The module should do everything itself... But your error occurs on the line above and it says: > File "C:\Python30\lib\io.py", line 615, in __init__ > _fileio._FileIO.__init__(self, name, mode, closefd) > IOError: [Errno 13] Permission denied: 'C:\\Documents and > Settings\\Benjamin > Serrato\\My Documents\\python\\backup_list' You don't have permission to write that file. Now either you don't have the right permissions on the OS or the file is locked by another program? But it looks like this is the file you are passing to zipfile? I thought the filename in the constructor was supposed to be the actual zipfile name (ie the output!) not the file you are trying to zip? But I'm no zipfile module expert so I may be wrong there. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From ctcast at gmail.com Mon Apr 6 02:17:20 2009 From: ctcast at gmail.com (Chris Castillo) Date: Sun, 5 Apr 2009 19:17:20 -0500 Subject: [Tutor] base n fractional In-Reply-To: References: <50e459210904051336v60dfc6ddt280d3c9c8f6e035b@mail.gmail.com> Message-ID: <50e459210904051717q6b67575dr95a926aba9d03a18@mail.gmail.com> well i want to use a for loop to work from the right most digit to the left most digit at a negative 1 increment. For each digit i want to divide it by 1/base and add the next digit. Then I need to take that sum and multiply it by 1/b to get the decimal equivalent so when I say that to myself i see: number = 234 mysum = 0 for digit in range(len(number) -1, -1, -1): mysum = (mysum) * (1/base) + int(number[digit]) On Sun, Apr 5, 2009 at 6:25 PM, Alan Gauld wrote: > > "Chris Castillo" wrote > > I need some help converting the fractional (right side of the decimal) to >> base 10. >> I have this but it doesn't work >> > > Frankly, based on your algorithm, I'm not sure what exactly you want > to do but taking the above statement as a starting point > > I'd convert the fractional part into an integer then divide by 10**(len(n)) > > Something like: > > n = raw_input('????') # say n ->1.234 > d,f = n.split('.') # so d -> '1', f -> '234' > result = int(f)/(10**len(f)) # result = 234/10**3 = 234/1000 = > 0.234 > > Or you could just add a dot back on and convert to float: > > f = float('.'+f) # float('.234') -> 0.234 > > for digit in range(len(myfrac) -1, -1, -1): >> mydecfrac = mydecfrac + int(myfrac[digit]) >> mydecfrac = mydecfrac / base >> > > This just confused me. > I think this might do what you are trying to do: > > for digit in reversed(list('234')): > mydecfrac = (mydecfrac + int(digit)) / base > > But that doesn't seem to do what I think you wanted? > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/l2p/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From metolone+gmane at gmail.com Mon Apr 6 07:08:17 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sun, 5 Apr 2009 22:08:17 -0700 Subject: [Tutor] Formatting zip module arguments correctly References: Message-ID: The error indicates your source file cannot be read. Did you have it open in an editor that locks it for exclusive use when you ran your program? Also, the command: zipfile.ZipFile(target, 'w').write(source) writes backup_list to the target zipfile, and returns None, assigning the return value to zip_command and passing that to os.system makes no sense. The command above will throw an exception if anything goes wrong (as you found), so the following code is probably what you want: try: zipfile.ZipFile(target, 'w').write(source) except IOError: print('Backup Failed') else: print('Successful backup to',target) -Mark "Benjamin Serrato" wrote in message news:dde7cc5d0904051354v1c103fb2wf28f1827349706c9 at mail.gmail.com... Please tell me why this code fails. I am new and I don't understand why my formatting of my zip arguments is incorrect. Since I am unsure how to communicate best so I will show the code, the error message, and what I believe is happening. #!c:\python30# Filename: backup_ver5.pyimport osimport time import zipfilesource = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list'target_dir = r'C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir' today = target_dir + os.sep + time.strftime('%Y%m%d') now = time.strftime('%H%M%S')comment = input('Enter a comment --> ')if len(comment) == 0: target = today + os.sep + now + '.zip' else: target = today + os.sep + now + '_' + \ comment.replace(' ', '_') + '.zip'if not os.path.exists(today): os.mkdir(today) print('Successfully created directory', today) print(target)print(source)zip_command = zipfile.ZipFile(target, 'w').write(source)if os.system(zip_command) == 0: print('Successful backup to', target)else: print('Backup FAILED') I receive this error message: Enter a comment --> C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_dir\200904 05\154956.zip C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_list Traceback (most recent call last): File "C:\Documents and Settings\Benjamin Serrato\My Documents\python\backup_ve r5.py", line 32, in zip_command = zipfile.ZipFile(target, 'w').write(source) File "c:\python30\lib\zipfile.py", line 1031, in write fp = io.open(filename, "rb") File "C:\Python30\lib\io.py", line 222, in open closefd) File "C:\Python30\lib\io.py", line 615, in __init__ _fileio._FileIO.__init__(self, name, mode, closefd) IOError: [Errno 13] Permission denied: 'C:\\Documents and Settings\\Benjamin Ser rato\\My Documents\\python\\backup_list' The two print tests before zip_command is assigned tell me that the two strings are being passed to zipfile.ZipFile() correctly. The traceback tells me I am not calling zipfile.ZipFile() correctly. The error in __init__ makes me more sure of this. Last, the problem seems to be that I am causing my path string to have double backslashes. I can't follow why the IOError shows that. I used this site to figure out how to use zipfile. zipfile is a class, I import it at the start of the program then I use it and its primary method. I pass the file I would like to write to zipfile.ZipFile('file to write', 'mode') and set the program to open an object set to be writable. Then the command writes the file to the destination folder with a sub-method like so, "".zipfile('files to write'). Where am I going wrong? ------------------------------------------------------------------------------ _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From kabads at gmail.com Mon Apr 6 09:30:41 2009 From: kabads at gmail.com (AdamC) Date: Mon, 6 Apr 2009 08:30:41 +0100 Subject: [Tutor] Checking for string in a list strangeness Message-ID: I'm writing a small cgi application for children to use and I want to check that the name they enter isn't a swear word. I've populated a database with a swear word list and then check this list against the input, but it always shows up as not being a swear word. Can anyone see why this would always throw up FALSE, even if I supply a word which is in the list? TIA Adam Code (with some of my debugging - so this is not the finished article). #!/usr/bin/env python from random import * from cgi import * import cgitb; cgitb.enable() import string import MySQLdb print "Content-type: text/html\n\n" try: connection = MySQLdb.connect(host="learningtimes.db",user="adcripps", passwd="ed3oq3zu", db="times") except MySQLdb.Error, e: print "Error %d %s" %(e.args[0], e.args[1]) sys.exit(1) mycursor = connection.cursor() print """

Join the game

""" print """
""" #Popluate my swear word list from the db. mycursor.execute("select badword from badwords") myrowcount = int(mycursor.rowcount) swearlist = [] for i in range (0, myrowcount): myrow = mycursor.fetchone() swearlist.append(str(myrow)) team = FieldStorage() #Need some kind of swear checker here. #for i in swearlist: # shows swear list OK # print i; checkteam = team.getvalue('team') #print """

"""+str (checkteam) + """

"""; # this works OK #print str(swearlist) # As does this print "name = " + str(team.getvalue('team')) if str(team.getvalue('team')) in swearlist: # This is fail! print "swearing" else: print "not swearing" if team.getvalue('team'): if team.getvalue('team') in swearlist: #This also is fail. print "swear" sys.exit(0) else: print "no swear" team = team.getvalue('team') statement = """insert into pupil (nickname, points, played, morepoints, moreplayed, footballteam) values ('""" + team + """', 0,0,0,0,0)""" mycursor.execute(statement) print """
You have now joined.
You should now support a team.""" # disconnect the database mycursor.close() connection.close() -- You back your data up on the same planet? http://www.monkeez.org PGP key: 0x7111B833 From stefan_ml at behnel.de Mon Apr 6 08:49:36 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Mon, 06 Apr 2009 08:49:36 +0200 Subject: [Tutor] Trouble Parsing XML using lxml In-Reply-To: <49312.64.252.205.230.1238807122.squirrel@webmail1.hrnoc.net> References: <49312.64.252.205.230.1238807122.squirrel@webmail1.hrnoc.net> Message-ID: marc at marcd.org wrote: > I am trying to parse a structure that looks like: > > {urn:FindingImport}TOOL - GD > {urn:FindingImport}TOOL_VERSION - 2.0.8.8 > {urn:FindingImport}AUTHENTICATED_FINDING - TRUE > {urn:FindingImport}GD_VUL_NAME - Rename Built-in Guest Account > {urn:FindingImport}GD_SEVERITY - 2 > {urn:FindingImport}FINDING - None > {urn:FindingImport}FINDING_ID - V0001115 > {urn:FindingImport}FINDING_STATUS - NF > {urn:FindingImport}TOOL - GD > {urn:FindingImport}TOOL_VERSION - 2.0.8.8 > {urn:FindingImport}AUTHENTICATED_FINDING - TRUE > {urn:FindingImport}GD_VUL_NAME - Rename Built-in Administrator Account > {urn:FindingImport}GD_SEVERITY - 2 > {urn:FindingImport}FINDING - None > {urn:FindingImport}FINDING_ID - V0001117 > > This is the result when the original data is run through 'for element in > root.iter():' as described in the lxml tutorial. Note that this does not give you the "structure" (i.e. the hierarchy of elements) but only the plain elements in document order. XML is a tree structure that has elements at the same level and child-parent relationships between elements at different hierarchy levels. > This structure repeats > many times in the document with different values after each tag. I want > to take the values and place them in one csv line for each structure in > the file. The closest I have come is something like (but doesn't work): > > for element in root.iter("{urn:FindingImport}TOOL"): > print element.text > print element.getnext().text > print element.getnext().text > > The initial print element.tag and the first element.getnext().text work as > I would like, but I am not finding a way to parse past that. The second > element.getnext().text returns the value for the same tag as the one prior > to it. .getnext() returns the sibling of the element, not its child. I assume that "TOOL" is the top-level element of the repeating subtree that you want to extract here. In that case, you can use e.g. element.find("{urn:FindingImport}GD_VUL_NAME") to retrieve the subelement named 'GD_VUL_NAME', or element.findtext("{urn:FindingImport}GD_VUL_NAME") to retrieve its text content directly. You should also take a look at lxml.objectify, which provides a very handy way to deal with the kind of XML that you have here. It will allow you to do this: for tool in root.iter("{urn:FindingImport}TOOL"): print tool.GD_VUL_NAME, tool.FINDING BTW, if all you want is to map the XML to CSV, without any major restructuring in between, take a look at iterparse(). It works a lot like the .iter() method, but iterates during parsing, which allows you to delete subtrees after use to safe memory. Stefan From alan.gauld at btinternet.com Mon Apr 6 10:19:04 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Apr 2009 09:19:04 +0100 Subject: [Tutor] base n fractional References: <50e459210904051336v60dfc6ddt280d3c9c8f6e035b@mail.gmail.com> <50e459210904051717q6b67575dr95a926aba9d03a18@mail.gmail.com> Message-ID: "Chris Castillo" wrote > well i want to use a for loop to work from the right most digit to the > left > most digit at a negative 1 increment. For each digit i want to divide it > by > 1/base and add the next digit. Then I need to take that sum and multiply > it > by 1/b to get the decimal equivalent > > so when I say that to myself i see: > number = 234 > mysum = 0 > for digit in range(len(number) -1, -1, -1): > mysum = (mysum) * (1/base) + int(number[digit]) Lets walk that algorithm for base 10... Loop 1 - mysum = 0 * 1/10 + 4 => 4 Loop 2 - mysum = 4 * 1/10 + 3 => 3.4 Loop 3 - mysum = 3.4 * 1/10 +2 => 2.34 That's not what you were looking for I suspect? You still need the final division... But I still think its easier to get the digits directly using >> for digit in reversed(list('234')): And the calculation then becomes: mysum = mysum * (1/base) + int(digit) But that gives the same result. I think you want: mysum = (mysum + int(digit))/base So walking the loop gives loop 1 - mysum = (0 + 4)/10 -> 0.4 loop 2 - mysum = (0.4 + 3)/10 -> 0.34 loop 3 - mysum = (0.34 + 2)/10 -> 0.234 However I still think converting to int and then dividing by base**len(n) is easier. mysum = int('234')/10**len('234') -> 234/1000 -> 0.234 Is there a particular reason for you using the looping algorithm rather than the tools in Python? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From kent37 at tds.net Mon Apr 6 12:30:44 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 6 Apr 2009 06:30:44 -0400 Subject: [Tutor] Checking for string in a list strangeness In-Reply-To: References: Message-ID: <1c2a2c590904060330n42d0132didb9cab2808a2b66@mail.gmail.com> On Mon, Apr 6, 2009 at 3:30 AM, AdamC wrote: > I'm writing a small cgi application for children to use and I want to > check that the name they enter isn't a swear word. > #for i in swearlist: ? ? ? # shows swear list OK > # ? ?print i; Perhaps the words in swearlist include some whitespace? Try for i in swearlist: print repr(i) and look for leading and trailing spaces, tabs, etc. Kent From davea at ieee.org Mon Apr 6 14:53:28 2009 From: davea at ieee.org (Dave Angel) Date: Mon, 06 Apr 2009 08:53:28 -0400 Subject: [Tutor] base n fractional In-Reply-To: References: Message-ID: <49D9FB48.3030603@ieee.org> Chris Castillo wrote: > Message: 1 > Date: Sun, 5 Apr 2009 15:36:09 -0500 > From: Chris Castillo > Subject: [Tutor] base n fractional > To: tutor at python.org > Message-ID: > <50e459210904051336v60dfc6ddt280d3c9c8f6e035b at mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > I need some help converting the fractional (right side of the decimal) to > base 10. > I have this but it doesn't work > > mynum = raw_input("Please enter a number: ") > myint, myfrac = mynum.split(".") > base = raw_input("Please enter the base you would like to convert to: ") > base = int(base) > mydecfrac = 0 > fraclen = len(myfrac) - 1 > > for digit in range(len(myfrac) -1, -1, -1): > mydecfrac = mydecfrac + int(myfrac[digit]) > mydecfrac = mydecfrac / base > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > First we need a clear statement (with examples) of your goal. Your prompts to the user indicate you want to convert from decimal to some other base. But your comments here and elsewhere on the thread indicate the exact opposite. The two problems are related, but mixing them will just confuse everybody. > so when I say that to myself i see: > number = 234 > mysum = 0 > for digit in range(len(number) -1, -1, -1): > mysum = (mysum) * (1/base) + int(number[digit]) This just isn't valid python. You can't subscript an integer. You probably need a string here. That is what raw_input() would produce. So let's get a specific example, and try to work with it. Perhaps you want base = 5 myfrac = "234" and you want to figure the value that .234 would mean if it's interpreted as base 5. First, let's do it by hand. The two is in the first digit to the right of the decimal place, and therefore represents 2/5 The three is in the next place, and represents 3/25 And the four is in the next place and represents 4/125 Result is 0.552 decimal There are two ways to work a base conversion. One is to do the arithmetic in the source base, and do successive multiplies of the destination base. That would mean working in base 5 in this case, which is probably more work in Python. The other is to work in the result base, and do the multiplies of the source base. That's the approach you were taking, and it works great if the precision of a float is acceptable. Your code is fine, although a bit convoluted. Only problem is that you're working in integers, when you need float. So just change mydecfrac to 0.0 and it'll work. From kent37 at tds.net Mon Apr 6 14:54:17 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 6 Apr 2009 08:54:17 -0400 Subject: [Tutor] Checking for string in a list strangeness In-Reply-To: References: <1c2a2c590904060330n42d0132didb9cab2808a2b66@mail.gmail.com> Message-ID: <1c2a2c590904060554w176c7d7dnd0dff61f68383001@mail.gmail.com> On Mon, Apr 6, 2009 at 8:26 AM, AdamC wrote: > 2009/4/6 Kent Johnson : >> On Mon, Apr 6, 2009 at 3:30 AM, AdamC wrote: >>> I'm writing a small cgi application for children to use and I want to >>> check that the name they enter isn't a swear word. >> >>> #for i in swearlist: ? ? ? # shows swear list OK >>> # ? ?print i; >> >> Perhaps the words in swearlist include some ?whitespace? Try >> for i in swearlist: >> ?print repr(i) >> >> and look for leading and trailing spaces, tabs, etc. >> >> Kent > > I think you're on to something here Kent. Thanks. There doesn't appear > to be any whitespaces in the words but an entry in the list appears > like this: > > print swearlist[0] > returns > ('xxxx',) > > where xxxx is the expletive in the database. Ah, yes, the result of mycursor.fetchone() is a tuple containing the fetched elements. Even though you are only reading one field, it is still returned in a tuple. That is what the parentheses and comma signify. Try this: swearlist = [] for i in range (0, myrowcount): myrow = mycursor.fetchone() swearlist.append(myrow[0]) Kent From hihiren1 at gmail.com Mon Apr 6 16:23:25 2009 From: hihiren1 at gmail.com (Kumar) Date: Mon, 6 Apr 2009 19:53:25 +0530 Subject: [Tutor] problem in replacing regex Message-ID: Hello everyone, I have one account number. I want to replace all this account numbers and convert them as URLS. but I don't want to convert existing urls which has account numbers. e.g. 1234-4578 first i convert any existing url into and then I check that if any above number exist then I convert them to my site's url . i am already having regex to check and replace this number. And I can replace all occurrences of this number and convert them into url using re.sub() but problem is that if the value is http://sadfsdf.com/1234-4578 then it first converts http://sadfsdf.com/1234-4578 into http://sadfsdf.com/1234-4578 and then again it converts last number into url so the complete url gets broken. can anybody help me how can I omit the searching existing urls with above number? I have already tried many examples but none of them seems working. Although I can use re.sub('[^/](myregex)','',value) but it won't be a permenant solution as if somebody enters /1234-4578 then also it won't convert it. Thanks, Kumar -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Mon Apr 6 16:41:12 2009 From: bgailer at gmail.com (bob gailer) Date: Mon, 06 Apr 2009 10:41:12 -0400 Subject: [Tutor] Please use plain text. Message-ID: <49DA1488.5030701@gmail.com> An HTML attachment was scrubbed... URL: From bgailer at gmail.com Mon Apr 6 16:56:44 2009 From: bgailer at gmail.com (bob gailer) Date: Mon, 06 Apr 2009 10:56:44 -0400 Subject: [Tutor] Checking for string in a list strangeness In-Reply-To: References: Message-ID: <49DA182C.9070104@gmail.com> An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Mon Apr 6 17:02:44 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Mon, 06 Apr 2009 08:02:44 -0700 Subject: [Tutor] Please use plain text. In-Reply-To: <49DA1488.5030701@gmail.com> References: <49DA1488.5030701@gmail.com> Message-ID: <49DA1994.6020103@sbcglobal.net> I'm curious about this request. What mail reader are you using that causes the problem? What small text is small? Someone's post here, or just a general statement. I just put this msg in plain text, but I see no difference below. Your large and small example looks the same to me. I'm using SeaMonkey. Personally, I'd rather see these mail lists turn into forums formats. Here's an example. . This is about the best forum format I know. Maybe the difference is "they" seem to have a lot of bucks, but here we are using old inexpensive technology. There are simpler versions of the forum cited. Lots of software and hardware mfgers use them. bob gailer wrote: > Please use plain text rather than formatted text. > > Small font size is hard for me to read. > > So is large! > > > -- > Bob Gailer > Chapel Hill NC > 919-636-4239 > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)**** "Less than all cannot satisfy Man." -- William Blake From denis.spir at free.fr Mon Apr 6 17:06:36 2009 From: denis.spir at free.fr (spir) Date: Mon, 6 Apr 2009 17:06:36 +0200 Subject: [Tutor] problem in replacing regex In-Reply-To: References: Message-ID: <20090406170636.7b9d613f@o> Le Mon, 6 Apr 2009 19:53:25 +0530, Kumar s'exprima ainsi: > Hello everyone, > > I have one account number. I want to replace all this account numbers and > convert them as URLS. > > but I don't want to convert existing urls which has account numbers. [...] I'm not sure of what you expect, what you try, what you get. Probably you could be more helpful in providing examples of all of that. > but problem is that if the value is http://sadfsdf.com/1234-4578 then it > first converts http://sadfsdf.com/1234-4578 into http://sadfsdf.com/1234-4578 and then > again it converts last number into url so the complete url gets broken. What I'm sure is that there's something wrong in what you state above. regex.sub() works as expected even with a replacement substring that matches the pattern: >>> import re >>> print re.sub('x', 'x--x', "___x___x___") '___x--x___x--x___' [call: re.sub(format, replacement, source)] As you see, 'x' in the replacement substring will *not* be re-matched and re-replaced. Maybe I did not understand your issue? > Thanks, > Kumar denis ------ la vita e estrany From bgailer at gmail.com Mon Apr 6 17:23:03 2009 From: bgailer at gmail.com (bob gailer) Date: Mon, 06 Apr 2009 11:23:03 -0400 Subject: [Tutor] Please use plain text. In-Reply-To: <49DA1994.6020103@sbcglobal.net> References: <49DA1488.5030701@gmail.com> <49DA1994.6020103@sbcglobal.net> Message-ID: <49DA1E57.4050209@gmail.com> An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: moz-screenshot-11.jpg Type: image/jpeg Size: 6292 bytes Desc: not available URL: From elena.valhalla at gmail.com Mon Apr 6 18:52:23 2009 From: elena.valhalla at gmail.com (Elena of Valhalla) Date: Mon, 6 Apr 2009 18:52:23 +0200 Subject: [Tutor] Please use plain text. In-Reply-To: <49DA1994.6020103@sbcglobal.net> References: <49DA1488.5030701@gmail.com> <49DA1994.6020103@sbcglobal.net> Message-ID: <5c5e5c350904060952w40c4d640ia084915f30cd58d3@mail.gmail.com> On Mon, Apr 6, 2009 at 5:02 PM, Wayne Watson wrote: > I'm curious about this request. What mail reader are you using that causes > the problem? What small text is small? the same thing happens with gmail, and afaik almost every other graphical email client that supports html emails. With text based clients it's often even worse, since they don't support it at all and show the html code. On some lists there is the option to strip such formatting, maybe you don't see it because you configured it so. > Personally, I'd rather see these mail lists turn into forums formats. [...] please no! I can follow a mailing list from almost everywhere, as long as I have a cell phone, but I need a proper internet connection and a proper pc to follow a forum like that > This is about the best forum format I know. Maybe the difference is "they" > seem to have a lot of bucks, but here we are using old inexpensive > technology. There are simpler versions of the forum cited. Lots of software > and hardware mfgers use them. There are various free (as in speech) software forum managers, that example is using one (phpbb) and it's not superior expensive technology, it's just a different, IMHO inferior, technology that works for communities and general chatting, but is not as versatile as a mailing list for technical discussions -- Elena ``of Valhalla'' email: elena.valhalla at gmail.com From ctcast at gmail.com Mon Apr 6 19:04:56 2009 From: ctcast at gmail.com (Chris Castillo) Date: Mon, 6 Apr 2009 12:04:56 -0500 Subject: [Tutor] base n fractional In-Reply-To: <49D9FB48.3030603@ieee.org> References: <49D9FB48.3030603@ieee.org> Message-ID: <50e459210904061004n27d31407q3df4b1003d36045b@mail.gmail.com> yes that was what I was looking for. And the reason I am using the loop is the teacher I am currently taking wants us to fully comprehend iteration for the material we are covering in the class. Thank you for your help though, I definitely see where I was going wrong with it. On Mon, Apr 6, 2009 at 7:53 AM, Dave Angel wrote: > Chris Castillo wrote: > > Message: 1 >> Date: Sun, 5 Apr 2009 15:36:09 -0500 >> From: Chris Castillo >> Subject: [Tutor] base n fractional >> To: tutor at python.org >> Message-ID: >> <50e459210904051336v60dfc6ddt280d3c9c8f6e035b at mail.gmail.com> >> Content-Type: text/plain; charset="iso-8859-1" >> >> I need some help converting the fractional (right side of the decimal) to >> base 10. >> I have this but it doesn't work >> >> mynum = raw_input("Please enter a number: ") >> myint, myfrac = mynum.split(".") >> base = raw_input("Please enter the base you would like to convert to: ") >> base = int(base) >> mydecfrac = 0 >> fraclen = len(myfrac) - 1 >> >> for digit in range(len(myfrac) -1, -1, -1): >> mydecfrac = mydecfrac + int(myfrac[digit]) >> mydecfrac = mydecfrac / base >> -------------- next part -------------- >> An HTML attachment was scrubbed... >> URL: < >> http://mail.python.org/pipermail/tutor/attachments/20090405/612ca99f/attachment-0001.htm >> > >> >> > First we need a clear statement (with examples) of your goal. Your prompts > to the user indicate you want to convert from decimal to some other base. > But your comments here and elsewhere on the thread indicate the exact > opposite. The two problems are related, but mixing them will just confuse > everybody. > > so when I say that to myself i see: >> number = 234 >> mysum = 0 >> for digit in range(len(number) -1, -1, -1): >> mysum = (mysum) * (1/base) + int(number[digit]) >> > > This just isn't valid python. You can't subscript an integer. You > probably need a string here. That is what raw_input() would produce. > > So let's get a specific example, and try to work with it. > > > Perhaps you want > base = 5 > myfrac = "234" > > and you want to figure the value that .234 would mean if it's interpreted > as base 5. First, let's do it by hand. > The two is in the first digit to the right of the decimal place, and > therefore represents 2/5 > The three is in the next place, and represents 3/25 > And the four is in the next place and represents 4/125 > Result is 0.552 decimal > > There are two ways to work a base conversion. One is to do the arithmetic > in the source base, and do successive multiplies of the destination base. > That would mean working in base 5 in this case, which is probably more work > in Python. The other is to work in the result base, and do the multiplies > of the source base. That's the approach you were taking, and it works great > if the precision of a float is acceptable. > > > Your code is fine, although a bit convoluted. Only problem is that you're > working in integers, when you need float. So just change mydecfrac to 0.0 > and it'll work. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From tmz at pobox.com Mon Apr 6 19:30:42 2009 From: tmz at pobox.com (Todd Zullinger) Date: Mon, 6 Apr 2009 13:30:42 -0400 Subject: [Tutor] Please use plain text. In-Reply-To: <49DA1488.5030701@gmail.com> References: <49DA1488.5030701@gmail.com> Message-ID: <20090406173041.GQ17135@inocybe.teonanacatl.org> bob gailer wrote: > Please use plain text rather than formatted text. Was sending this request in an html formatted message intentional? I don't know about most folks, but I consider plain text to mean a content-type of text/plain rather than text/html. :) -- Todd OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Unquestionably, there is progress. The average American now pays out twice as much in taxes as he formerly got in wages. -- H. L. Mencken -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 542 bytes Desc: not available URL: From i-scream_cones at hotmail.com Mon Apr 6 21:30:06 2009 From: i-scream_cones at hotmail.com (Chris Lee) Date: Mon, 6 Apr 2009 12:30:06 -0700 Subject: [Tutor] Executing a GUI from another GUI with a click of a button Message-ID: Hello! Before I tell you about my problems, I ask for your consideration as I am a newbie at python... I was having problems as I tried to open another program within a program (both programs are Tkinter UIs) As for the reference, here is my code: #program: realprogram.py from Tkinter import * #Imports the Tkinter module import os def showtime(): os.system("clock.py") window = Tk() frame = Frame(window) frame.pack() clock_command = lambda : showtime() b3 = Button(frame, text="What time is it?", command=clock_command) b3.pack() window.mainloop() And the following code is the clock.py: #program: clock.py import Tkinter import time currenttime = '' clock = Tkinter.Label() clock.pack() def tick(): global currenttime newtime = time.strftime('%H:%M:%S') if newtime != currenttime: currenttime = newtime clock.config(text=currenttime) clock.after(200, tick) tick() clock.mainloop() I know for certain that the "clock.py" works (because i got it from the web.. -_-;;) but I'm having a real difficulty trying to open clock.py through my real program.... I know you're busy and everything, but I REALLY hope you can help me soon... _________________________________________________________________ Create a cool, new character for your Windows Live? Messenger. http://go.microsoft.com/?linkid=9656621 -------------- next part -------------- An HTML attachment was scrubbed... URL: From srilyk at gmail.com Mon Apr 6 21:59:39 2009 From: srilyk at gmail.com (W W) Date: Mon, 6 Apr 2009 14:59:39 -0500 Subject: [Tutor] Executing a GUI from another GUI with a click of a button In-Reply-To: References: Message-ID: <333efb450904061259g452112e9qff765c5d41a1db8f@mail.gmail.com> On Mon, Apr 6, 2009 at 2:30 PM, Chris Lee wrote: > > > I know for certain that the "clock.py" works (because i got it from the > web.. -_-;;) > > but I'm having a real difficulty trying to open clock.py through my real > program.... > > I know you're busy and everything, but I REALLY hope you can help me > soon... > One option would be to use subprocess. import subprocess as sp sp.call('python myfile.py') that's probably the simplest way to do it. Good luck, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Apr 6 23:01:11 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Apr 2009 22:01:11 +0100 Subject: [Tutor] Please use plain text. References: <49DA1488.5030701@gmail.com> <49DA1994.6020103@sbcglobal.net> Message-ID: "Wayne Watson" wrote > Personally, I'd rather see these mail lists turn into forums formats. Web forums are far less useful than mailing lists for technical groups. They require internet access and a browser to read them. Many require a graphical browser so don't work in text mode terminals. Very few are available via news feeds (and therefore miss out on the power of newsreaders for effective bulk reading) and many have quite poor search and archive capabilities. Many programmers work in mixed environments where graphical browers are non existent, or they are offline for extended periods. Similarly those who have to process many hundreds of messages a day tend to find news feeds more effective. There are python web forums(fora?) but I confess I don;t read any of them. But I do read half a dozen of the mailing lists (all via news feeds). Of course it may just be what I'm used to from the early days of the net, but I've yet to find a web forum that provides anything like the functionality of a newsgroup or the flexibility of a mailing list! Sadly forums do seem to be slowly taking over... And of course the mailing lists are easily readable on the web too via gmane, Activestate or the official archives. Alan G. From alan.gauld at btinternet.com Tue Apr 7 00:38:14 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Apr 2009 23:38:14 +0100 Subject: [Tutor] Executing a GUI from another GUI with a click of a button References: Message-ID: "Chris Lee" wrote > Before I tell you about my problems, I ask for your consideration > as I am a newbie at python... That's OK, its what the tutor list is for! :-) > I was having problems as I tried to open another program > within a program (both programs are Tkinter UIs) OK, That can be tricky, especially if you want the two programs to communicate but it is possible. > def showtime(): > os.system("clock.py") > > window = Tk() > frame = Frame(window) > frame.pack() > clock_command = lambda : showtime() > b3 = Button(frame, text="What time is it?", command=clock_command) Why not miss out the lambda and just do: b3 = Button(frame, text="What time is it?", command=showtime) lambdas are there to save you creating names for small one-liner type programs. In you case the onle liner just calls another function to you might as well just use the function directly as your command. > b3.pack() > window.mainloop() > And the following code is the clock.py: > ------------------------ #program: clock.py import Tkinter import time currenttime = '' clock = Tkinter.Label() clock.pack() def tick(): global currenttime newtime = time.strftime('%H:%M:%S') if newtime != currenttime: currenttime = newtime clock.config(text=currenttime) clock.after(200, tick) tick() clock.mainloop() > ----------------------- > I know for certain that the "clock.py" works (because i got it from the > web.. -_-;;) And you ran it from a command prompt and it worked ok? Its a very unusual Tkinter program in that it doesn't have any of the usual Tkinter preamble... > but I'm having a real difficulty trying to open clock.py through my real > program.... That might be bbecause you are calling clock.py directly instead of invoking the interpreter. That may depend on the Operating system you are using however... Can you tell us what does happen? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From MPirritano at ochca.com Tue Apr 7 00:48:05 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Mon, 6 Apr 2009 15:48:05 -0700 Subject: [Tutor] unicode to plain text conversion In-Reply-To: References: Message-ID: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> Hello python people, I am a total newbie. I have a very large file > 4GB that I need to convert from Unicode to plain text. I used to just use dos when the file was < 4GB but it no longer seems to work. Can anyone point me to some python code that might perform this function? Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: tutor-bounces+mpirritano=ochca.com at python.org [mailto:tutor-bounces+mpirritano=ochca.com at python.org] On Behalf Of AdamC Sent: Monday, April 06, 2009 12:31 AM To: Python Tutor Subject: [Tutor] Checking for string in a list strangeness I'm writing a small cgi application for children to use and I want to check that the name they enter isn't a swear word. I've populated a database with a swear word list and then check this list against the input, but it always shows up as not being a swear word. Can anyone see why this would always throw up FALSE, even if I supply a word which is in the list? TIA Adam Code (with some of my debugging - so this is not the finished article). #!/usr/bin/env python from random import * from cgi import * import cgitb; cgitb.enable() import string import MySQLdb print "Content-type: text/html\n\n" try: connection = MySQLdb.connect(host="learningtimes.db",user="adcripps", passwd="ed3oq3zu", db="times") except MySQLdb.Error, e: print "Error %d %s" %(e.args[0], e.args[1]) sys.exit(1) mycursor = connection.cursor() print """

Join the game

""" print """ """ #Popluate my swear word list from the db. mycursor.execute("select badword from badwords") myrowcount = int(mycursor.rowcount) swearlist = [] for i in range (0, myrowcount): myrow = mycursor.fetchone() swearlist.append(str(myrow)) team = FieldStorage() #Need some kind of swear checker here. #for i in swearlist: # shows swear list OK # print i; checkteam = team.getvalue('team') #print """

"""+str (checkteam) + """

"""; # this works OK #print str(swearlist) # As does this print "name = " + str(team.getvalue('team')) if str(team.getvalue('team')) in swearlist: # This is fail! print "swearing" else: print "not swearing" if team.getvalue('team'): if team.getvalue('team') in swearlist: #This also is fail. print "swear" sys.exit(0) else: print "no swear" team = team.getvalue('team') statement = """insert into pupil (nickname, points, played, morepoints, moreplayed, footballteam) values ('""" + team + """', 0,0,0,0,0)""" mycursor.execute(statement) print """
You have now joined.
You should now support a team.""" # disconnect the database mycursor.close() connection.close() -- You back your data up on the same planet? http://www.monkeez.org PGP key: 0x7111B833 _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor From wescpy at gmail.com Tue Apr 7 01:39:54 2009 From: wescpy at gmail.com (wesley chun) Date: Mon, 6 Apr 2009 16:39:54 -0700 Subject: [Tutor] unicode to plain text conversion In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> References: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> Message-ID: <78b3a9580904061639v18cf3b45r8ace64339ea4de84@mail.gmail.com> > I am a total newbie. I have a very large file > 4GB that I need to > convert from Unicode to plain text. I used to just use dos when the file > was < 4GB but it no longer seems to work. Can anyone point me to some > python code that might perform this function? can you elaborate on your conversion technique when you "used to just use dos" when the file was at an appropriate size? also let us know what encodings are being used during your conversion too. code snippets would be great, as well as current errors/exceptions that you're now getting because of the file sizes. thanks, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From MPirritano at ochca.com Tue Apr 7 01:43:42 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Mon, 6 Apr 2009 16:43:42 -0700 Subject: [Tutor] unicode to plain text conversion In-Reply-To: <78b3a9580904061639v18cf3b45r8ace64339ea4de84@mail.gmail.com> References: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> <78b3a9580904061639v18cf3b45r8ace64339ea4de84@mail.gmail.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC908186127@HCAMAIL03.ochca.com> Previously I was able to convert just by using: Type Unicode_filename.txt > new_text_file.txt That's it. Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: wesley chun [mailto:wescpy at gmail.com] Sent: Monday, April 06, 2009 4:40 PM To: Pirritano, Matthew Cc: Python Tutor Subject: Re: [Tutor] unicode to plain text conversion > I am a total newbie. I have a very large file > 4GB that I need to > convert from Unicode to plain text. I used to just use dos when the file > was < 4GB but it no longer seems to work. Can anyone point me to some > python code that might perform this function? can you elaborate on your conversion technique when you "used to just use dos" when the file was at an appropriate size? also let us know what encodings are being used during your conversion too. code snippets would be great, as well as current errors/exceptions that you're now getting because of the file sizes. thanks, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From wescpy at gmail.com Tue Apr 7 01:57:32 2009 From: wescpy at gmail.com (wesley chun) Date: Mon, 6 Apr 2009 16:57:32 -0700 Subject: [Tutor] unicode to plain text conversion In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC908186127@HCAMAIL03.ochca.com> References: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> <78b3a9580904061639v18cf3b45r8ace64339ea4de84@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC908186127@HCAMAIL03.ochca.com> Message-ID: <78b3a9580904061657k28194f41t92a6addde64f069e@mail.gmail.com> > Previously I was able to convert just by using: > Type Unicode_filename.txt > new_text_file.txt > That's it. wow, if that's all you had to do, i'm not sure it's worthwhile to learning a new programming language just to process it with an application when your original solution was so dead simple. i'd be more interested in finding clever ways of breaking up the oversized files into smaller chunks in order to have a DOS batch script make that call to "type" for each file chunk. -wesley From sifis_21 at hotmail.com Mon Apr 6 22:21:44 2009 From: sifis_21 at hotmail.com (Sifis Sifakis) Date: Mon, 6 Apr 2009 23:21:44 +0300 Subject: [Tutor] (no subject) Message-ID: Hi, I'm a student in Applied Informatics and i need to make a project in Python. More specificaly,i have to create a simple online bookstore using Python. While not having previous experience, I have problems to combine Pyhton with Html code. My question is ,I must include Python code as scripts into Html files or instead I must include Html code into Python files?Could you also send me some sample code of a webpage(online store) including Python in order your advise to be more understandable to me? Thank you in advance. _________________________________________________________________ News, entertainment and everything you care about at Live.com. Get it now! http://www.live.com/getstarted.aspx -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Apr 7 02:51:24 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 6 Apr 2009 20:51:24 -0400 Subject: [Tutor] unicode to plain text conversion In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> References: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> Message-ID: <1c2a2c590904061751r3dfc3fb9m927f184d165c3a77@mail.gmail.com> On Mon, Apr 6, 2009 at 6:48 PM, Pirritano, Matthew wrote: > Hello python people, > > I am a total newbie. I have a very large file > 4GB that I need to > convert from Unicode to plain text. I used to just use dos when the file > was < 4GB but it no longer seems to work. Can anyone point me to some > python code that might perform this function? What is the encoding of the Unicode file? Assuming that the file has lines that will each fit in memory, you can use the codecs module to decode the unicode. Something like this: import codecs inp = codecs.open('Unicode_file.txt', 'r', 'utf-16le') outp = open('new_text_file.txt') outp.writelines(inp) inp.close() outp.close() The above code assumes UTF-16LE encoding, change it to the correct one if that is not right. A list of supported encodings is here: http://docs.python.org/library/codecs.html#id3 Kent From david at abbottdavid.com Tue Apr 7 03:29:06 2009 From: david at abbottdavid.com (David) Date: Mon, 06 Apr 2009 21:29:06 -0400 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <49DAAC62.5020901@abbottdavid.com> Sifis Sifakis wrote: > Hi, > I'm a student in Applied Informatics and i need to make a project in > Python. More specificaly,i have to create a simple online bookstore > using Python. I would use Google App Engine; http://code.google.com/appengine/docs/python/gettingstarted/ -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From sierra_mtnview at sbcglobal.net Tue Apr 7 06:11:37 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Mon, 06 Apr 2009 21:11:37 -0700 Subject: [Tutor] Please use plain text. In-Reply-To: <49DA1E57.4050209@gmail.com> References: <49DA1488.5030701@gmail.com> <49DA1994.6020103@sbcglobal.net> <49DA1E57.4050209@gmail.com> Message-ID: <49DAD279.2000305@sbcglobal.net> An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/jpeg Size: 6292 bytes Desc: not available URL: From metolone+gmane at gmail.com Tue Apr 7 06:23:30 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Mon, 6 Apr 2009 21:23:30 -0700 Subject: [Tutor] Please use plain text. References: <49DA1488.5030701@gmail.com> <49DA1994.6020103@sbcglobal.net><49DA1E57.4050209@gmail.com> <49DAD279.2000305@sbcglobal.net> Message-ID: Signature.htmlI normally reply in whatever the original poster uses, because when I convert HTML to plain text it removes all the attribution indentation, making it difficult to tell who wrote what (see below...I converted this post back to plain text). HTML is generally painful to read unless your editor completely supports editing in it (mine doesn't), so I second the motion to post only plain text. -Mark "Wayne Watson" wrote in message news:49DAD279.2000305 at sbcglobal.net... That's strange--tiny image below. I use SeaMonkey, and it seems to be almost identical to Tbird. I tend to use html for my signature. I'm not sure what someone did to produce the tiny print below. Ah, I see, they apparently grabbed the image, and inserted it. When I grab images with SnagIt, I have the text size set to something much more reasonable than I see here. Maybe someone has exceptionally good eyes. Occasionally, when I print, e-mail, not my own, I get tiny letters. That's solvable (Win XP) by pumping up the size in Print Preview. I still find it odd, and it seems to happen randomly. bob gailer wrote: Wayne Watson wrote: I'm curious about this request. What mail reader are you using that causes the problem? I use Thunderbird. It is not the cause of the problem. It renders emails exactly as formatted by the sender. The specific example I was reacting to was this morning 1:08 AM: Re: [Tutor] Formatting zip module arguments correctly metolone+gmane at gmail.com In hopes that you can see graphics, here is an extract: [snip] -- Bob Gailer Chapel Hill NC 919-636-4239 -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) "Less than all cannot satisfy Man." -- William Blake _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor From the at internethamster.com Tue Apr 7 06:43:39 2009 From: the at internethamster.com (David Duncan) Date: Mon, 6 Apr 2009 23:43:39 -0500 Subject: [Tutor] (no subject) In-Reply-To: <49DAAC62.5020901@abbottdavid.com> References: <49DAAC62.5020901@abbottdavid.com> Message-ID: On Mon, Apr 6, 2009 at 8:29 PM, David wrote: > Sifis Sifakis wrote: >> >> Hi, >> I'm a student in Applied Informatics and i need to make a project in >> Python. More specificaly,i have to create a simple online bookstore using >> Python. > > I would use Google App Engine; > > http://code.google.com/appengine/docs/python/gettingstarted/ > I would suggest that you read the howto in the documentation on Python on the web. http://docs.python.org/dev/howto/webservers.html or go to the web programming wiki for more information on web work. http://wiki.python.org/moin/WebProgramming David. -- David Duncan Austin, TX From denis.spir at free.fr Tue Apr 7 10:32:49 2009 From: denis.spir at free.fr (spir) Date: Tue, 7 Apr 2009 10:32:49 +0200 Subject: [Tutor] Please use plain text. In-Reply-To: References: <49DA1488.5030701@gmail.com> <49DA1994.6020103@sbcglobal.net> Message-ID: <20090407103249.1ea46ccd@o> Le Mon, 6 Apr 2009 22:01:11 +0100, "Alan Gauld" s'exprima ainsi: > Web forums are far less useful than mailing lists for technical groups. > They require internet access and a browser to read them. > Many require a graphical browser so don't work in text mode > terminals. Very few are available via news feeds (and therefore > miss out on the power of newsreaders for effective bulk reading) > and many have quite poor search and archive capabilities. The main difference for me is about organisation. Forums are pre-structured as categories and threads. You constantly need to navigate here and there in an organisation that does not necessarily mirrors the content or your own views. It's often even hard and long just to review new posts. It's very time consuming for few benefit. The so-called 'forum' structure works better as an archive (provided the users use it well) than as... a forum in the basic sense of the term. With a list on the other hand one gets all news basically as a flat... list. This mirrors what a forum really is: a place where everybody can speak. This also allows a fast review even at hand. Custom selection, filtering, dispatch can be applied manually or automatically on top of this non-structured flow, precisely because it is not pre-structured. I nearly never visit forums. Denis ------ la vita e estrany From alan.gauld at btinternet.com Tue Apr 7 10:41:38 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 7 Apr 2009 09:41:38 +0100 Subject: [Tutor] unicode to plain text conversion References: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> Message-ID: "Pirritano, Matthew" wrote > I am a total newbie. I have a very large file > 4GB that I need to > convert from Unicode to plain text. I used to just use dos when the file > was < 4GB but it no longer seems to work. Can anyone point me to some > python code that might perform this function? When you say "DOS" what exactly do you mean? Are you really running an old DOS operating system or are you starting a DOS box within a Windows variant? If the latter and you are using COMMAND to start the session you will hit the 4GB limit but if you use CMD (under Win 2000/XP/Vista) I think it should work as before. (Although MS may have been too clever for you and updated the type command to work with unicode!) Worth a try... Alan G. From alan.gauld at btinternet.com Tue Apr 7 10:49:28 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 7 Apr 2009 09:49:28 +0100 Subject: [Tutor] (no subject) References: Message-ID: "Sifis Sifakis" wrote > I'm a student in Applied Informatics and i need to make a project in > Python. > More specificaly,i have to create a simple online bookstore using Python. We don;t do homework for you but we are happy to help with pointers and bug fixing. > Could you also send me some sample code of a webpage(online store) > including Python in order your advise to be more understandable to me? You need to learn about the Common Gateway Interface or CGI. This is the basis of all web server programming and Python supports it in its most basic form via the cgi module. There is a heap of documentation on the Python web site about web programming and the cgi module docs show simple examples. For a more complex project like a bookstore you might want to consider one of the web frameworks like Pylons, Django or TurboGears. But the CGI stuff lies under all of them. Once you get started come back here for help when things don't work or you get stuck. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sierra_mtnview at sbcglobal.net Tue Apr 7 11:26:50 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Tue, 07 Apr 2009 02:26:50 -0700 Subject: [Tutor] Please use plain text. In-Reply-To: <20090407103249.1ea46ccd@o> References: <49DA1488.5030701@gmail.com> <49DA1994.6020103@sbcglobal.net> <20090407103249.1ea46ccd@o> Message-ID: <49DB1C5A.7020008@sbcglobal.net> An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Tue Apr 7 11:29:47 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Tue, 07 Apr 2009 02:29:47 -0700 Subject: [Tutor] [AstroPy] numarray pdf 1.5 is broken. Using numdisplay, Python, PIL, and Tkinter In-Reply-To: References: <49D98CD4.3090508@sbcglobal.net> <49D9BC1C.4040805@lpta.in2p3.fr> <49D9F65D.9010804@sbcglobal.net> <49DA0952.8070709@stsci.edu> <49DA16B4.6080002@sbcglobal.net> <49DA1AE3.5080902@stsci.edu> <49DA4E15.4080109@lpta.in2p3.fr> Message-ID: <49DB1D0B.3000806@sbcglobal.net> An HTML attachment was scrubbed... URL: From denis.spir at free.fr Tue Apr 7 11:29:33 2009 From: denis.spir at free.fr (spir) Date: Tue, 7 Apr 2009 11:29:33 +0200 Subject: [Tutor] problem in replacing regex In-Reply-To: References: <20090406170636.7b9d613f@o> Message-ID: <20090407112933.47478155@o> [forwarded to list] Le Tue, 7 Apr 2009 12:23:33 +0530, Kumar s'exprima ainsi: > Hi denis, > > Thanks a lot for the reply. > > Actually on our web application when we display the data, at that time we do > parsing and make hyperlinks (through ) wherever possible. so if there is > any url like (http://www.hello.com) then while displaying data we convert it > to http://www.hello.com > and if we find any account number then we make them to go to our default > account page > e.g. text is "I am using 12345-45". then while viewing we replace it with > following. > I am using 12345-45 > > I hope above example would clear your problem. > > now my problem is first i try to convert all existing link to tag. this > work perfectly fine. > so e.g. the value is "I am using this url http://hello.com/accid/12345-45" > then as per above algorithm it works perfectly find and change it to > following. > I am using this url > http://hello.com/accid/12345-45 > now after that i again replace all accids to convert into url so above value > become followign > I am using this url http://hello.com/accid/12345-45">12345-45">http://hello.com/accid= href="http://hello.com/accid/12345-45">12345-45 > > and the complete link is messed up. > so while converting the accids into url i want to exclude the values which > start with http (e.g. http://hello.com/accid/12345-45) > > i hope it becomes more clear now. > one solution i have is i can exclude the accids start with / i.e. / > http://hello.com/accid/12345-45 but that is not perfect solution. > > Any pointer would be very helpful. > > Thanks, > Kumar Ok, now I understand. You need to convert both url-s and account numbers to html encoded links. Whatever the order you choose, numbers will be double-encoded. My solution (maybe not the best) would be to process both operations in one go, using a pattern that matches both and a smarter link writer func able to distinguish an url from a number. Pseudo code: toLinkPattern = re.compile("(urlFormat)|(accountNumFormat)") def toLink(match): string = match.group() if isAccountNum(string): return accountNumToLink(string) return urlToLink(string) result = toLinkPattern.sub(toLink, source) To make things easier, note that using groups() instead or group() will also tell you what kind of thing has been matched due to the position in match tuple. EG: import re pat = re.compile("([1-9])|([a-z])") print pat.findall("a1b2c") def replace(match): print match.group(), match.groups() (digit, letter) = (match.groups()[0],match.groups()[1]) print "digit:%s letter:%s" %(digit,letter) if digit is not None: return "0" return '@' print pat.sub(replace,"a1b2c") ==> [('', 'a'), ('1', ''), ('', 'b'), ('2', ''), ('', 'c')] a (None, 'a') digit:None letter:a 1 ('1', None) digit:1 letter:None b (None, 'b') digit:None letter:b 2 ('2', None) digit:2 letter:None c (None, 'c') digit:None letter:c @0 at 0@ You can also use named groups: pat = re.compile("(?P\d)|(?P[a-z])") def replace(match): digit,letter = (match.group('digit'),match.group('letter')) print "digit:%s letter:%s" %(digit,letter) if digit is not None: # or better directly: if match.group('digit') is not None: return "0" return '@' print pat.sub(replace,"a1b2c") ==> digit:None letter:a digit:1 letter:None digit:None letter:b digit:2 letter:None digit:None letter:c @0 at 0@ Denis ------ la vita e estrany From sander.sweers at gmail.com Tue Apr 7 13:35:45 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Tue, 7 Apr 2009 13:35:45 +0200 Subject: [Tutor] Please use plain text. In-Reply-To: <49DB1C5A.7020008@sbcglobal.net> References: <49DA1488.5030701@gmail.com> <49DA1994.6020103@sbcglobal.net> <20090407103249.1ea46ccd@o> <49DB1C5A.7020008@sbcglobal.net> Message-ID: 2009/4/7 Wayne Watson : > What I would consider a big difficulty with flat text is exactly that it is > flat. There are lots of times, in my experience, when a graphic, > particularly captured ones, can lead to much faster and smarter solution. I > think this list may allow them, but it seems at a delay. The problem lies not with plain text but how people write text. If one structures their e-mails properly it does not make any difference if it is in plain text or rtf/html. rtf/html also do not guarantee that your indentation is displayed properly. So if one want to keep using html/rtf I would recommend using a pastebin. > My experience is > the opposite of yours with forums. I use Yahoo Groups fairly often. Although > I don't like the advertising present there, which is pretty minimal, they at > least something of a cross between graphics and text. One can upload images > for examination by others. Some forums and all YGs even allow videos. I've > even used videos to solve some hardware problems. If you need to show your code with proper indentation and syntax highligthing use a pastebin. Also there are many places where one can upload (for free) pictures and videos which then can be linked to in the message. > Maybe mail lists should be > divided into segments. Say, one for people who use text and others for > whatever media they are comfortable with. And this accomplishes that tutors will have to read multiple lists and keep track of which list they are tutoring. If I was one of them I would refuse. It also does not solve the problem highlighted earlier. So I agree with Bob that on *mailing lists* plain text is the preferred format. But other formats should not be _rejected_ as this would scare away potential pythoniasts which goes against the gaol of the mailing list. Anyhow, this discussion has gone way off topic and I suggest all to drop it as it will only lead to flame wars. Greets Sander A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail? From sierra_mtnview at sbcglobal.net Tue Apr 7 15:44:06 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Tue, 07 Apr 2009 06:44:06 -0700 Subject: [Tutor] Please use plain text. In-Reply-To: References: <49DA1488.5030701@gmail.com> <49DA1994.6020103@sbcglobal.net> <20090407103249.1ea46ccd@o> <49DB1C5A.7020008@sbcglobal.net> Message-ID: <49DB58A6.7030804@sbcglobal.net> An HTML attachment was scrubbed... URL: From MPirritano at ochca.com Tue Apr 7 16:44:42 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Tue, 7 Apr 2009 07:44:42 -0700 Subject: [Tutor] unicode to plain text conversion In-Reply-To: References: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC90818618F@HCAMAIL03.ochca.com> I am using run>cmd in windows xp professional. The type command no longer seems to work on this file now that it exceeds 4GB. Only about 63000 lines are converted out of the 2 million plus. How can I find out the type of coding that was used to create this file? Is there a way to do this other than just asking the person who created it? That is possible, but I was just curious. Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: tutor-bounces+mpirritano=ochca.com at python.org [mailto:tutor-bounces+mpirritano=ochca.com at python.org] On Behalf Of Alan Gauld Sent: Tuesday, April 07, 2009 1:42 AM To: tutor at python.org Subject: Re: [Tutor] unicode to plain text conversion "Pirritano, Matthew" wrote > I am a total newbie. I have a very large file > 4GB that I need to > convert from Unicode to plain text. I used to just use dos when the file > was < 4GB but it no longer seems to work. Can anyone point me to some > python code that might perform this function? When you say "DOS" what exactly do you mean? Are you really running an old DOS operating system or are you starting a DOS box within a Windows variant? If the latter and you are using COMMAND to start the session you will hit the 4GB limit but if you use CMD (under Win 2000/XP/Vista) I think it should work as before. (Although MS may have been too clever for you and updated the type command to work with unicode!) Worth a try... Alan G. _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor From billa_lazarus at yahoo.com Tue Apr 7 17:32:59 2009 From: billa_lazarus at yahoo.com (Lazarus billa) Date: Tue, 7 Apr 2009 08:32:59 -0700 (PDT) Subject: [Tutor] Tutor Digest, Vol 62, Issue 15 In-Reply-To: Message-ID: <125843.58070.qm@web56108.mail.re3.yahoo.com> EASTER - 2009 "He is no more in the grave, He is risen" Do you enjoy His presence in your Heart?... Let us reach multitudes with this message. Please involve in our Good Shephered Ministries. Pray and plane to visit and witness to share the greatest mirical of our Lord. Praise Him Hallelujah. With Love and regards, Rev. Billa. Lazarus Good Shepherd Ministries, A.T.Agraharam, Gunatur - 522 004. Andhra Pradesh - South India. bilas999 at yahoo.com Phone: +91 9948189975. --- On Mon, 4/6/09, tutor-request at python.org wrote: From: tutor-request at python.org Subject: Tutor Digest, Vol 62, Issue 15 To: tutor at python.org Date: Monday, April 6, 2009, 7:56 AM Send Tutor mailing list submissions to tutor at python.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to tutor-request at python.org You can reach the person managing the list at tutor-owner at python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: 1. Re: Checking for string in a list strangeness (Kent Johnson) 2. Re: base n fractional (Dave Angel) 3. Re: Checking for string in a list strangeness (Kent Johnson) 4. problem in replacing regex (Kumar) 5. Please use plain text. (bob gailer) 6. Re: Checking for string in a list strangeness (bob gailer) ---------------------------------------------------------------------- Message: 1 Date: Mon, 6 Apr 2009 06:30:44 -0400 From: Kent Johnson Subject: Re: [Tutor] Checking for string in a list strangeness To: AdamC Cc: Python Tutor Message-ID: <1c2a2c590904060330n42d0132didb9cab2808a2b66 at mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 On Mon, Apr 6, 2009 at 3:30 AM, AdamC wrote: > I'm writing a small cgi application for children to use and I want to > check that the name they enter isn't a swear word. > #for i in swearlist: ? ? ? # shows swear list OK > # ? ?print i; Perhaps the words in swearlist include some whitespace? Try for i in swearlist: print repr(i) and look for leading and trailing spaces, tabs, etc. Kent ------------------------------ Message: 2 Date: Mon, 06 Apr 2009 08:53:28 -0400 From: Dave Angel Subject: Re: [Tutor] base n fractional To: tutor at python.org Message-ID: <49D9FB48.3030603 at ieee.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Chris Castillo wrote: > Message: 1 > Date: Sun, 5 Apr 2009 15:36:09 -0500 > From: Chris Castillo > Subject: [Tutor] base n fractional > To: tutor at python.org > Message-ID: > <50e459210904051336v60dfc6ddt280d3c9c8f6e035b at mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > I need some help converting the fractional (right side of the decimal) to > base 10. > I have this but it doesn't work > > mynum = raw_input("Please enter a number: ") > myint, myfrac = mynum.split(".") > base = raw_input("Please enter the base you would like to convert to: ") > base = int(base) > mydecfrac = 0 > fraclen = len(myfrac) - 1 > > for digit in range(len(myfrac) -1, -1, -1): > mydecfrac = mydecfrac + int(myfrac[digit]) > mydecfrac = mydecfrac / base > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > First we need a clear statement (with examples) of your goal. Your prompts to the user indicate you want to convert from decimal to some other base. But your comments here and elsewhere on the thread indicate the exact opposite. The two problems are related, but mixing them will just confuse everybody. > so when I say that to myself i see: > number = 234 > mysum = 0 > for digit in range(len(number) -1, -1, -1): > mysum = (mysum) * (1/base) + int(number[digit]) This just isn't valid python. You can't subscript an integer. You probably need a string here. That is what raw_input() would produce. So let's get a specific example, and try to work with it. Perhaps you want base = 5 myfrac = "234" and you want to figure the value that .234 would mean if it's interpreted as base 5. First, let's do it by hand. The two is in the first digit to the right of the decimal place, and therefore represents 2/5 The three is in the next place, and represents 3/25 And the four is in the next place and represents 4/125 Result is 0.552 decimal There are two ways to work a base conversion. One is to do the arithmetic in the source base, and do successive multiplies of the destination base. That would mean working in base 5 in this case, which is probably more work in Python. The other is to work in the result base, and do the multiplies of the source base. That's the approach you were taking, and it works great if the precision of a float is acceptable. Your code is fine, although a bit convoluted. Only problem is that you're working in integers, when you need float. So just change mydecfrac to 0.0 and it'll work. ------------------------------ Message: 3 Date: Mon, 6 Apr 2009 08:54:17 -0400 From: Kent Johnson Subject: Re: [Tutor] Checking for string in a list strangeness To: AdamC Cc: *tutor python Message-ID: <1c2a2c590904060554w176c7d7dnd0dff61f68383001 at mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 On Mon, Apr 6, 2009 at 8:26 AM, AdamC wrote: > 2009/4/6 Kent Johnson : >> On Mon, Apr 6, 2009 at 3:30 AM, AdamC wrote: >>> I'm writing a small cgi application for children to use and I want to >>> check that the name they enter isn't a swear word. >> >>> #for i in swearlist: ? ? ? # shows swear list OK >>> # ? ?print i; >> >> Perhaps the words in swearlist include some ?whitespace? Try >> for i in swearlist: >> ?print repr(i) >> >> and look for leading and trailing spaces, tabs, etc. >> >> Kent > > I think you're on to something here Kent. Thanks. There doesn't appear > to be any whitespaces in the words but an entry in the list appears > like this: > > print swearlist[0] > returns > ('xxxx',) > > where xxxx is the expletive in the database. Ah, yes, the result of mycursor.fetchone() is a tuple containing the fetched elements. Even though you are only reading one field, it is still returned in a tuple. That is what the parentheses and comma signify. Try this: swearlist = [] for i in range (0, myrowcount): myrow = mycursor.fetchone() swearlist.append(myrow[0]) Kent ------------------------------ Message: 4 Date: Mon, 6 Apr 2009 19:53:25 +0530 From: Kumar Subject: [Tutor] problem in replacing regex To: tutor at python.org Message-ID: Content-Type: text/plain; charset="iso-8859-1" Hello everyone, I have one account number. I want to replace all this account numbers and convert them as URLS. but I don't want to convert existing urls which has account numbers. e.g. 1234-4578 first i convert any existing url into and then I check that if any above number exist then I convert them to my site's url . i am already having regex to check and replace this number. And I can replace all occurrences of this number and convert them into url using re.sub() but problem is that if the value is http://sadfsdf.com/1234-4578 then it first converts http://sadfsdf.com/1234-4578 into http://sadfsdf.com/1234-4578 and then again it converts last number into url so the complete url gets broken. can anybody help me how can I omit the searching existing urls with above number? I have already tried many examples but none of them seems working. Although I can use re.sub('[^/](myregex)','',value) but it won't be a permenant solution as if somebody enters /1234-4578 then also it won't convert it. Thanks, Kumar -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 5 Date: Mon, 06 Apr 2009 10:41:12 -0400 From: bob gailer Subject: [Tutor] Please use plain text. To: tutor at python.org Message-ID: <49DA1488.5030701 at gmail.com> Content-Type: text/plain; charset="us-ascii" An HTML attachment was scrubbed... URL: ------------------------------ Message: 6 Date: Mon, 06 Apr 2009 10:56:44 -0400 From: bob gailer Subject: Re: [Tutor] Checking for string in a list strangeness To: AdamC Cc: Python Tutor Message-ID: <49DA182C.9070104 at gmail.com> Content-Type: text/plain; charset="us-ascii" An HTML attachment was scrubbed... URL: ------------------------------ _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor End of Tutor Digest, Vol 62, Issue 15 ************************************* -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Apr 7 17:52:32 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 7 Apr 2009 11:52:32 -0400 Subject: [Tutor] unicode to plain text conversion In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC90818618F@HCAMAIL03.ochca.com> References: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> <97D6F0A82A6E894DAF44B9F575305CC90818618F@HCAMAIL03.ochca.com> Message-ID: <1c2a2c590904070852v35cc4661j8ce1a8aa31edb300@mail.gmail.com> On Tue, Apr 7, 2009 at 10:44 AM, Pirritano, Matthew wrote: > How can I find out the type of coding that was used to create this file? > Is there a way to do this other than just asking the person who created > it? That is possible, but I was just curious. If you can look at the data as hex values it's pretty easy to distinguish utf-8, utf-16be and utf-16le, which are the most likely codings. In Python you could try f = open('unicode_file.txt') s = f.read(20) print repr(s) Kent From MPirritano at ochca.com Tue Apr 7 17:56:16 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Tue, 7 Apr 2009 08:56:16 -0700 Subject: [Tutor] unicode to plain text conversion In-Reply-To: <1c2a2c590904061751r3dfc3fb9m927f184d165c3a77@mail.gmail.com> References: <97D6F0A82A6E894DAF44B9F575305CC9081860D6@HCAMAIL03.ochca.com> <1c2a2c590904061751r3dfc3fb9m927f184d165c3a77@mail.gmail.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC908186242@HCAMAIL03.ochca.com> Thanks all! Kent, this syntax worked. I was able to figure it out the encoding just with trial and error. It is utf16. Now the only thing is that the conversion is double-spacing the lines of data. I'm thinking this must be something that I need to fix in my syntax. I will continue to try and figure it out, but any pointing out of the obvious or other ideas would be much appreciated. Again, newbie here. Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: tutor-bounces+mpirritano=ochca.com at python.org [mailto:tutor-bounces+mpirritano=ochca.com at python.org] On Behalf Of Kent Johnson Sent: Monday, April 06, 2009 5:51 PM To: Pirritano, Matthew Cc: Python Tutor Subject: Re: [Tutor] unicode to plain text conversion On Mon, Apr 6, 2009 at 6:48 PM, Pirritano, Matthew wrote: > Hello python people, > > I am a total newbie. I have a very large file > 4GB that I need to > convert from Unicode to plain text. I used to just use dos when the file > was < 4GB but it no longer seems to work. Can anyone point me to some > python code that might perform this function? What is the encoding of the Unicode file? Assuming that the file has lines that will each fit in memory, you can use the codecs module to decode the unicode. Something like this: import codecs inp = codecs.open('Unicode_file.txt', 'r', 'utf-16le') outp = open('new_text_file.txt') outp.writelines(inp) inp.close() outp.close() The above code assumes UTF-16LE encoding, change it to the correct one if that is not right. A list of supported encodings is here: http://docs.python.org/library/codecs.html#id3 Kent _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor From bgailer at gmail.com Tue Apr 7 18:46:24 2009 From: bgailer at gmail.com (bob gailer) Date: Tue, 07 Apr 2009 12:46:24 -0400 Subject: [Tutor] Please use plain text. In-Reply-To: <20090406173041.GQ17135@inocybe.teonanacatl.org> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> Message-ID: <49DB8360.7010101@gmail.com> Todd Zullinger wrote: > bob gailer wrote: > >> Please use plain text rather than formatted text. >> > > Was sending this request in an html formatted message intentional? I > don't know about most folks, but I consider plain text to mean a > content-type of text/plain rather than text/html. :) > Thanks for the request for clarification. By plain text I mean: one font and one size that is "readable". Most of the emails I receive appear the same in font and size, so whatever that is I like. Use of very small or very large font sizes or various fonts / colors makes it hard for me to read. As I compose this in Thunderbird I see Body Text and Variable Width. But I do appreciate code in Fixed Width. -- Bob Gailer Chapel Hill NC 919-636-4239 From MPirritano at ochca.com Tue Apr 7 18:52:54 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Tue, 7 Apr 2009 09:52:54 -0700 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <49DB8360.7010101@gmail.com> References: <49DA1488.5030701@gmail.com><20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> So Kent's syntax worked to convert my Unicode file to plain text. But now my data is double space. How can I fix this. Here is the code I'm using. import codecs inp = codecs.open('g:\\data\\amm\\text files\\test20090320.txt', 'r', 'utf-16') outp = open('g:\\data\\amm\\text files\\new_text_file.txt', 'w') outp.writelines(inp) inp.close() outp.close() Thanks so much for your help! matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 From kent37 at tds.net Tue Apr 7 19:10:06 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 7 Apr 2009 13:10:06 -0400 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> Message-ID: <1c2a2c590904071010y2a6718a1q15b29d7b1eb50a7d@mail.gmail.com> On Tue, Apr 7, 2009 at 12:52 PM, Pirritano, Matthew wrote: > So Kent's syntax worked to convert my Unicode file to plain text. But > now my data is double space. How can I fix this. ?Here is the code I'm > using. > > import codecs > > inp = codecs.open('g:\\data\\amm\\text files\\test20090320.txt', 'r', > 'utf-16') > outp = open('g:\\data\\amm\\text files\\new_text_file.txt', 'w') > outp.writelines(inp) > inp.close() > outp.close() I guess there is something funny going on with conversion of newlines. It would help to know what line endings are in the original data, and what are in the new data. One thing to try is to open the output file as binary - 'wb' instead of 'w'. The input file is opened as binary by the codecs module. If that doesn't work, you could try tostrip line endings from the original, then add back in to the new file: inp = codecs.open('g:\\data\\amm\\text files\\test20090320.txt', 'r', 'utf-16') outp = open('g:\\data\\amm\\text files\\new_text_file.txt', 'w') for line in inp: line = line.rstrip() outp.write(line) outp.write('\n') inp.close() outp.close() Note that this will strip all trailing white space from the input, I don't know if that is an issue... Kent From marc.tompkins at gmail.com Tue Apr 7 19:11:58 2009 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 7 Apr 2009 10:11:58 -0700 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> Message-ID: <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> On Tue, Apr 7, 2009 at 9:52 AM, Pirritano, Matthew wrote: > So Kent's syntax worked to convert my Unicode file to plain text. But > now my data is double space. How can I fix this. Here is the code I'm > using. > Sounds like you're being stung by the difference in newline handling between operating systems - to recap, MS-DOS and Windows terminate a line with a carriage return and linefeed (aka CRLF or '\r\n'); *nixes use just LF ('\n'); Mac OS up to version 9 uses just CR ('\r'). You will have noticed this, on Windows, if you ever open a text file in Notepad that was created on a different OS - instead of breaking into separate lines, everything appears on one long line with funky characters where the breaks should be. If you use a more sophisticated text editor such as Notepad++ or Textpad, everything looks normal. Python has automatic newline conversion; generally, you can read a text file from any OS and write to it correctly regardless of the OS that you happen to be running yourself. However, the automatic newline handling (from my perfunctory Googling) appears to break down when you're also converting between Unicode and ASCII; or it could be because you're essentially doing a read() from one file and a writelines() to the other; or something else entirely. Anyway, try this - import codecs > > inp = codecs.open('g:\\data\\amm\\text files\\test20090320.txt', 'r', > 'utf-16') > outp = open('g:\\data\\amm\\text files\\new_text_file.txt', 'w') > for outLine in inp: > outp.write(outLine.strip()) > inp.close() > outp.close() strip() will remove any leading or trailing whitespace - which should include any leftover CR or LF characters. HTH - -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From MPirritano at ochca.com Tue Apr 7 19:26:36 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Tue, 7 Apr 2009 10:26:36 -0700 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> References: <49DA1488.5030701@gmail.com><20090406173041.GQ17135@inocybe.teonanacatl.org><49DB8360.7010101@gmail.com><97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com> Thanks Marc, But I think that got rid of all of my carriage returns. Everything on just one line now. Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 ________________________________ From: tutor-bounces+mpirritano=ochca.com at python.org [mailto:tutor-bounces+mpirritano=ochca.com at python.org] On Behalf Of Marc Tompkins Sent: Tuesday, April 07, 2009 10:12 AM To: tutor at python.org Subject: Re: [Tutor] why is unicode converted file double spaced? On Tue, Apr 7, 2009 at 9:52 AM, Pirritano, Matthew wrote: So Kent's syntax worked to convert my Unicode file to plain text. But now my data is double space. How can I fix this. Here is the code I'm using. Sounds like you're being stung by the difference in newline handling between operating systems - to recap, MS-DOS and Windows terminate a line with a carriage return and linefeed (aka CRLF or '\r\n'); *nixes use just LF ('\n'); Mac OS up to version 9 uses just CR ('\r'). You will have noticed this, on Windows, if you ever open a text file in Notepad that was created on a different OS - instead of breaking into separate lines, everything appears on one long line with funky characters where the breaks should be. If you use a more sophisticated text editor such as Notepad++ or Textpad, everything looks normal. Python has automatic newline conversion; generally, you can read a text file from any OS and write to it correctly regardless of the OS that you happen to be running yourself. However, the automatic newline handling (from my perfunctory Googling) appears to break down when you're also converting between Unicode and ASCII; or it could be because you're essentially doing a read() from one file and a writelines() to the other; or something else entirely. Anyway, try this - import codecs inp = codecs.open('g:\\data\\amm\\text files\\test20090320.txt', 'r', 'utf-16') outp = open('g:\\data\\amm\\text files\\new_text_file.txt', 'w') for outLine in inp: outp.write(outLine.strip()) inp.close() outp.close() strip() will remove any leading or trailing whitespace - which should include any leftover CR or LF characters. HTH - -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Tue Apr 7 19:32:57 2009 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 7 Apr 2009 10:32:57 -0700 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com> Message-ID: <40af687b0904071032i7b0da02vce4ef51b97375e3d@mail.gmail.com> On Tue, Apr 7, 2009 at 10:26 AM, Pirritano, Matthew wrote: > Thanks Marc, > > > > But I think that got rid of all of my carriage returns. Everything on just > one line now. > Sorry! My bad! Try this instead: > for outLine in inp: > outp.write(outLine.strip() + '\n') > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Apr 7 19:52:43 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 7 Apr 2009 13:52:43 -0400 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> Message-ID: <1c2a2c590904071052we7950fx17f197352d30a5ed@mail.gmail.com> On Tue, Apr 7, 2009 at 1:11 PM, Marc Tompkins wrote: > Anyway, try this - > >> import codecs >> >> inp = codecs.open('g:\\data\\amm\\text files\\test20090320.txt', 'r', >> 'utf-16') >> outp = open('g:\\data\\amm\\text files\\new_text_file.txt', 'w') >> >> for outLine in inp: >> ??? outp.write(outLine.strip()) >> >> inp.close() >> outp.close() > > strip() will remove any leading or trailing whitespace - which should > include any leftover CR or LF characters. You really should use rstrip(), leading white space is generally significant in a text file, trailing white space not so much. Kent From marc.tompkins at gmail.com Tue Apr 7 19:57:58 2009 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 7 Apr 2009 10:57:58 -0700 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <1c2a2c590904071052we7950fx17f197352d30a5ed@mail.gmail.com> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> <1c2a2c590904071052we7950fx17f197352d30a5ed@mail.gmail.com> Message-ID: <40af687b0904071057o4b90947cha5959c93a8f5c69e@mail.gmail.com> On Tue, Apr 7, 2009 at 10:52 AM, Kent Johnson wrote: > You really should use rstrip(), leading white space is generally > significant in a text file, trailing white space not so much. > Good point. -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From MPirritano at ochca.com Tue Apr 7 19:57:55 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Tue, 7 Apr 2009 10:57:55 -0700 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <40af687b0904071032i7b0da02vce4ef51b97375e3d@mail.gmail.com> References: <49DA1488.5030701@gmail.com><20090406173041.GQ17135@inocybe.teonanacatl.org><49DB8360.7010101@gmail.com><97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com><40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com><97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com> <40af687b0904071032i7b0da02vce4ef51b97375e3d@mail.gmail.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC908186340@HCAMAIL03.ochca.com> Excellent! Thanks Marc, Kent, and everyone. Marc your code worked. Now to see if it will run on the full 4.5 GB file! : ) Thanks matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 ________________________________ From: tutor-bounces+mpirritano=ochca.com at python.org [mailto:tutor-bounces+mpirritano=ochca.com at python.org] On Behalf Of Marc Tompkins Sent: Tuesday, April 07, 2009 10:33 AM To: tutor at python.org Subject: Re: [Tutor] why is unicode converted file double spaced? On Tue, Apr 7, 2009 at 10:26 AM, Pirritano, Matthew wrote: Thanks Marc, But I think that got rid of all of my carriage returns. Everything on just one line now. Sorry! My bad! Try this instead: for outLine in inp: outp.write(outLine.strip() + '\n') -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Tue Apr 7 20:05:27 2009 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 7 Apr 2009 11:05:27 -0700 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC908186340@HCAMAIL03.ochca.com> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com> <40af687b0904071032i7b0da02vce4ef51b97375e3d@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC908186340@HCAMAIL03.ochca.com> Message-ID: <40af687b0904071105t58fc12feg2467477c54ba3117@mail.gmail.com> On Tue, Apr 7, 2009 at 10:57 AM, Pirritano, Matthew wrote: > Excellent! Thanks Marc, Kent, and everyone. Marc your code worked. Now to > see if it will run on the full 4.5 GB file! : ) > Glad to help - but you should definitely take Kent's advice and replace my strip() with rstrip(). Good luck! -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Tue Apr 7 20:15:17 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Tue, 07 Apr 2009 11:15:17 -0700 Subject: [Tutor] Python Lists--A Suggestion Message-ID: <49DB9835.4010008@sbcglobal.net> An HTML attachment was scrubbed... URL: From MPirritano at ochca.com Tue Apr 7 20:59:54 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Tue, 7 Apr 2009 11:59:54 -0700 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <40af687b0904071105t58fc12feg2467477c54ba3117@mail.gmail.com> References: <49DA1488.5030701@gmail.com><20090406173041.GQ17135@inocybe.teonanacatl.org><49DB8360.7010101@gmail.com><97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com><40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com><97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com><40af687b0904071032i7b0da02vce4ef51b97375e3d@mail.gmail.com><97D6F0A82A6E894DAF44B9F575305CC908186340@HCAMAIL03.ochca.com> <40af687b0904071105t58fc12feg2467477c54ba3117@mail.gmail.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC9081863B7@HCAMAIL03.ochca.com> I did get an error... Traceback (most recent call last): File "C:\Projects\unicode_convert.py", line 8, in outp.write(outLine.strip()+'\n') UnicodeEncodeError: 'ascii' codec can't encode characters in position 640-641: ordinal not in range(128) Should I be worried about this. And where does this message indicate that the error is. And what is the error? Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 ________________________________ From: tutor-bounces+mpirritano=ochca.com at python.org [mailto:tutor-bounces+mpirritano=ochca.com at python.org] On Behalf Of Marc Tompkins Sent: Tuesday, April 07, 2009 11:05 AM To: tutor at python.org Subject: Re: [Tutor] why is unicode converted file double spaced? On Tue, Apr 7, 2009 at 10:57 AM, Pirritano, Matthew wrote: Excellent! Thanks Marc, Kent, and everyone. Marc your code worked. Now to see if it will run on the full 4.5 GB file! : ) Glad to help - but you should definitely take Kent's advice and replace my strip() with rstrip(). Good luck! -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Apr 7 22:59:25 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 7 Apr 2009 21:59:25 +0100 Subject: [Tutor] Python Lists--A Suggestion References: <49DB9835.4010008@sbcglobal.net> Message-ID: "Wayne Watson" wrote > I offered in that thread that it might be useful to allow people to > post images and videos. They can sometimes be very useful > .... One response was that such sites are generally available. Indeed and posting big images or any size video in a mailing list would be very antisocial for a variety of reasons. But a consistent place to post might be useful since an archive might even develop. pastebin seems to be becoming a standard place to post long code segments, maybe something similar would be useful? Or is UTube good enuff? > As an aside, I would suggest that the mail lists come to some > common ground. In the last few weeks, ... > html code without much ado. In the PIL list, such a post will > immediately go to the moderator as suspicious. I guess it depends on how paranoid you are. HTML mails can be insecure and so many moderators restrict them. I know one list that bans them outright with an autom,ated (rather rude response) about posting insecure mails - It is an internet security forum though so that might be why! :-) > What does work is posting as text. I have all my email clients set to force everything to plain text regardless of how it arrives, and I try to post as text too, but some tools seem to make that difficult to achieve... ASCII email is how God intended it to be IMHO If it can't be done with ASCII art its not worth doing...:-) Alan G. From marc.tompkins at gmail.com Tue Apr 7 23:52:57 2009 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 7 Apr 2009 14:52:57 -0700 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC9081863B7@HCAMAIL03.ochca.com> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com> <40af687b0904071032i7b0da02vce4ef51b97375e3d@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC908186340@HCAMAIL03.ochca.com> <40af687b0904071105t58fc12feg2467477c54ba3117@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081863B7@HCAMAIL03.ochca.com> Message-ID: <40af687b0904071452w7c28437bv3745682ce62671fc@mail.gmail.com> On Tue, Apr 7, 2009 at 11:59 AM, Pirritano, Matthew wrote: > I did get an error? > > > > Traceback (most recent call last): > > File "C:\Projects\unicode_convert.py", line 8, in > > outp.write(outLine.strip()+'\n') > > UnicodeEncodeError: 'ascii' codec can't encode characters in position > 640-641: ordinal not in range(128) > Should I be worried about this. And where does this message indicate that > the error is. And what is the error? > > That error is telling you that your input file contains character(s) that don't have a valid representation in ASCII (which is the AMERICAN Standard Code for Information Interchange - no furrin languages need apply!) I came into this conversation late (for which I apologize), so I don't actually know what your input file contains; the fact that it's encoded in UTF-16 indicates to me that its creators anticipated that there might be non-English symbols in it. Where is the error... well, it's positions 640-641 (which, since most code points in UTF-16 are two bytes long, might mean character 319 or 320 if you opened it in a text editor - or it might mean character 640 or 641, I don't really know...) of _some_ line. Unfortunately we don't know which line, 'cause we're not keeping track of line numbers. Something I often do in similar situations: add print statements temporarily (printing to the screen makes a significant performance drag in the middle of a loop, so I comment out or delete the prints when it's time for production.) Two approaches: add an index variable and print just the number, or print the data you're about to try to save to the file. In your case, I'd go with the index, 'cause your lines are apparently much longer than screen width. index = 1 for outLine in inp: print(index) outp.write(outLine.rstrip() + '\n') index += 1 You'll have a long string of numbers, followed by the error/traceback. The last number before the error should be the line number that caused the error; open your file in a decent text editor, go to that line, and look at columns 319/320 (or 640/641 - I'm curious to find out which) - that will show you which character caused your problem. What to do about it, though: the "codecs" module has several ways of dealing with errors in encoding/decoding; it defaults to 'strict', which is what you're seeing. Other choices include 'replace' (replaces invalid ASCII characters with question marks, if I recall correctly) and 'ignore' (which just drops the invalid character from the output.) Change your file-opening line to: inp = codecs.open('g:\\data\\amm\\text files\\test20090320.txt', 'r','utf-16', 'replace') or inp = codecs.open('g:\\data\\amm\\text files\\test20090320.txt', 'r','utf-16', 'ignore') depending on your preference. Have fun - -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Apr 7 23:54:42 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 7 Apr 2009 17:54:42 -0400 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <97D6F0A82A6E894DAF44B9F575305CC9081863B7@HCAMAIL03.ochca.com> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com> <40af687b0904071032i7b0da02vce4ef51b97375e3d@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC908186340@HCAMAIL03.ochca.com> <40af687b0904071105t58fc12feg2467477c54ba3117@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081863B7@HCAMAIL03.ochca.com> Message-ID: <1c2a2c590904071454l65547b55u892a25c80493e3d1@mail.gmail.com> On Tue, Apr 7, 2009 at 2:59 PM, Pirritano, Matthew wrote: > Traceback (most recent call last): > > ? File "C:\Projects\unicode_convert.py", line 8, in > > ??? outp.write(outLine.strip()+'\n') > > UnicodeEncodeError: 'ascii' codec can't encode characters in position > 640-641: ordinal not in range(128) > > Should I be worried about this. And where does this message indicate that > the error is. And what is the error? The error is that your source file contains characters that are not in the Ascii set; in particular a character with value 128 was found at location 640 in some unknown line. Yes, you should be worried; the processing aborted at that point. The error happens during an implicit conversion from unicode to ascii within the write() call. What you have is equivalent to outp.write(outline.strip().encode('ascii') + '\n') You can the encoder to ignore errors or replace them with ? by adding the parameter errors = 'ignore' or errors = 'replace' to the encode() call, e.g. outp.write(outline.strip().encode('ascii', errors='replace') + '\n') If you want to know the line which contains the error you will have to modify your program to keep track of line numbers and trap and report errors. Kent From tmz at pobox.com Wed Apr 8 00:10:24 2009 From: tmz at pobox.com (Todd Zullinger) Date: Tue, 7 Apr 2009 18:10:24 -0400 Subject: [Tutor] Please use plain text. In-Reply-To: <49DB8360.7010101@gmail.com> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> Message-ID: <20090407221024.GR17135@inocybe.teonanacatl.org> bob gailer wrote: > Thanks for the request for clarification. By plain text I mean: > > one font and one size that is "readable". Most of the emails I > receive appear the same in font and size, so whatever that is I > like. What's readable to you may not be readable to the next person though. Most likely is that many of the posters here are not sending HTML formatted mail, and thus their messages don't contain any font or size information. That's left up to your mail client and it will use whatever defaults you have chosen. > Use of very small or very large font sizes or various fonts / colors > makes it hard for me to read. > > As I compose this in Thunderbird I see Body Text and Variable Width. > But I do appreciate code in Fixed Width. I believe the best answer to this issue is to not send HTML formatted mail at all. A truly plain text email contains no markup telling anyone's email client what fonts to use. That is something that rightly belongs to each user, IMO. Most of the mailing lists I subscribe to frown upon HTML mail. I don't worry about it a lot though, as I use a console based email tool and any HTML messages that are sent are simply dumped as text for me to read. (I know many others that direct all HTML mail to /dev/null.) -- Todd OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I am not young enough to know everything. -- Oscar Wilde (1854-1900) -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 542 bytes Desc: not available URL: From chase.mp at gmail.com Wed Apr 8 00:34:29 2009 From: chase.mp at gmail.com (chase.mp at gmail.com) Date: Tue, 07 Apr 2009 22:34:29 +0000 Subject: [Tutor] Python Logging Module Message-ID: <0022152d69553d0c380466fe9e61@google.com> Hello everyone... My intention is to create a generic logging function I can use across scripts. I have a few things I would like this function to be able to accomplish: 1. log info and above to console 2. log debug and above to file 3. format the output for each differently 4. allow areas of a program to use a different "logger", basically putting a different name in the event depending on which of my bad ideas broke the system 5. control log rotation within the script itself so far I have accomplished a lot of hair pulling...the following does some of the above: import logging import sys LOG_FILENAME = "./testing.log" def set_logging(): # Set up a specific logger my_logger = logging.getLogger(sys.argv[0]) # set up logging defaults logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M:%S', filename=LOG_FILENAME, filemode='w') # define a Handler which writes INFO messages or higher to the sys.stderr console = logging.StreamHandler() console.setLevel(logging.INFO) # set a format which is simpler for console use formatter = logging.Formatter('%(levelname)-8s %(message)s') # tell the handler to use this format console.setFormatter(formatter) # add the handler to the root logger logging.getLogger('').addHandler(console) set_logging() logging.debug("test_debug") logging.info("testing_info") I also have a separate bit that can do the rotation: import glob import logging import logging.handlers import sys LOG_FILENAME = './logging_rotateout' # Set up a specific logger with our desired output level fullscriptname = os.path.split(sys.argv[0]) scriptname = fullscriptname[1] log = logging.getLogger(scriptname) log.setLevel(logging.DEBUG) # Add the log message handler to the logger handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20, backupCount=5) log.addHandler(handler) #log messages log.debug("testing") log.info("console") I cannot for the life of me seem to combine these two functions, this seems odd to me, but I think I do not understand what logging.basicConfig does in the previous example. If I try to add: handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20, backupCount=5) logging.getLogger('').addHandler(handler) to the first piece of code I get all kinds of errors, but mainly: AttributeError: 'module' object has no attribute handlers I get this even though the line above it obviously adds the console handler. I have tried many things, I wish I had documented better all of my attempts at syntax. Any help would be greatly appreciated. Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Wed Apr 8 03:13:11 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Tue, 07 Apr 2009 18:13:11 -0700 Subject: [Tutor] Python Lists--A Suggestion In-Reply-To: References: <49DB9835.4010008@sbcglobal.net> Message-ID: <49DBFA27.5010900@sbcglobal.net> An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Apr 8 04:15:28 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 7 Apr 2009 22:15:28 -0400 Subject: [Tutor] Python Logging Module In-Reply-To: <0022152d69553d0c380466fe9e61@google.com> References: <0022152d69553d0c380466fe9e61@google.com> Message-ID: <1c2a2c590904071915n16673a59pcc667ff3e136b4dd@mail.gmail.com> On Tue, Apr 7, 2009 at 6:34 PM, wrote: > Hello everyone... > > My intention is to create a generic logging function I can use across > scripts. I have a few things I would like this function to be able to > accomplish: > > 1. log info and above to console > 2. log debug and above to file > 3. format the output for each differently > 4. allow areas of a program to use a different "logger", basically putting a > different name in the event depending on which of my bad ideas broke the > system > 5. control log rotation within the script itself You are on the right track to be looking at the logging module. > import logging.handlers > # Add the log message handler to the logger > handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20, > I cannot for the life of me seem to combine these two functions, this seems > odd to me, but I think I do not understand what logging.basicConfig does in > the previous example. If I try to add: > > handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, maxBytes=20, > backupCount=5) > logging.getLogger('').addHandler(handler) > > to the first piece of code I get all kinds of errors, but mainly: > > AttributeError: 'module' object has no attribute handlers Notice the import logging.handlers line in the code that works. Kent From kent37 at tds.net Wed Apr 8 04:24:44 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 7 Apr 2009 22:24:44 -0400 Subject: [Tutor] Python Lists--A Suggestion In-Reply-To: <49DBFA27.5010900@sbcglobal.net> References: <49DB9835.4010008@sbcglobal.net> <49DBFA27.5010900@sbcglobal.net> Message-ID: <1c2a2c590904071924m496bf324kcf77060a1b82de02@mail.gmail.com> On Tue, Apr 7, 2009 at 9:13 PM, Wayne Watson wrote: > Perhaps the Pyton organization could produce their mail lists, if they have > any control over them. Maybe that's the source of the inconsistency? That > is, some are not sponsored by them. I don't really know what you mean by "produce" their mail lists, but... The "Python organization" is a loose confederation of volunteers. There are about 100 mailing lists* hosted at python.org. Each list has its own maintainer(s) and its own community. I would be very surprised if anyone in all that collection of people has the time or interest to try to make all 100 lists conform to a single standard. * http://mail.python.org/mailman/listinfo Plain text is a very common standard for mailing lists. This list is fairly forgiving, as we do want to welcome beginners, but the welcome text includes this request: For all posts: - Format your email as plain text, not HTML Kent From sierra_mtnview at sbcglobal.net Wed Apr 8 05:55:27 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Tue, 07 Apr 2009 20:55:27 -0700 Subject: [Tutor] Python Lists--A Suggestion In-Reply-To: <1c2a2c590904071924m496bf324kcf77060a1b82de02@mail.gmail.com> References: <49DB9835.4010008@sbcglobal.net> <49DBFA27.5010900@sbcglobal.net> <1c2a2c590904071924m496bf324kcf77060a1b82de02@mail.gmail.com> Message-ID: <49DC202F.3060401@sbcglobal.net> An HTML attachment was scrubbed... URL: From denis.spir at free.fr Wed Apr 8 09:34:24 2009 From: denis.spir at free.fr (spir) Date: Wed, 8 Apr 2009 09:34:24 +0200 Subject: [Tutor] problem in replacing regex In-Reply-To: References: <20090406170636.7b9d613f@o> <20090407112933.47478155@o> Message-ID: <20090408093424.40665011@o> Le Wed, 8 Apr 2009 12:31:31 +0530, Kumar s'exprima ainsi: > Hi Danis, > > Just to be more specific that I can add [^/] in my expression which will > successfully work for url (i.e. http://sdfs/123-34) but it will also work > for non url (i.e. /123-34 ) so u am just trying it to make it specific for > url only > I have already tried [^(http).*/] but that also failed and it didn't work on > /123-34 You'd better provide an example of your input, what you expect as output, the relevant part of your prog and what you get as actual output. Denis > Thanks, > Hiren > > On Tue, Apr 7, 2009 at 7:54 PM, Kumar wrote: > > > Hi Danis, > > > > Your solution would work perfectly fine for one value. > ? > > but actually here i dont have one value (either number or url)... > > e.g. > > s = "https://hello.com/accid/12345-06450 > > > > /12345-06450 > > > > http://sadfsdf.com/12345-12 > > http://sadfsdf.com/asdf/asdf/asdf/12345-12 > > 12345-12" > > > > so everything above is one string value and actually that's why I am first > > doing both operations separately and that is main cause of problem. > > > > Thanks, > > Kumar ------ la vita e estrany From denis.spir at free.fr Wed Apr 8 09:55:54 2009 From: denis.spir at free.fr (spir) Date: Wed, 8 Apr 2009 09:55:54 +0200 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <1c2a2c590904071454l65547b55u892a25c80493e3d1@mail.gmail.com> References: <49DA1488.5030701@gmail.com> <20090406173041.GQ17135@inocybe.teonanacatl.org> <49DB8360.7010101@gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com> <40af687b0904071032i7b0da02vce4ef51b97375e3d@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC908186340@HCAMAIL03.ochca.com> <40af687b0904071105t58fc12feg2467477c54ba3117@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081863B7@HCAMAIL03.ochca.com> <1c2a2c590904071454l65547b55u892a25c80493e3d1@mail.gmail.com> Message-ID: <20090408095554.44664403@o> Le Tue, 7 Apr 2009 17:54:42 -0400, Kent Johnson s'exprima ainsi: > > ??? outp.write(outLine.strip()+'\n') > UnicodeEncodeError: 'ascii' codec can't encode characters in position > 640-641: ordinal not in range(128) Hem, sorry for stepping in the thread. Isn't the issue that lines previously decoded as utf16 are now expanded with an ASCII-by-default NL char? Thought that when one wants to cope with unicode all literals need to be explicitely formed as unicode strings. Otherwise python silently falls back to ascii default and one gets such getting-on-the-nerves UnicodeEncodeError messages. (A bit like if when adding an int and a float python tried to produce an int result, imo.) As the source was utf16, it may contain non ascii chars, so the whole output should be managed as unicode, no? denis ------ la vita e estrany From cashbang at googlemail.com Wed Apr 8 12:28:09 2009 From: cashbang at googlemail.com (David Cash) Date: Wed, 8 Apr 2009 11:28:09 +0100 Subject: [Tutor] Regex for a variable within a string Message-ID: <9abb7ef80904080328h129931efh90955d0d09600b28@mail.gmail.com> Hi, I'm new to python and have decided to develop a web crawler / file downloader as my first application. I am at the stage where the script requests a page and parses the page for URLs, then prints them out. However, I'd like to change my current regex that greps for 'http' to one that will grep for the url variable that is used in the connect string. I was hoping I could use something like p=re.compile((url).*?'"') but this is clearly not the right syntax. Apologies for such a newbie question! My current code is below: Thanks in advance #! /usr/bin/python import urllib2 import sys import re import getopt import HTMLParser import httplib #====== GET OPTIONS def main(argv): url = "nothing" filetype = "nofile" try: opts, args = getopt.getopt(argv, "u:f:",["url=","filetype="]) except getopt.GetoptError: usage() sys.exit(2) for opt, arg in opts: if opt in ("-u", "--url"): url = arg elif opt in ("-f","--filetype"): filetype = arg print "Using base url: " + url + "\n" "Using filetype: " + filetype request(url,filetype) #===== PRINT USAGE def usage(): print ''' Bulk file downloader -u = base url -f = filetype example: bulkdld.py -u http://www.google.com -f .wmv ''' sys.exit(1) #========REQUEST PAGE def request(url,filetype): txdata = None txheaders= { 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', 'Accept-Language': 'en-us', 'Keep-Alive': '300', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', } req = urllib2.Request(url, txdata, txheaders) u = urllib2.urlopen(req) headers = u.info() data = u.read() #print data p=re.compile('http://.*?"') ########## THIS IS THE REGEX IN QUESTION m=p.findall(data) print m if __name__ == "__main__": main(sys.argv[1:]) -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Apr 8 12:34:49 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 8 Apr 2009 06:34:49 -0400 Subject: [Tutor] problem in replacing regex In-Reply-To: <20090408093424.40665011@o> References: <20090406170636.7b9d613f@o> <20090407112933.47478155@o> <20090408093424.40665011@o> Message-ID: <1c2a2c590904080334p36b10338ue2516e72ae9f86d7@mail.gmail.com> > Kumar s'exprima ainsi: > >> Hi Danis, >> >> Just to be more specific that I can add [^/] in my expression which will >> successfully work for url (i.e. http://sdfs/123-34) but it will also work >> for non url (i.e. /123-34 ) so u am just trying it to make it specific for >> url only >> I have already tried [^(http).*/] but that also failed and it didn't work on >> /123-34 The [^] don't do what you think they do. In a regex, [abc] means, match a single character that is either a, b, or c. [^abc] means match a single character that is not a, not b and not c. It does *not* mean to match a three-letter string that is not abc, or anything like that. I think you are trying to write a negative look-behind, something like (? References: <49DA1488.5030701@gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com> <40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com> <40af687b0904071032i7b0da02vce4ef51b97375e3d@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC908186340@HCAMAIL03.ochca.com> <40af687b0904071105t58fc12feg2467477c54ba3117@mail.gmail.com> <97D6F0A82A6E894DAF44B9F575305CC9081863B7@HCAMAIL03.ochca.com> <1c2a2c590904071454l65547b55u892a25c80493e3d1@mail.gmail.com> <20090408095554.44664403@o> Message-ID: <1c2a2c590904080339y4202e4d0x729a1e1bcc537166@mail.gmail.com> On Wed, Apr 8, 2009 at 3:55 AM, spir wrote: > Le Tue, 7 Apr 2009 17:54:42 -0400, > Kent Johnson s'exprima ainsi: > >> > ??? outp.write(outLine.strip()+'\n') > >> UnicodeEncodeError: 'ascii' codec can't encode characters in position >> 640-641: ordinal not in range(128) > > Hem, sorry for stepping in the thread. Isn't the issue that lines previously decoded as utf16 are now expanded with an ASCII-by-default NL char? Thought that when one wants to cope with unicode all literals need to be ?explicitely formed as unicode strings. Yes, we have not been explicit about the conversion, that is my fault. But in this case conversion to ascii is the desired behaviour so as long as the input falls within the ascii character set all is well. > Otherwise python silently falls back to ascii default and one gets such getting-on-the-nerves UnicodeEncodeError messages. (A bit like if when adding an int and a float python tried to produce an int result, imo.) > As the source was utf16, it may contain non ascii chars, so the whole output should be managed as unicode, no? Yes, an explicit call to encode() with the desired error parameter is one solution, as I indicated in a previous email. Kent From kent37 at tds.net Wed Apr 8 12:50:39 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 8 Apr 2009 06:50:39 -0400 Subject: [Tutor] Regex for a variable within a string In-Reply-To: <9abb7ef80904080328h129931efh90955d0d09600b28@mail.gmail.com> References: <9abb7ef80904080328h129931efh90955d0d09600b28@mail.gmail.com> Message-ID: <1c2a2c590904080350n5eb63fcegc6e7281980647ffb@mail.gmail.com> On Wed, Apr 8, 2009 at 6:28 AM, David Cash wrote: > Hi, I'm new to python and have decided to develop a web crawler / file > downloader as my first application. I am at the stage where the script > requests a page and parses the page for URLs, then prints them out. However, > I'd like to change my current regex that greps for 'http' to one that will > grep for the url variable that is used in the connect string. > > I was hoping I could use something like p=re.compile((url).*?'"') but this > is clearly not the right syntax. Apologies for such a newbie question! We live for newbie questions :-) I'm not too sure what you want to do. In general, if you have a string in a variable and you want to include that string in a regex, you should build a new string, then compile that. In your case, the way to do what you asked for is p = re.compile(url + ".*?") But I don't think this will do anything useful, for a couple of reasons. It finds the exact URL followed by any text. So the first match will match from the url to the end of the text. If your URL has a path component - for example http://some.domain.com/index.html - then the regex will not find other URLs in the domain, such as http://some.domain.com/good/stuff/index.html. You should look at BeautifulSoup, it is an add-on module that parses HTML and makes it easy to extract links. You also might be interested in the urlparse module, which has functions which break up a URL into components. http://personalpages.tds.net/~kent37/kk/00009.html # Intro to BeautifulSoup http://docs.python.org/library/urlparse.html Kent From hihiren1 at gmail.com Wed Apr 8 14:43:38 2009 From: hihiren1 at gmail.com (Kumar) Date: Wed, 8 Apr 2009 18:13:38 +0530 Subject: [Tutor] problem in replacing regex In-Reply-To: <1c2a2c590904080334p36b10338ue2516e72ae9f86d7@mail.gmail.com> References: <20090406170636.7b9d613f@o> <20090407112933.47478155@o> <20090408093424.40665011@o> <1c2a2c590904080334p36b10338ue2516e72ae9f86d7@mail.gmail.com> Message-ID: Hello Denis/Kent, Sorry if I created any confusion. Here is my value: myString = """ https://hello.com/accid/12345-12 /12345-12 http://sadfsdf.com/12345-12 http://sadfsdf.com/asdf/asdf/asdf/12345-12 12345-12 """ so above statements are the value of myString (one string only). I couldn't try denis' suggestion because I have to replace all occurrences of 12345-12 at one go only. Here I am trying to convert 12345-12 into 12345-12 so it will make all occurrences of 12345-12 as hyperlink. Now here I want to exclude existing hyperlinks which has 12345-12 (e.g. http://sadfsdf.com/[^/] and http://sadfsdf.com/asdf/asdf/asdf/12345-12 and like that) I have applied one thing like re.sub('([^/][0-9]{6}-[0-9]{2})',r'\1;', text ) but i think as kent mentioned i may be wrong about the working of [^/][0-9]{6}-[0-9]{2}). I assumed that while replacing it would ignore 12345-12 which has '/' prefixed. (i.e. /12345-12 ) this is just an example. I tried applying [^(http).*][0-9]{6}-[0-9]{2}) , as per my assumption it would ignore all the 12345-12 for which word is beginning with http. I may be wrong at this assumption. Please help how can i exclude number starting with http? Regards, Kumar On Wed, Apr 8, 2009 at 4:04 PM, Kent Johnson wrote: > > Kumar s'exprima ainsi: > > > >> Hi Danis, > >> > >> Just to be more specific that I can add [^/] in my expression which will > >> successfully work for url (i.e. http://sdfs/123-34) but it will also > work > >> for non url (i.e. /123-34 ) so u am just trying it to make it specific > for > >> url only > >> I have already tried [^(http).*/] but that also failed and it didn't > work on > >> /123-34 > > The [^] don't do what you think they do. > > In a regex, [abc] means, match a single character that is either a, b, > or c. [^abc] means match a single character that is not a, not b and > not c. It does *not* mean to match a three-letter string that is not > abc, or anything like that. > > I think you are trying to write a negative look-behind, something like > (? > Did you try Denis' suggestion of a single regex to match both patterns? > > Kent > > PS Please use Reply All to reply to the list. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From MPirritano at ochca.com Wed Apr 8 17:46:44 2009 From: MPirritano at ochca.com (Pirritano, Matthew) Date: Wed, 8 Apr 2009 08:46:44 -0700 Subject: [Tutor] why is unicode converted file double spaced? In-Reply-To: <1c2a2c590904080339y4202e4d0x729a1e1bcc537166@mail.gmail.com> References: <49DA1488.5030701@gmail.com><97D6F0A82A6E894DAF44B9F575305CC9081862BE@HCAMAIL03.ochca.com><40af687b0904071011pdb609d2ub7b64f007bffc0b5@mail.gmail.com><97D6F0A82A6E894DAF44B9F575305CC9081862FF@HCAMAIL03.ochca.com><40af687b0904071032i7b0da02vce4ef51b97375e3d@mail.gmail.com><97D6F0A82A6E894DAF44B9F575305CC908186340@HCAMAIL03.ochca.com><40af687b0904071105t58fc12feg2467477c54ba3117@mail.gmail.com><97D6F0A82A6E894DAF44B9F575305CC9081863B7@HCAMAIL03.ochca.com><1c2a2c590904071454l65547b55u892a25c80493e3d1@mail.gmail.com><20090408095554.44664403@o> <1c2a2c590904080339y4202e4d0x729a1e1bcc537166@mail.gmail.com> Message-ID: <97D6F0A82A6E894DAF44B9F575305CC908186686@HCAMAIL03.ochca.com> Excellent! Thanks! After specifying the output encoding as cp1252 I ran this short syntax and got zero errors. Thanks to Jon Peck from the SPSS list who also weighed in. Thanks matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 -----Original Message----- From: tutor-bounces+mpirritano=ochca.com at python.org [mailto:tutor-bounces+mpirritano=ochca.com at python.org] On Behalf Of Kent Johnson Sent: Wednesday, April 08, 2009 3:40 AM To: spir Cc: tutor at python.org Subject: Re: [Tutor] why is unicode converted file double spaced? On Wed, Apr 8, 2009 at 3:55 AM, spir wrote: > Le Tue, 7 Apr 2009 17:54:42 -0400, > Kent Johnson s'exprima ainsi: > >> > ??? outp.write(outLine.strip()+'\n') > >> UnicodeEncodeError: 'ascii' codec can't encode characters in position >> 640-641: ordinal not in range(128) > > Hem, sorry for stepping in the thread. Isn't the issue that lines previously decoded as utf16 are now expanded with an ASCII-by-default NL char? Thought that when one wants to cope with unicode all literals need to be ?explicitely formed as unicode strings. Yes, we have not been explicit about the conversion, that is my fault. But in this case conversion to ascii is the desired behaviour so as long as the input falls within the ascii character set all is well. > Otherwise python silently falls back to ascii default and one gets such getting-on-the-nerves UnicodeEncodeError messages. (A bit like if when adding an int and a float python tried to produce an int result, imo.) > As the source was utf16, it may contain non ascii chars, so the whole output should be managed as unicode, no? Yes, an explicit call to encode() with the desired error parameter is one solution, as I indicated in a previous email. Kent _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor From mebooakhter at yahoo.com Wed Apr 8 19:23:54 2009 From: mebooakhter at yahoo.com (Muneeb Akhter) Date: Wed, 8 Apr 2009 10:23:54 -0700 (PDT) Subject: [Tutor] Creating an uncompile module? Message-ID: <276087.78844.qm@web90304.mail.mud.yahoo.com> I am registered on your list serv. so I geuss I could?get some help on a personal project that will read bytecode?for python 2.6?and spit out the original?code or atleast?the steps.?Does?something like this already?exist (must be?something very similar)? Or could?you give me some information of how the code is compiled? ? Thanks, -Muneeb -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Apr 8 19:52:13 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 8 Apr 2009 13:52:13 -0400 Subject: [Tutor] Creating an uncompile module? In-Reply-To: <276087.78844.qm@web90304.mail.mud.yahoo.com> References: <276087.78844.qm@web90304.mail.mud.yahoo.com> Message-ID: <1c2a2c590904081052p49325518r96b9a99e1f7ffae7@mail.gmail.com> On Wed, Apr 8, 2009 at 1:23 PM, Muneeb Akhter wrote: > I am registered on your list serv. so I geuss I could?get some help on a > personal project that will read bytecode?for python 2.6?and spit out the > original?code or atleast?the steps.?Does?something like this already?exist > (must be?something very similar)? Or could?you give me some information of > how the code is compiled? If you google "python decompiler" you will find an older decompile that does not work on 2.6 and perhaps a commercial version that is up-to-date. The python bytecodes are described here: http://docs.python.org/library/dis.html#python-bytecode-instructions Kent From kent37 at tds.net Wed Apr 8 21:59:25 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 8 Apr 2009 15:59:25 -0400 Subject: [Tutor] Python Lists--A Suggestion In-Reply-To: <49DC202F.3060401@sbcglobal.net> References: <49DB9835.4010008@sbcglobal.net> <49DBFA27.5010900@sbcglobal.net> <1c2a2c590904071924m496bf324kcf77060a1b82de02@mail.gmail.com> <49DC202F.3060401@sbcglobal.net> Message-ID: <1c2a2c590904081259k198c821ft30f9f8e044dd5506@mail.gmail.com> On Tue, Apr 7, 2009 at 11:55 PM, Wayne Watson wrote: > My suggestion is aimed at those who want to use graphic > material to support their questions or problems. Make a postable web site > called something like www.python-tutor-pix-post.net, where one could easily > put something there for 30 days. At the end of 30 days, it goes away. How about http://sendspace.com/ ? Kent From sierra_mtnview at sbcglobal.net Wed Apr 8 22:27:27 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Wed, 08 Apr 2009 13:27:27 -0700 Subject: [Tutor] Python Lists--A Suggestion In-Reply-To: <1c2a2c590904081259k198c821ft30f9f8e044dd5506@mail.gmail.com> References: <49DB9835.4010008@sbcglobal.net> <49DBFA27.5010900@sbcglobal.net> <1c2a2c590904071924m496bf324kcf77060a1b82de02@mail.gmail.com> <49DC202F.3060401@sbcglobal.net> <1c2a2c590904081259k198c821ft30f9f8e044dd5506@mail.gmail.com> Message-ID: <49DD08AF.10104@sbcglobal.net> An HTML attachment was scrubbed... URL: From emile at fenx.com Wed Apr 8 23:46:08 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 08 Apr 2009 14:46:08 -0700 Subject: [Tutor] Python Lists--A Suggestion In-Reply-To: <49DBFA27.5010900@sbcglobal.net> References: <49DB9835.4010008@sbcglobal.net> <49DBFA27.5010900@sbcglobal.net> Message-ID: Wayne Watson wrote: > Perhaps the Pyton organization could produce their mail lists, if they > have any control over them. Maybe that's the source of the > inconsistency? Has anyone pointed out that posting through gmane eliminates the inconsistency? Emile From iwasroot at gmail.com Thu Apr 9 00:05:46 2009 From: iwasroot at gmail.com (Moos Heintzen) Date: Wed, 8 Apr 2009 15:05:46 -0700 Subject: [Tutor] problem in replacing regex In-Reply-To: References: Message-ID: <7b13ba330904081505w71f45c3bhc59cf12f90252a3e@mail.gmail.com> Hi, You can do the substitution in many ways. You can first search for bare account numbers and substitute them with urls. Then substitute urls into tags. To substitute account numbers that aren't in urls, you simply substitutes account numbers if they don't start with a "/", as you have been trying to do. re.sub() can accept a function instead of a string. The function receives the match object and returns a replacement. This way you can do extra processing to matches. import re text = """https://hello.com/accid/12345-12 12345-12 http://sadfsdf.com/asdf/asdf/asdf/12345-12 start12345-12end this won't be replaced start/123-45end """ def sub_num(m): if m.group(1) == '/': return m.group(0) else: # put url here return m.group(1) + 'http://example.com/' + m.group(2) >>> print re.sub(r'(\D)(\d+-\d+)', sub_num , text) https://hello.com/accid/12345-12 http://example.com/12345-12 http://sadfsdf.com/asdf/asdf/asdf/12345-12 starthttp://example.com/12345-12end this won't be replaced start/123-45end >>> _ This is assuming there isn't any tags in the input, so you should do this before substituting urls into tags. I have super cow powers! Moos From kent37 at tds.net Thu Apr 9 00:07:26 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 8 Apr 2009 18:07:26 -0400 Subject: [Tutor] Python Lists--A Suggestion In-Reply-To: <49DD08AF.10104@sbcglobal.net> References: <49DB9835.4010008@sbcglobal.net> <49DBFA27.5010900@sbcglobal.net> <1c2a2c590904071924m496bf324kcf77060a1b82de02@mail.gmail.com> <49DC202F.3060401@sbcglobal.net> <1c2a2c590904081259k198c821ft30f9f8e044dd5506@mail.gmail.com> <49DD08AF.10104@sbcglobal.net> Message-ID: <1c2a2c590904081507h3c597d6cldb001cb12db07765@mail.gmail.com> On Wed, Apr 8, 2009 at 4:27 PM, Wayne Watson wrote: > Sounds fine to me. Is that actually a real link though? If so, who posts > there. If not, I'd suggest, www.pytutor-sendspace.com. Yes it is real, try it! It's open to the public so anyone can post. Kent > Kent Johnson wrote: > > On Tue, Apr 7, 2009 at 11:55 PM, Wayne Watson > wrote: > > > My suggestion is aimed at those who want to use graphic > material to support their questions or problems. Make a postable web site > called something like www.python-tutor-pix-post.net, where one could easily > put something there for 30 days. At the end of 30 days, it goes away. > > > How about http://sendspace.com/ ? > > Kent > > > > -- > > Wayne Watson (Watson Adventures, Prop., Nevada City, CA) > > (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time) > > "Less than all cannot satisfy Man." -- William Blake > > From david at abbottdavid.com Thu Apr 9 02:38:09 2009 From: david at abbottdavid.com (David) Date: Wed, 08 Apr 2009 20:38:09 -0400 Subject: [Tutor] Consolidate reused code blocks Message-ID: <49DD4371.10703@abbottdavid.com> Hi Everyone, I have a simple todo list program with functions to add, delete, edit and print out a list of todo's. I need to understand how to remove the same blocks of code I use in each function. Should I create a class for the same block of code I am including in each function? Here are two of the functions; def main(): print '\nYour current Todo list is: \n' if os.path.exists('todo.dat'): try: fname = open('todo.dat', 'rb') data = cPickle.Unpickler(fname) todo = data.load() load_todo(todo) for k, v in todo.iteritems(): print k, v[0], v[1] finally: fname.close() menu() else: todo = {} print "You have 0 todo's" menu() def del_todo(): if os.path.exists('todo.dat'): try: fname = open('todo.dat', 'rb') data = cPickle.Unpickler(fname) todo = data.load() load_todo(todo) finally: fname.close() else: todo = {} try: print '\nYour current Todo list is: \n' for k, v in todo.iteritems(): print k answer = raw_input('\nWhich Todo do you want to remove? ') del todo[answer] print '\nDeleted Todo', answer print '\nYour current Todo list is: \n' for k, v in todo.iteritems(): print k, v[0],v[1] load_todo(todo) except KeyError, e: print '\nError! Please enter the Todo to be removed.\n' print 'Case and spaces are important.' Here is the whole thing :) http://linuxcrazy.pastebin.com/f4206de5a Thanks for your time, -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From kent37 at tds.net Thu Apr 9 04:04:04 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 8 Apr 2009 22:04:04 -0400 Subject: [Tutor] Consolidate reused code blocks In-Reply-To: <49DD4371.10703@abbottdavid.com> References: <49DD4371.10703@abbottdavid.com> Message-ID: <1c2a2c590904081904o26747e60n6fda4be5201431e9@mail.gmail.com> On Wed, Apr 8, 2009 at 8:38 PM, David wrote: > Hi Everyone, > > I have a simple todo list program with functions to add, delete, edit and > print out a list of todo's. I need to understand how to remove the same > blocks of code I use in each function. Should I create a class for the same > block of code I am including in each function? No, you can make functions to contain common code. You are sort of on the right track with your load_todo() function, though it should really be called save_todo(). > Here are two of the > functions; > > def main(): > ? ?print '\nYour current Todo list is: \n' > ? ?if os.path.exists('todo.dat'): > ? ? ? ?try: > ? ? ? ? ? ?fname = open('todo.dat', 'rb') > ? ? ? ? ? ?data = cPickle.Unpickler(fname) > ? ? ? ? ? ?todo = data.load() > ? ? ? ? ? ?load_todo(todo) Not sure why you call load_todo() here, it writes back to the file you just read. You could have a real load_todo() that looks like this: def load_todo(): if (os.path.exists('todo.dat')): with open('todo.dat', 'rb') as fname: # with statement is simpler than try/finally data = cPickle.Unpickler(fname) todo = data.load() else: todo = {} return todo Then your main() could look like this: def main(): print '\nYour current Todo list is: \n' todo = load_todo() if todo: for k, v in todo.iteritems(): print k, v[0], v[1] else: print "You have 0 todo's" menu() Kent From david at abbottdavid.com Thu Apr 9 04:18:11 2009 From: david at abbottdavid.com (David) Date: Wed, 08 Apr 2009 22:18:11 -0400 Subject: [Tutor] [Fwd: Re: Consolidate reused code blocks] Message-ID: <49DD5AE3.6030203@abbottdavid.com> -- Powered by Gentoo GNU/Linux http://linuxcrazy.com -------------- next part -------------- An embedded message was scrubbed... From: David Subject: Re: [Tutor] Consolidate reused code blocks Date: Wed, 08 Apr 2009 22:16:18 -0400 Size: 2514 URL: From david at abbottdavid.com Thu Apr 9 04:22:02 2009 From: david at abbottdavid.com (David) Date: Wed, 08 Apr 2009 22:22:02 -0400 Subject: [Tutor] Consolidate reused code blocks In-Reply-To: <1c2a2c590904081904o26747e60n6fda4be5201431e9@mail.gmail.com> References: <49DD4371.10703@abbottdavid.com> <1c2a2c590904081904o26747e60n6fda4be5201431e9@mail.gmail.com> Message-ID: <49DD5BCA.9010606@abbottdavid.com> Kent Johnson wrote: > On Wed, Apr 8, 2009 at 8:38 PM, David wrote: >> Hi Everyone, >> >> I have a simple todo list program with functions to add, delete, edit and >> print out a list of todo's. I need to understand how to remove the same >> blocks of code I use in each function. Should I create a class for the same >> block of code I am including in each function? > > No, you can make functions to contain common code. You are sort of on > the right track with your load_todo() function, though it should > really be called save_todo(). > >> Here are two of the >> functions; >> >> def main(): >> print '\nYour current Todo list is: \n' >> if os.path.exists('todo.dat'): >> try: >> fname = open('todo.dat', 'rb') >> data = cPickle.Unpickler(fname) >> todo = data.load() >> load_todo(todo) > > Not sure why you call load_todo() here, it writes back to the file you > just read. > > You could have a real load_todo() that looks like this: > > def load_todo(): > if (os.path.exists('todo.dat')): > with open('todo.dat', 'rb') as fname: # with statement is simpler > than try/finally > data = cPickle.Unpickler(fname) > todo = data.load() > else: > todo = {} > return todo > > Then your main() could look like this: > > def main(): > print '\nYour current Todo list is: \n' > todo = load_todo() > if todo: > for k, v in todo.iteritems(): > print k, v[0], v[1] > else: > print "You have 0 todo's" > menu() > > Kent > > Ok, I got it now, rename load_todo to save_todo, then create the load_todo. thanks kent -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From sierra_mtnview at sbcglobal.net Thu Apr 9 04:32:07 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Wed, 08 Apr 2009 19:32:07 -0700 Subject: [Tutor] Python Lists--A Suggestion In-Reply-To: References: <49DB9835.4010008@sbcglobal.net> <49DBFA27.5010900@sbcglobal.net> Message-ID: <49DD5E27.9090000@sbcglobal.net> An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Thu Apr 9 04:34:27 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Wed, 08 Apr 2009 19:34:27 -0700 Subject: [Tutor] Python Lists--A Suggestion In-Reply-To: <1c2a2c590904081507h3c597d6cldb001cb12db07765@mail.gmail.com> References: <49DB9835.4010008@sbcglobal.net> <49DBFA27.5010900@sbcglobal.net> <1c2a2c590904071924m496bf324kcf77060a1b82de02@mail.gmail.com> <49DC202F.3060401@sbcglobal.net> <1c2a2c590904081259k198c821ft30f9f8e044dd5506@mail.gmail.com> <49DD08AF.10104@sbcglobal.net> <1c2a2c590904081507h3c597d6cldb001cb12db07765@mail.gmail.com> Message-ID: <49DD5EB3.2070803@sbcglobal.net> An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Apr 9 12:35:07 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 9 Apr 2009 11:35:07 +0100 Subject: [Tutor] Using Easygui in Python v3 Message-ID: I'm currently updating my tutorial to cover Python V3. I thought it miight be good to include some stuff on EasyGUI for the user input topic and on checking the web site it said Easygui was compatible with Python v3. However after downloading and installing I get a "No module named StringIO" error under Python v3. Before I start digging deeper I thought I'd ask if anyone else had the same problem and knows of a fix? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From alan.gauld at btinternet.com Thu Apr 9 12:50:12 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 9 Apr 2009 11:50:12 +0100 Subject: [Tutor] Using Easygui in Python v3 References: Message-ID: "Alan Gauld" wrote > user input topic and on checking the web site it said Easygui was > compatible with Python v3. However after downloading and installing > I get a "No module named StringIO" error under Python v3. OK, Not much digging required. StringIO has moved to io.StringIO in v3 So changing the version check code to: if runningPython3: from tkinter import * import tkinter.filedialog as tk_FileDialog from io import StringIO else: from Tkinter import * import tkFileDialog as tk_FileDialog import StringIO and deleting the original import StringIO line seems to work... If someone else could carry out a more exhaustive test than my basic EnterBox then I'll notify the easygui team. Alan G. From daychilde at gmail.com Thu Apr 9 17:47:59 2009 From: daychilde at gmail.com (Alexander Daychilde (Gmail)) Date: Thu, 9 Apr 2009 08:47:59 -0700 Subject: [Tutor] Accessing a list inside a class... Message-ID: <009701c9b92a$90279d10$b076d730$@com> I feel like I'm missing something simple, but I have now spent hours googling for an answer. I think I must not be searching for the right terms, or else this is something I'm not supposed to be doing - but it seems straightforward to me. Here's my test code (condensed from the actual much longer code I'm adapting from functions to objects - I'm new to OOP, but I'm trying J ): ___________________________________________ class TestObject: def __init__(self): # options and arguments from commandline-->OptionParser self.opt = "" self.args = [] def load_cfg(self): # read+parse commandlnie self._parse_commandline() def load_ini(self): ##### I'm trying to get at 'inifile' from the commandline... ##### print self.opt['inifile'] def _parse_commandline(self): parser = OptionParser() parser.add_option("-i", dest = "inifile", help = "specify an ini file to load") (self.opt, self.args) = parser.parse_args() if __name__ == '__main__': # parses command-line argumenmts from optparse import OptionParser test = TestObject() test.load_cfg() test.load_ini() ___________________________________________ In the middle is a comment with five hashes to show you the crux of my problem. If I eliminate "['inifile']", I can print the list. How in the world can I get just the 'inifile' element? Ideally, of course, I'm not printing this - I'm going to access it from outside the class, or even inside the class. The error I get when running the above code: Traceback (most recent call last): File "listinclass.py", line 34, in test.load_ini() File "listinclass.py", line 17, in load_ini print self.opt['inifile'] AttributeError: Values instance has no attribute '__getitem__' I've googled for "list in a class" and pretty much every variant I can think of, and I just can't seem to find a single example of someone trying to get to an element of a list stored in a class. so I apologize for asking a basic/simple question, but I just can't find it. Many thanks for your consideration. -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Thu Apr 9 18:30:49 2009 From: bgailer at gmail.com (bob gailer) Date: Thu, 09 Apr 2009 12:30:49 -0400 Subject: [Tutor] Accessing a list inside a class... In-Reply-To: <009701c9b92a$90279d10$b076d730$@com> References: <009701c9b92a$90279d10$b076d730$@com> Message-ID: <49DE22B9.1050202@gmail.com> With the small font size and lots of blank lines I find this hard to read! When I look at the optparse documentation I guess you want print self.opt.inifile Alexander Daychilde (Gmail) wrote: > > I feel like I?m missing something simple, but I have now spent hours > googling for an answer. I think I must not be searching for the right > terms, or else this is something I?m not supposed to be doing ? but it > seems straightforward to me? > > Here?s my test code (condensed from the actual much longer code I?m > adapting from functions to objects ? I?m new to OOP, but I?m trying J ): > > ___________________________________________ > > class TestObject: > > def __init__(self): > > # options and arguments from commandline-->OptionParser > > self.opt = "" > > self.args = [] > > def load_cfg(self): > > # read+parse commandlnie > > self._parse_commandline() > > def load_ini(self): > > ##### I'm trying to get at 'inifile' from the commandline... ##### > > print self.opt['inifile'] > > def _parse_commandline(self): > > parser = OptionParser() > > parser.add_option("-i", dest = "inifile", help = "specify an ini file > to load") > > (self.opt, self.args) = parser.parse_args() > > if __name__ == '__main__': > > # parses command-line argumenmts > > from optparse import OptionParser > > test = TestObject() > > test.load_cfg() > > test.load_ini() > > ___________________________________________ > > In the middle is a comment with five hashes to show you the crux of my > problem? > > If I eliminate ?[?inifile?]?, I can print the list. > > How in the world can I get just the ?inifile? element? > > Ideally, of course, I?m not printing this ? I?m going to access it > from outside the class, or even inside the class? > > The error I get when running the above code: > > Traceback (most recent call last): > > File "listinclass.py", line 34, in > > test.load_ini() > > File "listinclass.py", line 17, in load_ini > > print self.opt['inifile'] > > AttributeError: Values instance has no attribute '__getitem__' > > I?ve googled for ?list in a class? and pretty much every variant I can > think of, and I just can?t seem to find a single example of someone > trying to get to an element of a list stored in a class? so I > apologize for asking a basic/simple question, but I just can?t find it. > > Many thanks for your consideration? > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer Chapel Hill NC 919-636-4239 From denis.spir at free.fr Thu Apr 9 18:42:46 2009 From: denis.spir at free.fr (spir) Date: Thu, 9 Apr 2009 18:42:46 +0200 Subject: [Tutor] Accessing a list inside a class... In-Reply-To: <009701c9b92a$90279d10$b076d730$@com> References: <009701c9b92a$90279d10$b076d730$@com> Message-ID: <20090409184246.4a478857@o> Le Thu, 9 Apr 2009 08:47:59 -0700, "Alexander Daychilde (Gmail)" s'exprima ainsi: > The error I get when running the above code: > > Traceback (most recent call last): > > File "listinclass.py", line 34, in > > test.load_ini() > > File "listinclass.py", line 17, in load_ini > > print self.opt['inifile'] > > AttributeError: Values instance has no attribute '__getitem__' This means the following: * You're trying to access an item inside something called self.opt by using a key: 'inifile'. * self.opt should then be able to return you an item from a key, in other words it should be an object that behaves like a dictionary, right? * The "magic" method the allows this behaviour is called __getitem__ (the same as for list indexing, actually). So that python looks for this method in the class of self.opt, that happens to be Values, and tells you... Sure, not very clear! > I've googled for "list in a class" and pretty much every variant I can think > of, and I just can't seem to find a single example of someone trying to get > to an element of a list stored in a class. so I apologize for asking a > basic/simple question, but I just can't find it. The issue has nothing to do with the fact the self.opt happens to be an attribute. Denis ------ la vita e estrany From emile at fenx.com Thu Apr 9 20:40:04 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 09 Apr 2009 11:40:04 -0700 Subject: [Tutor] Accessing a list inside a class... In-Reply-To: <009701c9b92a$90279d10$b076d730$@com> References: <009701c9b92a$90279d10$b076d730$@com> Message-ID: Alexander Daychilde (Gmail) wrote: > class TestObject: > def __init__(self): > # options and arguments from commandline-->OptionParser > self.opt = "" here, self.opt is initialized as a string... > self.args = [] > def load_cfg(self): > # read+parse commandlnie > self._parse_commandline() > def load_ini(self): > ##### I'm trying to get at 'inifile' from the commandline... ##### > print self.opt['inifile'] ... and here you're accessing it as a dict > def _parse_commandline(self): > parser = OptionParser() > parser.add_option("-i", dest = "inifile", help = "specify an ini > file to load") > (self.opt, self.args) = parser.parse_args() ... and here it's changed to whatever is returned by parser.parse_args > > if __name__ == '__main__': > # parses command-line argumenmts > from optparse import OptionParser > test = TestObject() ... so when __init__ has complete, test.opt will be a string > test.load_cfg() ... and after this it's whatever is returned by parser.parse_args try print test.opt to see what was returned -- that'll probably help clear things up. If it's really a list type, you don't access those with keys, so self.opt['inifile'] would fail. > test.load_ini() HTH, Emile From scott.newton at gmail.com Thu Apr 9 23:52:17 2009 From: scott.newton at gmail.com (Scott Newton) Date: Thu, 9 Apr 2009 17:52:17 -0400 Subject: [Tutor] Finding all IP addresses associated with a host Message-ID: I'm very new to python, and my google-fu has failed to locate an answer to the following: How can I find all of the (ipv4) addresses that are currently up on a host? I realize I can call ifconfig and then munge the output with awk, but that seems messy, and before I tried that I though I would ask around. To give a bit more information, I'm writing a script that will use the ip address assigned to the wifi interface to determine location (this is on a Mac) and then mount network drives appropriate to the location. If anyone has a better idea how to do this, I'd be happy to hear them. Thanks in advance! Scott Newton -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Fri Apr 10 01:25:24 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 09 Apr 2009 16:25:24 -0700 Subject: [Tutor] Finding all IP addresses associated with a host In-Reply-To: References: Message-ID: Scott Newton wrote: > I'm very new to python, and my google-fu has failed to locate an answer > to the following: > > How can I find all of the (ipv4) addresses that are currently up on a host? In google I searched for "python what's my ip" and about the fourth entry down found a link that led me to http://pypi.python.org/pypi/netifaces/0.3 How far does that get you? HTH, Emile > > I realize I can call ifconfig and then munge the output with awk, but > that seems messy, and before I tried that I though I would ask around. > > To give a bit more information, I'm writing a script that will use the > ip address assigned to the wifi interface to determine location (this is > on a Mac) and then mount network drives appropriate to the location. If > anyone has a better idea how to do this, I'd be happy to hear them. > > Thanks in advance! > > Scott Newton > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From steve.mckmps at gmail.com Fri Apr 10 01:27:22 2009 From: steve.mckmps at gmail.com (Steve Lyskawa) Date: Thu, 9 Apr 2009 19:27:22 -0400 Subject: [Tutor] BeautifulSoup confusion Message-ID: <18f6be670904091627y27c9e729x8c2cae9354195d23@mail.gmail.com> I am not a programmer by trade but I've been using Python for 10+ years, usually for text file conversion and protocol analysis. I'm having a problem with Beautiful Soup. I can get it to scrape off all the href links on a web page but I am having problems selecting specific URI's from the output supplied by Beautiful Soup. What exactly is it returning to me and what command would I use to find that out? Do I have to take each line it give me and put it into a list before I can, for example, get only certain URI's containing a certain string or use the results to get the web page that the URI is referring to? The pseudo code for what I am trying to do: Get all URI's from web page that contain string "env.html" Open the web page it is referring to. Scrape selected information off of that page. I'm have problem with step #1. I can get all URI's but I can't see to get re.compile to work right. If I could get it to give me the URI only without tags or link description, that would be ideal. Thanks for your help. Steve Lyskawa -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Fri Apr 10 03:11:07 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 9 Apr 2009 21:11:07 -0400 Subject: [Tutor] BeautifulSoup confusion In-Reply-To: <18f6be670904091627y27c9e729x8c2cae9354195d23@mail.gmail.com> References: <18f6be670904091627y27c9e729x8c2cae9354195d23@mail.gmail.com> Message-ID: <1c2a2c590904091811gcd51491o9b1e00f374a73108@mail.gmail.com> On Thu, Apr 9, 2009 at 7:27 PM, Steve Lyskawa wrote: > I'm having a > problem with Beautiful Soup. ?I can get it to scrape off all the href links > on a web page but I am having problems selecting specific URI's from the > output supplied by Beautiful Soup. > What exactly is it returning to me and what command would I use to find that > out? Generally it gives you Tag and NavigableString objects, or lists of same. To find out what something is, print its type: print type(x) > Do I have to take each line it give me and put it into a list before I > can, for example, get only certain URI's containing a certain string or use > the results to get the web page that the URI is?referring?to? > The pseudo code for what I am trying to do: > Get all URI's from web page that contain string "env.html" > Open the web page it is referring to. > Scrape selected information off of that page. If you want to get all URI's at once, then that would imply creating a list. You could also process URI's one-at-a-time. > I'm have problem with step #1. ?I can get all URI's but I can't see to get > re.compile to work right. ?If I could get it to give me the URI only without > tags or link description, that would be ideal. Something like this should get you started: soup = BeautifulSoup() for anchor in soup.findAll('a', href=re.compile(r'env\.html')): print anchor['href'] That says, find all tags whose 'href' attribute matches the regex 'env\.html'. The Tag object will be assigned to the anchor variable. Then the value of the 'href' attribute is printed. I find it very helpful with BS to experiment at the command line. It often takes a few tries to understand what it is giving you and how to get exactly what you want. Kent From sierra_mtnview at sbcglobal.net Fri Apr 10 05:52:06 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Thu, 09 Apr 2009 20:52:06 -0700 Subject: [Tutor] What is Dani Web? Message-ID: <49DEC266.7080008@sbcglobal.net> An HTML attachment was scrubbed... URL: From scott.newton at gmail.com Fri Apr 10 07:41:14 2009 From: scott.newton at gmail.com (Scott Newton) Date: Fri, 10 Apr 2009 01:41:14 -0400 Subject: [Tutor] Finding all IP addresses associated with a host In-Reply-To: References: Message-ID: Thanks, not sure how I missed that one. -Scott On Thu, Apr 9, 2009 at 7:25 PM, Emile van Sebille wrote: > Scott Newton wrote: > >> I'm very new to python, and my google-fu has failed to locate an answer to >> the following: >> >> How can I find all of the (ipv4) addresses that are currently up on a >> host? >> > > > In google I searched for "python what's my ip" and about the fourth entry > down found a link that led me to http://pypi.python.org/pypi/netifaces/0.3 > > How far does that get you? > > HTH, > > Emile > > > > >> I realize I can call ifconfig and then munge the output with awk, but that >> seems messy, and before I tried that I though I would ask around. >> >> To give a bit more information, I'm writing a script that will use the ip >> address assigned to the wifi interface to determine location (this is on a >> Mac) and then mount network drives appropriate to the location. If anyone >> has a better idea how to do this, I'd be happy to hear them. >> >> Thanks in advance! >> >> Scott Newton >> >> >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From shiv_mbm at hotmail.com Fri Apr 10 07:58:32 2009 From: shiv_mbm at hotmail.com (ShivKumar Anand) Date: Fri, 10 Apr 2009 11:28:32 +0530 Subject: [Tutor] How to write server which listens to specific port Message-ID: Dear All, Greetings I have developed an application in Python 2.4.2, TurboGears 1.0, cherryPy 2.3.1. Now, I have to develop a server, which listens to a specific port (104) and save the file to a specific location (/tmp/myDir) on RHEL 4/5. Guide me how to do that. what all packages I have to use. Thanks in anticipation. Shiv _________________________________________________________________ The new Windows Live Messenger. You don?t want to miss this. http://www.microsoft.com/india/windows/windowslive/messenger.aspx -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Fri Apr 10 08:09:29 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 10 Apr 2009 08:09:29 +0200 Subject: [Tutor] BeautifulSoup confusion In-Reply-To: <18f6be670904091627y27c9e729x8c2cae9354195d23@mail.gmail.com> References: <18f6be670904091627y27c9e729x8c2cae9354195d23@mail.gmail.com> Message-ID: Steve Lyskawa wrote: > I am not a programmer by trade but I've been using Python for 10+ years, > usually for text file conversion and protocol analysis. I'm having a > problem with Beautiful Soup. I can get it to scrape off all the href links > on a web page but I am having problems selecting specific URI's from the > output supplied by Beautiful Soup. > What exactly is it returning to me and what command would I use to find that > out? Do I have to take each line it give me and put it into a list before I > can, for example, get only certain URI's containing a certain string or use > the results to get the web page that the URI is referring to? > > The pseudo code for what I am trying to do: > > Get all URI's from web page that contain string "env.html" > Open the web page it is referring to. > Scrape selected information off of that page. That's very easy to do with lxml.html, which offers an iterlinks() method on elements to iterate over all links in a document (not only a-href, but also in stylesheets, for example). It can parse directly from a URL, so you don't need to go through urllib and friends, and it can make links in a document absolute before iterating over them, so that relative links will work for you are doing. http://codespeak.net/lxml/lxmlhtml.html#working-with-links Also, you should use the urlparse module to split the URL (in case it contains parameters etc.) and check only the path section. Stefan From hihiren1 at gmail.com Fri Apr 10 08:39:15 2009 From: hihiren1 at gmail.com (Kumar) Date: Fri, 10 Apr 2009 12:09:15 +0530 Subject: [Tutor] problem in replacing regex In-Reply-To: <7b13ba330904081505w71f45c3bhc59cf12f90252a3e@mail.gmail.com> References: <7b13ba330904081505w71f45c3bhc59cf12f90252a3e@mail.gmail.com> Message-ID: Hey Moos/Denis/Kent, Thanks a lot for your replies and suggestions. Actually the value which I had given was separated by \n\r. I now realized that this cannot be done with one regular expression as there is one limit in negative look behind that it requires fixed width. I guess This limit is there up to python 2.7 version. So actually though I don't want, but I had to do it using splitting :( But thanks a lot for your help, this has given me better understanding the regex. Thanks you all thank you so much. Regards, Kumar On Thu, Apr 9, 2009 at 3:35 AM, Moos Heintzen wrote: > Hi, > > You can do the substitution in many ways. > > You can first search for bare account numbers and substitute them with > urls. Then substitute urls into tags. > > To substitute account numbers that aren't in urls, you simply > substitutes account numbers if they don't start with a "/", as you > have been trying to do. > > re.sub() can accept a function instead of a string. The function > receives the match object and returns a replacement. This way you can > do extra processing to matches. > > import re > > text = """https://hello.com/accid/12345-12 > > 12345-12 > > http://sadfsdf.com/asdf/asdf/asdf/12345-12 > > start12345-12end > > this won't be replaced > start/123-45end > """ > > def sub_num(m): > if m.group(1) == '/': > return m.group(0) > else: > # put url here > return m.group(1) + 'http://example.com/' + m.group(2) > > >>> print re.sub(r'(\D)(\d+-\d+)', sub_num , text) > https://hello.com/accid/12345-12 > > http://example.com/12345-12 > > http://sadfsdf.com/asdf/asdf/asdf/12345-12 > > starthttp://example.com/12345-12end > > this won't be replaced > start/123-45end > > >>> _ > > This is assuming there isn't any tags in the input, so you should > do this before substituting urls into tags. > > > I have super cow powers! > > Moos > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ksterling at mindspring.com Fri Apr 10 09:01:25 2009 From: ksterling at mindspring.com (Ken Oliver) Date: Fri, 10 Apr 2009 03:01:25 -0400 (EDT) Subject: [Tutor] What is Dani Web? Message-ID: <13807877.1239346885669.JavaMail.root@mswamui-swiss.atl.sa.earthlink.net> An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Apr 10 10:23:36 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Apr 2009 09:23:36 +0100 Subject: [Tutor] How to write server which listens to specific port References: Message-ID: "ShivKumar Anand" wrote > I have developed an application in Python 2.4.2, TurboGears 1.0, cherryPy > 2.3.1. Well done. > Now, I have to develop a server, which listens to a specific port (104) > and save the file to a specific location (/tmp/myDir) on RHEL 4/5. Does this server have anything to do with your TurboGears application? Or is it something brand new? Turbo gears can be configured to use any port using the config files. If its new, what protocol are you using? Is it http or tcp/ip or raw IP or something else? > Guide me how to do that. what all packages I have to use. At the most basic level you can use the socket module and do it all from scratch, but if its a higher lervel protocol there may be higher level modules already written that will help. We need to know a lot more about the problem and the environment - what kind of computer/OS etc. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From shiv_mbm at hotmail.com Fri Apr 10 10:59:51 2009 From: shiv_mbm at hotmail.com (ShivKumar Anand) Date: Fri, 10 Apr 2009 14:29:51 +0530 Subject: [Tutor] How to write server which listens to specific port In-Reply-To: References: Message-ID: Thanks Alan for reply. I have to implement HL7 in my existing application. so, I have to send/receive messages which are in a text file (not implementing v3.0, in which the messages are in xml format) I found tutorial on socket programming and trying from that. By importing socket. I am not sure about how to send the files. (not yet) How can we configure in TurboGears to send and receive files? The protocol I want to use is TCP/IP v4 Operating system is RHEL 5 Regards, Shiv > To: tutor at python.org > From: alan.gauld at btinternet.com > Date: Fri, 10 Apr 2009 09:23:36 +0100 > Subject: Re: [Tutor] How to write server which listens to specific port > > > "ShivKumar Anand" wrote > > > I have developed an application in Python 2.4.2, TurboGears 1.0, cherryPy > > 2.3.1. > > Well done. > > > Now, I have to develop a server, which listens to a specific port (104) > > and save the file to a specific location (/tmp/myDir) on RHEL 4/5. > > Does this server have anything to do with your TurboGears application? > Or is it something brand new? Turbo gears can be configured to use > any port using the config files. > > If its new, what protocol are you using? Is it http or tcp/ip or raw IP > or something else? > > > Guide me how to do that. what all packages I have to use. > > At the most basic level you can use the socket module and do it all > from scratch, but if its a higher lervel protocol there may be higher level > modules already written that will help. > > We need to know a lot more about the problem and the > environment - what kind of computer/OS etc. > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor _________________________________________________________________ Twice the fun?Share photos while you chat with Windows Live Messenger. http://www.microsoft.com/india/windows/windowslive/messenger.aspx -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Fri Apr 10 12:56:34 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 10 Apr 2009 06:56:34 -0400 Subject: [Tutor] How to write server which listens to specific port In-Reply-To: References: Message-ID: <1c2a2c590904100356v2c3721c7n621efef8a9f5e264@mail.gmail.com> On Fri, Apr 10, 2009 at 4:59 AM, ShivKumar Anand wrote: > I?have to implement HL7 in my existing application. > so, I have to send/receive messages which are?in a text file (not > implementing v3.0, in which the messages are in xml format) Googling "python hl7" finds a couple of hints for the server and a message parser: http://mail.python.org/pipermail/python-list/2007-March/600733.html http://pypi.python.org/pypi/hl7/0.1.0 > How can we configure in TurboGears to send and receive files? > > The protocol I want to use is TCP/IP v4 I don't think you can use TurboGears / CherryPy to create a server that is not HTTP-based. For that look at socketserver or asyncore/asynchat in the std lib or Twisted. Kent From sierra_mtnview at sbcglobal.net Fri Apr 10 12:58:32 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Fri, 10 Apr 2009 03:58:32 -0700 Subject: [Tutor] What is Dani Web? In-Reply-To: <13807877.1239346885669.JavaMail.root@mswamui-swiss.atl.sa.earthlink.net> References: <13807877.1239346885669.JavaMail.root@mswamui-swiss.atl.sa.earthlink.net> Message-ID: <49DF2658.8040005@sbcglobal.net> An HTML attachment was scrubbed... URL: From inthefridge at gmail.com Fri Apr 10 17:01:33 2009 From: inthefridge at gmail.com (Spencer Parker) Date: Fri, 10 Apr 2009 09:01:33 -0600 Subject: [Tutor] ideas on how to process a file Message-ID: I have a flat file database that I want to get information from. The basic text looks like this: ITEM { "FULLNAME" "apps114.chgit.com!file:/etc" "RECOVERY" "0" } They are all on a separate line in the text file. I had thought about trying to drop it into a list or a dictionary. Most likely a dictionary. What would be the best practice to get this into a dictionary? I had thought about using regular expressions to pull the data I need. Just not totally sure how to approach this for the most part. Not looking for solutions just ideas on how to approach the problem. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Apr 10 17:24:21 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Apr 2009 16:24:21 +0100 Subject: [Tutor] ideas on how to process a file References: Message-ID: "Spencer Parker" wrote in message >I have a flat file database that I want to get information from. The >basic > text looks like this: > > ITEM { > "FULLNAME" "apps114.chgit.com!file:/etc" > "RECOVERY" "0" > } > > They are all on a separate line in the text file. I had thought about > trying to drop it into a list or a dictionary. Most likely a dictionary. What would be the key and what would be the data? With a regular structure delimited by braces you should be able to process the file line by line fairly easily. > thought about using regular expressions to pull the data I need. You probably don;t need regex, but it depends on what exactly you want to filter/extract. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From sjpark at rondaandspencer.info Fri Apr 10 16:50:14 2009 From: sjpark at rondaandspencer.info (Spencer Parker) Date: Fri, 10 Apr 2009 08:50:14 -0600 Subject: [Tutor] Reading a file and best approach for it... Message-ID: I have a flat file database that I want to get information from. The basic text looks like this: ITEM { "FULLNAME" "apps114.chgit.com!file:/etc" "RECOVERY" "0" } They are all on a separate line in the text file. I had thought about trying to drop it into a list or a dictionary. Most likely a dictionary. What would be the best practice to get this into a dictionary? I had thought about using regular expressions to pull the data I need. Just not totally sure how to approach this for the most part. Not looking for solutions just ideas on how to approach the problem. -------------- next part -------------- An HTML attachment was scrubbed... URL: From inthefridge at gmail.com Fri Apr 10 18:04:49 2009 From: inthefridge at gmail.com (Spencer Parker) Date: Fri, 10 Apr 2009 10:04:49 -0600 Subject: [Tutor] ideas on how to process a file In-Reply-To: References: Message-ID: I forgot to respond back to the list: The only info I really need is the name. So I have been using readlines() to go through and find the lines that have fullname, if line.find("FULLNAME")>=0: I am now having issues with it writing it to a file. This is my code: http://pastebin.com/m11053edf I have it printing each line to the console just fine. But when I write it to a file it only writes the last line it finds. I got this figured out...I had the write_file in the wrong spot. It was just overwriting the file each time. Thanks again! On Fri, Apr 10, 2009 at 9:24 AM, Alan Gauld wrote: > > "Spencer Parker" wrote in message > > I have a flat file database that I want to get information from. The >> basic >> text looks like this: >> >> ITEM { >> "FULLNAME" "apps114.chgit.com!file:/etc" >> "RECOVERY" "0" >> } >> >> They are all on a separate line in the text file. I had thought about >> trying to drop it into a list or a dictionary. Most likely a dictionary. >> > > What would be the key and what would be the data? > > With a regular structure delimited by braces you should be able > to process the file line by line fairly easily. > > > thought about using regular expressions to pull the data I need. >> > > You probably don;t need regex, but it depends on what exactly > you want to filter/extract. > > HTH, > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Fri Apr 10 18:18:40 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 10 Apr 2009 12:18:40 -0400 Subject: [Tutor] ideas on how to process a file In-Reply-To: References: Message-ID: <1c2a2c590904100918l42d62d73x56868cacfbd39e67@mail.gmail.com> On Fri, Apr 10, 2009 at 12:04 PM, Spencer Parker wrote: > > This is my code: > http://pastebin.com/m11053edf I guess you have something like this now: for line in text_file.readlines(): if line.find('FULLNAME')>=0: write_file.writelines(line) This can be better written as for line in text_file: # No need for readlines(), a file is iterable if 'FULLNAME' in line: write_file.write(line) # writelines() is for writing multiple lines at once Kent From jfabiani at yolo.com Fri Apr 10 17:35:13 2009 From: jfabiani at yolo.com (johnf) Date: Fri, 10 Apr 2009 08:35:13 -0700 Subject: [Tutor] trouble understanding the difference Message-ID: <200904100835.13446.jfabiani@yolo.com> I can get the following to work: os.system('lp -d printer invoice.pdf') but what I think is the correct way for the future is: subprocess.Popen('lp -d printer invoice.pdf') and it does not work??? Can someone explain what I'm doing wrong? Traceback (most recent call last): File "/home/johnf/downloads/dabo/dabo/ui/uiwx/dControlMixin.py", line 27, in _onWxHit self.raiseEvent(dEvents.Hit, evt, *args, **kwargs) File "/home/johnf/downloads/dabo/dabo/ui/uiwx/dPemMixin.py", line 949, in raiseEvent super(dPemMixin, self).raiseEvent(eventClass, nativeEvent, *args, **kwargs) File "/home/johnf/downloads/dabo/dabo/lib/eventMixin.py", line 93, in raiseEvent bindingFunction(event) File "RptForm1.py", line 222, in doReport sendtoprint = subprocess.Popen('lp -d printer invoice.pdf') File "/usr/lib/python2.5/subprocess.py", line 593, in __init__ errread, errwrite) File "/usr/lib/python2.5/subprocess.py", line 1135, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory -- John Fabiani From jfabiani at yolo.com Fri Apr 10 18:41:13 2009 From: jfabiani at yolo.com (johnf) Date: Fri, 10 Apr 2009 09:41:13 -0700 Subject: [Tutor] trouble understanding the difference In-Reply-To: <200904100835.13446.jfabiani@yolo.com> References: <200904100835.13446.jfabiani@yolo.com> Message-ID: <200904100941.13256.jfabiani@yolo.com> On Friday 10 April 2009 08:35:13 am johnf wrote: > I can get the following to work: > os.system('lp -d printer invoice.pdf') > > but what I think is the correct way for the future is: > subprocess.Popen('lp -d printer invoice.pdf') > and it does not work??? > > Can someone explain what I'm doing wrong? > > Traceback (most recent call last): > File "/home/johnf/downloads/dabo/dabo/ui/uiwx/dControlMixin.py", line 27, > in _onWxHit > self.raiseEvent(dEvents.Hit, evt, *args, **kwargs) > File "/home/johnf/downloads/dabo/dabo/ui/uiwx/dPemMixin.py", line 949, in > raiseEvent > super(dPemMixin, self).raiseEvent(eventClass, nativeEvent, *args, > **kwargs) > File "/home/johnf/downloads/dabo/dabo/lib/eventMixin.py", line 93, in > raiseEvent > bindingFunction(event) > File "RptForm1.py", line 222, in doReport > sendtoprint = subprocess.Popen('lp -d printer invoice.pdf') > File "/usr/lib/python2.5/subprocess.py", line 593, in __init__ > errread, errwrite) > File "/usr/lib/python2.5/subprocess.py", line 1135, in _execute_child > raise child_exception > OSError: [Errno 2] No such file or directory I tried subprocess.call('lp -d priner invoice.pdf') and I get the same error message. I have not changed the subject line even though I made it to general. -- John Fabiani From inthefridge at gmail.com Fri Apr 10 18:43:31 2009 From: inthefridge at gmail.com (Spencer Parker) Date: Fri, 10 Apr 2009 10:43:31 -0600 Subject: [Tutor] ideas on how to process a file In-Reply-To: <1c2a2c590904100918l42d62d73x56868cacfbd39e67@mail.gmail.com> References: <1c2a2c590904100918l42d62d73x56868cacfbd39e67@mail.gmail.com> Message-ID: Oh...nice...this makes things much easier than what I had before. I mainly used writelines because I couldn't figure out why it was only writing one line. Then I did and never took out the writelines...I just did and it works just fine for the most part. Thanks again for the help. On Fri, Apr 10, 2009 at 10:18 AM, Kent Johnson wrote: > On Fri, Apr 10, 2009 at 12:04 PM, Spencer Parker > wrote: > > > > This is my code: > > http://pastebin.com/m11053edf > > I guess you have something like this now: > > for line in text_file.readlines(): > if line.find('FULLNAME')>=0: > write_file.writelines(line) > > This can be better written as > > for line in text_file: # No need for readlines(), a file is iterable > if 'FULLNAME' in line: > write_file.write(line) # writelines() is for writing multiple lines at > once > > Kent > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jfabiani at yolo.com Fri Apr 10 18:47:13 2009 From: jfabiani at yolo.com (johnf) Date: Fri, 10 Apr 2009 09:47:13 -0700 Subject: [Tutor] trouble understanding the difference In-Reply-To: <200904100941.13256.jfabiani@yolo.com> References: <200904100835.13446.jfabiani@yolo.com> <200904100941.13256.jfabiani@yolo.com> Message-ID: <200904100947.13899.jfabiani@yolo.com> On Friday 10 April 2009 09:41:13 am johnf wrote: > On Friday 10 April 2009 08:35:13 am johnf wrote: > > I can get the following to work: > > os.system('lp -d printer invoice.pdf') > > > > but what I think is the correct way for the future is: > > subprocess.Popen('lp -d printer invoice.pdf') > > and it does not work??? > > > > Can someone explain what I'm doing wrong? Thanks to "Weidner, Ronald" the correct way is subprocess.Popen(['lp', '-d', 'printer', 'invoice.pdf']) -- John Fabiani From smiles at worksmail.net Fri Apr 10 18:34:30 2009 From: smiles at worksmail.net (C or L Smith) Date: Fri, 10 Apr 2009 22:19:30 +0545 Subject: [Tutor] how to make classes play well together Message-ID: <31CBE227E16E442D86480FE5D8C27AA1@kisc.edu.np> Unum is a module that allows one to handle units with numbers, e.g. 3*M -> 3 [m], a measurement of 3 meters. I have a module that I wrote some time ago that handles uncertainties with numbers as they are involved with calculations. Let's call it the pnum module (for physical numbers) e.g. pnum(3,.1) specifies a value of 3 +/- 0.1. I worked with someone at that time of writing it who said..."it would be nice to get this working with Unum." Well...it's that time, perhaps, but I could use some help in knowing what's the best way to go. (Q1) My basic question is how to have these two work together. If they are to be two separate entities, my routines need to check whether they are dealing with a unum or not before doing certain operations. e.g. my routines define "x" and "u" attributes for the numbers, but if the pnum gets wrapped up with a unum then asking for x and u no longer works. I have to extract the pnum part from the pnum/unum composite before asking for x and u. But...does that mean I have to be unum aware in the pnum module? i.e. I have to import unum (actually Unum from unum) so I can figure out what I am dealing with in the routines that I have defined, e.g. def __mul__(self, other): if isinstance(other,Unum): return other*self else: return pnum(self.x*other.x,math.hypot(other.u*self.x,other.x*self.u),1) or def cos(x): if isinstance(x,pnum): return x.cos() elif isinstance(x,Unum): arg = x.asNumber(1) if isinstance(arg,pnum):#it's a pnum unum return pnum(arg[0],arg[1],1).cos() else:#it's a pure unum return math.cos(arg) return math.cos(x) Since unums only have a simple numeric portion, they don't need a special cos() routine, but my pnum's have two numbers to keep track of and so there *is* a special cos() routine. When a script calls for cos(x) my module handles it, checks to see who x is and then acts accordingly. I can't just have a test for whether x is a pnum and if not, pass off the result to math.cos(x) because math.cos() will ask for a float, unum will ask pnum for a float representation...and I don't know what to give back. If I give back the value part of pnum then I lose the uncertainty portion. If I give back the value, uncertainty pnum tuple it generate "TypeError: __float__ returned non-float (type instance)". So...you can see what I do to try handle the situation with the cos() routine above. Which brings me to the 2nd question. (Q2) If someone doesn't have unum installed, they should still be able to run pnum...but now it has tests for unums sprinkled all throughout it. Do I now have to put the tests for unums inside a try/except sort of structure to allow pnum to run if unum isn't present? Right now everything is working only so long as pnum imports unum so all the tests work. The two directions I would go now (not knowing any better) would be to a) put unum tests (and the import itself?) into try/except blocks or b) weave pnums into unum so that unums get a new attribute and all the instructions on how to deal with the value,uncertainty tuple that every numeric part of a unum would have (instead of just a float). Thanks for help in pointing me in a sane direction, /c From inthefridge at gmail.com Fri Apr 10 19:00:27 2009 From: inthefridge at gmail.com (Spencer Parker) Date: Fri, 10 Apr 2009 11:00:27 -0600 Subject: [Tutor] ideas on how to process a file In-Reply-To: References: <1c2a2c590904100918l42d62d73x56868cacfbd39e67@mail.gmail.com> Message-ID: The question is now...what do I do to find duplicate entries in the text file I am reading. I just want to filter them out. There are a ton of duplicate entries in there. On Fri, Apr 10, 2009 at 10:43 AM, Spencer Parker wrote: > Oh...nice...this makes things much easier than what I had before. > > I mainly used writelines because I couldn't figure out why it was only > writing one line. Then I did and never took out the writelines...I just did > and it works just fine for the most part. > > Thanks again for the help. > > > On Fri, Apr 10, 2009 at 10:18 AM, Kent Johnson wrote: > >> On Fri, Apr 10, 2009 at 12:04 PM, Spencer Parker >> wrote: >> > >> > This is my code: >> > http://pastebin.com/m11053edf >> >> I guess you have something like this now: >> >> for line in text_file.readlines(): >> if line.find('FULLNAME')>=0: >> write_file.writelines(line) >> >> This can be better written as >> >> for line in text_file: # No need for readlines(), a file is iterable >> if 'FULLNAME' in line: >> write_file.write(line) # writelines() is for writing multiple lines at >> once >> >> Kent >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Fri Apr 10 19:12:15 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Fri, 10 Apr 2009 19:12:15 +0200 Subject: [Tutor] ideas on how to process a file In-Reply-To: References: <1c2a2c590904100918l42d62d73x56868cacfbd39e67@mail.gmail.com> Message-ID: 2009/4/10 Spencer Parker : > The question is now...what do I do to find duplicate entries in the text > file I am reading.? I just want to filter them out.? There are a ton of > duplicate entries in there. >>> for line in text_file: # No need for readlines(), a file is iterable >>> ?if 'FULLNAME' in line: >>> ? ?write_file.write(line) ?# writelines() is for writing multiple lines >>> at once results = [] for line in text_file: if 'FULLNAME' in line and line not in results: results.append(line) write_file.write(line) Now you write out line immeditaly or wait and write out the results list. Greets Sander From kent37 at tds.net Fri Apr 10 19:37:11 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 10 Apr 2009 13:37:11 -0400 Subject: [Tutor] ideas on how to process a file In-Reply-To: References: <1c2a2c590904100918l42d62d73x56868cacfbd39e67@mail.gmail.com> Message-ID: <1c2a2c590904101037qaf8a5c1xccb1585df4fdf44e@mail.gmail.com> On Fri, Apr 10, 2009 at 1:12 PM, Sander Sweers wrote: > results = [] > > for line in text_file: > ? ?if 'FULLNAME' in line and line not in results: > ? ? ? ?results.append(line) > ? ? ? ?write_file.write(line) In general it is better to use a set for this kind of application, it will have much better performance if the number of lines is large. results = set() ... results.add(line) Kent From kent37 at tds.net Fri Apr 10 19:47:04 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 10 Apr 2009 13:47:04 -0400 Subject: [Tutor] how to make classes play well together In-Reply-To: <31CBE227E16E442D86480FE5D8C27AA1@kisc.edu.np> References: <31CBE227E16E442D86480FE5D8C27AA1@kisc.edu.np> Message-ID: <1c2a2c590904101047g7f0666c0m103f5f53a1aa163e@mail.gmail.com> On Fri, Apr 10, 2009 at 12:34 PM, C or L Smith wrote: > Unum is a module that allows one to handle units with numbers, e.g. 3*M -> 3 [m], a measurement of 3 meters. > > I have a module that I wrote some time ago that handles uncertainties with numbers as they are involved with calculations. Let's call it the pnum module (for physical numbers) e.g. pnum(3,.1) specifies a value of 3 +/- 0.1. > > I worked with someone at that time of writing it who said..."it would be nice to get this working with Unum." Well...it's that time, perhaps, but I could use some help in knowing what's the best way to go. > > (Q1) My basic question is how to have these two work together. If they are to be two separate entities, my routines need to check whether they are dealing with a unum or not before doing certain operations. e.g. my routines define "x" and "u" attributes for the numbers, but if the pnum gets wrapped up with a unum then asking for x and u no longer works. I have to extract the pnum part from the pnum/unum composite before asking for x and u. I think I would try wrapping unums with pnums rather than the other way around, so you could have pnum(3, .1) or pnum(3*M, .1*M) and you probably want to allow pnum(3*M, .1) where the units are implied on the uncertainty. > But...does that mean I have to be unum aware in the pnum module? i.e. I have to import unum (actually Unum from unum) so I can figure out what I am dealing with in the routines that I have defined, Yes > e.g. > > ?def __mul__(self, other): > ?if isinstance(other,Unum): > ? return other*self > ?else: > ? return pnum(self.x*other.x,math.hypot(other.u*self.x,other.x*self.u),1) I don't think you would need the test for Unum, assuming math.hypot() works with unums. > ?def cos(x): > ?if isinstance(x,pnum): > ? return x.cos() > ?elif isinstance(x,Unum): > ? arg = x.asNumber(1) > ? if isinstance(arg,pnum):#it's a pnum unum > ? ?return pnum(arg[0],arg[1],1).cos() > ? else:#it's a pure unum > ? ?return math.cos(arg) > ?return math.cos(x) Here you might need some special code but not quite like this. > (Q2) If someone doesn't have unum installed, they should still be able to run pnum...but now it has tests for unums sprinkled all throughout it. Do I now have to put the tests for unums inside a try/except sort of structure to allow pnum to run if unum isn't present? You can probably do something like this, to import the actual Unum class if it is present, otherwise define a dummy class: try: from unum import Unum except: class Unum(object): pass Then any tests for isinstance(x, Unum) will fail so the code inside the if block doesn't have to work without unum. Kent From RWeidner at ea.com Fri Apr 10 19:48:53 2009 From: RWeidner at ea.com (Weidner, Ronald) Date: Fri, 10 Apr 2009 10:48:53 -0700 Subject: [Tutor] ideas on how to process a file In-Reply-To: References: <1c2a2c590904100918l42d62d73x56868cacfbd39e67@mail.gmail.com> Message-ID: In my last e-mail I think I suggested making an Item object. This time I'll suggest an ItemCollection object. Now you can have an Add(self, item) that could validate. This could be some rather simple loop and test logic. -- Ronald Weidner From: tutor-bounces+rweidner=ea.com at python.org [mailto:tutor-bounces+rweidner=ea.com at python.org] On Behalf Of Spencer Parker Sent: Friday, April 10, 2009 1:00 PM To: Kent Johnson Cc: Alan Gauld; tutor at python.org Subject: Re: [Tutor] ideas on how to process a file The question is now...what do I do to find duplicate entries in the text file I am reading. I just want to filter them out. There are a ton of duplicate entries in there. On Fri, Apr 10, 2009 at 10:43 AM, Spencer Parker > wrote: Oh...nice...this makes things much easier than what I had before. I mainly used writelines because I couldn't figure out why it was only writing one line. Then I did and never took out the writelines...I just did and it works just fine for the most part. Thanks again for the help. On Fri, Apr 10, 2009 at 10:18 AM, Kent Johnson > wrote: On Fri, Apr 10, 2009 at 12:04 PM, Spencer Parker > wrote: > > This is my code: > http://pastebin.com/m11053edf I guess you have something like this now: for line in text_file.readlines(): if line.find('FULLNAME')>=0: write_file.writelines(line) This can be better written as for line in text_file: # No need for readlines(), a file is iterable if 'FULLNAME' in line: write_file.write(line) # writelines() is for writing multiple lines at once Kent -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan at lsd.co.za Fri Apr 10 20:44:37 2009 From: stefan at lsd.co.za (Stefan Lesicnik) Date: Fri, 10 Apr 2009 20:44:37 +0200 Subject: [Tutor] OOP / Classes questions Message-ID: <5cb309e70904101144h75347dc1r72a6dd3edd29718d@mail.gmail.com> Hi guys, I am a relative newbie to python and OOP concepts and am trying to work through wxpython. I've seen understanding classes is essential to this and have been trying to work through them. I have a few questions re the below code (i suspect its due to a lack of my understanding of classes) 1. I am not sure why / when you use the self. notation. I understand that self refers to the instance you create, so you can have multiple instances and the class knows which one you are talking about. Why do they use self.Bind or similar and at other times use file = (without the self) 2. In the Bind( they use self.OnQuit which seems to call the function OnQuit). Again, why is it self.OnQuit? 3. def OnQuit(self, event): - where does event here come from and what does it do with it? I guess as a general kind of question, can anyone recommend some tutorial or documentation re OOP and classes? thanks! stefan #!/usr/bin/python # menuexample.py import wx class MenuExample(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(250, 150)) menubar = wx.MenuBar() file = wx.Menu() quit = wx.MenuItem(file, 1, '&Quit\tCtrl+Q') quit.SetBitmap(wx.Bitmap('icons/exit.png')) file.AppendItem(quit) self.Bind(wx.EVT_MENU, self.OnQuit, id=1) menubar.Append(file, '&File') self.SetMenuBar(menubar) self.Centre() self.Show(True) def OnQuit(self, event): self.Close() app = wx.App() MenuExample(None, -1, '') app.MainLoop() From jsseabold at gmail.com Fri Apr 10 20:59:03 2009 From: jsseabold at gmail.com (Skipper Seabold) Date: Fri, 10 Apr 2009 14:59:03 -0400 Subject: [Tutor] OOP / Classes questions In-Reply-To: <5cb309e70904101144h75347dc1r72a6dd3edd29718d@mail.gmail.com> References: <5cb309e70904101144h75347dc1r72a6dd3edd29718d@mail.gmail.com> Message-ID: On Fri, Apr 10, 2009 at 2:44 PM, Stefan Lesicnik wrote: > I guess as a general kind of question, can anyone recommend some > tutorial or documentation re OOP and classes? I've found Alan Gauld's tutorials to be very useful when just getting started in both OOP and Python. http://www.freenetpages.co.uk/hp/alan.gauld/ hth, Skipper From steve at alchemy.com Fri Apr 10 20:53:02 2009 From: steve at alchemy.com (Steve Willoughby) Date: Fri, 10 Apr 2009 11:53:02 -0700 Subject: [Tutor] OOP / Classes questions In-Reply-To: <5cb309e70904101144h75347dc1r72a6dd3edd29718d@mail.gmail.com> References: <5cb309e70904101144h75347dc1r72a6dd3edd29718d@mail.gmail.com> Message-ID: <20090410185302.GB78523@dragon.alchemy.com> On Fri, Apr 10, 2009 at 08:44:37PM +0200, Stefan Lesicnik wrote: > I am a relative newbie to python and OOP concepts and am trying to > work through wxpython. I've seen understanding classes is essential to > this and have been trying to work through them. Welcome! If you're really new, wxpython may be a bit advanced, so if it feels like you're just not getting what's going on, you might want to step back a level and try simpler projects and work back up to this. > 1. I am not sure why / when you use the self. notation. I understand > that self refers to the instance you create, so you can have multiple > instances and the class knows which one you are talking about. > Why do they use self.Bind or similar and at other times use file = > (without the self) Inside the definition of a class method such as MenuExample.__init__() in your included code (and thank you, by the way, for including actual code to discuss!), if you assign to a variable such as file = wx.Menu() this is referring to a *local* variable "file" which will exist as long as the __init__() method is actually running (each time) and then get deleted when that function finishes running. It's a temporary variable, in other words. On the other hand, something like self.file = wx.Menu() would store this in an *instance* variable "self.file" which will exist as long as that object instance does, stored into the object itself so to speak. Calling a method like self.Bind(...) means to invoke the Bind method on this same object instance. If you wanted to call the Bind method on object foo, you'd say foo.Bind() so this is the same thing, just referring to the same object that's in the middle of its own __init__() call at the time. > 2. In the Bind( they use self.OnQuit which seems to call the function > OnQuit). Again, why is it self.OnQuit? Because it's not simply a function, it's a method of the class, and specifically you're asking the individual object "self" to perform its OnQuit action. > 3. def OnQuit(self, event): - where does event here come from and what > does it do with it? Here's one reason I said wxpython is pretty advanced. GUI code tends to be event-driven, so as the user triggers events such as clicking on buttons or typing keys, those events cause methods to be called in the objects controlling those on-screen widgets. So if, say, the user clicked a "quit" button, WX says that a button-press event occurred, and will call the OnQuit method here with information about the event in the object called "event". Your method could just ignore that parameter if you don't care about that, or you could examine it to see details as to what event led you to the OnQuit action. -- Steve Willoughby | Using billion-dollar satellites steve at alchemy.com | to hunt for Tupperware. From sander.sweers at gmail.com Fri Apr 10 23:08:55 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Fri, 10 Apr 2009 23:08:55 +0200 Subject: [Tutor] Make beautifulsoup show the data it has an issue with Message-ID: Hello Tutors, I am having some issues with a malformed tag in a html page. BeautifulSoup barfs with the following. raceback (most recent call last): File "", line 1, in tsoup = BeautifulSoup(readPage('http://url.sanitized')) File "C:\Python25\lib\site-packages\BeautifulSoup.py", line 1493, in __init__ BeautifulStoneSoup.__init__(self, *args, **kwargs) File "C:\Python25\lib\site-packages\BeautifulSoup.py", line 1224, in __init__ self._feed(isHTML=isHTML) File "C:\Python25\lib\site-packages\BeautifulSoup.py", line 1257, in _feed self.builder.feed(markup) File "C:\Python25\lib\HTMLParser.py", line 108, in feed self.goahead(0) File "C:\Python25\lib\HTMLParser.py", line 148, in goahead k = self.parse_starttag(i) File "C:\Python25\lib\HTMLParser.py", line 226, in parse_starttag endpos = self.check_for_whole_start_tag(i) File "C:\Python25\lib\HTMLParser.py", line 301, in check_for_whole_start_tag self.error("malformed start tag") File "C:\Python25\lib\HTMLParser.py", line 115, in error raise HTMLParseError(message, self.getpos()) HTMLParseError: malformed start tag, at line 167, column 73 How can I make it return the data it has an issue with? Thanks Sander From kent37 at tds.net Fri Apr 10 23:19:29 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 10 Apr 2009 17:19:29 -0400 Subject: [Tutor] Make beautifulsoup show the data it has an issue with In-Reply-To: References: Message-ID: <1c2a2c590904101419y72bd0676t57e368017b49f101@mail.gmail.com> On Fri, Apr 10, 2009 at 5:08 PM, Sander Sweers wrote: > Hello Tutors, > > I am having some issues with a malformed tag in a html page. > BeautifulSoup barfs with the following. > > raceback (most recent call last): > ?File "C:\Python25\lib\HTMLParser.py", line 115, in error > ? ?raise HTMLParseError(message, self.getpos()) > HTMLParseError: malformed start tag, at line 167, column 73 > > How can I make it return the data it has an issue with? Read the bad url yourself and look at line 167, column 73? Or, catch the exception, have the code find out where the error is and display the bad line. Kent From i-scream_cones at hotmail.com Fri Apr 10 23:22:19 2009 From: i-scream_cones at hotmail.com (Chris Lee) Date: Fri, 10 Apr 2009 14:22:19 -0700 Subject: [Tutor] Problem with converting Python to EXE using py2exe Message-ID: Hi tutors, So I finally made my program. When I run it with python, it works perfectly! But when I convert it to an executable using py2exe (correctly), I get an error: Traceback (most recent call last): File "oentry.py", line 52, in File "Tkinter.pyc", line 3270, in __init__ File "Tkinter.pyc", line 3226, in __init__ _tkinter.TclError: Couldn't open "base.gif": no such file or directory alright,, I know what you're talking about, Mr.Python... I linked an image to the python file, but the exe can't find it.. (right?) anyways, here's the snippet of my code which uses the "base.gif" pic = tkinter.PhotoImage base = pic(file="base.gif") Now, I'm just wondering... for the "base.gif" part, would it be better to write out the whole directory? any help would be appreciated. Thanks a bunch! -Christopher "script teeny" Leeeeee _________________________________________________________________ Share photos with friends on Windows Live Messenger http://go.microsoft.com/?linkid=9650734 -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Apr 11 01:36:31 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 11 Apr 2009 00:36:31 +0100 Subject: [Tutor] OOP / Classes questions References: <5cb309e70904101144h75347dc1r72a6dd3edd29718d@mail.gmail.com> Message-ID: "Skipper Seabold" wrote >> tutorial or documentation re OOP and classes? > > I've found Alan Gauld's tutorials to be very useful when just getting > started in both OOP and Python. > > http://www.freenetpages.co.uk/hp/alan.gauld/ Thanks for the plug :-) However the URL has changed since freenetpages have been frozen so I cannot update there anymore. The new URL is http://www.alan-g.me.uk for Python v2.x tutorial and http://www.alan-g.me.uk/l2p for Python v3 tutorial (still under construction) As to the OPs questions I'll post a separate answer on that in a few minures... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Apr 11 01:51:36 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 11 Apr 2009 00:51:36 +0100 Subject: [Tutor] OOP / Classes questions References: <5cb309e70904101144h75347dc1r72a6dd3edd29718d@mail.gmail.com> Message-ID: "Stefan Lesicnik" wrote > I am a relative newbie to python and OOP concepts and am trying to > work through wxpython. I've seen understanding classes is essential to > this and have been trying to work through them. Steve gave you the basic explanation, I'll try a slightly different tack - which may or may not help! But its what I find most useful when working with objects. > 1. I am not sure why / when you use the self. notation. I understand > that self refers to the instance you create, so you can have multiple > instances and the class knows which one you are talking about. You just answered your own question :-) But. I find it best to think about objects as "actors" or robots within my application that communicate by sending each other messages. The syntax for sending a message to object foo is returnValue = foo.message() So although it *looks* like you are just calling a function, don't think of it that way, think of it as sending a message. Think that foo might be a process running on another computer, the notation is just a way of expressing that you are sending a request to foo, wherever it is, to perform some operation. The only way to get an actor to do anything it to tell it to do so via a message. That means that if an actor needs to do something to itself it needs to send itself a message. That's when you use self. When you need to send yourself a message. Either to access some data or to initiate a method (methods get invoked as a result of messages being received - that's why they are different from functons even if they look the same!) > Why do they use self.Bind or similar and at other times use file = > (without the self) without self its just a local variable or function. With self its a message to an object, which object just happens to be the same one sending the message. ( In fact in OOP circles this is actually referred to as "self messaging" ) > 2. In the Bind( they use self.OnQuit which seems to call the function > OnQuit). Again, why is it self.OnQuit? Bind doesn't call OnQuit instead it creates a reference to the OnQuit method of self. The GUI framework calls that method when the bound event occurs. Because OnQuit is a method bind must pass a reference to the object containing the method as well as the method name. So the framework then sends a message to the object when the event ocurs. > 3. def OnQuit(self, event): - where does event here come from and what > does it do with it? The event object is created by the framework when the event occurs. ie The GUI framework detects that a button has been pressed, a mouse moved etc... (and The Operating System must detect the event and pass it to the Framework before that can happen)... then it creates an event and looks to see what if any "event handlers" have been registered. If there is a handler it sends a message to the object/handler concerned with the event object attached. The handler can then inspect the event to find out which key was pressed, where the mouse moved to, etc As I say thats just a slightly different way to think about objects and OOP I personally find it very helpful, you may too. If not, don't worry about it! :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Apr 11 01:55:36 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 11 Apr 2009 00:55:36 +0100 Subject: [Tutor] Problem with converting Python to EXE using py2exe References: Message-ID: "Chris Lee" wrote > _tkinter.TclError: Couldn't open "base.gif": no such file or directory > ... I linked an image to the python file, but the exe can't find it.. > (right?) Correct > base = pic(file="base.gif") > Now, I'm just wondering... for the "base.gif" part, would it be better > to write out the whole directory? No because that requires that your user install the program and other files exactly where you did. Thats not friendly. Instead use a relative path, for example keep the images in a sub-directory of your project and have a registry setting or config file or environment variable specify where that directory is. You can then read the config data at startup and use that to create the path for the image. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From dkuhlman at rexx.com Sat Apr 11 02:25:22 2009 From: dkuhlman at rexx.com (Dave Kuhlman) Date: Fri, 10 Apr 2009 17:25:22 -0700 Subject: [Tutor] Yet another Python book Message-ID: <20090411002522.GA16288@cutter.rexx.com> I've collected my training materials plus a few more advanced topics and I've also written up a reasonably large set of Python exercises. Then, I turned all of the above into a "book". If you have comments, suggestions, corrections, etc, I'll appreciate them. You can get it at no cost, in various formats here: http://www.rexx.com/~dkuhlman/#a-python-book And, if you really do feel a need to kill a tree, you can purchase a copy on paper here: http://www.lulu.com/content/paperback-book/a-python-book/2599081 - Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman From bgailer at gmail.com Sat Apr 11 03:40:13 2009 From: bgailer at gmail.com (bob gailer) Date: Fri, 10 Apr 2009 21:40:13 -0400 Subject: [Tutor] sets module equivalent with Python 2.6 In-Reply-To: References: Message-ID: <49DFF4FD.7030108@gmail.com> PyProg PyProg wrote: > Hello all, > > I want to use an equivalent of sets module with Python 2.6 ... but > sets module is deprecated on 2.6 version. > set is now a built-in type. Replace sets.Set() with set() > In fact I want to make that but with Python 2.6: > > >>>> toto_1 = [0, 1, 2, 3, 4] >>>> toto_2 = [1, 127, 4, 7, 12] >>>> >>>> import sets >>>> a = sets.Set(toto_1) >>>> b = sets.Set(toto_2) >>>> c = a.symmetric_difference(b) >>>> d = [p for p in c] >>>> d.sort() >>>> print d >>>> Note [p for p in c] can be simplified to list(c) > [0, 2, 3, 7, 12, 127] > > Can you help me ?. > Looks like we did. > Thanks in advance. > Welcome in retrospect. -- Bob Gailer Chapel Hill NC 919-636-4239 From alan.gauld at btinternet.com Sat Apr 11 09:56:02 2009 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 11 Apr 2009 07:56:02 +0000 (GMT) Subject: [Tutor] Problem with converting Python to EXE using py2exe In-Reply-To: References: Message-ID: <207126.96892.qm@web86710.mail.ird.yahoo.com> > That's a great idea Mr. Gauld! > But I have no idea how to do that... > can you maybe write a small example for a newb like me?? pretty please with cheese on top?? > > > base = pic(file="base.gif") > > > > > > Now, I'm just wondering... for the "base.gif" part, would it be better > > > to write out the whole directory? > > > No because that requires that your user install the program and > > other files exactly where you did. Thats not friendly. Instead use > > a relative path, To use a relative path base = pic(file=root+"/base.gif") To find out what root is.... > of your project and have a registry setting Use the _winreg module to access the registry > or config file or Use config parser module to read/write to a config file > environment variable specify where that directory is. use os.getenv() to read an environment variable Assuming we go with an environment variable: called MYAPPROOT root = os.getenv('MYAPPROOT') base = pic(file=root+"/base.gif") Now all you need to do is get the installation routine (or the user) to set up the MYAPPROOT environment variable to point at the installation directory. How you do that will depend on how you produce your installer, it could be done by a DOS batch file, another pytthon script or a commercial installer. HTH Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Sat Apr 11 13:00:08 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 11 Apr 2009 07:00:08 -0400 Subject: [Tutor] Problem with converting Python to EXE using py2exe In-Reply-To: References: Message-ID: <49E07838.4020909@ieee.org> ALAN GAULD wrote: > > That's a great idea Mr. Gauld! >> > But I have no idea how to do that... >> > can you maybe write a small example for a newb like me?? pretty please with cheese on top?? >> >>>> > > > base = pic(file="base.gif") >>>> > > > >>>> > > > Now, I'm just wondering... for the "base.gif" part, would it be better >>>> > > > to write out the whole directory? >>>> >> > >> >>> > > No because that requires that your user install the program and >>> > > other files exactly where you did. Thats not friendly. Instead use >>> > > a relative path, >>> > To use a relative path base = pic(file=root+"/base.gif") I would get 'root' this way, rather than requiring anything to be set up by the install (registry, environment vars, config): root = os.path.dirname(__file__) or sometimes root = os.path.dirname(mymodule.__file__) (assuming mymodule is a successfully imported module) And then I'd turn the relative path into an absolute with: os.path.join(root, "base.gif") I use this technique whenever I need to locate something relative to the "installed" code, in other words to fetch const data that was copied with the .py files. I reserve using install variables to finding things relating to the choices the user made at install time, which may very well vary per user. And sometimes that means I can avoid install parameters entirely, letting the install be simply two steps: 1) unzip the files into an appropriate place (arbitray, and user chosen, but typically in a subdirectory of c:\program files on Windows). 2) Set up a batch file, shortcut, path string, or file association to point to the executable(s) in that directory. (Does anybody think that using __file__ this way is uncool? For example, should I better use sys.path[0] ? Or something more convoluted involving sys.modules ?) From jk_kriegel at yahoo.com Sat Apr 11 06:38:51 2009 From: jk_kriegel at yahoo.com (jake_k2011) Date: Fri, 10 Apr 2009 21:38:51 -0700 (PDT) Subject: [Tutor] Python Dice Game Message-ID: <22982720.post@talk.nabble.com> http://www.nabble.com/file/p22982720/button.py button.py http://www.nabble.com/file/p22982720/dieview.py dieview.py http://www.nabble.com/file/p22982720/graphics.py graphics.py http://www.nabble.com/file/p22982720/msdie.py msdie.py http://www.nabble.com/file/p22982720/roller.py roller.py Im' trying to get this dice game to work and i'm having the worst time to get it to work :\ I'm a total noob and just a BEGINNER. I can't seem to get the buttons to work.Basically they are constructed and maybe activated in the main program, andI need to be able to use them in the functions roll() and turn() i believe. CAn anyone help to get this to work?? roller.py #!/usr/bin/python # roller.py # Graphics program to roll a pair of dice. Uses custom widgets # Button and GDie. from random import randrange from graphics import GraphWin, Point from graphics import* from button import Button from dieview import DieView def main(): intro() scoreA = 0 scoreB = 0 # create the application window win = GraphWin("Dice Roller", 600,400) win.setCoords(0, 0, 400, 150) win.setBackground("green2") # Draw the interface widgets die1 = DieView(win, Point(90,120), 40) die2 = DieView(win, Point(210,120), 40) die3 = DieView(win, Point(90,60), 40) die4 = DieView(win, Point(210,60), 40) die5 = DieView(win, Point(150,90), 40) rollButton = Button(win, Point(150,30), 150, 10, "Roll Dice") rollButton.activate() passButton = Button(win, Point(150,10), 150, 10, "Pass Turn") passButton.activate() scoretext1 = Text(Point(300,120),"Player A Score:").draw(win) scoretext2 = Text(Point(300,100),scoreA).draw(win) scoretext3 = Text(Point(300, 70),"Player B Score:").draw(win) scoretext4 = Text(Point(300, 50),scoreB).draw(win) turn = "A" while scoreA<1000 and scoreB<1000: tempscore = turn(passButton) if turn == "A": scoreA = scoreA+tempscore scoretext2.undraw() scoretext2 = Text(Point(300,100),scoreA).draw(win) turn = "B" else: scoreB = scoreB+tempscore scoretext4.undraw() scoretext4 = Text(Point(300, 50),scoreB).draw(win) turn = "A" if scoreA >= 1000: print "Congratulations, Player A WINS!!!" else: print "Congratulations, Player B WINS!!!" raw_input("Press to close game.") def intro(): intro = GraphWin("Introduction", 500, 500) intro.setCoords(0,0,100,100) text1=Text(Point(50,90), "Player A begins by clicking the 'Roll' button.").draw(intro) text2=Text(Point(50,80), "The player has up to three rolls to score as many points").draw(intro) text3=Text(Point(50,70), "as possible. If 'snake-eyes' (two ones) are rolled,").draw(intro) text4=Text(Point(50,60), "the player loses all points from the current round.").draw(intro) text5=Text(Point(50,50), "At any time, a player can choose to protect his or her").draw(intro) text6=Text(Point(50,40), "score by passing the roll to the other player.").draw(intro) text7=Text(Point(50,30), "The game ends when the first player reaches 1,000 points.").draw(intro) text8=Text(Point(50,10), "Click screen to begin game.").draw(intro) intro.getMouse() intro.close() def turn(passButton): pt = win.getMouse() score = 0 rollcount = 0 snakeeyes = 0 while snakeeyes == 0 and rollcount < 3 and not passButton.clicked(pt): tempscore,snakeeyes = scoreroll() score = score+tempscore rollcount = rollcount+1 return score def scoreroll(): score = 0 rlist = roll() if rlist.count(1) == 2: score = 0 snakeeyes = 1 else: for value in rlist: if rlist.count(value)==3: score = score+value*100 elif rlist.count(value)==2: score = score+value*10 else: score = score+value return score, snakeeyes def roll(): pt = win.getMouse() while not quitButton.clicked(pt): if rollButton.clicked(pt): value1 = randrange(1,7) die1.setValue(value1) value2 = randrange(1,7) die2.setValue(value2) value3= randrange(1,7) die3.setValue(value3) value4 = randrange(1,7) die4.setValue(value4) value5 = randrange(1,7) die5.setValue(value5) rlist = [value1,value2,value3,value4,value5] return rlist main() THANKS -- View this message in context: http://www.nabble.com/Python-Dice-Game-tp22982720p22982720.html Sent from the Python - tutor mailing list archive at Nabble.com. From alan.gauld at btinternet.com Sat Apr 11 14:51:41 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 11 Apr 2009 13:51:41 +0100 Subject: [Tutor] Problem with converting Python to EXE using py2exe References: <49E07838.4020909@ieee.org> Message-ID: "Dave Angel" wrote >> To use a relative path base = pic(file=root+"/base.gif") > > > I would get 'root' this way, rather than requiring anything to be set up > by the install (registry, environment vars, config): Good idea, I forgot that in Python you can find the full path that way. Too used to working in C/C++ where the file macro only gives the filename... > (Does anybody think that using __file__ this way is uncool? For > example, should I better use sys.path[0] ? Or something more convoluted > involving sys.modules ?) No I think __file__ is good for this. Alan G. From alan.gauld at btinternet.com Sat Apr 11 14:57:00 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 11 Apr 2009 13:57:00 +0100 Subject: [Tutor] Python Dice Game References: <22982720.post@talk.nabble.com> Message-ID: "jake_k2011" wrote > get it to work :\ I'm a total noob and just a BEGINNER. I can't seem to > get > the buttons to work.Basically they are constructed and maybe activated in > the main program, andI need to be able to use them in the functions > roll() > and turn() i believe. > > CAn anyone help to get this to work?? I'm not familiar with the Graphics module or how it works, but in general when constructing widgets you need to either a) pass in some kind of callback function, or b) use a bind function to connect the button with the function or c) use a naming convention to associate the button and callback. I don't see anything in your code that connects the button to the function you want to be called. So I suspect you need to look at the GUI callback mechanism in more detail? > rollButton = Button(win, Point(150,30), 150, 10, "Roll Dice") > rollButton.activate() > passButton = Button(win, Point(150,10), 150, 10, "Pass Turn") > passButton.activate() > def turn(passButton): > pt = win.getMouse() > score = 0 > rollcount = 0 > snakeeyes = 0 > while snakeeyes == 0 and rollcount < 3 and not passButton.clicked(pt): > tempscore,snakeeyes = scoreroll() > score = score+tempscore > rollcount = rollcount+1 > return score > > > def roll(): > pt = win.getMouse() > while not quitButton.clicked(pt): > if rollButton.clicked(pt): > value1 = randrange(1,7) > die1.setValue(value1) > value2 = randrange(1,7) > die2.setValue(value2) > value3= randrange(1,7) > die3.setValue(value3) > value4 = randrange(1,7) > die4.setValue(value4) > value5 = randrange(1,7) > die5.setValue(value5) > rlist = [value1,value2,value3,value4,value5] > return rlist HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From bhaaluu at gmail.com Sat Apr 11 15:04:34 2009 From: bhaaluu at gmail.com (bhaaluu) Date: Sat, 11 Apr 2009 09:04:34 -0400 Subject: [Tutor] Yet another Python book In-Reply-To: <20090411002522.GA16288@cutter.rexx.com> References: <20090411002522.GA16288@cutter.rexx.com> Message-ID: Thanks for making this book available, Dave! This stuff looks very useful. On Fri, Apr 10, 2009 at 8:25 PM, Dave Kuhlman wrote: > I've collected my training materials plus a few more advanced > topics and I've also written up a reasonably large set of Python > exercises. ?Then, I turned all of the above into a "book". > > If you have comments, suggestions, corrections, etc, I'll > appreciate them. > > You can get it at no cost, in various formats here: > > ? ?http://www.rexx.com/~dkuhlman/#a-python-book > > And, if you really do feel a need to kill a tree, you can purchase a > copy on paper here: > > ? ?http://www.lulu.com/content/paperback-book/a-python-book/2599081 > > - Dave > > > -- > Dave Kuhlman > http://www.rexx.com/~dkuhlman > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- b h a a l u u at g m a i l dot c o m Kid on Bus: What are you gonna do today, Napoleon? Napoleon Dynamite: Whatever I feel like I wanna do. Gosh! From bhaaluu at gmail.com Sat Apr 11 15:58:46 2009 From: bhaaluu at gmail.com (bhaaluu) Date: Sat, 11 Apr 2009 09:58:46 -0400 Subject: [Tutor] Fwd: Yet another Python book In-Reply-To: References: <20090411002522.GA16288@cutter.rexx.com> Message-ID: ---------- Forwarded message ---------- From: Tom Green Date: Sat, Apr 11, 2009 at 9:38 AM Subject: Re: [Tutor] Yet another Python book To: bhaaluu Book looks great.? Any help to master Python is appreciated. Great work. Mike. On Sat, Apr 11, 2009 at 9:04 AM, bhaaluu wrote: > > Thanks for making this book available, Dave! > > This stuff looks very useful. > > On Fri, Apr 10, 2009 at 8:25 PM, Dave Kuhlman wrote: > > I've collected my training materials plus a few more advanced > > topics and I've also written up a reasonably large set of Python > > exercises. ?Then, I turned all of the above into a "book". > > > > If you have comments, suggestions, corrections, etc, I'll > > appreciate them. > > > > You can get it at no cost, in various formats here: > > > > ? ?http://www.rexx.com/~dkuhlman/#a-python-book > > > > And, if you really do feel a need to kill a tree, you can purchase a > > copy on paper here: > > > > ? ?http://www.lulu.com/content/paperback-book/a-python-book/2599081 > > > > - Dave > > > > > > -- > > Dave Kuhlman > > http://www.rexx.com/~dkuhlman > > _______________________________________________ > > Tutor maillist ?- ?Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > > > -- > b h a a l u u at g m a i l dot c o m > Kid on Bus: What are you gonna do today, Napoleon? > Napoleon Dynamite: Whatever I feel like I wanna do. Gosh! > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -- b h a a l u u at g m a i l dot c o m Kid on Bus: What are you gonna do today, Napoleon? Napoleon Dynamite: Whatever I feel like I wanna do. Gosh! From dotancohen at gmail.com Sat Apr 11 17:57:10 2009 From: dotancohen at gmail.com (Dotan Cohen) Date: Sat, 11 Apr 2009 18:57:10 +0300 Subject: [Tutor] Yet another Python book In-Reply-To: <20090411002522.GA16288@cutter.rexx.com> References: <20090411002522.GA16288@cutter.rexx.com> Message-ID: <880dece00904110857sba47b01j4f449e1697209cd1@mail.gmail.com> While the content looks to provide a reasonable learning curve, there is no mention of the Python version used. One of the first examples shows the interpreter for Python 2.6. I really think that a book coming out today should cover Python 3.0, and from the look of things that can be done with very little effort. Other than that, I'd really like to see a chapter on PyQt. PyGTK is nice from what I understand (I've actually never used it) but PyQt with Qt 4 really is the cutting edge, and is very cross-platform compatible. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il From python at bdurham.com Sat Apr 11 18:01:00 2009 From: python at bdurham.com (python at bdurham.com) Date: Sat, 11 Apr 2009 12:01:00 -0400 Subject: [Tutor] Yet another Python book In-Reply-To: References: <20090411002522.GA16288@cutter.rexx.com> Message-ID: <1239465660.22178.1310049665@webmail.messagingengine.com> Dave, Great stuff!!! Thanks for sharing your hard work with the community! Malcolm From dextrous85 at gmail.com Sat Apr 11 18:02:50 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Sat, 11 Apr 2009 21:32:50 +0530 Subject: [Tutor] Yet another Python book In-Reply-To: <1239465660.22178.1310049665@webmail.messagingengine.com> References: <20090411002522.GA16288@cutter.rexx.com> <1239465660.22178.1310049665@webmail.messagingengine.com> Message-ID: <5487b3060904110902j70b25e09o4646fe4beeba0bc0@mail.gmail.com> awesome; I really like the Jython part :) Thanks for your efforts. On Sat, Apr 11, 2009 at 9:31 PM, wrote: > Dave, > > Great stuff!!! Thanks for sharing your hard work with the community! > > Malcolm > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Sat Apr 11 21:07:38 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 11 Apr 2009 15:07:38 -0400 Subject: [Tutor] Problem with converting Python to EXE using py2exe In-Reply-To: References: Message-ID: <49E0EA7A.7020104@ieee.org> Alan Gould wrote: > > Good idea, I forgot that in Python you can find the full path that way. > Too used to working in C/C++ where the file macro only gives the > filename... > In C++, you wouldn't want the full path to the source file, but the full path to the .EXE file. That can be gotten in Windows, by using GetModuleHandle(0), and some other function on that handle. Sorry I don't remember the name of the second function, but I haven't done any C++ work in a few years. It might have a name like GetModuleXXXXX(handle). Anyway, that's the technique I used in that environment. To me, if it's possible to avoid an install entirely, I'm in favor. Two other tricks I used in the C++/Win world; 1) Let the name of the exe, as well as the location, be significant. If you put most of the code in dll's, it doesn't cost much to have multiple copies of the .EXE, each one with a different behavior. That's especially useful when you make file associations or SendTo associations to those various executables. Windows launches the program with only the filename as an argument, but I can have one more parameter hidden in the filename. 2) You can append arbitrary data to the EXE, and it won't affect normal running. Windows only maps in the amount specified in the header. But you can fetch readonly data from the end of the file, and use it any way you like. To append, you can simply do COPY /B myprog.exe + extradata.bin I wish Python.exe was designed this way. We could package up small scripts by appending them to a renamed EXE, The user could treat that as a self-contained executable, ready to run. This wouldn't obviate the need for installing Python itself, but it could make integrating it into normal usage simpler. See EXEMAKER for an implementation of #1, though it doesn't work with some imports, and has the problem that a console gets created even if you're running a GUI python script. It's a good start, though. From zebra05 at gmail.com Sat Apr 11 22:13:59 2009 From: zebra05 at gmail.com (OkaMthembo) Date: Sat, 11 Apr 2009 22:13:59 +0200 Subject: [Tutor] PHP as seen by a Python lover Message-ID: Hi All, I just tried setting up PHP on my machine,with Apache 2.2-something (Win. XP). I came back running. It's a SCHLEP. I love Python. Happy Easter to you all - and to those that do not celebrate it, have a great weekend! -- Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: From iwasroot at gmail.com Sun Apr 12 00:46:18 2009 From: iwasroot at gmail.com (Moos Heintzen) Date: Sat, 11 Apr 2009 15:46:18 -0700 Subject: [Tutor] Optional groups in RE's Message-ID: <7b13ba330904111546n21d90202i7572a75c55b02a23@mail.gmail.com> Hello Tutors! I was trying to make some groups optional in a regular expression, but I couldn't do it. For example, I have the string: >>> data = "42 sdlfks d f60 sdf sdf Title" and the pattern: >>> pattern = "(.*?).*?(.*?).*?(.*?)" This works when all the groups are present. >>> re.search(pattern, data).groups() ('42', '60', 'Title') However, I don't know how to make an re to deal with possibly missing groups. For example, with: >>> data = "42 sdlfks d f60 sdf sdf" I tried >>> pattern = "(.*?).*?(.*?).*?(?:(.*?))?" >>> re.search(pattern, data).groups() ('42', '60', None) but it doesn't work when _is_ present. >>> data = "<price>42</price> sdlfks d f<ship>60</ship> sdf sdf <title>Title" >>> re.search(pattern, data).groups() ('42', '60', None) I tried something like (?:pattern)+ and (?:pattern)* but I couldn't get what I wanted. (.*?)? doesn't seem to be a valid re either. I know (?:pattern) is a non-capturing group. I just read that | has very low precedence, so I used parenthesis liberally to "or" pattern and a null string. >>> pattern = "(.*?).*?(.*?).*?(?:(?:(.*?))|)" >>> re.search(pattern, data).groups() ('42', '60', None) (?:(?:pattern)|(?:.*)) didn't work either. I want to be able to have some groups as optional, so when that group isn't matched, it returns None. When it's match it should return what is matched. Is that possible with one re? I could probably do it with more than one re (and did it) but with one re the solution is much more elegant. (i.e. I could have named groups, then pass the resultant dictionary to a processing function) I also tried matching optional groups before, and curious about the solution. Moos From metolone+gmane at gmail.com Sun Apr 12 02:50:44 2009 From: metolone+gmane at gmail.com (Mark Tolonen) Date: Sat, 11 Apr 2009 17:50:44 -0700 Subject: [Tutor] Optional groups in RE's References: <7b13ba330904111546n21d90202i7572a75c55b02a23@mail.gmail.com> Message-ID: "Moos Heintzen" wrote in message news:7b13ba330904111546n21d90202i7572a75c55b02a23 at mail.gmail.com... > Hello Tutors! > > I was trying to make some groups optional in a regular expression, but > I couldn't do it. > > For example, I have the string: > >>>> data = "42 sdlfks d f60 sdf sdf >>>> Title" [ re attempts stripped] Your data looks like XML. If it is actually well-formed XML, have you tried ElementTree? PythonWin 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32. Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information. >>> from xml.etree import ElementTree as ET >>> data = "42 sdlfks d f60 sdf sdf >>> Title" >>> t=ET.fromstring(data) >>> t.tag 'object' >>> for e in t.getchildren(): ... print e.tag,e.text ... price 42 ship 60 title Title >>> -Mark From kent37 at tds.net Sun Apr 12 02:54:53 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 11 Apr 2009 20:54:53 -0400 Subject: [Tutor] Optional groups in RE's In-Reply-To: <7b13ba330904111546n21d90202i7572a75c55b02a23@mail.gmail.com> References: <7b13ba330904111546n21d90202i7572a75c55b02a23@mail.gmail.com> Message-ID: <1c2a2c590904111754v6ed68f8ej6178f479a66c84f1@mail.gmail.com> On Sat, Apr 11, 2009 at 6:46 PM, Moos Heintzen wrote: > Hello Tutors! > > I was trying to make some groups optional in a regular expression, but > I couldn't do it. > > For example, I have the string: > >>>> data = "42 sdlfks d f60 sdf sdf ?Title" > > and the pattern: >>>> pattern = "(.*?).*?(.*?).*?(.*?)" > > This works when all the groups are present. > >>>> re.search(pattern, data).groups() > ('42', '60', 'Title') > > However, I don't know how to make an re to deal with possibly missing groups. > For example, with: >>>> data = "42 sdlfks d f60 sdf sdf" > > I tried >>>> pattern = "(.*?).*?(.*?).*?(?:(.*?))?" >>>> re.search(pattern, data).groups() > ('42', '60', None) > > but it doesn't work when _is_ present. This re doesn't have to match anything after </ship> so it doesn't. You can force it to match to the end by adding $ at the end but that is not enough, you have to make the "</ship>.*?" *not* match <title>. One way to do that is to use [^<]>? instead of .*?: In [1]: import re In [2]: d1 = "<price>42</price> sdlfks d f<ship>60</ship> sdf sdf <title>Title" In [3]: d2 = "42 sdlfks d f60 sdf sdf" In [7]: pattern = "(.*?).*?(.*?)[^<]*?(?:(.*?))?$" In [8]: re.search(pattern, d1).groups() Out[8]: ('42', '60', 'Title') In [9]: re.search(pattern, d2).groups() Out[9]: ('42', '60', None) Kent From krayzie_jm at yahoo.com Sun Apr 12 02:53:19 2009 From: krayzie_jm at yahoo.com (krayzie_jm at yahoo.com) Date: Sun, 12 Apr 2009 00:53:19 +0000 Subject: [Tutor] Help In-Reply-To: References: Message-ID: <65466529-1239497599-cardhu_decombobulator_blackberry.rim.net-2126214402-@bxe1249.bisx.prod.on.blackberry> Sent from my BlackBerry? device from Digicel -----Original Message----- From: tutor-request at python.org Date: Sun, 12 Apr 2009 02:51:01 To: Subject: Tutor Digest, Vol 62, Issue 42 Send Tutor mailing list submissions to tutor at python.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to tutor-request at python.org You can reach the person managing the list at tutor-owner at python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: 1. Re: Yet another Python book (Dotan Cohen) 2. Re: Yet another Python book (python at bdurham.com) 3. Re: Yet another Python book (vishwajeet singh) 4. Re: Problem with converting Python to EXE using py2exe (Dave Angel) 5. PHP as seen by a Python lover (OkaMthembo) 6. Optional groups in RE's (Moos Heintzen) 7. Re: Optional groups in RE's (Mark Tolonen) ---------------------------------------------------------------------- Message: 1 Date: Sat, 11 Apr 2009 18:57:10 +0300 From: Dotan Cohen Subject: Re: [Tutor] Yet another Python book To: tutor at python.org Message-ID: <880dece00904110857sba47b01j4f449e1697209cd1 at mail.gmail.com> Content-Type: text/plain; charset=UTF-8 While the content looks to provide a reasonable learning curve, there is no mention of the Python version used. One of the first examples shows the interpreter for Python 2.6. I really think that a book coming out today should cover Python 3.0, and from the look of things that can be done with very little effort. Other than that, I'd really like to see a chapter on PyQt. PyGTK is nice from what I understand (I've actually never used it) but PyQt with Qt 4 really is the cutting edge, and is very cross-platform compatible. -- Dotan Cohen http://what-is-what.com http://gibberish.co.il ------------------------------ Message: 2 Date: Sat, 11 Apr 2009 12:01:00 -0400 From: python at bdurham.com Subject: Re: [Tutor] Yet another Python book To: tutor at python.org Message-ID: <1239465660.22178.1310049665 at webmail.messagingengine.com> Content-Type: text/plain; charset="ISO-8859-1" Dave, Great stuff!!! Thanks for sharing your hard work with the community! Malcolm ------------------------------ Message: 3 Date: Sat, 11 Apr 2009 21:32:50 +0530 From: vishwajeet singh Subject: Re: [Tutor] Yet another Python book To: python at bdurham.com Cc: tutor at python.org Message-ID: <5487b3060904110902j70b25e09o4646fe4beeba0bc0 at mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" awesome; I really like the Jython part :) Thanks for your efforts. On Sat, Apr 11, 2009 at 9:31 PM, wrote: > Dave, > > Great stuff!!! Thanks for sharing your hard work with the community! > > Malcolm > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 4 Date: Sat, 11 Apr 2009 15:07:38 -0400 From: Dave Angel Subject: Re: [Tutor] Problem with converting Python to EXE using py2exe To: tutor at python.org Message-ID: <49E0EA7A.7020104 at ieee.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Alan Gould wrote: > > Good idea, I forgot that in Python you can find the full path that way. > Too used to working in C/C++ where the file macro only gives the > filename... > In C++, you wouldn't want the full path to the source file, but the full path to the .EXE file. That can be gotten in Windows, by using GetModuleHandle(0), and some other function on that handle. Sorry I don't remember the name of the second function, but I haven't done any C++ work in a few years. It might have a name like GetModuleXXXXX(handle). Anyway, that's the technique I used in that environment. To me, if it's possible to avoid an install entirely, I'm in favor. Two other tricks I used in the C++/Win world; 1) Let the name of the exe, as well as the location, be significant. If you put most of the code in dll's, it doesn't cost much to have multiple copies of the .EXE, each one with a different behavior. That's especially useful when you make file associations or SendTo associations to those various executables. Windows launches the program with only the filename as an argument, but I can have one more parameter hidden in the filename. 2) You can append arbitrary data to the EXE, and it won't affect normal running. Windows only maps in the amount specified in the header. But you can fetch readonly data from the end of the file, and use it any way you like. To append, you can simply do COPY /B myprog.exe + extradata.bin I wish Python.exe was designed this way. We could package up small scripts by appending them to a renamed EXE, The user could treat that as a self-contained executable, ready to run. This wouldn't obviate the need for installing Python itself, but it could make integrating it into normal usage simpler. See EXEMAKER for an implementation of #1, though it doesn't work with some imports, and has the problem that a console gets created even if you're running a GUI python script. It's a good start, though. ------------------------------ Message: 5 Date: Sat, 11 Apr 2009 22:13:59 +0200 From: OkaMthembo Subject: [Tutor] PHP as seen by a Python lover To: Python Message-ID: Content-Type: text/plain; charset="utf-8" Hi All, I just tried setting up PHP on my machine,with Apache 2.2-something (Win. XP). I came back running. It's a SCHLEP. I love Python. Happy Easter to you all - and to those that do not celebrate it, have a great weekend! -- Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 6 Date: Sat, 11 Apr 2009 15:46:18 -0700 From: Moos Heintzen Subject: [Tutor] Optional groups in RE's To: "tutor at python.org" Message-ID: <7b13ba330904111546n21d90202i7572a75c55b02a23 at mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Hello Tutors! I was trying to make some groups optional in a regular expression, but I couldn't do it. For example, I have the string: >>> data = "42 sdlfks d f60 sdf sdf Title" and the pattern: >>> pattern = "(.*?).*?(.*?).*?(.*?)" This works when all the groups are present. >>> re.search(pattern, data).groups() ('42', '60', 'Title') However, I don't know how to make an re to deal with possibly missing groups. For example, with: >>> data = "42 sdlfks d f60 sdf sdf" I tried >>> pattern = "(.*?).*?(.*?).*?(?:(.*?))?" >>> re.search(pattern, data).groups() ('42', '60', None) but it doesn't work when _is_ present. >>> data = "<price>42</price> sdlfks d f<ship>60</ship> sdf sdf <title>Title" >>> re.search(pattern, data).groups() ('42', '60', None) I tried something like (?:pattern)+ and (?:pattern)* but I couldn't get what I wanted. (.*?)? doesn't seem to be a valid re either. I know (?:pattern) is a non-capturing group. I just read that | has very low precedence, so I used parenthesis liberally to "or" pattern and a null string. >>> pattern = "(.*?).*?(.*?).*?(?:(?:(.*?))|)" >>> re.search(pattern, data).groups() ('42', '60', None) (?:(?:pattern)|(?:.*)) didn't work either. I want to be able to have some groups as optional, so when that group isn't matched, it returns None. When it's match it should return what is matched. Is that possible with one re? I could probably do it with more than one re (and did it) but with one re the solution is much more elegant. (i.e. I could have named groups, then pass the resultant dictionary to a processing function) I also tried matching optional groups before, and curious about the solution. Moos ------------------------------ Message: 7 Date: Sat, 11 Apr 2009 17:50:44 -0700 From: "Mark Tolonen" Subject: Re: [Tutor] Optional groups in RE's To: tutor at python.org Message-ID: "Moos Heintzen" wrote in message news:7b13ba330904111546n21d90202i7572a75c55b02a23 at mail.gmail.com... > Hello Tutors! > > I was trying to make some groups optional in a regular expression, but > I couldn't do it. > > For example, I have the string: > >>>> data = "42 sdlfks d f60 sdf sdf >>>> Title" [ re attempts stripped] Your data looks like XML. If it is actually well-formed XML, have you tried ElementTree? PythonWin 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32. Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information. >>> from xml.etree import ElementTree as ET >>> data = "42 sdlfks d f60 sdf sdf >>> Title" >>> t=ET.fromstring(data) >>> t.tag 'object' >>> for e in t.getchildren(): ... print e.tag,e.text ... price 42 ship 60 title Title >>> -Mark ------------------------------ _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor End of Tutor Digest, Vol 62, Issue 42 ************************************* From davea at ieee.org Sun Apr 12 03:09:12 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 11 Apr 2009 21:09:12 -0400 Subject: [Tutor] Problem with converting Python to EXE using py2exe In-Reply-To: References: Message-ID: <49E13F38.5010309@ieee.org> Dave Angel wrote: > > In C++, you wouldn't want the full path to the source file, but the full > path to the .EXE file. That can be gotten in Windows, by using > GetModuleHandle(0), and some other function on that handle. Sorry I > don't remember the name of the second function, but I haven't done any > C++ work in a few years. It might have a name like > GetModuleXXXXX(handle). Anyway, that's the technique I used in that > environment. To me, if it's possible to avoid an install entirely, I'm > in favor. > > *GetModuleFileName*() is the function I was trying to remember. From david at abbottdavid.com Sun Apr 12 03:15:55 2009 From: david at abbottdavid.com (David) Date: Sat, 11 Apr 2009 21:15:55 -0400 Subject: [Tutor] Help In-Reply-To: <65466529-1239497599-cardhu_decombobulator_blackberry.rim.net-2126214402-@bxe1249.bisx.prod.on.blackberry> References: <65466529-1239497599-cardhu_decombobulator_blackberry.rim.net-2126214402-@bxe1249.bisx.prod.on.blackberry> Message-ID: <49E140CB.6000805@abbottdavid.com> krayzie_jm at yahoo.com wrote: > Sent from my BlackBerry? device from Digicel Don't have a BlackBerry, sorry can not help. -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From iwasroot at gmail.com Sun Apr 12 04:06:33 2009 From: iwasroot at gmail.com (Moos Heintzen) Date: Sat, 11 Apr 2009 19:06:33 -0700 Subject: [Tutor] Optional groups in RE's In-Reply-To: References: <7b13ba330904111546n21d90202i7572a75c55b02a23@mail.gmail.com> Message-ID: <7b13ba330904111906w48b2dcd0tc91776299617b602@mail.gmail.com> Mark Tolonen wrote: > Your data looks like XML. ?If it is actually well-formed XML, have you tried > ElementTree? It is XML. I used minidom from xml.dom, and it worked fine, except it was ~16 times slower. I'm parsing a ~70mb file, and the difference is 3 minutes to 10 seconds with re's. I used separate re's for each field I wanted, and it worked nicely. (1-1 between DOM calls and re.search and re.finditer) This problem raised when I tried to do the match in one re. I guess instead of minidom I could try lxml, which uses libxml2, which is written in C. Kent Johnson wrote: > This re doesn't have to match anything after so it doesn't. > You can force it to match to the end by adding $ at the end but that > is not enough, you have to make the ".*?" *not* match . > One way to do that is to use [^<]*? instead of .*?: Ah. Thanks. Unfortunately, the input string is multi-line, and doesn't end in Moos P.S. I'm still relatively new to RE's, or IRE's. sed, awk, grep, and perl have different format for re's. grep alone has four different versions of RE's! Since the only form of re I'm using is "start(.*?)end" I was thinking about writing a C program to do that. From kent37 at tds.net Sun Apr 12 05:07:02 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 11 Apr 2009 23:07:02 -0400 Subject: [Tutor] Optional groups in RE's In-Reply-To: <7b13ba330904111906w48b2dcd0tc91776299617b602@mail.gmail.com> References: <7b13ba330904111546n21d90202i7572a75c55b02a23@mail.gmail.com> <7b13ba330904111906w48b2dcd0tc91776299617b602@mail.gmail.com> Message-ID: <1c2a2c590904112007n556a657ai366efe5ef27021e1@mail.gmail.com> On Sat, Apr 11, 2009 at 10:06 PM, Moos Heintzen wrote: > Mark Tolonen wrote: >> Your data looks like XML. ?If it is actually well-formed XML, have you tried >> ElementTree? > > It is XML. I used minidom from xml.dom, and it worked fine, except it > was ~16 times slower. I'm parsing a ~70mb file, and the difference is > 3 minutes to 10 seconds with re's. > > I used separate re's for each field I wanted, and it worked nicely. > (1-1 between DOM calls and re.search and re.finditer) > > This problem raised when I tried to do the match in one re. > > I guess instead of minidom I could try lxml, which uses libxml2, which > is written in C. ElementTree is likely faster than minidom, it ha a C implementation. > Kent Johnson wrote: >> This re doesn't have to match anything after so it doesn't. >> You can force it to match to the end by adding $ at the end but that >> is not enough, you have to make the ".*?" *not* match . >> One way to do that is to use [^<]*? instead of .*?: > > Ah. Thanks. > Unfortunately, the input string is multi-line, and doesn't end in Perhaps you should show your actual input then. Kent From mydomdom at gmail.com Sun Apr 12 09:13:32 2009 From: mydomdom at gmail.com (Dominique) Date: Sun, 12 Apr 2009 07:13:32 +0000 (UTC) Subject: [Tutor] How to programmatically EDIT a python file using an editor of my choice ? Message-ID: Hi, I'd like to edit and not run a .py file programmatically. os.path(filename.py) runs the file using python. What I'd like is to only edit the file using Idle or Notepad++ or Spe... I could do this by making a new file with a .txt extension. But in that case, it is the program associated with the .txt extension that would open the file. What I'd like is to open the file using an editor of my choice. Is there an easy way to do this ? Thanks in advance Dominique From alan.gauld at btinternet.com Sun Apr 12 09:45:42 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Apr 2009 08:45:42 +0100 Subject: [Tutor] How to programmatically EDIT a python file using an editorof my choice ? References: Message-ID: "Dominique" wrote > What I'd like is to only edit the file using Idle or Notepad++ or Spe... > What I'd like is to open the file using an editor of my choice. How would you do this non programatically and not from the GUI? In other words can you do it from an OS command line? If so put that command in a call to subprocess.Popen. If not is thre an API to the editor, for example Windows editors may have a COM interface that you could access. But without being specific about the editor and the operating system it is hard to be specific about a solution. Generically, if on Unix, you should probably respect the EDITOR or VISUAL environment variables since they reflect the users editor preferences so the code should look like: ed = os.getenv('EDITOR') vi = os.getenv('VISUAL') editor = vi or ed if editor: subprocess.Popen().call([editor, filename]) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mydomdom at gmail.com Sun Apr 12 15:53:12 2009 From: mydomdom at gmail.com (Dominique) Date: Sun, 12 Apr 2009 13:53:12 +0000 (UTC) Subject: [Tutor] =?utf-8?q?How_to_programmatically_EDIT_a_python_file_usin?= =?utf-8?q?g_an=09editorof_my_choice_=3F?= References: Message-ID: Alan Gauld btinternet.com> writes: > > How would you do this non programatically and not from the GUI? > In other words can you do it from an OS command line? Hello Alan, Thank you for your response. I want to do it from a GUI (with wxPython). I work with Windows XP SP3, python 2.5 and wxPython 2.8.9.2. subprocess.Popen(args = ["notepad", filename]) works fine. Same for subprocess.Popen(["C:\Program Files\Editra\Editra.exe", filename]). It opens the file with notepad or Editra without problems. subprocess.Popen("notepad") or subprocess.Popen("C:\Program Files\Editra\Editra.exe") work also fine. Conversely, subprocess.Popen("notepad++") doesn't work. If I do subprocess.Popen(args = ["notepad++", filename]), I get an exception. The exception is as follows: Traceback (most recent call last): File "", line 1, in s.Popen(args = ["notepad++.exe", "c:\\Redirect.txt"]) File "C:\Python25\lib\subprocess.py", line 594, in __init__ errread, errwrite) File "C:\Python25\lib\subprocess.py", line 816, in _execute_child startupinfo) WindowsError: [Error 2] Le fichier sp?cifi? est introuvable (the specified file cannot be found) I tried several other ways without any success: subprocess.Popen(args = ["notepad++.exe", filename]) subprocess.Popen(args = ["C:\Program Files\Notepad++\notepad++.exe", filename]) I wonder if it is due to the location of the softwares. Notepad.exe is located in C:\WINDOWS while notepad++ is located in C:\Program Files\Notepad++ >If not is thre an API to the editor, for example Windows editors >may have a COM interface that you could access. Do you mean that it is not always possible to open another software from python, notably if the software doesn't have a COM interface ? > > ed = os.getenv('EDITOR') > vi = os.getenv('VISUAL') os.getenv is always None, which may be correct since I am on Windows ? Thanks a lot for your help. Dominique From sander.sweers at gmail.com Sun Apr 12 16:20:19 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 12 Apr 2009 16:20:19 +0200 Subject: [Tutor] How to programmatically EDIT a python file using an editorof my choice ? In-Reply-To: References: Message-ID: 2009/4/12 Dominique : > I tried several other ways without any success: > subprocess.Popen(args = ["notepad++.exe", filename]) > subprocess.Popen(args = ["C:\Program Files\Notepad++\notepad++.exe", filename]) Try this: subprocess.Popen(args = [r'C:\Program Files\Notepad++\notepad++.exe', filename]) Greets Sander From sander.sweers at gmail.com Sun Apr 12 16:21:55 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 12 Apr 2009 16:21:55 +0200 Subject: [Tutor] Make beautifulsoup show the data it has an issue with In-Reply-To: <1c2a2c590904101419y72bd0676t57e368017b49f101@mail.gmail.com> References: <1c2a2c590904101419y72bd0676t57e368017b49f101@mail.gmail.com> Message-ID: 2009/4/10 Kent Johnson : > Or, catch > the exception, have the code find out where the error is and display > the bad line. This is what I was looking for. I know how to catch the exception but how do I make it display the bad line? Thanks Sander From kent37 at tds.net Sun Apr 12 16:47:09 2009 From: kent37 at tds.net (Kent Johnson) Date: Sun, 12 Apr 2009 10:47:09 -0400 Subject: [Tutor] Make beautifulsoup show the data it has an issue with In-Reply-To: References: <1c2a2c590904101419y72bd0676t57e368017b49f101@mail.gmail.com> Message-ID: <1c2a2c590904120747k3db6f686g5a26c602f12ea8a0@mail.gmail.com> On Sun, Apr 12, 2009 at 10:21 AM, Sander Sweers wrote: > 2009/4/10 Kent Johnson : >> Or, catch >> the exception, have the code find out where the error is and display >> the bad line. > > This is what I was looking for. I know how to catch the exception but > how do I make it display the bad line? You had: tsoup = BeautifulSoup(readPage('http://url.sanitized')) and you got HTMLParseError: malformed start tag, at line 167, column 73 so try something like this (assuming Python 2.x): from HTMLParser import HTMLParseError data = readPage('http://url.sanitized') try: tsoup = BeautifulSoup(data) except HTMLParseError, ex: lines = data.splitlines() bad_line = lines[ex.lineno] print ex print repr(bad_line) # use repr() so non-printing chars will show Kent From alan.gauld at btinternet.com Sun Apr 12 17:02:29 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Apr 2009 16:02:29 +0100 Subject: [Tutor] How to programmatically EDIT a python file using an editorof my choice ? References: Message-ID: "Dominique" wrote > I tried several other ways without any success: > subprocess.Popen(args = ["notepad++.exe", filename]) > subprocess.Popen(args = ["C:\Program Files\Notepad++\notepad++.exe", > filename]) > > I wonder if it is due to the location of the softwares. In a way. I think it's because you are using single backslashes. You need to make the command a raw string by putting an r in front.: subprocess.Popen([r"C:\Program Files\Notepad++\notepad++.exe", filename]) Otherwise Python gets confused by \n and thinks its a newline character >>If not is thre an API to the editor, for example Windows editors >>may have a COM interface that you could access. > > Do you mean that it is not always possible to open another software from > python, > notably if the software doesn't have a COM interface ? You can always open the program but some programs may not take command line arguments to open a file. Thuis you need to drive them via COM to effectively use the File->Open menu option from Python. Fortunately most editors will accept a command line filename so its only rarely an issue for editors. But things like statistics [packages, graphics editors etc can be more difficult. >> ed = os.getenv('EDITOR') >> vi = os.getenv('VISUAL') > > os.getenv is always None, Presumably you don;t have either EDITOR or VISUAL defined. What if you try os.getenv('PATH') That should return something. > which may be correct since I am on Windows ? No, getenv() works on any OS, although Unix tends to use environment variables much more than Windows, which favours the registry. Alan G. From mydomdom at gmail.com Sun Apr 12 19:46:29 2009 From: mydomdom at gmail.com (Dominique) Date: Sun, 12 Apr 2009 17:46:29 +0000 (UTC) Subject: [Tutor] =?utf-8?q?How_to_programmatically_EDIT_a_python_file_usin?= =?utf-8?q?g=09an=09editorof_my_choice_=3F?= References: Message-ID: OK. With r before the path of the software, it works perfectly. subprocess.Popen([r"C:\Program Files\Editra\Editra.exe", filename]) I saw that once but I don't remember and cannot find the reason for the use of r. I'll try not to forget to test with and without r in case a file access doesn't work. Thanks a lot to both of you Sander and Alan for your help. Dominique From sander.sweers at gmail.com Sun Apr 12 20:06:51 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 12 Apr 2009 20:06:51 +0200 Subject: [Tutor] How to programmatically EDIT a python file using an editorof my choice ? In-Reply-To: References: Message-ID: 2009/4/12 Dominique : > With r before the path of the software, it works perfectly. > subprocess.Popen([r"C:\Program Files\Editra\Editra.exe", filename]) > I saw that once but I don't remember and cannot find the reason for the use of r. The r means the string is a raw string. > I'll try not to forget to test with and without r in case a file access doesn't > work. The real issue is what Alan pointed out. The backlash is an escape character and if you want to use it in a string should be escaped by a backlash.Using a raw string will give the same result as 'C:\\Program Files\\Editra\\Editra.exe' Greets Sander From sander.sweers at gmail.com Sun Apr 12 20:15:57 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 12 Apr 2009 20:15:57 +0200 Subject: [Tutor] Make beautifulsoup show the data it has an issue with In-Reply-To: <1c2a2c590904120747k3db6f686g5a26c602f12ea8a0@mail.gmail.com> References: <1c2a2c590904101419y72bd0676t57e368017b49f101@mail.gmail.com> <1c2a2c590904120747k3db6f686g5a26c602f12ea8a0@mail.gmail.com> Message-ID: 2009/4/12 Kent Johnson : > try: > ?tsoup = BeautifulSoup(data) > except HTMLParseError, ex: Aah, ex is the object/class which has the error data. This was the missing piece for me. Many thanks Kent! Greets Sander From sierra_mtnview at sbcglobal.net Sun Apr 12 21:41:50 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Sun, 12 Apr 2009 12:41:50 -0700 Subject: [Tutor] Reading a "Raw "Image File Message-ID: <49E243FE.3010506@sbcglobal.net> I can read a raw of 640x480 bytes with: raw_file=open('sent_internal.raw') raw_image=raw_file.read() It likely contains a lot of 0 values, black=0. The result above is a string of 242 characters. I'm guessing the 0's were ignored, and only >0 values were kept. If so, how do I get all 307200 bytes into a read? Whoops ... Ah, I missed 'rb'; however, I want to continue. I found the minimum value among the first 242 items to be 31 without the 'rb'. Among the 307200, I found it to be 150, which I believe is correct. That is, there are pixels in the image that are pretty bright. So what weirdness took place w/o the 'rb'? Why no 150? One more question, when I tried (no 'wb') read(size=307200), why di read balk at size, syntactically. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose is 5.52*10**-6 meters. That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From alan.gauld at btinternet.com Sun Apr 12 22:42:31 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Apr 2009 21:42:31 +0100 Subject: [Tutor] Reading a "Raw "Image File References: <49E243FE.3010506@sbcglobal.net> Message-ID: "Wayne Watson" wrote > raw_image=raw_file.read() > One more question, when I tried (no 'wb') read(size=307200), > why di read balk at size, syntactically. It doesn't balk at the size syntactically it finds what it thinks is an End Of file character code in the data and stops reading. That's how text files are terminated and why you need to use rb to read non text data. Having read it as binary its then up to you to interpret the binary byte stream in whatever way you need, for example decoding it with the struct module or treating it as an image for PIL to manipulate or whatever. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From emile at fenx.com Sun Apr 12 22:46:12 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 12 Apr 2009 13:46:12 -0700 Subject: [Tutor] Reading a "Raw "Image File In-Reply-To: <49E243FE.3010506@sbcglobal.net> References: <49E243FE.3010506@sbcglobal.net> Message-ID: <49E25314.9030107@fenx.com> Wayne Watson wrote: > I can read a raw of 640x480 bytes with: > > raw_file=open('sent_internal.raw') raw_file=open('sent_internal.raw','rb') > raw_image=raw_file.read() > > It likely contains a lot of 0 values, black=0. The result above is a > string of 242 characters. I'm guessing the 0's were ignored, and only >0 > values were kept. If so, how do I get all 307200 bytes into a read? > Whoops ... Ah, I missed 'rb'; however, I want to continue. ah > > I found the minimum value among the first 242 items to be 31 without the > 'rb'. the first 32 characters in ascii are control characters > Among the 307200, I found it to be 150, which I believe is > correct. That is, there are pixels in the image that are pretty bright. > So what weirdness took place w/o the 'rb'? Why no 150? not sure what this means... > > One more question, when I tried (no 'wb') assuming you mean 'rb' above > read(size=307200), > why di read > balk at size, syntactically. > read terminates upon first end on line character encountered HTH, Emile From emile at fenx.com Sun Apr 12 22:46:12 2009 From: emile at fenx.com (Emile van Sebille) Date: Sun, 12 Apr 2009 13:46:12 -0700 Subject: [Tutor] Reading a "Raw "Image File In-Reply-To: <49E243FE.3010506@sbcglobal.net> References: <49E243FE.3010506@sbcglobal.net> Message-ID: <49E25314.9030107@fenx.com> Wayne Watson wrote: > I can read a raw of 640x480 bytes with: > > raw_file=open('sent_internal.raw') raw_file=open('sent_internal.raw','rb') > raw_image=raw_file.read() > > It likely contains a lot of 0 values, black=0. The result above is a > string of 242 characters. I'm guessing the 0's were ignored, and only >0 > values were kept. If so, how do I get all 307200 bytes into a read? > Whoops ... Ah, I missed 'rb'; however, I want to continue. ah > > I found the minimum value among the first 242 items to be 31 without the > 'rb'. the first 32 characters in ascii are control characters > Among the 307200, I found it to be 150, which I believe is > correct. That is, there are pixels in the image that are pretty bright. > So what weirdness took place w/o the 'rb'? Why no 150? not sure what this means... > > One more question, when I tried (no 'wb') assuming you mean 'rb' above > read(size=307200), > why di read > balk at size, syntactically. > read terminates upon first end on line character encountered HTH, Emile From sierra_mtnview at sbcglobal.net Sun Apr 12 23:19:52 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Sun, 12 Apr 2009 14:19:52 -0700 Subject: [Tutor] Reading a "Raw "Image File In-Reply-To: <49E25314.9030107@fenx.com> References: <49E243FE.3010506@sbcglobal.net> <49E25314.9030107@fenx.com> Message-ID: <49E25AF8.3000001@sbcglobal.net> I think I meant why didn't I find the value 150 (a high) in the the 242 if I had correctly guessed only 242 non-zero pixels were the cause of the shrinkage. Instead I found 31 as the high. It makes sense though if it shortened to the first CR found. The CR was probably at 243. > >> Among the 307200, I found it to be 150, which I believe is >> correct. That is, there are pixels in the image that are pretty bright. >> So what weirdness took place w/o the 'rb'? Why no 150? > > not sure what this means... -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose is 5.52*10**-6 meters. That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From sierra_mtnview at sbcglobal.net Mon Apr 13 00:26:33 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Sun, 12 Apr 2009 15:26:33 -0700 Subject: [Tutor] Using Easygui in Python v3 In-Reply-To: References: Message-ID: <49E26A99.4060901@sbcglobal.net> Possibly contact Steve Ferg? Alan Gauld wrote: > I'm currently updating my tutorial to cover Python V3. > I thought it miight be good to include some stuff on EasyGUI for the > user input topic and on checking the web site it said Easygui was > compatible with Python v3. However after downloading and installing I > get a "No module named StringIO" error under Python v3. > > Before I start digging deeper I thought I'd ask if anyone else had the > same problem and knows of a fix? > > -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose is 5.52*10**-6 meters. That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From sierra_mtnview at sbcglobal.net Mon Apr 13 01:05:00 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Sun, 12 Apr 2009 16:05:00 -0700 Subject: [Tutor] Tkinter and Antialiasing Message-ID: <49E2739C.8020202@sbcglobal.net> If I draw a fairly slanted line across an image using Tkinter, it looks a bit jagged. Is this typical of Tkinter graphics, or is there an option or mechanism that will antialias? Win XP, Python 2.5, Tkinter 8.4. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose is 5.52*10**-6 meters. That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From alan.gauld at btinternet.com Mon Apr 13 01:16:51 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Apr 2009 00:16:51 +0100 Subject: [Tutor] Using Easygui in Python v3 References: <49E26A99.4060901@sbcglobal.net> Message-ID: "Wayne Watson" wrote > Possibly contact Steve Ferg? Yes I did that and Steve has included my fix in the latest version. He also kindly gave a plug to my tutorial :-) I've now included a section on EasyGui in my "Talking to the User" topic in the Python v3 version. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/l2p/ From alan.gauld at btinternet.com Mon Apr 13 01:41:16 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Apr 2009 00:41:16 +0100 Subject: [Tutor] Tkinter and Antialiasing References: <49E2739C.8020202@sbcglobal.net> Message-ID: "Wayne Watson" wrote > If I draw a fairly slanted line across an image using Tkinter, it looks a > bit jagged. Is this typical of Tkinter graphics, or is there an option or > mechanism that will antialias? I think that is a feature of the canvas and its underlying code. I can't see any anti-aliasing features although you can of course render it as a smooth line, and mess around with the splines settings, but I don't think that will help in this case if it's a single straight line. This might be an area where OpenGL or another toolkit might help. If its part of a graphic then generating the graphic as an image and loading the image into the canvas might work better. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From sierra_mtnview at sbcglobal.net Mon Apr 13 02:19:09 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Sun, 12 Apr 2009 17:19:09 -0700 Subject: [Tutor] Tkinter and Antialiasing In-Reply-To: References: <49E2739C.8020202@sbcglobal.net> Message-ID: <49E284FD.5090603@sbcglobal.net> I can probably get around it in this case. What I was attempting to do is draw radial lines out from a center. Each maybe 15 degrees apart. Basically compass style. With the jaggies it looks a bit odd; however, I can probably do this with a graphics tool, and then superimpose it on the image I planned to use it for. Extra work, but it should do the trick. Then again I can probably draw the radial lines from near the perimeter to have it look more like a compass, and that'll probably look satisfactory. Each line being maybe 10-20 pixels. I tried PILs similar draw methods with the same results. Tkinter probably uses them anyway. Maybe there's hope when the new PIL is released. Alan Gauld wrote: > > "Wayne Watson" wrote > >> If I draw a fairly slanted line across an image using Tkinter, it >> looks a bit jagged. Is this typical of Tkinter graphics, or is there >> an option or mechanism that will antialias? > > I think that is a feature of the canvas and its underlying code. > I can't see any anti-aliasing features although you can of course > render it as a smooth line, and mess around with the splines settings, > but I don't think that will help in this case if it's a single > straight line. > > This might be an area where OpenGL or another toolkit might help. > If its part of a graphic then generating the graphic as an image and > loading the image into the canvas might work better. > > HTH, > > -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose is 5.52*10**-6 meters. That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From sierra_mtnview at sbcglobal.net Mon Apr 13 02:54:09 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Sun, 12 Apr 2009 17:54:09 -0700 Subject: [Tutor] Using numpy Array Capabilities Message-ID: <49E28D31.2090701@sbcglobal.net> I'm pretty sure numpy can manipulate arrays, but I'm barely familiar with it. Basically, what I would like to do is to take a list like: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] and convert it to; [['0x0', '0x1', '0x2', '0x3', '0x4'], ['0x5', '0x6', '0x7', '0x8', '0x9']] That is, it becomes a matrix of 2 rows and 5 columns. I generated the above from a loop in Python, and generated hex equivalents in the last list. Can numpy do this simply without resorting to loops? How would one code it? I think numpy produces an array like object, but it's not necessarily the same as Python. However, the numpy arrays are going to be input into an object that expects numpy arrays. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose is 5.52*10**-6 meters. That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From jrodriguo at hotmail.com Mon Apr 13 04:21:21 2009 From: jrodriguo at hotmail.com (j mercedes) Date: Mon, 13 Apr 2009 02:21:21 +0000 Subject: [Tutor] FW: another newbie Message-ID: hi, its me again jay, thanks alot everyone for your input on the last program i created that solved for the mode,mean, and average of a set of numbers it actually worked great...i used the import in a bit different method but still very valuable...if anyone can give me any ideas on this program i am doing now it would definetly be appreciated here it goes:import shelveimport stringUNKNOWN = 0HOME = 1WORK = 2FAX = 3CELL = 4class phoneentry: def __init__(self, name = 'Unknown', number = 'Unknown',type = UNKNOWN): self.name = name self.number = number self.type = type# create string representationdef __repr__(self): return('%s:%d' % ( self.name, self.type ))# fuzzy compare or two itemsdef __cmp__(self, that): this = string.lower(str(self)) that = string.lower(that) if string.find(this, that) >= 0: return(0) return(cmp(this, that))def showtype(self): if self.type == UNKNOWN: return('Unknown') if self.type == HOME: return('Home') if self.type == WORK: return('Work') if self.type == FAX: return('Fax') if self.type == CELL: return('Cellular')class phonedb: def __init__(self, dbname = 'phonedata'): self.dbname = dbname; self.shelve = shelve.open(self.dbname);def __del__(self): self.shelve.close() self.shelve = Nonedef add(self, name, number, type = HOME): e = phoneentry(name, number, type) self.shelve[str(e)] = edef lookup(self, string): list = [] for key in self.shelve.keys(): e = self.shelve[key] if cmp(e, string) == 0: list.append(e) return(list)# if not being loaded as a module, run a small testif __name__ == '__main__': foo = phonedb() foo._add_('Sean Reifschneider', '970-555-1111', HOME) foo.add('Sean Reifschneider', '970-555-2222', CELL) foo.add('Evelyn Mitchell', '970-555-1111', HOME)print 'First lookup:'for entry in foo.lookup('reifsch'): print '%-40s %s (%s)' % ( entry.name, entry.number, entry.showtype())print 'Second lookup:'for entry in foo.lookup('e'): print '%-40s %s (%s)' % ( entry.name, entry.number, entry.showtype() ) -------------- next part -------------- An HTML attachment was scrubbed... URL: From daniel.chaowang at gmail.com Mon Apr 13 06:00:35 2009 From: daniel.chaowang at gmail.com (Daniel) Date: Mon, 13 Apr 2009 12:00:35 +0800 Subject: [Tutor] get bytes back from string representation? Message-ID: <57fe16f60904122100p5c8d2d03t2534d7ddc624e241@mail.gmail.com> hi list, I am using SUDS to talk with a web service written by C#. The service will crawl a web page, then return its content as bytes. it's something like: >>> from suds.client import Client >>> url = "http://WSServer/Service1.asmx?wsdl" >>> client = Client(url) >>> page = client.service.GetURLContent("http://www.google.cn") >>> print page (CrawlResult){ crawStatus = "SUCC" rawByte = "PGh0bWw+PGhlYWQ+PG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBjb2 ... " the problem is I need to convert the rawByte from string to bytes, then explain it as text with encoding (like "UTF-8"). Any simple way to convert this string back to bytes? I am not clear with that. Thanks, Daniel. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mydomdom at gmail.com Mon Apr 13 08:26:27 2009 From: mydomdom at gmail.com (Dominique) Date: Mon, 13 Apr 2009 06:26:27 +0000 (UTC) Subject: [Tutor] =?utf-8?q?How_to_programmatically_EDIT_a_python_file_usin?= =?utf-8?q?g_an=09editorof_my_choice_=3F?= References: Message-ID: Sander Sweers gmail.com> writes: Hello Sander, > > The r means the string is a raw string. > > The real issue is what Alan pointed out. The backlash is an escape > character and if you want to use it in a string should be escaped by a > backlash.Using a raw string will give the same result as 'C:\\Program > Files\\Editra\\Editra.exe' > > Greets > Sander OK. I understand. What surprises me is that before calling the subprocess.Popen() method, I normalized the path using: filename = os.path.normpath(filename). Consequently, I thought the path was corrected from the backslash problems. Anyway, Thank you very much for your help. Dominique From sander.sweers at gmail.com Mon Apr 13 09:18:30 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 13 Apr 2009 09:18:30 +0200 Subject: [Tutor] How to programmatically EDIT a python file using an editorof my choice ? In-Reply-To: References: Message-ID: 2009/4/13 Dominique : > What surprises me is that before calling the subprocess.Popen() method, I > normalized the path using: > filename = os.path.normpath(filename). It does.But do you also call os.path.normpath for the program path? See below an example. >>> import os >>> path = 'C:\Program Files\Notepad++\notepad++.exe' >>> os.path.normpath(path) 'C:\\Program Files\\Notepad++\notepad++.exe' Greets Sander From alan.gauld at btinternet.com Mon Apr 13 09:32:21 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Apr 2009 08:32:21 +0100 Subject: [Tutor] get bytes back from string representation? References: <57fe16f60904122100p5c8d2d03t2534d7ddc624e241@mail.gmail.com> Message-ID: "Daniel" wrote Caveat, I know nothing about SUDS. > rawByte = > "PGh0bWw+PGhlYWQ+PG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBjb2 > the problem is I need to convert the rawByte from string to bytes, then > explain it as text with encoding (like "UTF-8"). A string is a collection of bytes. What you see when you print a string is just the interpretation of those bytes as characters. So the real question is how do you want to interpret the bytes? What do the bytes represent? For example are they in a different character encoding, perhaps unicode? In that case simple changing the string encoding (and locale serttings?) should enable you to print them as a string. Or do they represent binary data such as numbers or objects that need to be extracted and then turned into string representation. In that case you may need to use the struct module. It all depends on what the bytes actually represent. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Apr 13 09:37:59 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Apr 2009 08:37:59 +0100 Subject: [Tutor] Using numpy Array Capabilities References: <49E28D31.2090701@sbcglobal.net> Message-ID: "Wayne Watson" wrote > Basically, what I would like to do is to take a list like: > > [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] > > and convert it to; > [['0x0', '0x1', '0x2', '0x3', '0x4'], ['0x5', '0x6', '0x7', '0x8', > '0x9']] What is the basis of this conversion? Why did you split it into two lists? Why convert into hex strings? If the original list had been: [3, 5, 78, 21, -3, 7, 5] What would your output look like? > That is, it becomes a matrix of 2 rows and 5 columns. I generated the > above from a loop in Python, and generated hex equivalents in the last > list. It might help to see those loops because it might explain the reasoning behind the apparently arbitrary decisions. > Can numpy do this simply without resorting to loops? Can't help there, I've never used numpy... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From daniel.chaowang at gmail.com Mon Apr 13 11:03:56 2009 From: daniel.chaowang at gmail.com (Daniel) Date: Mon, 13 Apr 2009 17:03:56 +0800 Subject: [Tutor] get bytes back from string representation? In-Reply-To: References: <57fe16f60904122100p5c8d2d03t2534d7ddc624e241@mail.gmail.com> Message-ID: <57fe16f60904130203o34617d8bs3b746934286e62e6@mail.gmail.com> (also cc tutor) Thank you, Alan. I found the solution. the type of rawByte in SOAP is so I need to convert it back to ascii with binascii module, like: >>> btxt = binascii.a2b_base64(page.rawByte) then btxt can be treated as a normal string. thanks, Daniel On Mon, Apr 13, 2009 at 3:32 PM, Alan Gauld wrote: > > "Daniel" wrote > > Caveat, I know nothing about SUDS. > > rawByte = "PGh0bWw+PGhlYWQ+PG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBjb2 >> > > the problem is I need to convert the rawByte from string to bytes, then >> explain it as text with encoding (like "UTF-8"). >> > > A string is a collection of bytes. What you see when you print a string is > just the interpretation of those bytes as characters. So the real question > is how do you want to interpret the bytes? What do the bytes represent? > > For example are they in a different character encoding, perhaps unicode? > In that case simple changing the string encoding (and locale serttings?) > should enable you to print them as a string. > > Or do they represent binary data such as numbers or objects that need > to be extracted and then turned into string representation. In that case > you may need to use the struct module. > > It all depends on what the bytes actually represent. > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mydomdom at gmail.com Mon Apr 13 11:07:19 2009 From: mydomdom at gmail.com (Dominique) Date: Mon, 13 Apr 2009 09:07:19 +0000 (UTC) Subject: [Tutor] =?utf-8?q?How_to_programmatically_EDIT_a_python_file_usin?= =?utf-8?q?g_an=09editorof_my_choice_=3F?= References: Message-ID: Sander Sweers gmail.com> writes: > > 2009/4/13 Dominique gmail.com>: > > What surprises me is that before calling the subprocess.Popen() method, I > > normalized the path using: > > filename = os.path.normpath(filename). > > It does.But do you also call os.path.normpath for the program path? > See below an example. > > >>> import os > >>> path = 'C:\Program Files\Notepad++\notepad++.exe' > >>> os.path.normpath(path) > 'C:\\Program Files\\Notepad++\notepad++.exe' > > Greets > Sander > _______________________________________________ > Tutor maillist - Tutor python.org > http://mail.python.org/mailman/listinfo/tutor > > You are perfectly right. I forgot to do it also for the program path ! Thanks. 1) Code directly in the console: In addition, the print instruction alters the path because of the backslash and the n: >>> import os.path >>> path = 'C:\Program Files\Notepad++\notepad++.exe' >>> path 'C:\\Program Files\\Notepad++\notepad++.exe' >>> print path C:\Program Files\Notepad++ otepad++.exe 2) Code inside a wxPython application: But inside a wxPython app, the following code: import os.path import subprocess as s path = 'C:\Program Files\Notepad++\notepad++.exe' print "path before = ",path path = os.path.normpath(path) print "path after = ", path s.Popen(path) returns: path before = C:\Program Files\Notepad++ otepad++.exe path after = C:\Program Files\Notepad++ otepad++.exe And of course it raises an exception when trying to open notepad++. If I add r before the path or 2 backslashes inside the path, everything is ok. It returns: path before = C:\Program Files\Notepad++\notepad++.exe path after = C:\Program Files\Notepad++\notepad++.exe And Notepad++ is opened. Note that the path returned only contains 1 backslash, as opposed to what is returned if it is coded directly in the console. That is what confused me. I think it should be linked to wxPython. Thanks a lot for your explanations and your time. Dominique From sierra_mtnview at sbcglobal.net Mon Apr 13 13:58:34 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Mon, 13 Apr 2009 04:58:34 -0700 Subject: [Tutor] Using numpy Array Capabilities In-Reply-To: References: <49E28D31.2090701@sbcglobal.net> Message-ID: <49E328EA.3070004@sbcglobal.net> Alan Gauld wrote: > > "Wayne Watson" wrote > >> Basically, what I would like to do is to take a list like: >> >> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >> >> and convert it to; >> [['0x0', '0x1', '0x2', '0x3', '0x4'], ['0x5', '0x6', '0x7', '0x8', >> '0x9']] > > What is the basis of this conversion? Use of pyfits (FITS formats for astronomy) needs it this way. > Why did you split it into two lists? Again, pyfits. It's a matrix/array (their version and used widely in math). > Why convert into hex strings? Ultimately, I may have to do it for pyfits. I was preparing for it's use there, so it was convenient to use what I had. The "array" I constructed is the important thing. In math, arrays (matrices) are always of the form r*c*p*..., where those symbols are integers. r,c, p, ... represent dimensions. x,y,z for example. > > If the original list had been: > > [3, 5, 78, 21, -3, 7, 5] > > What would your output look like? > There wouldn't be any. You have 7 elements. The first row of the matrix/table/array would be [3,4,78,21], and the second row would not contain the same # of elements. >> That is, it becomes a matrix of 2 rows and 5 columns. I generated the >> above from a loop in Python, and generated hex equivalents in the >> last list. > > It might help to see those loops because it might > explain the reasoning behind the apparently arbitrary > decisions. Explained above. > >> Can numpy do this simply without resorting to loops? > > Can't help there, I've never used numpy... > Well, the FITS and numpy guys (NASA) will be back today, so I'll work it out with them. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose is 5.52*10**-6 meters. That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From sierra_mtnview at sbcglobal.net Mon Apr 13 17:05:02 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Mon, 13 Apr 2009 08:05:02 -0700 Subject: [Tutor] Event Handling--Key Press, Mouse Movement ... Message-ID: <49E3549E.6070408@sbcglobal.net> Sometime ago, one of my posts brought a response brief exchange on event handling*. Where might I find more about event handling with respect to the keyboard, and particularly with a mouse. In the latter case, I'm interested in finding the pixel position of the mouse on a canvas. * I see from Alan Gauld, which refers to his web site. Still I'm curious about other sources. The few books I have on Python seem to avoid the subject. Perhaps it goes by another name in Python-ville. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose side is 5.52*10**-6 meters (tiny!). That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From sierra_mtnview at sbcglobal.net Mon Apr 13 17:48:08 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Mon, 13 Apr 2009 08:48:08 -0700 Subject: [Tutor] Event Handling--Key Press, Mouse Movement ... In-Reply-To: <49E3549E.6070408@sbcglobal.net> References: <49E3549E.6070408@sbcglobal.net> Message-ID: <49E35EB8.20900@sbcglobal.net> I see between Alan's site and the NM Tech Tkinter manual, that's good coverage for what I need. Wayne Watson wrote: > Sometime ago, one of my posts brought a response brief exchange on > event handling*. Where might I find more about event handling with > respect to the keyboard, and particularly with a mouse. In the latter > case, I'm interested in finding the pixel position of the mouse on a > canvas. > > * I see from Alan Gauld, which refers to his web site. Still I'm > curious about other sources. The few books I have on Python seem to > avoid the subject. Perhaps it goes by another name in Python-ville. > -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose side is 5.52*10**-6 meters (tiny!). That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From alan.gauld at btinternet.com Mon Apr 13 17:59:16 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Apr 2009 16:59:16 +0100 Subject: [Tutor] Event Handling--Key Press, Mouse Movement ... References: <49E3549E.6070408@sbcglobal.net> Message-ID: "Wayne Watson" wrote > about other sources. The few books I have on Python seem to avoid the > subject. Perhaps it goes by another name in Python-ville. No, its just that GUI programming is, relatively, a minority interest for Python programmers and event handling is a subset of GUI programming more than it is specifically Pythonic. The events themselves tend to be GUI specific - Windows being slightly different to X Windows or MacOS for example. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From gregor.lingl at aon.at Mon Apr 13 20:22:03 2009 From: gregor.lingl at aon.at (Gregor Lingl) Date: Mon, 13 Apr 2009 20:22:03 +0200 Subject: [Tutor] Event Handling--Key Press, Mouse Movement ... In-Reply-To: <49E3549E.6070408@sbcglobal.net> References: <49E3549E.6070408@sbcglobal.net> Message-ID: <49E382CB.8000400@aon.at> Wayne Watson schrieb: > Sometime ago, one of my posts brought a response brief exchange on > event handling*. Where might I find more about event handling with > respect to the keyboard, and particularly with a mouse. In the latter > case, I'm interested in finding the pixel position of the mouse on a > canvas. > Hi Wayne, there is some (limited) support of event handling in the turtle module of Python 2.6 Example: >>> from turtle import * >>> reset() >>> def printpos(x,y): print x,y >>> onscreenclick(printpos) >>> -284.0 80.0 # result of clicking the turtle module's playground is a Tkinter Canvas. Don't know if that fit's to your needs in some way. The module runs as is also with Python 2.5 and yuo can download it separately from here: http://svn.python.org/view/python/trunk/Lib/lib-tk/ More information on: http://docs.python.org/library/turtle.html#module-turtle Examples are in the source distribution (along with a demoViewer program, which also serves as an example on how to embed turtle graphics into a Tkinter application. You can download it separately from here: http://svn.python.org/view/python/trunk/Demo/turtle/ If you have enough time and if you are interested in it, here is a PyCon talk on the turtle module, which - as 6th "way" - contains a section on event driven programs followed by two examples of games: http://blip.tv/file/1947495 Best regards, Gregor > * I see from Alan Gauld, which refers to his web site. Still I'm > curious about other sources. The few books I have on Python seem to > avoid the subject. Perhaps it goes by another name in Python-ville. > From srilyk at gmail.com Mon Apr 13 20:47:21 2009 From: srilyk at gmail.com (W W) Date: Mon, 13 Apr 2009 13:47:21 -0500 Subject: [Tutor] Event Handling--Key Press, Mouse Movement ... In-Reply-To: <49E382CB.8000400@aon.at> References: <49E3549E.6070408@sbcglobal.net> <49E382CB.8000400@aon.at> Message-ID: <333efb450904131147g4d55a6c2i1fdc92063de99c7b@mail.gmail.com> On Mon, Apr 13, 2009 at 1:22 PM, Gregor Lingl wrote: > Wayne Watson schrieb: > >> Sometime ago, one of my posts brought a response brief exchange on event >> handling*. Where might I find more about event handling with respect to the >> keyboard, and particularly with a mouse. In the latter case, I'm interested >> in finding the pixel position of the mouse on a canvas. > > Easiest way: from Tkinter import * root = Tk() c = Canvas(root, width = 100, height = 100) def clicked(event): print "Clicked at x: %d y: %d" % (event.x, event.y) c.bind("", clicked) root.mainloop() and fire that up. There's also some motion events but I'd have to look them up to recall. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From sudhanshu9252 at gmail.com Mon Apr 13 22:36:04 2009 From: sudhanshu9252 at gmail.com (sudhanshu gautam) Date: Tue, 14 Apr 2009 02:06:04 +0530 Subject: [Tutor] OOPS Problem Message-ID: <6f9dbdf40904131336u6d603120y22f0258dc5d4088d@mail.gmail.com> I am reading python since last 15 days ,read some basics of python , Last night was reading about the OOPS concepts ,but getting some problems , going to discuss with you people. MY CODE : I made a class with the name student that prints simply name of the student as well as his roll_no also, pasting code here . code: class student: def __init__(self,name,roll_no): self.name=name self.roll_no def display(self): print 'name',self.name,'roll_no',self.roll_no s1=student("sudhanshu",21) s2=student("rahul",22) s1.display() s2.display() student is a class name problem 1: Why we always use first method as a __init__ problem 2: why we put first argument as a self inside the __init__ parentheses if __init__ is a constructor then how it works. How I can run this code , working on ubuntu : but when write inside the terminal python filename.py but it shows nothing regards password -------------- next part -------------- An HTML attachment was scrubbed... URL: From wescpy at gmail.com Mon Apr 13 22:46:06 2009 From: wescpy at gmail.com (wesley chun) Date: Mon, 13 Apr 2009 13:46:06 -0700 Subject: [Tutor] OOPS Problem In-Reply-To: <6f9dbdf40904131336u6d603120y22f0258dc5d4088d@mail.gmail.com> References: <6f9dbdf40904131336u6d603120y22f0258dc5d4088d@mail.gmail.com> Message-ID: <78b3a9580904131346p590675dcqdab217cc7b29cfe8@mail.gmail.com> hi, and welcome to Python! my comments below... > I made a class with the name student that prints simply name of the student > as well as his roll_no also, pasting code here . sounds pretty straightforward > class student: > ??? def __init__(self,name,roll_no): > ??? ?? ?self.name=name > ??? ?? ?self.roll_no i recommend you Capitalize the name of the class "student" to "Student" so that you can more easily visualize the difference between a class and other variables and functions, i.e., class Student. also, this is the end of __init__(). the rest of the code needs to be "dedented" indicate that it is *not* part of that method body: > ?? ?def display(self): > ?? ??? ?print 'name',self.name,'roll_no',self.roll_no similarly, your application code is below, and should not be indented at all: > s1=student("sudhanshu",21) # also change to Student later > s2=student("rahul",22) > s1.display() > s2.display() > > student is a class name exactly why i suggest changing the name to "Student" instead. > problem 1: Why we always use first method as a __init__ __init__() is not required. in general, people put that in their code as the 1st method if they have one, and they generally do, but not always. > problem 2: why we put first argument as a self inside the __init__ parentheses self is the required 1st parameter for *all* methods, not just __init__(). exceptions are static and class methods. > if __init__ is a constructor then how it works. Python allocates your objects for you, so it is more of an "initializer" than a "constructor." it is the 1st method that Python calls on your behalf after it instantiates your object for you. using __init__() allows you to "initialize" additional attributes before your newly-created instance is returned to you. > How I can run this code , working on ubuntu : but when write inside the > terminal python filename.py but it shows nothing if you make the above fixes, then running it on the cmd-line with "$ python FILE.py" will work, but just replace "FILE" with the name of your file. (also, the "$" is the shell prompt, so don't type that in.) hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From geoterium at gmail.com Tue Apr 14 00:55:57 2009 From: geoterium at gmail.com (George Wahid) Date: Tue, 14 Apr 2009 00:55:57 +0200 Subject: [Tutor] sender name in smtplib Message-ID: I want to send an email from a google mail account so that the sender name will be 'User Name' instead of 'username at gmail.com' in the inbox. this is the code I use: (I'm using python 2.5) >>>import smtplib >>>g=smtplib.SMTP('smtp.googlemail.com') >>>g.ehlo();g.starttls();g.ehlo() >>>g.login('username at gmail.com','somepassword') >>>g.sendmail('User Name','username at gmail.com','From: User Name\r\nTo: username at gmail.com\r\nSubject: how r u ?\r\nfoobar') {} >>>g.quit() but this doesn't work, what should I do ? From david at abbottdavid.com Tue Apr 14 01:04:48 2009 From: david at abbottdavid.com (David) Date: Mon, 13 Apr 2009 19:04:48 -0400 Subject: [Tutor] sender name in smtplib In-Reply-To: References: Message-ID: <49E3C510.2040209@abbottdavid.com> George Wahid wrote: > I want to send an email from a google mail account so that the sender > name will be 'User Name' instead of 'username at gmail.com' in the inbox. > this is the code I use: (I'm using python 2.5) >>>> import smtplib >>>> g=smtplib.SMTP('smtp.googlemail.com') >>>> g.ehlo();g.starttls();g.ehlo() >>>> g.login('username at gmail.com','somepassword') >>>> g.sendmail('User Name','username at gmail.com','From: User Name\r\nTo: username at gmail.com\r\nSubject: how r u ?\r\nfoobar') > {} >>>> g.quit() > > but this doesn't work, what should I do ? Here is how I do it, may help; http://dwabbott.com/code/index14.html -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From tim at johnsons-web.com Tue Apr 14 01:22:37 2009 From: tim at johnsons-web.com (Tim Johnson) Date: Mon, 13 Apr 2009 15:22:37 -0800 Subject: [Tutor] Trapping warnings from MySQLdb Message-ID: <200904131522.38061.tim@johnsons-web.com> FYI: Using python 2.5 with the MySQLdb module. I need to be able to raise an exeception on a MySQL warning. I have used my own class for years that wraps the MySQLdb module, but never did build in a feature that would let me 'tell' my class instances to raise an except for a warning: Example: I'm converting csv data into Mysql commands and executing them via a cursor object. I instantiate a cursor that returns a tuple as: self._rdb = self._conn.cursor() ## where self._conn is an object of the ## MySQdb.connect class # And I instantiate a cursor that returns a dictionary with self._rdb = self._conn.cursor(MySQLdb.cursors.DictCursor) Is there a way to create a cursor that _will_ raise an exeception on a warning? I have looked at the cursor.py module and was not able to find such a feature. Any and all ideas would be appreciated. This will be very useful to us. thanks tim From kent37 at tds.net Tue Apr 14 02:28:36 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 13 Apr 2009 20:28:36 -0400 Subject: [Tutor] sender name in smtplib In-Reply-To: References: Message-ID: <1c2a2c590904131728r6902571cqcf01ba537e32d138@mail.gmail.com> On Mon, Apr 13, 2009 at 6:55 PM, George Wahid wrote: > I want to send an email from a google mail account so that the sender > name will be 'User Name' instead of 'username at gmail.com' in the inbox. > this is the code I use: ? ? ? ? (I'm using python 2.5) >>>>import smtplib >>>>g=smtplib.SMTP('smtp.googlemail.com') >>>>g.ehlo();g.starttls();g.ehlo() >>>>g.login('username at gmail.com','somepassword') >>>>g.sendmail('User Name','username at gmail.com','From: User Name\r\nTo: username at gmail.com\r\nSubject: how r u ?\r\nfoobar') > {} >>>>g.quit() > > but this doesn't work, what should I do ? The first two arguments to sendmail() are the from address and the to address. Also the From: part of the body should be a valid address. I think you want something like g.sendmail('George Wahid ', 'User Name ', 'From: George Wahid \r\nTo: User Name \r\nSubject: how r u ?\r\nfoobar') Kent From kent37 at tds.net Tue Apr 14 02:45:49 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 13 Apr 2009 20:45:49 -0400 Subject: [Tutor] Trapping warnings from MySQLdb In-Reply-To: <200904131522.38061.tim@johnsons-web.com> References: <200904131522.38061.tim@johnsons-web.com> Message-ID: <1c2a2c590904131745u6e5a7b2x9ae3a31c56a24eae@mail.gmail.com> On Mon, Apr 13, 2009 at 7:22 PM, Tim Johnson wrote: > FYI: Using python 2.5 with the MySQLdb module. > I need to be able to raise an exeception on a MySQL warning. > > I have used my own class for years that wraps the MySQLdb module, > but never did build in a feature that would let me 'tell' my class > instances to raise an except for a warning: > Example: > I'm converting csv data into Mysql commands and executing them via > a cursor object. I instantiate a cursor that returns a tuple as: > self._rdb = self._conn.cursor() ## where self._conn is an object of the > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ## MySQdb.connect class > # And I instantiate a cursor that returns a dictionary with > self._rdb = self._conn.cursor(MySQLdb.cursors.DictCursor) > > Is there a way to create a cursor that _will_ raise an exeception on a > warning? > > I have looked at the cursor.py module and was not able to find such > a feature. Any and all ideas would be appreciated. This will be very > useful to us. >From a quick look at MySQLdb-1.2.2, it seems to be using the python std lib module warnings to report warnings. http://docs.python.org/library/warnings.html >From the warnings docs, it seems like warnings.simplefilter() is what you are looking for. Try this, perhaps in your wrapper module: import warnings import MySQLdb warnings.simplefilter("error", MySQLdb.Warning) I think that will tell the warnings module to treat MySQLdb.Warning as an error. Kent From tim at johnsons-web.com Tue Apr 14 03:54:21 2009 From: tim at johnsons-web.com (Tim Johnson) Date: Mon, 13 Apr 2009 17:54:21 -0800 Subject: [Tutor] Trapping warnings from MySQLdb In-Reply-To: <1c2a2c590904131745u6e5a7b2x9ae3a31c56a24eae@mail.gmail.com> References: <200904131522.38061.tim@johnsons-web.com> <1c2a2c590904131745u6e5a7b2x9ae3a31c56a24eae@mail.gmail.com> Message-ID: <200904131754.21387.tim@johnsons-web.com> On Monday 13 April 2009, Kent Johnson wrote: <....> > >From a quick look at MySQLdb-1.2.2, it seems to be using the python > > std lib module warnings to report warnings. > http://docs.python.org/library/warnings.html > > >From the warnings docs, it seems like warnings.simplefilter() is what > > you are looking for. Try this, perhaps in your wrapper module: > import warnings > import MySQLdb > warnings.simplefilter("error", MySQLdb.Warning) > > I think that will tell the warnings module to treat MySQLdb.Warning as an Yes! That is what I was looking for. Here's the code (in my wrapper class) def rcmd(self,s,test=0,trapwarnings=0): self._connect_method = 'row' if test: print("
SQL = %s
" % (s)) #DEBUG st if trapwarnings: warnings.simplefilter("error", MySQLdb.Warning) return self._rdb.execute(s) ## and the interface: try: db.rcmd(cmd,0,1) except _mysql_exceptions.Warning,e: ## give user ad hoc analysis of the offending cmd handle_warnings_here() ## Thanks a lot Ken tim From icebergwtf at gmail.com Tue Apr 14 04:48:36 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Tue, 14 Apr 2009 10:48:36 +0800 Subject: [Tutor] path to executing .py file Message-ID: <4314c1f70904131948v3958854ra85737622e980a83@mail.gmail.com> Hello everybody! I'm working on my code repository (svn) auto-backup script which get hotcopy of svn repository directory to a directory named by date in same location where script file is, it executed by a timer program every 00:00 clock. Everything works fine when I'm testing by double click it. But when auto execute it, I got unexpected behavior. The backup directory will be created under "C:\Documents and settings\username". I create backup directory by this way: os.mkdir(os.getcwd() + arch_dir) is there some way to get path to my executing script, so I can replaced "os.getcwd()" in above line? And I have another question, I've tried remove a copy of svn repository by calling shutil.rmtree(svn_repos_copy_dir) I got error "Access denied!" Is that mean my script has no power to delete it? Is there some way to give the right to do that? thanks tiefeng wu 2009-04-14 -------------- next part -------------- An HTML attachment was scrubbed... URL: From shiv_mbm at hotmail.com Tue Apr 14 04:59:58 2009 From: shiv_mbm at hotmail.com (ShivKumar Anand) Date: Tue, 14 Apr 2009 08:29:58 +0530 Subject: [Tutor] How to write server which listens to specific port In-Reply-To: <1c2a2c590904100356v2c3721c7n621efef8a9f5e264@mail.gmail.com> References: <1c2a2c590904100356v2c3721c7n621efef8a9f5e264@mail.gmail.com> Message-ID: Thanks Kent for this valuable information. My problem is I dont know how to send/receive a file using socket. How to get the filename specifically. Thanks Shiv > Date: Fri, 10 Apr 2009 06:56:34 -0400 > Subject: Re: [Tutor] How to write server which listens to specific port > From: kent37 at tds.net > To: shiv_mbm at hotmail.com > CC: tutor at python.org > > On Fri, Apr 10, 2009 at 4:59 AM, ShivKumar Anand wrote: > > > I have to implement HL7 in my existing application. > > so, I have to send/receive messages which are in a text file (not > > implementing v3.0, in which the messages are in xml format) > > Googling "python hl7" finds a couple of hints for the server and a > message parser: > http://mail.python.org/pipermail/python-list/2007-March/600733.html > http://pypi.python.org/pypi/hl7/0.1.0 > > > How can we configure in TurboGears to send and receive files? > > > > The protocol I want to use is TCP/IP v4 > > I don't think you can use TurboGears / CherryPy to create a server > that is not HTTP-based. For that look at socketserver or > asyncore/asynchat in the std lib or Twisted. > > Kent _________________________________________________________________ How fun is this? IMing with Windows Live Messenger just got better. http://www.microsoft.com/india/windows/windowslive/messenger.aspx -------------- next part -------------- An HTML attachment was scrubbed... URL: From dorseye at gmail.com Tue Apr 14 05:27:06 2009 From: dorseye at gmail.com (Eric Dorsey) Date: Mon, 13 Apr 2009 21:27:06 -0600 Subject: [Tutor] Trouble understanding modifying parent class.. Message-ID: Hi tutors, I am studying classes a bit, and am having trouble with this concept and would appreciate your help! class A: def __init__(self, name, value=1): self.name = name self.value = value And now I want a subclass, one that overrides the value=1 and defaults to value=2, how do I code that? I'm understanding subclasses that have different methods that have different behavior, but I'm having trouble doing this. Do we have to call the __init__ of the parent somehow? Is it "replaced" or overridden in an __init__ method of the subclass? .. or stated simply: If you have class A: which has def __init__(self, name, value=1), how do you code a subclass B(A): that automatically starts with value=2? -------------- next part -------------- An HTML attachment was scrubbed... URL: From charlie137 at gmail.com Tue Apr 14 06:31:58 2009 From: charlie137 at gmail.com (Guillaume Chereau) Date: Tue, 14 Apr 2009 12:31:58 +0800 Subject: [Tutor] Trouble understanding modifying parent class.. In-Reply-To: References: Message-ID: <8e9327d40904132131x4eb59a1qf9c650e9ed6de10b@mail.gmail.com> On Tue, Apr 14, 2009 at 11:27 AM, Eric Dorsey wrote: > Hi tutors, I am studying classes a bit, and am having trouble with this > concept and would appreciate your help! > > > class A: > ??? def __init__(self, name, value=1): > ??????? self.name = name > ??????? self.value = value > > And now I want a subclass, one that overrides the value=1 and defaults to > value=2, how do I code that? I'm understanding subclasses that have > different methods that have different behavior, but I'm having trouble doing > this. > > Do we have to call the __init__ of the parent somehow? Is it "replaced" or > overridden in an __init__ method of the subclass? > > .. or stated simply: > If you have class A: which has def __init__(self, name, value=1), how do you > code a subclass B(A): that automatically starts with value=2? Simply type : class B(A): def __init__(self, name, value=2): super(B, self).__init__(name, value) # rest of the __init__ method "super" will automatically call the __init__ method of the parent class (A) You can also do like this, but it less clean : class B(A): def __init__(self, name, value=2): A.__init__(self, name, value) # rest of the __init__ method cheers, Guillaume From sudhanshu9252 at gmail.com Tue Apr 14 07:07:46 2009 From: sudhanshu9252 at gmail.com (sudhanshu gautam) Date: Tue, 14 Apr 2009 10:37:46 +0530 Subject: [Tutor] python books Message-ID: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> I am new in python , so need a good books , previously read python Bible and swaroop but not satisfied . so tell me good books in pdf format those contents good problems also -------------- next part -------------- An HTML attachment was scrubbed... URL: From dextrous85 at gmail.com Tue Apr 14 07:10:27 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Tue, 14 Apr 2009 10:40:27 +0530 Subject: [Tutor] python books In-Reply-To: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> References: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> Message-ID: <5487b3060904132210h3d1efb5cj474f5afa912d0af4@mail.gmail.com> Take a look at Useless python http://www.uselesspython.com/ On Tue, Apr 14, 2009 at 10:37 AM, sudhanshu gautam wrote: > I am new in python , so need a good books , previously read python Bible > and swaroop but not satisfied . > > > so tell me good books in pdf format those contents good problems also > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From wescpy at gmail.com Tue Apr 14 09:16:10 2009 From: wescpy at gmail.com (wesley chun) Date: Tue, 14 Apr 2009 00:16:10 -0700 Subject: [Tutor] python books In-Reply-To: <5487b3060904132210h3d1efb5cj474f5afa912d0af4@mail.gmail.com> References: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> <5487b3060904132210h3d1efb5cj474f5afa912d0af4@mail.gmail.com> Message-ID: <78b3a9580904140016n271cd24cpf1cc4a1d5c68c20a@mail.gmail.com> >> I am new in python , so need a good books , previously read python Bible and swaroop but not satisfied . >> >> >> so tell me good books in pdf format those contents good problems also if you like a conversational style and know at least one programming language, i've written a pretty popular book with the Python crowd (see below). you can find it pretty much anywhere here in the US. if you happen to be in Southeast Asia, then you can pick it up from one of the retailers out there. in particular, here are at least 4 places in India: http://www.cb-india.com/proddetail.asp?prod=11266 http://www.nbcindia.com/Descriptions.asp?title_id=123 http://www.sapnaonline.com/MoreInfoBK.aspx?lcID=EBK0067672 http://www.flipkart.com/books/TU23FI6YAB.html it is also available in PDF format if you subscribe to O'Reilly's Safari service. best wishes! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From alan.gauld at btinternet.com Tue Apr 14 09:58:33 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 14 Apr 2009 08:58:33 +0100 Subject: [Tutor] How to write server which listens to specific port References: <1c2a2c590904100356v2c3721c7n621efef8a9f5e264@mail.gmail.com> Message-ID: "ShivKumar Anand" wrote > My problem is I dont know how to send/receive a file using socket. > > How to get the filename specifically. Could you use ftp? There is an ftp module. You need to work out what you want to do over this connection and choose the appropriate protocol. Then you can use a higher level module if one is available. Raw sockets should only be needed if you are doing bespoke communications between processes. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Apr 14 10:06:22 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 14 Apr 2009 09:06:22 +0100 Subject: [Tutor] path to executing .py file References: <4314c1f70904131948v3958854ra85737622e980a83@mail.gmail.com> Message-ID: "tiefeng wu" wrote > is there some way to get path to my executing script, so I can replaced > "os.getcwd()" in above line? Look at the recent thread on creating exe files with py2exe. Investigate the __file__ variable... > shutil.rmtree(svn_repos_copy_dir) > > I got error "Access denied!" Is that mean my script has no power to > delete > it? More likely the user running the script does not have the correct access permissions or someone else is using the repository at the time thus locking it. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Apr 14 10:12:35 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 14 Apr 2009 09:12:35 +0100 Subject: [Tutor] python books References: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> Message-ID: "sudhanshu gautam" wrote >I am new in python , so need a good books , previously read python Bible >and > swaroop but not satisfied . > > so tell me good books in pdf format those contents good problems also Does it have to be in PDF? There are many, many excellent Python web sites with lots of information if you can use HTML. The most obvious starting points for you would be the official Python tutorial and Dive Into Python and also, maybe, the Thinking in Python web book. FWIW my tutorial is also available in PDF, but if you have read the other resources it is likely too basic for you. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Apr 14 10:16:54 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 14 Apr 2009 09:16:54 +0100 Subject: [Tutor] Trouble understanding modifying parent class.. References: Message-ID: "Eric Dorsey" wrote > class A: > def __init__(self, name, value=1): > self.name = name > self.value = value > > And now I want a subclass, one that overrides the value=1 and defaults to > value=2, how do I code that? I'm understanding subclasses that have > different methods that have different behavior, but I'm having trouble > doing > this. > > Do we have to call the __init__ of the parent somehow? Is it "replaced" > or > overridden in an __init__ method of the subclass? You replace the behaviour you want to change, but in the replaced version you may choose to call the parent version. This typically leads to very simple methods in the subclass, as in your case: def B(A): def __init__(self, name, value=2): A.__init__(self,name,value) This defines a new init method which has a new default value. It then calls the original parent init method passing the name and value so that the A default never gets used in B instances. The OOP topic in my tutor describes this in more detail. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From daniel.chaowang at gmail.com Tue Apr 14 10:49:17 2009 From: daniel.chaowang at gmail.com (Daniel) Date: Tue, 14 Apr 2009 16:49:17 +0800 Subject: [Tutor] problem with a simple logger with RotatingFileHandler Message-ID: <57fe16f60904140149y11640aadr9f34c98b321f5073@mail.gmail.com> hi list, I write a simple logger, which follows the example on http://docs.python.org/library/logging.html, as this: def get_logger(log_filename): my_logger = logging.getLogger("MyLogger") my_logger.setLevel(logging.debug) handler = logging.handlers.RotatingFileHandler(log_filename, maxBytes=1000000, backupCount=5) my_logger.addHandler(handler) return my_logger def test_logger(): logger = get_logger(r"d:\tmp\mylog.txt") print logger msg = "repeted message" for i in range(10000): logger.info(msg) if __name__ == '__main__': test_logger() but the mylog.txt is empy after running. is there something I missed. Thanks, Daniel. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jrodriguo at hotmail.com Tue Apr 14 11:04:37 2009 From: jrodriguo at hotmail.com (j mercedes) Date: Tue, 14 Apr 2009 09:04:37 +0000 Subject: [Tutor] help Message-ID: hi, its me again jay, thanks alot everyone for your input on the last program i created that solved for the mode,mean, and average of a set of numbers it actually worked great..if anyone can give me any ideas on this program i am doing now it would be appreciated.I keep would like to understand why on the last portion it returns an error.thank you.import shelveimport stringUNKNOWN = 0HOME = 1WORK = 2FAX = 3CELL = 4class phoneentry: def __init__(self, name = 'Unknown', number = 'Unknown',type = UNKNOWN): self.name = name self.number = number self.type = type# create string representationdef __repr__(self): return('%s:%d' % ( self.name, self.type ))# fuzzy compare or two itemsdef __cmp__(self, that): this = string.lower(str(self)) that = string.lower(that) if string.find(this, that) >= 0: return(0) return(cmp(this, that))def showtype(self): if self.type == UNKNOWN: return('Unknown') if self.type == HOME: return('Home') if self.type == WORK: return('Work') if self.type == FAX: return('Fax') if self.type == CELL: return('Cellular')class phonedb: def __init__(self, dbname = 'phonedata'): self.dbname = dbname; self.shelve = shelve.open(self.dbname);def __del__(self): self.shelve.close() self.shelve = Nonedef add(self, name, number, type = HOME): e = phoneentry(name, number, type) self.shelve[str(e)] = edef lookup(self, string): list = [] for key in self.shelve.keys(): e = self.shelve[key] if cmp(e, string) == 0: list.append(e) return(list) if __name__ == '__main__': foo = phonedb() foo._add_('Sean Reifschneider', '970-555-1111', HOME) foo.add('Sean Reifschneider', '970-555-2222', CELL) foo.add('Evelyn Mitchell', '970-555-1111', HOME)print 'First lookup:'for entry in foo.lookup('reifsch'): print '%-40s %s (%s)' % ( entry.name, entry.number, entry.showtype())print 'Second lookup:'for entry in foo.lookup('e'): print '%-40s %s (%s)' % ( entry.name, entry.number, entry.showtype() ) -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Tue Apr 14 11:55:39 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Tue, 14 Apr 2009 11:55:39 +0200 Subject: [Tutor] problem with a simple logger with RotatingFileHandler In-Reply-To: <57fe16f60904140149y11640aadr9f34c98b321f5073@mail.gmail.com> References: <57fe16f60904140149y11640aadr9f34c98b321f5073@mail.gmail.com> Message-ID: 2009/4/14 Daniel : > def get_logger(log_filename): > ??? my_logger = logging.getLogger("MyLogger") > ??? my_logger.setLevel(logging.debug) ^^^^^^^ You have a small typo. This should be in capitals, logging.DEBUG. Greets Sander From kent37 at tds.net Tue Apr 14 12:35:16 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 14 Apr 2009 06:35:16 -0400 Subject: [Tutor] How to write server which listens to specific port In-Reply-To: References: <1c2a2c590904100356v2c3721c7n621efef8a9f5e264@mail.gmail.com> Message-ID: <1c2a2c590904140335i7c34504dn147278a186b99c4a@mail.gmail.com> On Mon, Apr 13, 2009 at 10:59 PM, ShivKumar Anand wrote: > Thanks?Kent for this valuable information. > > My problem is I dont know how to send/receive a file using socket. > How to get the filename specifically. Where does the file name come from? Do you ask the user? Is it requested by the remote client? There is an introduction to socket programming here: http://www.amk.ca/python/howto/sockets/ Did you look at the links I posted? The NetEpi project seems to have a complete implementation of an MLLP listener. It uses another project called halftwist that I can't find but it may still be helpful. The Mirth link also looks promising. This page seems to have a good description of the MLLP protocol: http://www.hl7.org/v3ballot/html/infrastructure/transport/transport-mllp.htm I don't know your experience level, but if you can't understand the above MLLP protocol document you are probably in over your head. Kent From sudhanshu9252 at gmail.com Tue Apr 14 19:42:19 2009 From: sudhanshu9252 at gmail.com (sudhanshu gautam) Date: Tue, 14 Apr 2009 23:12:19 +0530 Subject: [Tutor] python dictionary Message-ID: <6f9dbdf40904141042r4442a4d2h27b4c018023ffd6c@mail.gmail.com> I was reading about the python dictionaries had finished some methods related to the dictionary. we used get and setdefault methods to get the value from the dictionary if it lies inside it and setdefault used to set the value is side the dictionary if it does not lies inside the dictionary. so why we use setdefault I think in place of setdefault first we should convert that dictionary in the list and now we can add new value then again convert it in dictionary -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Tue Apr 14 19:52:14 2009 From: bgailer at gmail.com (bob gailer) Date: Tue, 14 Apr 2009 13:52:14 -0400 Subject: [Tutor] python dictionary In-Reply-To: <6f9dbdf40904141042r4442a4d2h27b4c018023ffd6c@mail.gmail.com> References: <6f9dbdf40904141042r4442a4d2h27b4c018023ffd6c@mail.gmail.com> Message-ID: <49E4CD4E.6020308@gmail.com> sudhanshu gautam wrote: > I was reading about the python dictionaries That is a good thing to do. > had finished some methods related to the dictionary. No comprehend. > > we used get and setdefault methods to get the value from the > dictionary if it lies inside it > > and setdefault used to set the value is side the dictionary if it does > not lies inside the dictionary. Those are good things to do. > > so why we use setdefault I think in place of setdefault first we > should convert that dictionary in the list and now we can add new > value then again convert it in dictionary > I'm sorry but I do not understand the final paragraph. It might help a little if you were to use more punctuation. Are you asking a question? Could you give an example? -- Bob Gailer Chapel Hill NC 919-636-4239 From alan.gauld at btinternet.com Tue Apr 14 20:14:44 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 14 Apr 2009 19:14:44 +0100 Subject: [Tutor] python dictionary References: <6f9dbdf40904141042r4442a4d2h27b4c018023ffd6c@mail.gmail.com> Message-ID: "sudhanshu gautam" wrote > and setdefault used to set the value is side the dictionary if it does > not > lies inside the dictionary. > > so why we use setdefault I think in place of setdefault first we should > convert that dictionary in the list and now we can add new value then > again > convert it in dictionary You probably could convert it to a list, append a tuple and then convert the list back to a dictionary. But why would you want to do that when setdefault is so much easier? Also the conversions between list and dictionary would take a lot of time and computer power for a big dictionary For example I tried >>> L = [(n,n) for n in range(1000000)] >>> d = dict(L) >>> d2 = [(k,v) for k,v in d.items()] >>> d.setdefault(-5,-5) - producing the dict from the list took about half a second - producing the list from the dict took over 3 seconds and used over 50% of my CPU... - using setdefault() was effectively instantaneous. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From sudhanshu9252 at gmail.com Tue Apr 14 22:20:22 2009 From: sudhanshu9252 at gmail.com (sudhanshu gautam) Date: Wed, 15 Apr 2009 01:50:22 +0530 Subject: [Tutor] oops concepts Message-ID: <6f9dbdf40904141320v5ec25d61sbc684e05938a5869@mail.gmail.com> are there any Links by which I can get good knowledge in OOPS . I want to go with good problems in OOPS so tell ,me any good book or link of oops concepts -------------- next part -------------- An HTML attachment was scrubbed... URL: From ian505 at gmail.com Wed Apr 15 00:16:46 2009 From: ian505 at gmail.com (Ian Egland) Date: Tue, 14 Apr 2009 18:16:46 -0400 Subject: [Tutor] python books In-Reply-To: References: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> Message-ID: I know it's not in PDF, but I thought it would be worth mentioning Python for Software Design: How to Think Like a Computer Scientist. I discovered this exactly a week after it was released, ordered it, and have been extremely happy with it so far. I am a student currently learning Java (unfortunately) and am really enjoying the original, knowledgeable examples and good presentation this book has to offer. Definitely one of my better book purchases, and significantly better than the Python Bible if I remember correctly. -Ian On Tue, Apr 14, 2009 at 4:12 AM, Alan Gauld wrote: > > "sudhanshu gautam" wrote > > I am new in python , so need a good books , previously read python Bible >> and >> swaroop but not satisfied . >> >> so tell me good books in pdf format those contents good problems also >> > > Does it have to be in PDF? There are many, many excellent Python web > sites with lots of information if you can use HTML. > > The most obvious starting points for you would be the official > Python tutorial and Dive Into Python and also, maybe, the > Thinking in Python web book. > > FWIW my tutorial is also available in PDF, but if you have read the other > resources it is likely too basic for you. > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Apr 15 00:27:33 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 14 Apr 2009 23:27:33 +0100 Subject: [Tutor] oops concepts References: <6f9dbdf40904141320v5ec25d61sbc684e05938a5869@mail.gmail.com> Message-ID: "sudhanshu gautam" wrote > are there any Links by which I can get good knowledge in OOPS . Try the cetus links web site. Also google and wikipedia will throw up lots of sites. > I want to go with good problems in OOPS so tell me any good > book or link of oops concepts My OOP topic lists 3 books as well as a link to cetus links. There are also things like Thinking In Java/C++ and the Design Patterns site. A lot depends on whether you want to really understand the theory behind OOP or are happy just knowing how to build classes and use OOP to write programs. Most people are happy with the latter and in that case just pick whichever site makes most sense to you and then write a lot of code. If you do want a good theoretical grounding look to books like OOSC by Meyer and OOAD by Booch. But you should also consider the pair of Schlaer-Mellor books which make no requirement that you use an OOP language to build a pure OOP system. They are quite different in approach to most modern OOP text books but once you get your head wrapped around the ideas it is a very powerful approach. For a more mathematically rigorous approach yet you should read up on concurrent sequential machines and state automata theory and any text book on formal simulation techniques. HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emadnawfal at gmail.com Wed Apr 15 01:39:21 2009 From: emadnawfal at gmail.com (=?windows-1256?B?RW1hZCBOYXdmYWwgKNrjx88g5Obd4Sk=?=) Date: Tue, 14 Apr 2009 19:39:21 -0400 Subject: [Tutor] sorting file paths made up of of strings and numbers Message-ID: <652641e90904141639y57c6d75dsf1d4295f18b451f3@mail.gmail.com> Hi tutors, How can I sort the following list in a way that takes care of the right order of numbers? The sorted function compares strings here as far as I can see, but I want to have filepath2 follow filepath1. Your help is appreciated. >>> myList ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89', 'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15', 'filepath88', 'filepath70', 'filepath33', 'filepath58', 'filepath28', 'filepath61', 'filepath23', 'filepath64', 'filepath72', 'filepath11', 'filepath63', 'filepath76', 'filepath87', 'filepath45', 'filepath41', 'filepath68', 'filepath81', 'filepath75', 'filepath62', 'filepath46', 'filepath36', 'filepath39', 'filepath5', 'filepath34', 'filepath8', 'filepath67', 'filepath14', 'filepath82', 'filepath29', 'filepath47', 'filepath79', 'filepath13', 'filepath16', 'filepath71', 'filepath27', 'filepath100', 'filepath20', 'filepath92', 'filepath57', 'filepath73', 'filepath40', 'filepath44', 'filepath83', 'filepath50', 'filepath66', 'filepath94', 'filepath86', 'filepath22', 'filepath17', 'filepath84', 'filepath38', 'filepath12', 'filepath53', 'filepath6', 'filepath48', 'filepath60', 'filepath51', 'filepath90', 'filepath4', 'filepath78', 'filepath69', 'filepath35', 'filepath97', 'filepath93', 'filepath24', 'filepath26', 'filepath1', 'filepath3', 'filepath96', 'filepath77', 'filepath98', 'filepath18', 'filepath43', 'filepath19', 'filepath56', 'filepath65', 'filepath95', 'filepath55', 'filepath7', 'filepath99', 'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10', 'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74', 'filepath21'] >>> sorted(myList) ['filepath0', 'filepath1', 'filepath10', 'filepath100', 'filepath11', 'filepath12', 'filepath13', 'filepath14', 'filepath15', 'filepath16', 'filepath17', 'filepath18', 'filepath19', 'filepath2', 'filepath20', 'filepath21', 'filepath22', 'filepath23', 'filepath24', 'filepath25', 'filepath26', 'filepath27', 'filepath28', 'filepath29', 'filepath3', 'filepath30', 'filepath31', 'filepath32', 'filepath33', 'filepath34', 'filepath35', 'filepath36', 'filepath37', 'filepath38', 'filepath39', 'filepath4', 'filepath40', 'filepath41', 'filepath42', 'filepath43', 'filepath44', 'filepath45', 'filepath46', 'filepath47', 'filepath48', 'filepath49', 'filepath5', 'filepath50', 'filepath51', 'filepath52', 'filepath53', 'filepath54', 'filepath55', 'filepath56', 'filepath57', 'filepath58', 'filepath59', 'filepath6', 'filepath60', 'filepath61', 'filepath62', 'filepath63', 'filepath64', 'filepath65', 'filepath66', 'filepath67', 'filepath68', 'filepath69', 'filepath7', 'filepath70', 'filepath71', 'filepath72', 'filepath73', 'filepath74', 'filepath75', 'filepath76', 'filepath77', 'filepath78', 'filepath79', 'filepath8', 'filepath80', 'filepath81', 'filepath82', 'filepath83', 'filepath84', 'filepath85', 'filepath86', 'filepath87', 'filepath88', 'filepath89', 'filepath9', 'filepath90', 'filepath91', 'filepath92', 'filepath93', 'filepath94', 'filepath95', 'filepath96', 'filepath97', 'filepath98', 'filepath99'] >>> ``` -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From wescpy at gmail.com Wed Apr 15 02:10:20 2009 From: wescpy at gmail.com (wesley chun) Date: Tue, 14 Apr 2009 17:10:20 -0700 Subject: [Tutor] sorting file paths made up of of strings and numbers In-Reply-To: <652641e90904141639y57c6d75dsf1d4295f18b451f3@mail.gmail.com> References: <652641e90904141639y57c6d75dsf1d4295f18b451f3@mail.gmail.com> Message-ID: <78b3a9580904141710g34612980xed802d07e0381854@mail.gmail.com> > How can I sort the following list in a way that takes care of the right > order of numbers? The sorted function compares strings here as far as I can > see, but I want to have filepath2 follow filepath1. Your help is > appreciated. >>>> myList > ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89', > 'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15', > : > 'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10', > 'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74', > 'filepath21'] unfortunately, we're restricted from solving your homework problems for you, however, we can tell you that you can still use sorted(). just combine that with operator.itemgetter(), and you should be well on your way to solving your problem! hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From emadnawfal at gmail.com Wed Apr 15 02:18:32 2009 From: emadnawfal at gmail.com (=?windows-1256?B?RW1hZCBOYXdmYWwgKNrjx88g5Obd4Sk=?=) Date: Tue, 14 Apr 2009 20:18:32 -0400 Subject: [Tutor] sorting file paths made up of of strings and numbers In-Reply-To: <78b3a9580904141710g34612980xed802d07e0381854@mail.gmail.com> References: <652641e90904141639y57c6d75dsf1d4295f18b451f3@mail.gmail.com> <78b3a9580904141710g34612980xed802d07e0381854@mail.gmail.com> Message-ID: <652641e90904141718u78405160icc3c05a11bc5f408@mail.gmail.com> On Tue, Apr 14, 2009 at 8:10 PM, wesley chun wrote: > > How can I sort the following list in a way that takes care of the right > > order of numbers? The sorted function compares strings here as far as I > can > > see, but I want to have filepath2 follow filepath1. Your help is > > appreciated. > >>>> myList > > ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89', > > 'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15', > > : > > 'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10', > > 'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74', > > 'filepath21'] > > > unfortunately, we're restricted from solving your homework problems > for you, however, we can tell you that you can still use sorted(). > just combine that with operator.itemgetter(), and you should be well > on your way to solving your problem! > > hope this helps! > -- wesley > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > "Core Python Programming", Prentice Hall, (c)2007,2001 > "Python Fundamentals", Prentice Hall, (c)2009 > http://corepython.com > > wesley.j.chun :: wescpy-at-gmail.com > python training and technical consulting > cyberweb.consulting : silicon valley, ca > http://cyberwebconsulting.com > Thank you so much Wesley for the tip. I'll look into itemgetter() This is not a homework problem though, but I'm working on a paper in which this is useful. So, it is related to school, but is by no means a homework. -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Apr 15 04:28:22 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 14 Apr 2009 22:28:22 -0400 Subject: [Tutor] sorting file paths made up of of strings and numbers In-Reply-To: <652641e90904141639y57c6d75dsf1d4295f18b451f3@mail.gmail.com> References: <652641e90904141639y57c6d75dsf1d4295f18b451f3@mail.gmail.com> Message-ID: <1c2a2c590904141928h43ebdae6me81672414a0af403@mail.gmail.com> 2009/4/14 Emad Nawfal (???? ????) : > Hi tutors, > How can I sort the following list in a way that takes care of the right > order of numbers? The sorted function compares strings here as far as I can > see, but I want to have filepath2 follow filepath1. Your help is > appreciated. >>>> myList > ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89', > 'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15', The sort() and sorted() functions have an optional key= parameter. The parameter should be a function whose argument is a list element and whose result is the desired key. So for your list, you need a function that creates an integer from the list item: In [1]: myList = ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89', 'filepath52', 'filepath37', 'filepath32', ' filepath2', 'filepath15'] In [2]: def index(fp): return int(fp[8:]) Now sort the list using the key: In [3]: myList.sort(key=index) In [4]: myList Out[4]: ['filepath0', 'filepath2', 'filepath15', 'filepath25', 'filepath32', 'filepath37', 'filepath49', 'filepath52', 'filepath54', 'filepath89'] Note that the key parameter takes the function object itself, there are no parentheses. i.e. key=index, not key=index(). Kent From bill at celestial.net Wed Apr 15 04:36:23 2009 From: bill at celestial.net (Bill Campbell) Date: Tue, 14 Apr 2009 19:36:23 -0700 Subject: [Tutor] sorting file paths made up of of strings and numbers In-Reply-To: <652641e90904141639y57c6d75dsf1d4295f18b451f3@mail.gmail.com> References: <652641e90904141639y57c6d75dsf1d4295f18b451f3@mail.gmail.com> Message-ID: <20090415023622.GB22771@ayn.mi.celestial.com> On Tue, Apr 14, 2009, Emad Nawfal (???? ????) wrote: > > Hi tutors, > How can I sort the following list in a way that takes care of the > right order of numbers? The sorted function compares strings here as > far as I can see, but I want to have filepath2 follow filepath1. Your > help is appreciated. There is at least one example of a method of dealing with this in the O'Reilly ``Python cookbook''. > >>> myList > ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89', > 'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15', > 'filepath88', 'filepath70', 'filepath33', 'filepath58', 'filepath28', > 'filepath61', 'filepath23', 'filepath64', 'filepath72', 'filepath11', > 'filepath63', 'filepath76', 'filepath87', 'filepath45', 'filepath41', > 'filepath68', 'filepath81', 'filepath75', 'filepath62', 'filepath46', > 'filepath36', 'filepath39', 'filepath5', 'filepath34', 'filepath8', > 'filepath67', 'filepath14', 'filepath82', 'filepath29', 'filepath47', > 'filepath79', 'filepath13', 'filepath16', 'filepath71', 'filepath27', > 'filepath100', 'filepath20', 'filepath92', 'filepath57', 'filepath73', > 'filepath40', 'filepath44', 'filepath83', 'filepath50', 'filepath66', > 'filepath94', 'filepath86', 'filepath22', 'filepath17', 'filepath84', > 'filepath38', 'filepath12', 'filepath53', 'filepath6', 'filepath48', > 'filepath60', 'filepath51', 'filepath90', 'filepath4', 'filepath78', > 'filepath69', 'filepath35', 'filepath97', 'filepath93', 'filepath24', > 'filepath26', 'filepath1', 'filepath3', 'filepath96', 'filepath77', > 'filepath98', 'filepath18', 'filepath43', 'filepath19', 'filepath56', > 'filepath65', 'filepath95', 'filepath55', 'filepath7', 'filepath99', > 'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10', > 'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74', > 'filepath21'] > >>> sorted(myList) > ['filepath0', 'filepath1', 'filepath10', 'filepath100', 'filepath11', > 'filepath12', 'filepath13', 'filepath14', 'filepath15', 'filepath16', > 'filepath17', 'filepath18', 'filepath19', 'filepath2', 'filepath20', > 'filepath21', 'filepath22', 'filepath23', 'filepath24', 'filepath25', > 'filepath26', 'filepath27', 'filepath28', 'filepath29', 'filepath3', > 'filepath30', 'filepath31', 'filepath32', 'filepath33', 'filepath34', > 'filepath35', 'filepath36', 'filepath37', 'filepath38', 'filepath39', > 'filepath4', 'filepath40', 'filepath41', 'filepath42', 'filepath43', > 'filepath44', 'filepath45', 'filepath46', 'filepath47', 'filepath48', > 'filepath49', 'filepath5', 'filepath50', 'filepath51', 'filepath52', > 'filepath53', 'filepath54', 'filepath55', 'filepath56', 'filepath57', > 'filepath58', 'filepath59', 'filepath6', 'filepath60', 'filepath61', > 'filepath62', 'filepath63', 'filepath64', 'filepath65', 'filepath66', > 'filepath67', 'filepath68', 'filepath69', 'filepath7', 'filepath70', > 'filepath71', 'filepath72', 'filepath73', 'filepath74', 'filepath75', > 'filepath76', 'filepath77', 'filepath78', 'filepath79', 'filepath8', > 'filepath80', 'filepath81', 'filepath82', 'filepath83', 'filepath84', > 'filepath85', 'filepath86', 'filepath87', 'filepath88', 'filepath89', > 'filepath9', 'filepath90', 'filepath91', 'filepath92', 'filepath93', > 'filepath94', 'filepath95', 'filepath96', 'filepath97', 'filepath98', > 'filepath99'] > >>> ``` > -- > ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? > ????????.....???? ??????? > "No victim has ever been more repressed and alienated than the truth" > Emad Soliman Nawfal > Indiana University, Bloomington > -------------------------------------------------------- >_______________________________________________ >Tutor maillist - Tutor at python.org >http://mail.python.org/mailman/listinfo/tutor -- Bill -- INTERNET: bill at celestial.com Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax: (206) 232-9186 Skype: jwccsllc (206) 855-5792 An almost hysterical antagonism toward the gold standard is one issue which unites statists of all persuasions. They seem to sense that gold and economic freedom are inseparable. -- Alan Greenspan From icebergwtf at gmail.com Wed Apr 15 05:10:22 2009 From: icebergwtf at gmail.com (tiefeng wu) Date: Wed, 15 Apr 2009 11:10:22 +0800 Subject: [Tutor] path to executing .py file Message-ID: <4314c1f70904142010v10aaf3b5ue114633e7beda41a@mail.gmail.com> > > is there some way to get path to my executing script, so I can replaced >> "os.getcwd()" in above line? >> > > Look at the recent thread on creating exe files with py2exe. > Investigate the __file__ variable... thanks, Alan Gauld. thanks for your patience for such a trivial question:) > shutil.rmtree(svn_repos_copy_dir) >> >> I got error "Access denied!" Is that mean my script has no power to delete >> it? >> > > More likely the user running the script does not have the correct > access permissions or someone else is using the repository at > the time thus locking it. as I mentioned, that's a COPY of svn repository, I'm sure there's no one locking it I tried manually delete it by 'del' and 'rm' under command line, and I got same error, but deleting in windows explorer (with same user login as script was called) with no problem. if this question has nothing concerned with this mailing list, I'm sorry an please ignore it. tiefeng wu -------------- next part -------------- An HTML attachment was scrubbed... URL: From mbikinyi_brat at yahoo.com Wed Apr 15 09:43:44 2009 From: mbikinyi_brat at yahoo.com (mbikinyi brat) Date: Wed, 15 Apr 2009 00:43:44 -0700 (PDT) Subject: [Tutor] OOP Message-ID: <670019.86287.qm@web110603.mail.gq1.yahoo.com> Dear ALL, I am a python beginner and has never programmed and has been struggling to understand how to create objects or classes in python. Can anyone help with any concrete example. I have read most recommended textbooks but still have difficulties figuring what it is all about. ? Thanks, Henry -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Apr 15 10:02:20 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 15 Apr 2009 09:02:20 +0100 Subject: [Tutor] OOP References: <670019.86287.qm@web110603.mail.gq1.yahoo.com> Message-ID: "mbikinyi brat" wrote > I am a python beginner and has never programmed Welcome to the tutor list. > and has been struggling to understand how to create > objects or classes in python. Can anyone help with any > concrete example. Most tutorials have examples of classes and objects. Certainly mine does. There is an example in the Raw Data topic (with a sidebar at the end explaining it line by line) There is also a more detailed explanatyioon in the OOP topic in the Advanced section Here is one example: # define a class class Message: def __init__(self, msg=""): self.text = msg def show(self): print self.text # create some objects m1 = Message("Hello world") m2 = Message("Goodbye!") m3 = Message() # send the objects a message m1.show() m3.show() m2.show() > I have read most recommended textbooks but still > have difficulties figuring what it is all about. If you can show us an example and tell us what you don't understand we might be able to give more specific answers. Also please tell us which version of Python you are using and which Operating System. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From beattls at gmail.com Wed Apr 15 10:50:35 2009 From: beattls at gmail.com (Evert Edel) Date: Wed, 15 Apr 2009 10:50:35 +0200 Subject: [Tutor] Looking for starter projects Message-ID: <3279d5360904150150wd52e084i7d9b6fd7b2480e34@mail.gmail.com> Hi all, This is my first post to this tutor list, so I hope you'll all get this message :). I've bought last year a book on python programming (Learning python from O'Reilly) to get started with python. I'm wanting to learn python for two purposes: learning some webdevving (with django?) and being able to write some smaller programs. Now since I've got the learning python book I first did a quick read trough it and now I'm going more slowly trough it and doing all the explained things (in the interactive prompt). I do understand the basics behind OOP and I'm wanting to start some smaller projects using the book as a reference to get my hands on python. It seems to me that it's better to learn it with smaller projects (and evolving with those). But I really don't know what smaller projects would be good to start on as beginner. I've got some ideas but those are more advanced (making a mpd client, some website ideas). I hope you can give me some starting projects to learn python :). Thanks in advance, Evert -------------- next part -------------- An HTML attachment was scrubbed... URL: From mbikinyi_brat at yahoo.com Wed Apr 15 10:52:44 2009 From: mbikinyi_brat at yahoo.com (mbikinyi brat) Date: Wed, 15 Apr 2009 01:52:44 -0700 (PDT) Subject: [Tutor] calculate area of a circle Message-ID: <708369.57431.qm@web110610.mail.gq1.yahoo.com> Hi All, This is a code I have written to calculate the area of a circle. def area(radius): ?temp=math.pi*radius**2 ?return temp ? I now call it by entering area(12) at the prompt in IDLE. This is the error message I get. Can someone help please? Traceback (most recent call last): ? File "", line 1, in ??? area(10) ? File "", line 2, in area ??? temp=math.pi*radius**2 NameError: global name 'math' is not defined ? Regards, Henry ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dextrous85 at gmail.com Wed Apr 15 10:59:16 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Wed, 15 Apr 2009 14:29:16 +0530 Subject: [Tutor] calculate area of a circle In-Reply-To: <708369.57431.qm@web110610.mail.gq1.yahoo.com> References: <708369.57431.qm@web110610.mail.gq1.yahoo.com> Message-ID: <5487b3060904150159m7fd78e2r1b1a798b7212cd97@mail.gmail.com> you need import math On Wed, Apr 15, 2009 at 2:22 PM, mbikinyi brat wrote: > Hi All, > This is a code I have written to calculate the area of a circle. > *def area(radius): > temp=math.pi*radius**2 > return temp* > ** > *I* now call it by entering *area(12)* at the prompt in IDLE. This is the > error message I get. Can someone help please? > *Traceback (most recent call last): > File "", line 1, in > area(10) > File "", line 2, in area > temp=math.pi*radius**2 > NameError: global name 'math' is not defined* > ** > Regards, > Henry > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From moron.oxy at gmail.com Wed Apr 15 11:01:24 2009 From: moron.oxy at gmail.com (Oxymoron) Date: Wed, 15 Apr 2009 19:01:24 +1000 Subject: [Tutor] calculate area of a circle In-Reply-To: <708369.57431.qm@web110610.mail.gq1.yahoo.com> References: <708369.57431.qm@web110610.mail.gq1.yahoo.com> Message-ID: <2096a7260904150201g339a72fap1ba15ec21758b5c8@mail.gmail.com> Hi, On Wed, Apr 15, 2009 at 6:52 PM, mbikinyi brat wrote: > Hi All, > This is a code I have written to calculate the area of a circle. > def area(radius): > ?temp=math.pi*radius**2 > ?return temp > > I now call it by entering area(12) at the prompt in IDLE. This is the error > message I get. Can someone help please? > Traceback (most recent call last): > ? File "", line 1, in > ??? area(10) > ? File "", line 2, in area > ??? temp=math.pi*radius**2 > NameError: global name 'math' is not defined You need to import the 'math' module to use its member pi. Prior to using the module (e.g. before typing in the area function), type: import math Lookup modules for more info (e.g. http://docs.python.org/tutorial/modules.html). -- K From mbikinyi_brat at yahoo.com Wed Apr 15 11:29:54 2009 From: mbikinyi_brat at yahoo.com (mbikinyi brat) Date: Wed, 15 Apr 2009 02:29:54 -0700 (PDT) Subject: [Tutor] colours in IDLE Message-ID: <275664.30512.qm@web110603.mail.gq1.yahoo.com> Dear ALL, When you type a code in IDLE-Python, they appear in different colours. For instance: ?def factorial(n): ?if n==0: ??return 1 ?else: ??recurse=factorial(n-1) ??result=n*recurse ??return result factorial in blue and return and else and result in red. Any significance to these colours? ? Regards, Henry -------------- next part -------------- An HTML attachment was scrubbed... URL: From moron.oxy at gmail.com Wed Apr 15 11:36:10 2009 From: moron.oxy at gmail.com (Oxymoron) Date: Wed, 15 Apr 2009 19:36:10 +1000 Subject: [Tutor] colours in IDLE In-Reply-To: <275664.30512.qm@web110603.mail.gq1.yahoo.com> References: <275664.30512.qm@web110603.mail.gq1.yahoo.com> Message-ID: <2096a7260904150236p2c6f61f8k94dc76dcfda87292@mail.gmail.com> Hi, On Wed, Apr 15, 2009 at 7:29 PM, mbikinyi brat wrote: > Dear ALL, > When you type a code in IDLE-Python, they appear in different colours. > For instance: > ?def factorial(n): > ?if n==0: > ??return 1 > ?else: > ??recurse=factorial(n-1) > ??result=n*recurse > ??return result > factorial in blue and return and else and result in red. > Any significance to these colours? > That's called "syntax-highlighting", it basically allows you to read the code clearer. Different Python language constructs are given appropriate colours. I don't use IDLE myself, but in your example, 'factorial' - is a function name - an identifier in general, that's assigned one colour, whereas 'def', 'else', 'return' are Python keywords - you cannot use these as identifiers - they're assigned yet another colour. Keywords, braces, operators, identifiers - these constitute the 'syntax' of Python. Hence using colours highlights the syntax so it's easier to read -- syntax-highlighting ;-). From sierra_mtnview at sbcglobal.net Wed Apr 15 11:45:07 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Wed, 15 Apr 2009 02:45:07 -0700 Subject: [Tutor] python books In-Reply-To: References: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> Message-ID: <49E5ACA3.1030804@sbcglobal.net> It's unlikely you are going to find a pdf on Python that's suitable for beginners. Do you mean pdf or a digital book? There are Python books in digital form on the web. I'm not quite sure how it works, but I know of at least one public library has them. I think it works that if you have a library card, then you can get web access to read the book on-line. I think this operates at a college level too. Consider Python for Absolute Beginners. If you are pressed for money, go to a college bookstore and see if you can find it used, probably $18 USD used (I know by experience! I do not own it or have done much more than browse a few pages of it.). I know of one university here that uses it. It is not likely that it would be used for a computer science (CS) classes, but I suspect there are students there in non-CS classes who might need to use Python. For example, engineers, math-majors, elementary statistics classes, and even upper-level astronomy classes. One can often get a good sense of a book by going to Amazon. They have a Look-Inside feature that allows you to browse parts of many books. -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose side is 5.52*10**-6 meters (tiny!). That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From moron.oxy at gmail.com Wed Apr 15 11:52:44 2009 From: moron.oxy at gmail.com (Oxymoron) Date: Wed, 15 Apr 2009 19:52:44 +1000 Subject: [Tutor] colours in IDLE(How does the interpreter knows the meaning of factorial) In-Reply-To: <989124.82708.qm@web110602.mail.gq1.yahoo.com> References: <989124.82708.qm@web110602.mail.gq1.yahoo.com> Message-ID: <2096a7260904150252o78dc1e7ag60a72352ac121884@mail.gmail.com> Hi, <> On Wed, Apr 15, 2009 at 7:41 PM, mbikinyi brat wrote: > Dear Oxymoron, > In my previous example I had to import math for it to work . But in this > code with the factorial, I have not imported anything yet when I call it > with factorial(5), I get the result. How is this possible? In this example, you're not using anything from an external module like math, your code is self-contained, so you did not have to import anything. Earlier you called math.pi - math is a module that's part of standard Python, but still needs to be imported, it is external to your file. > Regards, > Henry > > --- On Wed, 4/15/09, Oxymoron wrote: > > From: Oxymoron > Subject: Re: [Tutor] colours in IDLE > To: tutor at python.org > Date: Wednesday, April 15, 2009, 5:36 AM > > Hi, > > On Wed, Apr 15, 2009 at 7:29 PM, mbikinyi brat > wrote: >> Dear ALL, >> When you type a code in IDLE-Python, they appear in different colours. >> For instance: >> ?def factorial(n): >> ?if n==0: >> ??return 1 >> ?else: >> ??recurse=factorial(n-1) >> ??result=n*recurse >> ??return result >> factorial in blue and return and else and result in red. >> Any significance to these colours? >> > > That's called "syntax-highlighting", it basically allows you to read > the code clearer. Different Python language constructs are given > appropriate colours. I don't use IDLE myself, but in your example, > 'factorial' - is a function name - an identifier in general, that's > assigned one colour, whereas 'def', 'else', 'return' are Python > keywords - you cannot use these as identifiers - they're assigned yet > another colour. Keywords, braces, operators, identifiers - these > constitute the 'syntax' of Python. Hence using colours highlights the > syntax so it's easier to read -- syntax-highlighting ;-). > _______________________________________________ > Tutor maillist? -? Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- There is more to life than increasing its speed. -- Mahatma Gandhi From kent37 at tds.net Wed Apr 15 12:38:24 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 15 Apr 2009 06:38:24 -0400 Subject: [Tutor] OOP In-Reply-To: <670019.86287.qm@web110603.mail.gq1.yahoo.com> References: <670019.86287.qm@web110603.mail.gq1.yahoo.com> Message-ID: <1c2a2c590904150338y466be82dm2db0fc1e2dfa5f8d@mail.gmail.com> On Wed, Apr 15, 2009 at 3:43 AM, mbikinyi brat wrote: > Dear ALL, > I am a python beginner and has never programmed and has been struggling to > understand how to create objects or classes in python. Can anyone help with > any concrete example. I have read most recommended textbooks but still have > difficulties figuring what it is all about. Any tutorial should contain simple examples of how to create classes. If you are struggling with *why* to create classes you might like this essay, which gives several reasons why you might want to start using classes in a design, with examples from the standard library: http://personalpages.tds.net/~kent37/stories/00014.html Kent From emadnawfal at gmail.com Wed Apr 15 12:48:00 2009 From: emadnawfal at gmail.com (=?windows-1256?B?RW1hZCBOYXdmYWwgKNrjx88g5Obd4Sk=?=) Date: Wed, 15 Apr 2009 06:48:00 -0400 Subject: [Tutor] sorting file paths made up of of strings and numbers In-Reply-To: <20090415023622.GB22771@ayn.mi.celestial.com> References: <652641e90904141639y57c6d75dsf1d4295f18b451f3@mail.gmail.com> <20090415023622.GB22771@ayn.mi.celestial.com> Message-ID: <652641e90904150348n4059473h63c0d185d3e9a69@mail.gmail.com> On Tue, Apr 14, 2009 at 10:36 PM, Bill Campbell wrote: > On Tue, Apr 14, 2009, Emad Nawfal (???? ????) wrote: > > > > Hi tutors, > > How can I sort the following list in a way that takes care of the > > right order of numbers? The sorted function compares strings here as > > far as I can see, but I want to have filepath2 follow filepath1. Your > > help is appreciated. > > There is at least one example of a method of dealing with this in > the O'Reilly ``Python cookbook''. > > > >>> myList > > ['filepath54', 'filepath25', 'filepath49', 'filepath0', 'filepath89', > > 'filepath52', 'filepath37', 'filepath32', 'filepath2', 'filepath15', > > 'filepath88', 'filepath70', 'filepath33', 'filepath58', 'filepath28', > > 'filepath61', 'filepath23', 'filepath64', 'filepath72', 'filepath11', > > 'filepath63', 'filepath76', 'filepath87', 'filepath45', 'filepath41', > > 'filepath68', 'filepath81', 'filepath75', 'filepath62', 'filepath46', > > 'filepath36', 'filepath39', 'filepath5', 'filepath34', 'filepath8', > > 'filepath67', 'filepath14', 'filepath82', 'filepath29', 'filepath47', > > 'filepath79', 'filepath13', 'filepath16', 'filepath71', 'filepath27', > > 'filepath100', 'filepath20', 'filepath92', 'filepath57', 'filepath73', > > 'filepath40', 'filepath44', 'filepath83', 'filepath50', 'filepath66', > > 'filepath94', 'filepath86', 'filepath22', 'filepath17', 'filepath84', > > 'filepath38', 'filepath12', 'filepath53', 'filepath6', 'filepath48', > > 'filepath60', 'filepath51', 'filepath90', 'filepath4', 'filepath78', > > 'filepath69', 'filepath35', 'filepath97', 'filepath93', 'filepath24', > > 'filepath26', 'filepath1', 'filepath3', 'filepath96', 'filepath77', > > 'filepath98', 'filepath18', 'filepath43', 'filepath19', 'filepath56', > > 'filepath65', 'filepath95', 'filepath55', 'filepath7', 'filepath99', > > 'filepath91', 'filepath85', 'filepath9', 'filepath59', 'filepath10', > > 'filepath30', 'filepath31', 'filepath80', 'filepath42', 'filepath74', > > 'filepath21'] > > >>> sorted(myList) > > ['filepath0', 'filepath1', 'filepath10', 'filepath100', 'filepath11', > > 'filepath12', 'filepath13', 'filepath14', 'filepath15', 'filepath16', > > 'filepath17', 'filepath18', 'filepath19', 'filepath2', 'filepath20', > > 'filepath21', 'filepath22', 'filepath23', 'filepath24', 'filepath25', > > 'filepath26', 'filepath27', 'filepath28', 'filepath29', 'filepath3', > > 'filepath30', 'filepath31', 'filepath32', 'filepath33', 'filepath34', > > 'filepath35', 'filepath36', 'filepath37', 'filepath38', 'filepath39', > > 'filepath4', 'filepath40', 'filepath41', 'filepath42', 'filepath43', > > 'filepath44', 'filepath45', 'filepath46', 'filepath47', 'filepath48', > > 'filepath49', 'filepath5', 'filepath50', 'filepath51', 'filepath52', > > 'filepath53', 'filepath54', 'filepath55', 'filepath56', 'filepath57', > > 'filepath58', 'filepath59', 'filepath6', 'filepath60', 'filepath61', > > 'filepath62', 'filepath63', 'filepath64', 'filepath65', 'filepath66', > > 'filepath67', 'filepath68', 'filepath69', 'filepath7', 'filepath70', > > 'filepath71', 'filepath72', 'filepath73', 'filepath74', 'filepath75', > > 'filepath76', 'filepath77', 'filepath78', 'filepath79', 'filepath8', > > 'filepath80', 'filepath81', 'filepath82', 'filepath83', 'filepath84', > > 'filepath85', 'filepath86', 'filepath87', 'filepath88', 'filepath89', > > 'filepath9', 'filepath90', 'filepath91', 'filepath92', 'filepath93', > > 'filepath94', 'filepath95', 'filepath96', 'filepath97', 'filepath98', > > 'filepath99'] > > >>> ``` > > -- > > ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? > > ????????.....???? ??????? > > "No victim has ever been more repressed and alienated than the truth" > > Emad Soliman Nawfal > > Indiana University, Bloomington > > -------------------------------------------------------- > > >_______________________________________________ > >Tutor maillist - Tutor at python.org > >http://mail.python.org/mailman/listinfo/tutor > > > -- > Bill > -- > INTERNET: bill at celestial.com Bill Campbell; Celestial Software LLC > URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way > Voice: (206) 236-1676 Mercer Island, WA 98040-0820 > Fax: (206) 232-9186 Skype: jwccsllc (206) 855-5792 > > An almost hysterical antagonism toward the gold standard is one issue which > unites statists of all persuasions. They seem to sense that gold and > economic freedom are inseparable. -- Alan Greenspan > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > Thank you Kent and Bill. This is much easier than itemgetter. -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From srilyk at gmail.com Wed Apr 15 13:44:02 2009 From: srilyk at gmail.com (W W) Date: Wed, 15 Apr 2009 06:44:02 -0500 Subject: [Tutor] Looking for starter projects In-Reply-To: <3279d5360904150150wd52e084i7d9b6fd7b2480e34@mail.gmail.com> References: <3279d5360904150150wd52e084i7d9b6fd7b2480e34@mail.gmail.com> Message-ID: <333efb450904150444p642a2910q4ce7b70c5d5c3675@mail.gmail.com> On Wed, Apr 15, 2009 at 3:50 AM, Evert Edel wrote: > Hi all, > > Now since I've got the learning python book I first did a quick read trough > it and now I'm going more slowly trough it and doing all the explained > things (in the interactive prompt). I do understand the basics behind OOP > and I'm wanting to start some smaller projects using the book as a reference > to get my hands on python. It seems to me that it's better to learn it with > smaller projects (and evolving with those). That's probably a fairly good idea... I would guess that most of us learned the same way, or at least in similar ways. > > But I really don't know what smaller projects would be good to start on as > beginner. I've got some ideas but those are more advanced (making a mpd > client, some website ideas). > > I hope you can give me some starting projects to learn python :). > Well, what do you enjoy? That's really probably the best way to program - when you program things you enjoy you tend to be more likely to stick with it. Do you like games? Maybe you could program a tic-tac-toe game. Or a guessing game. Do you like math? Perhaps you could write programs that will do certain math functions for you (Like the Fibonacci series, computing area, maybe even doing some calculus). Do you like drawing pictures? If you use Tkinter or the turtle module you could write some programs to do that. Maybe you could try writing your name, first in block, then in script (actually the script would end out rather complicated, but certainly fun[if that's what you enjoy]!) And of course, if you get stuck anywhere and need some help, we're always willing to help point you in the right direction. Good luck! HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From srilyk at gmail.com Wed Apr 15 13:47:35 2009 From: srilyk at gmail.com (W W) Date: Wed, 15 Apr 2009 06:47:35 -0500 Subject: [Tutor] python books In-Reply-To: <49E5ACA3.1030804@sbcglobal.net> References: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> <49E5ACA3.1030804@sbcglobal.net> Message-ID: <333efb450904150447s7db00478i42d94f3d920ec7ad@mail.gmail.com> On Wed, Apr 15, 2009 at 4:45 AM, Wayne Watson wrote: > It's unlikely you are going to find a pdf on Python that's suitable for > beginners. Do you mean pdf or a digital book? There are Python books in > digital form on the web. I'm not quite sure how it works, but I know of at > least one public library has them. I think it works that if you have a > library card, then you can get web access to read the book on-line. I think > this operates at a college level too. > Snake Wrangling for Kids is a perfectly good pdf for beginners (both to programming and python). I'd say the same about Think like a computer scientist - http://www.greenteapress.com/thinkpython/thinkpython.html But maybe that's just me :P -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From gg.labs10 at gmail.com Wed Apr 15 16:40:31 2009 From: gg.labs10 at gmail.com (Giorgio Bonfiglio) Date: Wed, 15 Apr 2009 16:40:31 +0200 Subject: [Tutor] Import Modules Message-ID: <434e04240904150740w29defbb8i53dcda40b0e12ade@mail.gmail.com> Hi, hope this mailing list is still active. I'm learning phyton. I can write simple programs, and i've studied all examples provided by the Google App Engine Documentation. As i understood, i can import a module using something like: import modulename Or, import a specific function, using: from modulename import functionname Ok, now let's look at the first example provided by google: http://code.google.com/intl/it-IT/appengine/docs/python/gettingstarted/usingwebapp.html I can see those lines: from google.appengine.ext import webapp from google.appengine.ext.webapp.util import run_wsgi_app The first line imports the webapp subpackage (that is not actually a module, it's a subdirectory with some .py files into). So why do i need to import the specific function in the second line? Does'nt it get imported in the first? Thankyou for your help. -------------- next part -------------- An HTML attachment was scrubbed... URL: From RWeidner at ea.com Wed Apr 15 17:29:30 2009 From: RWeidner at ea.com (Weidner, Ronald) Date: Wed, 15 Apr 2009 08:29:30 -0700 Subject: [Tutor] Here's something to talk about Message-ID: # This code is posted for the purpose of conversation. If it is of some # value to someone that would be great. But what I hope is that the code # sparks conversations about what I did in this code and why I did it. Since # the list seems thick with OOP questions at the moment, I thought this might # be relevant. Digest and enjoy. class Item ( object ): def __init__( self ): self._FullName = '' self._Recovery = 0 self._Exporter = SimpleItemExporter (); def SetFullName( self, name ): self._FullName = name def SetRecovery( self, value ): self._Recovery = value def SetExporter( self, exporter ): self._Exporter = exporter def Export( self ): properties = { 'FullName':self._FullName, 'Recovery':self._Recovery } self._Exporter.Export( properties ) class ItemCollection( object ): def __init__( self ): self._ItemList = [] self._CollectionIndex = -1 self._Loader = None def Add ( self, item ): self._ItemList.append(item) def MoveFirst ( self ): self._CollectionIndex = -1 def MoveNext ( self ): self._CollectionIndex += 1 if self._CollectionIndex < ( len ( self._ItemList ) ): return self._ItemList[ self._CollectionIndex ] return None def MoveTo ( self, index ): pass def GetLength ( self ): pass def SetCollectionLoader ( self, loader ): self._Loader = loader def Load ( self ): if self._Loader <> None: self._Loader.LoadCollection(self) class SimpleItemExporter ( object ): def __init__( self ): pass def Export ( self, dictionary ): print "The recovery status of " +str(dictionary['FullName'])+ " is: " + str(dictionary['Recovery']) class XMLExporter ( object ): def __init__ ( self, tag='XML' ): self.SetRootTag( tag ) def SetRootTag ( self, tag ): self._RootTag = tag; def Export ( self, dictionary ): xml = "" keys = dictionary.keys() for key in keys: xml = xml + "\n\t<" + key + ">" + str(dictionary[key]) + "" print "<" + self._RootTag + ">" + xml + "\n" class SQLExporter ( object ): def __init__ ( self, tableName): self._TableName = tableName def Export ( self, dictionary ): value = "" field = "" first = 1 keys = dictionary.keys() for key in keys: if first <> 1: field = field + ", " value = value + ", " field = field + " " + key value = value + "'" + str(dictionary[key]) + "'" first = 0 print "INSERT into " + self._TableName + " (" + field + ") values (" + value + ")" class ItemCollectionLoader ( object ): def LoadCollection(self, collection): for i in range ( 1, 5 ): item = Item() item.SetFullName( 'Ron The Dude' ) item.SetRecovery( i ) collection.Add( item ) more = 1 collection = ItemCollection() # here we create a loader obbject loader = ItemCollectionLoader() # this loader object could do other things instead of the contrived example # shown here. Such as the loader object could read from a file, or a network # resource, stdin, whatever. As long as it's LoadCollection interface doesn't # change the object could be many things. I've decided to illustrate this concept # later in the code with the exporter object. collection.SetCollectionLoader(loader) collection.Load() collection.MoveFirst while more <> None: item = collection.MoveNext () if item <> None: # What's important here to note is that the exporter may have different # constructors but, the Export method takes the same arguments in each # export class. In other words, it's interface is the same. So, we # can easily switch one exporter object for another. With a little more # code you could even export these items in more than one format (if needed). #exporter = SimpleItemExporter() #exporter = XMLExporter("item") exporter = SQLExporter("itemTable") item.SetExporter(exporter) item.Export () else: more = None From inthefridge at gmail.com Wed Apr 15 17:42:44 2009 From: inthefridge at gmail.com (Spencer Parker) Date: Wed, 15 Apr 2009 09:42:44 -0600 Subject: [Tutor] regular expression problem Message-ID: I have a python script that takes a text file as an argument. It then loops through the text file pulling out specific lines of text that I want. I have a regular expression that evaluates the text to see if it matches a specific phrase. Right now I have it writing to another text file that output. The problem I am having is that it finds the phrase prints it, but then it continuously prints the statement. There is only 1 entries in the file for the result it finds, but it prints it multiple times...several hundred before it moves onto the next one. But it appends the first one to the next entry...and does this till it finds everything. http://dpaste.com/33982/ Any Help? -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Wed Apr 15 18:29:12 2009 From: bgailer at gmail.com (bob gailer) Date: Wed, 15 Apr 2009 12:29:12 -0400 Subject: [Tutor] regular expression problem In-Reply-To: References: Message-ID: <49E60B58.2060103@gmail.com> Spencer Parker wrote: > I have a python script that takes a text file as an argument. It then loops > through the text file pulling out specific lines of text that I want. I > have a regular expression that evaluates the text to see if it matches a > > specific phrase. Right now I have it writing to another text file that > output. The problem I am having is that it finds the phrase prints it, but > then it continuously prints the statement. There is only 1 entries in the > > file for the result it finds, but it prints it multiple times...several > hundred before it moves onto the next one. But it appends the first one to > the next entry...and does this till it finds everything. > > http://dpaste.com/33982/ > > > Any Help? > dedent the 2nd for loop. -- Bob Gailer Chapel Hill NC 919-636-4239 From inthefridge at gmail.com Wed Apr 15 18:59:34 2009 From: inthefridge at gmail.com (Spencer Parker) Date: Wed, 15 Apr 2009 10:59:34 -0600 Subject: [Tutor] regular expression problem In-Reply-To: <49E60B58.2060103@gmail.com> References: <49E60B58.2060103@gmail.com> Message-ID: After he said that...I realized where I was being dumb... On Wed, Apr 15, 2009 at 10:29 AM, bob gailer wrote: > Spencer Parker wrote: > >> I have a python script that takes a text file as an argument. It then >> loops >> through the text file pulling out specific lines of text that I want. I >> have a regular expression that evaluates the text to see if it matches a >> >> specific phrase. Right now I have it writing to another text file that >> output. The problem I am having is that it finds the phrase prints it, >> but >> then it continuously prints the statement. There is only 1 entries in the >> >> file for the result it finds, but it prints it multiple times...several >> hundred before it moves onto the next one. But it appends the first one >> to >> the next entry...and does this till it finds everything. >> >> http://dpaste.com/33982/ >> >> >> Any Help? >> >> > > dedent the 2nd for loop. > > -- > Bob Gailer > Chapel Hill NC > 919-636-4239 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jecarnell at saintfrancis.com Wed Apr 15 19:27:16 2009 From: jecarnell at saintfrancis.com (Carnell, James E) Date: Wed, 15 Apr 2009 12:27:16 -0500 Subject: [Tutor] Here's something to talk about (Weidner, Ronald) In-Reply-To: Message-ID: Since # the list seems thick with OOP questions at the moment, I thought this might # be relevant. Digest and enjoy. class Item ( object ): def __init__( self ): self._FullName = '' self._Recovery = 0 self._Exporter = SimpleItemExporter (); # <----? Don't understand Bummer, I was hoping to consider myself at the tip of intermediate python programming ... This is the first time I have ever seen a variable set to what appears to be a function address(?). Since I am at work I can't copy paste this thing yet. Is SimpleItemExporter from the parent class, object? I am assuming Item extends or inherits (or whatever the right "word" is) from object. From RWeidner at ea.com Wed Apr 15 19:51:18 2009 From: RWeidner at ea.com (Weidner, Ronald) Date: Wed, 15 Apr 2009 10:51:18 -0700 Subject: [Tutor] Here's something to talk about (Weidner, Ronald) In-Reply-To: References: Message-ID: class Item ( object ): def __init__( self ): self._FullName = '' self._Recovery = 0 self._Exporter = SimpleItemExporter (); # <----? Don't understand Bummer, I was hoping to consider myself at the tip of intermediate python programming ... This is the first time I have ever seen a variable set to what appears to be a function address(?). Since I am at work I can't copy paste this thing yet. Is SimpleItemExporter from the parent class, object? I am assuming Item extends or inherits (or whatever the right "word" is) from object. In the above code example, SimpleItemExporter is a class and Self._Exporter is an instance of that class. Yes, SimpleItemExporter does in fact inherit from object. Python lacks the 'new' keyword which may have made that line of code more clear but, in plain English this is what the line means... Create a new SimpleItemExporter and let me access it through the variable self._Exporter. By the way, I'm avoiding the notion of function address by implementing the concept of interfaces in these examples. Please Let us know if your questions about that line of code are answered. Once clear, we can move on to even more cool things in this code. There is a little more to the story relating to the whole self bit, but since I'm at work now too, I'll let someone else get into that for now. -- Ronald Weidner From jecarnell at saintfrancis.com Wed Apr 15 19:46:39 2009 From: jecarnell at saintfrancis.com (Carnell, James E) Date: Wed, 15 Apr 2009 12:46:39 -0500 Subject: [Tutor] Here's something to talk about (Weidner, Ronald) In-Reply-To: Message-ID: """But what I hope is that the code # sparks conversations about what I did in this code and why I did it.""" If anyone answers me thank you. really. Nevertheless, I am personally not pursuing to understand this code. Ronald Weidner left on vacation and won't be back until the 19th (I don't know French though). Maybe we should wait until then... ========================================== Bonjour, Je suis absente jusqu'au 19/04/09 inclus. Cordialement. Genevi?ve =========================================== From srilyk at gmail.com Wed Apr 15 20:14:55 2009 From: srilyk at gmail.com (W W) Date: Wed, 15 Apr 2009 13:14:55 -0500 Subject: [Tutor] Here's something to talk about (Weidner, Ronald) In-Reply-To: References: Message-ID: <333efb450904151114h404dfabfvf6f52dd7cdf1d83c@mail.gmail.com> On Wed, Apr 15, 2009 at 12:27 PM, Carnell, James E < jecarnell at saintfrancis.com> wrote: > Since # the list seems thick with OOP questions at the moment, I thought > this might # be relevant. Digest and enjoy. > > class Item ( object ): > > def __init__( self ): > self._FullName = '' > self._Recovery = 0 > self._Exporter = SimpleItemExporter (); # <----? Don't > understand > > Bummer, I was hoping to consider myself at the tip of intermediate > python programming ... > > This is the first time I have ever seen a variable set to what appears > to be a function address(?). Since I am at work I can't copy paste this > thing yet. Is SimpleItemExporter from the parent class, object? I am > assuming Item extends or inherits (or whatever the right "word" is) from > object. First off, the semicolon is probably a syntax error. SimpleItemExporter is probably a class, but I don't think object is the parent. If you were trying to access something from object I'm fairly certain you'd use the dot operator, so it would be object.SimpleItemExporter() But if it's a class it would be a new instance of that class. Otherwise it doesn't make a lot of sense to have the parenthesis (unless SimpleItemExporter returns something useful... which it isn't named like it should). Consider the following: In [1]: def foo(): ...: print "Hello" ...: ...: In [2]: x = foo() Hello In [3]: x In [4]: x('a') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) /home/wayne/ in () TypeError: 'NoneType' object is not callable In [5]: class foo: ...: def __init__(self, spam): ...: print spam ...: ...: In [7]: x = foo('eggs') eggs In [8]: x Out[8]: <__main__.foo instance at 0x9f0304c> In [9]: x() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /home/wayne/ in () AttributeError: foo instance has no __call__ method In [10]: x.__init__('eggs') eggs --and--- In [11]: class spam: ....: def __init__(self, eggs): ....: lay(eggs) ....: def lay(x): ....: print x ....: ....: In [13]: class foo(spam): ....: def __init__(self, bar): ....: lay(bar) ....: ....: In [14]: x = foo('Ni') --------------------------------------------------------------------------- NameError Traceback (most recent call last) /home/wayne/ in () /home/wayne/ in __init__(self, bar) NameError: global name 'lay' is not defined --------------------- The above snippets seem to show what I stated before - SimpleItemExporter is a class on it's own - not a method/function in the object class. If that were the case then my class foo should have direct access to the lay function, AFAIK. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Wed Apr 15 20:34:24 2009 From: emile at fenx.com (Emile van Sebille) Date: Wed, 15 Apr 2009 11:34:24 -0700 Subject: [Tutor] Here's something to talk about (Weidner, Ronald) In-Reply-To: <333efb450904151114h404dfabfvf6f52dd7cdf1d83c@mail.gmail.com> References: <333efb450904151114h404dfabfvf6f52dd7cdf1d83c@mail.gmail.com> Message-ID: W W wrote: > On Wed, Apr 15, 2009 at 12:27 PM, Carnell, James E > > wrote: > > Since # the list seems thick with OOP questions at the moment, I thought > this might # be relevant. Digest and enjoy. > > class Item ( object ): > > def __init__( self ): > self._FullName = '' > self._Recovery = 0 > self._Exporter = SimpleItemExporter (); # <----? Don't > understand > First off, the semicolon is probably a syntax error. Redundant, yes; syntax error, no. IIRC, semi-colons are optional line terminators. ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> class Test: ... a=1; ... b=2; ... >>>=Test() >>> t.a 1 Emile From denis.spir at free.fr Wed Apr 15 20:48:30 2009 From: denis.spir at free.fr (spir) Date: Wed, 15 Apr 2009 20:48:30 +0200 Subject: [Tutor] Here's something to talk about In-Reply-To: References: Message-ID: <20090415204830.1cd0b358@o> Le Wed, 15 Apr 2009 08:29:30 -0700, "Weidner, Ronald" s'exprima ainsi: > # This code is posted for the purpose of conversation. If it is of some > # value to someone that would be great. But what I hope is that the code > # sparks conversations about what I did in this code and why I did it. > Since # the list seems thick with OOP questions at the moment, I thought > this might # be relevant. Digest and enjoy. I must confess I do not really understand your intent (a) with the code itself (b) with the fact of publishing it Maybe I have read it too fast. What I saw is an implementation of strict object interface, in the sense strictly separating the inner and outer parts of an object. Sure, this is an aspect of OO. But AFAIK this is not very pythonic, not very common at least (and I personly do not feel attracted by this coding style -- rather I consider it a useless overload). For code security reasons? You may be interested in the concepts of the so-called "Object-capability model": http://en.wikipedia.org/wiki/Object_capability_model. Denis ------ la vita e estrany From ptmcg at austin.rr.com Wed Apr 15 21:09:51 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Wed, 15 Apr 2009 14:09:51 -0500 Subject: [Tutor] Here's something to talk about In-Reply-To: References: Message-ID: 1. Python is not Java (see Philip Eby's blog entry http://dirtsimple.org/2004/12/python-is-not-java.html). Let go of your concepts that only Items can go into an ItemCollection - Python already has some perfectly good collection builtins. Instead of writing a custom ItemCollection, why not write a generic Exporter that takes any Python sequence (such as a list, set, tuple, etc., anything that supports iter)? 2. SQLExporter does not use recommended form for substituting values into an SQL statement. It is preferred that one use the 2-argument form of execute, in which the first argument contains '?' or '%s' placeholders, and the second argument is a tuple of values. That is, instead of: first, last = "Bobby", "Tables" cursor.execute("INSERT into STUDENT (firstname, lastname) values ('"+first+"','"+last+"')") Do: first, last = "Bobby", "Tables" cursor.execute("INSERT into STUDENT (firstname, lastname) values (?,?)", (first,last)) No need for wrapping in quotes, already handles values with embedded quotes, and no SQL injection jeopardy (http://xkcd.com/327/). This slightly complicates your SQL exporter, it would have to return a tuple containing the INSERT statement and the tuple of substitution values, instead of just a string to be passed to execute. 3. The Pythonic way to create a comma-separated string of values from a list of strings is: ','.join(value_list). If you have a list of tuples and want two comma-separated strings, do: keystring, valstring = (','.join(s) for s in zip(*key_value_tuple_list)) 4. While I am not a slave to PEP8, your mixed case method names with leading capitals look too much like class names and/or .Net method names. In general, I find your approach too intrusive into the objects you would like to export or load. I would prefer to see a Loader and/or Exporter class that takes my own application object, configured with table name and field names, and then creates the proper XML or SQL representation. -- Paul From wescpy at gmail.com Wed Apr 15 21:20:20 2009 From: wescpy at gmail.com (wesley chun) Date: Wed, 15 Apr 2009 12:20:20 -0700 Subject: [Tutor] Here's something to talk about In-Reply-To: References: Message-ID: <78b3a9580904151220v74e6b767vc498dae5f05dd5da@mail.gmail.com> > 1. Python is not Java although i agree with all 4 points that paul makes, this 1st one stands out the most. when i saw the code the first time, the immediate thought that came to my mind was, "This looks like Java code written with Python syntax." i thing the same functionality can be accomplished using half or a third of the total # of lines of code originally presented. > 2. SQLExporter does not use recommended form for substituting values into an SQL statement. yes, in addition to the "preferred" feeling, it will also help mitigate SQL injection attacks. this code looks like Java + JS. creating strings with few operations helps with performance too, as in... > 3. The Pythonic way to create a comma-separated string of values from a list of strings is: ','.join(value_list). > : > 4. While I am not a slave to PEP8, your mixed case method names with leading capitals look too much like class names and/or .Net method names. also agreed. i think it's a common style guideline in multiple languages i'm familiar with (other than Python) to Capitalize class names but keep variables, functions, and methods all lowered. well, the OP was right... it has indeed led to some discussion on the list! cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From RWeidner at ea.com Wed Apr 15 21:21:23 2009 From: RWeidner at ea.com (Weidner, Ronald) Date: Wed, 15 Apr 2009 12:21:23 -0700 Subject: [Tutor] Here's something to talk about In-Reply-To: <20090415204830.1cd0b358@o> References: <20090415204830.1cd0b358@o> Message-ID: I must confess I do not really understand your intent (a) with the code itself (b) with the fact of publishing it Maybe I have read it too fast. What I saw is an implementation of strict object interface, in the sense strictly separating the inner and outer parts of an object. Sure, this is an aspect of OO. But AFAIK this is not very pythonic, not very common at least (and I personly do not feel attracted by this coding style -- rather I consider it a useless overload). For code security reasons? You may be interested in the concepts of the so-called "Object-capability model": http://en.wikipedia.org/wiki/Object_capability_model. -- It's ok not to be attracted to the code or the concept. It's not for everyone (or perhaps it's not for anyone). Thanks for the link. That was an interesting read. Let me confess my intentions. I enjoy programming and I also enjoy talking about programming, and sharing ideas. That's it. The problem that coding in this style tries to solve is change and evolution. During the evolution of software many things can change. Including the things we sometimes take for granted when we start a project. Like the input and output of the program. This example shows how cobbling a few objects together can produce some very different results with very little logic change. There are better more generic ways to solve these problems. Probably even more pythonic ways to solve them. I hope to discuss them in this thread. -- Ronald Weidner From denis.spir at free.fr Wed Apr 15 21:41:28 2009 From: denis.spir at free.fr (spir) Date: Wed, 15 Apr 2009 21:41:28 +0200 Subject: [Tutor] Here's something to talk about In-Reply-To: <78b3a9580904151220v74e6b767vc498dae5f05dd5da@mail.gmail.com> References: <78b3a9580904151220v74e6b767vc498dae5f05dd5da@mail.gmail.com> Message-ID: <20090415214128.2099e253@o> Le Wed, 15 Apr 2009 12:20:20 -0700, wesley chun s'exprima ainsi: > i think it's a common style guideline in multiple > languages i'm familiar with (other than Python) to Capitalize class > names but keep variables, functions, and methods all lowered. In most cases I know, class names are capitalized, while func and method names are camel-cased: ThisKlass thisFunc Denis ------ la vita e estrany From wescpy at gmail.com Wed Apr 15 21:55:47 2009 From: wescpy at gmail.com (wesley chun) Date: Wed, 15 Apr 2009 12:55:47 -0700 Subject: [Tutor] Here's something to talk about In-Reply-To: <20090415214128.2099e253@o> References: <78b3a9580904151220v74e6b767vc498dae5f05dd5da@mail.gmail.com> <20090415214128.2099e253@o> Message-ID: <78b3a9580904151255p30e6aafbra7d590cf0fd1fa8c@mail.gmail.com> >> i think it's a common style guideline in multiple >> languages i'm familiar with (other than Python) to Capitalize class >> names but keep variables, functions, and methods all lowered. > > In most cases I know, class names are capitalized, while func and method names are camel-cased: > ? ThisKlass this is indeed called "CamelCased". > ? thisFunc this is called "mixedCase". an all-lowered alternative is "lower_case_with_underscores," as in 'this_func'. PEP8 seems to lean towards the latter over the former, altho i use both and also do not stay strict to it. here's the link if you're curious: http://www.python.org/dev/peps/pep-0008/ cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From alan.gauld at btinternet.com Thu Apr 16 00:50:34 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 15 Apr 2009 23:50:34 +0100 Subject: [Tutor] colours in IDLE References: <275664.30512.qm@web110603.mail.gq1.yahoo.com> Message-ID: "mbikinyi brat" wrote > When you type a code in IDLE-Python, they appear in different colours. > For instance: > factorial in blue and return and else and result in red. > Any significance to these colours? Yes, they indicate different syntactic features. You can change them and see what they are for by looking at the configuration screens, the Highlighting tab. There is a drop down list that will show you what can be coloured. This is a useful tool since it can show an unterminated string (ie where you forgot the closing quote), or where you accidentally use a keyword as a variable name etc -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From RWeidner at ea.com Thu Apr 16 00:56:08 2009 From: RWeidner at ea.com (Weidner, Ronald) Date: Wed, 15 Apr 2009 15:56:08 -0700 Subject: [Tutor] Here's something to talk about In-Reply-To: References: Message-ID: 1. Python is not Java (see Philip Eby's blog entry http://dirtsimple.org/2004/12/python-is-not-java.html). Let go of your concepts that only Items can go into an ItemCollection - Python already has some perfectly good collection builtins. Instead of writing a custom ItemCollection, why not write a generic Exporter that takes any Python sequence (such as a list, set, tuple, etc., anything that supports iter)? 2. SQLExporter does not use recommended form for substituting values into an SQL statement. It is preferred that one use the 2-argument form of execute, in which the first argument contains '?' or '%s' placeholders, and the second argument is a tuple of values. That is, instead of: first, last = "Bobby", "Tables" cursor.execute("INSERT into STUDENT (firstname, lastname) values ('"+first+"','"+last+"')") Do: first, last = "Bobby", "Tables" cursor.execute("INSERT into STUDENT (firstname, lastname) values (?,?)", (first,last)) No need for wrapping in quotes, already handles values with embedded quotes, and no SQL injection jeopardy (http://xkcd.com/327/). This slightly complicates your SQL exporter, it would have to return a tuple containing the INSERT statement and the tuple of substitution values, instead of just a string to be passed to execute. 3. The Pythonic way to create a comma-separated string of values from a list of strings is: ','.join(value_list). If you have a list of tuples and want two comma-separated strings, do: keystring, valstring = (','.join(s) for s in zip(*key_value_tuple_list)) 4. While I am not a slave to PEP8, your mixed case method names with leading capitals look too much like class names and/or .Net method names. In general, I find your approach too intrusive into the objects you would like to export or load. I would prefer to see a Loader and/or Exporter class that takes my own application object, configured with table name and field names, and then creates the proper XML or SQL representation. -- Paul In the future, I'll try not to have a Java accent when speaking Python. ( No guarantee though :) ) That said, thanks for the syntax tips and Python preferred snips. One of your points represents a great opportunity to make mine. Suppose this code is several years old. Now we have a new requirement that states we need to work with the data as you described above. How much of the existing code would you have to change to make that change happen? The answer is exactly one line. And here is the line... exporter = SQLExporter("itemTable") And you would change the line to something like... exporter = SQLTupleExporter("itemTable") or perhaps... exporter = ExportItemToDatabase('127.0.0.1', 'user', 'pass', 'schema') Of course, you would need to define a class to go along with your change but all of that will be NEW code. Not a change to existing code. So, all of your existing code should still be valid and tested. The only thing you'll need to do validate and test your new code. There are no tentacles. Replacing exporter with a different kind of exporter does not invalidate anything you've done in your code before. Awesome. Maybe you need to do both? Maybe you need to export the old way, and export the new way. How difficult would that be with 2 export objects? In this case, you wouldn't need to change any lines of code, but rather you would add 2 more lines to the main logic and implement the needed class. It's really just a strategy for creating maintainable code. Maybe It's a good strategy, maybe it isn't. I guess it would depend on the project. I find the technique interesting and useful. -- Ronald Weidner From alan.gauld at btinternet.com Thu Apr 16 00:57:41 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 15 Apr 2009 23:57:41 +0100 Subject: [Tutor] Import Modules References: <434e04240904150740w29defbb8i53dcda40b0e12ade@mail.gmail.com> Message-ID: "Giorgio Bonfiglio" wrote > from google.appengine.ext import webapp > from google.appengine.ext.webapp.util import run_wsgi_app > > The first line imports the webapp subpackage (that is not actually a > module, > it's a subdirectory with some .py files into). So why do i need to import > the specific function in the second line? Does'nt it get imported in the > first? The webapp name will get imported but to reach the function you would need to use webapp.util.run_wsgi_app Somebody decided that was too close to hard work so they imported that specific function. When you import you import names, in the first case webapp. Where the name is a package (ie a folder) that gives you access to the modules (or sub packages) contained in that folder but not to the contents of those items directly, hence the need for webapp.util. You could just use the long name each time but the direct import makes for shorter coding, especially if you use the function a lot. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Apr 16 01:05:41 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Apr 2009 00:05:41 +0100 Subject: [Tutor] Here's something to talk about References: Message-ID: "Weidner, Ronald" wrote ># This code is posted for the purpose of conversation. If it is of some > # value to someone that would be great. But what I hope is that the code > # sparks conversations about what I did in this code and why I did it. > Since > # the list seems thick with OOP questions at the moment, I thought this > might > # be relevant. Digest and enjoy. Some fairly quick comments based on an initial scan: > > class Item ( object ): Its good to put some documentation strings describing the purpose of the class, same for the methods. > def __init__( self ): > self._FullName = '' > self._Recovery = 0 > self._Exporter = SimpleItemExporter (); Why not make these default values of parameters? That then gives the clients the chance to initialise the values at instantiation time. > class SimpleItemExporter ( object ): > > def __init__( self ): > pass This does nothing, you do not need an __init__ if its not used. > class XMLExporter ( object ): > def __init__ ( self, tag='XML' ): > self.SetRootTag( tag ) Nothing wrong with doing direct assignment inside a method. > class SQLExporter ( object ): > > def __init__ ( self, tableName): > self._TableName = tableName Which you do here! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Apr 16 01:11:42 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Apr 2009 00:11:42 +0100 Subject: [Tutor] Here's something to talk about (Weidner, Ronald) References: <333efb450904151114h404dfabfvf6f52dd7cdf1d83c@mail.gmail.com> Message-ID: "Emile van Sebille" wrote > Redundant, yes; syntax error, no. IIRC, semi-colons are optional line > terminators. statement terminators I think. ie you can have several statements on a line by separating with semicolons: >>> x=5; print x+2 7 Alan G From ptmcg at austin.rr.com Thu Apr 16 02:35:27 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Wed, 15 Apr 2009 19:35:27 -0500 Subject: [Tutor] Here's something to talk about In-Reply-To: References: Message-ID: Ronald - I really encourage you to try to embrace some of the basic Python idioms as part of your Java->Python journey: 1. Iterators for item in list_of_items: # do something with item Is all that is needed to visit each item in a Python list. Your verbose MoveFirst, MoveNext, if more <> None, etc. is completely subsumed into the built-in machinery Python has for iterators. (4-space source indent is also the norm.) 2. Static typing Change that snippet to: def visit_each_item(collection): for item in collection: # do something with item, like printing its name And this code works for lists, tuples, and sets of items, or dicts with items as keys (assuming an item is hashable). (The old method would accept non-lists, too, but naming a parameter 'list_of_items' sounds like only lists are intended.) And the items in the list/tuple/set/whatever don't even have to be all the same type, as long as they all support the concept of "doing something". Such as: def print_each_items_name(collection): for item in collection: print item.name The Java approach is to define an interface called Nameable (or INameable?), with methods GetName and SetName. You want to talk about self-maintaining code? I wrote code like this before sets were even added to the language definition, and when this new data type came along, my code library handled it without changing a single line! Good thing that I didn't try to hack in some hare-brained run-time type safety like: if isinstance(collection,(list,tuple,dict)): for item in collection: ... blah blah... Or I would have to chase down all such code and add 'set' to the list. 3. Interfaces Interfaces are there to allow compilers to verify that a static data type will comply with an interface's requirements at runtime. Python skips this and just does the runtime check - if the object does not have the necessary attribute (and methods are just callable attributes), then an AttributeError gets thrown. If you have a design that calls for inheritance from a pure virtual abstract base class, then in Python, that base class is completely unnecessary. Sometimes in my code I will still include such a base class, for purposes of documentation or to help me remember later what the heck was I thinking? But such a thing is for my benefit, not Python's. 4. Getters and Setters This is such a fundamental dogma for Java developers that it may be difficult for you to let go. But do so, please! Assume that rootTag in your XMLExporter is a public variable, which you later must change to a method call (maybe to ensure that the given tag is actually a kosher XML tag string, and not something like "123$#!@^^!!!") - this is where the Python property allows you to map set and get access to an attribute through a pair of methods, while external code continues to access the property by its public name, "rootTag". No additional refactoring or recoding or recompiling necessary. Meanwhile, you can trim significant fat of "just in case I need to make this a method someday" code from your classes, and just use the attributes directly. 5. New concepts in Python Python has some exciting features related to its built-in support for collections that makes creating and working with them a dream. Read up on list comprehensions, generator expressions, iterators, and generators. You won't need them right away, but the sooner you get them, the sooner you'll be able to dump verbose (and slow!) code like: return_list = [] for blah in somecollection: return_list.append(something_about(blah)) and write: return_list = [something_about(blah) for blah in somecollection] Not only is this more compact and concise, it also runs faster. Also, learn about zip and itertools - these will make your programs just fly. Please try to hold off on the Newbie's Hubris. The code that you posted doesn't really present anything novel, or particularly awesome. Instead, think about this: class ApplicationClass(object): # objects of this class have no attributes - yet! pass def makeExportable(obj, exporter): obj.exporter = exporter a = ApplicationClass() makeExportable(a, SQLExporter) Now I have added exportability to an instance, without changing the class at all. This might even be a class from a 3rd party lib, whose source code I don't even have! Welcome to Python, but check your Java baggage at the door! -- Paul From kent37 at tds.net Thu Apr 16 03:37:34 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 15 Apr 2009 21:37:34 -0400 Subject: [Tutor] Import Modules In-Reply-To: References: <434e04240904150740w29defbb8i53dcda40b0e12ade@mail.gmail.com> Message-ID: <1c2a2c590904151837m17d2a689t69c0ac392f757870@mail.gmail.com> On Wed, Apr 15, 2009 at 6:57 PM, Alan Gauld wrote: > When you import you import names, in the first case webapp. > Where the name is a package (ie a folder) that gives you access > to the modules (or sub packages) contained in that folder but > not to the contents of those items directly, hence the need for > webapp.util. In general, importing a package does not give access to members of a sub-package. You have to explicitly import the subpackage. For example, In [1]: import xml In [2]: xml.dom.Node --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) /Users/kent/ in () AttributeError: 'module' object has no attribute 'dom' In [3]: import xml.dom In [4]: xml.dom.Node Out[4]: There are some exceptions, notably os.path: In [5]: import os In [6]: os.path Out[6]: but that is a special case and requires special coding in the os module. Kent From alan.gauld at btinternet.com Thu Apr 16 09:21:37 2009 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Thu, 16 Apr 2009 07:21:37 +0000 (GMT) Subject: [Tutor] Import Modules In-Reply-To: <1c2a2c590904151837m17d2a689t69c0ac392f757870@mail.gmail.com> References: <434e04240904150740w29defbb8i53dcda40b0e12ade@mail.gmail.com> <1c2a2c590904151837m17d2a689t69c0ac392f757870@mail.gmail.com> Message-ID: <23364.82674.qm@web86708.mail.ird.yahoo.com> > In general, importing a package does not give access to members of a > sub-package. You have to explicitly import the subpackage. For > example, > > In [1]: import xml > In [2]: xml.dom.Node > --------------------------------------------------------------------------- > AttributeError Traceback (most recent call last) > AttributeError: 'module' object has no attribute 'dom' > There are some exceptions, notably os.path: > In [5]: import os > In [6]: os.path > but that is a special case and requires special coding in the os module. Interestingly I added the comment about sub packages specifically because I remembered os.path and assumed it was the norm! :-) I really must read up on packages. I will need to for my v3 rewrite anyway! Alan G. From mbikinyi_brat at yahoo.com Thu Apr 16 11:04:25 2009 From: mbikinyi_brat at yahoo.com (mbikinyi brat) Date: Thu, 16 Apr 2009 02:04:25 -0700 (PDT) Subject: [Tutor] importance of Docstring Message-ID: <962889.95602.qm@web110601.mail.gq1.yahoo.com> Dear ALL, What is really the importance of Docstring in Python??? Regards, Henry -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Thu Apr 16 11:39:13 2009 From: denis.spir at free.fr (spir) Date: Thu, 16 Apr 2009 11:39:13 +0200 Subject: [Tutor] importance of Docstring In-Reply-To: <962889.95602.qm@web110601.mail.gq1.yahoo.com> References: <962889.95602.qm@web110601.mail.gq1.yahoo.com> Message-ID: <20090416113913.5c03822b@o> Le Thu, 16 Apr 2009 02:04:25 -0700 (PDT), mbikinyi brat s'exprima ainsi: > Dear ALL, > What is really the importance of Docstring in Python??? > Regards, > Henry Very very very great, I guess ;-) Denis ------ la vita e estrany From a.t.hofkamp at tue.nl Thu Apr 16 12:02:21 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Thu, 16 Apr 2009 12:02:21 +0200 Subject: [Tutor] importance of Docstring In-Reply-To: <962889.95602.qm@web110601.mail.gq1.yahoo.com> References: <962889.95602.qm@web110601.mail.gq1.yahoo.com> Message-ID: <49E7022D.6060802@tue.nl> mbikinyi brat wrote: > Dear ALL, > What is really the importance of Docstring in Python??? > Regards, > Henry The same as comments, except at function, class, and module level. In addition, Python provides hooks for extracting that information, and eg put it in a document such as the standardlib documentation. Doc-strings are a means to document your design at the 'what' level rather than the lower 'how' level of code. A short exercise: def get_sum(val_lst): total = 0 for val in val_lst: total = total + val return total Now what does this function do? This is a 'what' question, namely, from a design point of view, if I call this function, what does it do, and what is its result-value? You have got the "how", namely you can read the code, and imagine what steps are taken to compute the return value. You have to do several deduction steps from the code to answer the question. 1. "total" is returned, so that variable represents the return-value of the function. 2. total is initialized to 0 3. total is iteratively incremented with each value from the val_lst 4. the for-loop as a whole thus computes the sum of val_lst. 5. that value is returned as function return value. 6. val_lst is not modified 7. its data values are not modified either. So the answer to the question "what does this function do?" is that it implements a sum function over a list (or even, an iterable). Also, 'total' represents the sum of the values summed so far. Experienced programmers see this in a fraction of a second for functions like above. However, if you have a 200-line function with 30 variables (which nobody here has of course, you all follow the PEP 8 guide lines :) ) and a complex flow of control between statements, the above deduction steps cost time and effort. Also, there is the chance that you make a mistake in your reasoning, or that the original author of the code made a mistake and your reasoning breaks down. Eg total = 0 def get_sum(val_lst): for val in val_lst: total = total + val return total Python has no problems whatsoever with this function. However, if you just read the function, it is not clear what the intention is. You'll have to find out how the global var 'total' is used to understand what the function does. The above reasoning and/or deduction problems originate from the fact that you are trying to reason towards the higher "what" level of the function from the low "how" level. Comments and doc-string are helpful to eliminate the reasoning effort: def get_sum(val_lst): """ Add all values from the 'val_lst' iterable together and return the sum. """ total = 0 # Sum of values added so far. # Sum all values of the list. for val in val_lst: total = total + val return total Now I have added some comment stating what the function does, what the meaning of the 'total' variable is, and what the purpose of the loop is. Effectively, I have stated the 'what' reasoning results that we have deduced previously from the code. Adding comments like this saves you the effort to do the same reasoning exercise I did before writing the code, and it eliminates some possible mistakes in your deduction reasoning. In addition, it clearly states the intended function (the 'what') of the function as a whole, the variable and the parts of the code, making it easier for you to verify whether the code does the right thing at each point, thus making code-reviewing and bug-finding easier. Now, tell me, what is easier? 1. Reading the whole code, doing the entire deduction process to discover what a function does, or 2. Read a single line in a doc-string or comment telling you exactly what you want to know. Note that unlike popular belief, in my opinion, code should be written in such a way that it is easy to read. Code is written only once, and in general read about 3 times, I seem to remember (people have been researching this). Code is much like any other document, the fact that a computer can also interpret it, should mostly be considered a lucky coindence. Sincerely, Albert From kent37 at tds.net Thu Apr 16 12:37:13 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 16 Apr 2009 06:37:13 -0400 Subject: [Tutor] Import Modules In-Reply-To: <23364.82674.qm@web86708.mail.ird.yahoo.com> References: <434e04240904150740w29defbb8i53dcda40b0e12ade@mail.gmail.com> <1c2a2c590904151837m17d2a689t69c0ac392f757870@mail.gmail.com> <23364.82674.qm@web86708.mail.ird.yahoo.com> Message-ID: <1c2a2c590904160337k78045c24rbee00bef405b378@mail.gmail.com> On Thu, Apr 16, 2009 at 3:21 AM, ALAN GAULD wrote: > > >> In general, importing a package does not give access to members of a >> sub-package. > > Interestingly I added the comment about sub packages specifically > because I remembered os.path and assumed it was the norm! :-) Yes, it took me a long time to realize that it is not. Strictly speaking I don't think os is even a package, it is a module that imports another module as it's path attribute. There is no os directory or os/__init__.py or os/path.py. Kent From denis.spir at free.fr Thu Apr 16 12:50:13 2009 From: denis.spir at free.fr (spir) Date: Thu, 16 Apr 2009 12:50:13 +0200 Subject: [Tutor] bootstrapping Message-ID: <20090416125013.3906f587@o> Hello, This question is rather intended to people who have some knowledge on parser generation and/or on self-compiling languages. I have a custom PEG parser generator in python, called 'pijnu', just bootstrapped. Meaning it's a kind of specific language which generator (compiler) is able to produce itself by parsing its own meta-grammar, written in itself. Here is a picture that tries to illustrate how this works: text.lang [langParser.py] ==> textTree [process.py] ==> ^ | +---------------------------------------------------+ | ^ lang.pijnu [pijnuParser.py] ==> langTree [generator.py] ==> ^ | +---------------------------------------------------+ | | v ^ pijnu.pijnu [pijnuParser.py] ==> pijnuTree [generator.py] ==> * level 1: use of a parser generated by pijnu to parse a user text, then process further * level 2: use of pijnu to generate a parser for a user language (or any other format) * level 3: use of pijnu to generate its own parser I'm an amateur and it's the first time I do such a thing. I thought a main advantage of bootstrapping was the ability to produce a compiler of next generation by feeding the present parser/generator with a modified grammar. Actually, this does not work for me. The reason I guess is that a feature addition or modification (semantic level) usually comes with a change in the language itself (syntactic, or rather grammatical, level). This latter change will prevent the current parser to accept the modified grammar: I need then to introduce the change in the current parser itself instead of only in the grammar. Is there anything wrong in my method and/or reflexion? [I still find it worthful anyway to recursively produce the generator by itself, not only for intellectual satisfaction, but also as way to validate the change.] Denis ------ la vita e estrany From denis.spir at free.fr Thu Apr 16 12:59:24 2009 From: denis.spir at free.fr (spir) Date: Thu, 16 Apr 2009 12:59:24 +0200 Subject: [Tutor] code publishing Message-ID: <20090416125924.1abee144@o> Hello, I have no clue whether there is a common place to publish some (free like the air) python source code for review, use, evolution... (It's about 17 5kb.) Denis ------ la vita e estrany From soulsweet01 at gmail.com Thu Apr 16 12:59:45 2009 From: soulsweet01 at gmail.com (Logan Thomas) Date: Thu, 16 Apr 2009 03:59:45 -0700 Subject: [Tutor] Building VST's with Python Message-ID: <69257c0f0904160359v4bcf2e81ibd6a3e3c4b2b5185@mail.gmail.com> I'm new to python but do have a little programming knowledge with C++....I got into programming so I could build Virtual Instruments because I love the world of sound and creating instruments that shape it...I have tried to find a program that would be an ideal vehicle for this task...I've narrowed it down to Delphi or Python....C++ would take too long for me to learn and would take to long to build an application...I was wondering if you could direct me to some tutorials or source code that might involve building virtual instruments or even games....Thanks for listening, Logan -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Thu Apr 16 13:32:15 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Thu, 16 Apr 2009 13:32:15 +0200 Subject: [Tutor] code publishing In-Reply-To: <20090416125924.1abee144@o> References: <20090416125924.1abee144@o> Message-ID: 2009/4/16 spir : > I have no clue whether there is a common place to publish some (free like the air) python source code for review, use, evolution... > (It's about 17 5kb.) Maybe http://python.pastebin.com is what you are looking for? Greets Sander From kent37 at tds.net Thu Apr 16 15:31:01 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 16 Apr 2009 09:31:01 -0400 Subject: [Tutor] code publishing In-Reply-To: <20090416125924.1abee144@o> References: <20090416125924.1abee144@o> Message-ID: <1c2a2c590904160631k290a3c1bj3472927fd51bb1cd@mail.gmail.com> On Thu, Apr 16, 2009 at 6:59 AM, spir wrote: > Hello, > > I have no clue whether there is a common place to publish some (free like the air) python source code for review, use, evolution... > (It's about 17 5kb.) If you just want a temporary place to put it for comments then pastebin is good. For open-source project hosting there are many options; google code, github and bitbucket are currently popular. For publishing, pypi is the standard. Kent From python at bdurham.com Thu Apr 16 17:01:20 2009 From: python at bdurham.com (python at bdurham.com) Date: Thu, 16 Apr 2009 11:01:20 -0400 Subject: [Tutor] importance of Docstring In-Reply-To: <49E7022D.6060802@tue.nl> References: <962889.95602.qm@web110601.mail.gq1.yahoo.com> <49E7022D.6060802@tue.nl> Message-ID: <1239894080.2939.1310821491@webmail.messagingengine.com> Albert, That was a great writeup on docstrings. I'm going to share that with my dev team. Thank you! Malcolm From inthefridge at gmail.com Thu Apr 16 19:37:35 2009 From: inthefridge at gmail.com (Spencer Parker) Date: Thu, 16 Apr 2009 11:37:35 -0600 Subject: [Tutor] print items in a list... Message-ID: I have a script that is putting elements into a list. Now instead of adding a for blah in blah statement...is there an easy way to print out the elements in a list that is similar to this? I don't have an absolute length on the size of the list either since it changes depending on what I am searching for. I have it finding the elements through a regular expression and then putting those into a list. Separate lists for each item I am searching for...if it find this put it into this list...else put into this list. Then I want to write each list to a text file. trying to clean up messy code for the most part. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mwalsh at mwalsh.org Thu Apr 16 19:42:19 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Thu, 16 Apr 2009 12:42:19 -0500 Subject: [Tutor] PyCon Videos Message-ID: <49E76DFB.2020209@mwalsh.org> Hi All, Not sure if it's common knowledge, particularly for those who didn't make it to PyCon this year, but all of the talks were recorded and will be available online in good time, thanks to Carl Karsten and his merry band of A/V volunteers. I can't even begin to grasp how much work is required to accomplish such a monumental task. I noticed this morning that the tutorials, are making their way to the blip.tv site, including "Internet Programming with Python" presented by our very own Wesley Chun. And I presume "An Introduction to Object-Oriented Programming", announced on this list a couple months ago by Michael Goldwasser, will be available soon as well. I'm really looking forward to watching both. Not a replacement for attending a PyCon in person -- which I highly recommend for anyone interested in python, no matter your skill level -- but certainly the next best thing. http://pycon.blip.tv/posts?view=archive&nsfw=dc It seems, many presenters included links to additional material (code examples, slides, etc) on the scheduled-talks page [1] -- not sure about the tutorials [2], but it doesn't look like it. [1] http://us.pycon.org/2009/conference/schedule/ [2] http://us.pycon.org/2009/tutorials/schedule/ HTH, Marty From denis.spir at free.fr Thu Apr 16 19:42:39 2009 From: denis.spir at free.fr (spir) Date: Thu, 16 Apr 2009 19:42:39 +0200 Subject: [Tutor] print items in a list... In-Reply-To: References: Message-ID: <20090416194239.5c2f0137@o> Le Thu, 16 Apr 2009 11:37:35 -0600, Spencer Parker s'exprima ainsi: > trying to clean up > messy code for the most part How does it look like? Denis ------ la vita e estrany From mbikinyi_brat at yahoo.com Thu Apr 16 19:54:42 2009 From: mbikinyi_brat at yahoo.com (mbikinyi brat) Date: Thu, 16 Apr 2009 10:54:42 -0700 (PDT) Subject: [Tutor] print output Message-ID: <657418.59346.qm@web110608.mail.gq1.yahoo.com> ? Dear ALL, I have defined variables as below and when I call them using the print function, I have something discontinous as in pink colour. How do I call it so that my output should be as in blue >>> counter=101 >>> miles=1000 >>> name="joe" >>> print counter 101 >>> print miles 1000 >>> print name joe >>> What I want:(Output) 101 1000 joe Regards, Henry -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Thu Apr 16 20:00:55 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 16 Apr 2009 14:00:55 -0400 Subject: [Tutor] Fwd: Import Modules In-Reply-To: <434e04240904160728s64ce05d0l743f79be4ddee76c@mail.gmail.com> References: <434e04240904150740w29defbb8i53dcda40b0e12ade@mail.gmail.com> <1c2a2c590904151837m17d2a689t69c0ac392f757870@mail.gmail.com> <23364.82674.qm@web86708.mail.ird.yahoo.com> <1c2a2c590904160337k78045c24rbee00bef405b378@mail.gmail.com> <434e04240904160728s64ce05d0l743f79be4ddee76c@mail.gmail.com> Message-ID: <1c2a2c590904161100u73404272m332f521ae22fd858@mail.gmail.com> Forwarding to the list ---------- Forwarded message ---------- From: GG Labs 10 Date: Thu, Apr 16, 2009 at 10:28 AM Subject: Re: [Tutor] Import Modules To: Kent Johnson Thankyou, i think i've understood the problem. Now, another App Engine related question: This code: --- import google print "Content-Type: text/html" print "" print "" print "" print "" print "google",dir(google) print "" --- Gives this output: --- google ['__builtins__', '__doc__', '__file__', '__name__', '__path__', 'appengine', 'net', 'pyglib'] --- appengine, net and pyglib are path (so subpackages of google package), not modules. Why when i import the google package all those subpackages are imported? In the __init__.py file i can't find anything (__all__ for example), and if i add a directory with a .py file at the same level of appengine, it doesn't get imported. Thankyou 2009/4/16 Kent Johnson > > On Thu, Apr 16, 2009 at 3:21 AM, ALAN GAULD wrote: > > > > > >> In general, importing a package does not give access to members of a > >> sub-package. > > > > Interestingly I added the comment about sub packages specifically > > because I remembered os.path and assumed it was the norm! :-) > > Yes, it took me a long time to realize that it is not. Strictly > speaking I don't think os is even a package, it is a module that > imports another module as it's path attribute. There is no os > directory or os/__init__.py or os/path.py. > > Kent > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From kent37 at tds.net Thu Apr 16 20:02:21 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 16 Apr 2009 14:02:21 -0400 Subject: [Tutor] print items in a list... In-Reply-To: References: Message-ID: <1c2a2c590904161102n1af24e0fk4a8ca203b39b9aa1@mail.gmail.com> On Thu, Apr 16, 2009 at 1:37 PM, Spencer Parker wrote: > I have a script that is putting elements into a list.? Now instead of adding > a for blah in blah statement...is there an easy way to print out the > elements in a list that is similar to this?? I don't have an absolute length > on the size of the list either since it changes depending on what I am > searching for.? I have it finding the elements through a regular expression > and then putting those into a list.? Separate lists for each item I am > searching for...if it find this put it into this list...else put into this > list.? Then I want to write each list to a text file.? trying to clean up > messy code for the most part. I don't understand the question. An example of what you are doing, or your current code, would help. Kent From dextrous85 at gmail.com Thu Apr 16 20:04:50 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Thu, 16 Apr 2009 23:34:50 +0530 Subject: [Tutor] print output In-Reply-To: <657418.59346.qm@web110608.mail.gq1.yahoo.com> References: <657418.59346.qm@web110608.mail.gq1.yahoo.com> Message-ID: <5487b3060904161104u2b791dcqf83c8eb091428ce2@mail.gmail.com> try this print str(counter) + "\n" + str(miles) + "\n"+ name On Thu, Apr 16, 2009 at 11:24 PM, mbikinyi brat wrote: > ** > *Dear ALL,* > I have defined variables as below and when I call them using the print > function, I have something discontinous as in pink colour. How do I call it > so that my output should be as in blue > *>>> counter=101 > >>> miles=1000 > >>> name="joe" > * > >>> print counter > 101 > >>> print miles > 1000 > >>> print name > joe > >>> > What I want:(Output) > > 101 > 1000 > joe > Regards, > Henry > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Thu Apr 16 20:26:57 2009 From: davea at ieee.org (Dave Angel) Date: Thu, 16 Apr 2009 14:26:57 -0400 Subject: [Tutor] print output In-Reply-To: References: Message-ID: <49E77871.7090107@ieee.org> mbikinyi brat wrote: > Dear ALL, > I have defined variables as below and when I call them using the print function, I have something discontinous as in pink colour. How do I call it so that my output should be as in blue > >>>> counter=101 >>>> miles=1000 >>>> name="joe" >>>> > > >>>> print counter >>>> > 101 > >>>> print miles >>>> > 1000 > >>>> print name >>>> > joe > > What I want:(Output) > > 101 > 1000 > joe > > Regards, > Henry > > > I assume you're just asking all the output to come out at the same time. Two ways to do that: 1) put multiple print statements (not print functions, as you're not using Python 3.0) on the line, like print counter; print miles; print name 2) Use newlines in a single print statement, to get them onto separate lines. print counter, "\n", miles, "\n", name 3) Write the whole thing in a definition, and call it. def myfun(): >>> counter=101 >>> miles=1000 >>> name="joe" >>> print counter >>> print miles >>> print name >>> myfun() (I can't seem to get those funny vertical bars to edit right, so this may not be formatted quite right.) From wescpy at gmail.com Thu Apr 16 22:36:57 2009 From: wescpy at gmail.com (wesley chun) Date: Thu, 16 Apr 2009 13:36:57 -0700 Subject: [Tutor] beginners resources list, Python education events Message-ID: <78b3a9580904161336v737c3ce0oc0734eb94e15e79b@mail.gmail.com> hey all, i'm trying to collate a complete newbie-to-programming list of resources and was wondering if you could help me fill in the holes... thx in advance! BOOKS Python Programming for the Absolute Beginner (Dawson, 2005) Python For Dummies (Maruch, Maruch, 2006) Python Programming: An Introduction to Computer Science (Zelle, 2003) Introduction to Computing and Programming in Python (Guzdial, 2004) How To Think Like a Computer Scientist (Elkner, Downey, Meyers, 2002) Python: Visual QuickStart Guide (Fehily, 2001) Learn to Program Using Python (Gauld, 2000) ONLINE How to Think like a Computer Scientist http://openbookproject.net/thinkCSpy Alan Gauld's Learning to Program http://www.freenetpages.co.uk/hp/alan.gauld LiveWires Python Course http://livewires.org.uk/python A Byte of Python http://byteofpython.info Guido van Robot http://gvr.sf.net Instant Hacking: Learning to Program with Python http://hetland.org/writing/instant-hacking.html Snake Wrangling for Kids http://briggs.net.nz/log/writing/snake-wrangling-for-kids please let me know if i'm missing anything as well as if any information is incorrect or out-of-date. also, does anyone know the latest about Alice (in particular, did they switch from Python to Java?) also, are there any good turtle ot vpython (or other 3D) drawing tools? also, are there any intros that use PyGame to teach programming via game-writing? on a somewhat related note, i'm doing 3 Python knowledge/education events within the next few months. 1. APRIL: O'Reilly/Safari Books Online "What is Python?" webcast (FREE to all) http://www.safaribooksonline.com/events/WhatIsPython.html 2. JUNE: Comprehensive Introduction to Python (3-day training course in San Francisco open to everyone!) http://mail.python.org/pipermail/python-list/2009-April/708428.html 3. JULY: O'Reilly Open Source Convention Tutorial instructor and session speaker (San Jose, CA) http://en.oreilly.com/oscon2009/public/schedule/speaker/44470 thanks! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From mbikinyi_brat at yahoo.com Thu Apr 16 22:45:23 2009 From: mbikinyi_brat at yahoo.com (mbikinyi brat) Date: Thu, 16 Apr 2009 13:45:23 -0700 (PDT) Subject: [Tutor] for loop Message-ID: <309631.3108.qm@web110604.mail.gq1.yahoo.com> Dear ALL, I am a beginner in python and I just copied the code in blue below and execute and had the result in green. Then I thought letter should be a special word in python. Then I now replace letter whith chic? and yet the same results is obtained. I cannot reconcile them. Can someone explained to me please? >>> for letter in "python": ?print "Current letter:", letter ? Current letter: p Current letter: y Current letter: t Current letter: h Current letter: o Current letter: n ? >>> for chic in "python": ?print "chic:", chic ? chic: p chic: y chic: t chic: h chic: o chic: n ? ? Regards, Henry -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Thu Apr 16 22:53:32 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 16 Apr 2009 13:53:32 -0700 Subject: [Tutor] for loop In-Reply-To: <309631.3108.qm@web110604.mail.gq1.yahoo.com> References: <309631.3108.qm@web110604.mail.gq1.yahoo.com> Message-ID: mbikinyi brat wrote: > Dear ALL, > I am a beginner in python and I just copied the code in blue below and > execute and had the result in green. Then I thought *letter* should be a > special word in python. Then I now replace letter whith *chic* and yet > the same results is obtained. of course -- changing the name of the variable used to hold results shouldn't change the outcome. I cannot reconcile them. Can someone > explained to me please? If you expect something different, then you need to write a different program. What did you want to happen? Emile > *>>> for letter in "python": > print "Current letter:", letter* > > *Current letter: p > Current letter: y > Current letter: t > Current letter: h > Current letter: o* > *Current letter: n* > ** > *>>> for chic in "python": > print "chic:", chic* > * > chic: p > chic: y > chic: t > chic: h > chic: o > chic: n* > * > * > * * > *Regards,* > *Henry* > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From david at abbottdavid.com Thu Apr 16 23:07:20 2009 From: david at abbottdavid.com (David) Date: Thu, 16 Apr 2009 17:07:20 -0400 Subject: [Tutor] beginners resources list, Python education events In-Reply-To: <78b3a9580904161336v737c3ce0oc0734eb94e15e79b@mail.gmail.com> References: <78b3a9580904161336v737c3ce0oc0734eb94e15e79b@mail.gmail.com> Message-ID: <49E79E08.9040608@abbottdavid.com> wesley chun wrote: > hey all, > > i'm trying to collate a complete newbie-to-programming list of > resources and was wondering if you could help me fill in the holes... > thx in advance! Hi wesley, A couple I liked; One Day of IDLE Toying http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html Dive Into Python http://www.diveintopython.org/toc/index.html A Python Course http://www.upriss.org.uk/python/PythonCourse.html -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From alan.gauld at btinternet.com Thu Apr 16 23:14:13 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Apr 2009 22:14:13 +0100 Subject: [Tutor] importance of Docstring References: <962889.95602.qm@web110601.mail.gq1.yahoo.com> Message-ID: "mbikinyi brat" wrote > What is really the importance of Docstring in Python??? Some really great comments about comments, however the significance of docstrings beyond normal comments is that tools like help() work with them Thus: >>> def f(x): '''f(x) -> x+5''' return x+5 >>> help(f) Help on function f in module __main__: f(x) f(x) -> x+5 >>> So immediately help() knows about f and can read its docstring. This is a huge help when using other peoples modules, or even your own modules that you haven't worked on for a while. You can import them and read the doctrings wiothout actually opening the code in an editor (which you would need to do for normal comments) So they are not essential but they do have a very real use beyond simple comments.. I say a bit more about docstrings in my tutorial topic "Add a little style" -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Apr 16 23:17:20 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Apr 2009 22:17:20 +0100 Subject: [Tutor] Building VST's with Python References: <69257c0f0904160359v4bcf2e81ibd6a3e3c4b2b5185@mail.gmail.com> Message-ID: "Logan Thomas" wrote > I'm new to python but do have a little programming knowledge with > C++....I > got into programming so I could build Virtual Instruments because I love > the > world of sound and creating instruments that shape it..I was wondering if > you could direct me to some tutorials or source code that might involve > building > virtual instruments or even games.... The obvious place would be pyGame I guess... It does have some support for sounds. There are also some sound modules in the standard library And for the Mac the QuickTime stuff is available although I'm not sure if it works for other platforms. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Apr 16 23:23:13 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Apr 2009 22:23:13 +0100 Subject: [Tutor] print output References: <657418.59346.qm@web110608.mail.gq1.yahoo.com> Message-ID: "mbikinyi brat" wrote > I have defined variables as below and when I call them using > the print function, I have something discontinous as in pink colour. > How do I call it so that my output should be as in blue >>> print counter 101 >>> print miles 1000 >>> print name joe >>> This is because you are using the interactive interpreter which evaluates eah line as you type it. If you type your code into a file called, say myprints.py and execute that from an OS prompt you will get the output you want. Alternativbely at the >>> prompt you can either concatenate all the variables separated by newlines or just put all 3 commands on a sinlgle line separated by semicolons: >>> print 5; print 6; print 7 5 6 7 >>> HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Apr 16 23:25:51 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Apr 2009 22:25:51 +0100 Subject: [Tutor] beginners resources list, Python education events References: <78b3a9580904161336v737c3ce0oc0734eb94e15e79b@mail.gmail.com> Message-ID: "wesley chun" wrote > Alan Gauld's Learning to Program > http://www.freenetpages.co.uk/hp/alan.gauld Now at: http://www.alan-g.me.uk/ for Python v2.x http://www.alan-g.me.uk/l2p for Python v3 and under construction, although well on the way now... The freenetpages site is scheduled to close and they are not allowing any updates, hence the move -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Apr 16 23:31:19 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Apr 2009 22:31:19 +0100 Subject: [Tutor] for loop References: <309631.3108.qm@web110604.mail.gq1.yahoo.com> Message-ID: "mbikinyi brat" wrote > Then I thought letter should be a special word in python. Why did you think that? Python has a very specific (and small) set of keywords that have special meaning - things like for, while and print But you can call variables pretty much whatever you like and Python will be happy. In fact the more meaningful the better, so letter is a very good choice. > Then I now replace letter whith chic and yet the same results > is obtained. Current letter: p Current letter: y chic: p chic: y They are not the same results, one says 'letter' and the other says 'chic', just as you told it to. In every other respect your code is identical so the output is identical. Python just does what you tell it to, it is not actually intelligent, it just interprets the commands you give it. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From gregor.lingl at aon.at Thu Apr 16 23:47:34 2009 From: gregor.lingl at aon.at (Gregor Lingl) Date: Thu, 16 Apr 2009 23:47:34 +0200 Subject: [Tutor] beginners resources list, Python education events In-Reply-To: <78b3a9580904161336v737c3ce0oc0734eb94e15e79b@mail.gmail.com> References: <78b3a9580904161336v737c3ce0oc0734eb94e15e79b@mail.gmail.com> Message-ID: <49E7A776.4050901@aon.at> wesley chun schrieb: > hey all, > ... > Python: Visual QuickStart Guide (Fehily, 2001) > This is outdated. There is a new one: Toby Donaldson: Python: Visual QuickStart Guide (2008) > Learn to Program Using Python (Gauld, 2000) > > ONLINE > How to Think like a Computer Scientist > http://openbookproject.net/thinkCSpy > > ... > please let me know if i'm missing anything as well as if any > information is incorrect or out-of-date. also, does anyone know the > latest about Alice (in particular, did they switch from Python to > Java?) also, are there any good turtle ot vpython (or other 3D) > drawing tools? As I'm the author of Pythons new turtle module (2.6 and 3.0), I'm - naturally - thinking that it is a good turtle drawing tool. In fact it is much more than merely a turtle module and includes features to create event driven programs and other things. As it is part of the Python standard distribution it can be used 'out of the box' So I think this deserves to be mentioned. (Did you not know about it or do you dislike it? If cou are really interested in it have a look at this: http://pycon.blip.tv/file/1947495/ ) Generally the Edu-Sig page has been reorganized recently by Andre Roberge and now contains a lot of up-to-date information on your topic. Regards, Gregor > also, are there any intros that use PyGame to teach > programming via game-writing? > > on a somewhat related note, i'm doing 3 Python knowledge/education > events within the next few months. > > 1. APRIL: O'Reilly/Safari Books Online "What is Python?" webcast (FREE to all) > http://www.safaribooksonline.com/events/WhatIsPython.html > > 2. JUNE: Comprehensive Introduction to Python (3-day training course > in San Francisco open to everyone!) > http://mail.python.org/pipermail/python-list/2009-April/708428.html > > 3. JULY: O'Reilly Open Source Convention Tutorial instructor and > session speaker (San Jose, CA) > http://en.oreilly.com/oscon2009/public/schedule/speaker/44470 > > thanks! > -- wesley > - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > "Core Python Programming", Prentice Hall, (c)2007,2001 > "Python Fundamentals", Prentice Hall, (c)2009 > http://corepython.com > > wesley.j.chun :: wescpy-at-gmail.com > python training and technical consulting > cyberweb.consulting : silicon valley, ca > http://cyberwebconsulting.com > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > From wescpy at gmail.com Thu Apr 16 23:51:29 2009 From: wescpy at gmail.com (wesley chun) Date: Thu, 16 Apr 2009 14:51:29 -0700 Subject: [Tutor] importance of Docstring In-Reply-To: References: <962889.95602.qm@web110601.mail.gq1.yahoo.com> Message-ID: <78b3a9580904161451q367ed5bbye6d2ad059ef4875e@mail.gmail.com> >>>> def f(x): > > '''f(x) -> x+5''' > return x+5 > >>>> help(f) > > Help on function f in module __main__: > > f(x) > ? f(x) -> x+5 another thing that has not been mentioned is that if you put docstrings everywhere, you can use tools like Epydoc, doxygen, or sphinx to generate full documentation of an entire codebase, all because developers made it easy by dropping in useful documentation in their docstrings instead of just 1-line comments. on top of this, you can add extra comments to DEMONSTRATE how the function works, i.e., def f(x): """ f(x) -> x+5 >>> f(123) 128 """ return x+5 this save your users critical time it takes to understand the code and how it works. on top of THAT, you can use the doctest module to *run* that code to confirm it works (or doesn't). try adding that code plus the 5 lines below to a file called f.py: def _test(): import doctest doctest.testmod() if __name__ == '__main__': _test() now run it: $ f.py $ you get no output which means everything worked! if you want to see it in action, use "-v": $ f.py -v Trying: f(123) Expecting: 128 ok 2 items had no tests: __main__ __main__._test 1 items passed all tests: 1 tests in __main__.f 1 tests in 3 items. 1 passed and 0 failed. Test passed. it tells you how many possible places you can add doctests, how many were found, and what the results were. if you had an error, then you would definitely get output, with or without the "-v". let's change our docstring output to the wrong answer and rerun: $ f.py ********************************************************************** File "f.py", line 7, in __main__.f Failed example: f(124) Expected: 128 Got: 129 ********************************************************************** 1 items had failures: 1 of 1 in __main__.f ***Test Failed*** 1 failures. of course, you can also implement doctesting along with your normal unit tests too and not necessarily have to add those 5 lines to all of your modules. hope you can now see the usefulness and importance of docstrings. -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From alan.gauld at btinternet.com Thu Apr 16 23:52:04 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Apr 2009 22:52:04 +0100 Subject: [Tutor] beginners resources list, Python education events References: <78b3a9580904161336v737c3ce0oc0734eb94e15e79b@mail.gmail.com> Message-ID: "wesley chun" wrote > i'm trying to collate a complete newbie-to-programming list of > resources and was wondering if you could help me fill in the holes... > thx in advance! What about the free (and even the pay for) videos on the ShowMeDo site. I thought they were pretty good especially for showing how to use the tools, like IDLE, Eclipse/PyDev, pdb etc Alan G. From jfabiani at yolo.com Fri Apr 17 00:52:02 2009 From: jfabiani at yolo.com (johnf) Date: Thu, 16 Apr 2009 15:52:02 -0700 Subject: [Tutor] list to string and string to list Message-ID: <200904161552.02772.jfabiani@yolo.com> I am dealing with a database field that can only store strings. I want to save the list to the field and when I retrieve the string convert it back to a list. But this does NOT work. mylist=[1,2,3,4] mystr=str(mylist) newlist= list(mystr) I keep thinking there must be a simple way of get this done. -- John Fabiani From jfabiani at yolo.com Fri Apr 17 01:05:30 2009 From: jfabiani at yolo.com (johnf) Date: Thu, 16 Apr 2009 16:05:30 -0700 Subject: [Tutor] list to string and string to list In-Reply-To: <200904161552.02772.jfabiani@yolo.com> References: <200904161552.02772.jfabiani@yolo.com> Message-ID: <200904161605.30942.jfabiani@yolo.com> On Thursday 16 April 2009 03:52:02 pm johnf wrote: > I am dealing with a database field that can only store strings. > I want to save the list to the field and when I retrieve the string convert > it back to a list. > > But this does NOT work. > mylist=[1,2,3,4] > mystr=str(mylist) > > newlist= list(mystr) > > I keep thinking there must be a simple way of get this done. Is this a good way? newlist = eval(mystr) -- John Fabiani From ricaraoz at gmail.com Fri Apr 17 01:32:01 2009 From: ricaraoz at gmail.com (=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=) Date: Thu, 16 Apr 2009 20:32:01 -0300 Subject: [Tutor] Here's something to talk about In-Reply-To: References: Message-ID: <49E7BFF1.7020008@gmail.com> Weidner, Ronald wrote: > One of your points represents a great opportunity to make mine. Suppose > this code is several years old. Now we have a new requirement that > states we need to work with the data as you described above. How > much of the existing code would you have to change to make that change > happen? The answer is exactly one line. And here is the line... > > exporter = SQLExporter("itemTable") > > And you would change the line to something like... > > exporter = SQLTupleExporter("itemTable") > > or perhaps... > > exporter = ExportItemToDatabase('127.0.0.1', 'user', 'pass', 'schema') > > Of course, you would need to define a class to go along with your change > but all of that will be NEW code. Not a change to existing code. So, > all of your existing code should still be valid and tested. The only > thing you'll need to do validate and test your new code. > The question I usually make to myself is not how many lines of code do I have to change. I'd rather think of how much of the code do I have to read and understand before I know which lines to change, and how hard will it be to understand it. If I read just a few lines and then know I have to replace """exporter = SQLExporter("itemTable")""" with """exporter = SQLTupleExporter("itemTable")""" in a thousand places, that's ok with me, the editor can handle this flawlessly in a few seconds. I'd rather have simpler code that I can understand easily and modular enough that I can find what to change without reading and understanding the whole thing. From kent37 at tds.net Fri Apr 17 01:55:10 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 16 Apr 2009 19:55:10 -0400 Subject: [Tutor] list to string and string to list In-Reply-To: <200904161552.02772.jfabiani@yolo.com> References: <200904161552.02772.jfabiani@yolo.com> Message-ID: <1c2a2c590904161655u50020d4dp4b90366d498470b6@mail.gmail.com> On Thu, Apr 16, 2009 at 6:52 PM, johnf wrote: > I am dealing with a database field that can only store strings. > I want to save the list to the field and when I retrieve the string convert it > back to a list. > > But this does NOT work. > mylist=[1,2,3,4] > mystr=str(mylist) > > newlist= list(mystr) > > I keep thinking there must be a simple way of get this done. You can use eval() but that is generally discouraged as a security hole. If the list will just contain integers you can do something ad hoc: In [7]: mylist=[1,2,3,4] In [8]: mystr=str(mylist) In [10]: newlist = map(int, mystr[1:-1].split(',')) In [11]: newlist Out[11]: [1, 2, 3, 4] In Python 3 you can use ast.literal_eval(). >>> mylist = [1, 2, 3, 4] >>> mystr = str(mylist) >>> import ast >>> ast.literal_eval(mystr) [1, 2, 3, 4] You can use these recipes: http://code.activestate.com/recipes/511473/ http://code.activestate.com/recipes/364469/ Kent From alan.gauld at btinternet.com Fri Apr 17 02:04:39 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 17 Apr 2009 01:04:39 +0100 Subject: [Tutor] list to string and string to list References: <200904161552.02772.jfabiani@yolo.com> <200904161605.30942.jfabiani@yolo.com> Message-ID: "johnf" wrote >> I want to save the list to the field and when I retrieve the string >> convert >> it back to a list. >> >> But this does NOT work. >> mylist=[1,2,3,4] >> mystr=str(mylist) >> >> newlist= list(mystr) >> >> I keep thinking there must be a simple way of get this done. > > Is this a good way? > newlist = eval(mystr) eval has all sorts of security implications so I wouldn't recommend it where you are reading data fropm an external source. One thing that might work is this: >>> L = [1,2,3,4,5] >>> s1 = ','.join(str(n) for n in L) >>> s1 '1,2,3,4,5' >>> newlist = [int(n) for n in s1.split(',')] >>> newlist [1, 2, 3, 4, 5] Provided your original data doesn't have commas to start with it should work, I think... And the data needs to be pretty homogenous to allow a single conversion function. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From jfabiani at yolo.com Fri Apr 17 02:16:55 2009 From: jfabiani at yolo.com (johnf) Date: Thu, 16 Apr 2009 17:16:55 -0700 Subject: [Tutor] list to string and string to list In-Reply-To: References: <200904161552.02772.jfabiani@yolo.com> <200904161605.30942.jfabiani@yolo.com> Message-ID: <200904161716.55358.jfabiani@yolo.com> On Thursday 16 April 2009 05:04:39 pm Alan Gauld wrote: > "johnf" wrote > > >> I want to save the list to the field and when I retrieve the string > >> convert > >> it back to a list. > >> > >> But this does NOT work. > >> mylist=[1,2,3,4] > >> mystr=str(mylist) > >> > >> newlist= list(mystr) > >> > >> I keep thinking there must be a simple way of get this done. > > > > Is this a good way? > > newlist = eval(mystr) > > eval has all sorts of security implications so I wouldn't recommend > it where you are reading data fropm an external source. > > One thing that might work is this: > >>> L = [1,2,3,4,5] > >>> s1 = ','.join(str(n) for n in L) > >>> s1 > > '1,2,3,4,5' > > >>> newlist = [int(n) for n in s1.split(',')] > >>> newlist > > [1, 2, 3, 4, 5] > > Provided your original data doesn't have commas to start with > it should work, I think... And the data needs to be pretty > homogenous to allow a single conversion function. Kent Johnson suggested newlist = map(int, mystr[1:-1].split(',')) -- John Fabiani From mwalsh at mwalsh.org Fri Apr 17 02:41:01 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Thu, 16 Apr 2009 19:41:01 -0500 Subject: [Tutor] list to string and string to list In-Reply-To: <200904161716.55358.jfabiani@yolo.com> References: <200904161552.02772.jfabiani@yolo.com> <200904161605.30942.jfabiani@yolo.com> <200904161716.55358.jfabiani@yolo.com> Message-ID: <49E7D01D.2040905@mwalsh.org> johnf wrote: > On Thursday 16 April 2009 05:04:39 pm Alan Gauld wrote: >> "johnf" wrote >> >>>> I want to save the list to the field and when I retrieve the string >>>> convert >>>> it back to a list. >>>> >>>> But this does NOT work. >>>> mylist=[1,2,3,4] >>>> mystr=str(mylist) >>>> >>>> newlist= list(mystr) >>>> >>>> I keep thinking there must be a simple way of get this done. >>> Is this a good way? >>> newlist = eval(mystr) >> eval has all sorts of security implications so I wouldn't recommend >> it where you are reading data fropm an external source. >> >> One thing that might work is this: >>>>> L = [1,2,3,4,5] >>>>> s1 = ','.join(str(n) for n in L) >>>>> s1 >> '1,2,3,4,5' >> >>>>> newlist = [int(n) for n in s1.split(',')] >>>>> newlist >> [1, 2, 3, 4, 5] >> >> Provided your original data doesn't have commas to start with >> it should work, I think... And the data needs to be pretty >> homogenous to allow a single conversion function. > > Kent Johnson suggested > > newlist = map(int, mystr[1:-1].split(',')) > Maybe that's a question in disguise, but I would think both suggestions are valid. Another, less valid, suggestion would be to pickle or shelve the list before storing it in the database -- although this poses similar security implications as eval. And the resulting string is not particularly easy to read, if that's important to you. import cPickle as pickle mylist = [1,2,3,4] mystr = pickle.dumps(mylist) # '(lp1\nI1\naI2\naI3\naI4\na.' newlist = pickle.loads(mystr) # [1, 2, 3, 4] HTH, Marty From mwalsh at mwalsh.org Fri Apr 17 02:54:51 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Thu, 16 Apr 2009 19:54:51 -0500 Subject: [Tutor] list to string and string to list In-Reply-To: <49E7D01D.2040905@mwalsh.org> References: <200904161552.02772.jfabiani@yolo.com> <200904161605.30942.jfabiani@yolo.com> <200904161716.55358.jfabiani@yolo.com> <49E7D01D.2040905@mwalsh.org> Message-ID: <49E7D35B.3030905@mwalsh.org> Martin Walsh wrote: > johnf wrote: >> On Thursday 16 April 2009 05:04:39 pm Alan Gauld wrote: >>> "johnf" wrote >>> >>>>> I want to save the list to the field and when I retrieve the string >>>>> convert >>>>> it back to a list. >>>>> >>>>> But this does NOT work. >>>>> mylist=[1,2,3,4] >>>>> mystr=str(mylist) >>>>> >>>>> newlist= list(mystr) >>>>> >>>>> I keep thinking there must be a simple way of get this done. >>>> Is this a good way? >>>> newlist = eval(mystr) >>> eval has all sorts of security implications so I wouldn't recommend >>> it where you are reading data fropm an external source. >>> >>> One thing that might work is this: >>>>>> L = [1,2,3,4,5] >>>>>> s1 = ','.join(str(n) for n in L) >>>>>> s1 >>> '1,2,3,4,5' >>> >>>>>> newlist = [int(n) for n in s1.split(',')] >>>>>> newlist >>> [1, 2, 3, 4, 5] >>> >>> Provided your original data doesn't have commas to start with >>> it should work, I think... And the data needs to be pretty >>> homogenous to allow a single conversion function. >> Kent Johnson suggested >> >> newlist = map(int, mystr[1:-1].split(',')) >> > > Maybe that's a question in disguise, but I would think both suggestions > are valid. > > Another, less valid, suggestion would be to pickle or shelve the list > before storing it in the database -- although this poses similar > security implications as eval. And the resulting string is not > particularly easy to read, if that's important to you. > > import cPickle as pickle > mylist = [1,2,3,4] > > mystr = pickle.dumps(mylist) > # '(lp1\nI1\naI2\naI3\naI4\na.' > > newlist = pickle.loads(mystr) > # [1, 2, 3, 4] Sorry to reply to my own post, but the json module in python 2.6+ (formerly 3rd party, simplejson) might work for your purposes also. But I must confess, I'm not that familiar. import json s = json.dumps([1, 2, 3, 4]) # '[1, 2, 3, 4]' l = json.loads(s) # [1, 2, 3, 4] > > HTH, > Marty From amonroe at columbus.rr.com Fri Apr 17 03:24:37 2009 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Thu, 16 Apr 2009 21:24:37 -0400 Subject: [Tutor] for loop In-Reply-To: <309631.3108.qm@web110604.mail.gq1.yahoo.com> References: <309631.3108.qm@web110604.mail.gq1.yahoo.com> Message-ID: <681197980154.20090416212437@columbus.rr.com> >>>> for letter in "python": > ?print "Current letter:", letter >>>> for chic in "python": > ?print "chic:", chic When you write a for-in loop, you can use any variable name you feel like using (letter, chic, mycoolvariable, x, etc.) It's that simple :) Alan From srilyk at gmail.com Fri Apr 17 04:01:48 2009 From: srilyk at gmail.com (W W) Date: Thu, 16 Apr 2009 21:01:48 -0500 Subject: [Tutor] for loop In-Reply-To: <309631.3108.qm@web110604.mail.gq1.yahoo.com> References: <309631.3108.qm@web110604.mail.gq1.yahoo.com> Message-ID: <333efb450904161901j68bd189axf1f38dffed759f43@mail.gmail.com> On Thu, Apr 16, 2009 at 3:45 PM, mbikinyi brat wrote: > Dear ALL, > I am a beginner in python and I just copied the code in blue below and > execute and had the result in green. Then I thought *letter* should be a > special word in python. Then I now replace letter whith *chic* and yet > the same results is obtained. I cannot reconcile them. Can someone explained > to me please? > I think your confusion lies in how python for loops work. "Python" in this case is a string. You could replace it with "Spam" "Knights" or "Ni", if you so desire. A string is an iterable - in other words, you can iterate over it automatically. With a language like C++ you would write something like this: string foo = "python"; for(int x = 0; x < foo.size; x++){ cout << foo.at(x) << endl; } to iterate over the string. Python takes care of that for you with any iterable type (list, set, etc.) for item in [1, 2, 3]: print item for letter in "word": print word for foobar in (1, 0, 234, 'hello', 'foo'): print foobar It all works the same. It's rather useful :) HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From mbikinyi_brat at yahoo.com Fri Apr 17 10:42:26 2009 From: mbikinyi_brat at yahoo.com (mbikinyi brat) Date: Fri, 17 Apr 2009 01:42:26 -0700 (PDT) Subject: [Tutor] Functions output Message-ID: <136674.45778.qm@web110606.mail.gq1.yahoo.com> Dear all, Here are two functions printme and change quite spaced apart. When executed the output in bold are joined. What can I do so that the results are separate in two blocks? ? #Function definition is here def printme(str): ??? "This prints a passed string into this function" ??? print str ??? return #Now you can call printme function printme("I'm first call to user defined function!") printme("Again second call to the same function") ? ? #Function definition is here def changeme(mylist): ??? "THIS CHANGES A PASSED LIST INTO THIS FUNCTION" ??? mylist.append([1, 2,3,4]) ??? ??? return " now you can call the changme function" mylist=[10, 20, 30] print "valuse outside the function:", mylist print "values inside this function:", mylist ? ? ? >>> I'm first call to user defined function! Again second call to the same function valuse outside the function: [10, 20, 30] values inside this function: [10, 20, 30] >>> Regards, Henry -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Fri Apr 17 11:54:18 2009 From: denis.spir at free.fr (spir) Date: Fri, 17 Apr 2009 11:54:18 +0200 Subject: [Tutor] importance of Docstring In-Reply-To: <49E7022D.6060802@tue.nl> References: <962889.95602.qm@web110601.mail.gq1.yahoo.com> <49E7022D.6060802@tue.nl> Message-ID: <20090417115418.502bd6f3@o> Le Thu, 16 Apr 2009 12:02:21 +0200, "A.T.Hofkamp" s'exprima ainsi: > Now what does this function do? > This is a 'what' question, namely, from a design point of view, if I call > this function, what does it do, and what is its result-value? > What I miss is a good place for "why?". Which, when needed, is different from "what?" and "how?". Why tells about the reason-to-be: why did I (need to) write the following piece of code? Not always needed (while what is nearly always needed), though, but really helpful in this case. Denis ------ la vita e estrany From kent37 at tds.net Fri Apr 17 12:35:01 2009 From: kent37 at tds.net (Kent Johnson) Date: Fri, 17 Apr 2009 06:35:01 -0400 Subject: [Tutor] Functions output In-Reply-To: <136674.45778.qm@web110606.mail.gq1.yahoo.com> References: <136674.45778.qm@web110606.mail.gq1.yahoo.com> Message-ID: <1c2a2c590904170335q66620dd2v6418781b5574b08d@mail.gmail.com> On Fri, Apr 17, 2009 at 4:42 AM, mbikinyi brat wrote: > Dear all, > Here are two functions printme and change quite spaced apart. When executed > the output in bold are joined. What can I do so that the results are > separate in two blocks? The format of the output depends only on the contents of the print statements, not on the arrangement of the code. You can print a blank line with a plain print statement or by including a newline '\n' in your output like this: print 'This is the first line\nThis is the second line' Kent From davea at ieee.org Fri Apr 17 14:37:36 2009 From: davea at ieee.org (Dave Angel) Date: Fri, 17 Apr 2009 08:37:36 -0400 Subject: [Tutor] Functions output In-Reply-To: References: Message-ID: <49E87810.1000408@ieee.org> mbikinyi brat wrote: > Dear all, > Here are two functions printme and change quite spaced apart. When executed the output in bold are joined. What can I do so that the results are separate in two blocks? > ? > #Function definition is here > def printme(str): > ??? "This prints a passed string into this function" > ??? print str > ??? return > #Now you can call printme function > printme("I'm first call to user defined function!") > printme("Again second call to the same function") > ? > ? > #Function definition is here > def changeme(mylist): > ??? "THIS CHANGES A PASSED LIST INTO THIS FUNCTION" > ??? mylist.append([1, 2,3,4]) > ??? > ??? return > " now you can call the changme function" > mylist=[10, 20, 30] > print "valuse outside the function:", mylist > print "values inside this function:", mylist > ? > ? > ? > > I'm first call to user defined function! > Again second call to the same function > valuse outside the function: [10, 20, 30] > values inside this function: [10, 20, 30] > > > Regards, > Henry > > I can't see "bold," in a text email. But presuming you're wondering why the two output lines are deferred: starting "I'm first..." and "Again second..." I wonder as well. When I try to replicate your situation, I'm starting python.exe at the XP Windows command prompt. It starts like this: M:\>c:\ProgFiles\Python26\python.exe Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on win32 What does yours say? Anyway when I type exactly what you did, I get several errors. So I suspect your transcript isn't faithful. You really should copy&paste from the console to the message. So I add indentation, and blank lines as appropriate, and as expected, I see each of those two lines echoes immediately after the corresponding call to printme(). I suspect you're doing this inside some shell tool, perhaps in some IDE, not at the command prompt. From ksarikhani at gmail.com Fri Apr 17 23:48:10 2009 From: ksarikhani at gmail.com (Kayvan Sarikhani) Date: Fri, 17 Apr 2009 17:48:10 -0400 Subject: [Tutor] Sending a disconnect after openssl s_client command? Message-ID: Tutors, I'm working on a script to verify whether a particular website supports SSLv2 via the following: --- BEGIN --- #!/usr/bin/python import os, re checkssl_out = open('checkssl.txt','w') website = 'somewebsitename' sslv2 = 'Protocol : SSLv2' print 'Checking:', website checksslv2 = os.popen('openssl s_client -ssl2 -connect somewebsitename:443').read().strip() if re.search(sslv2, checksslv2) == None: print >> checkssl_out, website, 'does NOT support SSLv2' else: print >> checkssl_out, website, 'supports: SSLv2' checkssl_out.close() --- END --- It works, but the problem is that OpenSSL does not automatically disconnect after end of input. I was curious if there's a way to send a CTRL-C at the end of the command, so that it *does* capture the output, and breaks after it. Any suggestions or help is appreciated! K -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Apr 18 00:31:30 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 17 Apr 2009 23:31:30 +0100 Subject: [Tutor] importance of Docstring References: <962889.95602.qm@web110601.mail.gq1.yahoo.com><49E7022D.6060802@tue.nl> <20090417115418.502bd6f3@o> Message-ID: "spir" wrote >> Now what does this function do? >> This is a 'what' question, namely, from a design point of view, if I >> call >> this function, what does it do, and what is its result-value? >> > > What I miss is a good place for "why?". Which, when needed, is different > from "what?" and "how?". Why tells about the reason-to-be: I always argue that comments should be for why(the code is written this way. what (do I do with this) should be the docstring how (the code works) should be self evident from clearly written code at the function lebel or in a design document for the bigger application level how! Those are my personal rules of thumb. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Apr 18 00:39:26 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 17 Apr 2009 23:39:26 +0100 Subject: [Tutor] Functions output References: <136674.45778.qm@web110606.mail.gq1.yahoo.com> Message-ID: "mbikinyi brat" wrote > Here are two functions printme and change quite spaced apart. The spacing of the functions inside the source file has nothing to do with the output. Python reads the input file, ignores any spacing and executes any lines it recognises as code. > When executed the output in bold are joined. > What can I do so that the results are separate in two blocks? If you want to separatye the output you need to insert some space. You are in complete control, the computer is not intelligent enough to deduce that you want the two outputs separated. You need to tell it to print some empty space. One way to do that is by printing newline charactrs('\n') To print 5 newlines you can do this: print '\n' * 5 > #Now you can call printme function > printme("I'm first call to user defined function!") > printme("Again second call to the same function") add print '\n' * 3 here > #Function definition is here > def changeme(mylist): > print "valuse outside the function:", mylist > print "values inside this function:", mylist Should result in: I'm first call to user defined function! Again second call to the same function valuse outside the function: [10, 20, 30] values inside this function: [10, 20, 30] HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mbikinyi_brat at yahoo.com Sat Apr 18 02:46:38 2009 From: mbikinyi_brat at yahoo.com (mbikinyi brat) Date: Fri, 17 Apr 2009 17:46:38 -0700 (PDT) Subject: [Tutor] Python and Semantic web Message-ID: <554153.68107.qm@web110608.mail.gq1.yahoo.com> -------------- next part -------------- An HTML attachment was scrubbed... URL: From mbikinyi_brat at yahoo.com Sat Apr 18 03:11:22 2009 From: mbikinyi_brat at yahoo.com (mbikinyi brat) Date: Fri, 17 Apr 2009 18:11:22 -0700 (PDT) Subject: [Tutor] Protege and python Message-ID: <600938.47175.qm@web110604.mail.gq1.yahoo.com> -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Sat Apr 18 16:04:22 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 18 Apr 2009 16:04:22 +0200 Subject: [Tutor] Python and Semantic web In-Reply-To: <554153.68107.qm@web110608.mail.gq1.yahoo.com> References: <554153.68107.qm@web110608.mail.gq1.yahoo.com> Message-ID: mbikinyi brat wrote: > > > > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor Uhm, what about "free and beer"? That would make a good subject. Stefan From pydev at rscorp.ab.ca Sat Apr 18 23:07:55 2009 From: pydev at rscorp.ab.ca (Scott SA) Date: Sat, 18 Apr 2009 15:07:55 -0600 Subject: [Tutor] Python logging Message-ID: <2029995F-8F47-4650-94B3-15937BF45A2D@rscorp.ab.ca> Hi, For some reason, I'm having a bit of trouble figuring this one out. I know it has to be relatively easy but it just isn't "working" for me at the moment. I want to re-direct the logs from python's logging library and while using a config file rather than instantiating all of the logging within my actual code. So, lets say I have a file log.conf that contains the various directives for the logging module. Amongst all of the other settings a handler might look like this: [handler_warn_logfile] class=handlers.RotatingFileHandler level=WARNING formatter=DatetimeLevelMessage args=('test_warn.log', 'a', 125829120, 5) filename=test_warn.log mode=a then loaded like this logging.config.fileConfig(log_config_path) logging.debug('Logging enabled') ... which basically says append WARNING messages to the rotating file handler in DatetimeLevelMessage format to a log-file named test_warn.log splitting the log-file when it exceeds 125829120 bytes. Because I don't have any full paths listed, the logger uses the current working directory i.e. "/home/user/workspace/logged_app/ test_warn.log" I could change the name to "logs/test_warn.log" for a sub-directory but I'd like to put a data on some of the logs and not necessarily in the root of the scripts but maybe elsewhere on the system i.e. /home/ user/logs/someapp/200904_logs/ I've done a bunch of inspection of dictionaries, methods and attributes but nothing seems to be surfacing that would allow me to re- direct the paths in a dynamic way -- other than using os.cwd('some_path') before instantiating the logging... which is quite hack-ish IMO. The only thing that comes to mind, after reviewing the logging via help(logging...), is that I may need to dynamically re-write the .conf file using ConfigParser. Any ideas on what I'm missing within the logging for redirecting the files? Or, should I bump this up to the main python list? Thanks in advance, Scott From kent37 at tds.net Sun Apr 19 12:34:24 2009 From: kent37 at tds.net (Kent Johnson) Date: Sun, 19 Apr 2009 06:34:24 -0400 Subject: [Tutor] Python logging In-Reply-To: <2029995F-8F47-4650-94B3-15937BF45A2D@rscorp.ab.ca> References: <2029995F-8F47-4650-94B3-15937BF45A2D@rscorp.ab.ca> Message-ID: <1c2a2c590904190334s46ef6e8ch30c6d04a12bdcd1d@mail.gmail.com> On Sat, Apr 18, 2009 at 5:07 PM, Scott SA wrote: > I want to re-direct the logs from python's logging library and while using a > config file rather than instantiating all of the logging within my actual > code. > > So, lets say I have a file log.conf that contains the various directives for > the logging module. Amongst all of the other settings a handler might look > like this: > > ? ? ? ?[handler_warn_logfile] > ? ? ? ?class=handlers.RotatingFileHandler > ? ? ? ?level=WARNING > ? ? ? ?formatter=DatetimeLevelMessage > ? ? ? ?args=('test_warn.log', 'a', 125829120, 5) > ? ? ? ?filename=test_warn.log > ? ? ? ?mode=a > > then loaded like this > > ? ? ? ?logging.config.fileConfig(log_config_path) > ? ? ? ?logging.debug('Logging enabled') > > > ... which basically says append WARNING messages to the rotating file > handler in DatetimeLevelMessage format to a log-file named test_warn.log > splitting the log-file when it exceeds 125829120 bytes. > > Because I don't have any full paths listed, the logger uses the current > working directory i.e. "/home/user/workspace/logged_app/test_warn.log" > > I could change the name to "logs/test_warn.log" for a sub-directory but I'd > like to put a data on some of the logs and not necessarily in the root of > the scripts but maybe elsewhere on the system i.e. > /home/user/logs/someapp/200904_logs/ Looking at the code for the logging module, I think it will work to create the RotatingFileHandler with the parameter delay=True, then in your code get the handler and change its baseFilename attribute to be the path you want, e.g. "/home/user/workspace/logged_app/test_warn.log". Kent From dextrous85 at gmail.com Sun Apr 19 12:52:25 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Sun, 19 Apr 2009 16:22:25 +0530 Subject: [Tutor] Webpy vs Django Message-ID: <5487b3060904190352h1e55b759y71cf7b12955f3c89@mail.gmail.com> Hi All, This is not to flame any war; I just wanted to know the key features to consider among the two web frame works. What advantage and disadvantages you have when you decide using any one of them. Thanks for your help -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Sun Apr 19 14:12:59 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 19 Apr 2009 14:12:59 +0200 Subject: [Tutor] Python logging In-Reply-To: <2029995F-8F47-4650-94B3-15937BF45A2D@rscorp.ab.ca> References: <2029995F-8F47-4650-94B3-15937BF45A2D@rscorp.ab.ca> Message-ID: Hi, just commenting on this part: Scott SA wrote: > [handler_warn_logfile] > class=handlers.RotatingFileHandler > level=WARNING > formatter=DatetimeLevelMessage > args=('test_warn.log', 'a', 125829120, 5) > filename=test_warn.log > mode=a > > ... which basically says append WARNING messages to the rotating file > handler in DatetimeLevelMessage format to a log-file named test_warn.log > splitting the log-file when it exceeds 125829120 bytes. You may have a reason to cut these at "125829120" bytes, but it's usually a bad idea to cut log files by size. It's much more natural to have time sliced log files (one per day or per hour), so that you directly know which file you have to look at to analyse that stuff that went so terribly wrong yesterday evening (especially once they get archived somewhere). Stefan From pydev at rscorp.ab.ca Sun Apr 19 21:17:32 2009 From: pydev at rscorp.ab.ca (Scott SA) Date: Sun, 19 Apr 2009 13:17:32 -0600 Subject: [Tutor] Webpy vs Django In-Reply-To: <5487b3060904190352h1e55b759y71cf7b12955f3c89@mail.gmail.com> References: <5487b3060904190352h1e55b759y71cf7b12955f3c89@mail.gmail.com> Message-ID: <2BAB60A0-E2FF-4803-A4D3-71293290AB77@rscorp.ab.ca> On Apr 19, 2009, at 4:52 AM, vishwajeet singh wrote: > This is not to flame any war; I just wanted to know the key features > to consider among the two web frame works. > > What advantage and disadvantages you have when you decide using any > one of them. I can't speak with any authority on Webpy (webpy.org), as I have not actually used it though I did read up on its features about a year ago and now just a quick glance again. I do have some experience with a different 'light-weight' framework called Quixote (www.quixote.ca) and have a production project using it... that I'm migrating to Django. I also have a couple of projects using Zope... also being migrated to Django. I don't mention all of this as any indication of prowess, in fact much of the code on these projects has been written by other developers I've contracted to, but that I have some similar experience. The main point I wish to make of all this is that these different frameworks kind of coalesce into sub-groups. They sort-of form into three groups: light-weight framework, full-featured framework and heavy CMS (lots of inseparable overhead). Furthermore, like a series of balls or coins in a simple Pachinko machine (http://en.wikipedia.org/wiki/Pachinko ) where each one takes a different route, the application you are developing will have a huge influence on requirements. Personally, I've been working with Django on and off for about 2-1/2 years and find it a joy to work with. It does not have the overhead and hidden 'magic' of Zope and I don't have to re-invent almost everything like Quixote. For _my_ needs, it has been a great blend of capabilities without the oppressing overhead of the CMS. I can add or remove the modules needed on a project-by-project basis. "Out of the box", the automated admin interface is a huge time-saver and its object-relational model is quite good at wrapping relational databases into a python-friendly framework. Also it's user-admin and authentication takes care of the core of most sites I work on. It has a good blend for my requirements of granular framework access and functional tools to save me time. And now, there is quite a nice selection of add-on applications that extend Django in a very modular fashion, though there are still growing pains. Like "Goldie Locks and the three bears", this one is "just right" for my needs. Just as Zope is overkill for projects that don't need CMS, Django would be overkill for a project that didn't need much for database access and admin interface. The authors of Django, try pretty hard to not hide anything that is going on in the background but there is still a lot being done for the developer. For example in creating db schemas, wrapping queries and result-sets into object calls, rendering content through templates and much more. All have ways of being modified but not necessarily by novice python developers. So the short answer to your question is that Webpy and Django are quite different in their tool-sets and are therefore tough to compare. As with all lightweight frameworks, they are _shorter_ on pre-defined features. That's not a negative, that _is_ the point of them. Django has tried to bridge, and in my opinion done a good job of it, a lighter-weight core with a well documented and developer-friendly interface for extension. It is a framework that has allowed me to gradually dig deeper and deeper into its workings to extract more advanced features while not bogging me down with redundant and tedious things like forms and user authentication. I chose Django because it had the most to offer for what I need and it seems to have the "legs" to cover a lot of ground. That said, I don't that anyone could say you were wrong by choosing either of the projects you mentioned given appropriate circumstances (though personally, I can't find _any_ circumstances where I'd recommend Zope today -- but that's probably a personal bias and I won't digress). I know this doesn't answer your question directly but I hope it helps broaden the perspective a little, Scott From pydev at rscorp.ab.ca Sun Apr 19 21:18:12 2009 From: pydev at rscorp.ab.ca (Scott SA) Date: Sun, 19 Apr 2009 13:18:12 -0600 Subject: [Tutor] Python logging In-Reply-To: References: <2029995F-8F47-4650-94B3-15937BF45A2D@rscorp.ab.ca> Message-ID: <4748C9BC-D25A-4DF1-B2D9-4927D93B0E98@rscorp.ab.ca> Hi Stephan & Kent, On Apr 19, 2009, at 6:12 AM, Stefan Behnel wrote: > just commenting on this part: > >> ... which basically says append WARNING messages to the rotating file >> handler in DatetimeLevelMessage format to a log-file named >> test_warn.log >> splitting the log-file when it exceeds 125829120 bytes. > > You may have a reason to cut these at "125829120" bytes, but it's > usually a > bad idea to cut log files by size. It's much more natural to have time > sliced log files (one per day or per hour), so that you directly > know which > file you have to look at to analyse that stuff that went so terribly > wrong > yesterday evening (especially once they get archived somewhere). I didn't set-up this portion of the file config. file and have not found much documentation for the conf-file formating (much of the rest is very well documented). I guess I should really find the actual code and read what it is doing... as Kent very gently prodded me with. I did go through a few PEPs too, and I don't recall seeing anything about time-based rotation ... but then maybe it was there and it didn't register with me. Thanks for the heads-up, I'll go back and re- read the material. > Looking at the code for the logging module, I think it will work to > create the RotatingFileHandler with the parameter delay=True, then in > your code get the handler and change its baseFilename attribute to be > the path you want, e.g. > "/home/user/workspace/logged_app/test_warn.log". That's an interesting feature, I'll have to go look and see what other jewels are lurking in various modules. So far I've not spent a lot of time going through the code for modules like this, but obviously I should (I often learn more than just how a bit of code works by reading how others have solved problems). Thank you both! Scott From ctcast at gmail.com Mon Apr 20 01:45:35 2009 From: ctcast at gmail.com (Chris Castillo) Date: Sun, 19 Apr 2009 18:45:35 -0500 Subject: [Tutor] Reading from files problem Message-ID: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> I am trying to open a text file and read multiple lines containing the following: 745777686,Alam,Gaus,CIS,15,30,25,15,5,31,15,48,70,97 888209279,Anderson,Judy,Math Ed,14,30,30,13,11,30,16,18,58,72 I want to ask the user for the student number, check to see if that number is even valid and display something like: last name, first name, student number, major time info was accessed(i know i need to import time module) exam scores homework scores average score for the class #Open and read text file gradesfile = open("grades.dat", "rfor lines in gradesfile: lines = lines.split(",") identifier = lines[0] lastname = lines[1] firstname = lines[2] major = lines[3] hw1 = lines[4] hw2 = lines[5] hw3 = lines[6] hw4 = lines[7] hw5 = lines[8] hw6 = lines[9] hw7 = lines[10] test1 = lines[11] test2 = lines[12] test3 = lines[13] totalpoints = 550 hwgrades = float(hw1) + float(hw2) + float(hw3) + float(hw4) + float(hw5) + float(hw6) + float(hw7) testgrades = float(test1) + float(test2) + float(test3) studentpoints = float(hwgrades) + float(testgrades) avgrades = round(float(studentpoints / totalpoints) * 100.0, 2) print identifier gradesfile.close() That's what I have so far but I have a feeling I shouldn't use a for loop. I would really like any help trying to figure out this program. Thanks in advance. From kent37 at tds.net Mon Apr 20 03:11:22 2009 From: kent37 at tds.net (Kent Johnson) Date: Sun, 19 Apr 2009 21:11:22 -0400 Subject: [Tutor] Python logging In-Reply-To: <4748C9BC-D25A-4DF1-B2D9-4927D93B0E98@rscorp.ab.ca> References: <2029995F-8F47-4650-94B3-15937BF45A2D@rscorp.ab.ca> <4748C9BC-D25A-4DF1-B2D9-4927D93B0E98@rscorp.ab.ca> Message-ID: <1c2a2c590904191811i28c9e62p3114818ed135ee43@mail.gmail.com> On Sun, Apr 19, 2009 at 3:18 PM, Scott SA wrote: > I guess I should really find the actual code and read what > it is doing... as Kent very gently prodded me with. It's in the Lib directory of your Python installation. > I did go through a few > PEPs too, and I don't recall seeing anything about time-based rotation ... > but then maybe it was there and it didn't register with me. See TimedRotatingFileHandler. http://docs.python.org/library/logging.html#timedrotatingfilehandler Kent From amonroe at columbus.rr.com Mon Apr 20 03:11:40 2009 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Sun, 19 Apr 2009 21:11:40 -0400 Subject: [Tutor] Reading from files problem In-Reply-To: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> Message-ID: <811456402836.20090419211140@columbus.rr.com> > gradesfile = open("grades.dat", "r > for lines in gradesfile: > [snip] > That's what I have so far but I have a feeling I shouldn't use a for > loop. Actually a for loop seems like the right tool for this job, assuming you really _do_ want to process every line in the file. Alan From ctcast at gmail.com Mon Apr 20 04:34:49 2009 From: ctcast at gmail.com (Chris Castillo) Date: Sun, 19 Apr 2009 21:34:49 -0500 Subject: [Tutor] Reading from files problem In-Reply-To: <811456402836.20090419211140@columbus.rr.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <811456402836.20090419211140@columbus.rr.com> Message-ID: <50e459210904191934r4ef5682axa9d3b75717ba8d09@mail.gmail.com> so how would I check to see if the student number entered by the user matches one of the student numbers in the file and then display that students info? On 4/19/09, R. Alan Monroe wrote: > >> gradesfile = open("grades.dat", "r >> for lines in gradesfile: >> [snip] >> That's what I have so far but I have a feeling I shouldn't use a for >> loop. > > Actually a for loop seems like the right tool for this job, assuming > you really _do_ want to process every line in the file. > > Alan > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From pydevrsc at gmail.com Mon Apr 20 04:36:21 2009 From: pydevrsc at gmail.com (Scottsa) Date: Sun, 19 Apr 2009 20:36:21 -0600 Subject: [Tutor] Reading from files problem In-Reply-To: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> Message-ID: <1D06B594-F33C-4F8C-A94B-D589B71608B5@gmail.com> Hi Chris, On Apr 19, 2009, at 5:45 PM, Chris Castillo wrote: > I am trying to open a text file and read multiple lines containing the > following: > > 745777686,Alam,Gaus,CIS,15,30,25,15,5,31,15,48,70,97 > 888209279,Anderson,Judy,Math Ed,14,30,30,13,11,30,16,18,58,72 This is CSV format data (comma separated values), and python has a very nice library for reading it. It does all of the heavy-lifting (file-handling) and splits the lines into lists. something like this: import csv studentReader = csv.reader(open('students.csv'), 'rb') for student in studentReader: student_values = do_something(student) > I want to ask the user for the student number, check to see if that > number is even valid and display something like: .../snip/... > totalpoints = 550 > hwgrades = float(hw1) + float(hw2) + float(hw3) + float(hw4) + > float(hw5) + float(hw6) + float(hw7) > testgrades = float(test1) + float(test2) + float(test3) > studentpoints = float(hwgrades) + float(testgrades) > avgrades = round(float(studentpoints / totalpoints) * 100.0, 2) This could be done with the 'reduce' built-in library function, combined with list slicing: and hwgrades = reduce(lambda x, y: float(x) + float(y), student_values[5:8]) ... which translates into columns #6, #7 and #8 (remember, lists are 0=based) You could write a simple method that does the reduction for you: def reduce_numbers(number_list): ret_val = 0.0 for val in number_list: ret_val += float(val) return ret_val Then you'd call it like this: some_result = reduce_numbers(student[5:8] lamda defines a method, just like 'def' does, but does not name it because it is used and disposed of immediately. So, "do_something" could look like this (not tested or debugged): def do_something(student_values, sought_id): student_id = lines[0] if sought_id == student_id: last_name = lines[1] first_name = lines[2] major = lines[3] hw_grades = reduce(lambda x, y: float(x) + float(y), student_values[5:8]) test_grades = reduce(lambda x, y: float(x) + float(y), student_values[8:10]) ...[other code you may wish to perform]... print first_name, last_name, identifier Note: I didn't count the cols so my 'split' numbers are not correct, but you get the idea. You could even move the testing phase to the reading like this: sought_id = 888209279 # however you are going to obtain this i.e. from sys.argv or otherwise. for student in studentReader: if student[0] == sought_id: doSomething(student) Please also check-out string formatting here: It allows you to make prettier output if you like and find it easier to read than a lot of string joins (val1 + val2 + ' - ' + val3... yuck!) '%s, %s (%s): %0.2f points' % (last_name, first_name, student_id, points) 'Doe, Jane (888209279): 76.22 points' > That's what I have so far but I have a feeling I shouldn't use a for > loop. I would really like any help trying to figure out this program. > Thanks in advance. If you are doing a lot of querying of the data, you could use sqlite which would mean you iterate the file once into the database and you can use sql to perform the lookup. You'd have to create the schema and use the student ID as the primary key. You could also pre-process the values on import, so the results would be pretty simple to obtain. select student_name, test_grades, student_points, avg_grades from studentdb where student_id = '888209279'; would return the values in a list. You could also perform searches by last-name (all the "smith's", for example, or partial names or... I realize that is a bit over what you were starting with, but it is likely another logical step in your venture... Okay, I hope this is clear enough to get you over a couple of points, Scott From alan.gauld at btinternet.com Mon Apr 20 08:59:15 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Apr 2009 07:59:15 +0100 Subject: [Tutor] Reading from files problem References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> Message-ID: "Chris Castillo" wrote >I am trying to open a text file and read multiple lines containing the > following: > > 745777686,Alam,Gaus,CIS,15,30,25,15,5,31,15,48,70,97 > 888209279,Anderson,Judy,Math Ed,14,30,30,13,11,30,16,18,58,72 You could use the CSV module for this, although a basic for loop is fine too if the data is this simple. > I want to ask the user for the student number, check to see if that > number is even valid and display something like: You might want to store the data in a dictionary keyed by ID number? > #Open and read text file > > gradesfile = open("grades.dat", "r" > for lines in gradesfile: > lines = lines.split(",") > identifier = lines[0] > lastname = lines[1] > firstname = lines[2] > major = lines[3] OK so far > hw1 = lines[4] > hw2 = lines[5] > hw3 = lines[6] > hw4 = lines[7] > hw5 = lines[8] > hw6 = lines[9] > hw7 = lines[10] You could store these in a list using list slicing, and convert to numbers using a list comprehension hw = [float(n) for n in lines[4:11]] > test1 = lines[11] > test2 = lines[12] > test3 = lines[13] And here: test = [float(n) for n in lines[11:14]] > totalpoints = 550 > hwgrades = float(hw1) + float(hw2) + float(hw3) + float(hw4) + > float(hw5) + float(hw6) + float(hw7) You could then use sum() to get the totals hwgrades = sum(hw) > testgrades = float(test1) + float(test2) + float(test3) testgrades = sum(test) > studentpoints = float(hwgrades) + float(testgrades) you don;t need the flooat conversions because you already did that above. > avgrades = round(float(studentpoints / totalpoints) * 100.0, 2) same here. > print identifier > > > gradesfile.close() > That's what I have so far but I have a feeling I shouldn't use a for > loop. I would really like any help trying to figure out this program. > Thanks in advance. Thats all fine for reading one stiudent, but you overwrite the data each time through the loop! This also looks like an obvious use for a class so I'd create a Student class to hold all the data (You could create methods to do the totals/averages too, plus add a __str__ method to print the student data in the format required- I'll leave that as an excercise for the reader!)) So I'd change the structure to be like this(pseudo code) students = dict() # empty dict for line in gradesfile: line = line.split(',') s = Student() s.id = line[0] s.lastname = line[1] etc.... s.hwtotal = sum(hw) etc.... students[s.id] = s Now you can access/print any student record with id = raw_input('type an id number') s = students[id] print s Finally you could put the for loop inside the Student class __init__ method so it boils down to: for line in gradesfile: s = Student(line) students[s.id] = s In fact you could cram it all into one line with another list comprehension, but that seems like a step too far to me... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Mon Apr 20 09:57:33 2009 From: denis.spir at free.fr (spir) Date: Mon, 20 Apr 2009 09:57:33 +0200 Subject: [Tutor] Reading from files problem In-Reply-To: References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> Message-ID: <20090420095733.24bf2750@o> Le Mon, 20 Apr 2009 07:59:15 +0100, "Alan Gauld" s'exprima ainsi: > > #Open and read text file > > > > gradesfile = open("grades.dat", "r" > > for lines in gradesfile: > > lines = lines.split(",") > > identifier = lines[0] > > lastname = lines[1] > > firstname = lines[2] > > major = lines[3] > > OK so far Not OK for me. The structure is the one of a table. A variety of words/concepts/metaphors can properly describe a table structure. But the above choice is certainly wrong: It says that a table row/record is a 'lines' *and* is made of 'lines'! Possible alternatives: for record in gradesfile: fields = record.split(",") for row in gradesfile: cells = row.split(",") for line in gradesfile: data = line.split(",") # 'data' is supposed to be plural of 'datum' (?) Denis ------ la vita e estrany From denis.spir at free.fr Mon Apr 20 10:17:41 2009 From: denis.spir at free.fr (spir) Date: Mon, 20 Apr 2009 10:17:41 +0200 Subject: [Tutor] Reading from files problem In-Reply-To: References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> Message-ID: <20090420101741.42eab7f1@o> Le Mon, 20 Apr 2009 07:59:15 +0100, "Alan Gauld" s'exprima ainsi: > So I'd change the structure to be like this(pseudo code) > > students = dict() # empty dict > for line in gradesfile: > line = line.split(',') > s = Student() > s.id = line[0] > s.lastname = line[1] > etc.... > s.hwtotal = sum(hw) > etc.... > students[s.id] = s If a student type is created, then it's worth having a readDataLine() method (whatever its name) to feed it: students = dict() # empty dict for line in gradestable: # not a file, but data read from it line = line.split(',') student = Student() student.readDataLine(line) id = line[0] students[id] = student [Actually, you wouldn't even need splitting the line before passing it, except here its also a practicle way to extract the id to use it as dict key.] If ever this is the standard, or even only, way to feed a student object with data, then the data-line-reading method may be __init__ itself. So that the loop becomes: for line in gradestable: line = line.split(',') student = Student(line) id = line[0] students[id] = student Denis ------ la vita e estrany From pydev at rscorp.ab.ca Mon Apr 20 11:03:09 2009 From: pydev at rscorp.ab.ca (Scott SA) Date: Mon, 20 Apr 2009 03:03:09 -0600 Subject: [Tutor] Reading from files problem In-Reply-To: References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> Message-ID: <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> On Apr 20, 2009, at 12:59 AM, Alan Gauld wrote: > You might want to store the data in a dictionary keyed by ID number? I had thought of suggesting this, but it appeared that the OP was going to re-iterate the file each time he wished to query the CSV. May have been a bad assumption on my part as I envisioned pickling a dict. and that just got too complicated. > test = [float(n) for n in lines[11:14]] > hwgrades = sum(hw) The composite of this would be: sum([float(n) for n in lines[11:14]]) ... which, I agree, is easier on the eyes/brain than the reduce(lambda:...) example I gave. sum is also on along with with range and other built-ins. Chris: wrapping the for-loop in square-brackets is part of list comprehension, found here (part 5.1.4) > Thats all fine for reading one stiudent, but you overwrite the data > each time through the loop! This also looks like an obvious use for > a class so I'd create a Student class to hold all the data > (You could create methods to do the totals/averages too, plus add a > __str__ method to print the student data in the format required- > I'll leave that as an excercise for the reader!)) This part is actually the reason I've replied, everything before this was 'just along the way'. Classes are a big subject for starting out, here are the main docs. Also, check out 'dive into python' and others for help in getting a handle on that. I figured that the Student class proposed probably needed an example to get over the initial hurdle. class Student(object): def __init__(self) pass In its most basic form, this is pretty much the 'hello world' for classes. > So I'd change the structure to be like this(pseudo code) > > students = dict() # empty dict > for line in gradesfile: > line = line.split(',') > s = Student() This step creates an instance of the class. Just for the moment, think of it as a fancy variable -- how python will store and reference the live data. In the end, you would need a class-instance for each and every student (line of the file). > s.id = line[0] And this adds an 'id' attribute to the class Pre-defined in the class, this would look like: class Student(object): def __init__(self) self.id = None When the instance is created, the id has None as its value (or anything you wanted). The "self" reference means the instance of the class itself, more on that in a moment. Still accessed the same as above: s.id = n > s.lastname = line[1] > etc.... > s.hwtotal = sum(hw) > etc.... > students[s.id] = s As mentioned, id, lastname, hwtotal, etc. become attributes of the class. Nothing terribly magical, they are actually stored in a dictionary (i.e. s.__dict__) and long-hand access would be: s.__dict__['id'] So, the next step to this would be to actually use the class to do the heavy lifting. This is what Alan is talking about a bit further down. class Student(object): def __init__(self, csv_data): csv_list = csv_data.split(',') self.id = csv_list[0] ... self. hwgrades = self._listFloats(csv_list[4:10]) def _list_floats(self, str_list): return [float(n) for n in str_list] def hw_grade_total(self): sum(self.hwgrades) The two methods are part of the business-logic of the class - notice internally they are accessed by 'self'. This is very important, so python knows what data to work with. Assuming you're not using the CSV library or already have the row/line from the file as a list: for student_data in grades_file: s = Student(student_data) student_dict[s.id] = s So, when python creates the class instance, it calls the __init__ method. Since you've passed it a list of student data, it processes it at the same time. In this example, it will cause an error if you don't pass any data, by the way. You might need to consider verifying that each line has the correct number of fields otherwise an error could be generated. Accessing the grade total method is like this: grade_total = s.hw_grade_total() Or, if you want the raw list of floats: grade_list = s.hwgrades I still contend that all of this is ideal for a database, like SQLite, which would allow for searching by name as well as ID, etc. It is the persistence of data that motivates this perspective. So what I would do is make a class Students, with a file-import method using the CSV lib which then did the work of putting all the data into a database, bypassing a Student class (until there was a valid reason for one). Once the data is loaded in, it can be referenced without re- interpreting the CSV file, again through methods in a Students class. I hope this helps, Scott PS. My email is acting up, did my prev. message actually make it to the list? From denis.spir at free.fr Mon Apr 20 15:05:28 2009 From: denis.spir at free.fr (spir) Date: Mon, 20 Apr 2009 15:05:28 +0200 Subject: [Tutor] vars() Message-ID: <20090420150528.25ae0fbe@o> Hello, Just discovered vars(). Looking for the reasons and use cases that led to introduce this builtin. But could not find any reference. Any pointer/clue? Denis PS: same about all() and any() -- but here I found some literature... in french: fr.wikipedia.org/wiki/Python_(langage) ------ la vita e estrany From alan.gauld at btinternet.com Mon Apr 20 15:24:37 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Apr 2009 14:24:37 +0100 Subject: [Tutor] Reading from files problem References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> Message-ID: "Scott SA" wrote > May have been a bad assumption on my part as I envisioned pickling a > dict. and that just got too complicated. A pickled dict? That would be a shelve mebbe? > PS. My email is acting up, did my prev. message actually make it to > the list? Yes, I saw it just after sending mine... Alan G From padhu.47 at gmail.com Mon Apr 20 15:30:04 2009 From: padhu.47 at gmail.com (Padmanaban Ganesan) Date: Mon, 20 Apr 2009 19:00:04 +0530 Subject: [Tutor] vars() In-Reply-To: <20090420150528.25ae0fbe@o> References: <20090420150528.25ae0fbe@o> Message-ID: <8aa4df020904200630m7c34c989h55d6897108b4fa97@mail.gmail.com> Hello Denis, Please find more details , Use cases about the Built-in in the following book under the *Chapter 9* - Oops, Creating modules. *Book Name :* Python Programming for the Absolute Beginner *Author:* Michael Dawson Ta, Paddy 2009/4/20 spir > Hello, > > Just discovered vars(). Looking for the reasons and use cases that led to > introduce this builtin. But could not find any reference. > Any pointer/clue? > > Denis > > PS: same about all() and any() -- but here I found some literature... in > french: > fr.wikipedia.org/wiki/Python_(langage) > > ------ > la vita e estrany > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Ta, Paddy "The Secret to creativity is knowing how to hide your sources " -------------- next part -------------- An HTML attachment was scrubbed... URL: From dextrous85 at gmail.com Mon Apr 20 15:40:47 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Mon, 20 Apr 2009 19:10:47 +0530 Subject: [Tutor] Webpy vs Django In-Reply-To: <2BAB60A0-E2FF-4803-A4D3-71293290AB77@rscorp.ab.ca> References: <5487b3060904190352h1e55b759y71cf7b12955f3c89@mail.gmail.com> <2BAB60A0-E2FF-4803-A4D3-71293290AB77@rscorp.ab.ca> Message-ID: <5487b3060904200640g57c0c4e8me4e18ef8cc869b22@mail.gmail.com> Thanks for your elaborate reply. On Mon, Apr 20, 2009 at 12:47 AM, Scott SA wrote: > On Apr 19, 2009, at 4:52 AM, vishwajeet singh wrote: > > This is not to flame any war; I just wanted to know the key features to >> consider among the two web frame works. >> >> What advantage and disadvantages you have when you decide using any one of >> them. >> > > I can't speak with any authority on Webpy (webpy.org), as I have not > actually used it though I did read up on its features about a year ago and > now just a quick glance again. I do have some experience with a different > 'light-weight' framework called Quixote (www.quixote.ca) and have a > production project using it... that I'm migrating to Django. I also have a > couple of projects using Zope... also being migrated to Django. I don't > mention all of this as any indication of prowess, in fact much of the code > on these projects has been written by other developers I've contracted to, > but that I have some similar experience. > > The main point I wish to make of all this is that these different > frameworks kind of coalesce into sub-groups. They sort-of form into three > groups: light-weight framework, full-featured framework and heavy CMS (lots > of inseparable overhead). Furthermore, like a series of balls or coins in a > simple Pachinko machine (http://en.wikipedia.org/wiki/Pachinko) where each > one takes a different route, the application you are developing will have a > huge influence on requirements. > > Personally, I've been working with Django on and off for about 2-1/2 years > and find it a joy to work with. It does not have the overhead and hidden > 'magic' of Zope and I don't have to re-invent almost everything like > Quixote. For _my_ needs, it has been a great blend of capabilities without > the oppressing overhead of the CMS. I can add or remove the modules needed > on a project-by-project basis. > > "Out of the box", the automated admin interface is a huge time-saver and > its object-relational model is quite good at wrapping relational databases > into a python-friendly framework. Also it's user-admin and authentication > takes care of the core of most sites I work on. It has a good blend for my > requirements of granular framework access and functional tools to save me > time. And now, there is quite a nice selection of add-on applications that > extend Django in a very modular fashion, though there are still growing > pains. Like "Goldie Locks and the three bears", this one is "just right" for > my needs. > > Just as Zope is overkill for projects that don't need CMS, Django would be > overkill for a project that didn't need much for database access and admin > interface. The authors of Django, try pretty hard to not hide anything that > is going on in the background but there is still a lot being done for the > developer. For example in creating db schemas, wrapping queries and > result-sets into object calls, rendering content through templates and much > more. All have ways of being modified but not necessarily by novice python > developers. > > So the short answer to your question is that Webpy and Django are quite > different in their tool-sets and are therefore tough to compare. As with all > lightweight frameworks, they are _shorter_ on pre-defined features. That's > not a negative, that _is_ the point of them. Django has tried to bridge, and > in my opinion done a good job of it, a lighter-weight core with a well > documented and developer-friendly interface for extension. It is a framework > that has allowed me to gradually dig deeper and deeper into its workings to > extract more advanced features while not bogging me down with redundant and > tedious things like forms and user authentication. > > I chose Django because it had the most to offer for what I need and it > seems to have the "legs" to cover a lot of ground. That said, I don't that > anyone could say you were wrong by choosing either of the projects you > mentioned given appropriate circumstances (though personally, I can't find > _any_ circumstances where I'd recommend Zope today -- but that's probably a > personal bias and I won't digress). > > I know this doesn't answer your question directly but I hope it helps > broaden the perspective a little, > > Scott > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From HellZFury+Python at gmail.com Mon Apr 20 15:48:30 2009 From: HellZFury+Python at gmail.com (Matt) Date: Mon, 20 Apr 2009 09:48:30 -0400 Subject: [Tutor] Looping Message-ID: Hey everyone, First post to this list. I hope I'm doing it right. Let's say I want to run func 10 times Is there a more pythonic way to do it than this: for i in xrange(10): func() Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From HellZFury+Python at gmail.com Mon Apr 20 16:46:47 2009 From: HellZFury+Python at gmail.com (Matt) Date: Mon, 20 Apr 2009 10:46:47 -0400 Subject: [Tutor] String Encoding problem Message-ID: Hey everyone, I'm hoping someone here can help me solve an odd problem (bug?). I'm having trouble with string encoding, object deletion, and the xml.etree library. If this isn't the right list to be posting this question, please let me know. I'm new to Python and don't know of any other "help me" Python mailing lists. I have tried debugging this ad-infinitem. Anyway, at the bottom of this e-mail you will find the code of a python file. This is a gross over-simplification of my code, with little exception handling so that the errors are obvious. Running this interactively, if you finish off with 'del db', it exits fine and creates a skeleton xml file called 'db.xml' with text ''. However, if you instead CTRL-D, it throws at exception while quitting and then leaves an empty 'db.xml' which won't work. Can anyone here help me figure out why this is? Stuff I've done: I've traced this down to the self.commit() call in __del__. The stacktrace and a few print statements injected into xml.etree leads me to the call 'root'.encode('us-ascii') throwing a LookupError on line 751 of xml.etree.ElementTree. This makes no sense to me, since it works fine normally. Thank you very much. Any and all help or pointers are appreciated. ~Matt #### db.py ### from xml.etree import ElementTree as ET import os class Database(object): def __init__(self, path): self.__dbpath = path ## Path to the database self.load() def __del__(self): ## FIXME: Known bug: ## del db at command line works properly ## Ctrl-D, when there is no db file present, results in a LookupError ## and empty xml file from StringIO import StringIO from traceback import print_exc trace = StringIO() try: print 5 self.commit() print 7 except Exception: print_exc(100, trace) print trace.getvalue() def load(self): if os.path.exists(self.__dbpath): self.root = ET.parse(self.__dbpath).getroot() else: self.root = ET.Element("root") def commit(self): ET.ElementTree(self.root).write(self.__dbpath) db = Database('db.xml') -------------- next part -------------- An HTML attachment was scrubbed... URL: From srilyk at gmail.com Mon Apr 20 16:52:18 2009 From: srilyk at gmail.com (W W) Date: Mon, 20 Apr 2009 09:52:18 -0500 Subject: [Tutor] Looping In-Reply-To: References: Message-ID: <333efb450904200752g446ae071u2456a22a536ef11e@mail.gmail.com> On Mon, Apr 20, 2009 at 8:48 AM, Matt > wrote: > Hey everyone, > > First post to this list. I hope I'm doing it right. > > Let's say I want to run func 10 times Is there a more pythonic way to do it > than this: > for i in xrange(10): > func() AFAIK that's the most pythonic way to do it... and probably how I'd do it... if I had a function I needed to run 10 times :P -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From a.t.hofkamp at tue.nl Mon Apr 20 17:26:30 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Mon, 20 Apr 2009 17:26:30 +0200 Subject: [Tutor] Looping In-Reply-To: References: Message-ID: <49EC9426.30709@tue.nl> Matt wrote: > Hey everyone, > > First post to this list. I hope I'm doing it right. > > Let's say I want to run func 10 times Is there a more pythonic way to do it than this: > for i in xrange(10): > func() no, that looks fine for your requirement. What may be a bit weird here is the requirement to run the same function 10 times without further input/output parameters. I have never needed such a construct. What kind of function do you have that needs to be called 10 times? If you are doing timing experiments, the timeit library has a way of repeating the same experiment a number of times. Albert From dineshbvadhia at hotmail.com Mon Apr 20 18:07:01 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Mon, 20 Apr 2009 09:07:01 -0700 Subject: [Tutor] parse text for paragraghs/sections Message-ID: Hi! I want to parse text and pickup sections. For example, from the text: t = """abc
 DEF ghi jkl 
MNO pqr""" ... pickup all text between the tags
 and 
and replace with another piece of text. I tried t = re.sub(r"\
[A-Za-z0-9]\
", "DBV", t) ... but it doesn't work. How do you do this with re? Thanks Dinesh -------------- next part -------------- An HTML attachment was scrubbed... URL: From emadnawfal at gmail.com Mon Apr 20 18:38:53 2009 From: emadnawfal at gmail.com (=?windows-1256?B?RW1hZCBOYXdmYWwgKNrjx88g5Obd4Sk=?=) Date: Mon, 20 Apr 2009 12:38:53 -0400 Subject: [Tutor] py-editdist Message-ID: <652641e90904200938v55fcdbfand9aaf82c640c7ad@mail.gmail.com> Hi Tutors, I tried to install this edit distance module but failed: http://www.mindrot.org/projects/py-editdist/ I'm running Ubuntu Linux. The error message is below. Your help appreciated emad at emad-desktop:~/Desktop/py-editdist-0.3$ python setup.py build running build running build_ext building 'editdist' extension gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c editdist.c -o build/temp.linux-i686-2.5/editdist.o editdist.c:22:20: error: Python.h: No such file or directory editdist.c:91: error: expected ?)? before string constant editdist.c:94: error: expected ?=?, ?,?, ?;?, ?asm? or ?__attribute__? before ?*? token editdist.c:110: error: expected ?=?, ?,?, ?;?, ?asm? or ?__attribute__? before ?editdist_methods? editdist.c:116: error: expected ?)? before string constant editdist.c:119: error: expected ?=?, ?,?, ?;?, ?asm? or ?__attribute__? before ?initeditdist? error: command 'gcc' failed with exit status 1 -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Mon Apr 20 18:44:50 2009 From: denis.spir at free.fr (spir) Date: Mon, 20 Apr 2009 18:44:50 +0200 Subject: [Tutor] Looping In-Reply-To: <49EC9426.30709@tue.nl> References: <49EC9426.30709@tue.nl> Message-ID: <20090420184450.372f2ed2@o> Le Mon, 20 Apr 2009 17:26:30 +0200, "A.T.Hofkamp" s'exprima ainsi: > Matt wrote: > > Hey everyone, > > > > First post to this list. I hope I'm doing it right. > > > > Let's say I want to run func 10 times Is there a more pythonic way to do > > it than this: for i in xrange(10): > > func() > > no, that looks fine for your requirement. > > What may be a bit weird here is the requirement to run the same function 10 > times without further input/output parameters. I have never needed such a > construct. > What kind of function do you have that needs to be called 10 times? Actually, it's even rare to have to run a func n times, where n is known at runtime only. Builtin iteration (for item in container) removes 99% (about ;-) of such needs. Denis ------ la vita e estrany From mwalsh at mwalsh.org Mon Apr 20 19:17:20 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Mon, 20 Apr 2009 12:17:20 -0500 Subject: [Tutor] Sending a disconnect after openssl s_client command? In-Reply-To: References: Message-ID: <49ECAE20.2000600@mwalsh.org> Kayvan Sarikhani wrote: > Tutors, > > I'm working on a script to verify whether a particular website > supports SSLv2 via the following: > > --- BEGIN --- > #!/usr/bin/python > import os, re > > checkssl_out = open('checkssl.txt','w') > > website = 'somewebsitename' > sslv2 = 'Protocol : SSLv2' > > print 'Checking:', website > > checksslv2 = os.popen('openssl s_client -ssl2 -connect > somewebsitename:443').read().strip() > > if re.search(sslv2, checksslv2) == None: > print >> checkssl_out, website, 'does NOT support SSLv2' > else: > print >> checkssl_out, website, 'supports: SSLv2' > > checkssl_out.close() > --- END --- > > It works, but the problem is that OpenSSL does not automatically > disconnect after end of input. I was curious if there's a way to send a > CTRL-C at the end of the command, so that it *does* capture the output, > and breaks after it. Any suggestions or help is appreciated! You can do something like the following (untested) to simulate a CTRL-C, but I'd recommend against it, as I doubt it will work as you expect ... import os, signal from subprocess import Popen, PIPE openssl_cmd = 'openssl s_client -ssl2 -connect somewebsitename:443' openssl = Popen(openssl_cmd, shell=True, stdout=PIPE, stderr=PIPE) os.kill(openssl.pid, signal.SIGINT) # dead, I bet, before any output is generated stdout, stderr = openssl.communicate() Instead, you may want to try to mimic this command-line behavior ... echo "GET /" | openssl s_client -ssl2 -connect somewebsitename:443 ... in which case, you can try something like this ... from subprocess import Popen, PIPE openssl_cmd = 'openssl s_client -ssl2 -connect somewebsitename:443' openssl = Popen( openssl_cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE ) stdout, stderr = openssl.communicate('GET /') Alternatively, if you're using python 2.6 and above, it looks like you can do something similar with a few lines of code, and the ssl module from the standard lib ... # untested! import ssl try: cert = ssl.get_server_certificate( ('somewebsitename', 443), ssl.PROTOCOL_SSLv2 ) except ssl.SSLError, ex: # site may not support sslv2 ... HTH, Marty > > K > From denis.spir at free.fr Mon Apr 20 19:22:59 2009 From: denis.spir at free.fr (spir) Date: Mon, 20 Apr 2009 19:22:59 +0200 Subject: [Tutor] String Encoding problem In-Reply-To: References: Message-ID: <20090420192259.052cb091@o> Le Mon, 20 Apr 2009 10:46:47 -0400, Matt s'exprima ainsi: > Hey everyone, > > I'm hoping someone here can help me solve an odd problem (bug?). I'm having > trouble with string encoding, object deletion, and the xml.etree library. If > this isn't the right list to be posting this question, please let me know. > I'm new to Python and don't know of any other "help me" Python mailing > lists. I have tried debugging this ad-infinitem. Anyway, at the bottom of > this e-mail you will find the code of a python file. This is a gross > over-simplification of my code, with little exception handling so that the > errors are obvious. > > Running this interactively, if you finish off with 'del db', it exits fine > and creates a skeleton xml file called 'db.xml' with text ''. > However, if you instead CTRL-D, it throws at exception while quitting and > then leaves an empty 'db.xml' which won't work. Can anyone here help me > figure out why this is? > > Stuff I've done: > I've traced this down to the self.commit() call in __del__. The stacktrace > and a few print statements injected into xml.etree leads me to the call > 'root'.encode('us-ascii') throwing a LookupError on line 751 of > xml.etree.ElementTree. This makes no sense to me, since it works fine > normally. > > Thank you very much. Any and all help or pointers are appreciated. > > ~Matt > > #### db.py ### > from xml.etree import ElementTree as ET > import os > > class Database(object): > def __init__(self, path): > self.__dbpath = path ## Path to the database > self.load() > def __del__(self): > ## FIXME: Known bug: > ## del db at command line works properly > ## Ctrl-D, when there is no db file present, results in a > LookupError > ## and empty xml file > from StringIO import StringIO > from traceback import print_exc > trace = StringIO() > try: > print 5 > self.commit() > print 7 > except Exception: > print_exc(100, trace) > print trace.getvalue() > def load(self): > if os.path.exists(self.__dbpath): > self.root = ET.parse(self.__dbpath).getroot() > else: > self.root = ET.Element("root") > def commit(self): > ET.ElementTree(self.root).write(self.__dbpath) > db = Database('db.xml') Actually, it all runs well for me -- after the following modification: def __del__(self): ## FIXME: Known bug: ## del db at command line works properly ## Ctrl-D, when there is no db file present, results in a LookupError ## and empty xml file try: print 5 self.commit() print 7 except Exception: raise Notes: * I don't know for what reason you needed such a complicated traceback construct. * Before I did this modif, I indeed had a weird exception about stringIO. * __del__() seems to do the contrary: it writes back to file through commit()??? * "del db" works fine, anyway * When I run without any bd.xml, it properly creates one with text "". * When I run with an ampty db.xml, I have the following exception message: Traceback (most recent call last): File "xmlTree.py", line 29, in db = Database('db.xml') File "xmlTree.py", line 10, in __init__ self.load() File "xmlTree.py", line 24, in load self.root = ET.parse(self.__dbpath).getroot() File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 862, in parse tree.parse(source, parser) File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 587, in parse self._root = parser.close() File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 1254, in close self._parser.Parse("", 1) # end of data xml.parsers.expat.ExpatError: no element found: line 2, column 0 5 Exception exceptions.AttributeError: AttributeError("'Database' object has no attribute 'root'",) in > ignored ------ la vita e estrany From alan.gauld at btinternet.com Mon Apr 20 19:27:03 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Apr 2009 18:27:03 +0100 Subject: [Tutor] Looping References: Message-ID: "Matt" wrote > Let's say I want to run func 10 times Is there a more pythonic way to do > it > than this: > for i in xrange(10): > func() Yes, use range() rather than xrange :-) But more seriously, as others have pointed out, if func() is well written then calling it ten times will have no effect over calling it once. (Except taking up 10 times as much time!) Its like saying for n in range(10): x = 42 The assignment is identical each time through the loop In fact it's worse than that because you don't even store the result of func() so it has no effect whatsoever on your program outside the loop. It would be more normal to do something like: for value in mycollection: result = func(somevalue) in other words apply func() to each value in a collection. So unless your func() really takes arguments or prints time related output or modifies global data (a bad practice) then what you are asking doesn't make much sense. Finally, if your function is really a program in disguise - ie it does all its own input/output then it would make more sense to put the loop inside the function and put the repeat count as a parameter: def func(repetitions): for n in range(repetitions): # your old func code here And call it: func(repetitions=10) # use keyword for clarity of purpose HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Apr 20 19:33:13 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Apr 2009 18:33:13 +0100 Subject: [Tutor] py-editdist References: <652641e90904200938v55fcdbfand9aaf82c640c7ad@mail.gmail.com> Message-ID: "Emad Nawfal (???? ????)" wrote > emad at emad-desktop:~/Desktop/py-editdist-0.3$ python setup.py build > running build > gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall > editdist.c:22:20: error: Python.h: No such file or directory Looks like you need the Python source code installed, do you have that? Alan G From HellZFury+Python at gmail.com Mon Apr 20 19:34:10 2009 From: HellZFury+Python at gmail.com (Matt) Date: Mon, 20 Apr 2009 13:34:10 -0400 Subject: [Tutor] Looping In-Reply-To: References: Message-ID: Thank you. Func is in fact a call to an external non-python program. =] Therefore, the loop must actually be mindlessly done. My question, however, has been answered. As an aside, why use range rather than xrange? I was under the impression that xrange is a generator and therefore more memory efficient. On Mon, Apr 20, 2009 at 1:27 PM, Alan Gauld wrote: > "Matt" > wrote > > Let's say I want to run func 10 times Is there a more pythonic way to do >> it >> than this: >> for i in xrange(10): >> func() >> > > Yes, use range() rather than xrange :-) > > But more seriously, as others have pointed out, if func() is well > written then calling it ten times will have no effect over calling > it once. (Except taking up 10 times as much time!) > > Its like saying > > for n in range(10): > x = 42 > > The assignment is identical each time through the loop > > In fact it's worse than that because you don't even store the > result of func() so it has no effect whatsoever on your program > outside the loop. It would be more normal to do something like: > > for value in mycollection: > result = func(somevalue) > > in other words apply func() to each value in a collection. > > So unless your func() really takes arguments or prints time > related output or modifies global data (a bad practice) then > what you are asking doesn't make much sense. > > Finally, if your function is really a program in disguise - ie it > does all its own input/output then it would make more sense > to put the loop inside the function and put the repeat count > as a parameter: > > def func(repetitions): > for n in range(repetitions): > # your old func code here > > And call it: > > func(repetitions=10) # use keyword for clarity of purpose > > HTH, > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Apr 20 19:31:01 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Apr 2009 18:31:01 +0100 Subject: [Tutor] parse text for paragraghs/sections References: Message-ID: "Dinesh B Vadhia" wrote in > ... pickup all text between the tags
 and 
and replace with > another piece of text. > How do you do this with re? Preferably you don't, use an HTML parser. It is both easier and more reliable since it is almost impossible to handle all HTML constructs reliably using regex. BeautifiulSoup is a good HTML parser although you can use the ones that are in the standard library too. Of course it may not be an HTML file in which case regex may or may not be appropriate, but there are several other parsing modules around, a quick search of the recent list archives will reveal several discussions. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From denis.spir at free.fr Mon Apr 20 19:46:31 2009 From: denis.spir at free.fr (spir) Date: Mon, 20 Apr 2009 19:46:31 +0200 Subject: [Tutor] parse text for paragraghs/sections In-Reply-To: References: Message-ID: <20090420194631.78053d77@o> Le Mon, 20 Apr 2009 09:07:01 -0700, "Dinesh B Vadhia" s'exprima ainsi: > t = """abc
 DEF ghi jkl 
MNO pqr""" > > ... pickup all text between the tags
 and 
and replace with > another piece of text. > > I tried > > t = re.sub(r"\
[A-Za-z0-9]\
", "DBV", t) > > ... but it doesn't work. You need: -1- Add ' ' to the character class. -2- Repete it with '*' or '+'. from re import compile as Pattern p = Pattern(r"
[A-Za-z0-9 ]*
") t = """abc
 DEF ghi jkl 
MNO pqr""" print p.sub('@', t) ==> abc @ MNO pqr Denis PS: I really wonder why sub() takes (1) the replacement string (2) the source text, as parameters. I always get caught by this (IMO weird) parameter order. And you get no error... only wrong result. ------ la vita e estrany From srilyk at gmail.com Mon Apr 20 19:50:56 2009 From: srilyk at gmail.com (W W) Date: Mon, 20 Apr 2009 12:50:56 -0500 Subject: [Tutor] Looping In-Reply-To: References: Message-ID: <333efb450904201050x6ccee5e8w678f7059cc24ce9a@mail.gmail.com> On Mon, Apr 20, 2009 at 12:34 PM, Matt > wrote: > Thank you. Func is in fact a call to an external non-python program. =] > Therefore, the loop must actually be mindlessly done. My question, however, > has been answered. > As an aside, why use range rather than xrange? I was under the impression > that xrange is a generator and therefore more memory efficient. > Probably because for small values (under 100), range just looks cleaner, and as far as memory goes it's not a whole lot of difference. AFAIK, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn?t. - Primo Levi -------------- next part -------------- An HTML attachment was scrubbed... URL: From emadnawfal at gmail.com Mon Apr 20 19:55:31 2009 From: emadnawfal at gmail.com (=?windows-1256?B?RW1hZCBOYXdmYWwgKNrjx88g5Obd4Sk=?=) Date: Mon, 20 Apr 2009 13:55:31 -0400 Subject: [Tutor] py-editdist In-Reply-To: References: <652641e90904200938v55fcdbfand9aaf82c640c7ad@mail.gmail.com> Message-ID: <652641e90904201055u102a9ff9o7b0d76ec6a54c574@mail.gmail.com> 2009/4/20 Alan Gauld > > "Emad Nawfal (???? ????)" wrote > > emad at emad-desktop:~/Desktop/py-editdist-0.3$ python setup.py build >> running build >> gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall >> editdist.c:22:20: error: Python.h: No such file or directory >> > > Looks like you need the Python source code installed, > do you have that? > > Alan G > Python comes with Ubuntu, or do you mean that I should re-install it from source code? > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From matthew.strax-haber at nasa.gov Mon Apr 20 19:49:07 2009 From: matthew.strax-haber at nasa.gov (Strax-Haber, Matthew (LARC-D320)) Date: Mon, 20 Apr 2009 12:49:07 -0500 Subject: [Tutor] String Encoding problem In-Reply-To: <20090420192259.052cb091@o> Message-ID: ________________________________ > From: spir > Date: Mon, 20 Apr 2009 12:22:59 -0500 > To: Python Tutor > Subject: Re: [Tutor] String Encoding problem > > Le Mon, 20 Apr 2009 10:46:47 -0400, > Matt s'exprima ainsi: > >> Hey everyone, >> >> I'm hoping someone here can help me solve an odd problem (bug?). I'm having >> trouble with string encoding, object deletion, and the xml.etree library. If >> this isn't the right list to be posting this question, please let me know. >> I'm new to Python and don't know of any other "help me" Python mailing >> lists. I have tried debugging this ad-infinitem. Anyway, at the bottom of >> this e-mail you will find the code of a python file. This is a gross >> over-simplification of my code, with little exception handling so that the >> errors are obvious. >> >> Running this interactively, if you finish off with 'del db', it exits fine >> and creates a skeleton xml file called 'db.xml' with text ''. >> However, if you instead CTRL-D, it throws at exception while quitting and >> then leaves an empty 'db.xml' which won't work. Can anyone here help me >> figure out why this is? >> >> Stuff I've done: >> I've traced this down to the self.commit() call in __del__. The stacktrace >> and a few print statements injected into xml.etree leads me to the call >> 'root'.encode('us-ascii') throwing a LookupError on line 751 of >> xml.etree.ElementTree. This makes no sense to me, since it works fine >> normally. >> >> Thank you very much. Any and all help or pointers are appreciated. >> >> ~Matt >> >> #### db.py ### >> from xml.etree import ElementTree as ET >> import os >> >> class Database(object): >> def __init__(self, path): >> self.__dbpath = path ## Path to the database >> self.load() >> def __del__(self): >> ## FIXME: Known bug: >> ## del db at command line works properly >> ## Ctrl-D, when there is no db file present, results in a >> LookupError >> ## and empty xml file >> from StringIO import StringIO >> from traceback import print_exc >> trace = StringIO() >> try: >> print 5 >> self.commit() >> print 7 >> except Exception: >> print_exc(100, trace) >> print trace.getvalue() >> def load(self): >> if os.path.exists(self.__dbpath): >> self.root = ET.parse(self.__dbpath).getroot() >> else: >> self.root = ET.Element("root") >> def commit(self): >> ET.ElementTree(self.root).write(self.__dbpath) >> db = Database('db.xml') > > Actually, it all runs well for me -- after the following modification: > > def __del__(self): > ## FIXME: Known bug: > ## del db at command line works properly > ## Ctrl-D, when there is no db file present, results in a LookupError > ## and empty xml file > try: > print 5 > self.commit() > print 7 > except Exception: > raise I must be missing something.... I run the following code (in DB.py) without any other files in the current directory: from xml.etree import ElementTree as ET import os class Database(object): def __init__(self, path): self.dbpath = path ## Path to the database self.load() def __del__(self): try: print 5 self.commit() print 7 except Exception: raise def load(self): if os.path.exists(self.dbpath): self.root = ET.parse(self.dbpath).getroot() else: self.root = ET.Element("root") def commit(self): ET.ElementTree(self.root).write(self.dbpath) db = Database('db.xml') Output: 5 Exception LookupError: LookupError('unknown encoding: us-ascii',) in > ignored If you're not getting the same output, please let me know what your environment is. Perhaps this is an implementation difference across platforms. > > Notes: > * I don't know for what reason you needed such a complicated traceback > construct. That was only to demonstrate the error. Without that, you see a LookupError without any trace. > * Before I did this modif, I indeed had a weird exception about stringIO. Top-level imports are not consistently available in __del__. That shouldn't be necessary with the code I have above. > * __del__() seems to do the contrary: it writes back to file through commit()??? Yes, I know. In my actual code, there is a flag that is set when certain run-time conditions are met or when the user wants the DB to be saved on quit. Most of the time, however, modifications to the database need to be done in memory because they are not intended to be saved. > * "del db" works fine, anyway > * When I run without any bd.xml, it properly creates one with text "". > * When I run with an ampty db.xml, I have the following exception message: > > Traceback (most recent call last): > File "xmlTree.py", line 29, in > db = Database('db.xml') > File "xmlTree.py", line 10, in __init__ > self.load() > File "xmlTree.py", line 24, in load > self.root = ET.parse(self.__dbpath).getroot() > File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 862, in parse > tree.parse(source, parser) > File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 587, in parse > self._root = parser.close() > File "/usr/lib/python2.5/xml/etree/ElementTree.py", line 1254, in close > self._parser.Parse("", 1) # end of data > xml.parsers.expat.ExpatError: no element found: line 2, column 0 > 5 > Exception exceptions.AttributeError: AttributeError("'Database' object has no > attribute 'root'",) in object at 0xb7e78fec>> ignored > > ------ > la vita e estrany > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -- ~Matthew Strax-Haber National Aeronautics and Space Administration Langley Research Center (LaRC) Co-op, Safety-Critical Avionics Systems Branch Matthew.Strax-Haber at nasa.gov -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Apr 20 20:13:13 2009 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Mon, 20 Apr 2009 18:13:13 +0000 (GMT) Subject: [Tutor] py-editdist In-Reply-To: <652641e90904201055u102a9ff9o7b0d76ec6a54c574@mail.gmail.com> References: <652641e90904200938v55fcdbfand9aaf82c640c7ad@mail.gmail.com> <652641e90904201055u102a9ff9o7b0d76ec6a54c574@mail.gmail.com> Message-ID: <679064.70165.qm@web86710.mail.ird.yahoo.com> I mean you need a source installation not a binary one, it is looking for python.h which is one of the C header files in the Python source code. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ ________________________________ From: Emad Nawfal (???? ????) To: Alan Gauld Cc: tutor at python.org Sent: Monday, 20 April, 2009 6:55:31 PM Subject: Re: [Tutor] py-editdist 2009/4/20 Alan Gauld "Emad Nawfal (???? ????)" wrote emad at emad-desktop:~/Desktop/py-editdist-0.3$ python setup.py build running build gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall editdist.c:22:20: error: Python.h: No such file or directory Looks like you need the Python source code installed, do you have that? Alan G Python comes with Ubuntu, or do you mean that I should re-install it from source code? _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From emadnawfal at gmail.com Mon Apr 20 20:16:36 2009 From: emadnawfal at gmail.com (=?windows-1256?B?RW1hZCBOYXdmYWwgKNrjx88g5Obd4Sk=?=) Date: Mon, 20 Apr 2009 14:16:36 -0400 Subject: [Tutor] py-editdist In-Reply-To: <679064.70165.qm@web86710.mail.ird.yahoo.com> References: <652641e90904200938v55fcdbfand9aaf82c640c7ad@mail.gmail.com> <652641e90904201055u102a9ff9o7b0d76ec6a54c574@mail.gmail.com> <679064.70165.qm@web86710.mail.ird.yahoo.com> Message-ID: <652641e90904201116j3f771af0n4064287b220c410d@mail.gmail.com> On Mon, Apr 20, 2009 at 2:13 PM, ALAN GAULD wrote: > I mean you need a source installation not a binary one, it is looking for > python.h > which is one of the C header files in the Python source code. > > Alan Gauld > Author of the Learn To Program website > http://www.alan-g.me.uk/ > > > ------------------------------ > *From:* Emad Nawfal (???? ????) > *To:* Alan Gauld > *Cc:* tutor at python.org > *Sent:* Monday, 20 April, 2009 6:55:31 PM > *Subject:* Re: [Tutor] py-editdist > > > > 2009/4/20 Alan Gauld > >> >> "Emad Nawfal (???? ????)" wrote >> >> emad at emad-desktop:~/Desktop/py-editdist-0.3$ python setup.py build >>> running build >>> gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall >>> editdist.c:22:20: error: Python.h: No such file or directory >>> >> >> Looks like you need the Python source code installed, >> do you have that? >> >> Alan G >> > Python comes with Ubuntu, or do you mean that I should re-install it from > source code? > >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > > Thank you Alan,, > Problem solved. I installed the Python-dev packages and everything worked fine. > > -- > ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? > ??????? > "No victim has ever been more repressed and alienated than the truth" > > Emad Soliman Nawfal > Indiana University, Bloomington > -------------------------------------------------------- > -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Mon Apr 20 20:29:46 2009 From: denis.spir at free.fr (spir) Date: Mon, 20 Apr 2009 20:29:46 +0200 Subject: [Tutor] list about PL design Message-ID: <20090420202946.597e0db4@o> Hello, just started a mailing list about programming language design. It's called 'PiLuD' ;-) For anyone interested. subscribe : pilud+subscribe at googlegroups.com send mail : pilud at googlegroups.com home page : http://groups.google.com/group/pilud You'll find a list of sample topics, only intended to help the list start & live, at http://groups.google.com/group/pilud/web/topics Add your own center of interests. Denis ------ la vita e estrany -------------- next part -------------- An HTML attachment was scrubbed... URL: From mwalsh at mwalsh.org Mon Apr 20 22:12:56 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Mon, 20 Apr 2009 15:12:56 -0500 Subject: [Tutor] String Encoding problem In-Reply-To: References: Message-ID: <49ECD748.6050306@mwalsh.org> Matt wrote: > Hey everyone, > > I'm hoping someone here can help me solve an odd problem (bug?). I'm > having trouble with string encoding, object deletion, and the xml.etree > library. If this isn't the right list to be posting this question, > please let me know. I'm new to Python and don't know of any other "help > me" Python mailing lists. I have tried debugging this ad-infinitem. > Anyway, at the bottom of this e-mail you will find the code of a python > file. This is a gross over-simplification of my code, with little > exception handling so that the errors are obvious. > > Running this interactively, if you finish off with 'del db', it exits > fine and creates a skeleton xml file called 'db.xml' with text ' />'. However, if you instead CTRL-D, it throws at exception while > quitting and then leaves an empty 'db.xml' which won't work. Can anyone > here help me figure out why this is? > > Stuff I've done: > I've traced this down to the self.commit() call in __del__. The > stacktrace and a few print statements injected into xml.etree leads me > to the call 'root'.encode('us-ascii') throwing a LookupError on line 751 > of xml.etree.ElementTree. This makes no sense to me, since it works fine > normally. The environment available to __del__ methods during program termination is wonky, and apparently not very consistent either. I can't say that I completely understand it myself, perhaps someone else can provide a better explanation for both of us, but some of the causes are described in the documentation: http://docs.python.org/reference/datamodel.html#object.__del__ What is your rationale for using __del__? Are you trying to force a 'commit()' call on Database instances when your program terminates -- in the case of an unhandled exception, for example? HTH, Marty > > Thank you very much. Any and all help or pointers are appreciated. > > ~Matt > > #### db.py ### > from xml.etree import ElementTree as ET > import os > > class Database(object): > def __init__(self, path): > self.__dbpath = path ## Path to the database > self.load() > def __del__(self): > ## FIXME: Known bug: > ## del db at command line works properly > ## Ctrl-D, when there is no db file present, results in a > LookupError > ## and empty xml file > from StringIO import StringIO > from traceback import print_exc > trace = StringIO() > try: > print 5 > self.commit() > print 7 > except Exception: > print_exc(100, trace) > print trace.getvalue() > def load(self): > if os.path.exists(self.__dbpath): > self.root = ET.parse(self.__dbpath).getroot() > else: > self.root = ET.Element("root") > def commit(self): > ET.ElementTree(self.root).write(self.__dbpath) > db = Database('db.xml') From kent37 at tds.net Mon Apr 20 22:50:22 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 20 Apr 2009 16:50:22 -0400 Subject: [Tutor] String Encoding problem In-Reply-To: References: Message-ID: <1c2a2c590904201350n2774160wbfe7c60f254a7be2@mail.gmail.com> On Mon, Apr 20, 2009 at 10:46 AM, Matt wrote: > Running this interactively, if you finish off with 'del db', it exits fine > and creates a skeleton xml file called 'db.xml' with text ''. > However, if you instead CTRL-D, it throws at exception while quitting and > then leaves an empty 'db.xml' which won't work. Can anyone here help me > figure out why this is? > > Stuff I've done: > I've traced this down to the self.commit() call in __del__. The stacktrace > and a few print statements injected into xml.etree leads me to the call > 'root'.encode('us-ascii') throwing a LookupError on line 751 of > xml.etree.ElementTree. This makes no sense to me, since it works fine > normally. Please show the exact error message and stack trace when you post errors, it can be very helpful. What you are doing with __del__ is unusual and not common practice. A better way to ensure cleanup is to use a close() method which a client must call, or to use a context manager and 'with' statement. I think the reason your code is failing is because some module needed by the encode() call has already been unloaded before your __del__() method is called. > > Thank you very much. Any and all help or pointers are appreciated. If you defined a close() method, you could write client code like this: from contextlib import closing with closing(Database('db.xml')) as db: # do something with db # when this block exits db will be closed It's also not too hard to make an openDatabase() function so you could write with (openDatabase('db.xml')) as db: # etc though that is not really a beginner challenge. Some notes and further pointers here: http://personalpages.tds.net/~kent37/kk/00015.html Kent From mwalsh at mwalsh.org Mon Apr 20 23:05:01 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Mon, 20 Apr 2009 16:05:01 -0500 Subject: [Tutor] String Encoding problem In-Reply-To: References: Message-ID: <49ECE37D.4070101@mwalsh.org> Forwarding to the list. Matt, perhaps you can repost in plain text, my mail client seems to have mangled your source ... Strax-Haber, Matthew (LARC-D320) wrote: >> *From: *Martin Walsh >> >> The environment available to __del__ methods during program termination >> is wonky, and apparently not very consistent either. I can't say that I >> completely understand it myself, perhaps someone else can provide a >> better explanation for both of us, but some of the causes are described >> in the documentation: >> >> http://docs.python.org/reference/datamodel.html#object.__del__ >> >> What is your rationale for using __del__? Are you trying to force a >> 'commit()' call on Database instances when your program terminates -- in >> the case of an unhandled exception, for example? > > Perhaps I oversimplified a bit. In my actual code, there is a database > class and an operator class. The actual structure is this: > > In operator: > def __init__(self, path, saveDB=True, cleanUp=True): > '''Constructor''' ## Calculate filesystem paths > self.WORK_DIR = path + '.tmp' DB_PATH = path > + '.xml' self.SAVE_DB = saveDB ## finish(): Delete > unnecessary files created by run? self.CLEANUP = cleanUp ## > finish(): Delete database at end of run? ## Make sure we > have a working directory (exception on failed write) if not > os.path.isdir(self.WORK_DIR): os.mkdir(self.WORK_DIR) > > self._db = DB.Database(DB_PATH) > ## SOME OTHER ENVIRONMENT SETUP STUFF > def _cleanUpEnvironment(self): try: ## Delete > temp files created for this run for path,dirs,files in > os.walk(self.WORK_DIR, topdown=False): for f in files: > os.unlink(os.path.join(path,f)) for d in dirs: > os.rmdir(os.path.join(path,d)) os.rmdir(self.WORK_DIR) > except: print >>sys.stderr, 'Could not delete temp > files; left at:' print >>sys.stderr, self.WORK_DIR def > finish(self): '''Clean up and finish the run (write out to > the database)''' if self.SAVE_DB: self._db.commit() > if self.CLEANUP: self._cleanUpEnvironment() > def __del__(self): ## FIXME: Known bug: ## del t at > command line works properly ## Ctrl-D, when there is no db file > present, results in a LookupError self.finish() > if __name__ == '__main__': > printHelp() > ## Provide tab completion to the user import readline, > rlcompleter readline.parse_and_bind('tab: complete') > t = OperatorClassName(os.path.splitext(__file__)[0]) > > In database: > def commit(self): '''Write the database back to the file''' > ## Noticed exceptions: IOError > ET.ElementTree(self.root).write(self.__dbpath) > > The operator also has a series of functions that collect data and enter > it into the database. Here?s the usage pattern I want to have: > > User calls Operator.py. User runs a bunch of functions of the form > t.(). When the user quits, the database will save or not > save depending on the status of t.SAVE_DB (which some functions in the > operator will programmatically change). > >> >> HTH, >> Marty >> >>> >>> Thank you very much. Any and all help or pointers are appreciated. >>> >>> ~Matt >>> >>> #### db.py ### >>> from xml.etree import ElementTree as ET >>> import os >>> >>> class Database(object): >>> def __init__(self, path): >>> self.__dbpath = path ## Path to the database >>> self.load() >>> def __del__(self): >>> ## FIXME: Known bug: >>> ## del db at command line works properly >>> ## Ctrl-D, when there is no db file present, results in a >>> LookupError >>> ## and empty xml file >>> from StringIO import StringIO >>> from traceback import print_exc >>> trace = StringIO() >>> try: >>> print 5 >>> self.commit() >>> print 7 >>> except Exception: >>> print_exc(100, trace) >>> print trace.getvalue() >>> def load(self): >>> if os.path.exists(self.__dbpath): >>> self.root = ET.parse(self.__dbpath).getroot() >>> else: >>> self.root = ET.Element("root") >>> def commit(self): >>> ET.ElementTree(self.root).write(self.__dbpath) >>> db = Database('db.xml') >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor > > -- > ~Matthew Strax-Haber > National Aeronautics and Space Administration > Langley Research Center (LaRC) > Co-op, Safety-Critical Avionics Systems Branch > W: 757-864-7378; C: 561-704-0029 > Mail Stop 130 > Matthew.Strax-Haber at nasa.gov From timmichelsen at gmx-topmail.de Mon Apr 20 23:08:27 2009 From: timmichelsen at gmx-topmail.de (Tim Michelsen) Date: Mon, 20 Apr 2009 23:08:27 +0200 Subject: [Tutor] Webpy vs Django In-Reply-To: <5487b3060904200640g57c0c4e8me4e18ef8cc869b22@mail.gmail.com> References: <5487b3060904190352h1e55b759y71cf7b12955f3c89@mail.gmail.com> <2BAB60A0-E2FF-4803-A4D3-71293290AB77@rscorp.ab.ca> <5487b3060904200640g57c0c4e8me4e18ef8cc869b22@mail.gmail.com> Message-ID: I can recommend you web2py: http://www.web2py.com/ It has been designed for didactical needs and has a low learning curve. Look at the manual extarct for an idea: Free manual chapters - http://mdp.cti.depaul.edu/examples/static/web2py_manual_cut.pdf or check the docs: http://www.web2py.com/examples/default/docs All the best, Timmie From ksarikhani at gmail.com Mon Apr 20 23:24:18 2009 From: ksarikhani at gmail.com (Kayvan Sarikhani) Date: Mon, 20 Apr 2009 17:24:18 -0400 Subject: [Tutor] Sending a disconnect after openssl s_client command? In-Reply-To: <49ECAE20.2000600@mwalsh.org> References: <49ECAE20.2000600@mwalsh.org> Message-ID: On Mon, Apr 20, 2009 at 1:17 PM, Martin Walsh wrote: > from subprocess import Popen, PIPE > > openssl_cmd = 'openssl s_client -ssl2 -connect somewebsitename:443' > openssl = Popen( > openssl_cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE > ) > stdout, stderr = openssl.communicate('GET /') > > Alternatively, if you're using python 2.6 and above, it looks like you > can do something similar with a few lines of code, and the ssl module > from the standard lib ... > > # untested! > import ssl > try: > cert = ssl.get_server_certificate( > ('somewebsitename', 443), ssl.PROTOCOL_SSLv2 > ) > except ssl.SSLError, ex: > # site may not support sslv2 > ... > > HTH, > Marty Thanks Marty; this does indeed help...it just also means I need to really learn how subprocess works. ;) I wish I could claim to be using 2.6, but unfortunately the most current version at work is Python 2.5.2...most boxes here are even below, and I can't convince them to upgrade. Ah, well. Thanks again though! K -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Apr 20 23:34:41 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Apr 2009 22:34:41 +0100 Subject: [Tutor] Looping References: Message-ID: "Matt" wrote > As an aside, why use range rather than xrange? I was under the impression > that xrange is a generator and therefore more memory efficient. xrange was a kludge to improve on range's memory efficiency but it is a horrible name that obscures the code. Also it does not exist in v3 so if you use it you will need to change the code for v3. It is as well to be as consistent with v3 as possible IMHO Alan G From matthew.strax-haber at nasa.gov Mon Apr 20 23:17:07 2009 From: matthew.strax-haber at nasa.gov (Strax-Haber, Matthew (LARC-D320)) Date: Mon, 20 Apr 2009 16:17:07 -0500 Subject: [Tutor] String Encoding problem In-Reply-To: <49ECE37D.4070101@mwalsh.org> Message-ID: Sorry about that. Hopefully this is better: In operator: def __init__(self, path, saveDB=True, cleanUp=True): '''Constructor''' ## Calculate filesystem paths self.WORK_DIR = path + '.tmp' DB_PATH = path + '.xml' self.SAVE_DB = saveDB ## finish(): Delete unnecessary files created by run? self.CLEANUP = cleanUp ## finish(): Delete database at end of run? ## Make sure we have a working directory (exception on failed write) if not os.path.isdir(self.WORK_DIR): os.mkdir(self.WORK_DIR) self._db = DB.Database(DB_PATH) ## SOME OTHER ENVIRONMENT SETUP STUFF def _cleanUpEnvironment(self): '''Delete temp files created for this run''' try: for path,dirs,files in os.walk(self.WORK_DIR, topdown=False): for f in files: os.unlink(os.path.join(path,f)) for d in dirs: os.rmdir(os.path.join(path,d)) os.rmdir(self.WORK_DIR) except: print >>sys.stderr, 'Could not delete temp files; left at:' print >>sys.stderr, self.WORK_DIR def finish(self): '''Clean up and finish the run (write out to the database)''' if self.SAVE_DB: self._db.commit() if self.CLEANUP: self._cleanUpEnvironment() def __del__(self): ## FIXME: Known bug: ## del t at command line works properly ## Ctrl-D, when there is no db file present, results in a LookupError self.finish() if __name__ == '__main__': printHelp() ## Provide tab completion to the user import readline, rlcompleter readline.parse_and_bind('tab: complete') t = OperatorClassName(os.path.splitext(__file__)[0]) In database: def __init__(self, path): '''Constructor''' self.__dbpath = path ## Path to the database self.load() def load(self): '''Read the database out from the file''' from xml.parsers.expat import ExpatError if os.path.exists(self.__dbpath): ## Noticed exceptions: IOError, ExpatError try: self.root = ET.parse(self.__dbpath).getroot() except ExpatError: raise ExpatError('Invalid XML in ' + self.__dbpath) else: self.root = ET.Element("root") def commit(self): '''Write the database back to the file''' ## Noticed exceptions: IOError ET.ElementTree(self.root).write(self.__dbpath) -- ~Matthew Strax-Haber National Aeronautics and Space Administration Langley Research Center (LaRC) Co-op, Safety-Critical Avionics Systems Branch W: 757-864-7378; C: 561-704-0029 Mail Stop 130 Matthew.Strax-Haber at nasa.gov ________________________________ From: Martin Walsh Date: Mon, 20 Apr 2009 16:05:01 -0500 To: Python Tutor Cc: "Strax-Haber, Matthew (LARC-D320)" Subject: Re: [Tutor] String Encoding problem Forwarding to the list. Matt, perhaps you can repost in plain text, my mail client seems to have mangled your source ... Strax-Haber, Matthew (LARC-D320) wrote: >> *From: *Martin Walsh >> >> The environment available to __del__ methods during program termination >> is wonky, and apparently not very consistent either. I can't say that I >> completely understand it myself, perhaps someone else can provide a >> better explanation for both of us, but some of the causes are described >> in the documentation: >> >> http://docs.python.org/reference/datamodel.html#object.__del__ >> >> What is your rationale for using __del__? Are you trying to force a >> 'commit()' call on Database instances when your program terminates -- in >> the case of an unhandled exception, for example? > > Perhaps I oversimplified a bit. In my actual code, there is a database > class and an operator class. The actual structure is this: > > In operator: > def __init__(self, path, saveDB=True, cleanUp=True): > '''Constructor''' ## Calculate filesystem paths > self.WORK_DIR = path + '.tmp' DB_PATH = path > + '.xml' self.SAVE_DB = saveDB ## finish(): Delete > unnecessary files created by run? self.CLEANUP = cleanUp ## > finish(): Delete database at end of run? ## Make sure we > have a working directory (exception on failed write) if not > os.path.isdir(self.WORK_DIR): os.mkdir(self.WORK_DIR) > > self._db = DB.Database(DB_PATH) > ## SOME OTHER ENVIRONMENT SETUP STUFF > def _cleanUpEnvironment(self): try: ## Delete > temp files created for this run for path,dirs,files in > os.walk(self.WORK_DIR, topdown=False): for f in files: > os.unlink(os.path.join(path,f)) for d in dirs: > os.rmdir(os.path.join(path,d)) os.rmdir(self.WORK_DIR) > except: print >>sys.stderr, 'Could not delete temp > files; left at:' print >>sys.stderr, self.WORK_DIR def > finish(self): '''Clean up and finish the run (write out to > the database)''' if self.SAVE_DB: self._db.commit() > if self.CLEANUP: self._cleanUpEnvironment() > def __del__(self): ## FIXME: Known bug: ## del t at > command line works properly ## Ctrl-D, when there is no db file > present, results in a LookupError self.finish() > if __name__ == '__main__': > printHelp() > ## Provide tab completion to the user import readline, > rlcompleter readline.parse_and_bind('tab: complete') > t = OperatorClassName(os.path.splitext(__file__)[0]) > > In database: > def commit(self): '''Write the database back to the file''' > ## Noticed exceptions: IOError > ET.ElementTree(self.root).write(self.__dbpath) > > The operator also has a series of functions that collect data and enter > it into the database. Here's the usage pattern I want to have: > > User calls Operator.py. User runs a bunch of functions of the form > t.(). When the user quits, the database will save or not > save depending on the status of t.SAVE_DB (which some functions in the > operator will programmatically change). > >> >> HTH, >> Marty >> >>> >>> Thank you very much. Any and all help or pointers are appreciated. >>> >>> ~Matt >>> >>> #### db.py ### >>> from xml.etree import ElementTree as ET >>> import os >>> >>> class Database(object): >>> def __init__(self, path): >>> self.__dbpath = path ## Path to the database >>> self.load() >>> def __del__(self): >>> ## FIXME: Known bug: >>> ## del db at command line works properly >>> ## Ctrl-D, when there is no db file present, results in a >>> LookupError >>> ## and empty xml file >>> from StringIO import StringIO >>> from traceback import print_exc >>> trace = StringIO() >>> try: >>> print 5 >>> self.commit() >>> print 7 >>> except Exception: >>> print_exc(100, trace) >>> print trace.getvalue() >>> def load(self): >>> if os.path.exists(self.__dbpath): >>> self.root = ET.parse(self.__dbpath).getroot() >>> else: >>> self.root = ET.Element("root") >>> def commit(self): >>> ET.ElementTree(self.root).write(self.__dbpath) >>> db = Database('db.xml') >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor > > -- > ~Matthew Strax-Haber > National Aeronautics and Space Administration > Langley Research Center (LaRC) > Co-op, Safety-Critical Avionics Systems Branch > W: 757-864-7378; C: 561-704-0029 > Mail Stop 130 > Matthew.Strax-Haber at nasa.gov -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Mon Apr 20 23:55:16 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 20 Apr 2009 17:55:16 -0400 Subject: [Tutor] String Encoding problem In-Reply-To: References: <49ECE37D.4070101@mwalsh.org> Message-ID: <1c2a2c590904201455w4cb59a2y2374ed345b7e5fc@mail.gmail.com> Can you give us a simple description of what you are trying to do? And if you can post in plain text instead of HTML that would be helpful. Maybe this will give you some ideas - you can trap the control-D and do your cleanup: http://openbookproject.net/pybiblio/tips/wilson/simpleExceptions.php Kent From matthew.strax-haber at nasa.gov Tue Apr 21 00:00:58 2009 From: matthew.strax-haber at nasa.gov (Strax-Haber, Matthew (LARC-D320)) Date: Mon, 20 Apr 2009 17:00:58 -0500 Subject: [Tutor] String Encoding problem In-Reply-To: <1c2a2c590904201455w4cb59a2y2374ed345b7e5fc@mail.gmail.com> Message-ID: I've solved the problem by passing on the work of deciding when to commit to client code. This isn't ideal but it will do what is necessary and unfortunately I don't have any more time to dedicate to this. I hate not being able to find a reasonable workaround :/. -- ~Matthew Strax-Haber National Aeronautics and Space Administration Langley Research Center (LaRC) Co-op, Safety-Critical Avionics Systems Branch W: 757-864-7378; C: 561-704-0029 Mail Stop 130 Matthew.Strax-Haber at nasa.gov ________________________________ From: Kent Johnson Date: Mon, 20 Apr 2009 16:55:16 -0500 To: "Strax-Haber, Matthew (LARC-D320)" Cc: Python Tutor Subject: Re: [Tutor] String Encoding problem Can you give us a simple description of what you are trying to do? And if you can post in plain text instead of HTML that would be helpful. Maybe this will give you some ideas - you can trap the control-D and do your cleanup: http://openbookproject.net/pybiblio/tips/wilson/simpleExceptions.php Kent -------------- next part -------------- An HTML attachment was scrubbed... URL: From ptmcg at austin.rr.com Tue Apr 21 00:56:59 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Mon, 20 Apr 2009 17:56:59 -0500 Subject: [Tutor] Looping In-Reply-To: References: Message-ID: <7F1B3F9D883348279D3593541B67FB59@AWA2> > > xrange was a kludge to improve on range's memory efficiency > but it is a horrible name that obscures the code. > > Also it does not exist in v3 so if you use it you will need to change > the code for v3. It is as well to be as consistent with v3 as possible > IMHO > > Alan G I have felt this way also (i.e., "xrange was a kludge" and "it is a horrible name"), and have resisted the general adoption of xrange in my code. Fortunately, I rarely use range, but iterate over sequences directly; or if I absolutely need the item's index, I iterate over enumerate(seq). But even if you use "range" exclusively, you may need to change code when migrating to Python 3. In Py2, range returns a list; in Py3 range returns an iterator (a la Py2-xrange's behavior). If you have code that uses the value returned from range as a list, then in Py3 you will need to explicitly convert it to a list using "list(range(n))". I concur with Alan's suggestion that you code to Py3 semantics, but there are certainly cases where xrange makes sense vs. range (such as doing some sort of repetitive test 1e8 times, there is no sense in creating a 100-million entry list just to iterate over it). So I'll propose some usages for those who use range: 1. For Py2-Py3 range-xrange compatibility, add this code to the top of your Python scripts: try: range = xrange except NameError: pass In this way, your code under Py2 will use xrange whenever you call range, and you will adopt the future-compatible range behavior. (Hmm, maybe this would have been a good option to add to the "future" module...) 2. In all cases where you really must use the result returned from range as a list, ALWAYS write this as "list(range(n))", as in: nonnegative_numbers_up_to_but_not_including_10 = list(range(10)) even_numbers_up_to_but_not_including_20 = list(range(0,20,2)) Using the "list(range(n))" form when you actually need a list will prepare you for the change in idiom when you upgrade to Py3, and is fully Py2 compatible (even if you don't first bind xrange to range as in suggestion 1 - it simply copies the original list). It also makes it very explicit that you REALLY WANT THE RANGE AS A LIST, and not just as something for counting up to n. -- Paul From HellZFury+Python at gmail.com Tue Apr 21 01:53:32 2009 From: HellZFury+Python at gmail.com (Matt) Date: Mon, 20 Apr 2009 19:53:32 -0400 Subject: [Tutor] Functional Derivatives Message-ID: I'm trying to calculate the derivative of a function in Python like so: def D5(func,h=1e-5): ??? ""' Return derivative of function func''' ??? def df(x): return (func(x+h)-func(x))/h df.__name__ = func.__name__ + '_dx' ??? return df However, I run into the problem of limited float precision. This is evident for this: import math print D5(D5(D5(D5(math.sin))))(0.3) => -5551.11512313 print math.sin(0.3) => 0.295520206661 Is there any way that any of you can think of to avoid this for general-purpose functions? Thanks. From cfuller084 at thinkingplanet.net Tue Apr 21 02:20:02 2009 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Mon, 20 Apr 2009 19:20:02 -0500 Subject: [Tutor] Functional Derivatives In-Reply-To: References: Message-ID: <200904201920.03157.cfuller084@thinkingplanet.net> You should look up "numerical methods" or similar. There are ways of rearranging your calculations to minimize the loss of precision. You could also try a numerical/scientific library like GMP (for integers or rational numbers). I don't know of any (Python) library for extended precision floats, but there ought to be. Cheers On Monday 20 April 2009 18:53, Matt wrote: > I'm trying to calculate the derivative of a function in Python like so: > > def D5(func,h=1e-5): > ??? ""' Return derivative of function func''' > ??? def df(x): return (func(x+h)-func(x))/h > df.__name__ = func.__name__ + '_dx' > ??? return df > > However, I run into the problem of limited float precision. This is > evident for this: > import math > print D5(D5(D5(D5(math.sin))))(0.3) > => -5551.11512313 > print math.sin(0.3) > => 0.295520206661 > Is there any way that any of you can think of to avoid this for > general-purpose functions? Thanks. > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From kent37 at tds.net Tue Apr 21 02:37:36 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 20 Apr 2009 20:37:36 -0400 Subject: [Tutor] Functional Derivatives In-Reply-To: References: Message-ID: <1c2a2c590904201737i47ab7903rc39eeed5f819eafc@mail.gmail.com> On Mon, Apr 20, 2009 at 7:53 PM, Matt wrote: > I'm trying to calculate the derivative of a function in Python like so: > > def D5(func,h=1e-5): > ??? ""' Return derivative of function func''' > ??? def df(x): ? ?return (func(x+h)-func(x))/h > ? ?df.__name__ = func.__name__ + '_dx' > ??? return df > > However, I run into the problem of limited float precision. > Is there any way that any of you can think of to avoid this for > general-purpose functions? Thanks. mpmath? http://code.google.com/p/mpmath/ Kent From ctcast at gmail.com Tue Apr 21 04:01:20 2009 From: ctcast at gmail.com (Chris Castillo) Date: Mon, 20 Apr 2009 21:01:20 -0500 Subject: [Tutor] Reading from files problem In-Reply-To: References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> Message-ID: <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> this worked for me fine. #Author - Chris Castillo #Created 4/19/09 #Program 6 import time #Open and read text file with student information gradesfile = open("grades.dat", "r") time_acc = time.asctime() #Ask user for student ID to lookup student information sought_id = raw_input("Please enter a student identifier to look up grades: ") #Assign values for each students' info in list for line in gradesfile: lines = line.split(",") student_id = lines[0] last_name = lines[1] first_name = lines[2] major = lines[3] hw1 = lines[4] hw2 = lines[5] hw3 = lines[6] hw4 = lines[7] hw5 = lines[8] hw6 = lines[9] hw7 = lines[10] test1 = lines[11] test2 = lines[12] test3 = lines[13] #Arithmetic operations for all students' grades total_points = 550 hw_grades = float(hw1) + float(hw2) + float(hw3) + float(hw4) + float(hw5) + float(hw6) + float(hw7) test_grades = float(test1) + float(test2) + float(test3) student_points = float(hw_grades) + float(test_grades) avg_grade = round(float(student_points / total_points) * 100.0, 2) #Assign grades to letter values if avg_grade < 60: letter_grade="F" elif avg_grade < 70: letter_grade="D" elif avg_grade < 80: letter_grade="C" elif avg_grade < 90: letter_grade="B" elif avg_grade < 101: letter_grade="A" else: print avg_grade, "is not valid" #variable for output to request.dat text file req_output = student_id + ", " + last_name + ", " + first_name + ", " + time_acc #Check to see if student id number is in the list of student id numbers if lines[0] == sought_id: new_file = open("requests.dat", "w+") new_file.write(req_output) print "\n", last_name + ", "+ first_name + ", " + student_id + ", " + major print "\n", time_acc print "\nExams - ", test1 + ", " + test2 + ", " + test3 print "Homework - ", hw1, ", ",hw2, ", ", hw3, ", ", hw4, ",", hw5, ", ", hw6, ", ", hw7 print "\nTotal points earned - ", student_points print "\nGrade: ", avg_grade, "that is a ", letter_grade gradesfile.close() hope this helps someone. I know it's probably not the most efficient way to handle this kind of problem but It works nonetheless. On Mon, Apr 20, 2009 at 8:24 AM, Alan Gauld wrote: > > "Scott SA" wrote > > May have been a bad assumption on my part as I envisioned pickling a >> dict. and that just got too complicated. >> > > A pickled dict? > That would be a shelve mebbe? > > > PS. My email is acting up, did my prev. message actually make it to the >> list? >> > > Yes, I saw it just after sending mine... > > Alan G > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dextrous85 at gmail.com Tue Apr 21 06:44:12 2009 From: dextrous85 at gmail.com (vishwajeet singh) Date: Tue, 21 Apr 2009 10:14:12 +0530 Subject: [Tutor] Webpy vs Django In-Reply-To: References: <5487b3060904190352h1e55b759y71cf7b12955f3c89@mail.gmail.com> <2BAB60A0-E2FF-4803-A4D3-71293290AB77@rscorp.ab.ca> <5487b3060904200640g57c0c4e8me4e18ef8cc869b22@mail.gmail.com> Message-ID: <5487b3060904202144m27de120dxe883e6ee035f8732@mail.gmail.com> Thanks for your answer but can you give me reasons for same. On Tue, Apr 21, 2009 at 2:38 AM, Tim Michelsen wrote: > I can recommend you web2py: > http://www.web2py.com/ > > It has been designed for didactical needs and has a low learning curve. > > Look at the manual extarct for an idea: > Free manual chapters - > http://mdp.cti.depaul.edu/examples/static/web2py_manual_cut.pdf > > or check the docs: > http://www.web2py.com/examples/default/docs > > All the best, > Timmie > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Cheers, Vishwajeet http://www.singhvishwajeet.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From cfuller084 at thinkingplanet.net Tue Apr 21 07:38:58 2009 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Tue, 21 Apr 2009 00:38:58 -0500 Subject: [Tutor] Functional Derivatives In-Reply-To: References: Message-ID: <200904210038.58676.cfuller084@thinkingplanet.net> His problem was composing four functions, each with a small error. The first two applications work well enough, but there is a about a percent error in the third composition. The big source of error is f(x+h)-f(x). Subtracting two floating point numbers that are nearly equal is a known source of innaccuracy. Scaling all the terms by a very large number reduced the error, but not as well as fewer compositions. Cheers BIGNUM = 1 def D5(func, h=1e-5): ""' Return derivative of function func''' def df(x): return (BIGNUM*func(x+h)-BIGNUM*func(x))/(BIGNUM*h) return df import math print D5(math.sin)(0.3) print math.cos(0.3) print D5(D5(math.sin))(0.3) print -math.sin(0.3) print print D5(D5(D5(math.sin)))(0.3) print -math.cos(0.3) # actually, other powers, higher or lower, work less well.. BIGNUM = 1e10 print D5(D5(D5(math.sin)))(0.3) print -math.cos(0.3) From alan.gauld at btinternet.com Tue Apr 21 09:30:27 2009 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Tue, 21 Apr 2009 07:30:27 +0000 (GMT) Subject: [Tutor] Reading from files problem In-Reply-To: <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> Message-ID: <91655.74845.qm@web86703.mail.ird.yahoo.com> > this worked for me fine. I'm glad you solved tyour problem. But you have done so in an extremely inefficient way, both inefficient in the way it uses computer resources - both CPU and storage, and inefficient in the way it uses your time - way more code than you need. If you only need an answer to this specific problem now then thats not a problem, if you ever intend to write another program it is worth learning how to improve the code quality. Take the time to look at the suggestions you were given so that at least in the future you can avoid making the same mistakes and save yourself some time. > #Ask user for student ID to lookup student information > sought_id = raw_input("Please enter a student identifier to look up grades: ") Perhaps the biggest inefficiency of all is that you reprocess all of the data every time you run the program. Yet you are taking the trouble to split the data out into separate files. Why not use those files? If you checked if the required data already existed you could save an awful lot of work for your processor and your disk as well as speed things up for your user. With a small number of students the latter issue probably isn't a big concern however. for line in gradesfile: ... #variable for output to request.dat text file req_output = student_id + ", " + last_name + ", " + first_name + ", " + time_acc #Check to see if student id number is in the list of student id numbers if lines[0] == sought_id: new_file = open("requests.dat", "w+") new_file.write(req_output) Here you write a single file despite processing all of the input data. print "\n", last_name + ", "+ first_name + ", " + student_id + ", " + major But here you ignore the files you (may) already have created and print the data out from scratch again. It works but it doesn't make much sense. > hope this helps someone. > I know it's probably not the most efficient way to handle this kind of problem > but It works nonetheless. It is almost the most inefficient way to do it. You can get from New York to San Francisco via Europe but I don't recommend it. Alan G -------------- next part -------------- An HTML attachment was scrubbed... URL: From bermanrl at cfl.rr.com Tue Apr 21 18:48:55 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Tue, 21 Apr 2009 12:48:55 -0400 Subject: [Tutor] PDF to text conversion In-Reply-To: <91655.74845.qm@web86703.mail.ird.yahoo.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> Message-ID: <49EDF8F7.2090508@cfl.rr.com> Hi, I must convert a history file in PDF format that goes from May of 1988 to current date. Readings are taken twice weekly and consist of the date taken mm/dd/yy and the results appearing as a 10 character numeric + special characters sequence. This is obviously an easy setup for a very small database application with the date as the key, the result string as the data. My problem is converting the PDF file into a text file which I can then read and process. I do not see any free python libraries having this capacity. I did see a PDFPILOT program for Windows but this application is being developed on Linux and should also run on Windows; so I do not want to incorporate a Windows only application. I do not think i am breaking any new frontiers with this application. Have any of you worked with such a library, or do you know of one or two I can download and work with? Hopefully, they have reasonable documentation. My development environment is: Python Linux Ubuntu version 8.10 Thanks for any help you might be able to offer. Robert Berman From bgailer at gmail.com Tue Apr 21 18:54:36 2009 From: bgailer at gmail.com (bob gailer) Date: Tue, 21 Apr 2009 12:54:36 -0400 Subject: [Tutor] PDF to text conversion In-Reply-To: <49EDF8F7.2090508@cfl.rr.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> <49EDF8F7.2090508@cfl.rr.com> Message-ID: <49EDFA4C.9030506@gmail.com> Robert Berman wrote: > Hi, > > I must convert a history file in PDF format that goes from May of 1988 > to current date. Readings are taken twice weekly and consist of the > date taken mm/dd/yy and the results appearing as a 10 character > numeric + special characters sequence. This is obviously an easy setup > for a very small database application with the date as the key, the > result string as the data. > > My problem is converting the PDF file into a text file which I can > then read and process. I do not see any free python libraries having > this capacity. I did see a PDFPILOT program for Windows but this > application is being developed on Linux and should also run on > Windows; so I do not want to incorporate a Windows only application. > > I do not think i am breaking any new frontiers with this application. > Have any of you worked with such a library, or do you know of one or > two I can download and work with? Hopefully, they have reasonable > documentation. If this is a one-time conversion just use the save as text feature of adobe reader. > > My development environment is: > > Python > Linux > Ubuntu version 8.10 > > > Thanks for any help you might be able to offer. > > > Robert Berman > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer Chapel Hill NC 919-636-4239 From emadnawfal at gmail.com Tue Apr 21 19:21:21 2009 From: emadnawfal at gmail.com (=?windows-1256?B?RW1hZCBOYXdmYWwgKNrjx88g5Obd4Sk=?=) Date: Tue, 21 Apr 2009 13:21:21 -0400 Subject: [Tutor] PDF to text conversion In-Reply-To: <49EDFA4C.9030506@gmail.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> <49EDF8F7.2090508@cfl.rr.com> <49EDFA4C.9030506@gmail.com> Message-ID: <652641e90904211021w20130818q1f40a8d1a56b830a@mail.gmail.com> On Tue, Apr 21, 2009 at 12:54 PM, bob gailer wrote: > Robert Berman wrote: > >> Hi, >> >> I must convert a history file in PDF format that goes from May of 1988 to >> current date. Readings are taken twice weekly and consist of the date taken >> mm/dd/yy and the results appearing as a 10 character numeric + special >> characters sequence. This is obviously an easy setup for a very small >> database application with the date as the key, the result string as the >> data. >> >> My problem is converting the PDF file into a text file which I can then >> read and process. I do not see any free python libraries having this >> capacity. I did see a PDFPILOT program for Windows but this application is >> being developed on Linux and should also run on Windows; so I do not want to >> incorporate a Windows only application. >> >> I do not think i am breaking any new frontiers with this application. Have >> any of you worked with such a library, or do you know of one or two I can >> download and work with? Hopefully, they have reasonable documentation. >> > > If this is a one-time conversion just use the save as text feature of adobe > reader. > > >> My development environment is: >> >> Python >> Linux >> Ubuntu version 8.10 >> >> >> Thanks for any help you might be able to offer. >> >> >> Robert Berman >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > > -- > Bob Gailer > Chapel Hill NC > 919-636-4239 > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > I tried pyPdf once, just for fun, and it was nice: http://pybrary.net/pyPdf/ -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From bermanrl at cfl.rr.com Tue Apr 21 19:36:59 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Tue, 21 Apr 2009 13:36:59 -0400 Subject: [Tutor] PDF to text conversion In-Reply-To: <49EDFA4C.9030506@gmail.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> <49EDF8F7.2090508@cfl.rr.com> <49EDFA4C.9030506@gmail.com> Message-ID: <49EE043B.9010609@cfl.rr.com> Bob, Thank you for the quick reply. I am acquainted with that method, and that will certainly work to do some really serious testing; but, the data collection is an ongoing process and the users are requesting that every month the latest entries (8) are brought into the system. What is rather irksome is that the output from the system cannot be changed from PDF to text; so obviously I am going to have to resolve the situation at my end. I am envisioning a simple program that once started reads the data file, converts the data into text, and then sends the data to the database. The program doesn't care if there are 8 test results or 80,000 test results. That is why i am looking for a python module. Thanks again, Robert Berman bob gailer wrote: > Robert Berman wrote: >> Hi, >> >> I must convert a history file in PDF format that goes from May of >> 1988 to current date. Readings are taken twice weekly and consist of >> the date taken mm/dd/yy and the results appearing as a 10 character >> numeric + special characters sequence. This is obviously an easy >> setup for a very small database application with the date as the >> key, the result string as the data. >> >> My problem is converting the PDF file into a text file which I can >> then read and process. I do not see any free python libraries having >> this capacity. I did see a PDFPILOT program for Windows but this >> application is being developed on Linux and should also run on >> Windows; so I do not want to incorporate a Windows only application. >> >> I do not think i am breaking any new frontiers with this application. >> Have any of you worked with such a library, or do you know of one or >> two I can download and work with? Hopefully, they have reasonable >> documentation. > > If this is a one-time conversion just use the save as text feature of > adobe reader. > >> >> My development environment is: >> >> Python >> Linux >> Ubuntu version 8.10 >> >> >> Thanks for any help you might be able to offer. >> >> >> Robert Berman >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > From bermanrl at cfl.rr.com Tue Apr 21 19:44:16 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Tue, 21 Apr 2009 13:44:16 -0400 Subject: [Tutor] PDF to text conversion In-Reply-To: <652641e90904211021w20130818q1f40a8d1a56b830a@mail.gmail.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> <49EDF8F7.2090508@cfl.rr.com> <49EDFA4C.9030506@gmail.com> <652641e90904211021w20130818q1f40a8d1a56b830a@mail.gmail.com> Message-ID: <49EE05F0.3080006@cfl.rr.com> Hello Emad, I have seriously looked at the documentation associated with pyPDF. This seems to have the page as its smallest element of work, and what i need is a line by line process to go from .PDF format to Text. I don't think pyPDF will meet my needs but thank you for bringing it to my attention. Thanks, Robert Berman Emad Nawfal (???? ????) wrote: > > > On Tue, Apr 21, 2009 at 12:54 PM, bob gailer > wrote: > > Robert Berman wrote: > > Hi, > > I must convert a history file in PDF format that goes from May > of 1988 to current date. Readings are taken twice weekly and > consist of the date taken mm/dd/yy and the results appearing > as a 10 character numeric + special characters sequence. This > is obviously an easy setup for a very small database > application with the date as the key, the result string as > the data. > > My problem is converting the PDF file into a text file which I > can then read and process. I do not see any free python > libraries having this capacity. I did see a PDFPILOT program > for Windows but this application is being developed on Linux > and should also run on Windows; so I do not want to > incorporate a Windows only application. > > I do not think i am breaking any new frontiers with this > application. Have any of you worked with such a library, or do > you know of one or two I can download and work with? > Hopefully, they have reasonable documentation. > > > If this is a one-time conversion just use the save as text feature > of adobe reader. > > > > My development environment is: > > Python > Linux > Ubuntu version 8.10 > > > Thanks for any help you might be able to offer. > > > Robert Berman > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > > -- > Bob Gailer > Chapel Hill NC > 919-636-4239 > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > > I tried pyPdf once, just for fun, and it was nice: > http://pybrary.net/pyPdf/ > -- > ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? > ????????.....???? ??????? > "No victim has ever been more repressed and alienated than the truth" > > Emad Soliman Nawfal > Indiana University, Bloomington > -------------------------------------------------------- From emile at fenx.com Tue Apr 21 19:51:13 2009 From: emile at fenx.com (Emile van Sebille) Date: Tue, 21 Apr 2009 10:51:13 -0700 Subject: [Tutor] PDF to text conversion In-Reply-To: <49EDF8F7.2090508@cfl.rr.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> <49EDF8F7.2090508@cfl.rr.com> Message-ID: Robert Berman wrote: > Have any of you worked with such a library, or do you know of one or two > I can download and work with? Hopefully, they have reasonable > documentation. > > My development environment is: > > Python > Linux > Ubuntu version 8.10 > I've used [root at fcfw2 /]# /usr/bin/pdftotext -v pdftotext version 2.01 Copyright 1996-2002 Glyph & Cog, LLC [root at fcfw2 /]# cat /etc/issue Red Hat Linux release 9 (Shrike) HTH, Emile From jfabiani at yolo.com Tue Apr 21 19:49:31 2009 From: jfabiani at yolo.com (johnf) Date: Tue, 21 Apr 2009 10:49:31 -0700 Subject: [Tutor] PDF to text conversion In-Reply-To: <49EE043B.9010609@cfl.rr.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <49EDFA4C.9030506@gmail.com> <49EE043B.9010609@cfl.rr.com> Message-ID: <200904211049.31686.jfabiani@yolo.com> On Tuesday 21 April 2009 10:36:59 am Robert Berman wrote: > Bob, > > Thank you for the quick reply. I am acquainted with that method, and > that will certainly work to do some really serious testing; but, the > data collection is an ongoing process and the users are requesting that > every month the latest entries (8) are brought into the system. What is > rather irksome is that the output from the system cannot be changed from > PDF to text; so obviously I am going to have to resolve the situation at > my end. > > I am envisioning a simple program that once started reads the data file, > converts the data into text, and then sends the data to the database. > The program doesn't care if there are 8 test results or 80,000 test > results. That is why i am looking for a python module. > > Thanks again, > > Robert Berman > > bob gailer wrote: > > Robert Berman wrote: > >> Hi, > >> > >> I must convert a history file in PDF format that goes from May of > >> 1988 to current date. Readings are taken twice weekly and consist of > >> the date taken mm/dd/yy and the results appearing as a 10 character > >> numeric + special characters sequence. This is obviously an easy > >> setup for a very small database application with the date as the > >> key, the result string as the data. > >> > >> My problem is converting the PDF file into a text file which I can > >> then read and process. I do not see any free python libraries having > >> this capacity. I did see a PDFPILOT program for Windows but this > >> application is being developed on Linux and should also run on > >> Windows; so I do not want to incorporate a Windows only application. > >> > >> I do not think i am breaking any new frontiers with this application. > >> Have any of you worked with such a library, or do you know of one or > >> two I can download and work with? Hopefully, they have reasonable > >> documentation. > > > > If this is a one-time conversion just use the save as text feature of > > adobe reader. > > > >> My development environment is: > >> > >> Python > >> Linux > >> Ubuntu version 8.10 > >> > >> > >> Thanks for any help you might be able to offer. > >> > >> > >> Robert Berman On linux pdftotext is available and you might want to check out ghostscript which runs on windows and linux. -- John Fabiani From dineshbvadhia at hotmail.com Tue Apr 21 20:05:32 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Tue, 21 Apr 2009 11:05:32 -0700 Subject: [Tutor] PDF to text conversion Message-ID: Hi Robert I don't have an answer but can have my sympathy. I've been looking for a quality pdf to text convertor for months and not turned up anything useful. I've tried many free programs which are poor. I too wanted a Python-only solution and tried pyPdf but that didn't work. Just today I download a trial version of a so called top-notch converter and it produced unfaithful text. Not sure what the answer is! Dinesh -------------------------------------------------------------------------------- Message: 5 Date: Tue, 21 Apr 2009 13:44:16 -0400 From: Robert Berman Subject: Re: [Tutor] PDF to text conversion To: "Emad Nawfal (???? ????)" Cc: tutor at python.org Message-ID: <49EE05F0.3080006 at cfl.rr.com> Content-Type: text/plain; charset=windows-1256; format=flowed Hello Emad, I have seriously looked at the documentation associated with pyPDF. This seems to have the page as its smallest element of work, and what i need is a line by line process to go from .PDF format to Text. I don't think pyPDF will meet my needs but thank you for bringing it to my attention. Thanks, Robert Berman -------------- next part -------------- An HTML attachment was scrubbed... URL: From jfabiani at yolo.com Tue Apr 21 20:43:30 2009 From: jfabiani at yolo.com (johnf) Date: Tue, 21 Apr 2009 11:43:30 -0700 Subject: [Tutor] PDF to text conversion In-Reply-To: References: Message-ID: <200904211143.30855.jfabiani@yolo.com> On Tuesday 21 April 2009 11:05:32 am Dinesh B Vadhia wrote: > Hi Robert > > I don't have an answer but can have my sympathy. I've been looking for a > quality pdf to text convertor for months and not turned up anything useful. > I've tried many free programs which are poor. I too wanted a Python-only > solution and tried pyPdf but that didn't work. Just today I download a > trial version of a so called top-notch converter and it produced unfaithful > text. Not sure what the answer is! > > Dinesh > > > --------------------------------------------------------------------------- >----- > > Message: 5 > Date: Tue, 21 Apr 2009 13:44:16 -0400 > From: Robert Berman > Subject: Re: [Tutor] PDF to text conversion > To: "Emad Nawfal (???? ????)" > Cc: tutor at python.org > Message-ID: <49EE05F0.3080006 at cfl.rr.com> > Content-Type: text/plain; charset=windows-1256; format=flowed > > Hello Emad, > > I have seriously looked at the documentation associated with pyPDF. This > seems to have the page as its smallest element of work, and what i need > is a line by line process to go from .PDF format to Text. I don't think > pyPDF will meet my needs but thank you for bringing it to my attention. > > Thanks, > > > Robert Berman Have you tried going from a PDF to PS and then to text? -- John Fabiani From contactdayo at gmail.com Tue Apr 21 20:44:51 2009 From: contactdayo at gmail.com (Dayo Adewunmi) Date: Tue, 21 Apr 2009 19:44:51 +0100 Subject: [Tutor] PDF to text conversion In-Reply-To: References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> <49EDF8F7.2090508@cfl.rr.com> Message-ID: <49EE1423.6060900@gmail.com> Emile van Sebille wrote: > Robert Berman wrote: > > >> Have any of you worked with such a library, or do you know of one or >> two I can download and work with? Hopefully, they have reasonable >> documentation. >> >> My development environment is: >> >> Python >> Linux >> Ubuntu version 8.10 >> > > I've used > > [root at fcfw2 /]# /usr/bin/pdftotext -v > pdftotext version 2.01 > Copyright 1996-2002 Glyph & Cog, LLC > [root at fcfw2 /]# cat /etc/issue > Red Hat Linux release 9 (Shrike) > > > HTH, > > Emile > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > Hi Robert, pdftotext is part of poppler-utils, an Ubuntu package which can be installed like so: sudo aptitude install poppler-utils But I to would be interested in finding a python library/module for this. Regards, Dayo From mwalsh at mwalsh.org Tue Apr 21 20:57:56 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Tue, 21 Apr 2009 13:57:56 -0500 Subject: [Tutor] PDF to text conversion In-Reply-To: <49EE05F0.3080006@cfl.rr.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> <49EDF8F7.2090508@cfl.rr.com> <49EDFA4C.9030506@gmail.com> <652641e90904211021w20130818q1f40a8d1a56b830a@mail.gmail.com> <49EE05F0.3080006@cfl.rr.com> Message-ID: <49EE1734.3060604@mwalsh.org> Robert Berman wrote: > Hello Emad, > > I have seriously looked at the documentation associated with pyPDF. This > seems to have the page as its smallest element of work, and what i need > is a line by line process to go from .PDF format to Text. I don't think > pyPDF will meet my needs but thank you for bringing it to my attention. > > Thanks, > > > Robert Berman Have you looked at pdfminer? http://www.unixuser.org/~euske/python/pdfminer/index.html Looks promising. HTH, Marty > > Emad Nawfal (???? ????) wrote: >> >> >> On Tue, Apr 21, 2009 at 12:54 PM, bob gailer > > wrote: >> >> Robert Berman wrote: >> >> Hi, >> >> I must convert a history file in PDF format that goes from May >> of 1988 to current date. Readings are taken twice weekly and >> consist of the date taken mm/dd/yy and the results appearing >> as a 10 character numeric + special characters sequence. This >> is obviously an easy setup for a very small database >> application with the date as the key, the result string as >> the data. >> >> My problem is converting the PDF file into a text file which I >> can then read and process. I do not see any free python >> libraries having this capacity. I did see a PDFPILOT program >> for Windows but this application is being developed on Linux >> and should also run on Windows; so I do not want to >> incorporate a Windows only application. >> >> I do not think i am breaking any new frontiers with this >> application. Have any of you worked with such a library, or do >> you know of one or two I can download and work with? >> Hopefully, they have reasonable documentation. >> >> >> If this is a one-time conversion just use the save as text feature >> of adobe reader. >> >> >> >> My development environment is: >> >> Python >> Linux >> Ubuntu version 8.10 >> >> >> Thanks for any help you might be able to offer. >> >> >> Robert Berman >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> >> >> -- Bob Gailer >> Chapel Hill NC >> 919-636-4239 >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> >> >> I tried pyPdf once, just for fun, and it was nice: >> http://pybrary.net/pyPdf/ >> -- >> ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? >> ????????.....???? ??????? >> "No victim has ever been more repressed and alienated than the truth" >> >> Emad Soliman Nawfal >> Indiana University, Bloomington >> -------------------------------------------------------- > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From david at abbottdavid.com Tue Apr 21 21:01:03 2009 From: david at abbottdavid.com (David) Date: Tue, 21 Apr 2009 15:01:03 -0400 Subject: [Tutor] PDF to text conversion In-Reply-To: <49EDFA4C.9030506@gmail.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> <49EDF8F7.2090508@cfl.rr.com> <49EDFA4C.9030506@gmail.com> Message-ID: <49EE17EF.2040401@abbottdavid.com> bob gailer wrote: > Robert Berman wrote: >> Hi, >> >> I must convert a history file in PDF format that goes from May of 1988 >> to current date. Readings are taken twice weekly and consist of the >> date taken mm/dd/yy and the results appearing as a 10 character >> numeric + special characters sequence. This is obviously an easy setup >> for a very small database application with the date as the key, the >> result string as the data. >> >> My problem is converting the PDF file into a text file which I can >> then read and process. I do not see any free python libraries having >> this capacity. I did see a PDFPILOT program for Windows but this >> application is being developed on Linux and should also run on >> Windows; so I do not want to incorporate a Windows only application. How about pyPdf; http://pybrary.net/pyPdf/ And an example; http://code.activestate.com/recipes/511465/ -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From norman at khine.net Tue Apr 21 21:02:55 2009 From: norman at khine.net (Norman Khine) Date: Tue, 21 Apr 2009 21:02:55 +0200 Subject: [Tutor] PDF to text conversion In-Reply-To: <49EE1423.6060900@gmail.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> <49EDF8F7.2090508@cfl.rr.com> <49EE1423.6060900@gmail.com> Message-ID: <9c2c8ffb0904211202m3a9e608ewa458fb14737edf1b@mail.gmail.com> the itools library from hforge.org has a PDF2TEXT implementation itools.pdf http://www.hforge.org/itools norman On Tue, Apr 21, 2009 at 8:44 PM, Dayo Adewunmi wrote: > Emile van Sebille wrote: >> >> Robert Berman wrote: >> >> >>> Have any of you worked with such a library, or do you know of one or two >>> I can download and work with? Hopefully, they have reasonable documentation. >>> >>> My development environment is: >>> >>> Python >>> Linux >>> Ubuntu version 8.10 >>> >> >> I've used >> >> [root at fcfw2 /]# /usr/bin/pdftotext -v >> pdftotext version 2.01 >> Copyright 1996-2002 Glyph & Cog, LLC >> [root at fcfw2 /]# cat /etc/issue >> Red Hat Linux release 9 (Shrike) >> >> >> HTH, >> >> Emile >> >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > Hi Robert, > pdftotext is part of poppler-utils, an Ubuntu package which can be installed > like so: > > sudo aptitude install poppler-utils > > But I to would be interested in finding a python library/module for this. > > Regards, > > Dayo > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From mwalsh at mwalsh.org Tue Apr 21 21:22:49 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Tue, 21 Apr 2009 14:22:49 -0500 Subject: [Tutor] Sending a disconnect after openssl s_client command? In-Reply-To: References: <49ECAE20.2000600@mwalsh.org> Message-ID: <49EE1D09.5080604@mwalsh.org> Kayvan Sarikhani wrote: > On Mon, Apr 20, 2009 at 1:17 PM, Martin Walsh > wrote: > > from subprocess import Popen, PIPE > > openssl_cmd = 'openssl s_client -ssl2 -connect somewebsitename:443' > openssl = Popen( > openssl_cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE > ) > stdout, stderr = openssl.communicate('GET /') > > Alternatively, if you're using python 2.6 and above, it looks like you > can do something similar with a few lines of code, and the ssl module > from the standard lib ... > > # untested! > import ssl > try: > cert = ssl.get_server_certificate( > ('somewebsitename', 443), ssl.PROTOCOL_SSLv2 > ) > except ssl.SSLError, ex: > # site may not support sslv2 > ... > > HTH, > Marty > > > Thanks Marty; this does indeed help...it just also means I need to > really learn how subprocess works. ;) I wish I could claim to be using > 2.6, but unfortunately the most current version at work is Python > 2.5.2...most boxes here are even below, and I can't convince them to > upgrade. Ah, well. Yep, subprocess is the way to go. In that case, if you're not offended by the extra dependency, then you might be interested in http://pypi.python.org/pypi/ssl, which appears to be a backport of the 2.6 ssl module. I haven't tried it myself, but it has a get_server_certificate helper also, so I'd expect it to work the same way. Although, you'll probably want to explore in greater detail the properties of the exception that is raised by a site not supporting sslv2. When I tried I received an SSLError(errno=6) for a server configured w/o sslv2. > > Thanks again though! You're welcome, glad it helped. :) Marty From bermanrl at cfl.rr.com Wed Apr 22 00:37:39 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Tue, 21 Apr 2009 18:37:39 -0400 Subject: [Tutor] PDF to text conversion In-Reply-To: <9c2c8ffb0904211202m3a9e608ewa458fb14737edf1b@mail.gmail.com> References: <50e459210904191645i31d5e33dnfa358b4db3469236@mail.gmail.com> <5210161E-AFA9-48D5-B5F8-1C63B3F87E15@rscorp.ab.ca> <50e459210904201901m259f35aai911f9e29c2fae9de@mail.gmail.com> <91655.74845.qm@web86703.mail.ird.yahoo.com> <49EDF8F7.2090508@cfl.rr.com> <49EE1423.6060900@gmail.com> <9c2c8ffb0904211202m3a9e608ewa458fb14737edf1b@mail.gmail.com> Message-ID: <49EE4AB3.4040103@cfl.rr.com> First, thanks to everyone who contributed to this thread. I have a number of possible solutions and a number of paths to pursue to determine which avenue I should take to resolve this remaining issue. I did try the itools library and while everything installed nicely, most of the tests failed so I am not particularly overjoyed with the results. Thank you Dinesh for the vote of sympathy. I do appreciate it. I did use Adobe Reader to convert the history PDF file into a text file and it did seem to do it faithfully. So now I will work out a parsing function to extract my data and send it to a SQLLITE database. I am thrilled both with the number of suggestions I have received from this group and the quality of the suggestions. Thanks again, Robert Berman Norman Khine wrote: > the itools library from hforge.org has a PDF2TEXT implementation itools.pdf > > http://www.hforge.org/itools > > norman > > On Tue, Apr 21, 2009 at 8:44 PM, Dayo Adewunmi wrote: > >> Emile van Sebille wrote: >> >>> Robert Berman wrote: >>> >>> >>> >>>> Have any of you worked with such a library, or do you know of one or two >>>> I can download and work with? Hopefully, they have reasonable documentation. >>>> >>>> My development environment is: >>>> >>>> Python >>>> Linux >>>> Ubuntu version 8.10 >>>> >>>> >>> I've used >>> >>> [root at fcfw2 /]# /usr/bin/pdftotext -v >>> pdftotext version 2.01 >>> Copyright 1996-2002 Glyph & Cog, LLC >>> [root at fcfw2 /]# cat /etc/issue >>> Red Hat Linux release 9 (Shrike) >>> >>> >>> HTH, >>> >>> Emile >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> http://mail.python.org/mailman/listinfo/tutor >>> >>> >> Hi Robert, >> pdftotext is part of poppler-utils, an Ubuntu package which can be installed >> like so: >> >> sudo aptitude install poppler-utils >> >> But I to would be interested in finding a python library/module for this. >> >> Regards, >> >> Dayo >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > From dineshbvadhia at hotmail.com Wed Apr 22 12:27:55 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Wed, 22 Apr 2009 03:27:55 -0700 Subject: [Tutor] PDF to text conversion Message-ID: The best converter so far is pdftotext from http://www.glyphandcog.com/ who maintain an open source project at http://www.foolabs.com/xpdf/. It's not a Python library but you can call pdftotext from with Python using os.system(). I used the pdftotext -layout option and that gave the best result. hth. dinesh -------------------------------------------------------------------------------- Message: 4 Date: Tue, 21 Apr 2009 18:37:39 -0400 From: Robert Berman Subject: Re: [Tutor] PDF to text conversion To: tutor at python.org Message-ID: <49EE4AB3.4040103 at cfl.rr.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed First, thanks to everyone who contributed to this thread. I have a number of possible solutions and a number of paths to pursue to determine which avenue I should take to resolve this remaining issue. I did try the itools library and while everything installed nicely, most of the tests failed so I am not particularly overjoyed with the results. Thank you Dinesh for the vote of sympathy. I do appreciate it. I did use Adobe Reader to convert the history PDF file into a text file and it did seem to do it faithfully. So now I will work out a parsing function to extract my data and send it to a SQLLITE database. I am thrilled both with the number of suggestions I have received from this group and the quality of the suggestions. Thanks again, Robert Berman -------------- next part -------------- An HTML attachment was scrubbed... URL: From bermanrl at cfl.rr.com Wed Apr 22 14:00:43 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 22 Apr 2009 08:00:43 -0400 Subject: [Tutor] PDF to text conversion In-Reply-To: References: Message-ID: <49EF06EB.4080905@cfl.rr.com> Dinesh, I have pdftotext version 3.0.0. I have decided to use this to go from PDF to text. It is not the ideal solution, but is is a certainly doable solution. Thank you, Robert Dinesh B Vadhia wrote: > The best converter so far is pdftotext from > http://www.glyphandcog.com/ who maintain an open source project at > http://www.foolabs.com/xpdf/. > > It's not a Python library but you can call pdftotext from with Python > using os.system(). I used the pdftotext -layout option and that gave > the best result. hth. > > dinesh > > ------------------------------------------------------------------------ > Message: 4 > Date: Tue, 21 Apr 2009 18:37:39 -0400 > From: Robert Berman > > Subject: Re: [Tutor] PDF to text conversion > To: tutor at python.org > Message-ID: <49EE4AB3.4040103 at cfl.rr.com > > > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > First, thanks to everyone who contributed to this thread. I have a > number of possible solutions and a number of paths to pursue to > determine which avenue I should take to resolve this remaining issue. I > did try the itools library and while everything installed nicely, most > of the tests failed so I am not particularly overjoyed with the results. > > Thank you Dinesh for the vote of sympathy. I do appreciate it. > > I did use Adobe Reader to convert the history PDF file into a text file > and it did seem to do it faithfully. So now I will work out a parsing > function to extract my data and send it to a SQLLITE database. > > I am thrilled both with the number of suggestions I have received from > this group and the quality of the suggestions. > > Thanks again, > > Robert Berman > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From david at abbottdavid.com Wed Apr 22 14:11:27 2009 From: david at abbottdavid.com (David) Date: Wed, 22 Apr 2009 08:11:27 -0400 Subject: [Tutor] PDF to text conversion In-Reply-To: <49EF06EB.4080905@cfl.rr.com> References: <49EF06EB.4080905@cfl.rr.com> Message-ID: <49EF096F.2080204@abbottdavid.com> Robert Berman wrote: > Dinesh, > > I have pdftotext version 3.0.0. I have decided to use this to go from > PDF to text. It is not the ideal solution, but is is a certainly doable > solution. > > Thank you, > > Robert > > Dinesh B Vadhia wrote: >> The best converter so far is pdftotext from >> http://www.glyphandcog.com/ who maintain an open source project at >> http://www.foolabs.com/xpdf/. >> >> It's not a Python library but you can call pdftotext from with Python >> using os.system(). I used the pdftotext -layout option and that gave >> the best result. hth. >> >> dinesh >> You can use subprocess; #!/usr/bin/python from subprocess import call call(['pdftotext', 'test.pdf']) -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From eduardo.susan at gmail.com Wed Apr 22 16:49:22 2009 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Wed, 22 Apr 2009 08:49:22 -0600 Subject: [Tutor] Organizing my code, should I use functions? Message-ID: <9356b9f30904220749o2007ec41s92a660c6bc93de8c@mail.gmail.com> Hello! I?m not a programmer and am a beginner with Python, I would like suggestion about ways of tackling the task I had to do. I have bee been using the xlrd package to get data from some spreadsheets successfully. I have developed a simple report from a script that compares number of shipments recorded in 2 spreadsheets, with data from previous years and current year, respectively. Later on I was asked to include in the same report numbers from another branch of our company. So, basically all the code logic was the same, only two different files needed to be processed, and I wondered how would I save time avoiding repeating code, creating extra variables, etc. So, I simply decided to alter the code as little as possible and use the import statement. It's working this way. Sounds wise? I thought if I converted it to a function or a class would make it more flexible, but my understanding of classes are too basic yet, and am not sure if it's worth using it. I have noticed that in lot's of python code example, people make functions out of everything. I'm still too much attached to procedural, not functional programming, if I understand the terms correctly. I would appreciate any suggestions. Here is the code: ## This script creates a report of shipments ## comparing the numbers from the previous year with the current year. import xlrd # Package to read Excel files import os import glob import time import datetime import dbi import odbc thismonth = time.strftime('%B', time.gmtime()) # Get's a string with the name of the current month hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) # Two excel files to process with shipping information thisyearFile = r'c:\Shipping Totals - 2009\Shipping Totals 2009 West.xls' prevyearFile = r'c:\Shipping Totals\Shipping Totals - 2008\Shipping Totals 2008 West.xls' thisDay = time.gmtime()[:3] prevyear = datetime.datetime.today() - datetime.timedelta(days=365) aYearAgo = prevyear.timetuple()[:3] ###### --- Code to find the right cell with today's shipments book = xlrd.open_workbook(thisyearFile) # Opens excel file dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) #Puts the date in the Excel format, like 3991.0 sh = book.sheet_by_name(thismonth) # The sheet which has the name of the month: April, May, June, etc. firstCol = sh.col_values(0) # Retrieves the first column tperday = firstCol.index('TOTAL Per Day') # Finds the cell 'Total Per Day' shipindex = tperday + 1 # The next cell after 'TOTAL Per Day', which contains the info about shipments # Looks for the column whose header is today's date for cols in range(sh.ncols): ? ?for valores in sh.col_values(cols): ? ? ? ?if valores == dayexcel: # If it finds the column with today's date ? ? ? ? ? ?todaysList = sh.col_values(cols) ? ? ? ? ? ?totals = sh.row_values(shipindex, end_colx=cols + 1) # sum up to the current date ? ? ? ? ? ?break # Crosses rows with column to find the right cell with the shipments of today shippedToday = todaysList[shipindex] totalShipments = sum([a for a in totals if type(a) == type(2.0)]) # Sums all shipments # Check previous year's shipments processing the file with last year's data booktwo = xlrd.open_workbook(prevyearFile) dayexcel = xlrd.xldate.xldate_from_date_tuple(aYearAgo, book.datemode) sh = booktwo.sheet_by_name(thismonth) firstCol = sh.col_values(0) tperday = firstCol.index('TOTAL Per Day') shipindex = tperday + 1 for cols in range(sh.ncols): ? ?for valores in sh.col_values(cols): ? ? ? ?if valores == dayexcel: ? ? ? ? ? ?lastyearsList = sh.col_values(cols) ? ? ? ? ? ?totals = sh.row_values(shipindex, end_colx=cols + 1) # sum up to the current date ? ? ? ? ? ?break shippedLastYear = lastyearsList[shipindex] OldTotalShipments = sum([a for a in totals if type(a) == type(2.0)]) # Imports the information from the Eastern division. See code on the bottom import bizreportereast as bz report = """ ===== Shipments Alberta Warehouse ===== - Shipments today: %d - Shipments this month: %d - Shipments this day, last year: %d - Shipments this month, last year: %d ===== Shipments Ontario Warehouse ===== - Shipments today: %d - Shipments this month: %d - Shipments this day, last year: %d - Shipments this month, last year: %d """ % (shippedToday, totalShipments, shippedLastYear, OldTotalShipments, bz.shippedToday, bz.totalShipments, bz.shippedLastYear, bz.OldTotalShipments) print report logfile = open('c:/myscripts/logbizreport.log', 'a') #### Code found in bizreportereast.py #### import xlrd import os import glob import time import datetime ###### --- Code to find the right cell with today's shipments thismonth = time.strftime('%B', time.gmtime()) hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) thisyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - 2009\Shipping Totals 2009 East.xls' prevyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - 2008\Shipping Totals 2008 East.xls' thisDay = time.gmtime()[:3] prevyear = datetime.datetime.today() - datetime.timedelta(days=365) aYearAgo = prevyear.timetuple()[:3] book = xlrd.open_workbook(thisyearFile) dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) sh = book.sheet_by_name(thismonth) firstCol = sh.col_values(0) From bgailer at gmail.com Wed Apr 22 17:47:00 2009 From: bgailer at gmail.com (bob gailer) Date: Wed, 22 Apr 2009 11:47:00 -0400 Subject: [Tutor] Organizing my code, should I use functions? In-Reply-To: <9356b9f30904220749o2007ec41s92a660c6bc93de8c@mail.gmail.com> References: <9356b9f30904220749o2007ec41s92a660c6bc93de8c@mail.gmail.com> Message-ID: <49EF3BF4.7080806@gmail.com> Eduardo Vieira wrote: > Hello! I?m not a programmer Do you mean that you were not a programmer and are now becoming one? > [snip] I wondered how would I save time avoiding repeating code, creating extra > variables, etc. So, I simply decided to alter the code as little as > possible and use the import statement. > It's working this way. Sounds > wise? I thought if I converted it to a function or a class would make > it more flexible, but my understanding of classes are too basic yet, > and am not sure if it's worth using it. > I have noticed that in lot's of python code example, people make > functions out of everything. I'm still too much attached to > procedural, not functional programming, if I understand the terms > correctly. I would appreciate any suggestions. > "functional programming" is a pretty specific concept. Using Python functions is not necessarily "functional programming". It is procedural. IMHO there are reasons for writing functions: 1) modularity - break up a "long" program into relatively independent pieces. 2) factor out common code. 3) package the "main" program so a) it can be conditionally called when the module is run and not when imported b) other function definitions can be written following the main program. Applying 2) to your example I find the following code is duplicated except for variable names. I removed comments and whitespace to make it more readable (to me). book = xlrd.open_workbook(thisyearFile) dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) sh = book.sheet_by_name(thismonth) firstCol = sh.col_values(0) tperday = firstCol.index('TOTAL Per Day') shipindex = tperday + 1 for valores in sh.col_values(cols): if valores == dayexcel: todaysList = sh.col_values(cols) totals = sh.row_values(shipindex, end_colx=cols + 1) break shippedToday = todaysList[shipindex] totalShipments = sum([a for a in totals if type(a) == type(2.0)]) To convert to a function ask: - what are the inputs? - what are the outputs? The input I see is the excel file name. The outputs I see are # shipped today and total # shipped. That leads to a function: def getShipmentInfo(xlfilename): # specify the input(s) here ###### --- Code to find the right cell with today's shipments book = xlrd.open_workbook(xlfilename) thisDay = time.gmtime()[:3] dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) thismonth = time.strftime('%B', time.gmtime()) sh = book.sheet_by_name(thismonth) firstCol = sh.col_values(0) tperday = firstCol.index('TOTAL Per Day') shipindex = tperday + 1 for valores in sh.col_values(cols): if valores == dayexcel: todaysList = sh.col_values(cols) totals = sh.row_values(shipindex, end_colx=cols + 1) break shipped = todaysList[shipindex] total = sum([a for a in totals if type(a) == type(2.0)]) return shipped, total # specify the output(s) here Eliminate unused statements and whitespace. Put the main program in a main function (reason 3) above) The program now looks like: ## This script creates a report of shipments ## comparing the numbers from the previous year with the current year. import xlrd # Package to read Excel files import time import datetime def main(): # Two excel files to process with shipping information thisyearFile = r'c:\Shipping Totals - 2009\Shipping Totals 2009 West.xls' prevyearFile = r'c:\Shipping Totals\Shipping Totals - 2008\ShippingTotals 2008 West.xls' shippedToday, totalShipments = getShipmentInfo(thisyearFile) shippedLastYear, OldTotalShipments = getShipmentInfo(prevyearFile) # Imports the information from the Eastern division. See code on the bottom .... rest of main program def getShipmentInfo(xlfilename): # specify the input(s) here .... rest of function code from above if __name__ == "__main__": main() This is not the only way to use functions or improve things. I leave dealing with the east data to someone else, but I think we can apply the same principles. > Here is the code: > > ## This script creates a report of shipments ## comparing the numbers > from the previous year with the current year. > > import xlrd # Package to read Excel files import os import glob import > time import datetime import dbi import odbc > > > > > thismonth = time.strftime('%B', time.gmtime()) # Get's a string with > the name of the current month > > hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) > > > # Two excel files to process with shipping information > > thisyearFile = r'c:\Shipping Totals - 2009\Shipping Totals 2009 West.xls' > prevyearFile = r'c:\Shipping Totals\Shipping Totals - 2008\Shipping > Totals 2008 West.xls' > > > > thisDay = time.gmtime()[:3] > prevyear = datetime.datetime.today() - datetime.timedelta(days=365) > aYearAgo = prevyear.timetuple()[:3] > > ###### --- Code to find the right cell with today's shipments > > book = xlrd.open_workbook(thisyearFile) # Opens excel file > > dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) > #Puts the date in the Excel format, like 3991.0 > > > sh = book.sheet_by_name(thismonth) # The sheet which has the name of > the month: April, May, June, etc. > > firstCol = sh.col_values(0) # Retrieves the first column > > tperday = firstCol.index('TOTAL Per Day') # Finds the cell 'Total Per Day' > > shipindex = tperday + 1 # The next cell after 'TOTAL Per Day', which > contains the info about shipments > > # Looks for the column whose header is today's date for cols in range(sh.ncols): > for valores in sh.col_values(cols): > if valores == dayexcel: # If it finds the column with today's date > todaysList = sh.col_values(cols) > totals = sh.row_values(shipindex, end_colx=cols + 1) # sum > up to the current date > break > > # Crosses rows with column to find the right cell with the shipments > of today shippedToday = todaysList[shipindex] > > totalShipments = sum([a for a in totals if type(a) == type(2.0)]) # > Sums all shipments > > > # Check previous year's shipments processing the file with last year's data > > booktwo = xlrd.open_workbook(prevyearFile) > > dayexcel = xlrd.xldate.xldate_from_date_tuple(aYearAgo, book.datemode) > > sh = booktwo.sheet_by_name(thismonth) > > firstCol = sh.col_values(0) > > > > tperday = firstCol.index('TOTAL Per Day') > > shipindex = tperday + 1 > > for cols in range(sh.ncols): > for valores in sh.col_values(cols): > if valores == dayexcel: > lastyearsList = sh.col_values(cols) > totals = sh.row_values(shipindex, end_colx=cols + 1) # sum > up to the current date > break > > > shippedLastYear = lastyearsList[shipindex] > > > OldTotalShipments = sum([a for a in totals if type(a) == type(2.0)]) > > > # Imports the information from the Eastern division. See code on the bottom > > import bizreportereast as bz > > > report = """ > > > ===== Shipments Alberta Warehouse ===== > > - Shipments today: %d > > - Shipments this month: %d > > - Shipments this day, last year: %d > > - Shipments this month, last year: %d > > > ===== Shipments Ontario Warehouse ===== > > - Shipments today: %d > > - Shipments this month: %d > > - Shipments this day, last year: %d > > - Shipments this month, last year: %d > > > """ % (shippedToday, totalShipments, shippedLastYear, > OldTotalShipments, bz.shippedToday, bz.totalShipments, > bz.shippedLastYear, bz.OldTotalShipments) > > print report > > logfile = open('c:/myscripts/logbizreport.log', 'a') > > > #### Code found in bizreportereast.py #### import xlrd import os > import glob import time import datetime > > > ###### --- Code to find the right cell with today's shipments > > thismonth = time.strftime('%B', time.gmtime()) > > hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) > > thisyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - > 2009\Shipping Totals 2009 East.xls' > prevyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - > 2008\Shipping Totals 2008 East.xls' > > > > thisDay = time.gmtime()[:3] > prevyear = datetime.datetime.today() - datetime.timedelta(days=365) > aYearAgo = prevyear.timetuple()[:3] > > > > book = xlrd.open_workbook(thisyearFile) > > dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) > > > sh = book.sheet_by_name(thismonth) > > firstCol = sh.col_values(0) > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Bob Gailer Chapel Hill NC 919-636-4239 From alan.gauld at btinternet.com Wed Apr 22 17:52:49 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 22 Apr 2009 16:52:49 +0100 Subject: [Tutor] Organizing my code, should I use functions? References: <9356b9f30904220749o2007ec41s92a660c6bc93de8c@mail.gmail.com> Message-ID: "Eduardo Vieira" wrote > ....I simply decided to alter the code as little as > possible and use the import statement. It's working this way. > Sounds wise? Its an acceptab;e approach bwhen you are doing exactly the same thing, but its not a very flexible approach and leaves you very vulnerable if you get a change that only applies to one of your solutions. > I thought if I converted it to a function or a class would make > it more flexible, A set of functions is probably sufficient from the sounds of things. That way you can mix n match the functions, pass in different arguments etc to get the same functionality but with more control. > functions out of everything. I'm still too much attached to > procedural, not functional programming, if I understand the terms > correctly. procedural programming is where you use procedures or functions to accomplish the task. You link those functions together, either in a top level function or in global level cotrol script. This is also (more correctly) known as imperative style programming. functional programming is quite different and comprises a set of rules about how the code should be structured. In functional programming the whole program is a function that returns a value. Every function should return a value and functions should not alter any global data. Every function should ideally be an expression. No stateful constructs such as loops or assignment statements should be used (repetition is done via recursion). In practice most folks relax the rules a little and allow simple loops (ie using a counter as in for n in range(n)) and some assignments. But the ideal is to build a program with no state and everything done as an expression with nested sub expressions. It is very closely aligned with formal methods in Computer Science and formal languages such as Z or VDM For most programmers functional programming is at least as big a conceptual leap from procedural or imperative programming as OOP. I'll comment on the code separately. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From johnjenkinson1 at gmail.com Wed Apr 22 17:54:02 2009 From: johnjenkinson1 at gmail.com (John Jenkinson) Date: Wed, 22 Apr 2009 10:54:02 -0500 Subject: [Tutor] Number cruncher Message-ID: <58fd32380904220854j5401edd7ld6d0c4b7c5ebbaa8@mail.gmail.com> I need to write code that determines a students final grade based on the number of each type of assignment turned in with its corresponding weighted percentage. There will be 15-20 inputs. I have not reached this point in my learning of python, so I would like to request a resource that would help me for this project. - John Jenkinson -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Wed Apr 22 18:07:53 2009 From: denis.spir at free.fr (spir) Date: Wed, 22 Apr 2009 18:07:53 +0200 Subject: [Tutor] Organizing my code, should I use functions? In-Reply-To: <9356b9f30904220749o2007ec41s92a660c6bc93de8c@mail.gmail.com> References: <9356b9f30904220749o2007ec41s92a660c6bc93de8c@mail.gmail.com> Message-ID: <20090422180753.1c41eb14@o> Le Wed, 22 Apr 2009 08:49:22 -0600, Eduardo Vieira s'exprima ainsi: > Hello! I?m not a programmer and am a beginner with Python, I would > like suggestion about ways of tackling the task I had to do. I have > bee been using the xlrd package to get data from some spreadsheets > successfully. > I have developed a simple report from a script that compares number of > shipments recorded in 2 spreadsheets, with data from previous years > and current year, respectively. > Later on I was asked to include in the same report numbers from > another branch of our company. So, basically all the code logic was > the same, only two different files needed to be processed, and I > wondered how would I save time avoiding repeating code, creating extra > variables, etc. So, I simply decided to alter the code as little as > possible and use the import statement. It's working this way. Sounds > wise? Functions are basically for two goals: * To structure your code according to the logic you have in mind and/or organise it for practicle and clarity reasons. * To *define* and name a block of code intended to be run later, through a call, possibly several times, especially when it gains flexibility with parameters. The first purpose is always good. The second one meets yours needs: to make your code able to process data from any spreadsheet. Even when importing a module (which is rather intended for higher level organisation), you still should use funcs inside the processing part of the program. > I thought if I converted it to a function or a class would make > it more flexible, but my understanding of classes are too basic yet, > and am not sure if it's worth using it. I guess classes are not really necessary here... > I have noticed that in lot's of python code example, people make > functions out of everything. I'm still too much attached to > procedural, not functional programming, if I understand the terms > correctly. I would appreciate any suggestions. > ... but behind misleading names functions are precisely the main structuring tool for procedural programming. [The distinction between procedural & functional paradigms is based on other traits and rather related to the math. sense of 'function'.] 'Functions' in python simply are named blocks of code, possibly with parameters, that perform an action and/or return a result. [They are also a kind of object but this is another story.] Denis ------ la vita e estrany From david at abbottdavid.com Wed Apr 22 18:16:25 2009 From: david at abbottdavid.com (David) Date: Wed, 22 Apr 2009 12:16:25 -0400 Subject: [Tutor] Number cruncher In-Reply-To: <58fd32380904220854j5401edd7ld6d0c4b7c5ebbaa8@mail.gmail.com> References: <58fd32380904220854j5401edd7ld6d0c4b7c5ebbaa8@mail.gmail.com> Message-ID: <49EF42D9.4020903@abbottdavid.com> John Jenkinson wrote: > I need to write code that determines a students final grade based on the > number of each type of assignment turned in with its corresponding > weighted percentage. There will be 15-20 inputs. I have not reached > this point in my learning of python, so I would like to request a > resource that would help me for this project. > > - John Jenkinson > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor If you want to start here; http://pytut.infogami.com/beginners When you get to 9 you will be able to do your lesson. -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From bermanrl at cfl.rr.com Wed Apr 22 18:28:26 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 22 Apr 2009 12:28:26 -0400 Subject: [Tutor] Number cruncher In-Reply-To: <58fd32380904220854j5401edd7ld6d0c4b7c5ebbaa8@mail.gmail.com> References: <58fd32380904220854j5401edd7ld6d0c4b7c5ebbaa8@mail.gmail.com> Message-ID: <49EF45AA.3040606@cfl.rr.com> Hello John, It is the general policy of the 'tutors group' to not solve homework problems. Given that very basic rule, there are obvious elements for which we can definitely give help. But, you have to take the first step and that is a relatively big one. You have to define an algorithm or recipe to solve the problem. To do so, you must define your inputs. As a first comment, while you did specify that each assignment is weighted, you gave no indication that a grade was a required input. Obviously a heavily weighted '88' may carry more import than a very light weight '94'. What you need to do is define (1) Your inputs, (2) your outputs (what comes out of the recipe and how you want it served. ie, how displayed. And then, once you have defined the obvious you must resolve the heart of the algorithm, how do you compute the outputs. Hopefully, while this is certainly not 'the' answer it will get you started. Robert Berman John Jenkinson wrote: > I need to write code that determines a students final grade based on > the number of each type of assignment turned in with its corresponding > weighted percentage. There will be 15-20 inputs. I have not reached > this point in my learning of python, so I would like to request a > resource that would help me for this project. > > - John Jenkinson > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From Snice14247 at aol.com Wed Apr 22 18:08:53 2009 From: Snice14247 at aol.com (IT_ForMe) Date: Wed, 22 Apr 2009 09:08:53 -0700 (PDT) Subject: [Tutor] Python help Message-ID: <23175541.post@talk.nabble.com> Anyone know how to program this using Python Add up the prices for items in a shopping cart. Some items are taxable and some items are not. You may assume a 7% tax rate for all taxable items. Apply a 10% discount if the customer is a student. (hint, instead of asking the user to input items, set them up as an associative array in the code) -- View this message in context: http://www.nabble.com/Python-help-tp23175541p23175541.html Sent from the Python - tutor mailing list archive at Nabble.com. From cfuller at thinkingplanet.net Wed Apr 22 18:20:05 2009 From: cfuller at thinkingplanet.net (Chris Fuller) Date: Wed, 22 Apr 2009 11:20:05 -0500 Subject: [Tutor] Number cruncher In-Reply-To: <58fd32380904220854j5401edd7ld6d0c4b7c5ebbaa8@mail.gmail.com> References: <58fd32380904220854j5401edd7ld6d0c4b7c5ebbaa8@mail.gmail.com> Message-ID: <200904221120.05779.cfuller@thinkingplanet.net> Most teachers use spreadsheets for this. Is there a reason you'd like to use Python? A spreadsheet would be easier to create and less error prone. If you don't have a copy of Microsoft Orifice handy, you can use Gnumeric or Open Office. Cheers On Wednesday 22 April 2009 10:54, John Jenkinson wrote: > I need to write code that determines a students final grade based on the > number of each type of assignment turned in with its corresponding weighted > percentage. There will be 15-20 inputs. I have not reached this point in > my learning of python, so I would like to request a resource that would > help me for this project. > > - John Jenkinson From bermanrl at cfl.rr.com Wed Apr 22 19:21:39 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Wed, 22 Apr 2009 13:21:39 -0400 Subject: [Tutor] Python help In-Reply-To: <23175541.post@talk.nabble.com> References: <23175541.post@talk.nabble.com> Message-ID: <49EF5223.3020209@cfl.rr.com> Yes. Show us your solution and perhaps we can help you make it a tad more efficient. Robert Berman IT_ForMe wrote: > Anyone know how to program this using Python > > Add up the prices for items in a shopping cart. Some items are taxable and > some items are not. You may assume a 7% tax rate for all taxable items. > Apply a 10% discount if the customer is a student. (hint, instead of asking > the user to input items, set them up as an associative array in the code) > From srilyk at gmail.com Wed Apr 22 19:23:05 2009 From: srilyk at gmail.com (W W) Date: Wed, 22 Apr 2009 12:23:05 -0500 Subject: [Tutor] Python help In-Reply-To: <23175541.post@talk.nabble.com> References: <23175541.post@talk.nabble.com> Message-ID: <333efb450904221023n22aef1b1g43bf749abbe732bd@mail.gmail.com> On Wed, Apr 22, 2009 at 11:08 AM, IT_ForMe wrote: > > Anyone know how to program this using Python > > Add up the prices for items in a shopping cart. Some items are taxable and > some items are not. You may assume a 7% tax rate for all taxable items. > Apply a 10% discount if the customer is a student. (hint, instead of asking > the user to input items, set them up as an associative array in the code) My guess is most of us could do it, and this is homework. We have a policy against that sort of thing, but if you'd like to give it a try and then ask for help if you get stuck, we're more than willing to help. -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Wed Apr 22 20:15:23 2009 From: bgailer at gmail.com (bob gailer) Date: Wed, 22 Apr 2009 14:15:23 -0400 Subject: [Tutor] Organizing my code, should I use functions? In-Reply-To: <9356b9f30904220938u2d88d538nd59cf362b6fc280e@mail.gmail.com> References: <9356b9f30904220749o2007ec41s92a660c6bc93de8c@mail.gmail.com> <49EF3BF4.7080806@gmail.com> <9356b9f30904220938u2d88d538nd59cf362b6fc280e@mail.gmail.com> Message-ID: <49EF5EBB.8090706@gmail.com> Please use reply-all so a copy goes to the list. Eduardo Vieira wrote: > On Wed, Apr 22, 2009 at 9:47 AM, bob gailer wrote: > >> Eduardo Vieira wrote: >> >>> Hello! I?m not a programmer >>> >> Do you mean that you were not a programmer and are now becoming one? >> >> > > In a way yes, I am aspiring to be one, but because of circumstances in > my life I won't be going to take schooling for that. > > > >> To convert to a function ask: - what are the inputs? - what are the outputs? >> >> The input I see is the excel file name. The outputs I see are # shipped >> today and total # shipped. >> >> That leads to a function: >> >> def getShipmentInfo(xlfilename): # specify the input(s) here >> ###### --- Code to find the right cell with today's shipments >> book = xlrd.open_workbook(xlfilename) >> thisDay = time.gmtime()[:3] >> dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) >> thismonth = time.strftime('%B', time.gmtime()) sh = >> book.sheet_by_name(thismonth) firstCol = sh.col_values(0) tperday = >> firstCol.index('TOTAL Per Day') shipindex = tperday + 1 for valores in >> sh.col_values(cols): >> if valores == dayexcel: todaysList = sh.col_values(cols) >> totals = sh.row_values(shipindex, end_colx=cols + 1) >> break >> shipped = todaysList[shipindex] >> total = sum([a for a in totals if type(a) == type(2.0)]) >> return shipped, total # specify the output(s) here >> >> Eliminate unused statements and whitespace. >> Put the main program in a main function (reason 3) above) >> The program now looks like: >> >> ## This script creates a report of shipments ## comparing the numbers from >> the previous year with the current year. >> import xlrd # Package to read Excel files import time import datetime def >> main(): >> # Two excel files to process with shipping information >> thisyearFile = r'c:\Shipping Totals - 2009\Shipping Totals 2009 West.xls' >> prevyearFile = r'c:\Shipping Totals\Shipping Totals - 2008\ShippingTotals >> 2008 West.xls' >> shippedToday, totalShipments = getShipmentInfo(thisyearFile) >> shippedLastYear, OldTotalShipments = getShipmentInfo(prevyearFile) >> >> # Imports the information from the Eastern division. See code on the >> bottom >> .... rest of main program >> def getShipmentInfo(xlfilename): # specify the input(s) here >> .... rest of function code from above >> if __name__ == "__main__": >> main() >> >> > Thank you for your help. I guess actually I will have to create 2 > functions: one for this year, and other for the previous year, right? > Because the calculations are different, as you can see in my original > code. > The ONLY difference I can see is the use of current vs prior year, which I missed. I also noticed that my code got clobbered, as did yours, by the email program. I will attempt to get it right this time and fix the year problem. add to main program: thisDay = time.gmtime()[:3] aYearAgo = prevyear.timetuple()[:3] modify function: def getShipmentInfo(xlfilename, day): # specify the input(s) here ###### --- Code to find the right cell with today's shipments book = xlrd.open_workbook(xlfilename) thisDay = time.gmtime()[:3] dayexcel = xlrd.xldate.xldate_from_date_tuple(day, book.datemode) thismonth = time.strftime('%B', time.gmtime()) sh = book.sheet_by_name(thismonth) firstCol = sh.col_values(0) tperday = firstCol.index('TOTAL Per Day') shipindex = tperday + 1 for cols in range(sh.ncols): for valores in sh.col_values(cols): if valores == dayexcel: todaysList = sh.col_values(cols) totals = sh.row_values(shipindex, end_colx=cols + 1) break shipped = todaysList[shipindex] total = sum([a for a in totals if type(a) == type(2.0)]) return shipped, total # specify the output(s) here modify calls to function: shippedToday, totalShipments = getShipmentInfo(thisyearFile, thisDay) shippedLastYear, OldTotalShipments = getShipmentInfo(prevyearFile, aYearAgo) Now the 1 function should do what you want. > > >> This is not the only way to use functions or improve things. >> >> I leave dealing with the east data to someone else, but I think we can apply >> the same principles. >> >> >>> Here is the code: >>> >>> ## This script creates a report of shipments ## comparing the numbers >>> from the previous year with the current year. >>> >>> import xlrd # Package to read Excel files import os import glob import >>> time import datetime import dbi import odbc >>> >>> >>> >>> >>> thismonth = time.strftime('%B', time.gmtime()) # Get's a string with >>> the name of the current month >>> >>> hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) >>> >>> >>> # Two excel files to process with shipping information >>> >>> thisyearFile = r'c:\Shipping Totals - 2009\Shipping Totals 2009 West.xls' >>> prevyearFile = r'c:\Shipping Totals\Shipping Totals - 2008\Shipping >>> Totals 2008 West.xls' >>> >>> >>> >>> thisDay = time.gmtime()[:3] >>> prevyear = datetime.datetime.today() - datetime.timedelta(days=365) >>> aYearAgo = prevyear.timetuple()[:3] >>> >>> ###### --- Code to find the right cell with today's shipments >>> >>> book = xlrd.open_workbook(thisyearFile) # Opens excel file >>> >>> dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) >>> #Puts the date in the Excel format, like 3991.0 >>> >>> >>> sh = book.sheet_by_name(thismonth) # The sheet which has the name of >>> the month: April, May, June, etc. >>> >>> firstCol = sh.col_values(0) # Retrieves the first column >>> >>> tperday = firstCol.index('TOTAL Per Day') # Finds the cell 'Total Per Day' >>> >>> shipindex = tperday + 1 # The next cell after 'TOTAL Per Day', which >>> contains the info about shipments >>> >>> # Looks for the column whose header is today's date for cols in >>> range(sh.ncols): >>> for valores in sh.col_values(cols): >>> if valores == dayexcel: # If it finds the column with today's date >>> todaysList = sh.col_values(cols) >>> totals = sh.row_values(shipindex, end_colx=cols + 1) # sum >>> up to the current date >>> break >>> >>> # Crosses rows with column to find the right cell with the shipments >>> of today shippedToday = todaysList[shipindex] >>> >>> totalShipments = sum([a for a in totals if type(a) == type(2.0)]) # >>> Sums all shipments >>> >>> >>> # Check previous year's shipments processing the file with last year's >>> data >>> >>> booktwo = xlrd.open_workbook(prevyearFile) >>> >>> dayexcel = xlrd.xldate.xldate_from_date_tuple(aYearAgo, book.datemode) >>> >>> sh = booktwo.sheet_by_name(thismonth) >>> >>> firstCol = sh.col_values(0) >>> >>> >>> >>> tperday = firstCol.index('TOTAL Per Day') >>> >>> shipindex = tperday + 1 >>> >>> for cols in range(sh.ncols): >>> for valores in sh.col_values(cols): >>> if valores == dayexcel: >>> lastyearsList = sh.col_values(cols) >>> totals = sh.row_values(shipindex, end_colx=cols + 1) # sum >>> up to the current date >>> break >>> >>> >>> shippedLastYear = lastyearsList[shipindex] >>> >>> >>> OldTotalShipments = sum([a for a in totals if type(a) == type(2.0)]) >>> >>> >>> # Imports the information from the Eastern division. See code on the >>> bottom >>> >>> import bizreportereast as bz >>> >>> >>> report = """ >>> >>> >>> ===== Shipments Alberta Warehouse ===== >>> >>> - Shipments today: %d >>> >>> - Shipments this month: %d >>> >>> - Shipments this day, last year: %d >>> >>> - Shipments this month, last year: %d >>> >>> >>> ===== Shipments Ontario Warehouse ===== >>> >>> - Shipments today: %d >>> >>> - Shipments this month: %d >>> >>> - Shipments this day, last year: %d >>> >>> - Shipments this month, last year: %d >>> >>> >>> """ % (shippedToday, totalShipments, shippedLastYear, >>> OldTotalShipments, bz.shippedToday, bz.totalShipments, >>> bz.shippedLastYear, bz.OldTotalShipments) >>> >>> print report >>> >>> logfile = open('c:/myscripts/logbizreport.log', 'a') >>> >>> >>> #### Code found in bizreportereast.py #### import xlrd import os >>> import glob import time import datetime >>> >>> >>> ###### --- Code to find the right cell with today's shipments >>> >>> thismonth = time.strftime('%B', time.gmtime()) >>> >>> hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) >>> >>> thisyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - >>> 2009\Shipping Totals 2009 East.xls' >>> prevyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - >>> 2008\Shipping Totals 2008 East.xls' >>> >>> >>> >>> thisDay = time.gmtime()[:3] >>> prevyear = datetime.datetime.today() - datetime.timedelta(days=365) >>> aYearAgo = prevyear.timetuple()[:3] >>> >>> >>> >>> book = xlrd.open_workbook(thisyearFile) >>> >>> dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) >>> >>> >>> sh = book.sheet_by_name(thismonth) >>> >>> firstCol = sh.col_values(0) >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> http://mail.python.org/mailman/listinfo/tutor >>> >>> >>> >> -- >> Bob Gailer >> Chapel Hill NC >> 919-636-4239 >> >> > > -- Bob Gailer Chapel Hill NC 919-636-4239 From bgailer at gmail.com Wed Apr 22 20:19:07 2009 From: bgailer at gmail.com (bob gailer) Date: Wed, 22 Apr 2009 14:19:07 -0400 Subject: [Tutor] Organizing my code, should I use functions? INDENTATION CORRECTION In-Reply-To: <49EF5EBB.8090706@gmail.com> References: <9356b9f30904220749o2007ec41s92a660c6bc93de8c@mail.gmail.com> <49EF3BF4.7080806@gmail.com> <9356b9f30904220938u2d88d538nd59cf362b6fc280e@mail.gmail.com> <49EF5EBB.8090706@gmail.com> Message-ID: <49EF5F9B.5060601@gmail.com> INDENTATION CORRECTION: shipped = todaysList[shipindex] total = sum([a for a in totals if type(a) == type(2.0)]) return shipped, total # specify the output(s) here bob gailer wrote: > Please use reply-all so a copy goes to the list. > > Eduardo Vieira wrote: >> On Wed, Apr 22, 2009 at 9:47 AM, bob gailer wrote: >> >>> Eduardo Vieira wrote: >>> >>>> Hello! I?m not a programmer >>>> >>> Do you mean that you were not a programmer and are now becoming one? >>> >>> >> >> In a way yes, I am aspiring to be one, but because of circumstances in >> my life I won't be going to take schooling for that. >> >> >> >>> To convert to a function ask: - what are the inputs? - what are the >>> outputs? >>> >>> The input I see is the excel file name. The outputs I see are # shipped >>> today and total # shipped. >>> >>> That leads to a function: >>> >>> def getShipmentInfo(xlfilename): # specify the input(s) here >>> ###### --- Code to find the right cell with today's shipments >>> book = xlrd.open_workbook(xlfilename) >>> thisDay = time.gmtime()[:3] >>> dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) >>> thismonth = time.strftime('%B', time.gmtime()) sh = >>> book.sheet_by_name(thismonth) firstCol = sh.col_values(0) tperday = >>> firstCol.index('TOTAL Per Day') shipindex = tperday + 1 for >>> valores in >>> sh.col_values(cols): >>> if valores == dayexcel: todaysList = >>> sh.col_values(cols) >>> totals = sh.row_values(shipindex, end_colx=cols + 1) >>> break >>> shipped = todaysList[shipindex] >>> total = sum([a for a in totals if type(a) == type(2.0)]) >>> return shipped, total # specify the output(s) here >>> >>> Eliminate unused statements and whitespace. >>> Put the main program in a main function (reason 3) above) >>> The program now looks like: >>> >>> ## This script creates a report of shipments ## comparing the >>> numbers from >>> the previous year with the current year. >>> import xlrd # Package to read Excel files import time import >>> datetime def >>> main(): >>> # Two excel files to process with shipping information >>> thisyearFile = r'c:\Shipping Totals - 2009\Shipping Totals 2009 >>> West.xls' >>> prevyearFile = r'c:\Shipping Totals\Shipping Totals - >>> 2008\ShippingTotals >>> 2008 West.xls' >>> shippedToday, totalShipments = getShipmentInfo(thisyearFile) >>> shippedLastYear, OldTotalShipments = getShipmentInfo(prevyearFile) >>> >>> # Imports the information from the Eastern division. See code on the >>> bottom >>> .... rest of main program >>> def getShipmentInfo(xlfilename): # specify the input(s) here >>> .... rest of function code from above >>> if __name__ == "__main__": >>> main() >>> >>> >> Thank you for your help. I guess actually I will have to create 2 >> functions: one for this year, and other for the previous year, right? >> Because the calculations are different, as you can see in my original >> code. >> > > The ONLY difference I can see is the use of current vs prior year, > which I missed. > I also noticed that my code got clobbered, as did yours, by the email > program. > I will attempt to get it right this time and fix the year problem. > > add to main program: > > thisDay = time.gmtime()[:3] > aYearAgo = prevyear.timetuple()[:3] > > modify function: > > def getShipmentInfo(xlfilename, day): # specify the input(s) here > ###### --- Code to find the right cell with today's shipments > book = xlrd.open_workbook(xlfilename) > thisDay = time.gmtime()[:3] > dayexcel = xlrd.xldate.xldate_from_date_tuple(day, book.datemode) > thismonth = time.strftime('%B', time.gmtime()) > sh = book.sheet_by_name(thismonth) > firstCol = sh.col_values(0) > tperday = firstCol.index('TOTAL Per Day') > shipindex = tperday + 1 > for cols in range(sh.ncols): > for valores in sh.col_values(cols): > if valores == dayexcel: > todaysList = sh.col_values(cols) > totals = sh.row_values(shipindex, end_colx=cols + 1) > break > shipped = todaysList[shipindex] > total = sum([a for a in totals if type(a) == type(2.0)]) > return shipped, total # specify the output(s) here > > modify calls to function: > > shippedToday, totalShipments = getShipmentInfo(thisyearFile, thisDay) > shippedLastYear, OldTotalShipments = getShipmentInfo(prevyearFile, > aYearAgo) > > > Now the 1 function should do what you want. >> >> >>> This is not the only way to use functions or improve things. >>> >>> I leave dealing with the east data to someone else, but I think we >>> can apply >>> the same principles. >>> >>> >>>> Here is the code: >>>> >>>> ## This script creates a report of shipments ## comparing the numbers >>>> from the previous year with the current year. >>>> >>>> import xlrd # Package to read Excel files import os import glob import >>>> time import datetime import dbi import odbc >>>> >>>> >>>> >>>> >>>> thismonth = time.strftime('%B', time.gmtime()) # Get's a string with >>>> the name of the current month >>>> >>>> hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) >>>> >>>> >>>> # Two excel files to process with shipping information >>>> >>>> thisyearFile = r'c:\Shipping Totals - 2009\Shipping Totals 2009 >>>> West.xls' >>>> prevyearFile = r'c:\Shipping Totals\Shipping Totals - 2008\Shipping >>>> Totals 2008 West.xls' >>>> >>>> >>>> >>>> thisDay = time.gmtime()[:3] >>>> prevyear = datetime.datetime.today() - datetime.timedelta(days=365) >>>> aYearAgo = prevyear.timetuple()[:3] >>>> >>>> ###### --- Code to find the right cell with today's shipments >>>> >>>> book = xlrd.open_workbook(thisyearFile) # Opens excel file >>>> >>>> dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) >>>> #Puts the date in the Excel format, like 3991.0 >>>> >>>> >>>> sh = book.sheet_by_name(thismonth) # The sheet which has the name of >>>> the month: April, May, June, etc. >>>> >>>> firstCol = sh.col_values(0) # Retrieves the first column >>>> >>>> tperday = firstCol.index('TOTAL Per Day') # Finds the cell 'Total >>>> Per Day' >>>> >>>> shipindex = tperday + 1 # The next cell after 'TOTAL Per Day', which >>>> contains the info about shipments >>>> >>>> # Looks for the column whose header is today's date for cols in >>>> range(sh.ncols): >>>> for valores in sh.col_values(cols): >>>> if valores == dayexcel: # If it finds the column with today's >>>> date >>>> todaysList = sh.col_values(cols) >>>> totals = sh.row_values(shipindex, end_colx=cols + 1) # sum >>>> up to the current date >>>> break >>>> >>>> # Crosses rows with column to find the right cell with the shipments >>>> of today shippedToday = todaysList[shipindex] >>>> >>>> totalShipments = sum([a for a in totals if type(a) == type(2.0)]) # >>>> Sums all shipments >>>> >>>> >>>> # Check previous year's shipments processing the file with last year's >>>> data >>>> >>>> booktwo = xlrd.open_workbook(prevyearFile) >>>> >>>> dayexcel = xlrd.xldate.xldate_from_date_tuple(aYearAgo, book.datemode) >>>> >>>> sh = booktwo.sheet_by_name(thismonth) >>>> >>>> firstCol = sh.col_values(0) >>>> >>>> >>>> >>>> tperday = firstCol.index('TOTAL Per Day') >>>> >>>> shipindex = tperday + 1 >>>> >>>> for cols in range(sh.ncols): >>>> for valores in sh.col_values(cols): >>>> if valores == dayexcel: >>>> lastyearsList = sh.col_values(cols) >>>> totals = sh.row_values(shipindex, end_colx=cols + 1) # sum >>>> up to the current date >>>> break >>>> >>>> >>>> shippedLastYear = lastyearsList[shipindex] >>>> >>>> >>>> OldTotalShipments = sum([a for a in totals if type(a) == type(2.0)]) >>>> >>>> >>>> # Imports the information from the Eastern division. See code on the >>>> bottom >>>> >>>> import bizreportereast as bz >>>> >>>> >>>> report = """ >>>> >>>> >>>> ===== Shipments Alberta Warehouse ===== >>>> >>>> - Shipments today: %d >>>> >>>> - Shipments this month: %d >>>> >>>> - Shipments this day, last year: %d >>>> >>>> - Shipments this month, last year: %d >>>> >>>> >>>> ===== Shipments Ontario Warehouse ===== >>>> >>>> - Shipments today: %d >>>> >>>> - Shipments this month: %d >>>> >>>> - Shipments this day, last year: %d >>>> >>>> - Shipments this month, last year: %d >>>> >>>> >>>> """ % (shippedToday, totalShipments, shippedLastYear, >>>> OldTotalShipments, bz.shippedToday, bz.totalShipments, >>>> bz.shippedLastYear, bz.OldTotalShipments) >>>> >>>> print report >>>> >>>> logfile = open('c:/myscripts/logbizreport.log', 'a') >>>> >>>> >>>> #### Code found in bizreportereast.py #### import xlrd import os >>>> import glob import time import datetime >>>> >>>> >>>> ###### --- Code to find the right cell with today's shipments >>>> >>>> thismonth = time.strftime('%B', time.gmtime()) >>>> >>>> hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) >>>> >>>> thisyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - >>>> 2009\Shipping Totals 2009 East.xls' >>>> prevyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - >>>> 2008\Shipping Totals 2008 East.xls' >>>> >>>> >>>> >>>> thisDay = time.gmtime()[:3] >>>> prevyear = datetime.datetime.today() - datetime.timedelta(days=365) >>>> aYearAgo = prevyear.timetuple()[:3] >>>> >>>> >>>> >>>> book = xlrd.open_workbook(thisyearFile) >>>> >>>> dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) >>>> >>>> >>>> sh = book.sheet_by_name(thismonth) >>>> >>>> firstCol = sh.col_values(0) >>>> _______________________________________________ >>>> Tutor maillist - Tutor at python.org >>>> http://mail.python.org/mailman/listinfo/tutor >>>> >>>> >>>> >>> -- >>> Bob Gailer >>> Chapel Hill NC >>> 919-636-4239 >>> >>> >> >> > > -- Bob Gailer Chapel Hill NC 919-636-4239 From yam at nerd.cx Wed Apr 22 20:35:29 2009 From: yam at nerd.cx (William Witteman) Date: Wed, 22 Apr 2009 14:35:29 -0400 Subject: [Tutor] Tokenizing Help Message-ID: <20090422183529.GA19821@yam.witteman.ca> I need to be able to decompose a formatted text file into identifiable, possibly named pieces. To tokenize it, in other words. There seem to be a vast array of modules to do this with (simpleparse, pyparsing etc) but I cannot understand their documentation. The file format I am looking at (it is a bibliographic reference file) looks like this: <1> # the references are enumerated AU - some text perhaps across lines AB - some other text AB - there may be multiples of some fields UN - any 2-letter combination may exist, other than by exhaustion, I cannot anticipate what will be found What I am looking for is some help to get started, either with explaining the implementation of one of the modules with respect to my format, or with an approach that I could use from the base library. Thanks. -- yours, William From wferguson1 at socal.rr.com Wed Apr 22 20:57:13 2009 From: wferguson1 at socal.rr.com (WM.) Date: Wed, 22 Apr 2009 11:57:13 -0700 Subject: [Tutor] Don't understand error messages. Message-ID: <49EF6889.6020106@socal.rr.com> Using Windows XP, Python 2.6 & Al Sweigart's "Invent Your Own...", I keyed in the Hangman Program. So far as I can tell it is totally proofed out. Still, I get an error message which makes no sense to me. Herewith, the program, followed by the error message. # CONSTANTS are all CAPS. # variables AreAllLikeThis import random HANGMANPIX = [''' ____ | | | | | | | | | | ===================''', ''' ____ | | | Q | | | | | | | ===================''', ''' ____ | | | Q | | | | | | | | | ===================''', ''' ____ | | | Q | | | | | | | - | | | | ===================''', ''' ____ | | | Q | | | | | | | H- | H | H | | ===================''', ''' ____ | | | Q | | | | | | | H-H | H H | H H | | ===================''', ''' ____ | | | Q | (| | | )| | | H-H | H H | H H | | ===================''', ''' ____ | | | Q | (| |) | )| |( | H-H | H H | H H | | ===================''', ''' ____ | | | Q | (| |) | )| |( | oH-H | H H | H H | | ===================''', ''' ____ | | | Q | (| |) | )| |( | oH-Ho | H H | H H | | ===================''', ''' ____ | | | Q | (| |) | )| |( | oH-Ho | H H | H H | O | ===================''', ''' ____ | | | Q | (| |) | )| |( | oH-Ho | H H | H H | O O | ==================='''] # line 151, equals line 96 in book. Words = 'ant baboon badger bat bear beaver beetle birdamel cat\ clam cobra cougar coyote crab crane crow deerdog donkey duck\ eagle ferret fish fox frog goat goose hawk iguana jackal koala\ leech lemur lion lizard llama mite mole monkey moose moth mouse\ mule newt otter owl oyster panda parrot pigeon python quail rabbit\ ram rat raven rhino salmon seal shark sheep skunk sloth slug snail\ snake spider squid stork swan tick tiger toad trout turkey turtle\ wasp weasel whale wolf wombat worm zebra'.split() def GetRandomWord(WordList): # This function returns a random string from the passed list of strings. WordIndex = random.randint (0, len(WordList) -1) return WordList [WordIndex] def DisplayBoard(HANGMAPIX, MissedLetters, CorrectLetters, SecretWord): print HANGMANPIX[len(MissedLetters)] print print 'MissedLetters:', for Letter in MissedLetters: print Letter, print Blanks = '_' * len(SecretWord) for i in range(len(SecretWord)): # 116 Replace Blanks with correctly guessed letters. if SecrettWord[i] in CorrectLetters: Blanks = Blanks[:i] + SecretWord[i] + Blanks[i+1:] for Letters in Blanks:#Show SecretWord w 1 space between letters. print Letter, print def GetGuess(AlreadyGuessed):# 124 Returns only a letter user entered. while True: print "Guess a letter." Guess = raw_input()#ppppppppppppppppppppppppppppppppppppppppppp Guess = Guess.lower() if len(Guess) != 1: print "Please enter a single letter." elif Guess in AlreadyGuessed: print "You used that letter, choose another." elif Guess not in 'abcdefghijklmnopqrstuvwxyz': print "Please enter a LETTER!" else: return Guess #138 def PlayAgain():#Returns T/F for play/quit. print "Wanna do it again? Yes or NO?" return raw_input().lower().startswith('y') print 'H A N G M A N' #145 MissedLetters = '' CorrectLetters = '' SecretWord = GetRandomWord(Words) GameIsDone = False # Let the user enter a letter. while True:#151 DisplayBoard(HANGMANPIX, MissedLetters, CorrectLetters, SecretWord) Guess = GetGuess(MissedLetters + CorrectLetters) if Guess in SecretWord: CorrectLetters = CorrectLetters + Guess#book158...onscreen213 # 160 Check if user has won. FoundAllLetters = True for i in range(len,(SecretWord)): if SecretWord[i] not in CorrectLetters: FoundAllLetters = False break if FoundAllLetters: print 'Yes! The secret word is "' + \ SecretWord +'"! You have won!' GameIsDone = True else: MissedLetters = MissedLetters + Guess #171 Check for too many guesses. if len(MissedLetters) == len(HANGMANPIX) - 1: DisplayBoard(HANGMANPIX, MissedLetters, CorrectLetters, SecretWord) print 'You have run out of guesses!\nAfter '\ + str(len(CorrectLetters)) + " missed guesses and "\ + str(len(CorrectLetters)) +\ "correct guesses. The word was "' + SecretWord + "."' GameIsDone = True #180 Ask user, "Go again?" if GameIsDone: if PlayAgain(): MissedLetters = '' CorrectLetters = '' GameIsDone = False SecretWord = getRandomWord(Words) else: break End of program, below is the error message: Traceback (most recent call last): File "C:\Python26\SweigartHangMan.py", line 212, in DisplayBoard(HANGMANPIX, MissedLetters, CorrectLetters, SecretWord) File "C:\Python26\SweigartHangMan.py", line 183, in DisplayBoard for Letters in Blanks:#Show SecretWord w 1 space between letters. UnboundLocalError: local variable 'Blanks' referenced before assignment From denis.spir at free.fr Wed Apr 22 21:13:54 2009 From: denis.spir at free.fr (spir) Date: Wed, 22 Apr 2009 21:13:54 +0200 Subject: [Tutor] Don't understand error messages. In-Reply-To: <49EF6889.6020106@socal.rr.com> References: <49EF6889.6020106@socal.rr.com> Message-ID: <20090422211354.203b63ca@o> Le Wed, 22 Apr 2009 11:57:13 -0700, "WM." s'exprima ainsi: [...] > def DisplayBoard(HANGMAPIX, MissedLetters, CorrectLetters, SecretWord): > print HANGMANPIX[len(MissedLetters)] > print > > print 'MissedLetters:', > for Letter in MissedLetters: > print Letter, > print > > Blanks = '_' * len(SecretWord) > > for i in range(len(SecretWord)): > # 116 Replace Blanks with correctly guessed letters. > if SecrettWord[i] in CorrectLetters: > Blanks = Blanks[:i] + SecretWord[i] + Blanks[i+1:] > > for Letters in Blanks:#Show SecretWord w 1 space between letters. > print Letter, > print [...] > End of program, below is the error message: > > Traceback (most recent call last): > File "C:\Python26\SweigartHangMan.py", line 212, in > DisplayBoard(HANGMANPIX, MissedLetters, CorrectLetters, SecretWord) > File "C:\Python26\SweigartHangMan.py", line 183, in DisplayBoard > for Letters in Blanks:#Show SecretWord w 1 space between letters. > UnboundLocalError: local variable 'Blanks' referenced before assignment Python's right ;-) You may use the word 'Blanks' without defining it. The reason is you definie Blanks inside a "for Letter in MissedLetters" loop. If ever MissedLetters happens to be empty, the loop will never be entered and Blanks remains unknown. Happy you! This in fact reveals a conceptual error in your program's design (BLanks should be first defined outside the loop)... that may have caused damage in a real-world case if ever not detected early enough. Denis ------ la vita e estrany From kent37 at tds.net Wed Apr 22 21:21:24 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 22 Apr 2009 15:21:24 -0400 Subject: [Tutor] Don't understand error messages. In-Reply-To: <49EF6889.6020106@socal.rr.com> References: <49EF6889.6020106@socal.rr.com> Message-ID: <1c2a2c590904221221s7a999925r82085185c32b44ac@mail.gmail.com> On Wed, Apr 22, 2009 at 2:57 PM, WM. wrote: > Using Windows XP, Python 2.6 & Al Sweigart's "Invent Your Own...", I keyed > in the Hangman Program. So far as I can tell it is totally proofed out. > ?Still, I get an error message which makes no sense to me. > def DisplayBoard(HANGMAPIX, MissedLetters, CorrectLetters, SecretWord): > ? print HANGMANPIX[len(MissedLetters)] > ? print > > ? print 'MissedLetters:', > ? for Letter in MissedLetters: > ? ? ? print Letter, > ? ? ? print > > ? ? ? Blanks = '_' * len(SecretWord) > > ? ? ? for i in range(len(SecretWord)): > ? ? ? ? ? # 116 Replace Blanks with correctly guessed letters. > ? ? ? ? ? if SecrettWord[i] in CorrectLetters: > ? ? ? ? ? ? ? Blanks = Blanks[:i] + SecretWord[i] + Blanks[i+1:] > ? ? ? ? ? ? ? ? for Letters in Blanks:#Show SecretWord w 1 space between > letters. > ? ? ? print Letter, The above line should be indented more than the previous line. I'm not sure if that is the cause of your error but give it a try and see. Kent From denis.spir at free.fr Wed Apr 22 21:23:30 2009 From: denis.spir at free.fr (spir) Date: Wed, 22 Apr 2009 21:23:30 +0200 Subject: [Tutor] Tokenizing Help In-Reply-To: <20090422183529.GA19821@yam.witteman.ca> References: <20090422183529.GA19821@yam.witteman.ca> Message-ID: <20090422212330.433c4ae6@o> Le Wed, 22 Apr 2009 14:35:29 -0400, William Witteman s'exprima ainsi: > I need to be able to decompose a formatted text file into identifiable, > possibly named pieces. To tokenize it, in other words. There seem to > be a vast array of modules to do this with (simpleparse, pyparsing etc) > but I cannot understand their documentation. I would recommand pyparsing, but this is an opinion. > The file format I am looking at (it is a bibliographic reference file) > looks like this: > > <1> # the references are enumerated > AU - some text > perhaps across lines > AB - some other text > AB - there may be multiples of some fields > UN - any 2-letter combination may exist, other than by exhaustion, I > cannot anticipate what will be found Regular expressions may be enough, depending on your actual needs. > What I am looking for is some help to get started, either with > explaining the implementation of one of the modules with respect to my > format, or with an approach that I could use from the base library. The question is: what do you need from the data? What do you expect as result? The best is to provide an example of result matching sample data. E.G. I wish as result a dictionary looking like { 'AU': 'some text\nperhaps across lines' 'AB': ['some other text', 'there may be multiples of some fields'] 'UN': 'any 2-letter combination may exist...' ... } From this depends the choice of an appropriate tool and hints on possible algorithms. > Thanks. Denis ------ la vita e estrany From wferguson1 at socal.rr.com Wed Apr 22 22:17:20 2009 From: wferguson1 at socal.rr.com (WM.) Date: Wed, 22 Apr 2009 13:17:20 -0700 Subject: [Tutor] Don't understand error messages. In-Reply-To: <1c2a2c590904221221s7a999925r82085185c32b44ac@mail.gmail.com> References: <49EF6889.6020106@socal.rr.com> <1c2a2c590904221221s7a999925r82085185c32b44ac@mail.gmail.com> Message-ID: <49EF7B50.6040400@socal.rr.com> Kent Johnson wrote: > On Wed, Apr 22, 2009 at 2:57 PM, WM. wrote: > >> Using Windows XP, Python 2.6 & Al Sweigart's "Invent Your Own...", I keyed >> in the Hangman Program. So far as I can tell it is totally proofed out. >> Still, I get an error message which makes no sense to me. >> > > >> def DisplayBoard(HANGMAPIX, MissedLetters, CorrectLetters, SecretWord): >> print HANGMANPIX[len(MissedLetters)] >> print >> >> print 'MissedLetters:', >> for Letter in MissedLetters: >> print Letter, >> print >> >> Blanks = '_' * len(SecretWord) >> >> for i in range(len(SecretWord)): >> # 116 Replace Blanks with correctly guessed letters. >> if SecrettWord[i] in CorrectLetters: >> Blanks = Blanks[:i] + SecretWord[i] + Blanks[i+1:] >> for Letters in Blanks:#Show SecretWord w 1 space between >> letters. >> print Letter, >> > > The above line should be indented more than the previous line. I'm not > sure if that is the cause of your error but give it a try and see. > > Kent > > The indents were dis-arrayed e-mail, due to a long REM; in the script the indents are as you suggest. Blanks = '_' * len(SecretWord) for i in range(len(SecretWord)): # 116 Replace Blanks with correctly guessed letters. if SecrettWord[i] in CorrectLetters: Blanks = Blanks[:i] + SecretWord[i] + Blanks[i+1:] for Letters in Blanks: print Letter, print From yam at nerd.cx Wed Apr 22 22:18:30 2009 From: yam at nerd.cx (William Witteman) Date: Wed, 22 Apr 2009 16:18:30 -0400 Subject: [Tutor] Tokenizing Help In-Reply-To: <20090422212330.433c4ae6@o> References: <20090422183529.GA19821@yam.witteman.ca> <20090422212330.433c4ae6@o> Message-ID: <20090422201830.GA20106@yam.witteman.ca> On Wed, Apr 22, 2009 at 09:23:30PM +0200, spir wrote: >> I need to be able to decompose a formatted text file into identifiable, >> possibly named pieces. To tokenize it, in other words. There seem to >> be a vast array of modules to do this with (simpleparse, pyparsing etc) >> but I cannot understand their documentation. > >I would recommand pyparsing, but this is an opinion. It looked like a good package to me as well, but I cannot see how to define the grammar - it may be that the notation just doesn't make sense to me. >Regular expressions may be enough, depending on your actual needs. Perhaps, but I am cautious, because every text and most websites discourage regexes for parsing. >The question is: what do you need from the data? What do you expect as result? The best is to provide an example of result matching sample data. E.G. I wish as result a dictionary looking like >{ >'AU': 'some text\nperhaps across lines' >'AB': ['some other text', 'there may be multiples of some fields'] >'UN': 'any 2-letter combination may exist...' >... >} I think that a dictionary could work, but it would have to use lists as the value, to prevent key collisions. That said, returning a list of dictionaries (one dictionary per bibliographic reference) would work very well in the large context of my program. >From this depends the choice of an appropriate tool and hints on possible algorithms. I hope this helps. I spent quite some time with pyparsing, but I was never able to express the rules of my grammar based on the examples on the website. -- yours, William From bgailer at gmail.com Wed Apr 22 23:16:56 2009 From: bgailer at gmail.com (bob gailer) Date: Wed, 22 Apr 2009 17:16:56 -0400 Subject: [Tutor] Tokenizing Help In-Reply-To: <20090422183529.GA19821@yam.witteman.ca> References: <20090422183529.GA19821@yam.witteman.ca> Message-ID: <49EF8948.7020500@gmail.com> William Witteman wrote: > I need to be able to decompose a formatted text file into identifiable, > possibly named pieces. To tokenize it, in other words. There seem to > be a vast array of modules to do this with (simpleparse, pyparsing etc) > but I cannot understand their documentation. > > The file format I am looking at (it is a bibliographic reference file) > looks like this: > > <1> # the references are enumerated > AU - some text > perhaps across lines > AB - some other text > AB - there may be multiples of some fields > UN - any 2-letter combination may exist, other than by exhaustion, I > cannot anticipate what will be found > > What I am looking for is some help to get started, either with > explaining the implementation of one of the modules with respect to my > format, or with an approach that I could use from the base library. > What is your ultimate goal? -- Bob Gailer Chapel Hill NC 919-636-4239 From eike.welk at gmx.net Wed Apr 22 23:23:11 2009 From: eike.welk at gmx.net (Eike Welk) Date: Wed, 22 Apr 2009 23:23:11 +0200 Subject: [Tutor] Tokenizing Help In-Reply-To: <20090422183529.GA19821@yam.witteman.ca> References: <20090422183529.GA19821@yam.witteman.ca> Message-ID: <200904222323.11510.eike.welk@gmx.net> Hello William! On Wednesday 22 April 2009, William Witteman wrote: > The file format I am looking at (it is a bibliographic reference > file) looks like this: > > <1> # the references are enumerated > AU - some text > perhaps across lines > AB - some other text > AB - there may be multiples of some fields > UN - any 2-letter combination may exist, other than by exhaustion, > I cannot anticipate what will be found How do you decide that a word is a keyword (AU, AB, UN) and not a part of the text? There could be a file like this: <567> AU - Bibliographical Theory and Practice - Volume 1 - The AU - Tag and its applications AB - Texts in Library Science <568> AU - Bibliographical Theory and Practice - Volume 2 - The AB - Tag and its applications AB - Texts in Library Science <569> AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU AB - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU ZZ - Somewhat nonsensical case To me it seems that a parsing library is unnecessary. Just look at the first few characters of each line and decide if its the start of a record, a tag or normal text. You might need some additional algorithm for corner cases. Kind regards, Eike. From alan.gauld at btinternet.com Thu Apr 23 00:49:36 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 22 Apr 2009 23:49:36 +0100 Subject: [Tutor] Don't understand error messages. References: <49EF6889.6020106@socal.rr.com> Message-ID: "WM." wrote > def DisplayBoard(HANGMAPIX, MissedLetters, CorrectLetters, SecretWord): > for Letter in MissedLetters: > Blanks = '_' * len(SecretWord) > for i in range(len(SecretWord)): > .... > for Letters in Blanks: > > MissedLetters = '' > while True:#151 > DisplayBoard(HANGMANPIX, MissedLetters, CorrectLetters, First time through the program MissedLetters is empty so the for loop never executes and Blanks is not initialised. The second for loop then fails. You need to initialise Blanks at the head of the function. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From eduardo.susan at gmail.com Thu Apr 23 00:53:23 2009 From: eduardo.susan at gmail.com (Eduardo Vieira) Date: Wed, 22 Apr 2009 16:53:23 -0600 Subject: [Tutor] Organizing my code, should I use functions? In-Reply-To: <49EF5EBB.8090706@gmail.com> References: <9356b9f30904220749o2007ec41s92a660c6bc93de8c@mail.gmail.com> <49EF3BF4.7080806@gmail.com> <9356b9f30904220938u2d88d538nd59cf362b6fc280e@mail.gmail.com> <49EF5EBB.8090706@gmail.com> Message-ID: <9356b9f30904221553y63c3f423s279ec034c85fc74@mail.gmail.com> On Wed, Apr 22, 2009 at 12:15 PM, bob gailer wrote: > Please use reply-all so a copy goes to the list. > Thank you very much for your help and suggestions, I've modified my code following your guidelines and looks so much better. Regards,' Eduardo From lowelltackett at yahoo.com Thu Apr 23 01:46:24 2009 From: lowelltackett at yahoo.com (Lowell Tackett) Date: Wed, 22 Apr 2009 16:46:24 -0700 (PDT) Subject: [Tutor] Organizing my code, should I use functions? Message-ID: <519200.12944.qm@web110110.mail.gq1.yahoo.com> Just a comment [that might nudge your insight] regarding functions. From the 'good-ol-days'--a function was called a subroutine. They are the same thing. When my thinking starts to muddle (often and easily) that connection always helps. From the virtual desk of Lowell Tackett --- On Wed, 4/22/09, Eduardo Vieira wrote: From: Eduardo Vieira Subject: [Tutor] Organizing my code, should I use functions? To: "tutor" Date: Wednesday, April 22, 2009, 10:49 AM Hello! I?m not a programmer and am a beginner with Python, I would like suggestion about ways of tackling the task I had to do. I have bee been using the xlrd package to get data from some spreadsheets successfully. I have developed a simple report from a script that compares number of shipments recorded in 2 spreadsheets, with data from previous years and current year, respectively. Later on I was asked to include in the same report numbers from another branch of our company. So, basically all the code logic was the same, only two different files needed to be processed, and I wondered how would I save time avoiding repeating code, creating extra variables, etc. So, I simply decided to alter the code as little as possible and use the import statement. It's working this way. Sounds wise? I thought if I converted it to a function or a class would make it more flexible, but my understanding of classes are too basic yet, and am not sure if it's worth using it. I have noticed that in lot's of python code example, people make functions out of everything. I'm still too much attached to procedural, not functional programming, if I understand the terms correctly. I would appreciate any suggestions. Here is the code: ## This script creates a report of shipments ## comparing the numbers from the previous year with the current year. import xlrd # Package to read Excel files import os import glob import time import datetime import dbi import odbc thismonth = time.strftime('%B', time.gmtime()) # Get's a string with the name of the current month hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) # Two excel files to process with shipping information thisyearFile = r'c:\Shipping Totals - 2009\Shipping Totals 2009 West.xls' prevyearFile = r'c:\Shipping Totals\Shipping Totals - 2008\Shipping Totals 2008 West.xls' thisDay = time.gmtime()[:3] prevyear = datetime.datetime.today() - datetime.timedelta(days=365) aYearAgo = prevyear.timetuple()[:3] ###### --- Code to find the right cell with today's shipments book = xlrd.open_workbook(thisyearFile) # Opens excel file dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) #Puts the date in the Excel format, like 3991.0 sh = book.sheet_by_name(thismonth) # The sheet which has the name of the month: April, May, June, etc. firstCol = sh.col_values(0) # Retrieves the first column tperday = firstCol.index('TOTAL Per Day') # Finds the cell 'Total Per Day' shipindex = tperday + 1 # The next cell after 'TOTAL Per Day', which contains the info about shipments # Looks for the column whose header is today's date for cols in range(sh.ncols): for valores in sh.col_values(cols): if valores == dayexcel: # If it finds the column with today's date todaysList = sh.col_values(cols) totals = sh.row_values(shipindex, end_colx=cols + 1) # sum up to the current date break # Crosses rows with column to find the right cell with the shipments of today shippedToday = todaysList[shipindex] totalShipments = sum([a for a in totals if type(a) == type(2.0)]) # Sums all shipments # Check previous year's shipments processing the file with last year's data booktwo = xlrd.open_workbook(prevyearFile) dayexcel = xlrd.xldate.xldate_from_date_tuple(aYearAgo, book.datemode) sh = booktwo.sheet_by_name(thismonth) firstCol = sh.col_values(0) tperday = firstCol.index('TOTAL Per Day') shipindex = tperday + 1 for cols in range(sh.ncols): for valores in sh.col_values(cols): if valores == dayexcel: lastyearsList = sh.col_values(cols) totals = sh.row_values(shipindex, end_colx=cols + 1) # sum up to the current date break shippedLastYear = lastyearsList[shipindex] OldTotalShipments = sum([a for a in totals if type(a) == type(2.0)]) # Imports the information from the Eastern division. See code on the bottom import bizreportereast as bz report = """ ===== Shipments Alberta Warehouse ===== - Shipments today: %d - Shipments this month: %d - Shipments this day, last year: %d - Shipments this month, last year: %d ===== Shipments Ontario Warehouse ===== - Shipments today: %d - Shipments this month: %d - Shipments this day, last year: %d - Shipments this month, last year: %d """ % (shippedToday, totalShipments, shippedLastYear, OldTotalShipments, bz.shippedToday, bz.totalShipments, bz.shippedLastYear, bz.OldTotalShipments) print report logfile = open('c:/myscripts/logbizreport.log', 'a') #### Code found in bizreportereast.py #### import xlrd import os import glob import time import datetime ###### --- Code to find the right cell with today's shipments thismonth = time.strftime('%B', time.gmtime()) hoje = time.strftime("%a, %b %d, %Y", time.gmtime()) thisyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - 2009\Shipping Totals 2009 East.xls' prevyearFile = r'c:\MS Excel\Shipping Totals\Shipping Totals - 2008\Shipping Totals 2008 East.xls' thisDay = time.gmtime()[:3] prevyear = datetime.datetime.today() - datetime.timedelta(days=365) aYearAgo = prevyear.timetuple()[:3] book = xlrd.open_workbook(thisyearFile) dayexcel = xlrd.xldate.xldate_from_date_tuple(thisDay, book.datemode) sh = book.sheet_by_name(thismonth) firstCol = sh.col_values(0) _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor From yam at nerd.cx Thu Apr 23 03:27:57 2009 From: yam at nerd.cx (William Witteman) Date: Wed, 22 Apr 2009 21:27:57 -0400 Subject: [Tutor] Tokenizing Help In-Reply-To: <49EF8948.7020500@gmail.com> References: <20090422183529.GA19821@yam.witteman.ca> <49EF8948.7020500@gmail.com> Message-ID: <20090423012757.GA20784@yam.witteman.ca> On Wed, Apr 22, 2009 at 05:16:56PM -0400, bob gailer wrote: >> <1> # the references are enumerated >> AU - some text >> perhaps across lines >> AB - some other text >> AB - there may be multiples of some fields >> UN - any 2-letter combination may exist, other than by exhaustion, I >> cannot anticipate what will be found >> >> What I am looking for is some help to get started, either with >> explaining the implementation of one of the modules with respect to my >> format, or with an approach that I could use from the base library. >> > > What is your ultimate goal? These references will populate a django model. -- yours, William From yam at nerd.cx Thu Apr 23 03:41:49 2009 From: yam at nerd.cx (William Witteman) Date: Wed, 22 Apr 2009 21:41:49 -0400 Subject: [Tutor] Tokenizing Help In-Reply-To: <200904222323.11510.eike.welk@gmx.net> References: <20090422183529.GA19821@yam.witteman.ca> <200904222323.11510.eike.welk@gmx.net> Message-ID: <20090423014149.GB20784@yam.witteman.ca> On Wed, Apr 22, 2009 at 11:23:11PM +0200, Eike Welk wrote: >How do you decide that a word is a keyword (AU, AB, UN) and not a part >of the text? There could be a file like this: > ><567> >AU - Bibliographical Theory and Practice - Volume 1 - The AU - Tag >and its applications >AB - Texts in Library Science ><568> >AU - Bibliographical Theory and Practice - Volume 2 - The >AB - Tag and its applications >AB - Texts in Library Science ><569> >AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - >AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU >AB - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - >AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU >ZZ - Somewhat nonsensical case This is a good case, and luckily the files are validated on the other end to prevent this kind of collision. >To me it seems that a parsing library is unnecessary. Just look at the >first few characters of each line and decide if its the start of a >record, a tag or normal text. You might need some additional >algorithm for corner cases. If this was the only type of file I'd need to parse, I'd agree with you, but this is one of at least 4 formats I'll need to process, and so a robust methodology will serve me better than a regex-based one-off. -- yours, William From kent37 at tds.net Thu Apr 23 04:23:34 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 22 Apr 2009 22:23:34 -0400 Subject: [Tutor] Tokenizing Help In-Reply-To: <20090423014149.GB20784@yam.witteman.ca> References: <20090422183529.GA19821@yam.witteman.ca> <200904222323.11510.eike.welk@gmx.net> <20090423014149.GB20784@yam.witteman.ca> Message-ID: <1c2a2c590904221923u54dc1118t8827e4e89e88f51a@mail.gmail.com> On Wed, Apr 22, 2009 at 9:41 PM, William Witteman wrote: > On Wed, Apr 22, 2009 at 11:23:11PM +0200, Eike Welk wrote: > >>How do you decide that a word is a keyword (AU, AB, UN) and not a part >>of the text? There could be a file like this: >> >><567> >>AU ?- Bibliographical Theory and Practice - Volume 1 - The AU ?- Tag >>and its applications >>AB ?- Texts in Library Science >><568> >>AU ?- Bibliographical Theory and Practice - Volume 2 - The >>AB ?- Tag and its applications >>AB ?- Texts in Library Science >><569> >>AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- >>AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU >>AB ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- >>AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU ?- AU >>ZZ ?- Somewhat nonsensical case > > This is a good case, and luckily the files are validated on the other > end to prevent this kind of collision. >>To me it seems that a parsing library is unnecessary. Just look at the >>first few characters of each line and decide if its the start of a >>record, a tag or normal text. You might need some additional >>algorithm for corner cases. I agree with this. The structure is simple and the lines are easily recognized. Here is one way to do it: data = '''<567> AU - Bibliographical Theory and Practice - Volume 1 - The AU - Tag and its applications AB - Texts in Library Science <568> AU - Bibliographical Theory and Practice - Volume 2 - The AB - Tag and its applications AB - Texts in Library Science <569> AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU AB - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - AU ZZ - Somewhat nonsensical case '''.splitlines() import pprint, re from collections import defaultdict def parse(data): ''' Yields dictionaries corresponding to bibliographic entries''' result = None key = None for line in data: if not line.strip(): continue # skip blank lines if re.search(r'^<\d+>', line): # start of a new entry if result: # return the previous entry and initialize yield result result = defaultdict(list) key = None else: m = re.search(r'^([A-Z]{2}) +- +(.*)', line) if m: # New field key, value = m.group(1, 2) result[key].append(value) else: # Extension of previous field if result and key: # sanity check result[key][-1] += '\n' + line if result: yield result for entry in parse(data): for key, value in entry.iteritems(): print key pprint.pprint(value) print Note that dicts do not preserve order so the fields are not output in the same order as they appear in the file. > If this was the only type of file I'd need to parse, I'd agree with you, > but this is one of at least 4 formats I'll need to process, and so a > robust methodology will serve me better than a regex-based one-off. Unless there is some commonality between the formats, each parser is going to be a one-off no matter how you implement it. Kent From wferguson1 at socal.rr.com Thu Apr 23 04:40:51 2009 From: wferguson1 at socal.rr.com (WM.) Date: Wed, 22 Apr 2009 19:40:51 -0700 Subject: [Tutor] Don't understand error messages. In-Reply-To: <1c2a2c590904221221s7a999925r82085185c32b44ac@mail.gmail.com> References: <49EF6889.6020106@socal.rr.com> <1c2a2c590904221221s7a999925r82085185c32b44ac@mail.gmail.com> Message-ID: <49EFD533.9020407@socal.rr.com> Well, Kent was right, it was an indent error, but 'way high in the program. I was so catching commas that I got sloppy at an indent change. sorry guys. From hans.dushanthakumar at hcn.com.au Thu Apr 23 08:54:49 2009 From: hans.dushanthakumar at hcn.com.au (Hans DushanthaKumar) Date: Thu, 23 Apr 2009 16:54:49 +1000 Subject: [Tutor] py.test on Windows References: <49EF6889.6020106@socal.rr.com><1c2a2c590904221221s7a999925r82085185c32b44ac@mail.gmail.com> <49EFD533.9020407@socal.rr.com> Message-ID: <2847FF397CB7124F92374EA511EEC856AADE2E@sydmail01.corporate.hcn.com.au> Hi, Has anyone here got py.test running on Windows? How do I run a test script? I have previously installed and used it on Linux and it all worked magically - all I needed to do was type in "py.test ". However on windows, the installation doesn't seem to set the paths appropriately. Thanks, Hans From denis.spir at free.fr Thu Apr 23 14:44:11 2009 From: denis.spir at free.fr (spir) Date: Thu, 23 Apr 2009 14:44:11 +0200 Subject: [Tutor] Don't understand error messages. In-Reply-To: <49EFD533.9020407@socal.rr.com> References: <49EF6889.6020106@socal.rr.com> <1c2a2c590904221221s7a999925r82085185c32b44ac@mail.gmail.com> <49EFD533.9020407@socal.rr.com> Message-ID: <20090423144411.1f35313a@o> Le Wed, 22 Apr 2009 19:40:51 -0700, "WM." s'exprima ainsi: > Well, Kent was right, it was an indent error, but 'way high in the > program. I was so catching commas that I got sloppy at an indent > change. sorry guys. > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > Sorry, I guess you're wrong. The design of the func I quoted in a previous reply is incorrect (as also noted by Alan). Simply, this design error was happily revealed by the syntactic one (indent). You'd better cope with both... I was thus right to state: happy you! (This kind of error can be very hard to diagnose.) Denis ------ la vita e estrany From denis.spir at free.fr Thu Apr 23 14:55:59 2009 From: denis.spir at free.fr (spir) Date: Thu, 23 Apr 2009 14:55:59 +0200 Subject: [Tutor] testing framework Message-ID: <20090423145559.3a3e7338@o> Hello, I would like to refactor tests of an application in a consistent form. I have a ton of tests for each module, but they are all ad hoc things. doctest looks great. The issue is it seems based on command line-like testing: """ This is the "example" module. The example module supplies one function, factorial(). For example, >>> import example >>> example.factorial(5) 120 """ My app is about parsing, which input and output usually both are big and complicated *strings*. So that I find the command line model really unappropriate for expressing test cases and their expected results. Any hint/pointer/comment welcome, Denis ------ la vita e estrany From moron.oxy at gmail.com Thu Apr 23 15:06:55 2009 From: moron.oxy at gmail.com (Oxymoron) Date: Thu, 23 Apr 2009 23:06:55 +1000 Subject: [Tutor] testing framework In-Reply-To: <20090423145559.3a3e7338@o> References: <20090423145559.3a3e7338@o> Message-ID: <2096a7260904230606v663584bm116e344a1c913446@mail.gmail.com> Hello, On Thu, Apr 23, 2009 at 10:55 PM, spir wrote: > > My app is about parsing, which input and output usually both are big and > complicated *strings*. So that I find the command line model really > unappropriate for expressing test cases and their expected results. > Interesting - haven't really used Python's test frameworks here - and perhaps my suggestion may be too basic - and if so I apologise - not sure what you mean by command line model in the context. Could you not have a directory with input files and expected output files, then write a test runner going through input -> apply_operation -> assert(output) cycles? The test runner can be as simple or as complicated as you want of course, so you can configure tests say by pieces of parsing functionality, but they need not deal with the tests' input themselves, which will mostly be preconfigured (or partially dynamic with plugged in values) etc. Heck, you could have configuration files for the test runner! Have I completely missed your question? -- K -- There is more to life than increasing its speed. -- Mahatma Gandhi -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Thu Apr 23 15:11:06 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 23 Apr 2009 09:11:06 -0400 Subject: [Tutor] testing framework In-Reply-To: <20090423145559.3a3e7338@o> References: <20090423145559.3a3e7338@o> Message-ID: <1c2a2c590904230611j4a17ce01vff0533fc606ffd6@mail.gmail.com> On Thu, Apr 23, 2009 at 8:55 AM, spir wrote: > Hello, > > I would like to refactor tests of an application in a consistent form. I have a ton of tests for each module, but they are all ad hoc things. > My app is about parsing, which input and output usually both are big and complicated *strings*. So that I find the command line model really unappropriate for expressing test cases and their expected results. The unittest module is the other option in the standard library. nose and py.test are popular add-on testing packages. More on unittest, and links to the others, here: http://personalpages.tds.net/~kent37/kk/00014.html Kent From ptmcg at austin.rr.com Thu Apr 23 16:24:16 2009 From: ptmcg at austin.rr.com (Paul McGuire) Date: Thu, 23 Apr 2009 09:24:16 -0500 Subject: [Tutor] Tokenizing Help In-Reply-To: References: Message-ID: For the given test case, this pyparsing sample parses the data, without having to anticipate all the possible 2-letter keys. from pyparsing import * integer = Word(nums) DASH = Literal('-').suppress() LT = Literal('<').suppress() GT = Literal('>').suppress() entrynum = LT + integer + GT keycode = Word(alphas.upper(),exact=2) key = GoToColumn(1).suppress() + keycode + DASH data = Group(key("key") + Empty() + SkipTo(key | entrynum | StringEnd())("value")) entry = entrynum("refnum") + OneOrMore(data)("data") for e in entry.searchString(test): print e.refnum for dd in e.data: print dd.key,':', dd.value print Prints: ['567'] ['AU'] : Bibliographical Theory and Practice - Volume 1 - The AU - Tag and its applications ['AB'] : Texts in Library Science ['568'] ['AU'] : Bibliographical Theory and Practice - Volume 2 - The ['AB'] : Tag and its applications ['AB'] : Texts in Library Science ['569'] ['AU'] : AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - ['AU'] : AU - AU - AU - AU - AU - AU - AU - AU - AU - AU ['AB'] : AU - AU - AU - AU - AU - AU - AU - AU - AU - AU - ['AU'] : AU - AU - AU - AU - AU - AU - AU - AU - AU - AU ['ZZ'] : Somewhat nonsensical case If you find that you have to also accept keycodes that consist of a capital letter followed by a numeric digit (like "B7"), modify the keycode definition to be: keycode = Word(alphas.upper(), alphanums.upper(), exact=2) -- Paul From alan.gauld at btinternet.com Thu Apr 23 16:49:13 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 23 Apr 2009 15:49:13 +0100 Subject: [Tutor] testing framework References: <20090423145559.3a3e7338@o> Message-ID: "spir" wrote > My app is about parsing, which input and output usually both are big > and complicated *strings*. So that I find the command line model > really unappropriate for expressing test cases and their expected > results. > > Any hint/pointer/comment welcome, What about using command line redirection so you can prepare the input data in a text file and pipe that into the tests. Would that work for you? Alan G From eike.welk at gmx.net Thu Apr 23 17:13:25 2009 From: eike.welk at gmx.net (Eike Welk) Date: Thu, 23 Apr 2009 17:13:25 +0200 Subject: [Tutor] Testing Frameworks and Organizations In-Reply-To: <20090423145559.3a3e7338@o> References: <20090423145559.3a3e7338@o> Message-ID: <200904231713.26092.eike.welk@gmx.net> Hello Spir; hello all! I have a similar question: For projects with multiple packages (directories) and modules (files); where do you put your testcode, what framework do you use, and what design patterns do you employ? I currently work on a project with one package and several modules. I use the unittest module and my testcode is currently a mess. I use the following pattern: ------ start of file (module_n) ------------- import my_package.module_1 as module_1 import my_package.module_2 as module_2 #module code goes here if __name__ == '__main__': import unittest # Self-testing code goes here. ------ end of file ------------- Files structured like this can't be run as scripts, because the interpreter won't find the imported modules in the first two lines. However it works from my IDE (Eclipse) and this is the reason why I didn't bother to change the testcode's sorry state until now. I would really like to know how the Python experts organize their testcode, and what is considered the recommended way to do it. Kind regards, Eike. From jfabiani at yolo.com Thu Apr 23 17:27:33 2009 From: jfabiani at yolo.com (johnf) Date: Thu, 23 Apr 2009 08:27:33 -0700 Subject: [Tutor] understanding urllib.urlopen Message-ID: <200904230827.33914.jfabiani@yolo.com> I'm trying to comfirm an address is a good address via google. Meaning does google report that the address exist. If I do urllib.urlopen( "http://maps.google.com/maps/geo?%s" % params ) with the address in params. I get the longitude and latitude correctly. If I use the following in the address bar of a browser "http://maps.google.com?q='18 Tadlock Place Woodland CA'" all works. But if I attempt this urllib.urlopen( "http://maps.google.com?q='18 Tadlock Place Woodland CA'" ) it always fails. So I don't understand urllib.urlopen(). Could some kind soul explain what I'm doing wrong. -- John Fabiani From emile at fenx.com Thu Apr 23 17:44:07 2009 From: emile at fenx.com (Emile van Sebille) Date: Thu, 23 Apr 2009 08:44:07 -0700 Subject: [Tutor] understanding urllib.urlopen In-Reply-To: <200904230827.33914.jfabiani@yolo.com> References: <200904230827.33914.jfabiani@yolo.com> Message-ID: johnf wrote: > But if I attempt this > urllib.urlopen( "http://maps.google.com?q='18 Tadlock Place Woodland CA'" ) > it always fails. > What do you get? I don't get an error. Emile From jfabiani at yolo.com Thu Apr 23 17:58:03 2009 From: jfabiani at yolo.com (johnf) Date: Thu, 23 Apr 2009 08:58:03 -0700 Subject: [Tutor] understanding urllib.urlopen In-Reply-To: References: <200904230827.33914.jfabiani@yolo.com> Message-ID: <200904230858.03505.jfabiani@yolo.com> On Thursday 23 April 2009 08:44:07 am Emile van Sebille wrote: > johnf wrote: > > > > But if I attempt this > > urllib.urlopen( "http://maps.google.com?q='18 Tadlock Place Woodland CA'" > > ) it always fails. > > What do you get? I don't get an error. > > Emile > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor OK - I'm also getting something when I use the command line. Must be the code I have. Thanks -- John Fabiani From srilyk at gmail.com Thu Apr 23 22:47:56 2009 From: srilyk at gmail.com (W W) Date: Thu, 23 Apr 2009 15:47:56 -0500 Subject: [Tutor] understanding urllib.urlopen In-Reply-To: <200904230858.03505.jfabiani@yolo.com> References: <200904230827.33914.jfabiani@yolo.com> <200904230858.03505.jfabiani@yolo.com> Message-ID: <333efb450904231347y7cd2cfa1m5d8d5e571a24b849@mail.gmail.com> On Thu, Apr 23, 2009 at 10:58 AM, johnf wrote: > On Thursday 23 April 2009 08:44:07 am Emile van Sebille wrote: > > johnf wrote: > > > > > > > But if I attempt this > > > urllib.urlopen( "http://maps.google.com?q='18Tadlock Place Woodland CA'" > > > ) it always fails. > > > > What do you get? I don't get an error. > > > > Emile > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > http://mail.python.org/mailman/listinfo/tutor > > OK - I'm also getting something when I use the command line. Must be the > code > I have. Thanks I don't know about maps, but I know the normal google search doesn't like to be scraped. -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Thu Apr 23 23:41:49 2009 From: bgailer at gmail.com (bob gailer) Date: Thu, 23 Apr 2009 17:41:49 -0400 Subject: [Tutor] understanding urllib.urlopen In-Reply-To: <200904230827.33914.jfabiani@yolo.com> References: <200904230827.33914.jfabiani@yolo.com> Message-ID: <49F0E09D.30202@gmail.com> johnf wrote: > I'm trying to comfirm an address is a good address via google. Meaning does > google report that the address exist. > > If I do > urllib.urlopen( "http://maps.google.com/maps/geo?%s" % params ) > with the address in params. I get the longitude and latitude correctly. > > If I use the following in the address bar of a browser > "http://maps.google.com?q='18 Tadlock Place Woodland CA'" > all works. > > But if I attempt this > urllib.urlopen( "http://maps.google.com?q='18 Tadlock Place Woodland CA'" ) > it always fails. > > So I don't understand urllib.urlopen(). Could some kind soul explain what I'm > doing wrong. > 1) put a / after .com 2) encode it (in this case that means replace blanks with %20) x=urllib.urlopen("http://maps.google.com/?q=18%20Tadlock%20Place%20Woodland%20CA") -- Bob Gailer Chapel Hill NC 919-636-4239 From taboyo22 at yahoo.com Fri Apr 24 02:54:35 2009 From: taboyo22 at yahoo.com (tonyon boyo) Date: Thu, 23 Apr 2009 17:54:35 -0700 (PDT) Subject: [Tutor] sorting algorithim Message-ID: <736020.87470.qm@web56601.mail.re3.yahoo.com> hello, ? i know how to print for bubble sort in python, is there a way to print each pass in the sort so i can see what happens at each step?? thanks ? def bubblesort(list): ??? for passes in range(len(list)-1, 0, -1): ??????? for index in range(passes): ??????????? if list[index] < list[index + 1]: ??????????????? list[index], list[index + 1] = list[index + 1], list[index] ??? return list print bubblesort([3,2,66,5,22,62,61,16]) ? ?And you will be hated by all for My name?s sake. But he who endures to the end shall be saved. Luke 13:13 ? ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From amonroe at columbus.rr.com Fri Apr 24 03:14:44 2009 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Thu, 23 Apr 2009 21:14:44 -0400 Subject: [Tutor] sorting algorithim In-Reply-To: <736020.87470.qm@web56601.mail.re3.yahoo.com> References: <736020.87470.qm@web56601.mail.re3.yahoo.com> Message-ID: <177298778220.20090423211444@columbus.rr.com> > i know how to print for bubble sort in python, is there a way to > print each pass in the sort so i can see what happens at each step?? > thanks A good first guess would be to try sticking "print list" in there in a few different places. Others will probably point out that "list" is a built-in keyword of python so using it as a variable name may prevent you from using that keyword. Alan From shaofeng.niu at gmail.com Fri Apr 24 08:41:31 2009 From: shaofeng.niu at gmail.com (Shaofeng NIu) Date: Fri, 24 Apr 2009 14:41:31 +0800 Subject: [Tutor] how to compile python3.0 Message-ID: I tried to compile and install python3.0 from source,but after "make",it shows: Python build finished, but the necessary bits to build these modules were not found: _dbm _gdbm _hashlib _sqlite3 _ssl _tkinter bz2 readline To find the necessary bits, look in setup.py in detect_modules() for the module's name. Could anybody tell me how to install these modules?Thank you! My OS is Ubuntu 8.10 -------------- next part -------------- An HTML attachment was scrubbed... URL: From contactdayo at gmail.com Fri Apr 24 12:54:57 2009 From: contactdayo at gmail.com (Dayo Adewunmi) Date: Fri, 24 Apr 2009 11:54:57 +0100 Subject: [Tutor] how to compile python3.0 In-Reply-To: References: Message-ID: <49F19A81.2030406@gmail.com> Shaofeng NIu wrote: > I tried to compile and install python3.0 from source,but after > "make",it shows: > > Python build finished, but the necessary bits to build these modules > were not found: > _dbm _gdbm _hashlib > _sqlite3 _ssl _tkinter > bz2 readline > To find the necessary bits, look in setup.py in detect_modules() for > the module's name. > > Could anybody tell me how to install these modules?Thank you! > My OS is Ubuntu 8.10 > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > This worked for me on Ubuntu 8.04: $ sudo apt-get install build-essential libncursesw5-dev libreadline5-dev libssl-dev libgdbm-dev libbz2-dev libc6-dev libsqlite3-dev tk-dev g++ gcc Solution for _dbm $ wget -c http://bugs.python.org/file12234/dbm.diff $ patch -p0 < dbm.diff $ sudo apt-get build-dep python2.5 $ make $ sudo make install Regards Dayo From a.t.hofkamp at tue.nl Fri Apr 24 12:57:37 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Fri, 24 Apr 2009 12:57:37 +0200 Subject: [Tutor] how to compile python3.0 In-Reply-To: References: Message-ID: <49F19B21.4040300@tue.nl> Shaofeng NIu wrote: > I tried to compile and install python3.0 from source,but after "make",it shows: > > Python build finished, but the necessary bits to build these modules were not found: > _dbm _gdbm _hashlib > _sqlite3 _ssl _tkinter > bz2 readline > To find the necessary bits, look in setup.py in detect_modules() for the module's name. > > Could anybody tell me how to install these modules?Thank you! > My OS is Ubuntu 8.10 In Python 2.X, modules are skipped when you do not have the necessary libraries and headers installed. Assuming the same happens with Python 3.0, the solution would be to install the libraries and headers needed by the above modules before compiling Python 3.0. These libraries and headers are available as seperate "XYZ-devel" packages for your OS. W.r.t. what libraries/packages you really need, some of them are guessable, bz2 eg will probably need a bz2-devel or a bzip2-devel package, etc. Others may be more difficult to guess For those, you may want to examine the function mentioned in the output to understand what libraries/headers it is missing. Note that different linuces use slightly different names to refer to some library, so the names may not match exactly. Albert From alan.gauld at btinternet.com Fri Apr 24 19:29:47 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 24 Apr 2009 18:29:47 +0100 Subject: [Tutor] sorting algorithim References: <736020.87470.qm@web56601.mail.re3.yahoo.com> Message-ID: "tonyon boyo" wrote > i know how to print for bubble sort in python, is > there a way to print each pass in the sort so > i can see what happens at each step? thanks Yes add a print statement inside the looop that prints the list being sorted. > def bubblesort(list): > for passes in range(len(list)-1, 0, -1): You could use list slicing here: for passes in list[-2::-1]: But you might find using a while loop is easier than a for loop for bubble sort -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emadnawfal at gmail.com Fri Apr 24 22:20:02 2009 From: emadnawfal at gmail.com (=?windows-1256?B?RW1hZCBOYXdmYWwgKNrjx88g5Obd4Sk=?=) Date: Fri, 24 Apr 2009 16:20:02 -0400 Subject: [Tutor] Pyparsing question: marking matches in Pyparsing Message-ID: <652641e90904241320o1b936cb9j3b27666972b3fd71@mail.gmail.com> Hello Tutors, I've used Pyparsing to write a Noun Phrase Extractor that extracts noun phrases from a Part-of-Speech tagged file. My question is: how can I mark, instead of extract, the phrases. For example, In the sentence: The DET big ADJ woman NOUN saw VERB and CONJ greeted VERB the DET green ADJ man NOUN both "The big woman" and "the green man" are noun phrases. I need the results to look like this: The big woman saw and greeted the green man # This script is far from complete. It works only with Arabic # This is a parser for NP chunks # This depends on non-vocalized texts and tags. from pyparsing import * CASE = oneOf("CASE_INDEF_ACC CASE_INDEF_GEN CASE_INDEF_NOM") DEMONSTRATIVE = oneOf("DE DEM_PRON_F DEM_PRON_FD DEM_PRON_FS DEM_PRON_MD DEM_PRON_MP DEM_PRON_MS") NOUN_SUFFIX = oneOf("NSUFF_FEM_DU_ACC NSUFF_FEM_DU_GEN NSUFF_FEM_DU_NOM NSUFF_FEM_PL NSUFF_FEM_SG NSUFF_MASC_DU_ACC NSUFF_MASC_DU_GEN NSUFF_MASC_DU_NOM NSUFF_MASC_PL_ACC NSUFF_MASC_PL_GEN NSUFF_MASC_PL_NOM") NOUN_SUFFIX_IDAFA = oneOf("NSUFF_FEM_DU_ACC_POSS NSUFF_FEM_DU_GEN_POSS NSUFF_FEM_DU_NOM_POSS NSUFF_MASC_DU_ACC_POSS NSUFF_MASC_DU_GEN_POSS NSUFF_MASC_DU_NOM_POSS NSUFF_MASC_PL_ACC_POSS NSUFF_MASC_PL_GEN_POSS NSUFF_MASC_PL_NOM_POSS") POSSESSIVE_PRONOUN = oneOf("POSS_PRON_1P POSS_PRON_1S POSS_PRON_2FS POSS_PRON_2MP POSS_PRON_2MS POSS_PRON_3D POSS_PRON_3FP POSS_PRON_3FS POSS_PRON_3MP POSS_PRON_3MS") PRONOUN = oneOf("PRON_1P PRON_1S PRON_2FS PRON_2MP PRON_2MS PRON_3D PRON_3FP PRON_3FS" "PRON_3MP PRON_3MS") lexical = Word(alphas+"$"+"<"+">"+"|"+"}"+"{") NOUN = Literal("NOUN") DET = Literal("DET") ADJ = Literal("ADJ") NOMINAL = lexical + NOUN + Optional(lexical + NOUN_SUFFIX) import sys infile = open(sys.argv[1]).read() # This is for the definite NP made up of a noun + an adjective # An example is Alrjl AlmHtrm AL = Literal("Al") DEFINITE_NOUN = AL + DET + lexical + NOUN + Optional(NOUN_SUFFIX) DEFINITE_ADJECTIVE = AL + DET + lexical + ADJ + Optional(NOUN_SUFFIX) NOUN_ADJ = DEFINITE_NOUN + ZeroOrMore(DEFINITE_ADJECTIVE) DEMON_NOUN_ADJ = Optional(DEMONSTRATIVE) + NOUN_ADJ # Now for the indefinite NP # Example: rjl mHtrm INDEFINITE_NOUN = lexical + NOUN + Optional(NOUN_SUFFIX) INDEFINITE_ADJECTIVE = lexical + ADJ + Optional(NOUN_SUFFIX) INDEF_NOUN_ADJ = INDEFINITE_NOUN + ZeroOrMore(DEFINITE_ADJECTIVE) pattern3 = OneOrMore(NOMINAL) + lexical + DET + NOMINAL #pattern2 = NP = pattern3 | DEMON_NOUN_ADJ | INDEF_NOUN_ADJ # get the file import sys infile = open(sys.argv[1]).read() tokens = NP.scanString(infile) for x in tokens: for i,v in enumerate(x[0]): if i%2 == 0: print v, print "\n" -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From inthefridge at gmail.com Fri Apr 24 23:26:32 2009 From: inthefridge at gmail.com (Spencer Parker) Date: Fri, 24 Apr 2009 15:26:32 -0600 Subject: [Tutor] Threading... Message-ID: I have a script that I want to test MySQL sonnections with. The way I have the script working is to just create connections, but I realized that it is going to wait for the first one to stop before starting a new connection. I want to throw a ton of open connections at it to see what the connection limit is and how it reacts when it hits that shelf. Would the best route for this be threading? or is there another way to go about it? -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Fri Apr 24 23:39:13 2009 From: denis.spir at free.fr (spir) Date: Fri, 24 Apr 2009 23:39:13 +0200 Subject: [Tutor] Pyparsing question: marking matches in Pyparsing In-Reply-To: <652641e90904241320o1b936cb9j3b27666972b3fd71@mail.gmail.com> References: <652641e90904241320o1b936cb9j3b27666972b3fd71@mail.gmail.com> Message-ID: <20090424233913.56f2d33f@o> Le Fri, 24 Apr 2009 16:20:02 -0400, Emad Nawfal (???? ????) s'exprima ainsi: > Hello Tutors, > I've used Pyparsing to write a Noun Phrase Extractor that extracts noun > phrases from a Part-of-Speech tagged file. My question is: how can I mark, > instead of extract, the phrases. For example, In the sentence: > > The DET big ADJ woman NOUN saw VERB and CONJ greeted VERB the DET green > ADJ man NOUN > > both "The big woman" and "the green man" are noun phrases. > I need the results to look like this: > > The big woman saw and greeted the green man [...] Paul MacGuire may have a better solution, but I would use what in pyparsing is called (I guess) a post-parse action. I don't remember the exact syntax, sorry, anyway the method is simply to define a func that will transform the parse result (node) produced by a pattern, then set it on the pattern. For instance: NOUN_PHRASE = def markNounPhrase(result): NOUN_PHRASE.setParseAction(markNounPhrase) !!! Not sure of the syntax. !!! Beware of the internal structure of the result (possibly the actual value you need to catch is one or more levels nested in the result produced by the pattern). You'd better print it before writing the func. Denis ------ la vita e estrany From alan.gauld at btinternet.com Sat Apr 25 01:26:11 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 25 Apr 2009 00:26:11 +0100 Subject: [Tutor] Threading... References: Message-ID: "Spencer Parker" wrote in message news:c7a040fa0904241426h70a0c82bhf95e476fe5ed068c at mail.gmail.com... >I have a script that I want to test MySQL sonnections with. The way I >have > the script working is to just create connections, but I realized that it > is > going to wait for the first one to stop before starting a new connection. > I > want to throw a ton of open connections at it to see what the connection > limit is and how it reacts when it hits that shelf. Would the best route > for this be threading? or is there another way to go about it? Yes threading is probably the way to go but remember that each thread will take up CPU resource so the limits on performance could be the client computer rather than the server. Also the network connection between client and server will have a strong impact on connectivity, and not just bandwidth, you really need to account for queue latency on the ports. Ideally you should be using multiple network connections, multiple ports per connection and multiple clients if you really want a feel for how a server will respond under load. Finally, make sure you aren't just repeating the same data request for each hit otherwise locking tests etc will slow things down too. Its a good idea to have as many rows as you want to run tests and have alternate queries hit opposite ends of the table, or better still have several similar tables and round-robin the queries. Just some common gotchas with database performance testing. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From Snice14247 at aol.com Sat Apr 25 03:00:05 2009 From: Snice14247 at aol.com (IT_ForMe) Date: Fri, 24 Apr 2009 18:00:05 -0700 (PDT) Subject: [Tutor] Python help In-Reply-To: <49EF5223.3020209@cfl.rr.com> References: <23175541.post@talk.nabble.com> <49EF5223.3020209@cfl.rr.com> Message-ID: <23227229.post@talk.nabble.com> cart = {"apple":2.00, "orange":2.50, "banana":1.75} print cart apple = 2 orange = 2.5 banana = 1.75 totalprice = 0 totalprice = apple + orange + banana print "your subtotal is 'totalprice'" taxedprice = (totalprice *.07) + totalprice print "your final price is 'taxedprice'" prompt = raw_input("are you a student?") if yes ('taxedprice' / 10) + taxed price = student print "your student price is 'student' else print "have a nice day" Robert Berman-2 wrote: > > Yes. > > Show us your solution and perhaps we can help you make it a tad more > efficient. > > Robert Berman > > IT_ForMe wrote: >> Anyone know how to program this using Python >> >> Add up the prices for items in a shopping cart. Some items are taxable >> and >> some items are not. You may assume a 7% tax rate for all taxable items. >> Apply a 10% discount if the customer is a student. (hint, instead of >> asking >> the user to input items, set them up as an associative array in the code) >> > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- View this message in context: http://www.nabble.com/Python-help-tp23175541p23227229.html Sent from the Python - tutor mailing list archive at Nabble.com. From shaofeng.niu at gmail.com Fri Apr 24 17:59:57 2009 From: shaofeng.niu at gmail.com (Shaofeng NIu) Date: Fri, 24 Apr 2009 23:59:57 +0800 Subject: [Tutor] how to compile python3.0 In-Reply-To: <49F19A81.2030406@gmail.com> References: <49F19A81.2030406@gmail.com> Message-ID: that worked for me,too :) thanks to all the peopel helping me 2009/4/24 Dayo Adewunmi > Shaofeng NIu wrote: > >> I tried to compile and install python3.0 from source,but after "make",it >> shows: >> >> Python build finished, but the necessary bits to build these modules were >> not found: >> _dbm _gdbm _hashlib _sqlite3 >> _ssl _tkinter bz2 readline >> To find the necessary bits, look in setup.py in detect_modules() >> for the module's name. >> >> Could anybody tell me how to install these modules?Thank you! >> My OS is Ubuntu 8.10 >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > > This worked for me on Ubuntu 8.04: > > $ sudo apt-get install build-essential libncursesw5-dev libreadline5-dev > libssl-dev libgdbm-dev libbz2-dev libc6-dev libsqlite3-dev tk-dev g++ gcc > > Solution for _dbm > $ wget -c http://bugs.python.org/file12234/dbm.diff > $ patch -p0 < dbm.diff > > $ sudo apt-get build-dep python2.5 > $ make > $ sudo make install > > Regards > > Dayo > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Apr 25 10:00:04 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 25 Apr 2009 09:00:04 +0100 Subject: [Tutor] Python help References: <23175541.post@talk.nabble.com> <49EF5223.3020209@cfl.rr.com> <23227229.post@talk.nabble.com> Message-ID: "IT_ForMe" wrote > cart = {"apple":2.00, "orange":2.50, "banana":1.75} You set this up but then don't use it. Also your problem statement said *some* items were taxable, implying some were not. You might want to record whether an item is taxable in the dictionary too. > print cart > apple = 2 > orange = 2.5 > banana = 1.75 You don't need these lines since they are replicating your dictionary above > totalprice = 0 You don't need this since you will set totalprice in the next line. > totalprice = apple + orange + banana You should use the dictionary here, and you could apply the tax to the taxable items before generating the total. Also you might like to use a loop to get the total since the cart might change the number of items in a real world scenario. > print "your subtotal is 'totalprice'" > taxedprice = (totalprice *.07) + totalprice It might be easier to just multiple by 1.07 Also it would be good to store the tax as a variable in case the rate changes. > print "your final price is 'taxedprice'" > prompt = raw_input("are you a student?") > if yes > ('taxedprice' / 10) + taxed price = student you must assign a value to a variable you cannot assign a variable to an expression. Also, I thought the students got a discount? This provides a surcharge! > print "your student price is 'student' > else > print "have a nice day" You also have some faulty syntax in the last if/else section which Python will tell you about. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From prasadaraon50 at gmail.com Sat Apr 25 11:38:00 2009 From: prasadaraon50 at gmail.com (prasad rao) Date: Sat, 25 Apr 2009 15:08:00 +0530 Subject: [Tutor] Is it syntactic,semantic or runtime? Message-ID: <9e3fac840904250238n392f41c1wd03aa254691ec17f@mail.gmail.com> helloI wrote a function to fetch data using urllib and displaying the data using internet Explorer. it is not preparing the html file So opening blank Explorer after complaining th html file not found. Where is the problem? Can I display the data read from a site without preparing a html file? def pp(): @@@ import urllib @@@ import subprocess @@@ so=urllib.urlopen('http://www.asstr.org') @@@ data=so.read() @@@ de=open('C:pp.html','w') @@@ de.write(data) @@@ subprocess.Popen(['C:\Program Files\Internet xplorer\\IEXPLORE','C:pp.html']) prasad -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Sat Apr 25 12:10:20 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Sat, 25 Apr 2009 12:10:20 +0200 Subject: [Tutor] Is it syntactic,semantic or runtime? In-Reply-To: <9e3fac840904250238n392f41c1wd03aa254691ec17f@mail.gmail.com> References: <9e3fac840904250238n392f41c1wd03aa254691ec17f@mail.gmail.com> Message-ID: 2009/4/25 prasad rao : > So opening blank Explorer after complaining th html file not found. > Where is the problem? There are typing errors on the below... > > def pp(): > @@@ import urllib > @@@ import subprocess > @@@ so=urllib.urlopen('http://www.asstr.org') > @@@ data=so.read() > @@@ de=open('C:pp.html','w') This create a file pp.html in the directory where the script was run from. I expect you want it in c:\ so it should look like. Notice the backslash is escaped with a backslash! de = open('c:\\pp.html', 'w') > @@@ de.write(data) > @@@ subprocess.Popen(['C:\Program Files\Internet > xplorer\\IEXPLORE','C:pp.html']) Again the same issue as above plus the path to iexplore.exe is bad. subprocess.Popen(['C:\\Program Files\\Internet Explorer\\iexplore.exe','C:\\pp.html']) This works for me. Greets Sander From domeier at umich.edu Sat Apr 25 05:57:42 2009 From: domeier at umich.edu (Matt Domeier) Date: Fri, 24 Apr 2009 23:57:42 -0400 Subject: [Tutor] output formatting Message-ID: <20090424235742.63985oy1agmiasso@web.mail.umich.edu> Hello, I have a series of lists that I need to output into files with a specific format. Specifically, I need to have line breaks after each entry of the list, and I need to do away with the ['..'] that accompany the data after I transform the list into a string. Can I simply append a '\n' to the end of all the list entries in order to enact line breaks? Thanks! From srilyk at gmail.com Sat Apr 25 13:39:44 2009 From: srilyk at gmail.com (W W) Date: Sat, 25 Apr 2009 06:39:44 -0500 Subject: [Tutor] output formatting In-Reply-To: <20090424235742.63985oy1agmiasso@web.mail.umich.edu> References: <20090424235742.63985oy1agmiasso@web.mail.umich.edu> Message-ID: <333efb450904250439l324377c3labf193d4c489c572@mail.gmail.com> On Fri, Apr 24, 2009 at 10:57 PM, Matt Domeier wrote: > Hello, > > I have a series of lists that I need to output into files with a specific > format. Specifically, I need to have line breaks after each entry of the > list, and I need to do away with the ['..'] that accompany the data after I > transform the list into a string. Can I simply append a '\n' to the end of > all the list entries in order to enact line breaks? > Is this what you're looking for? In [3]: print '\n'.join(['the quick brown fox', 'jumps over', 'the lazy dog']) the quick brown fox jumps over the lazy dog HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Sat Apr 25 13:45:51 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 25 Apr 2009 07:45:51 -0400 Subject: [Tutor] Is it syntactic,semantic or runtime? In-Reply-To: <9e3fac840904250238n392f41c1wd03aa254691ec17f@mail.gmail.com> References: <9e3fac840904250238n392f41c1wd03aa254691ec17f@mail.gmail.com> Message-ID: <1c2a2c590904250445i21e46d0dv608a9d1954973125@mail.gmail.com> On Sat, Apr 25, 2009 at 5:38 AM, prasad rao wrote: > hello > I wrote a function to fetch data using urllib and displaying the data > using internet Explorer. > it is not preparing the html file > So opening blank Explorer after complaining th html file not found. > Where is the problem? > Can I display the data read from a site ?without preparing > a html file? > > def pp(): > @@@ import urllib > @@@ import subprocess > @@@ so=urllib.urlopen('http://www.asstr.org') > @@@ data=so.read() > @@@ de=open('C:pp.html','w') > @@@ de.write(data) You should close de here so the file is flushed to disk: de.close() Kent > @@@ subprocess.Popen(['C:\Program Files\Internet > xplorer\\IEXPLORE','C:pp.html']) > > > > prasad > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > From emadnawfal at gmail.com Sat Apr 25 13:48:30 2009 From: emadnawfal at gmail.com (=?windows-1256?B?RW1hZCBOYXdmYWwgKNrjx88g5Obd4Sk=?=) Date: Sat, 25 Apr 2009 07:48:30 -0400 Subject: [Tutor] how to compile python3.0 In-Reply-To: References: <49F19A81.2030406@gmail.com> Message-ID: <652641e90904250448l495f1fc0tc7b85159bb4b922a@mail.gmail.com> On Fri, Apr 24, 2009 at 11:59 AM, Shaofeng NIu wrote: > that worked for me,too :) thanks to all the peopel helping me > > 2009/4/24 Dayo Adewunmi > >> Shaofeng NIu wrote: >> >>> I tried to compile and install python3.0 from source,but after "make",it >>> shows: >>> >>> Python build finished, but the necessary bits to build these modules were >>> not found: >>> _dbm _gdbm _hashlib _sqlite3 >>> _ssl _tkinter bz2 readline >>> To find the necessary bits, look in setup.py in detect_modules() >>> for the module's name. >>> >>> Could anybody tell me how to install these modules?Thank you! >>> My OS is Ubuntu 8.10 >>> ------------------------------------------------------------------------ >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> http://mail.python.org/mailman/listinfo/tutor >>> >>> >> >> This worked for me on Ubuntu 8.04: >> >> $ sudo apt-get install build-essential libncursesw5-dev libreadline5-dev >> libssl-dev libgdbm-dev libbz2-dev libc6-dev libsqlite3-dev tk-dev g++ gcc >> >> Solution for _dbm >> $ wget -c http://bugs.python.org/file12234/dbm.diff >> $ patch -p0 < dbm.diff >> >> $ sudo apt-get build-dep python2.5 >> $ make >> $ sudo make install >> >> Regards >> >> Dayo > > The following is a previous post by Vern Ceder. I thought it might help. I have Python 3.0/Tkinter/IDLE working fine on Ubuntu 8.10, but it takes a certain amount of fiddling. 1. Make sure the stock Ubuntu Python 3.0 package is not installed 2. download the Python 3.0 source from python.org 3. install the following packages: build-essential libsqlite3-dev libreadline5-dev libncurses5-dev zlib1g-dev libbz2-dev libssl-dev libgdbm-dev tk-dev 4. unpack the Python source and switch to that folder 5. build Python using the standard ./configure, make, make install sequence - if you want more detail/help on that process, just ask... I'd be happy to explain this process in more detail if anyone wants... > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > -- ?? ???? ?????? ????? ????? ??? ???? ??? ????? ?? ?????? ????????.....???? ??????? "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington -------------------------------------------------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Sat Apr 25 13:56:24 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 25 Apr 2009 07:56:24 -0400 Subject: [Tutor] Threading... In-Reply-To: References: Message-ID: <1c2a2c590904250456h316a9f9ibfb06217d40931cb@mail.gmail.com> On Fri, Apr 24, 2009 at 5:26 PM, Spencer Parker wrote: > I have a script that I want to test MySQL sonnections with.? The way I have > the script working is to just create connections, but I realized that it is > going to wait for the first one to stop before starting a new connection.? I > want to throw a ton of open connections at it to see what the connection > limit is and how it reacts when it hits that shelf.? Would the best route > for this be threading?? or is there another way to go about it? Threading would be a start, though as Alan says it may not let you test the limits. To get started with threading you might want to use one of the thread pool recipes in the Python cookbook. http://code.activestate.com/recipes/576576/ (with links to many more) http://code.activestate.com/recipes/576519/ (has the same interface as multiprocessing.Pool so you could convert from threads to processes easily) Kent From alan.gauld at btinternet.com Sat Apr 25 14:34:39 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 25 Apr 2009 13:34:39 +0100 Subject: [Tutor] Is it syntactic,semantic or runtime? References: <9e3fac840904250238n392f41c1wd03aa254691ec17f@mail.gmail.com> Message-ID: "prasad rao" wrote > helloI wrote a function to fetch data using urllib and displaying the > data > using internet Explorer. it is not preparing the html file > So opening blank Explorer after complaining th html file not found. The specific problem has been addressed but in general it is wise to manually check these kinds of problems to see where the problem is and solve that first. Thus in this case you needed to check: 1) Was the html file being created where you expected with the data you expected? 2) Could IE launch from the command line if you provided that file as an argument? In your case test 1 would fail - no file where you expected, so the next questions to ask are 1a) Is the file being created someplace else (use search) 1b) If so does it have the data I expect? 1c) Can I open IE with the new location That would have shown where the file was being created. And hopefully that IE could open it. This kind of basic detective work can very quickly identify the errors in the code, much faster than just peering at the source will - especially when, as in your case, there are several errors. And if you have done that kind of investigation and still can't find the problem then let us know what you have discovered when you post. Just some thoughts for the future, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From davea at ieee.org Sat Apr 25 15:18:10 2009 From: davea at ieee.org (Dave Angel) Date: Sat, 25 Apr 2009 09:18:10 -0400 Subject: [Tutor] output formatting In-Reply-To: References: Message-ID: <49F30D92.5050103@ieee.org> Matt Domeier wrote: > Hello, > > I have a series of lists that I need to output into files with a > specific format. Specifically, I need to have line breaks after each > entry of the list, and I need to do away with the ['..'] that > accompany the data after I transform the list into a string. Can I > simply append a '\n' to the end of all the list entries in order to > enact line breaks? > > Thanks! > > First point: you should include source code for what you're doing (cut 'n paste, don't retype it). Normally, it should also include sample data and/or sample results. For example, you say you're transforming the list into a string, and that ".." accompanies the data. I have to guess what you're doing, and therefore how to fis it. I'd guess that for some reason you're using "..".join(mylist) which would make a string by concatenating all the strings in the list, with ".." between them. As Wayne has pointed out, you could just concatenate them using the "\n" separator instead of the ".." But notice that puts newlines between the elements, but doesn't put one at the end. Since he's using print, that automatically adds a trailing newline. But probably you're not using print. You didn't say. But you say you want to put them into *files*, and "with a specific format." It's only a wild guess that the plural was a typo, and that the specific format is called a text file. Much more likely, you want to put some of them in one file, formatted a certain way, and some into another file, formatted differently. If that's the case, why not loop through the list, doing whatever you really need? You can always use a "\n" as part of the format when doing the call to write(), or whatever you're using to do the file I/O. Or just use writeline(), which implicitly adds the newline. From prasadaraon50 at gmail.com Sat Apr 25 15:44:30 2009 From: prasadaraon50 at gmail.com (prasad rao) Date: Sat, 25 Apr 2009 19:14:30 +0530 Subject: [Tutor] Is it syntactic,semantic or runtime? Message-ID: <9e3fac840904250644i2697454bk4d3ec7f363bc5e73@mail.gmail.com> hellothanks .After inserting back slashes and de.close it is working well thanks for the help prasad -------------- next part -------------- An HTML attachment was scrubbed... URL: From danliang20 at gmail.com Sat Apr 25 16:01:29 2009 From: danliang20 at gmail.com (Dan Liang) Date: Sat, 25 Apr 2009 10:01:29 -0400 Subject: [Tutor] Working with lines from file and printing to another keeping sequential order Message-ID: Dear Tutors, I have a file from which I want to extract lines that end in certain strings and print to a second file. More specifically, I want to: 1) iterate over each line in the file, and if it ends in "yes", print it. 2) move to the line following the one described in #1 above, and if it ends in, "no" print it. 3) move to third line, and if it ends in "no", print it. 4) move to fourth line, and if it ends in "no" discard it, but if it ends in "yes" repeat 1, 2, and 3 above. 5) move to fifth line, and if it ends in "no" discard it, but if it ends in "yes" repeat 1, 2, 3, and 4 above, and so on. The goal is to get a ratio of 1 to 2 "yes" to "no" lines from a file in such a way that keeps the order of the lines in output. An abstraction away from this so that any ratio of "yes" to "no" lines could be printed while keeping the order of the original lines would be great. I am new to Python and could not solve the problem. Your help is appreciated. Cheers, --dan -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Sat Apr 25 16:32:39 2009 From: bgailer at gmail.com (bob gailer) Date: Sat, 25 Apr 2009 10:32:39 -0400 Subject: [Tutor] Working with lines from file and printing to another keeping sequential order In-Reply-To: References: Message-ID: <49F31F07.8060702@gmail.com> Dan Liang wrote: > Dear Tutors, > > > I have a file from which I want to extract lines that end in certain > strings and print to a second file. More specifically, I want to: > > 1) iterate over each line in the file, and if it ends in "yes", print it. > 2) move to the line following the one described in #1 above, and if it > ends in, "no" print it. > 3) move to third line, and if it ends in "no", print it. > 4) move to fourth line, and if it ends in "no" discard it, but if it > ends in "yes" repeat 1, 2, and 3 above. > 5) move to fifth line, and if it ends in "no" discard it, but if it > ends in "yes" repeat 1, 2, 3, and 4 above, and so on. > > The goal is to get a ratio of 1 to 2 "yes" to "no" lines from a file > in such a way that keeps the order of the lines in output. An > abstraction away from this so that any ratio of "yes" to "no" lines > could be printed while keeping the order of the original lines would > be great. Please show us what code you have written, and in what way it fails to meet your expectations. Your specification is IMHO a nice piece of pseudocode which could translate to Python fairly easily! What do we do at steps 4ff if the line does not end in "yes" or "no"? If you have not written any code make a stab at it. You could start by asking "how in Python does one": open a file? iterate (loop)? get the next line from a file? test for equality? examine the end of a string? -- Bob Gailer Chapel Hill NC 919-636-4239 From ian-campbell at shaw.ca Sat Apr 25 17:35:00 2009 From: ian-campbell at shaw.ca (Ian Campbell) Date: Sat, 25 Apr 2009 08:35:00 -0700 Subject: [Tutor] # what do I do now to kill the the Windows program which has finished its work? Message-ID: <49F32DA4.8080403@shaw.ca> I am converting a Windows ProComm script into Python and need help. ; previous Procomm script here run "c:\buy.exe" taskId ; this runs the c:\buy.exe program and assigns the task identification number to the integer variable called "taskId" pause 1 ; wait one second while taskexists(taskId) exitTask(taskId) ; kills the task endwhile ; we carry on.... the ProComm code to completion How do I do the same in Python? os.startfile("c:\\buy.exe" , 'OPEN') # THIS WORKS and returns but does not return the taskID # what do I do now to kill the the Windows program which has finished its work? From domeier at umich.edu Sat Apr 25 17:02:47 2009 From: domeier at umich.edu (Matt Domeier) Date: Sat, 25 Apr 2009 11:02:47 -0400 Subject: [Tutor] output formatting In-Reply-To: <333efb450904250439l324377c3labf193d4c489c572@mail.gmail.com> References: <20090424235742.63985oy1agmiasso@web.mail.umich.edu> <333efb450904250439l324377c3labf193d4c489c572@mail.gmail.com> Message-ID: <20090425110247.8830724i9evjiqkg@web.mail.umich.edu> Hi Wayne, Yes, that should work perfectly, but it is in writing files that I'm having the problem. I'm still very new to python, so I only know to use something like filename.write(str(listname)) to write my list to a file.. Is there a straightforward method of combining the ease of the print function setup that you suggested with write? Or could I somehow use the print function itself to write to the file (rather than just output to my shell/prompt)? Thanks! Quoting W W : > On Fri, Apr 24, 2009 at 10:57 PM, Matt Domeier wrote: > >> Hello, >> >> I have a series of lists that I need to output into files with a specific >> format. Specifically, I need to have line breaks after each entry of the >> list, and I need to do away with the ['..'] that accompany the data after I >> transform the list into a string. Can I simply append a '\n' to the end of >> all the list entries in order to enact line breaks? >> > > Is this what you're looking for? > > In [3]: print '\n'.join(['the quick brown fox', 'jumps over', 'the lazy > dog']) > the quick brown fox > jumps over > the lazy dog > > HTH, > Wayne > From denis.spir at free.fr Sat Apr 25 19:21:44 2009 From: denis.spir at free.fr (spir) Date: Sat, 25 Apr 2009 19:21:44 +0200 Subject: [Tutor] output formatting In-Reply-To: <20090425110247.8830724i9evjiqkg@web.mail.umich.edu> References: <20090424235742.63985oy1agmiasso@web.mail.umich.edu> <333efb450904250439l324377c3labf193d4c489c572@mail.gmail.com> <20090425110247.8830724i9evjiqkg@web.mail.umich.edu> Message-ID: <20090425192144.0dd24d0d@o> Le Sat, 25 Apr 2009 11:02:47 -0400, Matt Domeier s'exprima ainsi: > Hi Wayne, > > Yes, that should work perfectly, but it is in writing files that I'm > having the problem. I'm still very new to python, so I only know to > use something like filename.write(str(listname)) to write my list to a > file.. > Is there a straightforward method of combining the ease of the print > function setup that you suggested with write? Or could I somehow use > the print function itself to write to the file (rather than just > output to my shell/prompt)? > > Thanks! I think you're confusing the writing function (file.write() or print) with the expression of the text to be written. You can safely reuse the expression proposed by Wayne inside write(): filename.write( '\n'.join(['the quick brown fox', 'jumps over', 'the lazy dog']) ) The remaining difference is that print will implicitely add an '\n' (end-of-line). Also (but this difference does not apply here), write() expects a string while print will silently convert to str whatever you pass it: f = file("test", 'w') try: f.write(1) except TypeError: f.write("*** not a str ***") f.write(str(1)) f.close() f = file("test", 'r') print f.read() f.close() ==> *** not a str *** 1 Denis > Quoting W W : > > > On Fri, Apr 24, 2009 at 10:57 PM, Matt Domeier wrote: > > > >> Hello, > >> > >> I have a series of lists that I need to output into files with a specific > >> format. Specifically, I need to have line breaks after each entry of the > >> list, and I need to do away with the ['..'] that accompany the data > >> after I transform the list into a string. Can I simply append a '\n' to > >> the end of all the list entries in order to enact line breaks? > >> > > > > Is this what you're looking for? > > > > In [3]: print '\n'.join(['the quick brown fox', 'jumps over', 'the lazy > > dog']) > > the quick brown fox > > jumps over > > the lazy dog > > > > HTH, > > Wayne > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > ------ la vita e estrany From danliang20 at gmail.com Sat Apr 25 20:11:28 2009 From: danliang20 at gmail.com (Dan Liang) Date: Sat, 25 Apr 2009 14:11:28 -0400 Subject: [Tutor] Working with lines from file and printing to another keeping sequential order In-Reply-To: <49F31F07.8060702@gmail.com> References: <49F31F07.8060702@gmail.com> Message-ID: Hi Bob and tutors, Thanks Bob for your response! currently I have the current code, but it does not work: ListLines= [] for line in open('test.txt'): line = line.rstrip() ListLines.append(line) for i in range(len(ListLines)): if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and ListLines[i+1].endswith("no"): print ListLines[i], ListLines[i+1], ListLines[i+2] elif ListLines[i].endswith("yes") and ListLines[i+1].endswith("no"): print ListLines[i], ListLines[i+1] elif ListLines[i].endswith("yes"): print ListLines[i] elif ListLines[i].endswith("no"): continue else: break I get the following error: Traceback (most recent call last): File "test.py", line 18, in if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and ListLines[i+1].endswith("no"): IndexError: list index out of range Lines in the file look like following: word1 word2 word3 word4 yes word1 word2 word3 word4 no word1 word2 word3 word4 no word1 word2 word3 word4 no word1 word2 word3 word4 yes word1 word2 word3 word4 no word1 word2 word3 word4 yes word1 word2 word3 word4 no word1 word2 word3 word4 yes word1 word2 word3 word4 yes word1 word2 word3 word4 no word1 word2 word3 word4 no word1 word2 word3 word4 no word1 word2 word3 word4 yes word1 word2 word3 word4 no word1 word2 word3 word4 yes word1 word2 word3 word4 yes word1 word2 word3 word4 no word1 word2 word3 word4 no word1 word2 word3 word4 no word1 word2 word3 word4 no word1 word2 word3 word4 yes > What do we do at steps 4ff if the line does not end in "yes" or "no"? I forgot to mention that I know for sure that the file has ONLY lines that end in either "yes" or "no". Any suggestions are appreciated. Also, I feel that my code is not the best way of solving the problem even if the problem of list indices is solved. Is my guess right? Thank you! -dan On Sat, Apr 25, 2009 at 10:32 AM, bob gailer wrote: > Dan Liang wrote: > >> Dear Tutors, >> >> >> I have a file from which I want to extract lines that end in certain >> strings and print to a second file. More specifically, I want to: >> >> 1) iterate over each line in the file, and if it ends in "yes", print it. >> 2) move to the line following the one described in #1 above, and if it >> ends in, "no" print it. >> 3) move to third line, and if it ends in "no", print it. >> 4) move to fourth line, and if it ends in "no" discard it, but if it ends >> in "yes" repeat 1, 2, and 3 above. >> 5) move to fifth line, and if it ends in "no" discard it, but if it ends >> in "yes" repeat 1, 2, 3, and 4 above, and so on. >> >> The goal is to get a ratio of 1 to 2 "yes" to "no" lines from a file in >> such a way that keeps the order of the lines in output. An abstraction away >> from this so that any ratio of "yes" to "no" lines could be printed while >> keeping the order of the original lines would be great. >> > > Please show us what code you have written, and in what way it fails to meet > your expectations. > > Your specification is IMHO a nice piece of pseudocode which could translate > to Python fairly easily! > > What do we do at steps 4ff if the line does not end in "yes" or "no"? > > If you have not written any code make a stab at it. You could start by > asking "how in Python does one": > open a file? > iterate (loop)? > get the next line from a file? > test for equality? > examine the end of a string? > > > -- > Bob Gailer > Chapel Hill NC > 919-636-4239 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From =?UTF-8?Q?=22Shantanoo_Mahajan_=28=E0=A4=B6=E0=A4=82=E0=A4=A4?= Sat Apr 25 20:41:55 2009 From: =?UTF-8?Q?=22Shantanoo_Mahajan_=28=E0=A4=B6=E0=A4=82=E0=A4=A4?= (=?UTF-8?Q?=22Shantanoo_Mahajan_=28=E0=A4=B6=E0=A4=82=E0=A4=A4?=) Date: Sun, 26 Apr 2009 00:11:55 +0530 Subject: [Tutor] Working with lines from file and printing to another keeping sequential order In-Reply-To: References: <49F31F07.8060702@gmail.com> Message-ID: <79ED4DB5-F95A-483E-AF73-74E35E75C183@gmail.com> On 25-Apr-09, at 11:41 PM, Dan Liang wrote: > Hi Bob and tutors, > > Thanks Bob for your response! currently I have the current code, but > it does not work: > > ListLines= [] > for line in open('test.txt'): > line = line.rstrip() > ListLines.append(line) > > for i in range(len(ListLines)): > > if ListLines[i].endswith("yes") and ListLines[i > +1].endswith("no") and ListLines[i+1].endswith("no"): > print ListLines[i], ListLines[i+1], ListLines[i+2] > elif ListLines[i].endswith("yes") and ListLines[i > +1].endswith("no"): > print ListLines[i], ListLines[i+1] > elif ListLines[i].endswith("yes"): > print ListLines[i] > elif ListLines[i].endswith("no"): > continue > else: > break > > I get the following error: > Traceback (most recent call last): > File "test.py", line 18, in > if ListLines[i].endswith("yes") and ListLines[i > +1].endswith("no") and ListLines[i+1].endswith("no"): > IndexError: list index out of range You need to put check for i <= len(ListLines)-2 or you can have a look @ try and except (exception handling in python) > Lines in the file look like following: > > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > > > What do we do at steps 4ff if the line does not end in "yes" or > "no"? > > I forgot to mention that I know for sure that the file has ONLY > lines that end in either "yes" or "no". > > Any suggestions are appreciated. Also, I feel that my code is not > the best way of solving the problem even if the problem of list > indices is solved. Is my guess right? > Another suggestion. I would not read all data from file into array (memory). If your file is large, you may hit out of memory error. regards, shantanoo > Thank you! > > -dan > > On Sat, Apr 25, 2009 at 10:32 AM, bob gailer > wrote: > Dan Liang wrote: > Dear Tutors, > > > I have a file from which I want to extract lines that end in certain > strings and print to a second file. More specifically, I want to: > > 1) iterate over each line in the file, and if it ends in "yes", > print it. > 2) move to the line following the one described in #1 above, and if > it ends in, "no" print it. > 3) move to third line, and if it ends in "no", print it. > 4) move to fourth line, and if it ends in "no" discard it, but if it > ends in "yes" repeat 1, 2, and 3 above. > 5) move to fifth line, and if it ends in "no" discard it, but if it > ends in "yes" repeat 1, 2, 3, and 4 above, and so on. > > The goal is to get a ratio of 1 to 2 "yes" to "no" lines from a file > in such a way that keeps the order of the lines in output. An > abstraction away from this so that any ratio of "yes" to "no" lines > could be printed while keeping the order of the original lines would > be great. > > Please show us what code you have written, and in what way it fails > to meet your expectations. > > Your specification is IMHO a nice piece of pseudocode which could > translate to Python fairly easily! > > What do we do at steps 4ff if the line does not end in "yes" or "no"? > > If you have not written any code make a stab at it. You could start > by asking "how in Python does one": > open a file? > iterate (loop)? > get the next line from a file? > test for equality? > examine the end of a string? > > > -- > Bob Gailer > Chapel Hill NC > 919-636-4239 > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Apr 25 21:00:11 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 25 Apr 2009 20:00:11 +0100 Subject: [Tutor] # what do I do now to kill the the Windows program which has finished its work? References: <49F32DA4.8080403@shaw.ca> Message-ID: "Ian Campbell" wrote > os.startfile("c:\\buy.exe" , 'OPEN') # THIS WORKS and returns but > does not return the taskID Take a look at the subprocess module. > # what do I do now to kill the the Windows program which has finished > its work? This is trickier, if the program does not stop itself when it finishes then you need to look at timers and threads and how to programmatically terminate a program in Windows. Usually the command line program will terminate itself when done but if it doesn't you can use time.sleep() as a delay. You will need to launch the program in a thread. You can then try killing the thread which may kill the program, or if not, use the Windows API to kill the program (gets messy). This is easier on Unix in my experience! HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From chinmaya at gmail.com Sat Apr 25 21:36:23 2009 From: chinmaya at gmail.com (chinmaya) Date: Sat, 25 Apr 2009 13:36:23 -0600 Subject: [Tutor] python books In-Reply-To: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> References: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> Message-ID: <3c722d570904251236v2c410f17p57d7185309f18a77@mail.gmail.com> On Mon, Apr 13, 2009 at 11:07 PM, sudhanshu gautam wrote: > I am new in python , so need a good books , previously read python Bible > and swaroop but not satisfied . > > > so tell me good books in pdf format those contents good problems also > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > I would say start with python tutorial, its nice decent starting material. There is no better way to learn language than to practice it as you read. Most of the tutorials out there are not written for 3.0, so you may want to install 2.6. I also recommend Dive Into python, its very beginner friendly, but remember it does not cover all (not all major) libraries never-the-less its one of the best beginner tutorial. Also install ipython its very powerful. And once you learn the interface its very easy to find documentation and library references. Also you can look at 100s of python videos in showmedo.com -- chinmaya sn -------------- next part -------------- An HTML attachment was scrubbed... URL: From contactdayo at gmail.com Sat Apr 25 21:45:11 2009 From: contactdayo at gmail.com (Dayo Adewunmi) Date: Sat, 25 Apr 2009 20:45:11 +0100 Subject: [Tutor] python books In-Reply-To: <3c722d570904251236v2c410f17p57d7185309f18a77@mail.gmail.com> References: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> <3c722d570904251236v2c410f17p57d7185309f18a77@mail.gmail.com> Message-ID: <49F36847.6070206@gmail.com> chinmaya wrote: > > > On Mon, Apr 13, 2009 at 11:07 PM, sudhanshu gautam > > wrote: > > I am new in python , so need a good books , previously read python > Bible and swaroop but not satisfied . > > > so tell me good books in pdf format those contents good problems also > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > > > I would say start with python tutorial, its nice decent starting > material. > There is no better way to learn language than to practice it as you read. > Most of the tutorials out there are not written for 3.0, so you may want > to install 2.6. > I also recommend Dive Into python, its very beginner friendly, but > remember > it does not cover all (not all major) libraries never-the-less its one > of the > best beginner tutorial. > > Also install ipython its very powerful. And once you learn the interface > its very easy to find documentation and library references. > > Also you can look at 100s of python videos in showmedo.com > > > > -- > chinmaya sn > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > I'm currently reading Think Python http://www.greenteapress.com/thinkpython/thinkpython.html Regards Dayo ----------- From ian-campbell at shaw.ca Sat Apr 25 21:49:57 2009 From: ian-campbell at shaw.ca (Ian Campbell) Date: Sat, 25 Apr 2009 12:49:57 -0700 Subject: [Tutor] I may have solved my problem with killing tasks in Windows Message-ID: <49F36965.70906@shaw.ca> I am trying to use this so maybe no one has to reply... sorry to inconvenience anyone. I got it from Google but did not copy his name and cannot give my thanks to original author, sorry. import subprocess from win32event import WaitForSingleObject, WAIT_TIMEOUT from win32api import TerminateProcess def execute_and_wait(args, timeout=None): """Execute a command and wait until termination. If timeout elapses and still not finished, kill it. args: list containing program and arguments, or a single string. timeout: maximum time to wait, in ms, or None. """ p = subprocess.Popen(args) if timeout: ret = WaitForSingleObject(p._handle, timeout) if ret==WAIT_TIMEOUT: TerminateProcess(p._handle, 1) return None return p.wait() ret = execute_and_wait(["notepad", "c:\\BuyLiveHit.ahk"], 5000) print "ret=",ret From emile at fenx.com Sat Apr 25 21:56:33 2009 From: emile at fenx.com (Emile van Sebille) Date: Sat, 25 Apr 2009 12:56:33 -0700 Subject: [Tutor] # what do I do now to kill the the Windows program which has finished its work? In-Reply-To: References: <49F32DA4.8080403@shaw.ca> Message-ID: Alan Gauld wrote: > then try killing the thread which may kill the program, or if not, use the > Windows API to kill the program (gets messy). This is easier on Unix > in my experience! Sysinternals created pstools for windows that I generally use for this kind of thing, particularly when it gets messy. :) Emile From alan.gauld at btinternet.com Sat Apr 25 23:28:16 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 25 Apr 2009 22:28:16 +0100 Subject: [Tutor] I may have solved my problem with killing tasks in Windows References: <49F36965.70906@shaw.ca> Message-ID: "Ian Campbell" wrote >I am trying to use this so maybe no one has to reply... sorry to > inconvenience anyone. No inconvenience and I'm glad you posted the solution you found. That is pretty much what I had in mind but I didn't know you could get the _handle from the return value in Popen! That makes it much easier. I would still tend to run the execute and wait from a separate thread just in case... > def execute_and_wait(args, timeout=None): > p = subprocess.Popen(args) > if timeout: > ret = WaitForSingleObject(p._handle, timeout) > if ret==WAIT_TIMEOUT: > TerminateProcess(p._handle, 1) > return None > return p.wait() -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From bgailer at gmail.com Sun Apr 26 03:49:25 2009 From: bgailer at gmail.com (bob gailer) Date: Sat, 25 Apr 2009 21:49:25 -0400 Subject: [Tutor] Working with lines from file and printing to another keeping sequential order In-Reply-To: References: <49F31F07.8060702@gmail.com> Message-ID: <49F3BDA5.5040500@gmail.com> Dan Liang wrote: > Hi Bob and tutors, > > Thanks Bob for your response! currently I have the current code, but > it does not work: > > [snip] Thank you for code, sample data and more complete specs. > Lines in the file look like following: > > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > word1 word2 word3 word4 yes > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 no > word1 word2 word3 word4 yes > > Any suggestions are appreciated. Also, I feel that my code is not the > best way of solving the problem even if the problem of list indices is > solved. Is my guess right? Yes indeed. In actuality you need only to examine one line at a time and keep track of the "state" of things. In computer theory we refer to a "state machine". There is a set of states. The program is always in exactly one of these states. In each state there are actions to take and a (conditional) moves to other states. the program starts in, let's say, state 0: state 0: open file, goto state 1. state 1: read next line, at eof goto state 6; if it ends in yes, goto state 2 else stay in state 1. state 2: read next line, at eof goto state 6; if it ends in yes stay in state 2 else goto state 3. state 3: read next line, at eof goto state 6. if it ends in yes goto state 2 else print line and goto state 4. state 4: read next line, at eof goto state 6. if it ends in yes goto state 2 else print line and goto state 5. state 5: read next line, at eof goto state 6. if it ends in yes goto state 2 else stay in state 5. state 6: close file and terminate That can be implemented in Python as follows. This is not the prettiest or most compact program, but is it a straightforward implementation of a state machine. state = 0 # initial state data = open('test.txt') while True: # iterate over lines if state == 0: # look for first line ending in 'yes' line = data.read().strip() if line: if line.endswith('yes'): state = 1 else state = 6 elif state == 1: # code for state i # more states elif state == 6: data.close() break To simplify things I'd create a function to read and strip a line and raise an exception at end of file. -- Bob Gailer Chapel Hill NC 919-636-4239 From kent37 at tds.net Sun Apr 26 03:56:48 2009 From: kent37 at tds.net (Kent Johnson) Date: Sat, 25 Apr 2009 21:56:48 -0400 Subject: [Tutor] # what do I do now to kill the the Windows program which has finished its work? In-Reply-To: <49F32DA4.8080403@shaw.ca> References: <49F32DA4.8080403@shaw.ca> Message-ID: <1c2a2c590904251856u21ff2f5dkc0b483df4c7b2f5c@mail.gmail.com> On Sat, Apr 25, 2009 at 11:35 AM, Ian Campbell wrote: > I am converting a Windows ProComm script into Python and ?need help. > > ; ?previous Procomm ?script here > > run "c:\buy.exe" taskId ? ? ? ? ? ? ? ?; this runs the c:\buy.exe program > and assigns the ?task identification number to the integer variable called > "taskId" > pause 1 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?; wait one second > while taskexists(taskId) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? exitTask(taskId) > ? ? ? ? ? ; kills the task > endwhile > > ; we carry on.... the ProComm ? ?code to completion > > > > How do I do the same in Python? I think something like this, in Python 2.6 anyway: from subprocess import Popen import time p = Popen("c:\\buy.exe") # Note double slashes for backslash time.sleep(1) if p.poll() is None: p.terminate() Kent From zebra05 at gmail.com Sun Apr 26 13:14:00 2009 From: zebra05 at gmail.com (OkaMthembo) Date: Sun, 26 Apr 2009 13:14:00 +0200 Subject: [Tutor] python books In-Reply-To: <49F36847.6070206@gmail.com> References: <6f9dbdf40904132207i31743d92i3e33fdd7e184c058@mail.gmail.com> <3c722d570904251236v2c410f17p57d7185309f18a77@mail.gmail.com> <49F36847.6070206@gmail.com> Message-ID: I'd recommend Core Python Programming by Wesley Chun.. On Sat, Apr 25, 2009 at 9:45 PM, Dayo Adewunmi wrote: > chinmaya wrote: > >> >> >> On Mon, Apr 13, 2009 at 11:07 PM, sudhanshu gautam < >> sudhanshu9252 at gmail.com > wrote: >> >> I am new in python , so need a good books , previously read python >> Bible and swaroop but not satisfied . >> >> >> so tell me good books in pdf format those contents good problems also >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> >> >> >> I would say start with python tutorial, its nice decent starting material. >> There is no better way to learn language than to practice it as you read. >> Most of the tutorials out there are not written for 3.0, so you may want >> to install 2.6. >> I also recommend Dive Into python, its very beginner friendly, but >> remember >> it does not cover all (not all major) libraries never-the-less its one of >> the >> best beginner tutorial. >> >> Also install ipython its very powerful. And once you learn the interface >> its very easy to find documentation and library references. >> >> Also you can look at 100s of python videos in showmedo.com < >> http://showmedo.com> >> >> >> -- >> chinmaya sn >> ------------------------------------------------------------------------ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > > I'm currently reading Think Python > http://www.greenteapress.com/thinkpython/thinkpython.html > > Regards > > Dayo > ----------- > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -- Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Sun Apr 26 16:55:36 2009 From: kent37 at tds.net (Kent Johnson) Date: Sun, 26 Apr 2009 10:55:36 -0400 Subject: [Tutor] Working with lines from file and printing to another keeping sequential order In-Reply-To: References: <49F31F07.8060702@gmail.com> Message-ID: <1c2a2c590904260755y8437e47g55a87d7b0885a9cd@mail.gmail.com> On Sat, Apr 25, 2009 at 2:11 PM, Dan Liang wrote: > Hi Bob and tutors, > > Thanks Bob for your response! currently I have the current code, but it does > not work: > > ListLines= [] > for line in open('test.txt'): > ??? line = line.rstrip() > ??? ListLines.append(line) This could be written with a list comprehension: ListLines = [ line.rstrip() for line in open('test.txt') ] > > for i in range(len(ListLines)): > > ??? if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and > ListLines[i+1].endswith("no"): > ??? ??? print ListLines[i], ListLines[i+1], ListLines[i+2] > ??? elif ListLines[i].endswith("yes") and ListLines[i+1].endswith("no"): > ??? ??? print ListLines[i], ListLines[i+1] > ??????? elif ListLines[i].endswith("yes"): > ??? ??? print ListLines[i] > ??? elif ListLines[i].endswith("no"): > ??? ??? continue > ??? else: > ??? ??? break You only need to test for ListLines[i].endswith('yes') once. Then you could use a loop to test for lines ending with 'no'. for i in range(len(ListLines)): if not ListLines[i].endswith('yes'): continue for offset in (1, 2): if i+offset < len(ListLines) and ListLines[i+offset].endswith('no'): print ListLines[i+offset] You could adapt the above for a variable number of 'no' lines with something like for offset in range(1, maxNo+1): > I get the following error: > Traceback (most recent call last): > ? File "test.py", line 18, in > ??? if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and > ListLines[i+1].endswith("no"): > IndexError: list index out of range That is because you have a 'yes' line at the end of the file so the check for 'no' tries to read past the end of ListLines. In my code the test for i+offset < len(ListLines) will prevent that exception. Kent From davholla2002 at yahoo.co.uk Sun Apr 26 20:03:41 2009 From: davholla2002 at yahoo.co.uk (David Holland) Date: Sun, 26 Apr 2009 18:03:41 +0000 (GMT) Subject: [Tutor] Lists in a file Message-ID: <16826.50317.qm@web25602.mail.ukl.yahoo.com> Hi, I am trying to create a program where I open a file full of lists and process them. However when the program does not recognize the lists as lists. Here is the relevant code :- def open_filedef(): ??? text_file =open ("voteinp.txt","r") ??? lines = text_file.readlines() ??? ??? ??? for line in lines: ??????? print line ??????? print line[0] ????????? ??? text_file.close() And an example of the type of text :- ['a','b','c'] ['e','d','f'] Any ideas? Thanks in advance David Holland -------------- next part -------------- An HTML attachment was scrubbed... URL: From bermanrl at cfl.rr.com Sun Apr 26 20:18:07 2009 From: bermanrl at cfl.rr.com (Robert Berman) Date: Sun, 26 Apr 2009 14:18:07 -0400 Subject: [Tutor] Lists in a file In-Reply-To: <16826.50317.qm@web25602.mail.ukl.yahoo.com> References: <16826.50317.qm@web25602.mail.ukl.yahoo.com> Message-ID: <49F4A55F.4000907@cfl.rr.com> David, You are processing a text file. It is your job to treat it as a file comprised of lists. I think what you are saying is that each line in the text file is a list. In that case for line in fileobject: listline = list(line) Now, listline is a list of the text items in line. Hope this helps. Robert Berman David Holland wrote: > Hi, > > I am trying to create a program where I open a file full of lists and > process them. > However when the program does not recognize the lists as lists. > Here is the relevant code :- > def open_filedef(): > text_file =open ("voteinp.txt","r") > lines = text_file.readlines() > > > for line in lines: > print line > print line[0] > > text_file.close() > > And an example of the type of text :- > > ['a','b','c'] > ['e','d','f'] > > Any ideas? > > > Thanks in advance > > > David Holland > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From kent37 at tds.net Sun Apr 26 21:04:44 2009 From: kent37 at tds.net (Kent Johnson) Date: Sun, 26 Apr 2009 15:04:44 -0400 Subject: [Tutor] Lists in a file In-Reply-To: <49F4A55F.4000907@cfl.rr.com> References: <16826.50317.qm@web25602.mail.ukl.yahoo.com> <49F4A55F.4000907@cfl.rr.com> Message-ID: <1c2a2c590904261204g518c938bgc2f6dc1002df584c@mail.gmail.com> On Sun, Apr 26, 2009 at 2:18 PM, Robert Berman wrote: > David, > > You are processing a text file. It is your job to treat it as a file > comprised of lists. I think what you are saying is that each line in the > text file is a list. In that case > > for line in fileobject: > ? listline = list(line) > > Now, listline is a list of the text items in line. listline will be a list of all the characters in the line, which is not what the OP wants. If the list will just contain quoted strings that don't themselves contain commas you can do something ad hoc: In [2]: s = "['a','b','c']" In [5]: [ str(item[1:-1]) for item in s[1:-1].split(',') ] Out[5]: ['a', 'b', 'c'] In Python 3 you can use ast.literal_eval(). >>> import ast >>> s = "['a','b','c']" >>> ast.literal_eval(s) ['a', 'b', 'c'] You can use these recipes: http://code.activestate.com/recipes/511473/ http://code.activestate.com/recipes/364469/ Kent From denis.spir at free.fr Sun Apr 26 21:22:52 2009 From: denis.spir at free.fr (spir) Date: Sun, 26 Apr 2009 21:22:52 +0200 Subject: [Tutor] Lists in a file In-Reply-To: <16826.50317.qm@web25602.mail.ukl.yahoo.com> References: <16826.50317.qm@web25602.mail.ukl.yahoo.com> Message-ID: <20090426212252.77d9c05a@o> Le Sun, 26 Apr 2009 18:03:41 +0000 (GMT), David Holland s'exprima ainsi: > Hi, > > I am trying to create a program where I open a file full of lists and > process them. However when the program does not recognize the lists as > lists. Here is the relevant code :- > def open_filedef(): > ??? text_file =open ("voteinp.txt","r") > ??? lines = text_file.readlines() > ??? > ??? > ??? for line in lines: > ??????? print line > ??????? print line[0] > ????????? > ??? text_file.close() > > And an example of the type of text :- > > ['a','b','c'] > ['e','d','f'] > > Any ideas? You're making a confusion between text items and what could be called "running program items". And also you're assuming that python reads your mind -- a feature not yet implemented but in project for py4000. More seriously, a list (or any "running program item") is something that is produced in memory when python runs your code, like when it runs l = [1,2,3] after having parsed it. What you're giving it now is *text*. Precisely text read from a file. So it regards as text. It could hold "I am trying to create a program where...". Right? Python has no way to guess that *you* consider this text as valid python code. You must tell it. Also, you do not ask python to run this. So why should it do so? Right? If you give python a text, it regards it as text (string): >>> t = "[1,2,3] ; [7,8,9]" >>> t '[1,2,3] ; [7,8,9]' >>> type(t) A text is a sequence of characters, so that if you split it into a list, you get characters; if you ask for the zerost char, you get it: >>> list(t) ['[', '1', ',', '2', ',', '3', ']', ' ', ';', ' ', '[', '7', ',', '8', ',', '9', ']'] >>> t[0] '[' If you want python to consider the text as if it were code, you must tell it so: >>> listtext1 = t[0:7] >>> listtext1 '[1,2,3]' >>> list1 = eval(listtext1) >>> list1 [1, 2, 3] >>> list1[1] 2 Now, as you see, I have a list object called list1. But this is considered bad practice for numerous good reasons. Actually, what you were doing is asking python to read a file that you consider as a snippet of program. So why don't you write it directly in the same file? Or for organisation purpose you want an external module to import? Denis ------ la vita e estrany From contactdayo at gmail.com Sun Apr 26 23:35:36 2009 From: contactdayo at gmail.com (Dayo Adewunmi) Date: Sun, 26 Apr 2009 22:35:36 +0100 Subject: [Tutor] How to run a .py file or load a module? Message-ID: <49F4D3A8.2060407@gmail.com> I'm looking at recursion in "Think Python", and this is the bit of code: #!/usr/bin/env python def countdown(n): if n <= 0: print 'Blastoff!' else: print n countdown(n-1) I've typed that in vim and saved as countdown.py, but I'm not sure how to run it. I've had other python files, where the triggering function didn't take any arguments, so I would just put a `foo()` at the end of the .py file. However with this particular function that requires an argument, I'm not sure how to run it. I've had to type it out in the python prompt and then call the function with an argument. That works, naturally. I've also tried this: >>>import countdown >>>countdown(10) but this is the error I get: Traceback (most recent call last): File "", line 1, in NameError: name 'countdown' is not defined How can I a) Open my shell, and do something like: $ python countdown.py but have it take an argument and pass it to the function, and execute. b) Import the function in the interactive interpreter, and call it like so: countdown(10) without getting the abovementioned error. Thanks. Dayo From dorseye at gmail.com Mon Apr 27 00:06:49 2009 From: dorseye at gmail.com (Eric Dorsey) Date: Sun, 26 Apr 2009 16:06:49 -0600 Subject: [Tutor] How to run a .py file or load a module? In-Reply-To: <49F4D3A8.2060407@gmail.com> References: <49F4D3A8.2060407@gmail.com> Message-ID: Dayo, I modified the code a little bit to make things work the way I think you meant it to work(hopefully), and I changed the name of the function so that its' not the same name as the python file itself, but hopefully this answers your questions. Here is my countdown.py def launchme(n): while n > 0: print n n -= 1 else: print 'Blastoff!' #uncomment to run from the shell #launchme(7) So, assuming we're running the interpreter from the same folder that countdown.py is in. You have to call module.function(parameter) >>> import countdown >>> countdown.launchme(4) 4 3 2 1 Blastoff! >>> If we uncomment the "#launchme(7)" line, and run it from the shell: $ python countdown.py 7 6 5 4 3 2 1 Blastoff! On Sun, Apr 26, 2009 at 3:35 PM, Dayo Adewunmi wrote: > I'm looking at recursion in "Think Python", and this is the bit of code: > > > #!/usr/bin/env python > > def countdown(n): > if n <= 0: > print 'Blastoff!' > else: print n > countdown(n-1) > > > I've typed that in vim and saved as countdown.py, but I'm not sure how to > run it. I've had other > python files, where the triggering function didn't take any arguments, > so I would just put a `foo()` at the end of the .py file. > > However with this particular function that requires an argument, I'm not > sure how to run it. I've had to type it out in the python prompt and then > call > the function with an argument. That works, naturally. > > I've also tried this: > > >>>import countdown > >>>countdown(10) > > but this is the error I get: > > Traceback (most recent call last): > File "", line 1, in > NameError: name 'countdown' is not defined > > How can I > > a) Open my shell, and do something like: $ python countdown.py but have > it take an argument and pass it to the function, and execute. > > b) Import the function in the interactive interpreter, and call it like so: > > countdown(10) > > without getting the abovementioned error. > Thanks. > > Dayo > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Mon Apr 27 00:07:19 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Mon, 27 Apr 2009 00:07:19 +0200 Subject: [Tutor] How to run a .py file or load a module? In-Reply-To: <49F4D3A8.2060407@gmail.com> References: <49F4D3A8.2060407@gmail.com> Message-ID: 2009/4/26 Dayo Adewunmi : > I'm looking at recursion in "Think Python", and this is the bit of code: > > #!/usr/bin/env python > > def countdown(n): > ? ? ? if n <= 0: > ? ? ? ? ? ? ? print 'Blastoff!' > ? ? ? else: ? ? ? ? ? ? ? ? print n > ? ? ? ? ? ? ? countdown(n-1) > > > I've typed that in vim and saved as countdown.py, but I'm not sure how to > run it. > > However with this particular function that requires an argument, I'm not > sure how to run it. > > I've had to type it out in the python prompt and then > call > the function with an argument. That works, naturally. > > I've also tried this: > > ? ? ? >>>import countdown > ? ? ? >>>countdown(10) When you import it lile this the function countdown is part of the module countdown. So you call it like countdown.countdown(10). Or import it like "from countdown import countdown" and then your example will work. > but this is the error I get: > > ? ? ? Traceback (most recent call last): > ? ? ? ? File "", line 1, in > ? ? ? NameError: name 'countdown' is not defined > > How can I > > a) Open my shell, and do something like: $ python countdown.py ? but have it > take an argument and pass it to the function, and execute. Look at sys.argv which returns a list with the first value being the script name and the second are the command line argument(s). http://docs.python.org/library/sys.html > b) Import the function in the interactive interpreter, and call it like so: > > ? ? ? countdown(10) > > without getting the abovementioned error. See above. The script would then look like: #!/usr/bin/env python import sys times = int(sys.argv[1]) # The argument given on the command line def countdown(n): if n <=0: print 'Blast off!' else: countdown(n-1) countdown(times) Greets Sander From norman at khine.net Mon Apr 27 03:02:41 2009 From: norman at khine.net (Norman Khine) Date: Mon, 27 Apr 2009 03:02:41 +0200 Subject: [Tutor] How to run a .py file or load a module? In-Reply-To: References: <49F4D3A8.2060407@gmail.com> Message-ID: <9c2c8ffb0904261802u1ccb11d7tb7c6671b9f6a8aa5@mail.gmail.com> On Mon, Apr 27, 2009 at 12:07 AM, Sander Sweers wrote: > 2009/4/26 Dayo Adewunmi : >> I'm looking at recursion in "Think Python", and this is the bit of code: >> >> #!/usr/bin/env python >> >> def countdown(n): >> ? ? ? if n <= 0: >> ? ? ? ? ? ? ? print 'Blastoff!' >> ? ? ? else: ? ? ? ? ? ? ? ? print n >> ? ? ? ? ? ? ? countdown(n-1) >> >> >> I've typed that in vim and saved as countdown.py, but I'm not sure how to >> run it. >> >> However with this particular function that requires an argument, I'm not >> sure how to run it. >> >> I've had to type it out in the python prompt and then >> call >> the function with an argument. That works, naturally. >> >> I've also tried this: >> >> ? ? ? >>>import countdown >> ? ? ? >>>countdown(10) > > When you import it lile this the function countdown is part of the > module countdown. So you call it like countdown.countdown(10). Or > import it like "from countdown import countdown" and then your example > will work. > >> but this is the error I get: >> >> ? ? ? Traceback (most recent call last): >> ? ? ? ? File "", line 1, in >> ? ? ? NameError: name 'countdown' is not defined >> >> How can I >> >> a) Open my shell, and do something like: $ python countdown.py ? but have it >> take an argument and pass it to the function, and execute. > > Look at sys.argv which returns a list with the first value being the > script name and the second are the command line argument(s). > http://docs.python.org/library/sys.html > >> b) Import the function in the interactive interpreter, and call it like so: >> >> ? ? ? countdown(10) >> >> without getting the abovementioned error. > > See above. > > The script would then look like: > > #!/usr/bin/env python > > import sys > > times = int(sys.argv[1]) # The argument given on the command line > > def countdown(n): > ? ?if n <=0: > ? ? ? ?print 'Blast off!' > ? ?else: > ? ? ? ?countdown(n-1) > > countdown(times) Don't forget to add: if __name__ == '__main__': launchme(times) > > Greets > Sander > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From david at abbottdavid.com Mon Apr 27 03:33:54 2009 From: david at abbottdavid.com (David) Date: Sun, 26 Apr 2009 21:33:54 -0400 Subject: [Tutor] How to run a .py file or load a module? In-Reply-To: <9c2c8ffb0904261802u1ccb11d7tb7c6671b9f6a8aa5@mail.gmail.com> References: <49F4D3A8.2060407@gmail.com> <9c2c8ffb0904261802u1ccb11d7tb7c6671b9f6a8aa5@mail.gmail.com> Message-ID: <49F50B82.8090206@abbottdavid.com> Norman Khine wrote: > On Mon, Apr 27, 2009 at 12:07 AM, Sander Sweers wrote: Here is another one for fun, you run it like python countdown.py 10 #!/usr/bin/env python import sys from time import sleep times = int(sys.argv[1]) # The argument given on the command line def countdown(n): try: while n != 1: n = n-1 print n sleep(1) finally: print 'Blast Off!' countdown(times) From a.t.hofkamp at tue.nl Mon Apr 27 08:46:35 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Mon, 27 Apr 2009 08:46:35 +0200 Subject: [Tutor] Threading... In-Reply-To: References: Message-ID: <49F554CB.3060106@tue.nl> Spencer Parker wrote: > Would the best route for this be > threading? or is there another way to go about it? At #python, you'd quickly get redirected away from threading (you are making a IO-bound app, so threading doesn't buy you anything in performance and gives a big headache in data protection), towards Twisted, a Python framework designed for programming asynchronous (network) applications (ie IO-bound applications with many IO connections, like yours). I do think that ultimately, you are better of with Twisted than with threading (until some Pythoneers extend the language with concurrency primitives that don't give you data protection problems as a bonus). However, Twisted is also quite twisted in nature. It takes a some time before you understand how to program in it. Sincerely, Albert From lie.1296 at gmail.com Mon Apr 27 09:09:14 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Mon, 27 Apr 2009 17:09:14 +1000 Subject: [Tutor] output formatting In-Reply-To: <20090425110247.8830724i9evjiqkg@web.mail.umich.edu> References: <20090424235742.63985oy1agmiasso@web.mail.umich.edu> <333efb450904250439l324377c3labf193d4c489c572@mail.gmail.com> <20090425110247.8830724i9evjiqkg@web.mail.umich.edu> Message-ID: Matt Domeier wrote: > Hi Wayne, > > Yes, that should work perfectly, but it is in writing files that I'm > having the problem. I'm still very new to python, so I only know to use > something like filename.write(str(listname)) to write my list to a file.. > Is there a straightforward method of combining the ease of the print > function setup that you suggested with write? > Or could I somehow use the > print function itself to write to the file (rather than just output to > my shell/prompt)? First note, print is a statement in python2.x and a function in python3.x. In both case it is possible to redirect print's output to another file. Simply: python2.x: print >> f, "Hello World" python3.x: print("Hello World", file=f) also for both versions: import sys sys.stdout = f print("Hello World") sys.stdout = sys.__stdout__ # return to normal The universal stdout redirection (3rd method) was discouraged because it could mess up things if there are two or more routines that tries to override sys.stdout or in multithreading situation (it is there mainly for hackish quick and dirty scripts). The python2.x's redirection method looks ugly (for me). The python3.x's syntax is better, but is a bit verbose. In any case, it is usually more recommended to use f.write() or f.writelines() From denis.spir at free.fr Mon Apr 27 10:15:23 2009 From: denis.spir at free.fr (spir) Date: Mon, 27 Apr 2009 10:15:23 +0200 Subject: [Tutor] How to run a .py file or load a module? In-Reply-To: <49F4D3A8.2060407@gmail.com> References: <49F4D3A8.2060407@gmail.com> Message-ID: <20090427101523.5fc09539@o> Le Sun, 26 Apr 2009 22:35:36 +0100, Dayo Adewunmi s'exprima ainsi: > How can I > > a) Open my shell, and do something like: $ python countdown.py > but have it take an argument and pass it to the function, and execute. When your code is (nicely) organised as a set of funcs or class definitions, you also need a "laucher" usually called "main()". Otherwise python only parses and records the definitions into live objects that wait for someone to tell them what they're supposed to do. I'll stick first at processes without any parameter, like if your func would always countdown from 10. There are several use patterns: (1) Program launched from command line. Just add a call to your func: countdown(10) (2) Module imported from other prog Nothing to add to your module. Instead, the importing code needs to hold: import countdown # the module (file) ... countdown.countdown(n) # the func itself or from countdown import countdown # the func, directly ... countdown(n) (3) Both You need to differenciate between launching and importing. Python provides a rather esoteric idiom for that: if __name__ == "__main__": countdown(10) The trick is that when a prog is launched directly (as opposed to imported), it silently gets a '__name__' attribute that is automatically set to "__main__". So that the one-line block above will only run when the prog is launched, like in case (1). While nothing will happen when the module is imported -- instead the importing code will have the countdown func available under name 'countdown' as expected, like in case (2). Clear? > b) Import the function in the interactive interpreter, and call it like so: > > countdown(10) > > without getting the abovementioned error. In the case of an import, as your func definition has the proper parameter, you have nothing to change. While for a launch from command-line, you need to get the parameter given by the user. But how? Python provides a way to read the command-line arguments under an attribute called 'argv' of the 'sys' module. argv is a list which zerost item is the name of the file. For instance if called python countdown.py 9 argv holds: ['countdown.py', '9'] Note that both are strings. Then you can catch and use the needed parameter, e.g. from time import sleep as wait from sys import argv as user_args def countdown(n=10): if n <= 0: print 'Blastoff!' else: wait(0.333) print n countdown(n-1) def launch(): if len(user_args) == 1: countdown() else: n = int(user_args[1]) countdown(n) if __name__ == "__main__": launch() (You can indeed put the content of launch() in the if block. But I find it clearer that way, and it happens to be a common practice.) Denis ------ la vita e estrany From kent37 at tds.net Mon Apr 27 12:39:42 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 27 Apr 2009 06:39:42 -0400 Subject: [Tutor] Threading... In-Reply-To: <49F554CB.3060106@tue.nl> References: <49F554CB.3060106@tue.nl> Message-ID: <1c2a2c590904270339y4d3a34f1nc28fb77e88a453b7@mail.gmail.com> On Mon, Apr 27, 2009 at 2:46 AM, A.T.Hofkamp wrote: > you are making > a IO-bound app, so threading doesn't buy you anything in performance and > gives a big headache in data protection Please explain. Presumably the single-threaded app is not IO-bound. Adding threads can push it to the point of being IO-bound. The requirements for communicating between threads in this app are modest and not likely to cause a big headache. Kent From a.t.hofkamp at tue.nl Mon Apr 27 15:29:15 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Mon, 27 Apr 2009 15:29:15 +0200 Subject: [Tutor] Threading... In-Reply-To: <1c2a2c590904270339y4d3a34f1nc28fb77e88a453b7@mail.gmail.com> References: <49F554CB.3060106@tue.nl> <1c2a2c590904270339y4d3a34f1nc28fb77e88a453b7@mail.gmail.com> Message-ID: <49F5B32B.1010809@tue.nl> Kent Johnson wrote: > On Mon, Apr 27, 2009 at 2:46 AM, A.T.Hofkamp wrote: >> you are making >> a IO-bound app, so threading doesn't buy you anything in performance and >> gives a big headache in data protection > > Please explain. Presumably the single-threaded app is not IO-bound. > Adding threads can push it to the point of being IO-bound. The Euh, what? Suppose I have a CPU-intensive task. Now by adding threads (ie more CPU-intensive tasks running at the same time) I can make it an IO-bound application? Wow. Let's call seti at home and climateprediction.net . As far as I know, doing DB queries is an IO-bound process, since you must make a connection to the remote DB server, and send data back and forth to the server over the network (which is ssssllllooooowwww compared to a calculation). With threading, you will have more network connections that you are waiting for. W.r.t. performance you don't gain anything, since the CPU is already mostly idle. > requirements for communicating between threads in this app are modest > and not likely to cause a big headache. With respect to the data problem, I don't know what the OP wants, but maybe he wants to run different tests at different times (ie first 1 minute test A, then 1 minute test B etc) or different mixes of queries. He may also want to collect performance results (avg x queries/second). Such data is shared between the threads, so it must be protected to prevent it from getting corrupted. Maybe it is modest in requirements, but you must do it none the less. Also note that the protection itself may also become a bottle neck in the program (you must constantly claim and release locks which adds overhead even when there is no other thread waiting). With Twisted, you can have at least the same number of IO connections as with threading, while you don't get the data protection problems and overhead for the simple reason that it is still a single-threaded program. Maybe threading is the correct solution here. However the OP also asked for "...or is there another way to go about it?". Twisted is definitely another way to go about it, and nobody mentioned it in previous posts, so I thought it correct to mention its existence here. Sincerely, Albert From kent37 at tds.net Mon Apr 27 18:46:15 2009 From: kent37 at tds.net (Kent Johnson) Date: Mon, 27 Apr 2009 12:46:15 -0400 Subject: [Tutor] Threading... In-Reply-To: <49F5B32B.1010809@tue.nl> References: <49F554CB.3060106@tue.nl> <1c2a2c590904270339y4d3a34f1nc28fb77e88a453b7@mail.gmail.com> <49F5B32B.1010809@tue.nl> Message-ID: <1c2a2c590904270946p420cd3efxb5cfc280634b7820@mail.gmail.com> On Mon, Apr 27, 2009 at 9:29 AM, A.T.Hofkamp wrote: > Kent Johnson wrote: >> >> On Mon, Apr 27, 2009 at 2:46 AM, A.T.Hofkamp wrote: >>> >>> you are making >>> a IO-bound app, so threading doesn't buy you anything in performance and >>> gives a big headache in data protection Perhaps I misinterpreted this. Do you mean "threading doesn't buy you anything in performance" vs Twisted or vs his current solution? Vs Twisted, I would agree. Vs single-threaded, I disagree, especially given the OP's stated goal of opening a lot of concurrent connections. >> Please explain. Presumably the single-threaded app is not IO-bound. >> Adding threads can push it to the point of being IO-bound. The > As far as I know, doing DB queries is an IO-bound process, since you must > make a connection to the remote DB server, and send data back and forth to > the server over the network (which is ssssllllooooowwww compared to a > calculation). >From the point of view of the client, there is likely to be a fair amount of dead time, waiting to establish a network connection, waiting to establish a database connection, waiting for the db to do it's thing. So my assumption is that the original, single-connection, single-threaded client is neither IO- nor CPU-bound, it is spending at least some time just waiting. In this scenario, both threads and Twisted can help to increase the load on the remote server by more fully utilizing the available IO bandwidth. That is what I meant by using threads to create an IO-bound app. > With threading, you will have more network connections that you are waiting > for. W.r.t. performance you don't gain anything, since the CPU is already > mostly idle. You will get more performance if the threaded app is able to use the dead time in the IO. ISTM this directly addresses the OP. His stated goal is "to throw a ton of open connections at" the DB. Certainly he can do that using threads. >> requirements for communicating between threads in this app are modest > With Twisted, you can have at least the same number of IO connections as > with threading, while you don't get the data protection problems and > overhead for the simple reason that it is still a single-threaded program. > > > > Maybe threading is the correct solution here. However the OP also asked for > "...or is there another way to go about it?". > Twisted is definitely another way to go about it, and nobody mentioned it in > previous posts, so I thought it correct to mention its existence here. I don't dispute that at all. Kent From sudhanshu9252 at gmail.com Mon Apr 27 19:08:30 2009 From: sudhanshu9252 at gmail.com (sudhanshu gautam) Date: Mon, 27 Apr 2009 22:38:30 +0530 Subject: [Tutor] How many types of the constructor Message-ID: <6f9dbdf40904271008w1708a72dtc63cf88e3ca81353@mail.gmail.com> when we work on the oops in the python then we have to pass the first parameter as a self then the value of the other parameters . for example: class Student: def __init__(self,name): self.name=name print 'The name is',self.name a=student('sudhanshu') a is an object of the student class so __init__ is a constructor in which we do not need to call the function separately . Now If I placed the name of the constructor rather than the __init__ __baba___ so will it also work as a constructor or constructor has specified already if yes then give me list of them ragards sudhanshu -------------- next part -------------- An HTML attachment was scrubbed... URL: From noufal at nibrahim.net.in Mon Apr 27 19:23:24 2009 From: noufal at nibrahim.net.in (Noufal Ibrahim) Date: Mon, 27 Apr 2009 22:53:24 +0530 Subject: [Tutor] How many types of the constructor In-Reply-To: <6f9dbdf40904271008w1708a72dtc63cf88e3ca81353@mail.gmail.com> References: <6f9dbdf40904271008w1708a72dtc63cf88e3ca81353@mail.gmail.com> Message-ID: <49F5EA0C.30300@nibrahim.net.in> Greetings Sudanshu, sudhanshu gautam wrote: > when we work on the oops in the python then we have to pass the first > parameter as a self then the value of the other parameters . Well, not explicitly. If you're familiar with OOPS in C++, self is analogous to the "this" pointer there. Also, you needn't call it self. Writing something like class A(object): def __init__(me, name): me.name = name print "The name is %s"%me.name will work fine as well. The actual identifier 'self' is just a convention. > for example: > class Student: > def __init__(self,name): > self.name =name > print 'The name is',self.name > > a=student('sudhanshu') > a is an object of the student class > so __init__ is a constructor in which we do not need to call the > function separately . __init__ is not *a* constructor as much as it is *the* constructor method. It is one of the many class methods that have special meanings. If you want the entire list, try http://docs.python.org/reference/datamodel.html#special-method-names > Now If I placed the name of the constructor rather than the __init__ > __baba___ so will it also work as a constructor or constructor has > specified already if yes then give me list of them __baba__ is not a special function so it will have no special significance. It is treated as any other class method. I believe the URL above has what you're looking for. Thanks -- ~noufal http://nibrahim.net.in/ From emile at fenx.com Mon Apr 27 19:30:33 2009 From: emile at fenx.com (Emile van Sebille) Date: Mon, 27 Apr 2009 10:30:33 -0700 Subject: [Tutor] How many types of the constructor In-Reply-To: <6f9dbdf40904271008w1708a72dtc63cf88e3ca81353@mail.gmail.com> References: <6f9dbdf40904271008w1708a72dtc63cf88e3ca81353@mail.gmail.com> Message-ID: sudhanshu gautam wrote: > Now If I placed the name of the constructor rather than the __init__ > __baba___ so will it also work as a constructor or constructor has > specified already if yes then give me list of them I'm not sure I parsed your question as you intended, but __init__ is pretty much the constructor method ( with __new__ ) But I suspect you may be asking about that group know as magic methods, which you may want to look into. These include things like __getattr__, __len__, __gt__, and lots more. These make it easy to do things like: >>> class Test: ... def __init__(self):pass ... def __gt__(self,other): return True ... >>> t = Test() >>> t>3 True >>> t>0 True >>> t>"hello" True >>> And you may even find out why the following work as well... >>> t<"hello" True >>> t <3 True >>> Regards, Emile From denis.spir at free.fr Mon Apr 27 19:55:38 2009 From: denis.spir at free.fr (spir) Date: Mon, 27 Apr 2009 19:55:38 +0200 Subject: [Tutor] How many types of the constructor In-Reply-To: <6f9dbdf40904271008w1708a72dtc63cf88e3ca81353@mail.gmail.com> References: <6f9dbdf40904271008w1708a72dtc63cf88e3ca81353@mail.gmail.com> Message-ID: <20090427195538.724657d9@o> Le Mon, 27 Apr 2009 22:38:30 +0530, sudhanshu gautam s'exprima ainsi: > Now If I placed the name of the constructor rather than the __init__ > __baba___ so will it also work as a constructor or constructor has specified > already if yes then give me list of them Actually __init__ is not the constructor, which is __new__ (much less used), but the initialiser. If you try to do their work using other names, it may not work as expected because python does some magic in the background. Still, if you really know what you're doing it's possible for some of these special methods, while not for __new__ I guess. Anyway, it's maybe a bad idea cause conventional names help and understand the code. > ragards > sudhanshu Denis ------ la vita e estrany From alan.gauld at btinternet.com Mon Apr 27 21:32:19 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 27 Apr 2009 20:32:19 +0100 Subject: [Tutor] How to run a .py file or load a module? References: <49F4D3A8.2060407@gmail.com> <20090427101523.5fc09539@o> Message-ID: Offlist response... Nice answer Denis, you neatly covered all the loose ends from previous answers that I was just about to pick up on! :-) "spir" wrote in message news:20090427101523.5fc09539 at o... > Le Sun, 26 Apr 2009 22:35:36 +0100, > Dayo Adewunmi s'exprima ainsi: > >> How can I >> >> a) Open my shell, and do something like: $ python countdown.py >> but have it take an argument and pass it to the function, and execute. > > When your code is (nicely) organised as a set of funcs or class > definitions, you also need a "laucher" usually called "main()". Only question is why, after saying it's usually called "main" that you chose "launch"?! :-) Alan G. From davholla2002 at yahoo.co.uk Mon Apr 27 22:21:58 2009 From: davholla2002 at yahoo.co.uk (David Holland) Date: Mon, 27 Apr 2009 20:21:58 +0000 (GMT) Subject: [Tutor] Lists in a file Message-ID: <694569.23386.qm@web25607.mail.ukl.yahoo.com> Thanks for all the suggestions.? I will have to try this. --- On Sun, 26/4/09, Kent Johnson wrote: From: Kent Johnson Subject: Re: [Tutor] Lists in a file To: "Robert Berman" Cc: "David Holland" , "tutor python" Date: Sunday, 26 April, 2009, 8:04 PM On Sun, Apr 26, 2009 at 2:18 PM, Robert Berman wrote: > David, > > You are processing a text file. It is your job to treat it as a file > comprised of lists. I think what you are saying is that each line in the > text file is a list. In that case > > for line in fileobject: > ? listline = list(line) > > Now, listline is a list of the text items in line. listline will be a list of all the characters in the line, which is not what the OP wants. If the list will just contain quoted strings that don't themselves contain commas you can do something ad hoc: In [2]: s = "['a','b','c']" In [5]: [ str(item[1:-1]) for item in s[1:-1].split(',') ] Out[5]: ['a', 'b', 'c'] In Python 3 you can use ast.literal_eval(). >>> import ast >>> s = "['a','b','c']" >>> ast.literal_eval(s) ['a', 'b', 'c'] You can use these recipes: http://code.activestate.com/recipes/511473/ http://code.activestate.com/recipes/364469/ Kent -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Apr 28 01:16:39 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Apr 2009 00:16:39 +0100 Subject: [Tutor] How many types of the constructor References: <6f9dbdf40904271008w1708a72dtc63cf88e3ca81353@mail.gmail.com> Message-ID: "sudhanshu gautam" wrote > Now If I placed the name of the constructor rather than the __init__ > __baba___ so will it also work as a constructor or constructor has > specified > already if yes then give me list of them Unlike some other OOP languages Python only has one constructor (OK As Denis pointed out it has the pair of new and init). It does not allow you to define multiple different constructors with different argument lists. We can fake this behaviour by defining the constructor to take an arbitrary list of arguments and then based on the length of that list calling different methods, something like (untested): class C: def __init__(this, *args): if len(args) == 0: this._defaultConstructor() elif len(args) == 1: this._oneArg(args[0]) elif len(args) == 2: this._twoArgs(args[0],args[1]) else: raise ValueError def _default(this): print 'default' def _oneArg(this, a1): print "Arg - ", a1 def _ twoArgs(this, a1,a2): print "Args - " a1, a2 c0 = C() # -> default c1 = C('foo') # -> Arg - foo c2 = C('bar', 42) # -> Args - bar 42 c3 = C(1,2,3) # -> error The single _ is just a convention to indicate a "private" method, that is one not intended for direct use We can also use named arguments and default values to achieve similar functionality. This is how the Widgets in most GUIs work. You call the widget with a specifically named subset of parameters. For example: from Tkinter import * top = Tk() Label(top, text="OK", foreground="Blue).pack() Label(top, text="Cancel").pack() top.mainloop() The second label just adopts the default colour scheme. Hopefully this answers your question. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Apr 28 01:17:51 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Apr 2009 00:17:51 +0100 Subject: [Tutor] How to run a .py file or load a module? References: <49F4D3A8.2060407@gmail.com> <20090427101523.5fc09539@o> Message-ID: "Alan Gauld" wrote > Offlist response... Oops, not offlist! Just as well I didn't say anything offensive! :-) Alan G From danliang20 at gmail.com Tue Apr 28 05:29:13 2009 From: danliang20 at gmail.com (Dan Liang) Date: Mon, 27 Apr 2009 23:29:13 -0400 Subject: [Tutor] Working with lines from file and printing to another keeping sequential order In-Reply-To: <1c2a2c590904260755y8437e47g55a87d7b0885a9cd@mail.gmail.com> References: <49F31F07.8060702@gmail.com> <1c2a2c590904260755y8437e47g55a87d7b0885a9cd@mail.gmail.com> Message-ID: Hi Bob, Shantanoo, Kent, and tutors, Thank you Bob, Shantanoo, Kent for all the nice feedback. Exception handling, the concept of states in cs, and the use of the for loop with offset helped a lot. Here is the code I now have, based on your suggestions, and it does what I need: ListLines = [ line.rstrip() for line in open('test.txt') ] countYes = 0 countNo = 0 for i in range(len(ListLines)): if ListLines[i].endswith('yes'): countYes+=1 print "countYes", countYes, "\t\t", ListLines[i] if not ListLines[i].endswith('yes'): continue for offset in (1, 2, 3, 4, 5, 6, 7, 8): if i+offset < len(ListLines) and ListLines[i+offset].endswith('no'): countNo+=1 print "countNo", countNo, "\t\t", ListLines[i+offset] Thank you again! --dan On Sun, Apr 26, 2009 at 10:55 AM, Kent Johnson wrote: > On Sat, Apr 25, 2009 at 2:11 PM, Dan Liang wrote: > > Hi Bob and tutors, > > > > Thanks Bob for your response! currently I have the current code, but it > does > > not work: > > > > ListLines= [] > > for line in open('test.txt'): > > line = line.rstrip() > > ListLines.append(line) > > This could be written with a list comprehension: > ListLines = [ line.rstrip() for line in open('test.txt') ] > > > > > for i in range(len(ListLines)): > > > > if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and > > ListLines[i+1].endswith("no"): > > print ListLines[i], ListLines[i+1], ListLines[i+2] > > elif ListLines[i].endswith("yes") and ListLines[i+1].endswith("no"): > > print ListLines[i], ListLines[i+1] > > elif ListLines[i].endswith("yes"): > > print ListLines[i] > > elif ListLines[i].endswith("no"): > > continue > > else: > > break > > You only need to test for ListLines[i].endswith('yes') once. Then you > could use a loop to test for lines ending with 'no'. > > for i in range(len(ListLines)): > if not ListLines[i].endswith('yes'): > continue > for offset in (1, 2): > if i+offset < len(ListLines) and ListLines[i+offset].endswith('no'): > print ListLines[i+offset] > > You could adapt the above for a variable number of 'no' lines with > something like > for offset in range(1, maxNo+1): > > > I get the following error: > > Traceback (most recent call last): > > File "test.py", line 18, in > > if ListLines[i].endswith("yes") and ListLines[i+1].endswith("no") and > > ListLines[i+1].endswith("no"): > > IndexError: list index out of range > > That is because you have a 'yes' line at the end of the file so the > check for 'no' tries to read past the end of ListLines. In my code the > test for i+offset < len(ListLines) will prevent that exception. > > Kent > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kf9150 at gmail.com Tue Apr 28 10:03:00 2009 From: kf9150 at gmail.com (Kelie) Date: Tue, 28 Apr 2009 08:03:00 +0000 (UTC) Subject: [Tutor] regular expression question Message-ID: Hello, The following code returns 'abc123abc45abc789jk'. How do I revise the pattern so that the return value will be 'abc789jk'? In other words, I want to find the pattern 'abc' that is closest to 'jk'. Here the string '123', '45' and '789' are just examples. They are actually quite different in the string that I'm working with. import re s = 'abc123abc45abc789jk' p = r'abc.+jk' lst = re.findall(p, s) print lst[0] Thanks for your help! From denis.spir at free.fr Tue Apr 28 10:16:51 2009 From: denis.spir at free.fr (spir) Date: Tue, 28 Apr 2009 10:16:51 +0200 Subject: [Tutor] Working with lines from file and printing to another keeping sequential order In-Reply-To: References: <49F31F07.8060702@gmail.com> <1c2a2c590904260755y8437e47g55a87d7b0885a9cd@mail.gmail.com> Message-ID: <20090428101651.58715828@o> Le Mon, 27 Apr 2009 23:29:13 -0400, Dan Liang s'exprima ainsi: > Hi Bob, Shantanoo, Kent, and tutors, > > Thank you Bob, Shantanoo, Kent for all the nice feedback. Exception > handling, the concept of states in cs, and the use of the for loop with > offset helped a lot. Here is the code I now have, based on your suggestions, > and it does what I need: > > ListLines = [ line.rstrip() for line in open('test.txt') ] > > countYes = 0 > countNo = 0 > > for i in range(len(ListLines)): > if ListLines[i].endswith('yes'): > countYes+=1 > print "countYes", countYes, "\t\t", ListLines[i] > > if not ListLines[i].endswith('yes'): > continue > > for offset in (1, 2, 3, 4, 5, 6, 7, 8): > if i+offset < len(ListLines) and ListLines[i+offset].endswith('no'): > > countNo+=1 > > print "countNo", countNo, "\t\t", ListLines[i+offset] It probably works, but there is something uselessly complicated, logically speaking: -1- case ends with 'yes', do -2- case not ends with 'yes', do -3- case ends with 'yes', again, do You'd better group -1- and -3-, no? Moreover, as action -2- is to continue, further code is simplified if -2- is placed first: for i in range(len(ListLines)): if not ListLines[i].endswith('yes'): continue # case line ends with 'yes': process it countYes+=1 print "countYes", countYes, "\t\t", ListLines[i] for offset in (1, 2, 3, 4, 5, 6, 7, 8): if i+offset < len(ListLines) and ListLines[i+offset].endswith('no'): countNo+=1 print "countNo", countNo, "\t\t", ListLines[i+offset] Also, use more than 1 space for indent, and be consistent (set the value in your editor settings and use the TAB key to achieve that); and avoid too many useless blank lines. Denis ------ la vita e estrany From contactdayo at gmail.com Tue Apr 28 10:29:39 2009 From: contactdayo at gmail.com (Dayo Adewunmi) Date: Tue, 28 Apr 2009 09:29:39 +0100 Subject: [Tutor] How to run a .py file or load a module? In-Reply-To: <49F50B82.8090206@abbottdavid.com> References: <49F4D3A8.2060407@gmail.com> <9c2c8ffb0904261802u1ccb11d7tb7c6671b9f6a8aa5@mail.gmail.com> <49F50B82.8090206@abbottdavid.com> Message-ID: <49F6BE73.4060900@gmail.com> David wrote: > Norman Khine wrote: >> On Mon, Apr 27, 2009 at 12:07 AM, Sander Sweers >> wrote: > Here is another one for fun, you run it like > python countdown.py 10 > > #!/usr/bin/env python > > import sys > from time import sleep > > times = int(sys.argv[1]) # The argument given on the command line > > def countdown(n): > try: > while n != 1: > n = n-1 > print n > sleep(1) > finally: > print 'Blast Off!' > > countdown(times) > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > Thank you all for all your valuable input on this. I have learned so much on this particular subject in such a short time. David, I ran your code, and noticed that given countdown(10) your countdown starts at 9 and Blastoff takes place after 1, not 0. To fix that, I changed while n ! = 1 to while n != 0 and changed n = n - 1 print n to print n n = n -1 Thanks for the time you guys have put into this. It's much appreciated. :-) Dayo From contactdayo at gmail.com Tue Apr 28 10:38:56 2009 From: contactdayo at gmail.com (Dayo Adewunmi) Date: Tue, 28 Apr 2009 09:38:56 +0100 Subject: [Tutor] How to run a .py file or load a module? In-Reply-To: <20090427101523.5fc09539@o> References: <49F4D3A8.2060407@gmail.com> <20090427101523.5fc09539@o> Message-ID: <49F6C0A0.9030009@gmail.com> Denis, this mail was very comprehensive, and went a long way of driving it all home for me. There are several different concepts that are involved in this simple problem that I had, and you guys explaining them has really expanded my pythonic horizon, especially the explanations on the argv module, and also the idea of from import as Thanks a lot, everybody. :-) Dayo ------------------ spir wrote: > Le Sun, 26 Apr 2009 22:35:36 +0100, > Dayo Adewunmi s'exprima ainsi: > > >> How can I >> >> a) Open my shell, and do something like: $ python countdown.py >> but have it take an argument and pass it to the function, and execute. >> > > When your code is (nicely) organised as a set of funcs or class definitions, you also need a "laucher" usually called "main()". Otherwise python only parses and records the definitions into live objects that wait for someone to tell them what they're supposed to do. I'll stick first at processes without any parameter, like if your func would always countdown from 10. > There are several use patterns: > > (1) Program launched from command line. > Just add a call to your func: > countdown(10) > > (2) Module imported from other prog > Nothing to add to your module. > Instead, the importing code needs to hold: > import countdown # the module (file) > ... > countdown.countdown(n) # the func itself > or > from countdown import countdown # the func, directly > ... > countdown(n) > > (3) Both > You need to differenciate between launching and importing. Python provides a rather esoteric idiom for that: > > if __name__ == "__main__": > countdown(10) > The trick is that when a prog is launched directly (as opposed to imported), it silently gets a '__name__' attribute that is automatically set to "__main__". So that the one-line block above will only run when the prog is launched, like in case (1). While nothing will happen when the module is imported -- instead the importing code will have the countdown func available under name 'countdown' as expected, like in case (2). Clear? > > >> b) Import the function in the interactive interpreter, and call it like so: >> >> countdown(10) >> >> without getting the abovementioned error. >> > > In the case of an import, as your func definition has the proper parameter, you have nothing to change. > While for a launch from command-line, you need to get the parameter given by the user. > But how? Python provides a way to read the command-line arguments under an attribute called 'argv' of the 'sys' module. > argv is a list which zerost item is the name of the file. For instance if called > python countdown.py 9 > argv holds: ['countdown.py', '9'] > Note that both are strings. Then you can catch and use the needed parameter, e.g. > > from time import sleep as wait > from sys import argv as user_args > > def countdown(n=10): > if n <= 0: > print 'Blastoff!' > else: > wait(0.333) > print n > countdown(n-1) > > def launch(): > if len(user_args) == 1: > countdown() > else: > n = int(user_args[1]) > countdown(n) > > if __name__ == "__main__": > launch() > > (You can indeed put the content of launch() in the if block. But I find it clearer that way, and it happens to be a common practice.) > > Denis > ------ > la vita e estrany > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > From =?UTF-8?Q?Marek_Spoci=C5=84ski at go2.pl Tue Apr 28 11:06:16 2009 From: =?UTF-8?Q?Marek_Spoci=C5=84ski at go2.pl (=?UTF-8?Q?Marek_Spoci=C5=84ski at go2.pl) Date: Tue, 28 Apr 2009 11:06:16 +0200 Subject: [Tutor] =?utf-8?q?regular_expression_question?= Message-ID: <4e200155.69860c84.49f6c708.c1623@o2.pl> > Hello, > > The following code returns 'abc123abc45abc789jk'. How do I revise the pattern so > that the return value will be 'abc789jk'? In other words, I want to find the > pattern 'abc' that is closest to 'jk'. Here the string '123', '45' and '789' are > just examples. They are actually quite different in the string that I'm working > with. > > import re > s = 'abc123abc45abc789jk' > p = r'abc.+jk' > lst = re.findall(p, s) > print lst[0] I suggest using r'abc.+?jk' instead. the additional ? makes the preceeding '.+' non-greedy so instead of matching as long string as it can it matches as short string as possible. From andreengels at gmail.com Tue Apr 28 11:16:28 2009 From: andreengels at gmail.com (Andre Engels) Date: Tue, 28 Apr 2009 11:16:28 +0200 Subject: [Tutor] regular expression question In-Reply-To: <4e200155.69860c84.49f6c708.c1623@o2.pl> References: <4e200155.69860c84.49f6c708.c1623@o2.pl> Message-ID: <6faf39c90904280216kd39073fv7c56b21207d534b4@mail.gmail.com> 2009/4/28 Marek Spoci?ski at go2.pl,Poland : >> Hello, >> >> The following code returns 'abc123abc45abc789jk'. How do I revise the pattern so >> that the return value will be 'abc789jk'? In other words, I want to find the >> pattern 'abc' that is closest to 'jk'. Here the string '123', '45' and '789' are >> just examples. They are actually quite different in the string that I'm working >> with. >> >> import re >> s = 'abc123abc45abc789jk' >> p = r'abc.+jk' >> lst = re.findall(p, s) >> print lst[0] > > I suggest using r'abc.+?jk' instead. > > the additional ? makes the preceeding '.+' non-greedy so instead of matching as long string as it can it matches as short string as possible. That was my first idea too, but it does not work for this case, because Python will still try to _start_ the match as soon as possible. To use .+? one would have to revert the string, then use the reverse regular expression on the result, which looks like a rather roundabout way of doing things. -- Andr? Engels, andreengels at gmail.com From =?UTF-8?Q?Marek_Spoci=C5=84ski Tue Apr 28 11:26:49 2009 From: =?UTF-8?Q?Marek_Spoci=C5=84ski (=?UTF-8?Q?Marek_Spoci=C5=84ski) Date: Tue, 28 Apr 2009 11:26:49 +0200 Subject: [Tutor] =?utf-8?q?regular_expression_question?= In-Reply-To: <6faf39c90904280216kd39073fv7c56b21207d534b4@mail.gmail.com> References: <4e200155.69860c84.49f6c708.c1623@o2.pl> <6faf39c90904280216kd39073fv7c56b21207d534b4@mail.gmail.com> Message-ID: <141b6191.3f17a27b.49f6cbd9.9bc6b@o2.pl> Dnia 28 kwietnia 2009 11:16 Andre Engels napisa?(a): > 2009/4/28 Marek Spoci?ski at go2.pl,Poland : > >> Hello, > >> > >> The following code returns 'abc123abc45abc789jk'. How do I revise the pattern so > >> that the return value will be 'abc789jk'? In other words, I want to find the > >> pattern 'abc' that is closest to 'jk'. Here the string '123', '45' and '789' are > >> just examples. They are actually quite different in the string that I'm working > >> with. > >> > >> import re > >> s = 'abc123abc45abc789jk' > >> p = r'abc.+jk' > >> lst = re.findall(p, s) > >> print lst[0] > > > > I suggest using r'abc.+?jk' instead. > > > > the additional ? makes the preceeding '.+' non-greedy so instead of matching as long string as it can it matches as short string as possible. > > That was my first idea too, but it does not work for this case, > because Python will still try to _start_ the match as soon as > possible. To use .+? one would have to revert the string, then use the > reverse regular expression on the result, which looks like a rather > roundabout way of doing things. I don't have access to python right now so i cannot test my ideas... And i don't really want to give you wrong idea too. From denis.spir at free.fr Tue Apr 28 11:45:53 2009 From: denis.spir at free.fr (spir) Date: Tue, 28 Apr 2009 11:45:53 +0200 Subject: [Tutor] regular expression question In-Reply-To: <4e200155.69860c84.49f6c708.c1623@o2.pl> References: <4e200155.69860c84.49f6c708.c1623@o2.pl> Message-ID: <20090428114553.77fd5f8c@o> Le Tue, 28 Apr 2009 11:06:16 +0200, Marek Spoci?ski at go2.pl, Poland s'exprima ainsi: > > Hello, > > > > The following code returns 'abc123abc45abc789jk'. How do I revise the > > pattern so that the return value will be 'abc789jk'? In other words, I > > want to find the pattern 'abc' that is closest to 'jk'. Here the string > > '123', '45' and '789' are just examples. They are actually quite > > different in the string that I'm working with. > > > > import re > > s = 'abc123abc45abc789jk' > > p = r'abc.+jk' > > lst = re.findall(p, s) > > print lst[0] > > I suggest using r'abc.+?jk' instead. > > the additional ? makes the preceeding '.+' non-greedy so instead of > matching as long string as it can it matches as short string as possible. Non-greedy repetition will not work in this case, I guess: from re import compile as Pattern s = 'abc123abc45abc789jk' p = Pattern(r'abc.+?jk') print p.match(s).group() ==> abc123abc45abc789jk (Someone explain why?) My solution would be to explicitely exclude 'abc' from the sequence of chars matched by '.+'. To do this, use negative lookahead (?!...) before '.': p = Pattern(r'(abc((?!abc).)+jk)') print p.findall(s) ==> [('abc789jk', '9')] But it's not exactly what you want. Because the internal () needed to express exclusion will be considered by findall as a group to be returned, so that you also get the last char matched in there. To avoid that, use non-grouping parens (?:...). This also avoids the need for parens around the whole format: p = Pattern(r'abc(?:(?!abc).)+jk') print p.findall(s) ['abc789jk'] Denis ------ la vita e estrany From kf9150 at gmail.com Tue Apr 28 12:20:45 2009 From: kf9150 at gmail.com (Kelie) Date: Tue, 28 Apr 2009 10:20:45 +0000 (UTC) Subject: [Tutor] regular expression question References: <4e200155.69860c84.49f6c708.c1623@o2.pl> <6faf39c90904280216kd39073fv7c56b21207d534b4@mail.gmail.com> Message-ID: Andre Engels gmail.com> writes: > > 2009/4/28 Marek Spoci?ski go2.pl,Poland 10g.pl>: > > I suggest using r'abc.+?jk' instead. > > > > That was my first idea too, but it does not work for this case, > because Python will still try to _start_ the match as soon as > possible. yeah, i tried the '?' as well and realized it would not work. From kf9150 at gmail.com Tue Apr 28 12:22:12 2009 From: kf9150 at gmail.com (Kelie) Date: Tue, 28 Apr 2009 10:22:12 +0000 (UTC) Subject: [Tutor] regular expression question References: <4e200155.69860c84.49f6c708.c1623@o2.pl> <20090428114553.77fd5f8c@o> Message-ID: spir free.fr> writes: > To avoid that, use non-grouping parens (?:...). This also avoids the need for parens around the whole format: > p = Pattern(r'abc(?:(?!abc).)+jk') > print p.findall(s) > ['abc789jk'] > > Denis This one works! Thank you Denis. I'll try it out on the actual much longer (multiline) string and see what happens. From kent37 at tds.net Tue Apr 28 12:32:34 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 28 Apr 2009 06:32:34 -0400 Subject: [Tutor] regular expression question In-Reply-To: <4e200155.69860c84.49f6c708.c1623@o2.pl> References: <4e200155.69860c84.49f6c708.c1623@o2.pl> Message-ID: <1c2a2c590904280332o121cdcd4k376232778bb55e2e@mail.gmail.com> 2009/4/28 Marek Spoci?ski at go2.pl,Poland : >> import re >> s = 'abc123abc45abc789jk' >> p = r'abc.+jk' >> lst = re.findall(p, s) >> print lst[0] > > I suggest using r'abc.+?jk' instead. > > the additional ? makes the preceeding '.+' non-greedy so instead of matching as long string as it can it matches as short string as possible. Did you try it? It doesn't do what you expect, it still matches at the beginning of the string. The re engine searches for a match at a location and returns the first one it finds. A non-greedy match doesn't mean "Find the shortest possible match anywhere in the string", it means, "find the shortest possible match starting at this location." Kent From kent37 at tds.net Tue Apr 28 12:36:47 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 28 Apr 2009 06:36:47 -0400 Subject: [Tutor] regular expression question In-Reply-To: References: Message-ID: <1c2a2c590904280336o34add4ddl982bba3949c3a953@mail.gmail.com> On Tue, Apr 28, 2009 at 4:03 AM, Kelie wrote: > Hello, > > The following code returns 'abc123abc45abc789jk'. How do I revise the pattern so > that the return value will be 'abc789jk'? In other words, I want to find the > pattern 'abc' that is closest to 'jk'. Here the string '123', '45' and '789' are > just examples. They are actually quite different in the string that I'm working > with. > > import re > s = 'abc123abc45abc789jk' > p = r'abc.+jk' > lst = re.findall(p, s) > print lst[0] re.findall() won't work because it finds non-overlapping matches. If there is a character in the initial match which cannot occur in the middle section, change .+ to exclude that character. For example, r'abc[^a]+jk' works with your example. Another possibility is to look for the match starting at different locations, something like this: p = re.compile(r'abc.+jk') lastMatch = None i = 0 while i < len(s): m = p.search(s, i) if m is None: break lastMatch = m.group() i = m.start() + 1 print lastMatch Kent From dineshbvadhia at hotmail.com Tue Apr 28 14:54:54 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Tue, 28 Apr 2009 05:54:54 -0700 Subject: [Tutor] finding mismatched or unpaired html tags Message-ID: I'm processing tens of thousands of html files and a few of them contain mismatched tags and ElementTree throws the error: "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: line 124, column 8" I now want to scan each file and simply identify each mismatched or unpaired tags (by line number) in each file. I've read the ElementTree docs and cannot see anything obvious how to do this. I know this is a common problem but feeling a bit clueless here - any ideas? Dinesh -------------- next part -------------- An HTML attachment was scrubbed... URL: From a.t.hofkamp at tue.nl Tue Apr 28 15:44:35 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Tue, 28 Apr 2009 15:44:35 +0200 Subject: [Tutor] finding mismatched or unpaired html tags In-Reply-To: References: Message-ID: <49F70843.6030907@tue.nl> Dinesh B Vadhia wrote: > I'm processing tens of thousands of html files and a few of them contain mismatched tags and ElementTree throws the error: > > "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: line 124, column 8" > > I now want to scan each file and simply identify each mismatched or > unpaired tags (by line number) in each file. I've read the ElementTree docs and cannot see anything obvious how to do this. I know this is a common problem but feeling a bit clueless here - any ideas? > Don't use elementTree, use BeautifulSoup instead. elementTree expects perfect input, typically generated by another computer. BeautifulSoup is designed to handle your everyday HTML page, filled with errors of all possible kinds. Sincerely, Albert From mwalsh at mwalsh.org Tue Apr 28 15:54:33 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Tue, 28 Apr 2009 08:54:33 -0500 Subject: [Tutor] finding mismatched or unpaired html tags In-Reply-To: <49F70843.6030907@tue.nl> References: <49F70843.6030907@tue.nl> Message-ID: <49F70A99.3050002@mwalsh.org> A.T.Hofkamp wrote: > Dinesh B Vadhia wrote: >> I'm processing tens of thousands of html files and a few of them >> contain mismatched tags and ElementTree throws the error: >> >> "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: >> line 124, column 8" >> >> I now want to scan each file and simply identify each mismatched or >> unpaired > tags (by line number) in each file. I've read the ElementTree docs and > cannot > see anything obvious how to do this. I know this is a common problem but > feeling a bit clueless here - any ideas? >> > > Don't use elementTree, use BeautifulSoup instead. > > elementTree expects perfect input, typically generated by another computer. > BeautifulSoup is designed to handle your everyday HTML page, filled with > errors of all possible kinds. But it also modifies the source html by default, adding closing tags, etc. Important to know, I suppose, if you intend to re-write the html files you parse with BeautifulSoup. Also, unless you're running python 3.0 or greater, use the 3.0.x series of BeautifulSoup -- otherwise you may run into the same issue. http://www.crummy.com/software/BeautifulSoup/3.1-problems.html HTH, Marty From dineshbvadhia at hotmail.com Tue Apr 28 16:03:00 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Tue, 28 Apr 2009 07:03:00 -0700 Subject: [Tutor] finding mismatched or unpaired html tags Message-ID: A.T. / Marty I'd prefer that the html parser didn't replace the missing tags as I want to know where and what the problems are. Also, the source html documents were generated by another computer ie. they are not web page documents. My sense is that it is only a few files out of tens of thousands. Cheers ... Dinesh -------------------------------------------------------------------------------- Message: 7 Date: Tue, 28 Apr 2009 08:54:33 -0500 From: Martin Walsh Subject: Re: [Tutor] finding mismatched or unpaired html tags To: "tutor at python.org" Message-ID: <49F70A99.3050002 at mwalsh.org> Content-Type: text/plain; charset=us-ascii A.T.Hofkamp wrote: > Dinesh B Vadhia wrote: >> I'm processing tens of thousands of html files and a few of them >> contain mismatched tags and ElementTree throws the error: >> >> "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: >> line 124, column 8" >> >> I now want to scan each file and simply identify each mismatched or >> unpaired > tags (by line number) in each file. I've read the ElementTree docs and > cannot > see anything obvious how to do this. I know this is a common problem but > feeling a bit clueless here - any ideas? >> > > Don't use elementTree, use BeautifulSoup instead. > > elementTree expects perfect input, typically generated by another computer. > BeautifulSoup is designed to handle your everyday HTML page, filled with > errors of all possible kinds. But it also modifies the source html by default, adding closing tags, etc. Important to know, I suppose, if you intend to re-write the html files you parse with BeautifulSoup. Also, unless you're running python 3.0 or greater, use the 3.0.x series of BeautifulSoup -- otherwise you may run into the same issue. http://www.crummy.com/software/BeautifulSoup/3.1-problems.html HTH, Marty -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Tue Apr 28 16:13:43 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 28 Apr 2009 10:13:43 -0400 Subject: [Tutor] finding mismatched or unpaired html tags In-Reply-To: References: Message-ID: <1c2a2c590904280713v5e91924fj2ed1d177b321e00e@mail.gmail.com> On Tue, Apr 28, 2009 at 8:54 AM, Dinesh B Vadhia wrote: > I'm processing tens of thousands of html files and a few of them contain > mismatched tags and ElementTree throws the error: > > "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: line 124, > column 8" > > I now want to scan each file and simply identify each mismatched or unpaired > tags (by line number) in each file.? I've read the ElementTree docs and > cannot see anything obvious how to do this.? I know this is a common problem > but feeling a bit clueless here - any ideas? It seems like the exception gives you the line number. What kind of exception is raised? The exception object may contain the line and column in a more accessible form, so you could catch the exception, get the line number, then read that line out of the file and show it. Kent From mwalsh at mwalsh.org Tue Apr 28 16:14:22 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Tue, 28 Apr 2009 09:14:22 -0500 Subject: [Tutor] finding mismatched or unpaired html tags In-Reply-To: References: Message-ID: <49F70F3E.5090004@mwalsh.org> Dinesh B Vadhia wrote: > A.T. / Marty > > I'd prefer that the html parser didn't replace the missing tags as I > want to know where and what the problems are. Also, the source html > documents were generated by another computer ie. they are not web page > documents. My sense is that it is only a few files out of tens of > thousands. Cheers ... > > Dinesh If this is a one time task, write a script to iterate over the html files, and collect the traceback info from those that throw a 'mismatched tag' error. Based on your example below, it appears to contain the line number. You'd only get one error per file per run, but you can run it until there are no errors remaining. I hope that makes sense. HTH, Marty > > > ------------------------------------------------------------------------ > Message: 7 > Date: Tue, 28 Apr 2009 08:54:33 -0500 > From: Martin Walsh > Subject: Re: [Tutor] finding mismatched or unpaired html tags > To: "tutor at python.org" > Message-ID: <49F70A99.3050002 at mwalsh.org> > Content-Type: text/plain; charset=us-ascii > > A.T.Hofkamp wrote: >> Dinesh B Vadhia wrote: >>> I'm processing tens of thousands of html files and a few of them >>> contain mismatched tags and ElementTree throws the error: >>> >>> "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: >>> line 124, column 8" >>> >>> I now want to scan each file and simply identify each mismatched or >>> unpaired >> tags (by line number) in each file. I've read the ElementTree docs and >> cannot >> see anything obvious how to do this. I know this is a common problem but >> feeling a bit clueless here - any ideas? >>> >> >> Don't use elementTree, use BeautifulSoup instead. >> >> elementTree expects perfect input, typically generated by another > computer. >> BeautifulSoup is designed to handle your everyday HTML page, filled with >> errors of all possible kinds. > > But it also modifies the source html by default, adding closing tags, > etc. Important to know, I suppose, if you intend to re-write the html > files you parse with BeautifulSoup. > > Also, unless you're running python 3.0 or greater, use the 3.0.x series > of BeautifulSoup -- otherwise you may run into the same issue. > > http://www.crummy.com/software/BeautifulSoup/3.1-problems.html > > HTH, > Marty > > > ------------------------------------------------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From dineshbvadhia at hotmail.com Tue Apr 28 16:41:36 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Tue, 28 Apr 2009 07:41:36 -0700 Subject: [Tutor] finding mismatched or unpaired html tags In-Reply-To: <1c2a2c590904280713v5e91924fj2ed1d177b321e00e@mail.gmail.com> References: <1c2a2c590904280713v5e91924fj2ed1d177b321e00e@mail.gmail.com> Message-ID: This is the error and traceback: Unexpected error opening J:/F2/....html: mismatched tag: line 124, column 8 Traceback (most recent call last): File "C:\....py", line 492, in raw = extractText(xhtmlfile) File "C:\....py", line 334, in extractText tree = make_tree(xhtmlfile) File "....py", line 169, in make_tree return tree UnboundLocalError: local variable 'tree' referenced before assignment Here is line 124, col 8 and I cannot see any obvious missing/mismatched tags: "

As to the present time I am unable physical and mentally to secure all this information at present.

" Dinesh From: Kent Johnson Sent: Tuesday, April 28, 2009 7:13 AM To: Dinesh B Vadhia Cc: tutor at python.org Subject: Re: [Tutor] finding mismatched or unpaired html tags On Tue, Apr 28, 2009 at 8:54 AM, Dinesh B Vadhia wrote: > I'm processing tens of thousands of html files and a few of them contain > mismatched tags and ElementTree throws the error: > > "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: line 124, > column 8" > > I now want to scan each file and simply identify each mismatched or unpaired > tags (by line number) in each file. I've read the ElementTree docs and > cannot see anything obvious how to do this. I know this is a common problem > but feeling a bit clueless here - any ideas? It seems like the exception gives you the line number. What kind of exception is raised? The exception object may contain the line and column in a more accessible form, so you could catch the exception, get the line number, then read that line out of the file and show it. Kent -------------- next part -------------- An HTML attachment was scrubbed... URL: From denis.spir at free.fr Tue Apr 28 17:12:51 2009 From: denis.spir at free.fr (spir) Date: Tue, 28 Apr 2009 17:12:51 +0200 Subject: [Tutor] finding mismatched or unpaired html tags In-Reply-To: References: <1c2a2c590904280713v5e91924fj2ed1d177b321e00e@mail.gmail.com> Message-ID: <20090428171251.5ec93bfd@o> Le Tue, 28 Apr 2009 07:41:36 -0700, "Dinesh B Vadhia" s'exprima ainsi: > This is the error and traceback: > > Unexpected error opening J:/F2/....html: mismatched tag: line 124, column 8 > > Traceback (most recent call last): > File "C:\....py", line 492, in > raw = extractText(xhtmlfile) > File "C:\....py", line 334, in extractText > tree = make_tree(xhtmlfile) > File "....py", line 169, in make_tree > return tree > UnboundLocalError: local variable 'tree' referenced before assignment > > > Here is line 124, col 8 and I cannot see any obvious missing/mismatched > tags: > > "

As to the present time I am unable physical and mentally to secure all > this information at present.

" > > Dinesh As for programming syntax error, the place where an html error is detected by the parser may well be one or more line(s) after the actual error: you should watch lines before #124. Also, the traceback looks strange: it seems that ElementTree interprets a python UnboundLocalError for the variable 'tree' as being caused by an html error in source. ??? denis ------ la vita e estrany From kent37 at tds.net Tue Apr 28 17:17:46 2009 From: kent37 at tds.net (Kent Johnson) Date: Tue, 28 Apr 2009 11:17:46 -0400 Subject: [Tutor] finding mismatched or unpaired html tags In-Reply-To: References: <1c2a2c590904280713v5e91924fj2ed1d177b321e00e@mail.gmail.com> Message-ID: <1c2a2c590904280817v7a29cbcdm11c88431b949d2aa@mail.gmail.com> On Tue, Apr 28, 2009 at 10:41 AM, Dinesh B Vadhia wrote: > This is the error and traceback: > > Unexpected error opening J:/F2/....html: mismatched tag: line 124, column 8 > > Traceback (most recent call last): > ? File "C:\....py", line 492, in > ??? raw = extractText(xhtmlfile) > ? File "C:\....py", line 334, in extractText > ??? tree = make_tree(xhtmlfile) > ? File "....py", line 169, in make_tree > ??? return tree > UnboundLocalError: local variable 'tree' referenced before assignment This is inconsistent. The exception in the stack trace is from a coding error in extractText. It looks like maybe ExtractText is catching exceptions and printing them, and a bug in the exception handling is causing the UnboundLocalError > Here is line 124, col 8 and I cannot see any obvious missing/mismatched > tags: > > "

As to the present time I am unable physical and mentally to secure all > this information at present.

" If you look at a few more lines do you see anything untoward? Perhaps there is a missing before the

, for example? I don't think

is allowed inside every tag. Kent From dineshbvadhia at hotmail.com Tue Apr 28 18:04:15 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Tue, 28 Apr 2009 09:04:15 -0700 Subject: [Tutor] finding mismatched or unpaired html tags In-Reply-To: <1c2a2c590904280817v7a29cbcdm11c88431b949d2aa@mail.gmail.com> References: <1c2a2c590904280713v5e91924fj2ed1d177b321e00e@mail.gmail.com> <1c2a2c590904280817v7a29cbcdm11c88431b949d2aa@mail.gmail.com> Message-ID: Found the mismatched tag on line 94: "

My Name in Nelma Lois Thornton-S.S. No. sjn-yz-yokv/p>" should be: "

My Name in Nelma Lois Thornton-S.S. No. sjn-yz-yokv

" I'll run all the html files through a simple script to identify the mismatches using etree. Thanks. Dinesh From: Kent Johnson Sent: Tuesday, April 28, 2009 8:17 AM To: Dinesh B Vadhia Cc: tutor at python.org Subject: Re: [Tutor] finding mismatched or unpaired html tags On Tue, Apr 28, 2009 at 10:41 AM, Dinesh B Vadhia wrote: > This is the error and traceback: > > Unexpected error opening J:/F2/....html: mismatched tag: line 124, column 8 > > Traceback (most recent call last): > File "C:\....py", line 492, in > raw = extractText(xhtmlfile) > File "C:\....py", line 334, in extractText > tree = make_tree(xhtmlfile) > File "....py", line 169, in make_tree > return tree > UnboundLocalError: local variable 'tree' referenced before assignment This is inconsistent. The exception in the stack trace is from a coding error in extractText. It looks like maybe ExtractText is catching exceptions and printing them, and a bug in the exception handling is causing the UnboundLocalError > Here is line 124, col 8 and I cannot see any obvious missing/mismatched > tags: > > "

As to the present time I am unable physical and mentally to secure all > this information at present.

" If you look at a few more lines do you see anything untoward? Perhaps there is a missing before the

, for example? I don't think

is allowed inside every tag. Kent -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Apr 28 18:20:48 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Apr 2009 17:20:48 +0100 Subject: [Tutor] finding mismatched or unpaired html tags References: Message-ID: "Dinesh B Vadhia" wrote > I'm processing tens of thousands of html files and a few of them contain > mismatched tags and ElementTree throws the error: > > "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: line > 124, column 8" IMHO the best way to cleanse HTML files is to use tidy. It is available for *nix and Windows and has a wealth of options to control it's output. It can even converty html into valid xhtml which ElementTree should be happy with. http://tidy.sourceforge.net/ It may not be Python but it's fast and effective! And there is a Python wrapper: http://utidylib.berlios.de/ although I've never used it. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From stefan_ml at behnel.de Tue Apr 28 19:39:17 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 28 Apr 2009 19:39:17 +0200 Subject: [Tutor] finding mismatched or unpaired html tags In-Reply-To: <49F70843.6030907@tue.nl> References: <49F70843.6030907@tue.nl> Message-ID: A.T.Hofkamp wrote: > Dinesh B Vadhia wrote: >> I'm processing tens of thousands of html files and a few of them >> contain mismatched tags and ElementTree throws the error: >> >> "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: >> line 124, column 8" >> >> I now want to scan each file and simply identify each mismatched or >> unpaired > tags (by line number) in each file. I've read the ElementTree docs and > cannot > see anything obvious how to do this. I know this is a common problem but > feeling a bit clueless here - any ideas? > > Don't use elementTree, use BeautifulSoup instead. Actually, now that the code is there anyway, the OP might be happier with lxml.html. It's a lot faster than BeautifulSoup, uses less memory, and often parses broken HTML better. It's also more user friendly for many HTML tasks. http://codespeak.net/lxml/lxmlhtml.html This might also be worth a read: http://blog.ianbicking.org/2008/12/10/lxml-an-underappreciated-web-scraping-library/ Stefan From david at abbottdavid.com Tue Apr 28 23:43:11 2009 From: david at abbottdavid.com (David) Date: Tue, 28 Apr 2009 17:43:11 -0400 Subject: [Tutor] Add newline's, wrap, a long string Message-ID: <49F7786F.7050305@abbottdavid.com> I am getting information from .txt files and posting them in fields on a web site. I need to break up single strings so they are around 80 characters then a new line because when I enter the info to the form on the website it has fields and it errors out with such a long string. here is a sample of the code; #!/usr/bin/python import subprocess import os u_e = subprocess.Popen( 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\\"-f2', shell=True, stdout=subprocess.PIPE,) os.waitpid(u_e.pid, 0) USE = u_e.stdout.read().strip() L = len(USE) print L print USE L returns 1337 Here is what USE returns; http://linuxcrazy.pastebin.com/m2239816f thanks -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From vinces1979 at gmail.com Tue Apr 28 23:53:48 2009 From: vinces1979 at gmail.com (vince spicer) Date: Tue, 28 Apr 2009 15:53:48 -0600 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: <49F7786F.7050305@abbottdavid.com> References: <49F7786F.7050305@abbottdavid.com> Message-ID: <1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> first, grabbing output from an external command try: import commands USE = commands.getoutput('grep USE /tmp/comprookie2000/emege_info.txt |head -n1|cut -d\\"-f2') then you can wrap strings, import textwrap Lines = textwrap.wrap(USE, 80) # return a list so in short: import commands, textwrap data = textwrap.wrap(commands.getoutput('my command'), 80) Vince On Tue, Apr 28, 2009 at 3:43 PM, David wrote: > I am getting information from .txt files and posting them in fields on a > web site. I need to break up single strings so they are around 80 characters > then a new line because when I enter the info to the form on the website it > has fields and it errors out with such a long string. > > here is a sample of the code; > > #!/usr/bin/python > import subprocess > import os > > u_e = subprocess.Popen( > 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\\"-f2', > shell=True, stdout=subprocess.PIPE,) > os.waitpid(u_e.pid, 0) > USE = u_e.stdout.read().strip() > L = len(USE) > print L > print USE > > L returns 1337 > > Here is what USE returns; > http://linuxcrazy.pastebin.com/m2239816f > > thanks > -david > -- > Powered by Gentoo GNU/Linux > http://linuxcrazy.com > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dineshbvadhia at hotmail.com Wed Apr 29 00:00:12 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Tue, 28 Apr 2009 15:00:12 -0700 Subject: [Tutor] finding mismatched or unpaired html tags Message-ID: Stefan / Alan et al Thank-you for all the advice and links. A simple script using etree is scanning 500K+ xhtml files and 2 files with mismatched files have been found so far which can be fixed manually. I'll definitely look into "tidy" as it sounds pretty cool. Because, we are running data processing programs on a 64-bit Windows box (yes, I know, I know ...) using 64-bit Python we can only use pure Python-only libraries. I believe that lxml uses C libraries. Again, thanks to everyone - a terrific community as usual! -------------------------------------------------------------------------------- Message: 5 Date: Tue, 28 Apr 2009 19:39:17 +0200 From: Stefan Behnel Subject: Re: [Tutor] finding mismatched or unpaired html tags To: tutor at python.org Message-ID: Content-Type: text/plain; charset=ISO-8859-1 A.T.Hofkamp wrote: > Dinesh B Vadhia wrote: >> I'm processing tens of thousands of html files and a few of them >> contain mismatched tags and ElementTree throws the error: >> >> "Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: >> line 124, column 8" >> >> I now want to scan each file and simply identify each mismatched or >> unpaired > tags (by line number) in each file. I've read the ElementTree docs and > cannot > see anything obvious how to do this. I know this is a common problem but > feeling a bit clueless here - any ideas? > > Don't use elementTree, use BeautifulSoup instead. Actually, now that the code is there anyway, the OP might be happier with lxml.html. It's a lot faster than BeautifulSoup, uses less memory, and often parses broken HTML better. It's also more user friendly for many HTML tasks. http://codespeak.net/lxml/lxmlhtml.html This might also be worth a read: http://blog.ianbicking.org/2008/12/10/lxml-an-underappreciated-web-scraping-library/ Stefan -------------- next part -------------- An HTML attachment was scrubbed... URL: From david at abbottdavid.com Wed Apr 29 00:44:41 2009 From: david at abbottdavid.com (David) Date: Tue, 28 Apr 2009 18:44:41 -0400 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: <1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> References: <49F7786F.7050305@abbottdavid.com> <1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> Message-ID: <49F786D9.3020306@abbottdavid.com> vince spicer wrote: > first, grabbing output from an external command try: > > import commands > > USE = commands.getoutput('grep USE /tmp/comprookie2000/emege_info.txt > |head -n1|cut -d\\"-f2') > > then you can wrap strings, > > import textwrap > > Lines = textwrap.wrap(USE, 80) # return a list > > so in short: > > import commands, textwrap > data = textwrap.wrap(commands.getoutput('my command'), 80) > > > > Vince > > > > > > On Tue, Apr 28, 2009 at 3:43 PM, David > wrote: > > I am getting information from .txt files and posting them in fields > on a web site. I need to break up single strings so they are around > 80 characters then a new line because when I enter the info to the > form on the website it has fields and it errors out with such a long > string. > > here is a sample of the code; > > #!/usr/bin/python > import subprocess > import os > > u_e = subprocess.Popen( > 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut > -d\\"-f2', shell=True, stdout=subprocess.PIPE,) > os.waitpid(u_e.pid, 0) > USE = u_e.stdout.read().strip() > L = len(USE) > print L > print USE > > L returns 1337 > > Here is what USE returns; > http://linuxcrazy.pastebin.com/m2239816f > > thanks > -david > -- > Powered by Gentoo GNU/Linux > http://linuxcrazy.com > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > Thanks Vince, I could not get command to work, but I did not try very hard; ["cut: the delimiter must be a single character Try `cut --help' for more", 'information. head: write error: Broken pipe'] But textwrap did the trick, here is what I came up with; #!/usr/bin/python import subprocess import os import textwrap import string def subopen(): u_e = subprocess.Popen( 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\\" -f2', shell=True, stdout=subprocess.PIPE,) os.waitpid(u_e.pid, 0) USE = u_e.stdout.read().strip() L = textwrap.wrap(USE, 80) # return a list Lines = string.join(L, '\n') fname = 'usetest.txt' fobj = open(fname, 'w') fobj.write(Lines) fobj.close subopen() Here is the output; http://linuxcrazy.pastebin.com/m66105e3 -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From ecasbasj at yahoo.es Wed Apr 29 00:51:09 2009 From: ecasbasj at yahoo.es (Emilio Casbas) Date: Tue, 28 Apr 2009 22:51:09 +0000 (GMT) Subject: [Tutor] Regular Expresions instances Message-ID: <802488.17412.qm@web24104.mail.ird.yahoo.com> Hi, following the example from http://docs.python.org/3.0/howto/regex.html If I execute the following code on the python shell (3.1a1): >>> import re >>> p = re.compile('ab*') >>> p I get the msg: <_sre.SRE_Pattern object at 0x013A3440> instead of the msg from the example: Why I get an SRE_Patterns object instead of a RegexObject instance? Regards Emilio From emile at fenx.com Wed Apr 29 01:15:55 2009 From: emile at fenx.com (Emile van Sebille) Date: Tue, 28 Apr 2009 16:15:55 -0700 Subject: [Tutor] Regular Expresions instances In-Reply-To: <802488.17412.qm@web24104.mail.ird.yahoo.com> References: <802488.17412.qm@web24104.mail.ird.yahoo.com> Message-ID: Emilio Casbas wrote: > Hi, > > following the example from > http://docs.python.org/3.0/howto/regex.html ...from version 3.0 docs... > > If I execute the following code on the python shell (3.1a1): > >>>> import re >>>> p = re.compile('ab*') >>>> p > > I get the msg: > <_sre.SRE_Pattern object at 0x013A3440> ... is the same as I get on version 2.5. Coincidence? > > instead of the msg from the example: > > > Why I get an SRE_Patterns object instead of a RegexObject instance? > > Regards > Emilio > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From emile at fenx.com Wed Apr 29 01:24:12 2009 From: emile at fenx.com (Emile van Sebille) Date: Tue, 28 Apr 2009 16:24:12 -0700 Subject: [Tutor] Regular Expresions instances In-Reply-To: References: <802488.17412.qm@web24104.mail.ird.yahoo.com> Message-ID: Emile van Sebille wrote: > Emilio Casbas wrote: >> Hi, >> >> following the example from >> http://docs.python.org/3.0/howto/regex.html > > ...from version 3.0 docs... > >> >> If I execute the following code on the python shell (3.1a1): >> >>>>> import re >>>>> p = re.compile('ab*') >>>>> p >> >> I get the msg: >> <_sre.SRE_Pattern object at 0x013A3440> > > > ... is the same as I get on version 2.5. Coincidence? Nope. I just installed 3.1a2 and it's the same there. Possibly a case of the documentation not keeping up with the release... Emile > >> >> instead of the msg from the example: >> >> >> Why I get an SRE_Patterns object instead of a RegexObject instance? >> >> Regards >> Emilio >> >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > From lie.1296 at gmail.com Wed Apr 29 03:08:56 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 29 Apr 2009 11:08:56 +1000 Subject: [Tutor] finding mismatched or unpaired html tags In-Reply-To: References: Message-ID: Dinesh B Vadhia wrote: > A.T. / Marty > > I'd prefer that the html parser didn't replace the missing tags as I > want to know where and what the problems are. Also, the source html > documents were generated by another computer ie. they are not web page > documents. If the source document was generated by a computer, and it produces invalid markup, shouldn't that be considered a bug in the producing program? Is it possible to fix the producing program instead (i.e. is it under your control)? [Or are you trying to know where the errors are because you're currently debugging it?] From mwalsh at mwalsh.org Wed Apr 29 06:06:59 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Tue, 28 Apr 2009 23:06:59 -0500 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: <49F7786F.7050305@abbottdavid.com> References: <49F7786F.7050305@abbottdavid.com> Message-ID: <49F7D263.9080402@mwalsh.org> David wrote: > I am getting information from .txt files and posting them in fields on a > web site. I need to break up single strings so they are around 80 > characters then a new line because when I enter the info to the form on > the website it has fields and it errors out with such a long string. > > here is a sample of the code; > > #!/usr/bin/python > import subprocess > import os > > u_e = subprocess.Popen( > 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\\"-f2', Did you copy and paste this faithfully? The 'cut -d\\"-f2' looks a bit odd. Is the delimiter a " (double-quote)? Perhaps you left out a space before the -f2? > shell=True, stdout=subprocess.PIPE,) > os.waitpid(u_e.pid, 0) 'u_e.wait()' would wait the way you intend, as well, I believe. > USE = u_e.stdout.read().strip() Or, if you use the communicate() method of the Popen object, the wait is implicit. As in, stdout, stderr = u_e.communicate() ... or perhaps ... USE = u_e.communicate()[0].strip() ... but, you don't need to use subprocess at all. How about (untested), # grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\" -f2 infof = open('/tmp/comprookie2000/emerge_info.txt') for line in infof: if 'USE' in line: USE = line.split('"')[1] break else: USE = '' infof.close() > L = len(USE) > print L > print USE > > L returns 1337 cosmic :) HTH, Marty From mwalsh at mwalsh.org Wed Apr 29 06:22:20 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Tue, 28 Apr 2009 23:22:20 -0500 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: <49F786D9.3020306@abbottdavid.com> References: <49F7786F.7050305@abbottdavid.com> <1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> <49F786D9.3020306@abbottdavid.com> Message-ID: <49F7D5FC.2050400@mwalsh.org> David wrote: > vince spicer wrote: >> first, grabbing output from an external command try: >> >> import commands >> >> USE = commands.getoutput('grep USE /tmp/comprookie2000/emege_info.txt >> |head -n1|cut -d\\"-f2') >> >> then you can wrap strings, >> >> import textwrap >> >> Lines = textwrap.wrap(USE, 80) # return a list >> >> so in short: >> >> import commands, textwrap >> data = textwrap.wrap(commands.getoutput('my command'), 80) >> >> >> >> Vince > Thanks Vince, > I could not get command to work, but I did not try very hard; > ["cut: the delimiter must be a single character Try `cut --help' for > more", 'information. head: write error: Broken pipe'] Ah, I see. This error is most likely due to the typo (missing space before -f2). > > But textwrap did the trick, here is what I came up with; > > #!/usr/bin/python > > import subprocess > import os > import textwrap > import string > > def subopen(): > u_e = subprocess.Popen( > 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut > -d\\" -f2', > shell=True, stdout=subprocess.PIPE,) > os.waitpid(u_e.pid, 0) > USE = u_e.stdout.read().strip() > L = textwrap.wrap(USE, 80) # return a list > Lines = string.join(L, '\n') Just one more comment, string.join is deprecated, yet join is a method of str objects. So ... Lines = '\n'.join(L) ... or use textwrap.fill which returns a string with the newlines already in place ... Lines = textwrap.fill(USE, 80) HTH, Marty > fname = 'usetest.txt' > fobj = open(fname, 'w') > fobj.write(Lines) > fobj.close > > subopen() > > Here is the output; > http://linuxcrazy.pastebin.com/m66105e3 > From jianchun.zhou at gmail.com Wed Apr 29 08:19:13 2009 From: jianchun.zhou at gmail.com (Jianchun Zhou) Date: Wed, 29 Apr 2009 14:19:13 +0800 Subject: [Tutor] Can not run under python 2.6? Message-ID: <2b767f890904282319q770d3412ub2e30625d362c87e@mail.gmail.com> Hi, there: I am new to python, and now I got a trouble: I have an application named canola, it is written under python 2.5, and can run normally under python 2.5 But when it comes under python 2.6, problem up, it says: Traceback (most recent call last): File "/usr/lib/python2.6/site-packages/terra/core/plugin_manager.py", line 151, in _load_plugins classes = plg.load() File "/usr/lib/python2.6/site-packages/terra/core/plugin_manager.py", line 94, in load mod = self._ldr.load() File "/usr/lib/python2.6/site-packages/terra/core/module_loader.py", line 42, in load mod = __import__(modpath, fromlist=[mod_name]) ImportError: Import by filename is not supported. Any body any idea what should I do? -- Best Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From norman at khine.net Wed Apr 29 09:05:22 2009 From: norman at khine.net (Norman Khine) Date: Wed, 29 Apr 2009 09:05:22 +0200 Subject: [Tutor] loops Message-ID: <9c2c8ffb0904290005g2912912eofe8c745b2588c27e@mail.gmail.com> hello, i have the following code: if unique_id is not None: training = self.get_site_root() from training import Training # link back to news item root = context.root formats = ['itinerary', 'news'] for document in formats: results = root.search(format=document, unique_id=unique_id) if results: result = results.get_documents() for item in result: print item.abspath item = self.get_handler(item.abspath) namespace['item_title'] = item.get_property('dc:title') if isinstance(training, Training): namespace['item_url'] = item.abspath else: # Need to strip the '/companies' from the path #namespace['item_url'] = Path(item.abspath)[1:] namespace['item_url'] = Path(item.abspath) else: namespace['item_title'] = None namespace['item_url'] = None is there a way to optimise the loop for 'formats' thanks From alan.gauld at btinternet.com Wed Apr 29 09:35:16 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Apr 2009 08:35:16 +0100 Subject: [Tutor] finding mismatched or unpaired html tags References: Message-ID: "Lie Ryan" wrote >> documents were generated by another computer ie. they are not web page >> documents. > > If the source document was generated by a computer, and it produces > invalid markup, shouldn't that be considered a bug in the producing Elementree parses xml, the source docs are html. Valid html may not be valid xml so the source could be correct even though it doesn't parse properly in elemtree. OTOH you could be right! :-) Alan G. From a.t.hofkamp at tue.nl Wed Apr 29 09:42:44 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Wed, 29 Apr 2009 09:42:44 +0200 Subject: [Tutor] loops In-Reply-To: <9c2c8ffb0904290005g2912912eofe8c745b2588c27e@mail.gmail.com> References: <9c2c8ffb0904290005g2912912eofe8c745b2588c27e@mail.gmail.com> Message-ID: <49F804F4.40300@tue.nl> Norman Khine wrote: > hello, > i have the following code: > > if unique_id is not None: > training = self.get_site_root() > from training import Training > # link back to news item > root = context.root > formats = ['itinerary', 'news'] > for document in formats: > results = root.search(format=document, unique_id=unique_id) > if results: Instead of processing results from each search directly, why not first collect results from all formats, followed by processing of the collected results? ie something like results = [] for doc in formats: results.extend(root.search(....)) for result in results: .... This does not give you better performance. However, it does give you the room to re-organize the searching. In the above, you can now replace the first loop by something like results = root.search_all_formats(formats, unique_id) Does this help? Albert From a.t.hofkamp at tue.nl Wed Apr 29 09:48:17 2009 From: a.t.hofkamp at tue.nl (A.T.Hofkamp) Date: Wed, 29 Apr 2009 09:48:17 +0200 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: <49F7786F.7050305@abbottdavid.com> References: <49F7786F.7050305@abbottdavid.com> Message-ID: <49F80641.4000200@tue.nl> David wrote: > I am getting information from .txt files and posting them in fields on a > web site. I need to break up single strings so they are around 80 > characters then a new line because when I enter the info to the form on > the website it has fields and it errors out with such a long string. > > here is a sample of the code; > > #!/usr/bin/python > import subprocess > import os > > u_e = subprocess.Popen( > 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\\"-f2', > shell=True, stdout=subprocess.PIPE,) > os.waitpid(u_e.pid, 0) > USE = u_e.stdout.read().strip() Why not do the above command sequence entirely in Python instead of this sub-process trickery with its shell quoting problems? It seems a much nicer solution to me. > L = len(USE) > print L > print USE > > L returns 1337 First stop for any problem that you have: The Python standard library. A search told me that Python has a textwrap module in stdlib. Would that be of use to you? Albert From denis.spir at free.fr Wed Apr 29 09:57:56 2009 From: denis.spir at free.fr (spir) Date: Wed, 29 Apr 2009 09:57:56 +0200 Subject: [Tutor] Regular Expresions instances In-Reply-To: <802488.17412.qm@web24104.mail.ird.yahoo.com> References: <802488.17412.qm@web24104.mail.ird.yahoo.com> Message-ID: <20090429095756.464f5b78@o> Le Tue, 28 Apr 2009 22:51:09 +0000 (GMT), Emilio Casbas s'exprima ainsi: > > Hi, > > following the example from > http://docs.python.org/3.0/howto/regex.html > > If I execute the following code on the python shell (3.1a1): > > >>> import re > >>> p = re.compile('ab*') > >>> p > > I get the msg: > <_sre.SRE_Pattern object at 0x013A3440> So do I using python 1.5.2. Slightly better than the version below (at least they tell you you got a %?$?^pattern). > instead of the msg from the example: > I guess this pure question of vocabulary. At a remote point in time the regex package has changed, especially the name of the type for patterns -- and the doc did not follow. > Why I get an SRE_Patterns object instead of a RegexObject instance? Actually, there are loads of lexical issues in the world of programming ;-) What you need to know is, IMO (comments welcome): (1) A pure string is used to describe how source text should be structured. (2) This string is then used to construct an object able to check that. There is a high confusion with terms like 'format', 'pattern', 'rule', 'expression'; and 'regex' itself is used for every other notion, including the *language* used to write strings in (1). I personly use 'pattern' for objects (2) -- but it's far to be a norm. When parsing with regexes, then 'RegexObject' can be a synonym for 'pattern'. On the other hand, you will find all other words, even 'pattern', used in sense (1). > Regards > Emilio Denis ------ la vita e estrany From denis.spir at free.fr Wed Apr 29 10:16:05 2009 From: denis.spir at free.fr (spir) Date: Wed, 29 Apr 2009 10:16:05 +0200 Subject: [Tutor] loops In-Reply-To: <49F804F4.40300@tue.nl> References: <9c2c8ffb0904290005g2912912eofe8c745b2588c27e@mail.gmail.com> <49F804F4.40300@tue.nl> Message-ID: <20090429101605.79fa41c8@o> Le Wed, 29 Apr 2009 09:42:44 +0200, "A.T.Hofkamp" s'exprima ainsi: > Norman Khine wrote: > > hello, > > i have the following code: > > > > if unique_id is not None: > > training = self.get_site_root() > > from training import Training > > # link back to news item > > root = context.root > > formats = ['itinerary', 'news'] > > for document in formats: > > results = root.search(format=document, unique_id=unique_id) > > if results: > > Instead of processing results from each search directly, why not first > collect results from all formats, followed by processing of the collected > results? > > ie something like > > results = [] > for doc in formats: > results.extend(root.search(....)) > > for result in results: > .... > > This does not give you better performance. However, it does give you the > room to re-organize the searching. > > > In the above, you can now replace the first loop by something like > > results = root.search_all_formats(formats, unique_id) > > > Does this help? > > Albert I find the logic far too complicated, as shown by the outline of the code. You can be sure that in most cases (but not all?) the major complications do not lie in the problem, but in the programmer's views on it. So I would try and find other approaches to simplify the algorithm. Also, you can abstract some of the problematic aspects of the process into funcs, that can each cope with a "limited and well-delimited" part of the problem's field. Hope I'm clear and this helps... def foo(...): ....... def bar(...): ....... def bazzz(...): ....... def blurp(...): ....... if blurp(): foo(...) while bazzz(...): bar(...) Denis ------ la vita e estrany From dineshbvadhia at hotmail.com Wed Apr 29 11:23:54 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Wed, 29 Apr 2009 02:23:54 -0700 Subject: [Tutor] finding mismatched or unpaired html tags Message-ID: Lie / Alan re: If the source document was generated by a computer, and it produces invalid markup, shouldn't that be considered a bug in the producing program? Yes, absolutely but we don't have access to the producing program only the produced xhtml files. Dinesh -------------------------------------------------------------------------------- Message: 7 Date: Wed, 29 Apr 2009 08:35:16 +0100 From: "Alan Gauld" Subject: Re: [Tutor] finding mismatched or unpaired html tags To: tutor at python.org Message-ID: Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response "Lie Ryan" wrote >> documents were generated by another computer ie. they are not web page >> documents. > > If the source document was generated by a computer, and it produces > invalid markup, shouldn't that be considered a bug in the producing Elementree parses xml, the source docs are html. Valid html may not be valid xml so the source could be correct even though it doesn't parse properly in elemtree. OTOH you could be right! :-) Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Apr 29 13:02:22 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 29 Apr 2009 07:02:22 -0400 Subject: [Tutor] Can not run under python 2.6? In-Reply-To: <2b767f890904282319q770d3412ub2e30625d362c87e@mail.gmail.com> References: <2b767f890904282319q770d3412ub2e30625d362c87e@mail.gmail.com> Message-ID: <1c2a2c590904290402p3f284ac9g6272de5f19cd4fe@mail.gmail.com> On Wed, Apr 29, 2009 at 2:19 AM, Jianchun Zhou wrote: > Hi, there: > > I am new to python, and now I got a trouble: > > I have an application named canola, it is written under python 2.5, and can > run normally under python 2.5 > > But when it comes under python 2.6, problem up, it says: > > Traceback (most recent call last): > ? File "/usr/lib/python2.6/site- > packages/terra/core/plugin_manager.py", line 151, in _load_plugins > ??? classes = plg.load() > ? File "/usr/lib/python2.6/site-packages/terra/core/plugin_manager.py", line > 94, in load > ??? mod = self._ldr.load() > ? File "/usr/lib/python2.6/site-packages/terra/core/module_loader.py", line > 42, in load > ??? mod = __import__(modpath, fromlist=[mod_name]) > ImportError: Import by filename is not supported. > > Any body any idea what should I do? What's New in Python 2.6 says, "Due to an implementation accident, if you passed a file path to the built-in __import__() function, it would actually import the specified file. This was never intended to work, however, and the implementation now explicitly checks for this case and raises an ImportError." It looks like imp.load_source() may do what you want. Kent From kent37 at tds.net Wed Apr 29 13:06:49 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 29 Apr 2009 07:06:49 -0400 Subject: [Tutor] Regular Expresions instances In-Reply-To: References: <802488.17412.qm@web24104.mail.ird.yahoo.com> Message-ID: <1c2a2c590904290406g469cfc15ud99c009ddfb321be@mail.gmail.com> On Tue, Apr 28, 2009 at 7:24 PM, Emile van Sebille wrote: > Emile van Sebille wrote: > Possibly a case of the documentation not keeping up with the release... It's pretty easy to file a bug report against the docs. Minor fixes like this are often incorporated fairly quickly. See http://docs.python.org/3.0/bugs.html Kent From kent37 at tds.net Wed Apr 29 13:10:04 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 29 Apr 2009 07:10:04 -0400 Subject: [Tutor] loops In-Reply-To: <9c2c8ffb0904290005g2912912eofe8c745b2588c27e@mail.gmail.com> References: <9c2c8ffb0904290005g2912912eofe8c745b2588c27e@mail.gmail.com> Message-ID: <1c2a2c590904290410w634b96bei6ddd8599c4cbe2d5@mail.gmail.com> On Wed, Apr 29, 2009 at 3:05 AM, Norman Khine wrote: > hello, > i have the following code: > > if unique_id is not None: > ? ?training = self.get_site_root() > ? ?from training import Training > ? ?# link back to news item > ? ?root = context.root > ? ?formats = ['itinerary', 'news'] > ? ?for document in formats: > ? ? ? ?results = root.search(format=document, unique_id=unique_id) What is root here? What are the allowed parameters for root.search()? Kent From david at abbottdavid.com Wed Apr 29 13:15:36 2009 From: david at abbottdavid.com (David) Date: Wed, 29 Apr 2009 07:15:36 -0400 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: <49F7D5FC.2050400@mwalsh.org> References: <49F7786F.7050305@abbottdavid.com> <1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> <49F786D9.3020306@abbottdavid.com> <49F7D5FC.2050400@mwalsh.org> Message-ID: <49F836D8.9060207@abbottdavid.com> Martin Walsh wrote: >> ... but, you don't need to use subprocess at all. How about (untested), >> >> # grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\" -f2 >> infof = open('/tmp/comprookie2000/emerge_info.txt') >> for line in infof: >> if 'USE' in line: >> USE = line.split('"')[1] >> break >> else: >> USE = '' >> infof.close() >> Thanks Martin works perfect. So I understand correctly, line.split('"') splits the list on " so [1] grabs after USE = " and up till [2] starts. > > > Just one more comment, string.join is deprecated, yet join is a method > of str objects. So ... > > Lines = '\n'.join(L) > > ... or use textwrap.fill which returns a string with the newlines > already in place ... > > Lines = textwrap.fill(USE, 80) > > HTH, > Marty Again thanks, I like the comments :) -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From lie.1296 at gmail.com Wed Apr 29 14:02:46 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 29 Apr 2009 22:02:46 +1000 Subject: [Tutor] Can not run under python 2.6? In-Reply-To: <2b767f890904282319q770d3412ub2e30625d362c87e@mail.gmail.com> References: <2b767f890904282319q770d3412ub2e30625d362c87e@mail.gmail.com> Message-ID: Jianchun Zhou wrote: > Hi, there: > > I am new to python, and now I got a trouble: > > I have an application named canola, it is written under python 2.5, and > can run normally under python 2.5 > > But when it comes under python 2.6, problem up, it says: > > Traceback (most recent call last): > File "/usr/lib/python2.6/site- > packages/terra/core/plugin_manager.py", line 151, in _load_plugins > classes = plg.load() > File "/usr/lib/python2.6/site-packages/terra/core/plugin_manager.py", > line 94, in load > mod = self._ldr.load() > File "/usr/lib/python2.6/site-packages/terra/core/module_loader.py", > line 42, in load > mod = __import__(modpath, fromlist=[mod_name]) > ImportError: Import by filename is not supported. > > Any body any idea what should I do? > Maybe your problem is related to this: http://mail.python.org/pipermail/python-dev/2008-January/076024.html From lie.1296 at gmail.com Wed Apr 29 14:25:41 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Wed, 29 Apr 2009 22:25:41 +1000 Subject: [Tutor] loops In-Reply-To: <9c2c8ffb0904290005g2912912eofe8c745b2588c27e@mail.gmail.com> References: <9c2c8ffb0904290005g2912912eofe8c745b2588c27e@mail.gmail.com> Message-ID: Norman Khine wrote: > hello, > i have the following code: Uhh... 5 levels deep of nesting, and from the hint that you used self.blah earlier this is inside a class which adds a minimum of two extra levels, in total of 7 levels nesting... We're in deep trouble. > is there a way to optimise the loop for 'formats' In this loop: for item in result: print item.abspath item = self.get_handler(item.abspath) namespace['item_title'] = item.get_property('dc:title') if isinstance(training, Training): namespace['item_url'] = item.abspath else: # Need to strip the '/companies' from the path #namespace['item_url'] = Path(item.abspath)[1:] namespace['item_url'] = Path(item.abspath) You assigned namespace['item_title'] and namespace['item_url'] several times throughout the loop, which means only the last item in result would have any lasting significance to the value of namespace['item_title'] and namespace['item_url']. You may be able to just use result[-1] to directly get the last item. From david at abbottdavid.com Wed Apr 29 14:54:53 2009 From: david at abbottdavid.com (David) Date: Wed, 29 Apr 2009 08:54:53 -0400 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: <49F836D8.9060207@abbottdavid.com> References: <49F7786F.7050305@abbottdavid.com> <1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> <49F786D9.3020306@abbottdavid.com> <49F7D5FC.2050400@mwalsh.org> <49F836D8.9060207@abbottdavid.com> Message-ID: <49F84E1D.40204@abbottdavid.com> David wrote: > Martin Walsh wrote: >> >> Just one more comment, string.join is deprecated, yet join is a method >> of str objects. So ... >> >> Lines = '\n'.join(L) >> >> ... or use textwrap.fill which returns a string with the newlines >> already in place ... >> >> Lines = textwrap.fill(USE, 80) >> >> HTH, >> Marty > > Again thanks, I like the comments :) > -david > Ok here is what I have now; #!/usr/bin/python import textwrap def get_use(): fname = open('/tmp/comprookie2000/emerge_info.txt') for line in fname: if 'USE' in line: output = line.split('"')[1] USE = textwrap.fill(output, 80) break else: USE = '' fname.close() get_use() Here is the whole program so far, what it does is it logs into a druple web site and posts. I would like to make it better, as you can see I do the same thing over and over. Also I don't think this is a python problem but I can only post one line or I get HTTP/1.1 417 Expectation Failed. I thought the problem was because it was one long string, but that is solved, may be it is looking for return/Enter to be pressed, not sure. http://linuxcrazy.pastebin.com/m7689c088 thanks -david -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From sander.sweers at gmail.com Wed Apr 29 15:28:16 2009 From: sander.sweers at gmail.com (Sander Sweers) Date: Wed, 29 Apr 2009 15:28:16 +0200 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: <49F84E1D.40204@abbottdavid.com> References: <49F7786F.7050305@abbottdavid.com> <1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> <49F786D9.3020306@abbottdavid.com> <49F7D5FC.2050400@mwalsh.org> <49F836D8.9060207@abbottdavid.com> <49F84E1D.40204@abbottdavid.com> Message-ID: 2009/4/29 David : > Here is the whole program so far, what it does is it logs into a druple web > site and posts. I would like to make it better, as you can see I do the same > thing over and over. > > http://linuxcrazy.pastebin.com/m7689c088 What you can do is define all the variables upfront. This way you can get rid of the else. Below is an example how you can do this with only looping once over the fle. CBUILD = '' ACCEPT_KEYWORDS = '' for line in fname: if 'ACCEPT_KEYWORDS' in line: output = line.split('"')[1] ACCEPT_KEYWORDS = textwrap.fill(output, 80) if 'CBUILD' in line: output = line.split('"')[1] CBUILD = textwrap.fill(output, 80) etc etc Greets Sander From mwalsh at mwalsh.org Wed Apr 29 15:30:40 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Wed, 29 Apr 2009 08:30:40 -0500 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: <49F836D8.9060207@abbottdavid.com> References: <49F7786F.7050305@abbottdavid.com> <1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> <49F786D9.3020306@abbottdavid.com> <49F7D5FC.2050400@mwalsh.org> <49F836D8.9060207@abbottdavid.com> Message-ID: <49F85680.6040509@mwalsh.org> David wrote: > Martin Walsh wrote: >>> ... but, you don't need to use subprocess at all. How about (untested), >>> >>> # grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\" -f2 >>> infof = open('/tmp/comprookie2000/emerge_info.txt') >>> for line in infof: >>> if 'USE' in line: >>> USE = line.split('"')[1] >>> break >>> else: >>> USE = '' >>> infof.close() >>> > > Thanks Martin works perfect. So I understand correctly, line.split('"') > splits the list on " so [1] grabs after USE = " and up till [2] starts. That's right, in other words it emulates the behavior of 'cut -d\" -f2'. Python having 0-based indexing, you use 1 instead of 2, etc. > Again thanks, I like the comments :) > -david > Welcome, glad it helped. Marty From mwalsh at mwalsh.org Wed Apr 29 15:37:39 2009 From: mwalsh at mwalsh.org (Martin Walsh) Date: Wed, 29 Apr 2009 08:37:39 -0500 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: References: <49F7786F.7050305@abbottdavid.com> <1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> <49F786D9.3020306@abbottdavid.com> <49F7D5FC.2050400@mwalsh.org> <49F836D8.9060207@abbottdavid.com> <49F84E1D.40204@abbottdavid.com> Message-ID: <49F85823.2070307@mwalsh.org> Sander Sweers wrote: > 2009/4/29 David : >> Here is the whole program so far, what it does is it logs into a druple web >> site and posts. I would like to make it better, as you can see I do the same >> thing over and over. >> >> http://linuxcrazy.pastebin.com/m7689c088 > > What you can do is define all the variables upfront. This way you can > get rid of the else. Below is an example how you can do this with only > looping once over the fle. > > CBUILD = '' > ACCEPT_KEYWORDS = '' > > for line in fname: > if 'ACCEPT_KEYWORDS' in line: > output = line.split('"')[1] > ACCEPT_KEYWORDS = textwrap.fill(output, 80) > > if 'CBUILD' in line: > output = line.split('"')[1] > CBUILD = textwrap.fill(output, 80) What is particularly important to note here is that Sander is iterating over the file 'fname' only once. When you loop thru the lines of a file with this form it is consumed, and you get nothing back for subsequent attempts, unless you re-open the file, or explicitly move back to the beginning, ie. fname.seek(0). HTH, Marty > > etc etc > > Greets > Sander > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From ecasbasj at yahoo.es Wed Apr 29 15:42:27 2009 From: ecasbasj at yahoo.es (Emilio Casbas) Date: Wed, 29 Apr 2009 13:42:27 +0000 (GMT) Subject: [Tutor] Regular Expresions instances In-Reply-To: <1c2a2c590904290406g469cfc15ud99c009ddfb321be@mail.gmail.com> References: <802488.17412.qm@web24104.mail.ird.yahoo.com> <1c2a2c590904290406g469cfc15ud99c009ddfb321be@mail.gmail.com> Message-ID: <371151.24668.qm@web24107.mail.ird.yahoo.com> Thanks to all for the responses. I've filled a bug report as you pointed out. http://bugs.python.org/msg86803 Thanks Emilio ----- Mensaje original ---- > De: Kent Johnson > Para: Emile van Sebille > CC: tutor at python.org > Enviado: mi?rcoles, 29 de abril, 2009 13:06:49 > Asunto: Re: [Tutor] Regular Expresions instances > > On Tue, Apr 28, 2009 at 7:24 PM, Emile van Sebille wrote: > > Emile van Sebille wrote: > > > Possibly a case of the documentation not keeping up with the release... > > It's pretty easy to file a bug report against the docs. Minor fixes > like this are often incorporated fairly quickly. See > http://docs.python.org/3.0/bugs.html > > Kent > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor From denis.spir at free.fr Wed Apr 29 15:56:08 2009 From: denis.spir at free.fr (spir) Date: Wed, 29 Apr 2009 15:56:08 +0200 Subject: [Tutor] imported scope Message-ID: <20090429155608.1f4090e8@o> Hello, I have an issue with scopes (namespaces). Say I have a source file 'test.py' with the single line: print vars() # same with locals() Now, from the command-line: spir at o:~/prog/pijnu$ python test.py {'__builtins__': , '__name__': '__main__', '__file__': 'test.py', '__doc__': None} Right, but when imported (my question is at the bottom of the output ;-): spir at o:~/prog/pijnu$ python Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import test {'__builtins__': {'IndexError': , 'all': , 'vars': , 'SyntaxError': , 'unicode': , [... here 5918 characters ...] 'AttributeError': , 'OverflowError': }, '__name__': 'test', '__file__': 'test.pyc', '__doc__': None} >>> Can someone explain? Denis ------ la vita e estrany From kent37 at tds.net Wed Apr 29 16:05:04 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 29 Apr 2009 10:05:04 -0400 Subject: [Tutor] imported scope In-Reply-To: <20090429155608.1f4090e8@o> References: <20090429155608.1f4090e8@o> Message-ID: <1c2a2c590904290705w187df2e2je2eddb364bc7dbd4@mail.gmail.com> On Wed, Apr 29, 2009 at 9:56 AM, spir wrote: > Hello, > > I have an issue with scopes (namespaces). > Say I have a source file 'test.py' with the single line: > ? print vars() ? ? ? ? # same with locals() > > Now, from the command-line: > > spir at o:~/prog/pijnu$ python test.py > {'__builtins__': , '__name__': '__main__', '__file__': 'test.py', '__doc__': None} > > Right, but when imported (my question is at the bottom of the output ;-): > > spir at o:~/prog/pijnu$ python > Python 2.5.2 (r252:60911, Oct ?5 2008, 19:24:49) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> import test > {'__builtins__': {'IndexError': , 'all': , 'vars': , 'SyntaxError': , 'unicode': , > [... here 5918 characters ...] > 'AttributeError': , 'OverflowError': }, '__name__': 'test', '__file__': 'test.pyc', '__doc__': None} >>>> > > Can someone explain? There is some discussion here: http://mail.python.org/pipermail/python-3000/2007-March/006161.html Short answer: __builtins__ is an implementation detail that you should ignore. Kent From eike.welk at gmx.net Wed Apr 29 17:44:54 2009 From: eike.welk at gmx.net (Eike Welk) Date: Wed, 29 Apr 2009 17:44:54 +0200 Subject: [Tutor] testing framework In-Reply-To: <20090423145559.3a3e7338@o> References: <20090423145559.3a3e7338@o> Message-ID: <200904291744.55205.eike.welk@gmx.net> Hello Spir! On Thursday 23 April 2009, spir wrote: > I would like to refactor tests of an application in a consistent > form. I have a ton of tests for each module, but they are all ad > hoc things. doctest looks great. The issue is it seems based on > command line-like testing: I have researched a workable solution based on the "py.test" framework. http://codespeak.net/py/dist/test/test.html (The nose framework works in a very similar way http://code.google.com/p/python-nose/) I think doctests are only for small examples that illustrate the docstrings. The "unittest" library has problems with library modules that use each other; one has to tweak "sys.path" prior to testing. (Doctests probably have this problem with "sys.path" too.) To use the "py.test" script you put all your test code into separate files. The filenames must start with "test_". These files contain test functions; their names must must also start with "test_". The "py.test" script searches for test files and uses a lot of magic to do the right thing, especially it fiddles with "sys.path" Here is your example done with "py.test": -------- start file "test_example.py" ------------ def test_factorial(): import example #The import my also fail fact5 = example.factorial(5) assert fact5 == 120 -------- end file -------------------------------- "py.test" can also execute tests written with the "unittest" library (important for me). The test files are then structured like this: -------- start file "test_unittest_example.py" ------------ import unittest pytest_plugins = "pytest_unittest" class TestExample(unittest.TestCase): #your existing unittest implementation goes here pass -------- end file ----------------------------------------- "py.test" writes nicely formated stack traces, and code snippets into the terminal window. On Linux at least, it even uses a bit of color and bold text. If you are spoiled by graphical debuggers and clickable error messages (like me), you can also use the test files for debugging with your IDE. You put something like this at the end of the test file: if __name__ == '__main__': test_factorial() #This test triggers the error So, the simplicity of its main concepts makes "py.test" quite convenient. Kind regards, Eike. From dineshbvadhia at hotmail.com Wed Apr 29 17:55:13 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Wed, 29 Apr 2009 08:55:13 -0700 Subject: [Tutor] Dictionary, integer, compression Message-ID: This could be a question for the comp.lang.python list but I'll try here first: Say, you have a dictionary of integers, are the integers stored in a compressed integer format or as integers ie. are integers encoded before being stored in the dictionary and then decoded when read? Dinesh -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Apr 29 18:32:57 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 29 Apr 2009 12:32:57 -0400 Subject: [Tutor] imported scope In-Reply-To: <20090429165437.658e76e8@o> References: <20090429155608.1f4090e8@o> <1c2a2c590904290705w187df2e2je2eddb364bc7dbd4@mail.gmail.com> <20090429165437.658e76e8@o> Message-ID: <1c2a2c590904290932v38539753mc600b404cafb28d9@mail.gmail.com> On Wed, Apr 29, 2009 at 10:54 AM, spir wrote: > Le Wed, 29 Apr 2009 10:05:04 -0400, > Kent Johnson s'exprima ainsi: >> There is some discussion here: >> http://mail.python.org/pipermail/python-3000/2007-March/006161.html >> >> Short answer: __builtins__ is an implementation detail that you should >> ignore. >> >> Kent >> > > Right, thank you, Kent. > The issue here is I cannot ignore __buitins__ (or __builtin__). Or maybe I don't understand your answer properly. > > Yop, sorry, I get it now. It's the same dict in both cases -- except __buitins__ is expanded in the second case... Not obvious! (Is that what you mean?) What are you trying to do? Why can't you ignore __builtins__? In your first example __builtins__ is a reference to a module. In the second example it is the module's __dict__. The email thread I linked to indicates that this is most likely a historical accident or oversight. Kent From alan.gauld at btinternet.com Wed Apr 29 18:35:53 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Apr 2009 17:35:53 +0100 Subject: [Tutor] Dictionary, integer, compression References: Message-ID: "Dinesh B Vadhia" wrote > Say, you have a dictionary of integers, are the integers stored > in a compressed integer format or as integers ie. are integers > encoded before being stored in the dictionary and then > decoded when read? I can't think of any reason to compress them, I imagine they are stored as integers. But given the way Python handlers integers with arbitrarily long numbers etc it may well be more complex than a simple integer (ie 4 byte number). But any form of compression would be likely to hit performamce so I doubt that they would be compressed. Is there anything that made you think they might be? HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From stefan_ml at behnel.de Wed Apr 29 18:36:19 2009 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 29 Apr 2009 18:36:19 +0200 Subject: [Tutor] Dictionary, integer, compression In-Reply-To: References: Message-ID: Dinesh B Vadhia wrote: > Say, you have a dictionary of integers, are the integers stored in a > compressed integer format or as integers ie. are integers encoded before > being stored in the dictionary and then decoded when read? Integer objects are not special cased in dictionaries. They are stored as normal int/long objects. Dictionaries do not use any kind of compression in Python. Stefan From alan.gauld at btinternet.com Wed Apr 29 18:44:03 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Apr 2009 17:44:03 +0100 Subject: [Tutor] Add newline's, wrap, a long string References: <49F7786F.7050305@abbottdavid.com><1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> <49F786D9.3020306@abbottdavid.com> <49F7D5FC.2050400@mwalsh.org> <49F836D8.9060207@abbottdavid.com> <49F84E1D.40204@abbottdavid.com> Message-ID: "Sander Sweers" wrote > What you can do is define all the variables upfront. This way you can > get rid of the else. Below is an example how you can do this with only > looping once over the fle. And you can put the variables as keys of a dictionary and avoid all the if tests: data = {'CBUILD':None, 'ACCEPT_KEYWORDS: None, .... } > for line in fname: for keyphrase in data: > if keyphrase in line: > output = line.split('"')[1] > data[keyphrase] = textwrap.fill(output, 80) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From david at abbottdavid.com Wed Apr 29 20:05:30 2009 From: david at abbottdavid.com (David) Date: Wed, 29 Apr 2009 14:05:30 -0400 Subject: [Tutor] Add newline's, wrap, a long string In-Reply-To: References: <49F7786F.7050305@abbottdavid.com><1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> <49F786D9.3020306@abbottdavid.com> <49F7D5FC.2050400@mwalsh.org> <49F836D8.9060207@abbottdavid.com> <49F84E1D.40204@abbottdavid.com> Message-ID: <49F896EA.1030705@abbottdavid.com> Alan Gauld wrote: > > "Sander Sweers" wrote > >> What you can do is define all the variables upfront. This way you can >> get rid of the else. Below is an example how you can do this with only >> looping once over the fle. > > And you can put the variables as keys of a dictionary and avoid all the > if tests: > > data = {'CBUILD':None, 'ACCEPT_KEYWORDS: None, > .... > } > >> for line in fname: > for keyphrase in data: >> if keyphrase in line: >> output = line.split('"')[1] >> data[keyphrase] = textwrap.fill(output, 80) > > I don't know how else to describe it, that is so cool :) [code] #!/usr/bin/python import textwrap data = {'CBUILD':None, 'CFLAGS':None, 'MAKEOPTS':None} def get_use(): fname = open('/tmp/comprookie2000/emerge_info.txt') for line in fname: for keyphrase in data: if keyphrase in line: output = line.split('"')[1] data[keyphrase] = textwrap.fill(output, 80) fname.close() get_use() CBUILD = data['CBUILD'] CFLAGS = data['CFLAGS'] MAKEOPTS = data['MAKEOPTS'] print 'CBUILD =',CBUILD print 'CFLAGS =',CFLAGS print 'MAKEOPTS =',MAKEOPTS [output] CBUILD = x86_64-pc-linux-gnu CFLAGS = -march=opteron -O2 -pipe MAKEOPTS = -j3 Thanks Alan Martin, Sander, hope I did it correctly. -- Powered by Gentoo GNU/Linux http://linuxcrazy.com From denis.spir at free.fr Wed Apr 29 20:23:51 2009 From: denis.spir at free.fr (spir) Date: Wed, 29 Apr 2009 20:23:51 +0200 Subject: [Tutor] Fw: imported scope Message-ID: <20090429202351.09f71627@o> Le Wed, 29 Apr 2009 12:32:57 -0400, Kent Johnson s'exprima ainsi: > On Wed, Apr 29, 2009 at 10:54 AM, spir wrote: > > Le Wed, 29 Apr 2009 10:05:04 -0400, > > Kent Johnson s'exprima ainsi: > >> There is some discussion here: > >> http://mail.python.org/pipermail/python-3000/2007-March/006161.html > >> > >> Short answer: __builtins__ is an implementation detail that you should > >> ignore. > >> > >> Kent > >> > > > > Right, thank you, Kent. > > The issue here is I cannot ignore __buitins__ (or __builtin__). Or maybe > > I don't understand your answer properly. > > > > Yop, sorry, I get it now. It's the same dict in both cases -- except > > __buitins__ is expanded in the second case... Not obvious! (Is that what > > you mean?) > > What are you trying to do? Why can't you ignore __builtins__? Actually, I don't mind of __builtins__, but of vars() or locals(). The issue is that __builtins__ were monstruously ;-) obfuscating a scope dict I needed. In fact, I printed it out to check it contains what I want; when I saw all that stuff there I thought there was something wrong in my method. Actually the dict contains what I need, and only 4 elements. Simply one of them is veeery BIG. For the sake of information, the reason for this is I need a kind of dynamic scoping, i.e. getting the vars from the caller scope; while python is statically scoped. I cannot find any other way than passing e.g. vars(). Hints welcome. By the way, I asked previously about the difference between vars() and locals() (couldn't find a situation where they differ). Still have no idea, and there seems to be nothing about that in docs. So why both? [...] > Kent Denis ------ la vita e estrany ------ la vita e estrany From dineshbvadhia at hotmail.com Wed Apr 29 20:36:22 2009 From: dineshbvadhia at hotmail.com (Dinesh B Vadhia) Date: Wed, 29 Apr 2009 11:36:22 -0700 Subject: [Tutor] Dictionary, integer, compression Message-ID: Alan I want to perform test runs on my local machine with very large numbers of integers stored in a dictionary. As the Python dictionary is an built-in function I thought that for very large dictionaries there could be compression. Done correctly, integer compression wouldn't affect performance but could enhance it. Weird, I know! I'll check in with the comp.lang.python lot. Dinesh -------------------------------------------------------------------------------- Message: 3 Date: Wed, 29 Apr 2009 17:35:53 +0100 From: "Alan Gauld" Subject: Re: [Tutor] Dictionary, integer, compression To: tutor at python.org Message-ID: Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original "Dinesh B Vadhia" wrote > Say, you have a dictionary of integers, are the integers stored > in a compressed integer format or as integers ie. are integers > encoded before being stored in the dictionary and then > decoded when read? I can't think of any reason to compress them, I imagine they are stored as integers. But given the way Python handlers integers with arbitrarily long numbers etc it may well be more complex than a simple integer (ie 4 byte number). But any form of compression would be likely to hit performamce so I doubt that they would be compressed. Is there anything that made you think they might be? HTH -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From dcml1 at crouse.us Wed Apr 29 20:42:41 2009 From: dcml1 at crouse.us (Dave Crouse) Date: Wed, 29 Apr 2009 13:42:41 -0500 Subject: [Tutor] Printing Problem python 3 Message-ID: <596da3790904291142g3dcf2e8fq55275ac366eb4d08@mail.gmail.com> Trying to print something with a { in it. Probably extremely simple, but it's frustrating me. :( print ('The \"This is a test \" {') i get this error ValueError: Single '{' encountered in format string -------------- next part -------------- An HTML attachment was scrubbed... URL: From sudhanshu9252 at gmail.com Wed Apr 29 21:03:06 2009 From: sudhanshu9252 at gmail.com (sudhanshu gautam) Date: Thu, 30 Apr 2009 00:33:06 +0530 Subject: [Tutor] How to Extend a class in another file in same directory Message-ID: <6f9dbdf40904291203qe8ed910s97435272de519bd1@mail.gmail.com> I made a program named as a title.py inside it used Inheritance , now I want to Extend the Parent Class in another program(file also) but having an Error lets check ,my code 1.first file title .py class global Studentdatabase: def __init__(self,name,age,standard,place,dateofbirth,sex): self.name=name self.age=age self.standard=standard self.place=place self.dateofbirth=dateofbirth self.sex=sex print 'Initalizing',self.name def tell(self): 'TELL DETAILS' print 'NAME,AGE,STANDARD,PLACE,DATEOFBIRTH,SEX',self.name ,self.age,self.standard,self.place,self.dateofbirth,self.sex class Teacher(Studentdatabase): def __init__(self,name,age,standard,place,dateofbirth,sex,salary): Studentdatabase.__init__(self,name,age,standard,place,dateofbirth,sex) self.salary=salary print 'name is ',self.name def tell(self): Studentdatabase.tell(self) print 'The salary is',self.salary class Student(Studentdatabase): def __init__(self,name,age,standard,place,dateofbirth,sex,marks): Studentdatabase.__init__(self,name,age,standard,place,dateofbirth,sex) self.marks=marks print 'The name',self.name def tell(self): Studentdatabase.tell(self) print 'The marks is',self.marks t=Teacher('Mr.sudhanshu',21,'B.E','Jaipur','12/02/1986','MALE',21000) s=Student('Anirudh',19,'Engineering','Delhi','12/3/1989','MALE',65) members=[t,s] for member in members: member.tell() 2.Now another file in which I want to extend that same parent class is here .First code running successfully but in another one having problem 'This another source code will represent the new Entry of database' import title class Newstudent(Studentdatabase): def __init__(self,name,age,standard,place,dateofbirth,sex,marks): Studentdatabase.__init__(self,name,age,standard,place,dateofbirth,sex) self.marks=marks print 'The new data base has name of new students',self.name def tell(self): Studentdatabase.tell(self) print 'The marks of the student is',self.marks s1=Newstudent('Rajiv',21,'M.B.A','MUMBAI','12 JAN,1987','MALE',267) s2=Newstudent('VIKASH',22,'M.B.A','DELHI','12 JAN 1985','MALE',234) s3=Newstudent('SAURAV',23,'B.TECH','BIHAR','12 JAN 1984','MALE',233) new=[s1,s2,s3] for newstudent in new: newstudent.tell() Now tell me that how I can extend it regards sudhanshu :) -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Wed Apr 29 21:03:13 2009 From: kent37 at tds.net (Kent Johnson) Date: Wed, 29 Apr 2009 15:03:13 -0400 Subject: [Tutor] Printing Problem python 3 In-Reply-To: <596da3790904291142g3dcf2e8fq55275ac366eb4d08@mail.gmail.com> References: <596da3790904291142g3dcf2e8fq55275ac366eb4d08@mail.gmail.com> Message-ID: <1c2a2c590904291203h1f24e2cfgf898d13490daebd4@mail.gmail.com> On Wed, Apr 29, 2009 at 2:42 PM, Dave Crouse wrote: > Trying to print something with a { in it. > Probably extremely simple, but it's frustrating me.? :( > > print ('The \"This is a test \" {') > > i get this error > > ValueError: Single '{' encountered in format string It works for me: Python 3.0.1 (r301:69561, Feb 13 2009, 20:04:18) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print ('The \"This is a test \" {') The "This is a test " { I guess you have redefined print() to use the latest style of string formatting: http://www.python.org/dev/peps/pep-3101/ Try using double braces {{ Kent From dcml1 at crouse.us Wed Apr 29 21:44:43 2009 From: dcml1 at crouse.us (Dave Crouse) Date: Wed, 29 Apr 2009 14:44:43 -0500 Subject: [Tutor] Printing Problem python 3 In-Reply-To: <1c2a2c590904291203h1f24e2cfgf898d13490daebd4@mail.gmail.com> References: <596da3790904291142g3dcf2e8fq55275ac366eb4d08@mail.gmail.com> <1c2a2c590904291203h1f24e2cfgf898d13490daebd4@mail.gmail.com> Message-ID: <596da3790904291244o1ed1c478xd612a80057151b38@mail.gmail.com> The double {{ }} worked perfectly, THANK YOU ! :) This was driving me crazy. On Wed, Apr 29, 2009 at 2:03 PM, Kent Johnson wrote: > print ('The \"This is a test \" {') > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sierra_mtnview at sbcglobal.net Wed Apr 29 21:48:30 2009 From: sierra_mtnview at sbcglobal.net (Wayne Watson) Date: Wed, 29 Apr 2009 12:48:30 -0700 Subject: [Tutor] Conventional Y Axis Label in pylab plot Message-ID: <49F8AF0E.6060500@sbcglobal.net> I'd like to label the plot axes as x and y. All I see is xlabel and ylabel. ylabel puts y on its side, rotated 90 degrees from the horizontal. Is there way to put it in a normal reading position? -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39? 15' 7" N, 121? 2' 32" W, 2700 feet All the neutrons, and protons in the human body occupy a cube whose side is 5.52*10**-6 meters (tiny!). That adds up to a 150 pound person. It's not a surprise that we are mostly space. (Calculation by WTW) From =?UTF-8?Q?=22Shantanoo_Mahajan_=28=E0=A4=B6=E0=A4=82=E0=A4=A4?= Wed Apr 29 22:15:55 2009 From: =?UTF-8?Q?=22Shantanoo_Mahajan_=28=E0=A4=B6=E0=A4=82=E0=A4=A4?= (=?UTF-8?Q?=22Shantanoo_Mahajan_=28=E0=A4=B6=E0=A4=82=E0=A4=A4?=) Date: Thu, 30 Apr 2009 01:45:55 +0530 Subject: [Tutor] Printing Problem python 3 In-Reply-To: <596da3790904291142g3dcf2e8fq55275ac366eb4d08@mail.gmail.com> References: <596da3790904291142g3dcf2e8fq55275ac366eb4d08@mail.gmail.com> Message-ID: <2E184C4E-C9A8-4486-956D-F17CA43D0975@gmail.com> On 30-Apr-09, at 12:12 AM, Dave Crouse wrote: > Trying to print something with a { in it. > Probably extremely simple, but it's frustrating me. :( > > print ('The \"This is a test \" {') > > i get this error > > ValueError: Single '{' encountered in format string Worked perfectly for me. === $ python3.0 Python 3.0 (r30:67503, Jan 14 2009, 09:13:26) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> print ('The \"This is a test \" {') The "This is a test " { >>> === - shantanoo From dcml1 at crouse.us Wed Apr 29 23:15:10 2009 From: dcml1 at crouse.us (Dave Crouse) Date: Wed, 29 Apr 2009 16:15:10 -0500 Subject: [Tutor] Printing Problem python 3 In-Reply-To: <2E184C4E-C9A8-4486-956D-F17CA43D0975@gmail.com> References: <596da3790904291142g3dcf2e8fq55275ac366eb4d08@mail.gmail.com> <2E184C4E-C9A8-4486-956D-F17CA43D0975@gmail.com> Message-ID: <596da3790904291415w314b4e53rd15f065d6385dcd3@mail.gmail.com> I got the same thing with idle, but when running as a script, it's not the same, it errors. I tried it on Windows and Linux. ------------------------------------------- [davec at Arch64 Python]$ less test.py #/usr/bin/python3 print ('The \"This is a test \" {') [davec at Arch64 Python]$ sh test.py test.py: line 3: syntax error near unexpected token `'The \"This is a test \" {'' test.py: line 3: `print ('The \"This is a test \" {')' [davec at Arch64 Python]$ python3 Python 3.0.1 (r301:69556, Feb 22 2009, 14:12:04) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print ('The \"This is a test \" {') The "This is a test " { >>> ------------------------------------------- However the double quotes was exactly what the doctor ordered ! :) 2009/4/29 "Shantanoo Mahajan (????? ?????)" : > print ('The \"This is a test \" {') From davea at ieee.org Wed Apr 29 23:47:09 2009 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Apr 2009 17:47:09 -0400 Subject: [Tutor] How to Extend a class in another file in same directory In-Reply-To: References: Message-ID: <49F8CADD.2080600@ieee.org> sudhanshu gautam wrote: > > > 2.Now another file in which I want to extend that same parent class is here > .First code running successfully but in another one having problem > > > 'This another source code will represent the new Entry of database' > import title > class Newstudent(Studentdatabase): > def __init__(self,name,age,standard,place,dateofbirth,sex,marks): > > Studentdatabase.__init__(self,name,age,standard,place,dateofbirth,sex) > self.marks=marks > print 'The new data base has name of new students',self.name > def tell(self): > Studentdatabase.tell(self) > print 'The marks of the student is',self.marks > > s1=Newstudent('Rajiv',21,'M.B.A','MUMBAI','12 JAN,1987','MALE',267) > s2=Newstudent('VIKASH',22,'M.B.A','DELHI','12 JAN 1985','MALE',234) > s3=Newstudent('SAURAV',23,'B.TECH','BIHAR','12 JAN 1984','MALE',233) > new=[s1,s2,s3] > for newstudent in new: > newstudent.tell() > > > > Now tell me that how I can extend it > You should tell the error message that you get, not just say "an error". So we must guess. If you want to refer to a name (Studentdatabase) in another module, you must import that module. Assuming some things about your sys.path, that could be as simple as import title This you did correctly. Then referencing the name inside that other module is done by using class Newstudent(title.Studentdatabase): Note there is another choice, suitable if there's really only a single name you want to import: from title import Studentdatabase That way, the name can be used without a prefix. I usually prefer the first form, but the second has definite uses, especially if the import is of a module several levels down in a package hierarchy. From alan.gauld at btinternet.com Thu Apr 30 01:58:26 2009 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 30 Apr 2009 00:58:26 +0100 Subject: [Tutor] Add newline's, wrap, a long string References: <49F7786F.7050305@abbottdavid.com><1e53c510904281453q3e49a468p3f9def7ce33b6c19@mail.gmail.com> <49F786D9.3020306@abbottdavid.com><49F7D5FC.2050400@mwalsh.org> <49F836D8.9060207@abbottdavid.com><49F84E1D.40204@abbottdavid.com> <49F896EA.1030705@abbottdavid.com> Message-ID: "David" wrote > I don't know how else to describe it, that is so cool :) :-) Couple of comments below. > import textwrap > data = {'CBUILD':None, 'CFLAGS':None, 'MAKEOPTS':None} > def get_use(): > fname = open('/tmp/comprookie2000/emerge_info.txt') > for line in fname: > for keyphrase in data: > if keyphrase in line: > output = line.split('"')[1] > data[keyphrase] = textwrap.fill(output, 80) > fname.close() > > get_use() It would be better practice to put data inside the function and then return the result. Also I'd pass the filename into the function. def get_use(fname): data = .... for line in open(fname): .... return data data = get_use('/tmp/foo/bar/baz.txt') That makes the function, and therefore the module, more easily reusable and allows you to call the function several times on different files wityhout overwriting the results. You may not need to do that now but it might be useful in the future. > CBUILD = data['CBUILD'] > CFLAGS = data['CFLAGS'] > MAKEOPTS = data['MAKEOPTS'] Don't bother storing in variables just use the dictionary directly in your print statements. Learn to love and use Python's collections. They are the secret of much of Python's power. Just some thoughts, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From vceder at canterburyschool.org Thu Apr 30 04:34:21 2009 From: vceder at canterburyschool.org (Vern Ceder) Date: Wed, 29 Apr 2009 22:34:21 -0400 Subject: [Tutor] Printing Problem python 3 In-Reply-To: <596da3790904291415w314b4e53rd15f065d6385dcd3@mail.gmail.com> References: <596da3790904291142g3dcf2e8fq55275ac366eb4d08@mail.gmail.com> <2E184C4E-C9A8-4486-956D-F17CA43D0975@gmail.com> <596da3790904291415w314b4e53rd15f065d6385dcd3@mail.gmail.com> Message-ID: <49F90E2D.5050503@canterburyschool.org> Dave Crouse wrote: > I got the same thing with idle, but when running as a script, it's not > the same, it errors. I tried it on Windows and Linux. > > ------------------------------------------- > > [davec at Arch64 Python]$ less test.py > #/usr/bin/python3 > print ('The \"This is a test \" {') > > [davec at Arch64 Python]$ sh test.py > test.py: line 3: syntax error near unexpected token `'The \"This is a > test \" {'' > test.py: line 3: `print ('The \"This is a test \" {')' Maybe I'm missing something, but this error is because you're running a Python script using the Linux shell, probably bash as the interpreter instead of Python. Also, if you're running it on its own as a script, you'd want to add a '!' after the '#' - otherwise it's just a comment, not setting the interpreter. To run a Python file as a script, you'd need to do: [davec at Arch64 Python]$ python3 test.py (or just ./test.py if it's executable and the interpreter is set using '#!/usr/bin/python3') When I do that, it works for me just fine without doubling the '{' Cheers, Vern -- This time for sure! -Bullwinkle J. Moose ----------------------------- Vern Ceder, Director of Technology Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804 vceder at canterburyschool.org; 260-436-0746; FAX: 260-436-5137 From mobiledreamers at gmail.com Thu Apr 30 08:31:42 2009 From: mobiledreamers at gmail.com (mobiledreamers at gmail.com) Date: Wed, 29 Apr 2009 23:31:42 -0700 Subject: [Tutor] is it possible to traverse two lists simulatenously using python Message-ID: Python for i,j in topgirls, richgirls: print i,j Cheetah #for $i,$j in $topgirls, $richgirls$i, $j #end for This doesnt work -- Bidegg worlds best auction site http://bidegg.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From kent37 at tds.net Thu Apr 30 12:20:40 2009 From: kent37 at tds.net (Kent Johnson) Date: Thu, 30 Apr 2009 06:20:40 -0400 Subject: [Tutor] is it possible to traverse two lists simulatenously using python In-Reply-To: References: Message-ID: <1c2a2c590904300320k6b9714e5t7c0696470c372a55@mail.gmail.com> On Thu, Apr 30, 2009 at 2:31 AM, wrote: > Python > for i,j in topgirls, richgirls: > ?? ?print i,j Use zip() or itertools.izip(): for i, j in zip(topgirls, richgirls): print i, j Kent From motoom at xs4all.nl Thu Apr 30 12:54:49 2009 From: motoom at xs4all.nl (Michiel Overtoom) Date: Thu, 30 Apr 2009 12:54:49 +0200 Subject: [Tutor] is it possible to traverse two lists simulatenously using python In-Reply-To: <1c2a2c590904300320k6b9714e5t7c0696470c372a55@mail.gmail.com> References: <1c2a2c590904300320k6b9714e5t7c0696470c372a55@mail.gmail.com> Message-ID: <49F98379.2070206@xs4all.nl> Kent Johnson wrote: > Use zip() or itertools.izip(): And when the sequences are of unequal length: topgirls=["Ann","Mary","Casey","Zoe"] richgirls=["Britt","Susan","Alice"] print "\nUsing zip()" for i,j in zip(topgirls,richgirls): print i,j print "\nUsing map()" for i,j in map(None,topgirls,richgirls): print i,j -- "The ability of the OSS process to collect and harness the collective IQ of thousands of individuals across the Internet is simply amazing." - Vinod Valloppillil http://www.catb.org/~esr/halloween/halloween4.html From lie.1296 at gmail.com Thu Apr 30 13:12:54 2009 From: lie.1296 at gmail.com (Lie Ryan) Date: Thu, 30 Apr 2009 21:12:54 +1000 Subject: [Tutor] is it possible to traverse two lists simulatenously using python In-Reply-To: <49F98379.2070206@xs4all.nl> References: <1c2a2c590904300320k6b9714e5t7c0696470c372a55@mail.gmail.com> <49F98379.2070206@xs4all.nl> Message-ID: Michiel Overtoom wrote: > Kent Johnson wrote: > >> Use zip() or itertools.izip(): > > And when the sequences are of unequal length: On python3.x, use itertools.zip_longest() From tim at johnsons-web.com Thu Apr 30 17:15:14 2009 From: tim at johnsons-web.com (Tim Johnson) Date: Thu, 30 Apr 2009 07:15:14 -0800 Subject: [Tutor] is it possible to traverse two lists simulatenously using python In-Reply-To: References: Message-ID: <200904300715.14196.tim@johnsons-web.com> On Wednesday 29 April 2009, mobiledreamers at gmail.com wrote: > Python > for i,j in topgirls, richgirls: > print i,j > > > Cheetah > #for $i,$j in $topgirls, $richgirls$i, $j > #end for > This doesnt work Hello - Please do not send email to the python ML via "undisclosed recipients". It's really poor form and it screws up my mail filters. thanks --------- Tim