From duxbuz at hotmail.com Thu May 1 10:08:17 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 1 May 2014 08:08:17 +0000 Subject: [Tutor] bit shifting Message-ID: I am trying to follow some code. It is basically a python scratch interfacing script. Anyway part of the script has this code. Searching google for >> greater than signs in code with python has its issues. Can anyone clarify this stuff. I know its about 4 bytes of data. It looks like its setting all bits HIGH to me? n = len(cmd) a = array('c') a.append(chr((n>> 24) & 0xFF)) a.append(chr((n>> 16) & 0xFF)) a.append(chr((n>> 8) & 0xFF)) a.append(chr(n & 0xFF)) More code for context for python version 2.7: from array import array import socket import time import sys from Tkinter import Tk from tkSimpleDialog import askstring root = Tk() root.withdraw() PORT = 42001 HOST = askstring('Scratch Connector', 'IP:') if not HOST: sys.exit() print("Connecting...") scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) scratchSock.connect((HOST, PORT)) print("Connected!") def sendScratchCommand(cmd): n = len(cmd) a = array('c') a.append(chr((n>> 24) & 0xFF)) a.append(chr((n>> 16) & 0xFF)) a.append(chr((n>> 8) & 0xFF)) a.append(chr(n & 0xFF)) scratchSock.send(a.tostring() + cmd) while True: msg = askstring('Scratch Connector', 'Send Broadcast:') if msg: sendScratchCommand('broadcast "' + msg + '"') Another for Python 3: import socket HOST = 'localhost' PORT = 42001 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) def sendCMD(cmd): n = len(cmd) a = [] a.append((n>> 24) & 0xFF) a.append((n>> 16) & 0xFF) a.append((n>> 8) & 0xFF) a.append(n & 0xFF) b = '' for i in list(range(len(a))): b += a[i] s.send(bytes(b+cmd,'UTF-8')) sendCMD('broadcast"hello"') From scott.w.d at cox.net Thu May 1 07:21:41 2014 From: scott.w.d at cox.net (Scott W Dunning) Date: Wed, 30 Apr 2014 22:21:41 -0700 Subject: [Tutor] Final review Message-ID: Hello, I am new to python and have a final review coming up and was hoping you could help me answer a few questions I came across while studying. So, I get a little confused about lists sometimes. This one is a little hard to make heads or tails of. I get confused about how to tell how many lists are within one list like the one below. How many lists are located inside alist is it 1 or 2, 3?? Also, do spaces count as an index in lists? >>> alist = [3, 67, "cat?, [56, 57, ?dog?], [], 3.14, False] >>> print alist[4:] [[], 3.14, False] The ouput for below is 2 when it seems like there should be 3 lists located inside x. Is it [10,20] that is not consider inside of x? Any tips on how to tell how to spot them more clearly? x = ['a', [2.0, 5, [10, 20]]] print len(x) I have a few more questions too but I figured I send this email for now while compile my other questions you guys can hopefully help to shed light on. Thanks for any help!! Scott From duxbuz at hotmail.com Thu May 1 11:08:21 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 1 May 2014 09:08:21 +0000 Subject: [Tutor] 2s complement binary for negative Message-ID: Can anyone clarify please? Just reading this: https://wiki.python.org/moin/BitwiseOperators The section on 2's complement binary for negative integers. It states: "Thus the number -5 is treated by bitwise operators as if it were written "...1111111111111111111011". " I am wondering why would this not be -4? I though -5 would be written ..1111111111111010 5 being 101b Thanks From duxbuz at hotmail.com Thu May 1 11:53:15 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 1 May 2014 09:53:15 +0000 Subject: [Tutor] bit shifting In-Reply-To: References: Message-ID: Ok I am getting somewhere with this now. A bitshift followed by ANDing the result of the shift! So I think n>> 24 & 0xFF is shift n 3 bytes right, save results in n and then AND n with 255 decimal? ---------------------------------------- > From: duxbuz at hotmail.com > To: tutor at python.org > Date: Thu, 1 May 2014 08:08:17 +0000 > Subject: [Tutor] bit shifting > > I am trying to follow some code. It is basically a python scratch interfacing script. > > > > Anyway part of the script has this code. > > > > Searching google for>> greater than signs in code with python has its issues. > > > > Can anyone clarify this stuff. > > > > I know its about 4 bytes of data. It looks like its setting all bits HIGH to me? > > > > n = len(cmd) > a = array('c') > a.append(chr((n>> 24) & 0xFF)) > a.append(chr((n>> 16) & 0xFF)) > a.append(chr((n>> 8) & 0xFF)) > a.append(chr(n & 0xFF)) > > > > > > More code for context for python version 2.7: > > > > from array import array > import socket > import time > import sys > > from Tkinter import Tk > from tkSimpleDialog import askstring > root = Tk() > root.withdraw() > > PORT = 42001 > HOST = askstring('Scratch Connector', 'IP:') > if not HOST: > sys.exit() > > print("Connecting...") > scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) > scratchSock.connect((HOST, PORT)) > print("Connected!") > > def sendScratchCommand(cmd): > n = len(cmd) > a = array('c') > a.append(chr((n>> 24) & 0xFF)) > a.append(chr((n>> 16) & 0xFF)) > a.append(chr((n>> 8) & 0xFF)) > a.append(chr(n & 0xFF)) > scratchSock.send(a.tostring() + cmd) > > while True: > msg = askstring('Scratch Connector', 'Send Broadcast:') > if msg: > sendScratchCommand('broadcast "' + msg + '"') > > > > Another for Python 3: > > import socket > > HOST = 'localhost' > PORT = 42001 > > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) > s.connect((HOST, PORT)) > > def sendCMD(cmd): > n = len(cmd) > a = [] > a.append((n>> 24) & 0xFF) > a.append((n>> 16) & 0xFF) > a.append((n>> 8) & 0xFF) > a.append(n & 0xFF) > b = '' > for i in list(range(len(a))): > b += a[i] > s.send(bytes(b+cmd,'UTF-8')) > > sendCMD('broadcast"hello"') > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From duxbuz at hotmail.com Thu May 1 12:02:54 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 1 May 2014 10:02:54 +0000 Subject: [Tutor] bit shifting In-Reply-To: References: , Message-ID: But what is the purpose of ANDing with 255? Would this not just return the same value? eg 1010 and with 11111111 would just return 00001010 or 1010 ---------------------------------------- > From: duxbuz at hotmail.com > To: tutor at python.org > Date: Thu, 1 May 2014 09:53:15 +0000 > Subject: Re: [Tutor] bit shifting > > Ok I am getting somewhere with this now. > > > A bitshift followed by ANDing the result of the shift! > > > So I think n>> 24 & 0xFF > > > is > > > shift n 3 bytes right, save results in n and then AND n with 255 decimal? > > ---------------------------------------- >> From: duxbuz at hotmail.com >> To: tutor at python.org >> Date: Thu, 1 May 2014 08:08:17 +0000 >> Subject: [Tutor] bit shifting >> >> I am trying to follow some code. It is basically a python scratch interfacing script. >> >> >> >> Anyway part of the script has this code. >> >> >> >> Searching google for>> greater than signs in code with python has its issues. >> >> >> >> Can anyone clarify this stuff. >> >> >> >> I know its about 4 bytes of data. It looks like its setting all bits HIGH to me? >> >> >> >> n = len(cmd) >> a = array('c') >> a.append(chr((n>> 24) & 0xFF)) >> a.append(chr((n>> 16) & 0xFF)) >> a.append(chr((n>> 8) & 0xFF)) >> a.append(chr(n & 0xFF)) >> >> >> >> >> >> More code for context for python version 2.7: >> >> >> >> from array import array >> import socket >> import time >> import sys >> >> from Tkinter import Tk >> from tkSimpleDialog import askstring >> root = Tk() >> root.withdraw() >> >> PORT = 42001 >> HOST = askstring('Scratch Connector', 'IP:') >> if not HOST: >> sys.exit() >> >> print("Connecting...") >> scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >> scratchSock.connect((HOST, PORT)) >> print("Connected!") >> >> def sendScratchCommand(cmd): >> n = len(cmd) >> a = array('c') >> a.append(chr((n>> 24) & 0xFF)) >> a.append(chr((n>> 16) & 0xFF)) >> a.append(chr((n>> 8) & 0xFF)) >> a.append(chr(n & 0xFF)) >> scratchSock.send(a.tostring() + cmd) >> >> while True: >> msg = askstring('Scratch Connector', 'Send Broadcast:') >> if msg: >> sendScratchCommand('broadcast "' + msg + '"') >> >> >> >> Another for Python 3: >> >> import socket >> >> HOST = 'localhost' >> PORT = 42001 >> >> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) >> s.connect((HOST, PORT)) >> >> def sendCMD(cmd): >> n = len(cmd) >> a = [] >> a.append((n>> 24) & 0xFF) >> a.append((n>> 16) & 0xFF) >> a.append((n>> 8) & 0xFF) >> a.append(n & 0xFF) >> b = '' >> for i in list(range(len(a))): >> b += a[i] >> s.send(bytes(b+cmd,'UTF-8')) >> >> sendCMD('broadcast"hello"') >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From kwpolska at gmail.com Thu May 1 12:07:09 2014 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Thu, 1 May 2014 12:07:09 +0200 Subject: [Tutor] Final review In-Reply-To: References: Message-ID: On Thu, May 1, 2014 at 7:21 AM, Scott W Dunning wrote: > Hello, I am new to python and have a final review coming up and was hoping you could help me answer a few questions I came across while studying. > > > So, I get a little confused about lists sometimes. This one is a little hard to make heads or tails of. I get confused about how to tell how many lists are within one list like the one below. How many lists are located inside alist is it 1 or 2, 3?? Two: [56, 57, "dog"] and [] (empty). > Also, do spaces count as an index in lists? What do you mean by ?spaces?, exactly? >>>> alist = [3, 67, "cat?, [56, 57, ?dog?], [], 3.14, False] alist contains: 0. the integer 3 1. the integer 67 2. the string "cat" ? 3. the list: 0. the integer 56 1. the integer 57 2. the string "dog" ? 4. an empty list 5. the float 3.14 6. the boolean False ? Your e-mail client uses smart quotes and thus mangles your replies (it converts U+0022 QUOTATION MARK into U+201C LEFT DOUBLE QUOTATION MARK and U+201D RIGHT DOUBLE QUOTATION MARK, which are not valid delimiters for Python strings). > The ouput for below is 2 when it seems like there should be 3 lists located inside x. Is it [10,20] that is not consider inside of x? Any tips on how to tell how to spot them more clearly? There are two lists in x, and one more list inside a list inside of x: > x = ['a', [2.0, 5, [10, 20]]] x contains: 0. the string 'a' 1. the list: 0. the float 2.0 1. the integer 5 2. the list: 0. the integer 10 1. the integer 20 -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From __peter__ at web.de Thu May 1 12:38:19 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 01 May 2014 12:38:19 +0200 Subject: [Tutor] bit shifting References: Message-ID: Ian D wrote: > I am trying to follow some code. It is basically a python scratch > interfacing script. > Anyway part of the script has this code. > Searching google for >> greater than signs in code with python has its > issues. > Can anyone clarify this stuff. > I know its about 4 bytes of data. It looks like its setting all bits HIGH > to me? > n = len(cmd) > a = array('c') > a.append(chr((n>> 24) & 0xFF)) > a.append(chr((n>> 16) & 0xFF)) > a.append(chr((n>> 8) & 0xFF)) > a.append(chr(n & 0xFF)) [...] > scratchSock.send(a.tostring() + cmd) This is a way to serialize the integer n. The code assumes that it consists of 32 bits or 4 bytes. Example: given a cmd that is 2271560481 bytes long the corresponding int n written in binary is >>> n = 2271560481 >>> bin(n) '0b10000111011001010100001100100001' To extract the highest byte you shift-right 24 bits and get >>> bin(n>>24) '0b10000111' In this case the mask 0xff or 0b11111111 has no effect >>> bin(n>>24 & 0xff) '0b10000111' (or rather it silences the error that commands longer than 2**32 are not supported) but to get the second highest byte the mask is necessary as >>> bin(n>>16) '0b1000011101100101' gives the 16 highest bits. Let's clip the high bits off: >>> bin(0b1000011101100101 ... & 0b0000000011111111) '0b1100101' # one leading 0-bit implied Two more bytes, and we get the string "\x87eC": >>> import array >>> a = array.array("c") >>> n = 0x87654321 # now you see how I picked the number for the example >>> n 2271560481 >>> a.append(chr(n>>24 & 0xff)) >>> a.append(chr(n>>16 & 0xff)) >>> a.append(chr(n>>8 & 0xff)) >>> a.append(chr(n>>0 & 0xff)) # 'n >> 0' is the same as juse 'n' >>> a.tostring() '\x87eC!' # "\x86" is Python's way to to display chr(0x87) To recognize the pattern underneath this odd assembly of bytes we can encode it: >>> a.tostring().encode("hex") '87654321' So "e" is the same as "\x65", "C" is "\x43", "!" is "\x21". By the way, there's a function in the library that does the above: >>> import struct >>> struct.pack("!I", 0x87654321) '\x87eC!' It also does the right thing when the number is too large, it raises an exception: >>> struct.pack("!I", 0x987654321) Traceback (most recent call last): File "", line 1, in struct.error: 'I' format requires 0 <= number <= 4294967295 From __peter__ at web.de Thu May 1 13:13:52 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 01 May 2014 13:13:52 +0200 Subject: [Tutor] 2s complement binary for negative References: Message-ID: Ian D wrote: > Can anyone clarify please? > > > Just reading this: > https://wiki.python.org/moin/BitwiseOperators > The section on 2's complement binary for negative integers. > It states: > "Thus the number -5 is treated by bitwise operators as if it were written > "...1111111111111111111011". " > > > I am wondering why would this not be -4? > > > I though -5 would be written ..1111111111111010 > > > 5 being 101b This may be easier to understand if you forget about the infinite number of leading ones. Let's have a look at 4-bit integers: binary unsigned --------------- 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 8 1001 9 1010 10 1011 11 1100 12 1101 13 1110 14 1111 15 If you add 5 + 11 you get 0101 1011 ----- 10000 but that doesn't fit into the 4 bits. Clip off the highest bit and you get 5 + 11 == 0 or 11 == -5 Following that logic we can relabel the 4-bit ints with a leading 1-bit: binary signed --------------- 0000 0 0001 1 0010 2 0011 3 0100 4 0101 5 0110 6 0111 7 1000 -8 1001 -7 1010 -6 1011 -5 1100 -4 1101 -3 1110 -2 1111 -1 From davea at davea.name Thu May 1 14:27:42 2014 From: davea at davea.name (Dave Angel) Date: Thu, 1 May 2014 08:27:42 -0400 (EDT) Subject: [Tutor] 2s complement binary for negative References: Message-ID: Ian D Wrote in message: > Can anyone clarify please? > > > Just reading this: > > https://wiki.python.org/moin/BitwiseOperators > > > The section on 2's complement binary for negative integers. > > > It states: > > > "Thus the number -5 is treated by bitwise operators as if it were written "...1111111111111111111011". " > > > I am wondering why would this not be -4? > > > I though -5 would be written ..1111111111111010 > > > 5 being 101b > That would be the case for a machine/language using ones-complement. http://en.m.wikipedia.org/wiki/Ones'_complement Twos-complement is usually explained by saying that for negative integers you take the ones-complement value and add one. But it might be more correct to say that for ones-complement machines you take the obvious result when subtracting numbers, and if it's negative you subtract one from it. Forty plus years ago I used a ones-complement machine, the CDC 6400, as well as a twos-complement one, the IBM 360. Since then, every architecture I used, and every one I implemented, were twos-complement, including all the Intel and AMD and Motorola ones. Twos-complement has some advantage in the hardware needed for most arithmetic, but that doesn't matter to us as users. What does matter is when we mix arithmetic and logic, on the same value. And when we support more than one size of integer. Probably the most visible difference is the fact that there are two values for zero in ones-complement, all zero bits, and all one bits. When doing arithmetic, it's normal to consider them equal, but when doing bitwise operations they're clearly different. In fact on the CDC, False and True are usually +0 and -0. So on that architecture python would need two equality operators. According to the page you referenced, Python chose to hide the underlying architecture when running on an occasional ones-complement machine. Good for them. -- DaveA From steve at pearwood.info Thu May 1 14:30:01 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 1 May 2014 22:30:01 +1000 Subject: [Tutor] Final review In-Reply-To: References: Message-ID: <20140501122958.GX4273@ando> On Wed, Apr 30, 2014 at 10:21:41PM -0700, Scott W Dunning wrote: > So, I get a little confused about lists sometimes. This one is a > little hard to make heads or tails of. I get confused about how to > tell how many lists are within one list like the one below. How many > lists are located inside alist is it 1 or 2, 3?? Also, do spaces > count as an index in lists? > > >>> alist = [3, 67, "cat?, [56, 57, ?dog?], [], 3.14, False] > >>> print alist[4:] > [[], 3.14, False] Spaces do not count as indexes. Spaces are just inserted when printing the list to make it easier for people to read. It is commas, not spaces, which separate items in the list. So you have: alist = [3, 67, "cat", [56, 57, "dog"], [], 3.14, False] Count the items separated by commas: 3 comma <== that's one 67 comma <== that's two "cat" comma <== three Next is tricky: the square bracket [ starts a new list. Since we don't care how many items are inside that list, we just keep going until we reach a matching ] closing square bracket: [56, 57, "dog"] comma <== that's four [] comma <== five 3.14 comma <== six False <== seventh and last Notice that because the commas *separate* items, there is one more item than separators: 1,2,3,4,5,6,7 (six commas and seven items). Also notice that out of the seven items inside alist, only two of them are lists: 3 INTEGER 67 INTEGER "cat" STRING [56, 57, "dog"] LIST with three items [] LIST with zero items 3.14 FLOAT False BOOLEAN FLAG > The ouput for below is 2 when it seems like there should be 3 lists > located inside x. Is it [10,20] that is not consider inside of x? > Any tips on how to tell how to spot them more clearly? > > x = ['a', [2.0, 5, [10, 20]]] > print len(x) Again, look at the commas: 'a' (a string) comma <== first item The next item begins with a square bracket, so it is a list. We ignore everything inside it *except more square brackets* until we reach the matching square bracket: [2.0 comma (but we don't care) 5 comma (still don't care) [ (and now we start yet another list, inside a list inside a list!) 10 comma (don't care) 20 (don't care) ] (we have reached the end of the innermost list) ] (we have reached the end of the inner list and no more items after that. So the list called "x" has two items: - the string "a" - a list containing 2.0, 5, and another list So there are two items inside list "x", three items inside the list inside "x", and two items inside the list inside that. When in doubt, this may help make it more obvious: x = ['a', [2.0, 5, [10, 20]]] for item in x: print(item) That will show you each item in turn. -- Steven From duxbuz at hotmail.com Thu May 1 16:38:44 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 1 May 2014 14:38:44 +0000 Subject: [Tutor] array('c') Message-ID: Hi I have this part of code and am unsure as to the effect of the array('c') part. Is it creating an array and adding 'c' as its first value? This does not seem to be the case. Thanks n = len(cmd) a = array('c') a.append(chr((n>> 24) & 0xFF)) a.append(chr((n>> 16) & 0xFF)) a.append(chr((n>> 8) & 0xFF)) a.append(chr(n & 0xFF)) scratchSock.send(a.tostring() + cmd) From __peter__ at web.de Thu May 1 17:35:22 2014 From: __peter__ at web.de (Peter Otten) Date: Thu, 01 May 2014 17:35:22 +0200 Subject: [Tutor] array('c') References: Message-ID: Ian D wrote: > I have this part of code and am unsure as to the effect of the array('c') > part. Is it creating an array and adding 'c' as its first value? No, the first arg to array.array() is the typecode; data may be passed as the second argument. The typecode "c" creates an array of 8-bit characters as defined by the C compiler. >>> a = array.array("c") >>> len(a) 0 >>> a = array.array("c", "abc") >>> len(a) 3 See for more. From breamoreboy at yahoo.co.uk Thu May 1 17:40:54 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 01 May 2014 16:40:54 +0100 Subject: [Tutor] array('c') In-Reply-To: References: Message-ID: On 01/05/2014 15:38, Ian D wrote: > Hi > > I have this part of code and am unsure as to the effect of the array('c') part. > > Is it creating an array and adding 'c' as its first value? > > This does not seem to be the case. > > Thanks > > n = len(cmd) > a = array('c') > a.append(chr((n>> 24) & 0xFF)) > a.append(chr((n>> 16) & 0xFF)) > a.append(chr((n>> 8) & 0xFF)) > a.append(chr(n & 0xFF)) > scratchSock.send(a.tostring() + cmd) The 'c' is actually a type code see https://docs.python.org/2/library/array.html#module-array but note that this does *NOT* exist in Python 3. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From stefan_ml at behnel.de Thu May 1 17:46:08 2014 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 01 May 2014 17:46:08 +0200 Subject: [Tutor] array('c') In-Reply-To: References: Message-ID: Ian D, 01.05.2014 16:38: > I have this part of code and am unsure as to the effect of the array('c') part. The argument that you pass into the constructor is a type identifier. https://docs.python.org/2/library/array.html#array.array The different types are defined at the top of that page. It seems that you are using Python 2, BTW. In Python 3, the "c" type is not supported anymore. I guess that's because characters do not actually exist at the C level, only bytes (which are represented by "b" and "B"). > n = len(cmd) > a = array('c') > a.append(chr((n>> 24) & 0xFF)) > a.append(chr((n>> 16) & 0xFF)) > a.append(chr((n>> 8) & 0xFF)) > a.append(chr(n & 0xFF)) > scratchSock.send(a.tostring() + cmd) You don't have to pass in strings. Integers will do just fine. The code above looks way too complicated, though. You might want to take a look at the struct module instead, which allows you to do C level formatting of simple and compound values. Stefan From breamoreboy at yahoo.co.uk Thu May 1 17:53:39 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 01 May 2014 16:53:39 +0100 Subject: [Tutor] Final review In-Reply-To: References: Message-ID: On 01/05/2014 06:21, Scott W Dunning wrote: > Hello, I am new to python and have a final review coming up and was hoping you could help me answer a few questions I came across while studying. > > So, I get a little confused about lists sometimes. This one is a little hard to make heads or tails of. I get confused about how to tell how many lists are within one list like the one below. How many lists are located inside alist is it 1 or 2, 3?? Also, do spaces count as an index in lists? > >>>> alist = [3, 67, "cat?, [56, 57, ?dog?], [], 3.14, False] >>>> print alist[4:] > [[], 3.14, False] > > The ouput for below is 2 when it seems like there should be 3 lists located inside x. Is it [10,20] that is not consider inside of x? Any tips on how to tell how to spot them more clearly? > > x = ['a', [2.0, 5, [10, 20]]] > print len(x) > > I have a few more questions too but I figured I send this email for now while compile my other questions you guys can hopefully help to shed light on. > > Thanks for any help!! > > Scott One way of showing what you've got in the list using iPython. In [2]: alist = [3, 67, "cat", [56, 57, 'dog'], [], 3.14, False] In [3]: for i, item in enumerate(alist): ...: print('index', i, type(item), 'item', item) ...: index 0 item 3 index 1 item 67 index 2 item cat index 3 item [56, 57, 'dog'] index 4 item [] index 5 item 3.14 index 6 item False -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From duxbuz at hotmail.com Thu May 1 18:58:59 2014 From: duxbuz at hotmail.com (Ian D) Date: Thu, 1 May 2014 16:58:59 +0000 Subject: [Tutor] array('c') In-Reply-To: References: , Message-ID: Thanks > To: tutor at python.org > From: breamoreboy at yahoo.co.uk > Date: Thu, 1 May 2014 16:40:54 +0100 > Subject: Re: [Tutor] array('c') > > On 01/05/2014 15:38, Ian D wrote: > > Hi > > > > I have this part of code and am unsure as to the effect of the array('c') part. > > > > Is it creating an array and adding 'c' as its first value? > > > > This does not seem to be the case. > > > > Thanks > > > > n = len(cmd) > > a = array('c') > > a.append(chr((n>> 24) & 0xFF)) > > a.append(chr((n>> 16) & 0xFF)) > > a.append(chr((n>> 8) & 0xFF)) > > a.append(chr(n & 0xFF)) > > scratchSock.send(a.tostring() + cmd) > > The 'c' is actually a type code see > https://docs.python.org/2/library/array.html#module-array but note that > this does *NOT* exist in Python 3. > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > --- > This email is free from viruses and malware because avast! Antivirus protection is active. > http://www.avast.com > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From linux at barrowhillfarm.org.uk Sat May 3 00:19:26 2014 From: linux at barrowhillfarm.org.uk (Bob Williams) Date: Fri, 02 May 2014 23:19:26 +0100 Subject: [Tutor] Logical error? Message-ID: <536419EE.5030906@barrowhillfarm.org.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I'm fairly new to coding and python. My system is linux (openSUSE 13.1). I've written the following code to examine a log file, and extract strings from certain lines if two conditions are met, namely that the file has been modified today, and the line contains the string 'recv'. - ---Code--- #!/usr/bin/python import sys import datetime import codecs import subprocess # Format date as YYYY/MM/DD today = (datetime.datetime.now()).strftime('%Y/%m/%d') fullPath = [] # declare (initially empty) lists truncPath = [] with codecs.open('/var/log/rsyncd.log', 'r') as rsyncd_log: for line in rsyncd_log.readlines(): fullPath += [line.decode('utf-8', 'ignore').strip()] if fullPath[-1][0:10] == today: print("\n Rsyncd.log has been modified in the last 24 hours...") else: print("\n No recent rsync activity. Nothing to do.\n") sys.exit() # Search for lines starting with today's date and containing 'recv' # Strip everything up to and including 'recv' and following last '/' path separator for i in range(0, len(fullPath)): if fullPath[i][0:10] == today and 'recv' in fullPath[i]: print("got there") begin = fullPath[i].find('recv ') end = fullPath[i].rfind('/') fullPath[i] = fullPath[i][begin+5:end] truncPath.append(fullPath[i]) print(" ...and the following new albums have been added:\n") else: print(" ...but no new music has been downloaded.\n") sys.exit() - ---Code--- The file rsyncd.log typically contains lines such as (sorry about the wrapping): 2014/05/02 19:43:14 [20282] host109-145-nnn-xxx.range109-145.btcentralplus.com recv Logical Progression Level 3 (1998) Intense/17 Words 2 B Heard Collective - Sonic Weapon.flac 72912051 72946196 I would expect the script to output a list of artist and album names, eg Logical Progression Level 3 (1998) Intense. IOW what is between the string 'recv' and the trailing '/'. What it actually produces is: :~> python ~/bin/newstuff.py Rsyncd.log has been modified in the last 24 hours... ...but no new music has been downloaded. This suggests that the first 'if' clause (matching the first 10 characters of the last line) is satisfied, but the second one isn't, as the flow jumps to the second 'else' clause. As the script runs without complaint, this is presumably a logical error rather than a syntax error, but I cannot see where I've gone wrong. Bob - -- Bob Williams System: Linux 3.11.10-7-desktop Distro: openSUSE 13.1 (x86_64) with KDE Development Platform: 4.13.0 Uptime: 06:00am up 11:26, 4 users, load average: 0.00, 0.02, 0.05 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.22 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlNkGewACgkQ0Sr7eZJrmU57YwCgg91pxyQbFMSe+TqHkEjMuzQ6 03MAnRQ50up6v+kYE+Hf/jK6yOqQw4Ma =+w0s -----END PGP SIGNATURE----- From illusiontechniques at gmail.com Sat May 3 00:42:05 2014 From: illusiontechniques at gmail.com (C Smith) Date: Fri, 2 May 2014 18:42:05 -0400 Subject: [Tutor] Fwd: Logical error? In-Reply-To: References: <536419EE.5030906@barrowhillfarm.org.uk> Message-ID: The first loop tests for the last element of fullPath to have today's date. The second loop tests the first element in fullPath, if it is not today, you will end up running sys.exit() when you hit the else clause in second loop. On Fri, May 2, 2014 at 6:38 PM, C Smith wrote: > The first loop tests for the last element of fullPath to have today's > date. The second loop tests the first element in fullPath, if it is not > today, you will end up running sys.exit() when you hit the else clause in > second loop. > > > On Fri, May 2, 2014 at 6:19 PM, Bob Williams wrote: > >> -----BEGIN PGP SIGNED MESSAGE----- >> Hash: SHA1 >> >> Hi, >> >> I'm fairly new to coding and python. My system is linux (openSUSE >> 13.1). I've written the following code to examine a log file, and >> extract strings from certain lines if two conditions are met, namely >> that the file has been modified today, and the line contains the >> string 'recv'. >> >> - ---Code--- >> #!/usr/bin/python >> >> import sys >> import datetime >> import codecs >> import subprocess >> >> # Format date as YYYY/MM/DD >> today = (datetime.datetime.now()).strftime('%Y/%m/%d') >> >> fullPath = [] # declare (initially empty) lists >> truncPath = [] >> >> with codecs.open('/var/log/rsyncd.log', 'r') as rsyncd_log: >> for line in rsyncd_log.readlines(): >> fullPath += [line.decode('utf-8', 'ignore').strip()] >> if fullPath[-1][0:10] == today: >> print("\n Rsyncd.log has been modified in the last 24 hours...") >> else: >> print("\n No recent rsync activity. Nothing to do.\n") >> sys.exit() >> >> # Search for lines starting with today's date and containing 'recv' >> # Strip everything up to and including 'recv' and following last '/' >> path separator >> for i in range(0, len(fullPath)): >> if fullPath[i][0:10] == today and 'recv' in fullPath[i]: >> print("got there") >> begin = fullPath[i].find('recv ') >> end = fullPath[i].rfind('/') >> fullPath[i] = fullPath[i][begin+5:end] >> truncPath.append(fullPath[i]) >> print(" ...and the following new albums have been added:\n") >> else: >> print(" ...but no new music has been downloaded.\n") >> sys.exit() >> >> - ---Code--- >> >> The file rsyncd.log typically contains lines such as (sorry about the >> wrapping): >> >> 2014/05/02 19:43:14 [20282] >> host109-145-nnn-xxx.range109-145.btcentralplus.com recv Logical >> Progression Level 3 (1998) Intense/17 Words 2 B Heard Collective - >> Sonic Weapon.flac 72912051 72946196 >> >> I would expect the script to output a list of artist and album names, >> eg Logical Progression Level 3 (1998) Intense. IOW what is between the >> string 'recv' and the trailing '/'. What it actually produces is: >> >> :~> python ~/bin/newstuff.py >> >> Rsyncd.log has been modified in the last 24 hours... >> ...but no new music has been downloaded. >> >> This suggests that the first 'if' clause (matching the first 10 >> characters of the last line) is satisfied, but the second one isn't, >> as the flow jumps to the second 'else' clause. >> >> As the script runs without complaint, this is presumably a logical >> error rather than a syntax error, but I cannot see where I've gone wrong. >> >> Bob >> - -- >> Bob Williams >> System: Linux 3.11.10-7-desktop >> Distro: openSUSE 13.1 (x86_64) with KDE Development Platform: 4.13.0 >> Uptime: 06:00am up 11:26, 4 users, load average: 0.00, 0.02, 0.05 >> -----BEGIN PGP SIGNATURE----- >> Version: GnuPG v2.0.22 (GNU/Linux) >> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ >> >> iEYEARECAAYFAlNkGewACgkQ0Sr7eZJrmU57YwCgg91pxyQbFMSe+TqHkEjMuzQ6 >> 03MAnRQ50up6v+kYE+Hf/jK6yOqQw4Ma >> =+w0s >> -----END PGP SIGNATURE----- >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat May 3 03:53:27 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 3 May 2014 11:53:27 +1000 Subject: [Tutor] Logical error? In-Reply-To: <536419EE.5030906@barrowhillfarm.org.uk> References: <536419EE.5030906@barrowhillfarm.org.uk> Message-ID: <20140503015327.GZ4273@ando> Hi Bob, and welcome! My responses interleaved with yours, below. On Fri, May 02, 2014 at 11:19:26PM +0100, Bob Williams wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Hi, > > I'm fairly new to coding and python. My system is linux (openSUSE > 13.1). Nice to know. And I see you have even more infomation about your system in your email signature, including your email client and uptime. But what you don't tell us is what version of Python you're using. I'm going to guess that it is something in the 3.x range, since you call print as a function rather than a statement, but can't be sure. Fortunately in this case I don't think the exact version matters. [...] > fullPath = [] # declare (initially empty) lists > truncPath = [] > > with codecs.open('/var/log/rsyncd.log', 'r') as rsyncd_log: > for line in rsyncd_log.readlines(): > fullPath += [line.decode('utf-8', 'ignore').strip()] A small note about performance here. If your log files are very large (say, hundreds of thousands or millions of lines) you will find that this part is *horribly horrible slow*. There's two problems, a minor and a major one. First, rsyncd_log.readlines will read the entire file in one go. Since you end up essentially copying the whole file, you end up with two large lists of lines. There are ways to solve that, and process the lines lazily, one line at a time without needing to store the whole file. But that's not the big problem. The big problem is this: fullPath += [line.decode('utf-8', 'ignore').strip()] which is an O(N**2) algorithm. Do you know that terminology? Very briefly: O(1) means approximately constant time: tripling the size of the input makes no difference to the processing time. O(N) means linear time: tripling the input triples the processing time. O(N**2) means quadratic time: tripling the input increases the processing time not by a factor of three, but a factor of three squared, or nine. With small files, and fast computers, you won't notice. But with huge files and a slow computer, that could be painful. Instead, a better approach is: fullPath.append(line.decode('utf-8', 'ignore').strip()) which avoids the O(N**2) performance trap. > if fullPath[-1][0:10] == today: > print("\n Rsyncd.log has been modified in the last 24 hours...") > else: > print("\n No recent rsync activity. Nothing to do.\n") > sys.exit() > > # Search for lines starting with today's date and containing 'recv' > # Strip everything up to and including 'recv' and following last '/' > path separator > for i in range(0, len(fullPath)): > if fullPath[i][0:10] == today and 'recv' in fullPath[i]: > print("got there") > begin = fullPath[i].find('recv ') > end = fullPath[i].rfind('/') > fullPath[i] = fullPath[i][begin+5:end] > truncPath.append(fullPath[i]) > print(" ...and the following new albums have been added:\n") > else: > print(" ...but no new music has been downloaded.\n") > sys.exit() Now at last we get to your immediate problem: the above is intended to iterate over the lines of fullPath. But it starts at the beginning of the file, which may not be today. The first time you hit a line which is not today, the program exits, before it gets a chance to advance to the more recent days. That probably means that it looks at the first line in the log, determines that it is not today, and exits. I'm going to suggest a more streamlined algorithm. Most of it is actual Python code, assuming you're using Python 3. Only the "process this line" part needs to be re-written. new_activity = False # Nothing has happened today. with open('/var/log/rsyncd.log', 'r', encoding='utf-8', errors='ignore') as rsyncd_log: for line in rsyncd_log: line = line.strip() if line[0:10] == today and 'recv' in line: new_activity = True process this line # <== fix this if not new_activity: print("no new albums have been added today") This has the benefit that every line is touched only once, not three times as in your version. Performance is linear, not quadratic. You should be able to adapt this to your needs. Good luck, and feel free to ask questions! -- Steven From linux at barrowhillfarm.org.uk Sat May 3 23:14:16 2014 From: linux at barrowhillfarm.org.uk (Bob Williams) Date: Sat, 03 May 2014 22:14:16 +0100 Subject: [Tutor] Logical error? In-Reply-To: <20140503015327.GZ4273@ando> References: <536419EE.5030906@barrowhillfarm.org.uk> <20140503015327.GZ4273@ando> Message-ID: <53655C28.8000404@barrowhillfarm.org.uk> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi Steven, On 03/05/14 02:53, Steven D'Aprano wrote: > Hi Bob, and welcome! > > My responses interleaved with yours, below. > > On Fri, May 02, 2014 at 11:19:26PM +0100, Bob Williams wrote: >> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 >> >> Hi, >> >> I'm fairly new to coding and python. My system is linux >> (openSUSE 13.1). > > Nice to know. And I see you have even more infomation about your > system in your email signature, including your email client and > uptime. But what you don't tell us is what version of Python you're > using. I'm going to guess that it is something in the 3.x range, > since you call print as a function rather than a statement, but > can't be sure. > I have both 2 and 3 installed here, but I call this script with python2. I guess I should concentrate on Python 3 as that's the way things are going. > Fortunately in this case I don't think the exact version matters. > > > [...] >> fullPath = [] # declare (initially empty) lists truncPath = [] >> >> with codecs.open('/var/log/rsyncd.log', 'r') as rsyncd_log: for >> line in rsyncd_log.readlines(): fullPath += [line.decode('utf-8', >> 'ignore').strip()] > > A small note about performance here. If your log files are very > large (say, hundreds of thousands or millions of lines) you will > find that this part is *horribly horrible slow*. There's two > problems, a minor and a major one. > The log file is typically a few thousand lines, so my code runs fast enough, but I understand your point. > First, rsyncd_log.readlines will read the entire file in one go. > Since you end up essentially copying the whole file, you end up > with two large lists of lines. There are ways to solve that, and > process the lines lazily, one line at a time without needing to > store the whole file. But that's not the big problem. > > The big problem is this: > > fullPath += [line.decode('utf-8', 'ignore').strip()] > > which is an O(N**2) algorithm. Do you know that terminology? Very > briefly: O(1) means approximately constant time: tripling the size > of the input makes no difference to the processing time. O(N) means > linear time: tripling the input triples the processing time. > O(N**2) means quadratic time: tripling the input increases the > processing time not by a factor of three, but a factor of three > squared, or nine. > > With small files, and fast computers, you won't notice. But with > huge files and a slow computer, that could be painful. > > Instead, a better approach is: > > fullPath.append(line.decode('utf-8', 'ignore').strip()) > > which avoids the O(N**2) performance trap. > Understood. > [snip] > > Now at last we get to your immediate problem: the above is intended > to iterate over the lines of fullPath. But it starts at the > beginning of the file, which may not be today. The first time you > hit a line which is not today, the program exits, before it gets a > chance to advance to the more recent days. That probably means that > it looks at the first line in the log, determines that it is not > today, and exits. > I'd missed the (now obvious) point that my if condition contained two terms which both have to be true. > I'm going to suggest a more streamlined algorithm. Most of it is > actual Python code, assuming you're using Python 3. Only the > "process this line" part needs to be re-written. > > new_activity = False # Nothing has happened today. with > open('/var/log/rsyncd.log', 'r', encoding='utf-8', errors='ignore') > as rsyncd_log: for line in rsyncd_log: line = line.strip() if > line[0:10] == today and 'recv' in line: new_activity = True process > this line # <== fix this > > if not new_activity: print("no new albums have been added today") > Thanks. This works nicely. > > > This has the benefit that every line is touched only once, not > three times as in your version. Performance is linear, not > quadratic. You should be able to adapt this to your needs. > > Good luck, and feel free to ask questions! > - -- Bob Williams System: Linux 3.11.10-7-desktop Distro: openSUSE 13.1 (x86_64) with KDE Development Platform: 4.13.0 Uptime: 06:00am up 11:26, 4 users, load average: 0.00, 0.02, 0.05 -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.22 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iEYEARECAAYFAlNlXCYACgkQ0Sr7eZJrmU71OACfSy1XWSDA08DNAndcA89AZg6Z +2IAniQJIrSd7wVJWl2MEtEdHlcdkwfj =YtSq -----END PGP SIGNATURE----- From david at graniteweb.com Sun May 4 00:11:28 2014 From: david at graniteweb.com (David Rock) Date: Sat, 3 May 2014 17:11:28 -0500 Subject: [Tutor] Question about O(N**2) Message-ID: <20140503221128.GD28262@wdfs> The the "Logical Error" question, this was brought up: The big problem is this: fullPath += [line.decode('utf-8', 'ignore').strip()] which is an O(N**2) algorithm. Do you know that terminology? Very briefly: O(1) means approximately constant time: tripling the size of the input makes no difference to the processing time. O(N) means linear time: tripling the input triples the processing time. O(N**2) means quadratic time: tripling the input increases the processing time not by a factor of three, but a factor of three squared, or nine. With small files, and fast computers, you won't notice. But with huge files and a slow computer, that could be painful. Instead, a better approach is: fullPath.append(line.decode('utf-8', 'ignore').strip()) which avoids the O(N**2) performance trap. I have two questions: 1. How do you know that fullPath += [line.decode('utf-8', 'ignore').strip()] is an O(N**2)? 2. How do you know that fullPath.append(line.decode('utf-8', 'ignore').strip()) is not? -- David Rock david at graniteweb.com From dyoo at hashcollision.org Sun May 4 00:59:40 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 3 May 2014 15:59:40 -0700 Subject: [Tutor] Question about O(N**2) In-Reply-To: <20140503221128.GD28262@wdfs> References: <20140503221128.GD28262@wdfs> Message-ID: Following up on this. Let's make sure that we're talking about the same thing. The assertion is that the following: fullPath += [...] where fullPath is a list of strings, exhibits O(n^2) time. I don't think this is true. Semantically, the statement above should be equivalent to: fullPath.append(...) and so I agree with David: this is not O(n^2), but rather O(n). Python's standard list implementation in C uses a strategy of geometric array resizing whenever the capacity has been exceeded. It expands the capacity by a multiplicative factor of its current capacity. Amortized analysis, a subject in computer science, lets us discover that the cost spread across many operations will be O(n). References: http://en.wikipedia.org/wiki/Dynamic_array --- What can be O(n^2) is the related, but not identical scenario where: fullPath += ... where fullPath is an immutable string. But this scenario is different than the original one. From steve at pearwood.info Sun May 4 05:21:42 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 4 May 2014 13:21:42 +1000 Subject: [Tutor] Logical error? In-Reply-To: <20140503015327.GZ4273@ando> References: <536419EE.5030906@barrowhillfarm.org.uk> <20140503015327.GZ4273@ando> Message-ID: <20140504032142.GH4273@ando> On Sat, May 03, 2014 at 11:53:27AM +1000, Steven D'Aprano wrote: > [...] > > fullPath = [] # declare (initially empty) lists > > truncPath = [] > > > > with codecs.open('/var/log/rsyncd.log', 'r') as rsyncd_log: > > for line in rsyncd_log.readlines(): > > fullPath += [line.decode('utf-8', 'ignore').strip()] > > A small note about performance here. If your log files are very large > (say, hundreds of thousands or millions of lines) you will find that > this part is *horribly horrible slow*. There's two problems, a minor and > a major one. Ah, actually I was mistaken about that. I forgot that for built-in lists, += augmented assignment is equivalent to calling list.extend(), so it actually does make the modifications in place. So I was wrong to say: > The big problem is this: > > fullPath += [line.decode('utf-8', 'ignore').strip()] > > which is an O(N**2) algorithm. This would be true, if += were implemented in terms of list addition. But it isn't, it is implemented as list.extend. -- Steven From dyoo at hashcollision.org Sun May 4 05:54:22 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 3 May 2014 20:54:22 -0700 Subject: [Tutor] Logical error? In-Reply-To: <20140504032142.GH4273@ando> References: <536419EE.5030906@barrowhillfarm.org.uk> <20140503015327.GZ4273@ando> <20140504032142.GH4273@ando> Message-ID: >> A small note about performance here. If your log files are very large >> (say, hundreds of thousands or millions of lines) you will find that >> this part is *horribly horrible slow*. There's two problems, a minor and >> a major one. > > Ah, actually I was mistaken about that. I forgot that for built-in > lists, += augmented assignment is equivalent to calling list.extend(), > so it actually does make the modifications in place. So I was wrong to > say: No problem. Linus's Law wins again. :P (http://en.wikipedia.org/wiki/Linus's_Law) Thank goodness for public mailing lists where we can share our successes and learning experiences together! With regards to the earlier part about using the decoding at the call to open(), rather than on each individual line, next time you'll want to make the point that it's better to do so at open() time not because it's more efficient, but because it's more correct. Correctness needs to be the winning argument here. Encoding is a property of the entire file, not a property on individual lines. In fact, we can get into trouble by doing the decoding piece-wise across lines because certain encodings are multi-byte in nature. What this means is that what might look like a newline in the uninterpreted bytes of a file may be deceptive: that "newline" byte might actually be part of a multibyte character! Let's see if we can construct an example to demonstrate. ######################################################################## for encoding in ('utf-8', 'utf-16', 'utf-32'): for i in range(0x110000): aChar = unichr(i) try: someBytes = aChar.encode(encoding) if '\n' in someBytes: print("%r contains a newline in its bytes encoded with %s" % (aChar, encoding)) except: ## Normally, try/catches with an empty except is a bad idea. ## Here, this is toy code, and we're just exploring. pass ######################################################################## This toy code goes through all possible Unicode code points, and then encodes them in three different codecs. We look to see if any of the encoded characters have newlines in them, and report. Try running it. Notice how many characters start being reported. :P Hopefully, this makes the point clearer: we must not try to decode individual lines. By that time, the damage has been done: the act of trying to break the file into lines by looking naively at newline byte characters is invalid when certain characters can themselves have newline characters. From dyoo at hashcollision.org Sun May 4 06:03:46 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 3 May 2014 21:03:46 -0700 Subject: [Tutor] Logical error? In-Reply-To: References: <536419EE.5030906@barrowhillfarm.org.uk> <20140503015327.GZ4273@ando> <20140504032142.GH4273@ando> Message-ID: > ######################################################################## > for encoding in ('utf-8', 'utf-16', 'utf-32'): > for i in range(0x110000): > aChar = unichr(i) > try: > someBytes = aChar.encode(encoding) > if '\n' in someBytes: > print("%r contains a newline in its bytes encoded with %s" % > (aChar, encoding)) > except: > ## Normally, try/catches with an empty except is a bad idea. > ## Here, this is toy code, and we're just exploring. > pass > ######################################################################## Gaa... Sorry about the bad indenting. Let me try that again. #################################### for encoding in ('utf-8', 'utf-16', 'utf-32'): for i in range(0x110000): aChar = unichr(i) try: someBytes = aChar.encode(encoding) if '\n' in someBytes: print("%r contains a newline in its bytes encoded with %s" % (aChar, encoding)) except: ## Normally, try/catches with an empty except is a bad idea. ## Here, this is toy code, and we're just exploring. pass #################################### > Hopefully, this makes the point clearer: we must not try to decode > individual lines. By that time, the damage has been done: the act of > trying to break the file into lines by looking naively at newline byte > characters is invalid when certain characters can themselves have > newline characters. Confusing last sentence. Let me try that again. The act of trying to break the file into lines by looking naively at newline byte characters is invalid because certain characters, under encoding, themselves consist of newline characters. We've got to open the file with the right encoding in play. Joel Spolsky's article on "The Absolute minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)" needs to be referenced. :P http://www.joelonsoftware.com/articles/Unicode.html From steve at pearwood.info Sun May 4 06:13:12 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 4 May 2014 14:13:12 +1000 Subject: [Tutor] Question about O(N**2) In-Reply-To: References: <20140503221128.GD28262@wdfs> Message-ID: <20140504041311.GJ4273@ando> On Sat, May 03, 2014 at 03:59:40PM -0700, Danny Yoo wrote: > Following up on this. Let's make sure that we're talking about the same thing. > > > The assertion is that the following: > > fullPath += [...] > > where fullPath is a list of strings, exhibits O(n^2) time. I don't > think this is true. Semantically, the statement above should be > equivalent to: > > fullPath.append(...) > > and so I agree with David: this is not O(n^2), but rather O(n). Danny is correct. For built-in lists, += augmented assignment is implemented using the extend method, not the + operator. Consequently, repeatedly calling += modifies the list in place, growing it as needed, which is amortized O(N) rather than O(N**2). Had += been implemented as + instead, as I initially mis-remembered, the behaviour would have been O(N**2). The process would have gone: fullPath = [] fullPath = [] + [a] # add a new path component fullPath = [a] + [b] fullPath = [a, b] + [c] fullPath = [a, b, c] + [d] fullPath = [a, b, c, d] + [e] ... Each individual + is an O(N+M) operation (if there are ten items in the first list, and five items in the second list, adding them needs to copy fifteen items). With M a constant 1, we can take each addition as O(N). And there are N additions, so we get O(N*N) or O(N**2). We can test this by seeing how long it takes to perform a bunch of additions. For timing, I'm going to use the recipe shown here: http://code.activestate.com/recipes/577896-benchmark-code-with-the-with-statement/ First, using augmented assignment for lists: py> size = 1000000 py> with Timer(): ... data = [] ... for i in range(size): ... data += [i] ... time taken: 0.548546 seconds py> with Timer(): ... data = [] ... for i in range(3*size): ... data += [i] ... time taken: 1.672449 seconds By increasing the amount of data by a factor of three, the time taken also increases by about a factor of three. That is O(N). Now let's try list addition. It's so much slower than += that I had to reduce the size by a lot just to have it complete in any reasonable amount of time: py> size = 10000 py> with Timer(): ... data = [] ... for i in range(size): ... data = data + [i] ... time taken: 0.331893 seconds py> with Timer(): ... data = [] ... for i in range(3*size): ... data = data + [i] ... time taken: 3.203508 seconds Here, increasing N by a factor of 3 takes about 10 times longer, which is consistent with O(N**2). -- Steven From jeanpierreda at gmail.com Sun May 4 09:14:05 2014 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Sun, 4 May 2014 00:14:05 -0700 Subject: [Tutor] Question about O(N**2) In-Reply-To: <20140504041311.GJ4273@ando> References: <20140503221128.GD28262@wdfs> <20140504041311.GJ4273@ando> Message-ID: On Sat, May 3, 2014 at 9:13 PM, Steven D'Aprano wrote: > > On Sat, May 03, 2014 at 03:59:40PM -0700, Danny Yoo wrote: > > Following up on this. Let's make sure that we're talking about the same thing. > > > > > > The assertion is that the following: > > > > fullPath += [...] > > > > where fullPath is a list of strings, exhibits O(n^2) time. I don't > > think this is true. Semantically, the statement above should be > > equivalent to: > > > > fullPath.append(...) > > > > and so I agree with David: this is not O(n^2), but rather O(n). > > Danny is correct. For built-in lists, += augmented assignment is > implemented using the extend method, not the + operator. Consequently, > repeatedly calling += modifies the list in place, growing it as needed, > which is amortized O(N) rather than O(N**2). Nit: starting from an empty list, it's worst-case O(N), no "amortized" qualifier necessary. -- Devin From jsmallwood82 at yahoo.com Thu May 1 02:18:43 2014 From: jsmallwood82 at yahoo.com (jordan smallwood) Date: Wed, 30 Apr 2014 17:18:43 -0700 (PDT) Subject: [Tutor] Help With Code Message-ID: <1398903523.34938.YahooMailNeo@web162702.mail.bf1.yahoo.com> Hey there, I have this code below (in to cm conversion) and I want to have the user try again if they enter in a non integer. What am I missing: ConversionConstant = 2.54 def CalculateCentimeters(inches): ? ? return ConversionConstant * inches def CalculateInches(centimeters): ? ? return centimeters / ConversionConstant try: ? ? value = float(raw_input('Please enter a number: ')) except ValueError: ? ? print "Not a valid number." ? ?? conversion_type = raw_input('Please enter a unit of measure (C/I)? ') output = None if conversion_type == 'C': ? ? ? ? output = CalculateInches(value) ? ? ? ? print "That's", output, "inches!" elif conversion_type == 'I': ? ? ? ? output = CalculateCentimeters(value) ? ? ? ? print "That's", output, "centimeters!" -------------- next part -------------- An HTML attachment was scrubbed... URL: From matt at hazza.net Thu May 1 11:35:51 2014 From: matt at hazza.net (Matt Harris) Date: Thu, 1 May 2014 10:35:51 +0100 Subject: [Tutor] Final review In-Reply-To: References: Message-ID: > The ouput for below is 2 when it seems like there should be 3 lists located inside x. Is it [10,20] that is not consider inside of x? Any tips on how to tell how to spot them more clearly? > > x = ['a', [2.0, 5, [10, 20]]] > print len(x) Hey Scott Here, it looks like x is a list containing 'a' and [2.0, 5, [10, 20]], hence why the len(x) gives 2. len(x[1]) gives you 3 ( [2.0, 5, [10, 20]] ) len(x[1][2]) gives you 2 ( [10, 20] ) Matt From felipe146 at hotmail.com Fri May 2 18:40:20 2014 From: felipe146 at hotmail.com (Felipe Melo) Date: Fri, 2 May 2014 13:40:20 -0300 Subject: [Tutor] Using import Message-ID: Hello, I'm starting with Python and I'm trying to work with "import" but I'm having a problem. I have the file c.py (that works when executed) with a function performing a multiplication: def mult(a,x): resul=a*x return(resul)print 'test print'ent1=2ent3=3dedo=mult(ent1,ent3)print 'result: ',dedo and I have the file b.py that calls c.py but should only print a sentence, without use the function in c.py (I tried to use the code without the comment part but got the problem, my goal is to use b.py with the commented part being part of the code): import c ent1=2ent2=7print 'testing b.py without any operation' #saida=c.mult(ent1,ent2)#print 'the output is:',saida My problem is when I execute b.py, it executes c.py before b.py: $ python b.py test printresult: 6testing b.py without any operation How should I work with this? What I want is use the function defined in c.py inside b.py Regards,Felipe -------------- next part -------------- An HTML attachment was scrubbed... URL: From salmanmanekia at gmail.com Sat May 3 01:51:38 2014 From: salmanmanekia at gmail.com (Muhammed Salman) Date: Sat, 3 May 2014 02:51:38 +0300 Subject: [Tutor] Advice needed by a new to Python but abit experience software dev Message-ID: Hi, I have to develop a simple web app. In which i have to create, update, read and delete people. I also should be able to make people friends and best friends with each other. Now, (obviously) I do not want you guys to do this project for me ;). But what I want is that maybe you guys can give me some hints about where to start and make my journey easy by giving me some good pointers on what to look for and what not to. The technologies I have to use for this project are mentioned below : 1. Flask 2. SQLAlchemy 3. PostgreSQL 4. Git + Github 5. WTForms 6. Heroku 7. Bootstrap 8. pytest Unfortunately, I only know about SQL and Git and abit about heroku. All other things are totally new to me. Waiting for advices and if somebody is open for tutoring or some sort of continuous guidance that would be also more than appreciated. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sun May 4 10:09:50 2014 From: __peter__ at web.de (Peter Otten) Date: Sun, 04 May 2014 10:09:50 +0200 Subject: [Tutor] Using import References: Message-ID: Felipe Melo wrote: [Felipe, please post plain text, your code examples are unreadable] > Hello, > I'm starting with Python and I'm trying to work with "import" but I'm > having a problem. I have the file c.py (that works when executed) with a > function performing a multiplication: > def mult(a,x): resul=a*x return(resul)print 'test > print'ent1=2ent3=3dedo=mult(ent1,ent3)print 'result: ',dedo > > and I have the file b.py that calls c.py but should only print a sentence, > without use the function in c.py (I tried to use the code without the > comment part but got the problem, my goal is to use b.py with the > commented part being part of the code): import c ent1=2ent2=7print > 'testing b.py without any operation' > #saida=c.mult(ent1,ent2)#print 'the output is:',saida > > > > My problem is when I execute b.py, it executes c.py before b.py: > $ python b.py test printresult: 6testing b.py without any operation > > > > How should I work with this? What I want is use the function defined in > c.py inside b.py You can prevent code from being executed on import by adding if __name__ == "__main__": ... # executed only when run as a script Example: $ cat hello.py def hello(name): print("Hello, {}".format(name)) if __name__ == "__main__": hello("Peter") $ python hello.py Hello, Peter $ cat hello_felipe.py import hello if __name__ == "__main__": hello.hello("Felipe") $ python hello_felipe.py Hello, Felipe From alan.gauld at btinternet.com Sun May 4 10:41:47 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 04 May 2014 09:41:47 +0100 Subject: [Tutor] New to Python In-Reply-To: References: <1398552813.94073.YahooMailNeo@web162703.mail.bf1.yahoo.com> Message-ID: On 28/04/14 13:56, Jordan Smallwood wrote: > I never got a response. Should I check my spam? Probably, although Dave and I both basically said the same as the current batch of answers. Namely the exercise is pretty clear: write a module with 2 functions. Now, what part of that specifically do you not understand? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Sun May 4 11:00:08 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 04 May 2014 10:00:08 +0100 Subject: [Tutor] Keeping change-in-place vs. copy methods straight In-Reply-To: References: Message-ID: On 28/04/14 19:45, taserian wrote: > I can't claim to be new to programming, but I've dabbled in Python over > and over again to get small problems and puzzles resolved. One thing > that I find I can't keep straight are the methods that change a list in > place, vs. those that return a copy (sometimes transformed) of the list. Join the club, it's (IMHO) one of the worst features of Python, you just have to get used to it. For the specific case of sort you can always use the sorted() function which does return a reference (not a copy!) to the sorted item. > Call me old-fashioned, but my programming experience mostly comes from > languages where you assigned the output of a function to another > variable, so you always had a copy of whatever you were working on. > > var array; > sorted = array.sort(); > > If you didn't care to keep both copies, you could always re-assign the > returned value to the original variable. The problem is if you expect a copy but actually its the original item that gets sorted what is returned is a reference. So you then have to work out if what you get back is a reference to the original or a new copy. And the problem with copies is that if you are dealing with very big data sets holding two copies in memory might not be possible and at the very least would be wasteful of resources. > sorted = arrayList.sort() > > sorted comes back as None, while arrayList has changed its order. Yes, and it's often inconvenient to do it over two lines. That's why sorted() was introduced: result = sorted(myList) And it works with most sortable items including dictionaries (based on the keys unless you specify otherwise) > Is there some sort of rule-of-thumb to determine if a function is > in-place or returns a value? Nope, read the documentation. Fortunately in Python that's usually as simple as typing help(func) at the >>> prompt. The other area where there are lots of slightly weird inconsistencies are the set operations. There are symbol versions and function versions of the same operation that behave slightly differently. You need to watch out for the differences and use the correct version. (The reasons for the differences are clear if you think about it but its still annoying to have such discrepancies.) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Sun May 4 11:05:50 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 04 May 2014 10:05:50 +0100 Subject: [Tutor] Help With Code In-Reply-To: <1398903523.34938.YahooMailNeo@web162702.mail.bf1.yahoo.com> References: <1398903523.34938.YahooMailNeo@web162702.mail.bf1.yahoo.com> Message-ID: On 01/05/14 01:18, jordan smallwood wrote: > Hey there, > > I have this code below (in to cm conversion) and I want to have the user > try again if they enter in a non integer. What am I missing: A loop. There is a common pattern or idiom in Pytthon: while True: get input if input ok: break # exits the loop else: print error # and go round the loop again So in your case it will look like this: while True: > try: > value = float(raw_input('Please enter a number: ')) break # only gets here if no error > except ValueError: > print "Not a valid number." Those two extra lines are all you need. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Sun May 4 11:14:07 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 04 May 2014 10:14:07 +0100 Subject: [Tutor] Advice needed by a new to Python but abit experience software dev In-Reply-To: References: Message-ID: On 03/05/14 00:51, Muhammed Salman wrote: > I have to develop a simple web app. ... I do not want you guys to > do this project for me ;). But what I want is that maybe you guys can > give me some hints about where to start and make my journey easy by > giving me some good pointers on what to look for and what not to. This list is mainly about the Python language and its standard library. Other libraries are best supported by their own forumns. And your list is pretty much all non-standard... > 1. Flask There should be a flask forum > 2. SQLAlchemy Definitely has several fora that discuss it including several web frameworks that use it (eg TurboGears) > 3. PostgreSQL > 4. Git + Github All industry standard and many forums. The Python bindings to PostGres are fairly standard so we can probably help there. > 5. WTForms > 6. Heroku > 7. Bootstrap Never heard of any of these. You probably need to go to their fora. > 8. pytest We can probably help here. > Unfortunately, I only know about SQL and Git and abit about heroku. All > other things are totally new to me. In that case its an interesting choice of technologies! I would focus first on the web framework. I don't personally know much about flask but I've heard good things about it. Thee should be tutorials etc on the flask site and they should give you clues about how the other bits fit in. For example if flask uses SqlAlchemy then the PostGres bits should be almost invisible to you. > is open for tutoring or some sort of continuous guidance that would be > also more than appreciated. We work mainly in a Q&A style so if you have specific questions that you think this list can help with feel free to post. The more specific the question the more specific the answer :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From kwpolska at gmail.com Sun May 4 11:54:47 2014 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Sun, 4 May 2014 11:54:47 +0200 Subject: [Tutor] Advice needed by a new to Python but abit experience software dev In-Reply-To: References: Message-ID: On Sat, May 3, 2014 at 1:51 AM, Muhammed Salman wrote: > Hi, > > I have to develop a simple web app. In which i have to create, update, read > and delete people. I also should be able to make people friends and best > friends with each other. Now, (obviously) I do not want you guys to do this > project for me ;). But what I want is that maybe you guys can give me some > hints about where to start and make my journey easy by giving me some good > pointers on what to look for and what not to. The technologies I have to use > for this project are mentioned below : > > Flask > SQLAlchemy > PostgreSQL > Git + Github > WTForms > Heroku > Bootstrap > pytest That?s kinda advanced ? and far beyond the target of this list. You are better off by asking the main Python mailing list, at https://mail.python.org/mailman/listinfo/python-list ? also available as comp.lang.python on Usenet. You should start by reading the documentation for Flask, Flask-WTF and Flask-SQLAlchemy. All three have awesome, extensive documentation that will help you out greatly and help you get started. (requires Python skills, which you should get from the official Python tutorial if you are an experienced developer.) Also, Bootstrap is a front-end framework: http://getbootstrap.com/ ? and you?re better off asking HTML/CSS people for advice, after you read the extensive docs. -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From steve at pearwood.info Sun May 4 12:31:33 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 4 May 2014 20:31:33 +1000 Subject: [Tutor] Keeping change-in-place vs. copy methods straight In-Reply-To: References: Message-ID: <20140504103132.GM4273@ando> On Sun, May 04, 2014 at 10:00:08AM +0100, Alan Gauld wrote: > For the specific case of sort you can always use > the sorted() function which does return a reference > (not a copy!) to the sorted item. sorted() does make a copy of the list: py> a = [2, 5, 3, 4, 1] py> sorted(a), a ([1, 2, 3, 4, 5], [2, 5, 3, 4, 1]) Perhaps you meant that it didn't copy the individual items inside the list? If so, you were correct: py> a = [ [5, 6], [1, 2], [7, 8], [3, 4] ] py> b = sorted(a) py> b[0].append(999) py> a [[5, 6], [1, 2, 999], [7, 8], [3, 4]] If you want to make copies of the items, you can import the copy module and use copy.deepcopy first. -- Steven From illusiontechniques at gmail.com Sun May 4 13:14:24 2014 From: illusiontechniques at gmail.com (C Smith) Date: Sun, 4 May 2014 07:14:24 -0400 Subject: [Tutor] append vs list addition Message-ID: I had always assumed that append() was more efficient, but some recent discussions seem to point at that it is the same as append(). Which is preferable and why? -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sun May 4 13:28:59 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 04 May 2014 12:28:59 +0100 Subject: [Tutor] Keeping change-in-place vs. copy methods straight In-Reply-To: <20140504103132.GM4273@ando> References: <20140504103132.GM4273@ando> Message-ID: On 04/05/14 11:31, Steven D'Aprano wrote: > On Sun, May 04, 2014 at 10:00:08AM +0100, Alan Gauld wrote: > >> For the specific case of sort you can always use >> the sorted() function which does return a reference >> (not a copy!) to the sorted item. > > sorted() does make a copy of the list: Really? That's a bummer. I assumed (never assume!) that it returned a reference to the original. I really, really, hate the way Python handles this :-( > Perhaps you meant that it didn't copy the individual items inside the > list? Nah, I just got it wrong! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From davea at davea.name Sun May 4 13:36:38 2014 From: davea at davea.name (Dave Angel) Date: Sun, 4 May 2014 07:36:38 -0400 (EDT) Subject: [Tutor] append vs list addition References: Message-ID: C Smith Wrote in message: > > I had always assumed that append() was more efficient, but some recent discussions seem to point at that it is the same as append(). Which is preferable and why? Please be more explicit, preferably with example code. list.append and list.__add__ don't even do the same thing. Perhaps you mean list.extend I would also encourage you to properly use text mail to post, as there are a number of problems triggered by html mails as you're doing now. Most of them don't matter with your current message, however. -- DaveA From davea at davea.name Sun May 4 13:46:49 2014 From: davea at davea.name (Dave Angel) Date: Sun, 4 May 2014 07:46:49 -0400 (EDT) Subject: [Tutor] Help With Code References: <1398903523.34938.YahooMailNeo@web162702.mail.bf1.yahoo.com> Message-ID: jordan smallwood Wrote in message: > > want to have the user try again if they enter in a non integer. What am I missing: Do you perhaps mean float? If so, see the other response. -- DaveA From davea at davea.name Sun May 4 14:48:59 2014 From: davea at davea.name (Dave Angel) Date: Sun, 4 May 2014 08:48:59 -0400 (EDT) Subject: [Tutor] Logical error? References: <536419EE.5030906@barrowhillfarm.org.uk> <20140503015327.GZ4273@ando> <20140504032142.GH4273@ando> Message-ID: Danny Yoo Wrote in message: > > > > >> Hopefully, this makes the point clearer: we must not try to decode >> individual lines. By that time, the damage has been done: the act of >> trying to break the file into lines by looking naively at newline byte >> characters is invalid when certain characters can themselves have >> newline characters. > > Confusing last sentence. Let me try that again. The act of trying to > break the file into lines by looking naively at newline byte > characters is invalid because certain characters, under encoding, > themselves consist of newline characters. We've got to open the file > with the right encoding in play. > > When the file is encoded, it's a binary file until you decode it. You should never use readline or equivalent on a binary file. Some encodings go out of their way to make it seem to work, but taking advantage of such details leaves you at risk when a new file having a different encoding comes along. -- DaveA From davea at davea.name Sun May 4 14:54:14 2014 From: davea at davea.name (Dave Angel) Date: Sun, 4 May 2014 08:54:14 -0400 (EDT) Subject: [Tutor] Keeping change-in-place vs. copy methods straight References: <20140504103132.GM4273@ando> Message-ID: Alan Gauld Wrote in message: >>> (not a copy!) to the sorted item. >> >> sorted() does make a copy of the list: > > Really? That's a bummer. > I assumed (never assume!) that it returned a reference to the original. > I really, really, hate the way Python handles this :-( > It's not clear to me what you would change. Would you only provide methods (like sort) that mangle their object? -- DaveA From illusiontechniques at gmail.com Sun May 4 15:51:17 2014 From: illusiontechniques at gmail.com (C Smith) Date: Sun, 4 May 2014 09:51:17 -0400 Subject: [Tutor] append vs list addition In-Reply-To: References: Message-ID: Sorry. I meant for example: list1 = [1,2,3] list2 = [3,4,5] newList = list1 + list2 versus for x in list2: list1.append(x) Which is the preferred way to add elements from one list to another? On Sun, May 4, 2014 at 7:36 AM, Dave Angel wrote: > C Smith Wrote in message: >> >> > I had always assumed that append() was more efficient, but some recent discussions seem to point at that it is the same as append(). Which is preferable and why? > > Please be more explicit, preferably with example code. > list.append and list.__add__ don't even do the same thing. > Perhaps you mean list.extend > > I would also encourage you to properly use text mail to post, as > there are a number of problems triggered by html mails as you're > doing now. Most of them don't matter with your current message, > however. > > -- > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Sun May 4 16:29:14 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 5 May 2014 00:29:14 +1000 Subject: [Tutor] append vs list addition In-Reply-To: References: Message-ID: <20140504142914.GO4273@ando> On Sun, May 04, 2014 at 09:51:17AM -0400, C Smith wrote: > Sorry. > > I meant for example: > list1 = [1,2,3] > list2 = [3,4,5] > > newList = list1 + list2 This creates a new list, containing the same items as list1 and list2. > versus > > for x in list2: > list1.append(x) This can be written more simply as list1.extend(list2). Either way, it doesn't create a new list, it modifies list1 in place. > Which is the preferred way to add elements from one list to another? Depends on what you want to do. The two code snippets do very different things. In the first case, you end up with three lists: list1 = [1, 2, 3] # unchanged list2 = [3, 4, 5] # unchanged newlist = [1, 2, 3, 3, 4, 5] If that's what you want, then using list addition is exactly the right solution. In the second case, you end up with two lists: list1 = [1, 2, 3, 3, 4, 5] # changed in place list2 = [3, 4, 5] # unchanged If that's the result you want, then using the extend method (or append, repeatedly) is exactly the right solution. Keep in mind that because list1 is changed in place, any other references to it will see the same change: py> data = {'key': list1, 'another': list2} py> list1.extend(list2) py> print data {'another': [3, 4, 5], 'key': [1, 2, 3, 3, 4, 5]} The second example (using extend) is possibly a little faster and more efficient, since it doesn't have to copy list1. But sometimes you *need* a copy, in which case, use addition. -- Steven From __peter__ at web.de Sun May 4 16:29:27 2014 From: __peter__ at web.de (Peter Otten) Date: Sun, 04 May 2014 16:29:27 +0200 Subject: [Tutor] append vs list addition References: Message-ID: C Smith wrote: > I meant for example: > list1 = [1,2,3] > list2 = [3,4,5] > > newList = list1 + list2 > > versus > > for x in list2: > list1.append(x) > > Which is the preferred way to add elements from one list to another? None of the above unless you need to keep the original list1. Use list1.extend(list2) # 1 or list1 += list2 # 2 I prefer (1), but both do the same under the hood: append all items from list2 or any iterable to list1. If you want to preserve the original list1 new_list = list1 + list2 is fine. From zebra05 at gmail.com Sun May 4 22:25:05 2014 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Sun, 4 May 2014 22:25:05 +0200 Subject: [Tutor] PyCountry currency formatting woes Message-ID: Hi everyone, I have a function which accepts an alpha2 country code and a price string, where the aim is to get the country's currency and use the currency.letter property of that currency to format the supplied price string. The above works fine so far - yet it falls over when called with Germany as the country as follows: currency = pycountry.currencies.get(numeric=country.numeric) The function implementation is as follows: def formatPrice(self, alpha2CountryCode, price): """ @param alpha2CountryCode: The 2-character country code for which to format the price value @param price: The price value as a string @return: A string representing the formatted monetary value for this country for this price. #Get country by alpha2 code country = pc.countries.get(alpha2=alpha2CountryCode.upper()) #Get currency by country's numeric and format price currency = pc.currencies.get(numeric=country.numeric) letter = currency.letter formattedCurrency = "%s %s" % (letter, price) return formattedCurrency Any ideas as to what the issue may be? Thanks :) -- Regards, Sithu Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Sun May 4 22:36:06 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 04 May 2014 21:36:06 +0100 Subject: [Tutor] PyCountry currency formatting woes In-Reply-To: References: Message-ID: On 04/05/2014 21:25, Sithembewena Lloyd Dube wrote: > Hi everyone, > > I have a function which accepts an alpha2 country code and a price > string, where the aim is to get the country's currency and use the > currency.letter property of that currency to format the supplied price > string. > > The above works fine so far - yet it falls over when called with Germany > as the country as follows: > > currency = pycountry.currencies.get(numeric=country.numeric) > > The function implementation is as follows: > > def formatPrice(self, alpha2CountryCode, price): > """ > @param alpha2CountryCode: The 2-character country code for > which to format the price value > @param price: The price value as a string > @return: A string representing the formatted monetary value for > this country for this price. > > #Get country by alpha2 code > country = pc.countries.get(alpha2=alpha2CountryCode.upper()) > > #Get currency by country's numeric and format price > currency = pc.currencies.get(numeric=country.numeric) > letter = currency.letter > formattedCurrency = "%s %s" % (letter, price) > > return formattedCurrency > > Any ideas as to what the issue may be? > > Thanks :) > > -- > Regards, > Sithu Lloyd Dube > Sorry but this list is aimed at people learning the core Python language, not a third party module, which I've never heard of incidentally. You might get lucky if some regular here has used the package, but even then "it falls over" isn't too helpful. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From zebra05 at gmail.com Sun May 4 22:55:50 2014 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Sun, 4 May 2014 22:55:50 +0200 Subject: [Tutor] PyCountry currency formatting woes In-Reply-To: References: Message-ID: Thanks, i was actually getting the error information to update the post. Apoligies to waste your time posting here - I could not find an appropriate PyCountry discussion list and my next best bet seemed to be a Python users' list. For those who care to look, the error is as follows (a concise example from an interactive shell: import pycountry country = pycountry.countries.get(alpha2='DE') currency = pycountry.currencies.get(numeric=country.numeric) Traceback (most recent call last): File "", line 1, in File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get return self.indices[field][value] KeyError: '276' The obvious issue here is that the pycountry.countries collection does not contain a currency with a numeric of 276 (Germany's numeric) - yet it does contain the Euro. Any ideas as to what the way around this may be? On Sun, May 4, 2014 at 10:25 PM, Sithembewena Lloyd Dube wrote: > Hi everyone, > > I have a function which accepts an alpha2 country code and a price string, > where the aim is to get the country's currency and use the currency.letter > property of that currency to format the supplied price string. > > The above works fine so far - yet it falls over when called with Germany > as the country as follows: > > currency = pycountry.currencies.get(numeric=country.numeric) > > The function implementation is as follows: > > def formatPrice(self, alpha2CountryCode, price): > """ > @param alpha2CountryCode: The 2-character country code for which > to format the price value > @param price: The price value as a string > @return: A string representing the formatted monetary value for > this country for this price. > > #Get country by alpha2 code > country = pc.countries.get(alpha2=alpha2CountryCode.upper()) > > #Get currency by country's numeric and format price > currency = pc.currencies.get(numeric=country.numeric) > letter = currency.letter > formattedCurrency = "%s %s" % (letter, price) > > return formattedCurrency > > Any ideas as to what the issue may be? > > Thanks :) > > -- > Regards, > Sithu Lloyd Dube > -- Regards, Sithu Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: From brian.van.den.broek at gmail.com Mon May 5 01:00:24 2014 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Sun, 4 May 2014 19:00:24 -0400 Subject: [Tutor] sending email via SMTP: code review requested Message-ID: Hi all, I am playing with the smtp and email modules from the standard library of Python 2.7.3 (I also want it to run on 2.6.6). I've not found the going easy; the SMTP and RFC 2822 standards are not ones I have worked with before. I have something that works, but I am not confident I am doing the right thing. For that matter, I am not very confident that I am not doing the wrong thing. I would very much appreciate some more experienced eyes on the code below. In addition to any outright errors concerning interaction with an SMTP server and constructing a MIME message, I would of course also welcome style comments. (Preemptively, I will note it isn't obvious I ought to have gone OOP with this.) I should also mention that I am writing this code as part of some tools to send myself and others reminder emails, the tools to be run from a cron job. I am storing an actual email account password in plaintext in my code. But, the account in question is one established just for the purpose of the reminder project and similar projects; it is not an account which houses my plans for world domination or the like. That said, I have removed the account name and password string below; it will thus require some adjustments to run for testing. And, as I side note, could anyone explain why changing a first world of a body line 'From' to '>From' is the preferred standard? I understand what the problem is that is being solved, but as most email clients interpret a leading '>' as an indication of quoting, I would have thought ' From' or something like '-From' would have been better. If I have my own code deal with the problem in one of these ways, will I be breaking anything? Anyway, thanks and best, Brian vdB import smtplib class SMTPSender(object): def __init__(self, server, port, sender, password, messages): self.server = server self.port = port self.sender = sender self.password = password self.messages = messages self._connect() try: self._send() finally: self._logout() def _connect(self): self.session = smtplib.SMTP(server, port) self.session.ehlo() self.session.starttls() self.session.ehlo self.session.login(sender, password) def _send(self): for message in self.messages: to_addresses = message["To"].split(",") self.session.sendmail(sender, to_addresses, message.as_string()) def _logout(self): self.session.quit() if __name__ == "__main__": server = "smtp.gmail.com" port = 587 sender = "myfunnyhandle at gmail.com" password = "mysecret" from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart # Quick and dirty test message msg = MIMEMultipart("alternative") msg["Subject"] = "SMTP Test MIMEText plain" msg["From"] = sender # Setting to anything but sender gets removed by gmail. msg["To"] = "someone at example.com, someoneelse at example.com" msg["Reply-to"] = "answerhere at example.com" body = "\n\n".join(["Test msg MIME Text", "From is a problem when occuring as the first word of a line."]) msg.attach(MIMEText(body, "plain")) sender = SMTPSender(server, port, sender, password, [msg,]) From mik.stephen at yahoo.com Mon May 5 01:17:57 2014 From: mik.stephen at yahoo.com (Stephen Mik) Date: Sun, 4 May 2014 16:17:57 -0700 (PDT) Subject: [Tutor] Stephen Mik-Novice Python Programmer(Version 3.4.0)Can't get help using Dictionaries, Lists to 'Hangman Game Problem" Message-ID: <1399245477.43168.YahooMailNeo@web124701.mail.ne1.yahoo.com> Dear Python World: ??? I am almost brand new to Python 3.4.0 and am taking a beginning Python Programming class at the nearby Community College. One major problem I have is time management with beginning pseudo code and coding for my Class Assignments. The instructor prefers Office Hour help,and does not respond to email. ??? One of my Class Assignments due on May 9,2014 deals with making a version of the "Hangman Game" which I'm sure somebody out there is familiar with. The program problem says to use 1.Dictionaries 2. Lists 3.Embedded While lists 4.for loops and more. I can refer to the Textbook for the class to get a basic idea of what some of the code will look like,but I cannot fathom how to use a Dictionary or List? in order to enhance the Basic Hangman Game Problem in the text? (written by Michael Dawson). The Text does cover "Dictionaries","Lists","While Loops","For Loops" but doesn't "amplify" and show with examples how some of these Data Structures can be used. And my instructor's Assignment directions are very terse and "you do it by yourself". ??? If I can't get some ideas and hints from the Python Community by Monday,May 5,2014, I will have to hold off this difficult? programming assignment until Tuesday,May 6,2014 when the instructor will be available to help. That leaves me just 3 days to pseudocode,code in Python,fix syntax and run-time and Logical errors before I can turn it in by Friday,May 16,2014. The instructor will only probably give me a suggestion and not some tangible ideas.Any thoughts out there about how to implement the Loops and Data Structures into a "Hangman Game" programming problem? SINCERELY,Stephen W. Mik -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Mon May 5 01:45:28 2014 From: illusiontechniques at gmail.com (C Smith) Date: Sun, 4 May 2014 19:45:28 -0400 Subject: [Tutor] Stephen Mik-Novice Python Programmer(Version 3.4.0)Can't get help using Dictionaries, Lists to 'Hangman Game Problem" In-Reply-To: <1399245477.43168.YahooMailNeo@web124701.mail.ne1.yahoo.com> References: <1399245477.43168.YahooMailNeo@web124701.mail.ne1.yahoo.com> Message-ID: Hey, you will want to include some code to show your progress so far. Can you write a basic program and then work the requirements into it? Do you have some idea of where to start? Are you supposed to modify a completed version of "hangman" that is in your text, or come up with an original 'hangman' program? Does the hangman game need a graphical interface, or is just printing words to the screen fine? On Sun, May 4, 2014 at 7:17 PM, Stephen Mik wrote: > Dear Python World: > I am almost brand new to Python 3.4.0 and am taking a beginning Python > Programming class at the nearby Community College. One major problem I have > is time management with beginning pseudo code and coding for my Class > Assignments. The instructor prefers Office Hour help,and does not respond to > email. > One of my Class Assignments due on May 9,2014 deals with making a > version of the "Hangman Game" which I'm sure somebody out there is familiar > with. The program problem says to use 1.Dictionaries 2. Lists 3.Embedded > While lists 4.for loops and more. I can refer to the Textbook for the class > to get a basic idea of what some of the code will look like,but I cannot > fathom how to use a Dictionary or List in order to enhance the Basic > Hangman Game Problem in the text (written by Michael Dawson). The Text does > cover "Dictionaries","Lists","While Loops","For Loops" but doesn't "amplify" > and show with examples how some of these Data Structures can be used. And my > instructor's Assignment directions are very terse and "you do it by > yourself". > If I can't get some ideas and hints from the Python Community by > Monday,May 5,2014, I will have to hold off this difficult programming > assignment until Tuesday,May 6,2014 when the instructor will be available to > help. That leaves me just 3 days to pseudocode,code in Python,fix syntax and > run-time and Logical errors before I can turn it in by Friday,May 16,2014. > The instructor will only probably give me a suggestion and not some tangible > ideas.Any thoughts out there about how to implement the Loops and Data > Structures into a "Hangman Game" programming problem? > SINCERELY,Stephen W. Mik > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From jakenblank at gmail.com Sun May 4 23:04:01 2014 From: jakenblank at gmail.com (Jake Blank) Date: Sun, 4 May 2014 17:04:01 -0400 Subject: [Tutor] Alice_in_wonderland Question Message-ID: Hi, So I'm doing a problem on the Alice_in_wonderland.txt where I have to write a program that reads a piece of text from a file specified by the user, counts the number of occurrences of each word, and writes a sorted list of words and their counts to an output file. The list of words should be sorted based on the counts, so that the most popular words appear at the top. Words with the same counts should be sorted alphabetically. My code right now is word_count = {} file = open ('alice_in_wonderland.txt', 'r') full_text = file.read().replace('--',' ') full_text_words = full_text.split() for words in full_text_words: stripped_words = words.strip(".,!?'`\"- ();:") try: word_count[stripped_words] += 1 except KeyError: word_count[stripped_words] = 1 ordered_keys = word_count.keys() sorted(ordered_keys) print ("All the words and their frequency in", 'alice in wonderland') for k in ordered_keys: print (k, word_count[k]) The Output here is just all of the words in the document NOT SORTED by amount of occurrence. I need help sorting this output of words in the Alice_in_wonderland.txt, as well as help asking the user for the input information about the files. If anyone could give me some guidance you will really be helping me out. Please and Thank you -------------- next part -------------- An HTML attachment was scrubbed... URL: From brian.van.den.broek at gmail.com Mon May 5 03:16:59 2014 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Sun, 4 May 2014 21:16:59 -0400 Subject: [Tutor] Alice_in_wonderland Question In-Reply-To: References: Message-ID: On May 4, 2014 8:31 PM, "Jake Blank" wrote: > > Hi, > So I'm doing a problem on the Alice_in_wonderland.txt where I have to write a program that reads a piece of text from a file specified by the user, counts the number of occurrences of each word, and writes a sorted list of words and their counts to an output file. The list of words should be sorted based on the counts, so that the most popular words appear at the top. Words with the same counts should be sorted alphabetically. > > My code right now is > > word_count = {} > file = open ('alice_in_wonderland.txt', 'r') > full_text = file.read().replace('--',' ') > full_text_words = full_text.split() > > for words in full_text_words: > stripped_words = words.strip(".,!?'`\"- ();:") > try: > word_count[stripped_words] += 1 > except KeyError: > word_count[stripped_words] = 1 > > ordered_keys = word_count.keys() > sorted(ordered_keys) > print ("All the words and their frequency in", 'alice in wonderland') > for k in ordered_keys: > print (k, word_count[k]) > > The Output here is just all of the words in the document NOT SORTED by amount of occurrence. > I need help sorting this output of words in the Alice_in_wonderland.txt, as well as help asking the user for the input information about the files. > > If anyone could give me some guidance you will really be helping me out. > > Please and Thank you Hi Jake, You are sorting the dictionary keys by the keys themselves, whereas what you want is the keys sorted by their associated values. Look at the key parameter in https://docs.python.org/3.4/library/functions.html#sorted. To get you started, here is an example in the vicinity: >>> data = ['abiab', 'cdocd', 'efaef', 'ghbgh'] >>> sorted(data) ['abiab', 'cdocd', 'efaef', 'ghbgh'] >>> sorted(data, key=lambda x:x[2]) ['efaef', 'ghbgh', 'abiab', 'cdocd'] >>> def get_third(x): return x[2] ... >>> sorted(data, key=get_third) ['efaef', 'ghbgh', 'abiab', 'cdocd'] >>> In case the lambda version is confusing, it is simply a way of doing the get_third version without having to create a function outside of the context of the sorted expression. If that sorts you, great. If not, please do ask a follow-up. (I was trying not to do it for you, but also not to frustrate by giving you too little of a push.) Best, Brian vdB From illusiontechniques at gmail.com Mon May 5 03:45:41 2014 From: illusiontechniques at gmail.com (C Smith) Date: Sun, 4 May 2014 21:45:41 -0400 Subject: [Tutor] Alice_in_wonderland Question In-Reply-To: References: Message-ID: >ordered_keys = word_count.keys() >sorted(ordered_keys) sorted() does not modify the list, but returns a sorted version of the list for me on Python 2.7 my_sorted_list = sorted(ordered_keys) This will alphabetize all of the words, regardless of frequency. >print ("All the words and their frequency in", 'alice in wonderland') >for k in ordered_keys: for k in my_sorted_keys: > print (k, word_count[k]) This will still print out all the words alphabetically, regardless of frequency. Maybe you could have a different dictionary for each value, for example, a dictionary of all the words that appear once, twice, three times. By 'asking the user for input information' do you mean so they could pass any txt file into the program to be sorted? On Sun, May 4, 2014 at 5:04 PM, Jake Blank wrote: > Hi, > So I'm doing a problem on the Alice_in_wonderland.txt where I have to write > a program that reads a piece of text from a file specified by the user, > counts the number of occurrences of each word, and writes a sorted list of > words and their counts to an output file. The list of words should be sorted > based on the counts, so that the most popular words appear at the top. Words > with the same counts should be sorted alphabetically. > > My code right now is > > word_count = {} > file = open ('alice_in_wonderland.txt', 'r') > full_text = file.read().replace('--',' ') > full_text_words = full_text.split() > > for words in full_text_words: > stripped_words = words.strip(".,!?'`\"- ();:") > try: > word_count[stripped_words] += 1 > except KeyError: > word_count[stripped_words] = 1 > > ordered_keys = word_count.keys() > sorted(ordered_keys) > print ("All the words and their frequency in", 'alice in wonderland') > for k in ordered_keys: > print (k, word_count[k]) > > The Output here is just all of the words in the document NOT SORTED by > amount of occurrence. > I need help sorting this output of words in the Alice_in_wonderland.txt, as > well as help asking the user for the input information about the files. > > If anyone could give me some guidance you will really be helping me out. > > Please and Thank you > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From brian.van.den.broek at gmail.com Mon May 5 04:19:38 2014 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Sun, 4 May 2014 22:19:38 -0400 Subject: [Tutor] Alice_in_wonderland Question In-Reply-To: References: Message-ID: Hi Jake, Please do be sure to use Reply All rather than just Reply. I'm sending my reply and and quotes from yours to the list; that way, others can follow along, learn and help. Also, in general, reply under the messages to which you respond, ideally trimming what isn't needed away. (You will see that is what I have done below.) Yes, that's not how email is used outside of technical circles. I'd maintain the technical circles's preference for not top posting is right. But, right or wrong, it is what those whom you are asking for free help prefer, so it is prudent to do it, gritting your teeth if you must :-) On 4 May 2014 21:36, Jake Blank wrote: > Hey Thanks for responding. > > So now my code looks like this: > from wordtools import extract_words > > source_filepath=input("Enter the path to the source file:") > dest_filepath =input("Enter the path to the destination file:") > > sourcef=open(source_filepath, 'r') > destf=open(dest_filepath, 'w') > for line in sourcef: > destf.write(line) > file=input ("Would you like to process another file?(Y/N):") > if file== "Y": > source_filepath=input("Enter the path to the source file:") > dest_filepath =input("Enter the path to the destination file:") > else: > word_count = {} > file = open (source_filepath, 'r') > full_text = file.read().replace('--',' ') > full_text_words = full_text.split() > > for words in full_text_words: > stripped_words = words.strip(".,!?'`\"- ();:") > try: > word_count[stripped_words] += 1 > except KeyError: > word_count[stripped_words] = 1 > > ordered_keys = word_count.keys() > sorted(ordered_keys) > print ('This is the output file for Alice in Wonderland') > for k in sorted(ordered_keys): > print (k, word_count[k]) > > The first part about the user specifying the file is a little off but > besides that I am able to return all of the words in the story with the > number of times they occur alphabetically. In order to return the sorted > list by number of times that each word occurs I am a little confused if i > have to change something in my print statement? I understand how i have to > sort the words by their associated values i'm confused where in my code i > would do that. > > Thanks, Jake > On Sun, May 4, 2014 at 9:16 PM, Brian van den Broek > wrote: >> >> On May 4, 2014 8:31 PM, "Jake Blank" wrote: >> Hi Jake, >> >> You are sorting the dictionary keys by the keys themselves, whereas >> what you want is the keys sorted by their associated values. >> >> Look at the key parameter in >> https://docs.python.org/3.4/library/functions.html#sorted. >> >> To get you started, here is an example in the vicinity: >> >> >>> data = ['abiab', 'cdocd', 'efaef', 'ghbgh'] >> >>> sorted(data) >> ['abiab', 'cdocd', 'efaef', 'ghbgh'] >> >>> sorted(data, key=lambda x:x[2]) >> ['efaef', 'ghbgh', 'abiab', 'cdocd'] >> >>> def get_third(x): return x[2] >> ... >> >>> sorted(data, key=get_third) >> ['efaef', 'ghbgh', 'abiab', 'cdocd'] >> >>> >> >> In case the lambda version is confusing, it is simply a way of doing >> the get_third version without having to create a function outside of >> the context of the sorted expression. >> >> If that sorts you, great. If not, please do ask a follow-up. (I was >> trying not to do it for you, but also not to frustrate by giving you >> too little of a push.) So, the code in your second message didn't seem to reflect any changes in light of the hint I gave. Did you not see how to apply it? If so, that's fine. But, rather than leave me guessing, it would be better to say; that way I, or others in the thread, can better direct efforts to help. Does this help more? >>> data = {'a':2, 'b':4, 'c':3, 'd':1} >>> sorted(data,key=lambda x:data[x]) ['d', 'a', 'c', 'b'] >>> sorted(data,key=lambda x:data[x]) ['d', 'a', 'c', 'b'] >>> data = {'a':2, 'b':4, 'c':3, 'd':1} >>> sorted(data) ['a', 'b', 'c', 'd'] >>> sorted(data,key=lambda x:data[x]) ['d', 'a', 'c', 'b'] >>> for letter in sorted(data,key=lambda x:data[x]): ... print(letter, data[letter]) ... d 1 a 2 c 3 b 4 >>> for letter in sorted(data,key=lambda x:data[x],reverse=True): ... print(letter, data[letter]) ... b 4 c 3 a 2 d 1 >>> Can you see how key=lambda x:data[x] is forcing the sort to consider not the keys of the dictionary, but their associated values? Best, Brian vdB From brian.van.den.broek at gmail.com Mon May 5 05:35:23 2014 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Sun, 4 May 2014 23:35:23 -0400 Subject: [Tutor] Alice_in_wonderland Question In-Reply-To: References: Message-ID: On May 4, 2014 11:13 PM, "Jake Blank" wrote: > > To figure that last part out I just did a simple if statement. > for k in sorted(word_count, key=lambda x:word_count[x], reverse=True): > if word_count[k] >=300: > print (k, word_count[k]) > And the output was correct. Jake, That was in an effort to get only the 15most frequent words, if I understood your earliest post aright. If so, even if the output was correct for the case, that is by accident. What happens in only 3 distinct words show up 300 times? If 42 do? You need to rethink, I think. Do you know how list indexing and list slicing work? > I did have one more question though. > > import os > from wordtools import extract_words > > source_filepath=input("Enter the path to the source file:") > dest_filepath =input("Enter the path to the destination file:") > > sourcef=open(source_filepath, 'r') > destf=open(dest_filepath, 'w') > for line in sourcef: > destf.write(line) > file=input ("Would you like to process another file?(Y/N):") > if file== "Y": > source_filepath=input("Enter the path to the source file:") > dest_filepath =input("Enter the path to the destination file:") > else: > > This code asks the user for a source/dest_filepath. > I'm wondering how I can make it so the program can tell if the source/dest_filepath the user entered is actually a program on the computer. " a program on the computer"? I assume that you mean file. What happens if you run open(" nowayyouhaveafilecalledthiscausethatdbesilly", 'r') That's rather different than if the path is the path of an existing file, right. So, you could try to react differently to the two sorts of results. Do you know how to use try an except? > Also i have to ask the user if they would like to "process another file(Y/N)?" and I'm not sure where to put that. I shall leave this for others; I'm not ready for my Monday, yet. Best, Brian vdB -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Mon May 5 06:26:43 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Sun, 4 May 2014 21:26:43 -0700 Subject: [Tutor] PyCountry currency formatting woes In-Reply-To: References: Message-ID: On Sun, May 4, 2014 at 1:55 PM, Sithembewena Lloyd Dube wrote: > Thanks, i was actually getting the error information to update the post. > Apoligies to waste your time posting here - I could not find an appropriate > PyCountry discussion list and my next best bet seemed to be a Python users' > list. > > You also posted on StackOverflow; I just answered you there. In short: the currency numeric is not guaranteed to be the same as the country numeric (in the case of the Euro, how could it possibly be?) The numeric for DE is 276; the numeric for the Euro is 978. Obviously they don't match. PyCountry is a wrapper around some tables provided by Debian; those tables don't include a country/currency mapping. You can find those mapping tables at http://www.currency-iso.org/en/home/tables/table-a1.html and roll your own wrapper; I'm sure it's been done a thousand times before, but I'm not aware of a Python package that does this. -------------- next part -------------- An HTML attachment was scrubbed... URL: From jakenblank at gmail.com Mon May 5 04:38:01 2014 From: jakenblank at gmail.com (Jake Blank) Date: Sun, 4 May 2014 22:38:01 -0400 Subject: [Tutor] Alice_in_wonderland Question In-Reply-To: References: Message-ID: Hi, I finally got it. This was the code: for k in sorted(word_count, key=lambda x:word_count[x], reverse=True): print (k, word_count[k]) The only question i have now is how to limit the amount of returns the program runs to the first 15 results. On Sun, May 4, 2014 at 10:19 PM, Brian van den Broek < brian.van.den.broek at gmail.com> wrote: > Hi Jake, > > Please do be sure to use Reply All rather than just Reply. I'm sending > my reply and and quotes from yours to the list; that way, others can > follow along, learn and help. > > Also, in general, reply under the messages to which you respond, > ideally trimming what isn't needed away. (You will see that is what I > have done below.) Yes, that's not how email is used outside of > technical circles. I'd maintain the technical circles's preference for > not top posting is right. But, right or wrong, it is what those whom > you are asking for free help prefer, so it is prudent to do it, > gritting your teeth if you must :-) > > On 4 May 2014 21:36, Jake Blank wrote: > > Hey Thanks for responding. > > > > So now my code looks like this: > > from wordtools import extract_words > > > > source_filepath=input("Enter the path to the source file:") > > dest_filepath =input("Enter the path to the destination file:") > > > > sourcef=open(source_filepath, 'r') > > destf=open(dest_filepath, 'w') > > for line in sourcef: > > destf.write(line) > > file=input ("Would you like to process another file?(Y/N):") > > if file== "Y": > > source_filepath=input("Enter the path to the source file:") > > dest_filepath =input("Enter the path to the destination file:") > > else: > > word_count = {} > > file = open (source_filepath, 'r') > > full_text = file.read().replace('--',' ') > > full_text_words = full_text.split() > > > > for words in full_text_words: > > stripped_words = words.strip(".,!?'`\"- ();:") > > try: > > word_count[stripped_words] += 1 > > except KeyError: > > word_count[stripped_words] = 1 > > > > ordered_keys = word_count.keys() > > sorted(ordered_keys) > > print ('This is the output file for Alice in Wonderland') > > for k in sorted(ordered_keys): > > print (k, word_count[k]) > > > > The first part about the user specifying the file is a little off but > > besides that I am able to return all of the words in the story with the > > number of times they occur alphabetically. In order to return the sorted > > list by number of times that each word occurs I am a little confused if i > > have to change something in my print statement? I understand how i have > to > > sort the words by their associated values i'm confused where in my code i > > would do that. > > > > Thanks, Jake > > > On Sun, May 4, 2014 at 9:16 PM, Brian van den Broek > > wrote: > >> > >> On May 4, 2014 8:31 PM, "Jake Blank" wrote: > > > > > > >> Hi Jake, > >> > >> You are sorting the dictionary keys by the keys themselves, whereas > >> what you want is the keys sorted by their associated values. > >> > >> Look at the key parameter in > >> https://docs.python.org/3.4/library/functions.html#sorted. > >> > >> To get you started, here is an example in the vicinity: > >> > >> >>> data = ['abiab', 'cdocd', 'efaef', 'ghbgh'] > >> >>> sorted(data) > >> ['abiab', 'cdocd', 'efaef', 'ghbgh'] > >> >>> sorted(data, key=lambda x:x[2]) > >> ['efaef', 'ghbgh', 'abiab', 'cdocd'] > >> >>> def get_third(x): return x[2] > >> ... > >> >>> sorted(data, key=get_third) > >> ['efaef', 'ghbgh', 'abiab', 'cdocd'] > >> >>> > >> > >> In case the lambda version is confusing, it is simply a way of doing > >> the get_third version without having to create a function outside of > >> the context of the sorted expression. > >> > >> If that sorts you, great. If not, please do ask a follow-up. (I was > >> trying not to do it for you, but also not to frustrate by giving you > >> too little of a push.) > > > So, the code in your second message didn't seem to reflect any changes > in light of the hint I gave. Did you not see how to apply it? If so, > that's fine. But, rather than leave me guessing, it would be better to > say; that way I, or others in the thread, can better direct efforts to > help. > > Does this help more? > > >>> data = {'a':2, 'b':4, 'c':3, 'd':1} > >>> sorted(data,key=lambda x:data[x]) > ['d', 'a', 'c', 'b'] > >>> sorted(data,key=lambda x:data[x]) > ['d', 'a', 'c', 'b'] > >>> data = {'a':2, 'b':4, 'c':3, 'd':1} > >>> sorted(data) > ['a', 'b', 'c', 'd'] > >>> sorted(data,key=lambda x:data[x]) > ['d', 'a', 'c', 'b'] > >>> for letter in sorted(data,key=lambda x:data[x]): > ... print(letter, data[letter]) > ... > d 1 > a 2 > c 3 > b 4 > >>> for letter in sorted(data,key=lambda x:data[x],reverse=True): > ... print(letter, data[letter]) > ... > b 4 > c 3 > a 2 > d 1 > >>> > > Can you see how key=lambda x:data[x] is forcing the sort to consider > not the keys of the dictionary, but their associated values? > > Best, > > Brian vdB > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jakenblank at gmail.com Mon May 5 05:13:54 2014 From: jakenblank at gmail.com (Jake Blank) Date: Sun, 4 May 2014 23:13:54 -0400 Subject: [Tutor] Alice_in_wonderland Question In-Reply-To: References: Message-ID: To figure that last part out I just did a simple if statement. for k in sorted(word_count, key=lambda x:word_count[x], reverse=True): if word_count[k] >=300: print (k, word_count[k]) And the output was correct. I did have one more question though. import os from wordtools import extract_words source_filepath=input("Enter the path to the source file:") dest_filepath =input("Enter the path to the destination file:") sourcef=open(source_filepath, 'r') destf=open(dest_filepath, 'w') for line in sourcef: destf.write(line) file=input ("Would you like to process another file?(Y/N):") if file== "Y": source_filepath=input("Enter the path to the source file:") dest_filepath =input("Enter the path to the destination file:") else: This code asks the user for a source/dest_filepath. I'm wondering how I can make it so the program can tell if the source/dest_filepath the user entered is actually a program on the computer. Also i have to ask the user if they would like to "process another file(Y/N)?" and I'm not sure where to put that. On Sun, May 4, 2014 at 10:38 PM, Jake Blank wrote: > Hi, > > I finally got it. > This was the code: > for k in sorted(word_count, key=lambda x:word_count[x], reverse=True): > print (k, word_count[k]) > > The only question i have now is how to limit the amount of returns the > program runs to the first 15 results. > > > > > On Sun, May 4, 2014 at 10:19 PM, Brian van den Broek < > brian.van.den.broek at gmail.com> wrote: > >> Hi Jake, >> >> Please do be sure to use Reply All rather than just Reply. I'm sending >> my reply and and quotes from yours to the list; that way, others can >> follow along, learn and help. >> >> Also, in general, reply under the messages to which you respond, >> ideally trimming what isn't needed away. (You will see that is what I >> have done below.) Yes, that's not how email is used outside of >> technical circles. I'd maintain the technical circles's preference for >> not top posting is right. But, right or wrong, it is what those whom >> you are asking for free help prefer, so it is prudent to do it, >> gritting your teeth if you must :-) >> >> On 4 May 2014 21:36, Jake Blank wrote: >> > Hey Thanks for responding. >> > >> > So now my code looks like this: >> > from wordtools import extract_words >> > >> > source_filepath=input("Enter the path to the source file:") >> > dest_filepath =input("Enter the path to the destination file:") >> > >> > sourcef=open(source_filepath, 'r') >> > destf=open(dest_filepath, 'w') >> > for line in sourcef: >> > destf.write(line) >> > file=input ("Would you like to process another file?(Y/N):") >> > if file== "Y": >> > source_filepath=input("Enter the path to the source file:") >> > dest_filepath =input("Enter the path to the destination file:") >> > else: >> > word_count = {} >> > file = open (source_filepath, 'r') >> > full_text = file.read().replace('--',' ') >> > full_text_words = full_text.split() >> > >> > for words in full_text_words: >> > stripped_words = words.strip(".,!?'`\"- ();:") >> > try: >> > word_count[stripped_words] += 1 >> > except KeyError: >> > word_count[stripped_words] = 1 >> > >> > ordered_keys = word_count.keys() >> > sorted(ordered_keys) >> > print ('This is the output file for Alice in Wonderland') >> > for k in sorted(ordered_keys): >> > print (k, word_count[k]) >> > >> > The first part about the user specifying the file is a little off but >> > besides that I am able to return all of the words in the story with the >> > number of times they occur alphabetically. In order to return the >> sorted >> > list by number of times that each word occurs I am a little confused if >> i >> > have to change something in my print statement? I understand how i >> have to >> > sort the words by their associated values i'm confused where in my code >> i >> > would do that. >> > >> > Thanks, Jake >> >> > On Sun, May 4, 2014 at 9:16 PM, Brian van den Broek >> > wrote: >> >> >> >> On May 4, 2014 8:31 PM, "Jake Blank" wrote: >> >> >> >> >> >> >> Hi Jake, >> >> >> >> You are sorting the dictionary keys by the keys themselves, whereas >> >> what you want is the keys sorted by their associated values. >> >> >> >> Look at the key parameter in >> >> https://docs.python.org/3.4/library/functions.html#sorted. >> >> >> >> To get you started, here is an example in the vicinity: >> >> >> >> >>> data = ['abiab', 'cdocd', 'efaef', 'ghbgh'] >> >> >>> sorted(data) >> >> ['abiab', 'cdocd', 'efaef', 'ghbgh'] >> >> >>> sorted(data, key=lambda x:x[2]) >> >> ['efaef', 'ghbgh', 'abiab', 'cdocd'] >> >> >>> def get_third(x): return x[2] >> >> ... >> >> >>> sorted(data, key=get_third) >> >> ['efaef', 'ghbgh', 'abiab', 'cdocd'] >> >> >>> >> >> >> >> In case the lambda version is confusing, it is simply a way of doing >> >> the get_third version without having to create a function outside of >> >> the context of the sorted expression. >> >> >> >> If that sorts you, great. If not, please do ask a follow-up. (I was >> >> trying not to do it for you, but also not to frustrate by giving you >> >> too little of a push.) >> >> >> So, the code in your second message didn't seem to reflect any changes >> in light of the hint I gave. Did you not see how to apply it? If so, >> that's fine. But, rather than leave me guessing, it would be better to >> say; that way I, or others in the thread, can better direct efforts to >> help. >> >> Does this help more? >> >> >>> data = {'a':2, 'b':4, 'c':3, 'd':1} >> >>> sorted(data,key=lambda x:data[x]) >> ['d', 'a', 'c', 'b'] >> >>> sorted(data,key=lambda x:data[x]) >> ['d', 'a', 'c', 'b'] >> >>> data = {'a':2, 'b':4, 'c':3, 'd':1} >> >>> sorted(data) >> ['a', 'b', 'c', 'd'] >> >>> sorted(data,key=lambda x:data[x]) >> ['d', 'a', 'c', 'b'] >> >>> for letter in sorted(data,key=lambda x:data[x]): >> ... print(letter, data[letter]) >> ... >> d 1 >> a 2 >> c 3 >> b 4 >> >>> for letter in sorted(data,key=lambda x:data[x],reverse=True): >> ... print(letter, data[letter]) >> ... >> b 4 >> c 3 >> a 2 >> d 1 >> >>> >> >> Can you see how key=lambda x:data[x] is forcing the sort to consider >> not the keys of the dictionary, but their associated values? >> >> Best, >> >> Brian vdB >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From japhy at pearachute.com Mon May 5 07:16:00 2014 From: japhy at pearachute.com (Japhy Bartlett) Date: Mon, 5 May 2014 00:16:00 -0500 Subject: [Tutor] sending email via SMTP: code review requested In-Reply-To: References: Message-ID: The "from" quirk is because it gets parsed as a header, I think. Sending is pretty simple, you should be OK. It may be worth setting up an outgoing-only mail server like postfix that only listens in localhost, gmail can be fussy about quotas. On Sunday, May 4, 2014, Brian van den Broek wrote: > Hi all, > > I am playing with the smtp and email modules from the standard library > of Python 2.7.3 (I also want it to run on 2.6.6). I've not found the > going easy; the SMTP and RFC 2822 standards are not ones I have worked > with before. I have something that works, but I am not confident I am > doing the right thing. For that matter, I am not very confident that I > am not doing the wrong thing. > > I would very much appreciate some more experienced eyes on the code below. > In addition to any outright errors concerning interaction with an SMTP > server and constructing a MIME message, I would of course also welcome > style comments. (Preemptively, I will note it isn't obvious I ought to > have gone OOP with this.) > > I should also mention that I am writing this code as part of some > tools to send myself and others reminder emails, the tools to be run > from a cron job. I am storing an actual email account password in > plaintext in my code. But, the account in question is one established > just for the purpose of the reminder project and similar projects; it > is not an account which houses my plans for world domination or the > like. That said, I have removed the account name and password string > below; it will thus require some adjustments to run for testing. > > And, as I side note, could anyone explain why changing a first world > of a body line 'From' to '>From' is the preferred standard? I > understand what the problem is that is being solved, but as most email > clients interpret a leading '>' as an indication of quoting, I would > have thought ' From' or something like '-From' would have been better. > If I have my own code deal with the problem in one of these ways, will > I be breaking anything? > > Anyway, thanks and best, > > Brian vdB > > import smtplib > > class SMTPSender(object): > def __init__(self, server, port, sender, password, messages): > self.server = server > self.port = port > self.sender = sender > self.password = password > self.messages = messages > > self._connect() > try: > self._send() > finally: > self._logout() > > def _connect(self): > self.session = smtplib.SMTP(server, port) > self.session.ehlo() > self.session.starttls() > self.session.ehlo > self.session.login(sender, password) > > def _send(self): > for message in self.messages: > to_addresses = message["To"].split(",") > self.session.sendmail(sender, to_addresses, > message.as_string()) > > def _logout(self): > self.session.quit() > > if __name__ == "__main__": > server = "smtp.gmail.com" > port = 587 > sender = "myfunnyhandle at gmail.com " > password = "mysecret" > > from email.mime.text import MIMEText > from email.mime.multipart import MIMEMultipart > > # Quick and dirty test message > msg = MIMEMultipart("alternative") > msg["Subject"] = "SMTP Test MIMEText plain" > msg["From"] = sender # Setting to anything but sender gets removed by > gmail. > msg["To"] = "someone at example.com , > someoneelse at example.com " > msg["Reply-to"] = "answerhere at example.com " > body = "\n\n".join(["Test msg MIME Text", > "From is a problem when occuring as the first word of a line."]) > msg.attach(MIMEText(body, "plain")) > > sender = SMTPSender(server, port, sender, password, [msg,]) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Mon May 5 13:21:02 2014 From: wprins at gmail.com (Walter Prins) Date: Mon, 5 May 2014 12:21:02 +0100 Subject: [Tutor] Stephen Mik-Novice Python Programmer(Version 3.4.0)Can't get help using Dictionaries, Lists to 'Hangman Game Problem" In-Reply-To: <1399245477.43168.YahooMailNeo@web124701.mail.ne1.yahoo.com> References: <1399245477.43168.YahooMailNeo@web124701.mail.ne1.yahoo.com> Message-ID: Hi Stephen, Please see below: On 5 May 2014 00:17, Stephen Mik wrote: > Dear Python World: > I am almost brand new to Python 3.4.0 and am taking a beginning Python > Programming class at the nearby Community College. One major problem I have > is time management with beginning pseudo code and coding for my Class > Assignments. I'm not sure we can help you with your time management skills, except that I'd like to gently suggest the obvious: a) the sooner you get started the better and b) regularly doing small amounts of work is better than leaving things until the last minute. As for getting started with pseudo code, this can be done right now. Pseudo code is simply starting to think about and write down the steps and procedures in your own words of what you'd like to happen to solve whatever problem you're working on. > The instructor prefers Office Hour help,and does not respond to > email. I'm not surprised. I imagine if she allowed students to contact her out of office hours and over email she'd do little else but deal with student queries 24 hours a day. ;) Are you allowed to collaborate with other students? **If** (and only if) you're allowed to do so, I'd suggest you could consider doing this. It's often beneficial to talk to your fellow students about the programming problems to gain a better understanding and some inspiration, and it also practices your programming communication skills. (It is often the case in programming courses that you're allowed (encouraged even) to help each other at some, usually conceptual level, provided that you still produce and submit your *own* solutions to the problem(s), obviously you are NOT allowed to directly share or copy each others solutions. But please clarify what is allowed before you do anything. I don't want to get you into trouble for illegal collaboration.) > One of my Class Assignments due on May 9,2014 deals with making a > version of the "Hangman Game" which I'm sure somebody out there is familiar > with. The program problem says to use 1.Dictionaries 2. Lists 3.Embedded > While lists 4.for loops and more. Can you post the exact problem statement please? > I can refer to the Textbook for the class > to get a basic idea of what some of the code will look like,but I cannot > fathom how to use a Dictionary or List in order to enhance the Basic > Hangman Game Problem in the text (written by Michael Dawson). OK. I don't have this book but managed to track down excerpts from it on Google books: http://books.google.co.uk/books?id=-WULAAAAQBAJ&printsec=frontcover#v=onepage&q&f=false I also managed to track down the web downloadable content here: http://www.programgames.com/page4.html I see there's 3 editions of this book. Which version are you using? > The Text does > cover "Dictionaries","Lists","While Loops","For Loops" but doesn't "amplify" > and show with examples how some of these Data Structures can be used. Right, I've downloaded the 3rd edition source code for the book and under the "chapter 5" folder, alongside the "hangman.py" program you're apparently expected to enhance, you're also actually given the other example source code from this chapter, e.g. "geek_translator.py", "hero's_inventory3.py", "high_scores.py", "high_scores2.py". These programs contain examples of the use of dictionaries, lists, while loops and for loops. So I suggest your book does in fact show examples of how these structures can be used? Have you looked at these programs at all and do you follow what they do? > If I can't get some ideas and hints from the Python Community by > Monday,May 5,2014, I will have to hold off this difficult programming > assignment until Tuesday,May 6,2014 when the instructor will be available to > help. You only posted this request for help *on* the 5th, this leaves yourself and us (a volunteer group not paid to do support) less than 24h to help you, going by your own timeline above. It would be better time management next time, if you get started on your assignments the day they're released, or shortly thereafter, especially if you're struggling and especially if you intend to to ask for help from third parties who might take an unpredictable amount of time to get back to you. In software it's always a good idea to leave yourself some "contingency" time to deal with the unexpected and with difficult problems. > That leaves me just 3 days to pseudocode,code in Python,fix syntax and > run-time and Logical errors before I can turn it in by Friday,May 16,2014. Good luck! > The instructor will only probably give me a suggestion and not some tangible > ideas.Any thoughts out there about how to implement the Loops and Data > Structures into a "Hangman Game" programming problem? I'd like to see the actual assignment question/requirement in order to better understand what exactly is required, given that the existing hangman program in fact already uses: a) a list b) a while loop c) a for loop (nested in an if statement) What exactly are you meant to produce and hand in? How is the mark allocation done? Hope that helps, Walter From illusiontechniques at gmail.com Mon May 5 19:07:07 2014 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 5 May 2014 13:07:07 -0400 Subject: [Tutor] Stephen Mik-Novice Python Programmer(Version 3.4.0)Can't get help using Dictionaries, Lists to 'Hangman Game Problem" In-Reply-To: <1399306791.91520.YahooMailNeo@web124702.mail.ne1.yahoo.com> References: <1399245477.43168.YahooMailNeo@web124701.mail.ne1.yahoo.com> <1399306791.91520.YahooMailNeo@web124702.mail.ne1.yahoo.com> Message-ID: Stephen, respond to the list so someone with more experience can help you. I am still learning a lot about Python, but when over-generalized questions are asked, I can tell that more experienced tutor-members will not be able to help you much unless you can learn to frame the questions correctly. You will notice this trend on many forums or mailing lists not even related to Python. Sites like stackoverflow.com (another great resource) will follow similar trends. One thing you could do is post the code from hangman.py. Some people will be averse to downloading even a .py file for various reasons, so you are more likely to get a response by posting the entire code in PLAIN TEXT. Right now your text is not plain text and will make it hard to copy/paste or indent code properly. If the instructor said you can use the code, use it. Changing variable names should take about 5 seconds, as long as you have at least feigned interest in the class. If changing variable names sounds daunting, don't feel hopeless, you have just not been exposed to it before. Adding a dictionary does not have to change the program dramatically. You could even just add a minor functionality like keeping track of score for players and not change the major parts of the program. Earlier you said: 'embedded while lists'. Did you mean 'embedded while loop'? If you can have a flash drive for the final, could you load it up with Python reference material? If you can, make sure it is in a format that you are comfortable searching and understanding. If you try to cheat in programming, it will be easy to tell if the instructor decides to ask you about your code. On Mon, May 5, 2014 at 12:19 PM, Stephen Mik wrote: > Stephen Mik-"hung-up" on Hangman Python code, pseudo code,worries about > using Dictionaries,Lists.embedded while lists,for loops: > Thank you,. C. Smith for responding to my help plea on Python-Tutor.org. One > version of the "Hang Man" problem is listed in the textbook,but it doesn't > use Dictionaries,Lists,embedded while lists;so I am fairly flummoxed as to > what is needed in the pseudo code,or Python code. If I just copy the code > from the Textbook,I am afraid that I will not learn very much about the > Algorithm used to guess the scrambled word in Hang Man.The program > assignment does not require a GUI to the screen,just printing words and some > primitive ASCII Graphics of a man being hung if the word is not guessed in > time. The instructor says we can use the code for the Hangman.py at the back > of the chapter,but must of course change the variable names and add > Dictionaries,Lists,etc. Another program is due by May 13,2014(A Tic-Tac-Toe > program,which has MANY functions. And to top it off,the Final Exam is > scheduled for Thursday,May 15,2014. In the Final we are NOT allowed textbook > or class notes,just a Flash Drive and a Python interpreter. Anything else > ,including looking up help on the Internet,is strictly forbidden.You talk > about pressure,I sure feel it now! SWM > On Sunday, May 4, 2014 4:45 PM, C Smith > wrote: > Hey, you will want to include some code to show your progress so far. > Can you write a basic program and then work the requirements into it? > Do you have some idea of where to start? Are you supposed to modify a > completed version of "hangman" that is in your text, or come up with > an original 'hangman' program? Does the hangman game need a graphical > interface, or is just printing words to the screen fine? > > On Sun, May 4, 2014 at 7:17 PM, Stephen Mik > wrote: >> Dear Python World: >> I am almost brand new to Python 3.4.0 and am taking a beginning Python >> Programming class at the nearby Community College. One major problem I >> have >> is time management with beginning pseudo code and coding for my Class >> Assignments. The instructor prefers Office Hour help,and does not respond >> to >> email. >> One of my Class Assignments due on May 9,2014 deals with making a >> version of the "Hangman Game" which I'm sure somebody out there is >> familiar >> with. The program problem says to use 1.Dictionaries 2. Lists 3.Embedded >> While lists 4.for loops and more. I can refer to the Textbook for the >> class >> to get a basic idea of what some of the code will look like,but I cannot >> fathom how to use a Dictionary or List in order to enhance the Basic >> Hangman Game Problem in the text (written by Michael Dawson). The Text >> does >> cover "Dictionaries","Lists","While Loops","For Loops" but doesn't >> "amplify" >> and show with examples how some of these Data Structures can be used. And >> my >> instructor's Assignment directions are very terse and "you do it by >> yourself". >> If I can't get some ideas and hints from the Python Community by >> Monday,May 5,2014, I will have to hold off this difficult programming >> assignment until Tuesday,May 6,2014 when the instructor will be available >> to >> help. That leaves me just 3 days to pseudocode,code in Python,fix syntax >> and >> run-time and Logical errors before I can turn it in by Friday,May 16,2014. >> The instructor will only probably give me a suggestion and not some >> tangible >> ideas.Any thoughts out there about how to implement the Loops and Data >> Structures into a "Hangman Game" programming problem? >> SINCERELY,Stephen W. Mik >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > From steve at pearwood.info Mon May 5 19:41:35 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 6 May 2014 03:41:35 +1000 Subject: [Tutor] Stephen Mik-Novice Python Programmer(Version 3.4.0)Can't get help using Dictionaries, Lists to 'Hangman Game Problem" In-Reply-To: <1399245477.43168.YahooMailNeo@web124701.mail.ne1.yahoo.com> References: <1399245477.43168.YahooMailNeo@web124701.mail.ne1.yahoo.com> Message-ID: <20140505174135.GT4273@ando> On Sun, May 04, 2014 at 04:17:57PM -0700, Stephen Mik wrote: > Any thoughts out there about > how to implement the Loops and Data Structures into a "Hangman Game" > programming problem? Yes. Consider the basic structure of a single game: Guess a letter. Is it correct? Do something. Otherwise it is wrong, do another thing. And repeat until done. So there's your basic loop structure. You would write it something like this: while True: # Loop forever. We'll break out of the loop when done. guess a letter if letter in secret_word: # handle a correct guess # if all the letters are shown, break out of the loop else: # handle a wrong guess # if the guy has been hanged, break out of the loop if won: print("Yay, you won!") else: print("You got hanged!") Use the "break" command to escape the infinite loop, e.g. something like this: while True: # more code goes here... if won: break That should give you somewhere to start. -- Steven From steve at pearwood.info Mon May 5 19:53:25 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 6 May 2014 03:53:25 +1000 Subject: [Tutor] sending email via SMTP: code review requested In-Reply-To: References: Message-ID: <20140505175325.GU4273@ando> On Sun, May 04, 2014 at 07:00:24PM -0400, Brian van den Broek wrote: > Hi all, > > I am playing with the smtp and email modules from the standard library > of Python 2.7.3 (I also want it to run on 2.6.6). I've not found the > going easy; the SMTP and RFC 2822 standards are not ones I have worked > with before. Neither have I :-( > I have something that works, but I am not confident I am > doing the right thing. For that matter, I am not very confident that I > am not doing the wrong thing. The code seems nicely written, it's understandable and easy to read. I haven't tried running it yet, but nothing stands out as obviously wrong. > I would very much appreciate some more experienced eyes on the code below. > In addition to any outright errors concerning interaction with an SMTP > server and constructing a MIME message, I would of course also welcome > style comments. (Preemptively, I will note it isn't obvious I ought to > have gone OOP with this.) Having the SMTPSender object send a message automatically on instantiation strikes me as a bit wiffy. I'm not sure if it's a good design or not. But for a simple cron job, it may be fine. [...] > And, as I side note, could anyone explain why changing a first world > of a body line 'From' to '>From' is the preferred standard? Because it's a dirty, nasty hack invented by somebody who wasn't thinking very carefully at the time, and now everybody does it. Bleh. > I > understand what the problem is that is being solved, but as most email > clients interpret a leading '>' as an indication of quoting, I would > have thought ' From' or something like '-From' would have been better. > If I have my own code deal with the problem in one of these ways, will > I be breaking anything? Yes. The idea is that your email client should recognise the hack when it sees a line ">From ..." and hide the leading ">". So if you use some other character, say, "!From ...", other people's mail clients won't know to hide the "!". -- Steven From wprins at gmail.com Mon May 5 20:33:00 2014 From: wprins at gmail.com (Walter Prins) Date: Mon, 5 May 2014 19:33:00 +0100 Subject: [Tutor] Stephen Mik-Novice Python Programmer(Version 3.4.0)Can't get help using Dictionaries, Lists to 'Hangman Game Problem" In-Reply-To: <1399309113.72623.YahooMailNeo@web124705.mail.ne1.yahoo.com> References: <1399245477.43168.YahooMailNeo@web124701.mail.ne1.yahoo.com> <1399309113.72623.YahooMailNeo@web124705.mail.ne1.yahoo.com> Message-ID: Hi Stephen, Firstly 2 requests: 1) Please do not respond to me personally; instead when interacting with a mailing list please use "reply-all", removing me (or other individuals) from the recipient list. You can also use "reply-list" if your mail program has that option. There are several reasons for requesting that you keep the communication on the list, one of which is that the exchanges between tutors and students are valuable to to other students, and taking them offline by restricting responses to one-on-one communication means other can't benefit from seeing these exchanges. Another reason is that if you have a one-on-one conversation with several people, then you end up possibly wasting several people's time by having multiple individuals potentially doing the same work by explaining the same things multiple times, not least due to them not being aware of each other. This can annoy and waste people's valuable time and may also deprive other students from having their questions answered, which is why you should always keep communications going back to the list whenever possible. (Imagine you were trying to help someone with some problem, and you generously spend an hour crafting a reply, only to find out after the fact that 2 other people have already done the same thing! You've just effectively wasted an hour that could've been spent helping someone else or with your family etc. due to someone not being considerate by keeping their communications on the mailing list.) 2) Please set your email client to use plain text, and ensure you properly format your emails with copious use of paragraphs. Your emails are coming through on my mail client as essentially one big paragraph. This makes it harder than it needs to be to read and respond to. In response to your email, see below: On 5 May 2014 17:58, Stephen Mik wrote: > However,time wise I am "up against it". The Hangman program is due by > Friday,May 9,2014. The next assignment;assignment 7,a Tic-Tac-Toe game with > a LOT of functions is due sometime in the week of May11-May 15,2014. There's a fully working Tic-Tac-Toe game presented in the textbook. What are you supposed to do for the next assignment? Get started on it now and it may well be finished well before this deadline. :) As with my previous email: PLEASE POST THE EXACT ASSIGNMENT TEXT. Do not paraphrase, do not summarize. Give as much information as possible about what is expected of you. > And the > Final Exam,which we are NOT to use the Textbook or written notes,or the > Internet, is scheduled for Friday morning,May 15,2014. It is comprehensive > and covers the first 6 chapters of the Textbook.We are only allowed a Flash > Drive,a "toolkit" of programs from the Textbook,and a computer in which > Python 3.4.0 is to be run. As you can see that I am under a LOT of PRESSURE > in the next 11 days! Then you need to practice as much as you can in the next 11 days. Write your own programs using all the constructs you've learned so far until you're totally comfortable with all the material. Play with the Python interpreter. Be totally comfortable with how it works etc. If you do that, you should have no problem with the exam, I would've thought. Also, start creating your toolkit *now*. Put Python on there (if you're expected and/or allowed to), and start working with your toolkit now. Use it as much as you can until it becomes a familiar and relatively comfortable place to be. Put snippets of code from the book on there , possibly put the entire source library from the book (which I posted earlier) on there, if you're allowed to. (Be sure to ask exactly what is and isn't allowed on your flash drive when you see the instructor.) > Personally,I don't think the Final Exam (25%of the class > grade) is fair,but the instructor is strict and I dare not question her > judgement or teaching pedagogy. Since I attend a Community College where > there is little interaction from the students,I have to "go it alone" much > of the time. There truly is little interaction between students,I couldn't > get a Tutor,about 90% of the Community College Lab staff doesn't know > Python,the list goes on. I think that I will consult with the instructor on > Tuesday,May 6,2014 in her crowded office hour for suggestions on how to > proceed. I still don't understand how a "Dictionary" could be incorporated > into an existing "hangman" program which doesn't apparently need it to > operate. One way would be to invent a new feature for the game that would require it. For example you could enhance the game to a) support multiple players and b) store some statistics (or maybe the game outcomes themselves) by user. One could use a dictionary to keep track of each players data for example. But as I requested before: Can you please post the actual homework/assignment instructions please? > Again,thanks for your help,concern and programming experience and > all the time you have volunteered to looking up the text on the Web. We > Python people need to stick together and form a" community". SWM You're welcome. Walter From brian.van.den.broek at gmail.com Mon May 5 23:26:44 2014 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Mon, 5 May 2014 17:26:44 -0400 Subject: [Tutor] sending email via SMTP: code review requested In-Reply-To: <20140505175325.GU4273@ando> References: <20140505175325.GU4273@ando> Message-ID: On 5 May 2014 13:53, Steven D'Aprano wrote: > On Sun, May 04, 2014 at 07:00:24PM -0400, Brian van den Broek wrote: >> Hi all, >> >> I am playing with the smtp and email modules from the standard library >> of Python 2.7.3 (I also want it to run on 2.6.6). I've not found the >> going easy; the SMTP and RFC 2822 standards are not ones I have worked >> with before. > > Neither have I :-( Hi All, Thanks for the feedback, Steven. (And to Japhy for the earlier reply). >> I have something that works, but I am not confident I am >> doing the right thing. For that matter, I am not very confident that I >> am not doing the wrong thing. > > The code seems nicely written, it's understandable and easy to read. I > haven't tried running it yet, but nothing stands out as obviously wrong. Well, gosh'n'golly-gee. >> I would very much appreciate some more experienced eyes on the code below. >> In addition to any outright errors concerning interaction with an SMTP >> server and constructing a MIME message, I would of course also welcome >> style comments. (Preemptively, I will note it isn't obvious I ought to >> have gone OOP with this.) > > Having the SMTPSender object send a message automatically on > instantiation strikes me as a bit wiffy. I'm not sure if it's a good > design or not. But for a simple cron job, it may be fine. That is a pattern I often have and for which I often have an associated spidey-tingle. If I have a class that exists to handle some processing and then be heard from no more, it always seems a bit funny to do: my_thing = MyOneTimeTaskClass(vars) my_thing.do_it() as every time I instantiate MyOneTimeTaskClass I am going to immediately ask the instance to do the things it does. Either way feels, as you say `wiffy.' (I think I shall steal that.) > [...] >> And, as I side note, could anyone explain why changing a first world >> of a body line 'From' to '>From' is the preferred standard? > > Because it's a dirty, nasty hack invented by somebody who wasn't > thinking very carefully at the time, and now everybody does it. Bleh. > >> I >> understand what the problem is that is being solved, but as most email >> clients interpret a leading '>' as an indication of quoting, I would >> have thought ' From' or something like '-From' would have been better. >> If I have my own code deal with the problem in one of these ways, will >> I be breaking anything? > > Yes. The idea is that your email client should recognise the hack when > it sees a line ">From ..." and hide the leading ">". So if you use some > other character, say, "!From ...", other people's mail clients won't > know to hide the "!". Oh dear. I developed my code mostly checking the resulting email with the gmail app on Android. It doesn't handle things this way; instead, some (variable! No, really) portion of the message body gets displayed as a quote. I would declare my surprise, but little about the act of malice that is the gmail Android app could surprise me now. (Unlike the web interface, there is quite literally no way to send plain text from the app.) Anyway, thanks again, Brian vdB From japhy at pearachute.com Tue May 6 00:43:00 2014 From: japhy at pearachute.com (Japhy Bartlett) Date: Mon, 5 May 2014 17:43:00 -0500 Subject: [Tutor] sending email via SMTP: code review requested In-Reply-To: References: <20140505175325.GU4273@ando> Message-ID: If you get deeper into processing emails, you might check out http://lamsonproject.org/ . I wasn't fond of the whole thing, but if you dig into the src there is some pretty good code for handling malformed MIME structures and unicode issues in a sane way. On Mon, May 5, 2014 at 4:26 PM, Brian van den Broek < brian.van.den.broek at gmail.com> wrote: > On 5 May 2014 13:53, Steven D'Aprano wrote: > > On Sun, May 04, 2014 at 07:00:24PM -0400, Brian van den Broek wrote: > >> Hi all, > >> > >> I am playing with the smtp and email modules from the standard library > >> of Python 2.7.3 (I also want it to run on 2.6.6). I've not found the > >> going easy; the SMTP and RFC 2822 standards are not ones I have worked > >> with before. > > > > Neither have I :-( > > > Hi All, > > Thanks for the feedback, Steven. (And to Japhy for the earlier reply). > > >> I have something that works, but I am not confident I am > >> doing the right thing. For that matter, I am not very confident that I > >> am not doing the wrong thing. > > > > The code seems nicely written, it's understandable and easy to read. I > > haven't tried running it yet, but nothing stands out as obviously wrong. > > Well, gosh'n'golly-gee. > > >> I would very much appreciate some more experienced eyes on the code > below. > >> In addition to any outright errors concerning interaction with an SMTP > >> server and constructing a MIME message, I would of course also welcome > >> style comments. (Preemptively, I will note it isn't obvious I ought to > >> have gone OOP with this.) > > > > Having the SMTPSender object send a message automatically on > > instantiation strikes me as a bit wiffy. I'm not sure if it's a good > > design or not. But for a simple cron job, it may be fine. > > That is a pattern I often have and for which I often have an > associated spidey-tingle. If I have a class that exists to handle some > processing and then be heard from no more, it always seems a bit funny > to do: > > my_thing = MyOneTimeTaskClass(vars) > my_thing.do_it() > > as every time I instantiate MyOneTimeTaskClass I am going to > immediately ask the instance to do the things it does. > > Either way feels, as you say `wiffy.' (I think I shall steal that.) > > > > [...] > >> And, as I side note, could anyone explain why changing a first world > >> of a body line 'From' to '>From' is the preferred standard? > > > > Because it's a dirty, nasty hack invented by somebody who wasn't > > thinking very carefully at the time, and now everybody does it. Bleh. > > > >> I > >> understand what the problem is that is being solved, but as most email > >> clients interpret a leading '>' as an indication of quoting, I would > >> have thought ' From' or something like '-From' would have been better. > >> If I have my own code deal with the problem in one of these ways, will > >> I be breaking anything? > > > > Yes. The idea is that your email client should recognise the hack when > > it sees a line ">From ..." and hide the leading ">". So if you use some > > other character, say, "!From ...", other people's mail clients won't > > know to hide the "!". > > > Oh dear. > > I developed my code mostly checking the resulting email with the gmail > app on Android. It doesn't handle things this way; instead, some > (variable! No, really) portion of the message body gets displayed as a > quote. I would declare my surprise, but little about the act of malice > that is the gmail Android app could surprise me now. (Unlike the web > interface, there is quite literally no way to send plain text from the > app.) > > Anyway, thanks again, > > Brian vdB > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From scott.w.d at cox.net Tue May 6 06:04:40 2014 From: scott.w.d at cox.net (Scott W Dunning) Date: Mon, 5 May 2014 21:04:40 -0700 Subject: [Tutor] Final review In-Reply-To: References: Message-ID: <7D3512F1-CC80-47D6-B9DD-C26837EFE3D3@cox.net> On May 1, 2014, at 5:30 AM, Steven D'Aprano wrote: Awesome, thanks everyone! I understand lists a lot better now. I have another question. I don?t understand why below would give an error? >>> greeting = 'Hello World? >>> greeting [len(greeting)] From meenuravi89 at gmail.com Tue May 6 07:13:26 2014 From: meenuravi89 at gmail.com (meenu ravi) Date: Tue, 6 May 2014 10:43:26 +0530 Subject: [Tutor] Final review In-Reply-To: <7D3512F1-CC80-47D6-B9DD-C26837EFE3D3@cox.net> References: <7D3512F1-CC80-47D6-B9DD-C26837EFE3D3@cox.net> Message-ID: Hi Scott, The variable greeting is of type "string". >>> greeting = "Hello world" >>> type(greeting) The len(string) will count each character in the value of variable "greeting" starting from '1'. H - 1 e - 2 l - 3 l - 4 0 - 5 space - 6(Space and special characters are also counted) w - 7 o - 8 r - 9 l - 10 d - 11 So the len(greeting) returns 11. >>> len(greeting) 11 But while accessing with the index, indexing starts with '0'. Like, >>> greeting.index('H') 0 >>> greeting.index('e') 1 >>> greeting.index('l') 2 Likewise, the index of d, which is the last word in the word "Hello world" is 10. So, the maximum index you can access in the word "Hello world" is 10. But when you try to give the command, >>> greeting [len(greeting)] It is trying to access the character at the position "11", where the string "Hello world" doesn't contain any value in the index "11" and the maximum index is 10. So it throws the following error. Traceback (most recent call last): File "", line 1, in IndexError: string index out of range So always the maximum index will be length - 1. So if you want to access the last character of the string "Hello world", give the command: >>> greeting[len(greeting)-1] 'd' Hope this helps Thanks On 6 May 2014 09:35, "Scott W Dunning" wrote: > > On May 1, 2014, at 5:30 AM, Steven D'Aprano wrote: > > Awesome, thanks everyone! I understand lists a lot better now. > > I have another question. I don?t understand why below would give an error? > > >>> greeting = 'Hello World? > >>> greeting [len(greeting)] > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zebra05 at gmail.com Tue May 6 09:15:33 2014 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Tue, 6 May 2014 09:15:33 +0200 Subject: [Tutor] PyCountry currency formatting woes In-Reply-To: References: Message-ID: Thanks for this response, this is exactly what I needed to know. On Mon, May 5, 2014 at 6:26 AM, Marc Tompkins wrote: > On Sun, May 4, 2014 at 1:55 PM, Sithembewena Lloyd Dube > wrote: > >> Thanks, i was actually getting the error information to update the post. >> Apoligies to waste your time posting here - I could not find an appropriate >> PyCountry discussion list and my next best bet seemed to be a Python users' >> list. >> >> > You also posted on StackOverflow; I just answered you there. In short: > the currency numeric is not guaranteed to be the same as the country > numeric (in the case of the Euro, how could it possibly be?) The numeric > for DE is 276; the numeric for the Euro is 978. Obviously they don't match. > > PyCountry is a wrapper around some tables provided by Debian; those tables > don't include a country/currency mapping. You can find those mapping > tables at > http://www.currency-iso.org/en/home/tables/table-a1.html > and roll your own wrapper; I'm sure it's been done a thousand times > before, but I'm not aware of a Python package that does this. > -- Regards, Sithu Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: From swdunning at me.com Tue May 6 05:43:02 2014 From: swdunning at me.com (Scott Dunning) Date: Mon, 05 May 2014 20:43:02 -0700 Subject: [Tutor] Final review In-Reply-To: References: Message-ID: <62D1E3F3-5A00-42AC-9AFD-80DF1D7DC3F1@me.com> On May 1, 2014, at 5:30 AM, Steven D'Aprano wrote: > Awesome, thanks everyone! I understand lists a lot better now. I have another question. I don?t understand why below would give an error? >>> greeting = 'Hello World' >>> greeting [len(greeting)] From fomcl at yahoo.com Tue May 6 12:46:22 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 6 May 2014 03:46:22 -0700 (PDT) Subject: [Tutor] attributes vs globals (was Re: global list) In-Reply-To: References: <20140424005956.GE17388@ando> <1398327273.10772.YahooMailNeo@web163804.mail.gq1.yahoo.com> <1398371418.67018.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: <1399373182.87007.YahooMailNeo@web163802.mail.gq1.yahoo.com> ________________________________ > From: Alan Gauld >To: tutor at python.org >Sent: Friday, April 25, 2014 12:15 AM >Subject: Re: [Tutor] attributes vs globals (was Re: global list) > > >On 24/04/14 21:48, Mark Lawrence wrote: >> On 24/04/2014 21:30, Albert-Jan Roskam wrote: >>> >>> As a side not, I find that variables (attributes) defined in __init__ >>> are also much like globals, but that's probably a different discussion. >> >> Would you please be kind enough to explain your logic. > >I can't speak for Albert but I do tend to agree that class >(or instance) variables are a lot like globals. They are >slightly better controlled because they are unique to an >instance but they do result in code(methods) that are >dependant on side effects. You are changing data that >is neither passed into the method or returned by it. >So its not obvious which instance attributes are being >modified by which methods. > >Now OOP theory says that with information hiding you >shouldn't care, so long as the external behaviour is >the correct, but in Python its? common to directly >access attributes. And if you inherit a class you >often do care about its internal state. > >So instance variables carry many of the same negative >characteristics that globals do, albeit confined to a >single entity and a single, constrained, set of functions.. Hi all, Sorry for the delayed reply, I was on holiday. Yes, Alan said what I meant with my original (somewhat vague) description. The snippet below further illustrates this (orignally I had only pseudo_global2 in mind, but the pseudo_global1 is another option). self.pseudo_global*'s value can be accessed everywhere, even though it is not an argument of the function or method. class Foo(object): ??? pseudo_global1 =? "I am all over the place, if I want to" ??? def __init__(self): ??????? self.pseudo_global2 = "I am also all over the place, if I want to" ??????? print self.pseudo_global2 ??? def some_method(self): ??????? print self.pseudo_global2 ??????? def some_function(): ??????????? print self.pseudo_global2 ??????? some_function() ??????? print Foo.pseudo_global11 if __name__ == "__main__": ??? Foo().some_method() regards, Albert-Jan From dyoo at hashcollision.org Tue May 6 19:21:33 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 6 May 2014 10:21:33 -0700 Subject: [Tutor] Final review In-Reply-To: <7D3512F1-CC80-47D6-B9DD-C26837EFE3D3@cox.net> References: <7D3512F1-CC80-47D6-B9DD-C26837EFE3D3@cox.net> Message-ID: On Mon, May 5, 2014 at 9:04 PM, Scott W Dunning wrote: > > On May 1, 2014, at 5:30 AM, Steven D'Aprano wrote: > > Awesome, thanks everyone! I understand lists a lot better now. > > I have another question. I don?t understand why below would give an error? > >>>> greeting = 'Hello World? >>>> greeting [len(greeting)] Hi Scott, Question: what do you expect to see? Also, you may want to try a smaller example that still demonstrates the problem. For example, consider the staccato greeting "hi", and work through the scenario. From davea at davea.name Mon May 5 03:44:03 2014 From: davea at davea.name (Dave Angel) Date: Sun, 4 May 2014 21:44:03 -0400 (EDT) Subject: [Tutor] append vs list addition References: Message-ID: C Smith Wrote in message: > Sorry. > > I meant for example: > list1 = [1,2,3] > list2 = [3,4,5] > > newList = list1 + list2 > > versus > > for x in list2: > list1.append(x) > > Which is the preferred way to add elements from one list to another? Thank you for switching to text mail. These examples still aren't equivalent. But in any similar example, if list2 is type list, then avoid the list. Use either extend, or the equivalent += . And if you aren't permitted to change list1, you should use += . The time when append is preferred over + is when you HAVE to loop over the items, like when they each need computing or when they're output from a generator. -- DaveA From alan.gauld at btinternet.com Mon May 5 02:16:17 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 05 May 2014 01:16:17 +0100 Subject: [Tutor] PyCountry currency formatting woes In-Reply-To: References: Message-ID: On 04/05/14 21:25, Sithembewena Lloyd Dube wrote: > currency = pycountry.currencies.get(numeric=country.numeric) > Have you tried using locales? import locale as loc loc.setlocale(loc.LC_ALL,'') # selects default - do this at start ... print(loc.currency(myNum_here)) I don't know the module you reference but locale is the standard library option... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From denis.heidtmann at gmail.com Tue May 6 20:54:57 2014 From: denis.heidtmann at gmail.com (Denis Heidtmann) Date: Tue, 6 May 2014 11:54:57 -0700 Subject: [Tutor] append vs list addition In-Reply-To: References: Message-ID: On Sun, May 4, 2014 at 6:44 PM, Dave Angel wrote: > C Smith Wrote in message: >> Sorry. >> >> I meant for example: >> list1 = [1,2,3] >> list2 = [3,4,5] >> >> newList = list1 + list2 >> >> versus >> >> for x in list2: >> list1.append(x) >> >> Which is the preferred way to add elements from one list to another? > > Thank you for switching to text mail. > > These examples still aren't equivalent. But in any similar > example, if list2 is type list, then avoid the list. Use either > extend, or the equivalent += . And if you aren't permitted to > change list1, you should use += .... > DaveA > Lurking here. I am confused by "avoid the list". What does that mean? Also, you say extend and += are equivalent, yet say " if you aren't permitted to change list1, you should use +=" If they are equivalent, why choose one over the other? Doesn't += always change the left-hand side? Not being critical--just confused. Thanks, -Denis From alan.gauld at btinternet.com Tue May 6 19:36:21 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 06 May 2014 18:36:21 +0100 Subject: [Tutor] Final review In-Reply-To: <62D1E3F3-5A00-42AC-9AFD-80DF1D7DC3F1@me.com> References: <62D1E3F3-5A00-42AC-9AFD-80DF1D7DC3F1@me.com> Message-ID: On 06/05/14 04:43, Scott Dunning wrote: > I have another question. I don?t understand why below would give an error? > >>>> greeting = 'Hello World' >>>> greeting [len(greeting)] Because list indexing starts at zero but len() returns the actual length. So the last element of a list is mylist[len(mylist)-1] or, more simply: mylist[-1] Aside: len() does work with range() and slicing so you can write myList[:len(mylist)] to get a copy of your list... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Mon May 5 21:04:56 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 05 May 2014 20:04:56 +0100 Subject: [Tutor] Keeping change-in-place vs. copy methods straight In-Reply-To: References: <20140504103132.GM4273@ando> Message-ID: On 04/05/14 13:54, Dave Angel wrote: > Alan Gauld Wrote in message: >> I assumed (never assume!) that it returned a reference to the original. >> I really, really, hate the way Python handles this :-( > > It's not clear to me what you would change. Would you only provide > methods (like sort) that mangle their object? No, I'd not provide methods that work on the object and return None. I much prefer the Smalltalk model where if all else fails you return self. Since Python has objects everywhere they could easily have adopted that model, it is so much more consistent. And it also allows method chaining... But just the consistency of always getting a useful object (I don't mean None!) back makes a big difference to programming. It does mean you need to make copy semantics obvious in the naming but that's not too onerous. So sorted() would become sortedCopy() or some such. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue May 6 19:44:58 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 06 May 2014 18:44:58 +0100 Subject: [Tutor] Alice_in_wonderland Question In-Reply-To: References: Message-ID: On 05/05/14 04:13, Jake Blank wrote: > I did have one more question though. > > import os > from wordtools import extract_words > > source_filepath=input("Enter the path to the source file:") > dest_filepath =input("Enter the path to the destination file:") > > I'm wondering how I can make it so the program can tell if the > source/dest_filepath the user entered is actually a program on the computer. Do you really mean is it a program? - ie an executable file? Or do you just want to know if the file (any kind of file) exists? There are ways to do what you want but you need to be much more specific. > Also i have to ask the user if they would like to "process another > file(Y/N)?" and I'm not sure where to put that. You need a loop. It will look something like(pseudo code) while True: you existing copy code here ask the user if they want to do more if they don't: break # exit the loop HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Mon May 5 10:10:28 2014 From: __peter__ at web.de (Peter Otten) Date: Mon, 05 May 2014 10:10:28 +0200 Subject: [Tutor] PyCountry currency formatting woes References: Message-ID: Sithembewena Lloyd Dube wrote: > Thanks, i was actually getting the error information to update the post. > Apoligies to waste your time posting here - I could not find an > appropriate PyCountry discussion list and my next best bet seemed to be a > Python users' list. > > For those who care to look, the error is as follows (a concise example > from an interactive shell: > > import pycountry > country = pycountry.countries.get(alpha2='DE') > currency = pycountry.currencies.get(numeric=country.numeric) > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get > return self.indices[field][value] > KeyError: '276' > > The obvious issue here is that the pycountry.countries collection does not > contain a currency with a numeric of 276 (Germany's numeric) - yet it does > contain the Euro. Any ideas as to what the way around this may be? It looks like the development version of babel http://babel.pocoo.org/docs/api/numbers/#babel.numbers.get_territory_currencies can do what you want: $ LANG=en_US.UTF-8 python Python 2.7.5+ (default, Feb 27 2014, 19:37:08) [GCC 4.8.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import babel.numbers as bn >>> bn.get_territory_currencies("DE") ['EUR'] >>> print bn.format_currency(1.234, "EUR") ?1.23 >>> print bn.format_currency(1.234, "EUR", locale="DE") 1,23 ? >>> import babel >>> babel.__version__ '2.0-dev' From __peter__ at web.de Tue May 6 18:51:29 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 06 May 2014 18:51:29 +0200 Subject: [Tutor] Alice_in_wonderland Question References: Message-ID: Jake Blank wrote: > I finally got it. > This was the code: > for k in sorted(word_count, key=lambda x:word_count[x], reverse=True): > print (k, word_count[k]) > > The only question i have now is how to limit the amount of returns the > program runs to the first 15 results. Hint: you can make a list of the first N items of a list with some_list = some_list[:N] The list in question is sorted(word_count, key=lambda x:word_count[x], reverse=True) You might also try the heapq.nlargest() function: >>> word_count {'pewter': 12, 'rawboned': 2, 'accolade': 2, 'Messiah': 15, 'summoning': 17, 'sleeking': 6, 'parse': 14, 'Freya': 8, 'chroniclers': 13, 'faith': 1} >>> import heapq >>> for word in heapq.nlargest(5, word_count, key=lambda w: word_count[w]): ... print(word, word_count[word]) ... summoning 17 Messiah 15 parse 14 chroniclers 13 pewter 12 See also From alan.gauld at btinternet.com Tue May 6 19:55:28 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 06 May 2014 18:55:28 +0100 Subject: [Tutor] sending email via SMTP: code review requested In-Reply-To: <20140505175325.GU4273@ando> References: <20140505175325.GU4273@ando> Message-ID: On 05/05/14 18:53, Steven D'Aprano wrote: >> And, as I side note, could anyone explain why changing a first world >> of a body line 'From' to '>From' is the preferred standard? > > Because it's a dirty, nasty hack invented by somebody who wasn't > thinking very carefully at the time, and now everybody does it. Bleh. Just to add to that. You need to remember with email (and a few other old favourites like ftp) that these things were invented at the very dawn of networking. They predate TCP/IP and what we know as the internet by quite a long ways. So when they came to "standardize" things, it was a case of trying to make the crud that already existed look as if it was a "modern" standard. Also when there were multiple, slightly incompatible, mail tools/formats out there they tended to take a vote by committee - so everybody's favourite hack got thrown into the pot. The older the technology the more likely it is to have horrible hacks embedded in it from the dawn of (computing) time. Its one reason that new email/messaging standards keep coming out. Somebody decides they need to get rid of the crud and do it right. But trying to get a zillion email users to adopt a new standard is not an easy task. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From davea at davea.name Tue May 6 22:21:24 2014 From: davea at davea.name (Dave Angel) Date: Tue, 6 May 2014 16:21:24 -0400 (EDT) Subject: [Tutor] append vs list addition References: Message-ID: Denis Heidtmann Wrote in message: > On Sun, May 4, 2014 at 6:44 PM, Dave Angel wrote: >> C Smith Wrote in message: >>> Sorry. >>> >>> I meant for example: >>> list1 = [1,2,3] >>> list2 = [3,4,5] >>> >>> newList = list1 + list2 >>> >>> versus >>> >>> for x in list2: >>> list1.append(x) >>> >>> Which is the preferred way to add elements from one list to another? >> >> Thank you for switching to text mail. >> >> These examples still aren't equivalent. But in any similar >> example, if list2 is type list, then avoid the list. Use either >> extend, or the equivalent += . And if you aren't permitted to >> change list1, you should use += > .... >> DaveA >> > > Lurking here. I am confused by "avoid the list". What does that mean? > Also, you say extend and += are equivalent, yet say " if you aren't > permitted to change list1, you should use +=" If they are equivalent, > why choose one over the other? Doesn't += always change the left-hand > side? > > Not being critical--just confused. You're right to be confused; my fingers were confused typing my last sentence. It should have ended: ... you should use + . Likewise the previous thought should have said: But in any similar example, if list2 is type list, then avoid the loop. I've been trying to use a tablet to run a newsreader, and between bugs in the reader and an over enthusiastic spell correction, I'm ready to give up. -- DaveA From denis.heidtmann at gmail.com Wed May 7 02:42:45 2014 From: denis.heidtmann at gmail.com (Denis Heidtmann) Date: Tue, 6 May 2014 17:42:45 -0700 Subject: [Tutor] append vs list addition In-Reply-To: References: Message-ID: On Tue, May 6, 2014 at 1:21 PM, Dave Angel wrote: >>>> > You're right to be confused; my fingers were confused typing my > last sentence. It should have ended: > ... you should use + . > > Likewise the previous thought should have said: > > But in any similar > example, if list2 is type list, then > avoid the loop. ... > DaveA Thanks. That makes me feel better. -Denis From wprins at gmail.com Wed May 7 12:50:44 2014 From: wprins at gmail.com (Walter Prins) Date: Wed, 7 May 2014 11:50:44 +0100 Subject: [Tutor] Useful blog post: How to debug small programs Message-ID: Hi all, I stumbled across this post today and thought it was worth sharing with the Python tutor list. It provides good advice to students about debugging your programs and how to ask for help on forums, in particular Stack overflow, but in that way equally applies to the Python tutor list. Link: http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ Best, Walter From jitu.icfai at gmail.com Wed May 7 15:13:11 2014 From: jitu.icfai at gmail.com (jitendra gupta) Date: Wed, 7 May 2014 18:43:11 +0530 Subject: [Tutor] xml parsing from xml Message-ID: Hi I just want to create a new xm file from existing xml file. so basically i want to put contry details in countryName.xml from these file. I thought to do via read a line by line with normal file handling. but there a problem with that. So i want to handle python XML . Could you please suggest on this. Any Idea is welcome Thanks & regards Jitendra 2 2008 141100 5 2011 59900 69 2011 13600 -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Wed May 7 16:37:32 2014 From: neilc at norwich.edu (Neil D. Cerutti) Date: Wed, 07 May 2014 10:37:32 -0400 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: On 5/7/2014 9:13 AM, jitendra gupta wrote: > Hi > > I just want to create a new xm file from existing xml file. so basically > i want to put contry details in countryName.xml from these file. > > I thought to do via read a line by line with normal file handling. but > there a problem with that. So i want to handle python XML . Could you > please suggest on this. > > Any Idea is welcome > > Thanks & regards > Jitendra > > > > > 2 > 2008 > 141100 > > > > > 5 > 2011 > 59900 > > > > 69 > 2011 > 13600 > > > > We need more details to be able to help. What should be in the resulting XML file or files? Do you want to create one XML file for each country in your source file? I agree with you that trying to parse XML manually, line-by-line, is a bad idea. Python comes with xml.etree.ElementTree, a library useful for parsing and generating xml documents. Take a look at the documentation and examples in the Python docs to get started. -- Neil Cerutti From alan.gauld at btinternet.com Wed May 7 18:11:07 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 07 May 2014 17:11:07 +0100 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: On 07/05/14 14:13, jitendra gupta wrote: > I thought to do via read a line by line with normal file handling. but > there a problem with that. So i want to handle python XML . Could you > please suggest on this. Python comes with several XML parsers. The simplest to use are probably sax and ElementTree (aka etree). The documenation gives examples of both. sax is easiest and fastest for simple XML in big files while etree is probably better for more complex XML structures. Your XML looks fairly simple > > > > 2 > 2008 > 141100 > > > > Either parser should be fine, see which one suits your personal style best. For more specific help we need more specific information. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From stefan_ml at behnel.de Wed May 7 18:56:34 2014 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 07 May 2014 18:56:34 +0200 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: Alan Gauld, 07.05.2014 18:11: > Python comes with several XML parsers. The simplest to use are probably sax > and ElementTree (aka etree). The documenation gives examples of both. sax > is easiest and fastest for simple XML in big files while etree is probably > better for more complex XML structures. I wouldn't say that SAX qualifies as "easiest". Sure, if the task is something like "count number of abc tags" or "find tag xyz and get an attribute value from it", then SAX is relatively easy and also quite fast. However, anything larger than that (i.e. *any* real task) quickly gets so complex and complicated that it's usually faster to learn ElementTree's iterparse() from scratch and write a working solution with it, than to write even a half working implementation of a SAX handler for a given problem. And that's completely ignoring the amount of time that such an unwieldy SAX handler will cost you in terms of long term code maintenance. BTW, ElementTree also has a SAX-like parsing mode, but comes with a simpler interface and saner parser configuration defaults. That makes the xml.sax package even less recommendable. Stefan From malaclypse2 at gmail.com Wed May 7 19:09:47 2014 From: malaclypse2 at gmail.com (Jerry Hill) Date: Wed, 7 May 2014 13:09:47 -0400 Subject: [Tutor] Useful blog post: How to debug small programs In-Reply-To: References: Message-ID: On Wed, May 7, 2014 at 6:50 AM, Walter Prins wrote: > Hi all, > > I stumbled across this post today and thought it was worth sharing > with the Python tutor list. It provides good advice to students about > debugging your programs and how to ask for help on forums, in > particular Stack overflow, but in that way equally applies to the > Python tutor list. > > Link: http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ That's a pretty good primer on debugging, especially for school assignments which tend to be short and well defined. I like to point people towards this page too: http://www.sscce.org/ That's more geared to how to get your questions answered on a mailing list (I assume it works equally well for stack overflow). Basically, the advice boils down to taking the code you're having problems with and cutting it down to a Short, Self Contained, Compilable Example. The key is that the code should be short enough to read comfortably, self contained enough that I could copy and paste it into an editor, run the code, and see exactly what you're having problems with. The compilable part isn't so important in python, but the code ought to run, without throwing Syntax Errors, unless that syntax error is what you're asking about. As it happens, the process of cutting your code down to that example often points you to where your problem is without even having to post. When it doesn't, you're able to put a compelling question in front of volunteers on your mailing list or forum of choice. I know I am much, much more likely to comment on code that fits these guidelines. -- Jerry From alan.gauld at btinternet.com Wed May 7 19:39:25 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 07 May 2014 18:39:25 +0100 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: On 07/05/14 17:56, Stefan Behnel wrote: > Alan Gauld, 07.05.2014 18:11: >> and ElementTree (aka etree). The documenation gives examples of both. sax >> is easiest and fastest for simple XML in big files ... > > I wouldn't say that SAX qualifies as "easiest". Sure, if the task is > something like "count number of abc tags" or "find tag xyz and get an > attribute value from it", then SAX is relatively easy and also quite fast. That's pretty much what I said. simple task, big file. sax is easy. For anything else use etree. > BTW, ElementTree also has a SAX-like parsing mode, but comes with a simpler > interface and saner parser configuration defaults. My experience was different. Etree is powerful but for simple tasks I just found sax easier to grok. (And most of my XML parsing is limited to simple extraction of a field or two.) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From neilc at norwich.edu Wed May 7 20:04:23 2014 From: neilc at norwich.edu (Neil D. Cerutti) Date: Wed, 07 May 2014 14:04:23 -0400 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: On 5/7/2014 1:39 PM, Alan Gauld wrote: > On 07/05/14 17:56, Stefan Behnel wrote: >> Alan Gauld, 07.05.2014 18:11: > >>> and ElementTree (aka etree). The documenation gives examples of both. >>> sax >>> is easiest and fastest for simple XML in big files ... >> >> I wouldn't say that SAX qualifies as "easiest". Sure, if the task is >> something like "count number of abc tags" or "find tag xyz and get an >> attribute value from it", then SAX is relatively easy and also quite >> fast. > > That's pretty much what I said. simple task, big file. sax is easy. > > For anything else use etree. > >> BTW, ElementTree also has a SAX-like parsing mode, but comes with a >> simpler >> interface and saner parser configuration defaults. > > My experience was different. Etree is powerful but for simple > tasks I just found sax easier to grok. (And most of my XML parsing > is limited to simple extraction of a field or two.) If I understand this task correctly it seems like a good application for SAX. As a state machine it could have a mere two states, assuming we aren't troubled about the parent nodes of Country tags. Using callbacks is a trickier form of programming than ElementTree calls for, though. In my own personal case, I partly prefer xml.sax simply because it ignores namespaces, a nice benefit in my cases. I wish I could make ElementTree do that. -- Neil Cerutti From stefan_ml at behnel.de Wed May 7 21:49:46 2014 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 07 May 2014 21:49:46 +0200 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: Neil D. Cerutti, 07.05.2014 20:04: > On 5/7/2014 1:39 PM, Alan Gauld wrote: >> On 07/05/14 17:56, Stefan Behnel wrote: >>> Alan Gauld, 07.05.2014 18:11: >>>> and ElementTree (aka etree). The documenation gives examples of both. >>>> sax is easiest and fastest for simple XML in big files ... >>> >>> I wouldn't say that SAX qualifies as "easiest". Sure, if the task is >>> something like "count number of abc tags" or "find tag xyz and get an >>> attribute value from it", then SAX is relatively easy and also quite >>> fast. >> >> That's pretty much what I said. simple task, big file. sax is easy. >> >> For anything else use etree. >> >>> BTW, ElementTree also has a SAX-like parsing mode, but comes with a >>> simpler interface and saner parser configuration defaults. >> >> My experience was different. Etree is powerful but for simple >> tasks I just found sax easier to grok. (And most of my XML parsing >> is limited to simple extraction of a field or two.) > > If I understand this task correctly it seems like a good application for > SAX. As a state machine it could have a mere two states, assuming we aren't > troubled about the parent nodes of Country tags. Yep, that's the kind of thing I meant. You get started, just trying to get out one little field out of the file, then notice that you need another one, and eventually end up writing a page full of code where a couple of lines would have done the job. Even just safely and correctly getting the text content of an element is surprisingly non-trivial in SAX. It's still unclear what the OP wanted exactly, though. To me, it read more like the task was to copy some content over from one XML file to another, in which case doing it in ET is just trivial thanks to the tree API, but SAX requires you to reconstruct the XML brick by brick here. > In my own personal case, I partly prefer xml.sax simply because it ignores > namespaces, a nice benefit in my cases. I wish I could make ElementTree do > that. The downside of namespace unaware parsing is that you never know what you get. It works for some input, but it may also just fail arbitrarily, for equally valid input. One cool thing about ET is that it makes namespace aware processing easy by using fully qualified tag names (one string says it all). Most other XML tools (including SAX) require some annoying prefix mapping setup that you have to carry around in order to tell the processor that you are really talking about the thing that it's showing to you. Stefan From dyoo at hashcollision.org Wed May 7 22:21:12 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 7 May 2014 13:21:12 -0700 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: On Wed, May 7, 2014 at 6:13 AM, jitendra gupta wrote: > I just want to create a new xm file from existing xml file. so basically i > want to put contry details in countryName.xml from these file. Side question: does your input have to be XML, or can it be in a simpler format such as JSON? From jitu.icfai at gmail.com Wed May 7 22:26:40 2014 From: jitu.icfai at gmail.com (jitendra gupta) Date: Thu, 8 May 2014 01:56:40 +0530 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: @All thanks, I cant use etree/SAX because there we cant get complete line , of course we can get it by tag name but we are not sure about tag also. Only we know what ever child of we need to put in new file with country name. Note: File size is around 800MB, for other requirement(Like converting xml to csv) i used lxml/others. but in my current scenario i dont know what child tag will be there . ###### INPUT XML ####### 2 2008 141100 ....... ....... 69 2011 13600 ######## outputxml (Liechtenstein.xml) ###### 2 2008 141100 ....... ....... ##### ##### 69 2011 13600 On Thu, May 8, 2014 at 1:19 AM, Stefan Behnel wrote: > Neil D. Cerutti, 07.05.2014 20:04: > > On 5/7/2014 1:39 PM, Alan Gauld wrote: > >> On 07/05/14 17:56, Stefan Behnel wrote: > >>> Alan Gauld, 07.05.2014 18:11: > >>>> and ElementTree (aka etree). The documenation gives examples of both. > >>>> sax is easiest and fastest for simple XML in big files ... > >>> > >>> I wouldn't say that SAX qualifies as "easiest". Sure, if the task is > >>> something like "count number of abc tags" or "find tag xyz and get an > >>> attribute value from it", then SAX is relatively easy and also quite > >>> fast. > >> > >> That's pretty much what I said. simple task, big file. sax is easy. > >> > >> For anything else use etree. > >> > >>> BTW, ElementTree also has a SAX-like parsing mode, but comes with a > >>> simpler interface and saner parser configuration defaults. > >> > >> My experience was different. Etree is powerful but for simple > >> tasks I just found sax easier to grok. (And most of my XML parsing > >> is limited to simple extraction of a field or two.) > > > > If I understand this task correctly it seems like a good application for > > SAX. As a state machine it could have a mere two states, assuming we > aren't > > troubled about the parent nodes of Country tags. > > Yep, that's the kind of thing I meant. You get started, just trying to get > out one little field out of the file, then notice that you need another > one, and eventually end up writing a page full of code where a couple of > lines would have done the job. Even just safely and correctly getting the > text content of an element is surprisingly non-trivial in SAX. > > It's still unclear what the OP wanted exactly, though. To me, it read more > like the task was to copy some content over from one XML file to another, > in which case doing it in ET is just trivial thanks to the tree API, but > SAX requires you to reconstruct the XML brick by brick here. > > > > In my own personal case, I partly prefer xml.sax simply because it > ignores > > namespaces, a nice benefit in my cases. I wish I could make ElementTree > do > > that. > > The downside of namespace unaware parsing is that you never know what you > get. It works for some input, but it may also just fail arbitrarily, for > equally valid input. > > One cool thing about ET is that it makes namespace aware processing easy by > using fully qualified tag names (one string says it all). Most other XML > tools (including SAX) require some annoying prefix mapping setup that you > have to carry around in order to tell the processor that you are really > talking about the thing that it's showing to you. > > Stefan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jitu.icfai at gmail.com Wed May 7 22:28:52 2014 From: jitu.icfai at gmail.com (jitendra gupta) Date: Thu, 8 May 2014 01:58:52 +0530 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: no only XML (Complex) On Thu, May 8, 2014 at 1:51 AM, Danny Yoo wrote: > On Wed, May 7, 2014 at 6:13 AM, jitendra gupta > wrote: > > > I just want to create a new xm file from existing xml file. so basically > i > > want to put contry details in countryName.xml from these file. > > > > Side question: does your input have to be XML, or can it be in a > simpler format such as JSON? > -------------- next part -------------- An HTML attachment was scrubbed... URL: From neilc at norwich.edu Wed May 7 22:32:55 2014 From: neilc at norwich.edu (Neil D. Cerutti) Date: Wed, 07 May 2014 16:32:55 -0400 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: On 5/7/2014 3:49 PM, Stefan Behnel wrote:> Neil D. Cerutti, 07.05.2014 20:04: >> In my own personal case, I partly prefer xml.sax simply because it ignores >> namespaces, a nice benefit in my cases. I wish I could make ElementTree do >> that. > > The downside of namespace unaware parsing is that you never know what you > get. It works for some input, but it may also just fail arbitrarily, for > equally valid input. > > One cool thing about ET is that it makes namespace aware processing easy by > using fully qualified tag names (one string says it all). Most other XML > tools (including SAX) require some annoying prefix mapping setup that you > have to carry around in order to tell the processor that you are really > talking about the thing that it's showing to you. It's a minor frustration. I don't want to: index = xml_obj.find( "{http://www.ed.gov/FSA/COD/2011/v3.0e}School/" "{http://www.ed.gov/FSA/COD/2011/v3.0e}Student/" "{http://www.ed.gov/FSA/COD/2011/v3.0e}Index") ElementTree doesn't obviously make it easier. I work around it by wrapping the find and findall functions. There's probably a class I could inherit instead. Or maybe there's an even better approach. -- Neil Cerutti From dyoo at hashcollision.org Wed May 7 22:39:51 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 7 May 2014 13:39:51 -0700 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: On Wed, May 7, 2014 at 1:26 PM, jitendra gupta wrote: > I cant use etree/SAX because there we cant get complete line , of course we > can get it by tag name but we are not sure about tag also. Only we know > what ever child of we need to put in new file with country name. Why can't you use such an approach here? You're dealing with structured data: there's no concept of "line" in XML, so I don't know what you mean. You can keep an intermediate state of the events you've seen. At some point, after you encounter the end element of an particular country, you'll have seen the information you need to determine which file the country should go to. The pseudocode would be something like: ################################################################ read events up to beginning of data buffer = [] while there are still events: collect events up to country end into the "buffer" decide what file it goes to, and replay the "buffer" into the appropriate file clear "buffer" ################################################################ If you don't want to deal with a event-driven approach that SAX emphasizes, you may still be able to do this problem with an XML-Pull parser. You mention that your input is hundreds of megabytes long, in which case you probably really do need to be careful about memory consumption. See: https://wiki.python.org/moin/PullDom for an example that filters subtrees. You should be able to quickly adapt that example to redirect elements based on whatever criteria you decide. From dyoo at hashcollision.org Wed May 7 22:49:41 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 7 May 2014 13:49:41 -0700 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: To elaborate: ######################################################## from xml.dom.pulldom import START_ELEMENT, parse import io sampleData = u""" 2 2008 141100 5 2011 59900 69 2011 13600 """ doc = parse(io.StringIO(sampleData)) for event, node in doc: if event == START_ELEMENT and node.localName == "country": doc.expandNode(node) print("--------------") print("This is the node for " + node.getAttribute('name')) print("--------------") print(node.toxml()) print("\n\n") ######################################################## We can decide and make control-flow decisions in a stream-like manner. Note that we can even look at attributes, before printing the node back wherever we want. From steve at pearwood.info Thu May 8 00:57:22 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 8 May 2014 08:57:22 +1000 Subject: [Tutor] Final review In-Reply-To: References: <62D1E3F3-5A00-42AC-9AFD-80DF1D7DC3F1@me.com> Message-ID: <20140507225722.GB4273@ando> On Tue, May 06, 2014 at 06:36:21PM +0100, Alan Gauld wrote: > Aside: > len() does work with range() and slicing so you can write > > myList[:len(mylist)] > > to get a copy of your list... Even easier is a blank slice. The start defaults to zero, the end to the length of the sequence, and the step to 1, so to make a copy of a list is: myList[:] # start=0, end=length or myList[::] # start=0, end=length, step=1 -- Steven From illusiontechniques at gmail.com Thu May 8 01:27:32 2014 From: illusiontechniques at gmail.com (C Smith) Date: Wed, 7 May 2014 19:27:32 -0400 Subject: [Tutor] How inefficient is this code? Message-ID: A topic came up on slashdot concerning "intermediate" programming, where the poster expressed the feeling that the easy stuff is too easy and the hard stuff is too hard. Someone did point out that 'intermediate' programming would still involve actually selling code or at least some professional experience, so this would probably fall into the category of 'novice'. I feel similarly right now and the cs 101 classes from udacity or coursera or the opencourseware stuff are very easy and boring. I tried the class "design of computer programs" on udacity and made it about halfway before being overwhelmed. The class involved making an api for a regex-like function to parse text (if I am communicating that correctly, it was sort of rewriting regex functionality in Python terms), and it seemed to go over a very definite cliff in terms of difficulty. Could someone provide a concise example of decorator use? Someone suggested projecteuler.net and I started blazing through things I had done before, when I realized this would be a good time to start more efficient practices instead of code that just happens to work and may be very inefficient. #sum all even fib seq integers under 4 million fibs = [1,2] sum = 0 while fibs[-1] < 4000000: nexty = fibs[-1] + fibs[-2] fibs.append(nexty) for xer in fibs: if xer%2 == 0: sum += xer print sum This gets the correct solution, but what would be ways to improve speed or use more complicated parts of Python to do the same thing. Thanks in advance From steve at pearwood.info Thu May 8 02:17:15 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 8 May 2014 10:17:15 +1000 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: <20140508001715.GD4273@ando> On Wed, May 07, 2014 at 06:43:11PM +0530, jitendra gupta wrote: > Hi > > I just want to create a new xm file from existing xml file. so basically i > want to put contry details in countryName.xml from these file. > > I thought to do via read a line by line with normal file handling. but > there a problem with that. So i want to handle python XML . Could you > please suggest on this. I'm afraid you need to explain in more detail what you want to do. Given the input quoted below: > > > 2 > 2008 > 141100 > > > > > 5 > 2011 > 59900 > > what output are you expecting? -- Steven From brian.van.den.broek at gmail.com Thu May 8 02:39:03 2014 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Wed, 7 May 2014 20:39:03 -0400 Subject: [Tutor] How inefficient is this code? In-Reply-To: References: Message-ID: On May 7, 2014 7:30 PM, "C Smith" wrote: > Someone suggested projecteuler.net and I started blazing through > things I had done before, when I realized this would be a good time to > start more efficient practices instead of code that just happens to > work and may be very inefficient. > > #sum all even fib seq integers under 4 million > fibs = [1,2] > sum = 0 > while fibs[-1] < 4000000: > nexty = fibs[-1] + fibs[-2] > fibs.append(nexty) > > for xer in fibs: > if xer%2 == 0: > sum += xer > print sum > > This gets the correct solution, but what would be ways to improve > speed or use more complicated parts of Python to do the same thing. Intuitive judgements about efficiency and 5$ will buy you a Starbuck's. That qualification made: It is most likely to be more efficient to dispense with the results list. Instead, move the parity test into the while loop and augment the running sum there. But, test the resultant code for efficiency or you only know that some guy told you it was likely more efficient. Best, Brian vdB -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Thu May 8 02:39:47 2014 From: illusiontechniques at gmail.com (C Smith) Date: Wed, 7 May 2014 20:39:47 -0400 Subject: [Tutor] How inefficient is this code? In-Reply-To: References: Message-ID: I see. Thanks that is exactly what I was looking for. Would your example be an instance of an object-oriented approach, compartmentalizing things into functions? On Wed, May 7, 2014 at 7:55 PM, Joe Cortes wrote: > Welcome to the wonderful world of generators! > > Looking at your code, you'll notice two things. First, you're > iterating over all the numbers twice: once to calculate them, and then > another time to actually do the sum. What's worse, at any given point > in time, you're only really using fibs[-1] and fibs[-2], and yet > you're still building this huge list with all the numbers. This is bad > in terms of memory efficiency. Luckily, it's such a common problem > that Python offers a really nice, really Pythonic solution. You can > define the following function: > > def fib(top): > first = 1 # As an aside, consider starting the sequence from 0, 1. > next = 2 > yield first > while next < top: > yield next > # For kicks, try rewriting the following three lines as a > tuple assignment. > new_next = next + first > first = next > next = new_next > > Now, how do you use this? This being Python, there is a super simple > way to do this: > > sum = 0 > for num in fib(4000000): > if num % 2 == 0: > sum += num > print sum > > > So, what's going on here? fib is mostly straightforward, but that > yield keyword might look new. > Here's the deal: when you have a function that uses the yield keyword, > that function becomes usable in the context of a for loop. Every time > it hits a yield it will, well, yield that value out to the for loop. > At the end of an iteration, control will be handed back to the > function, which will do its magic, and yield another value. Again, the > value will be sent out to the for loop, in this case as the "num" > variable, for use in that iteration. > When the function hits a return (or reaches the end of its body), the > for loop ends. > > This is called a generator function, and is a really nice way to > simplify and optimize your loops. > > You can read more about generators here: https://wiki.python.org/moin/Generators > > > On Wed, May 7, 2014 at 7:27 PM, C Smith wrote: >> A topic came up on slashdot concerning "intermediate" programming, >> where the poster expressed the feeling that the easy stuff is too easy >> and the hard stuff is too hard. >> >> Someone did point out that 'intermediate' programming would still >> involve actually selling code or at least some professional >> experience, so this would probably fall into the category of 'novice'. >> >> I feel similarly right now and the cs 101 classes from udacity or >> coursera or the opencourseware stuff are very easy and boring. I tried >> the class "design of computer programs" on udacity and made it about >> halfway before being overwhelmed. The class involved making an api for >> a regex-like function to parse text (if I am communicating that >> correctly, it was sort of rewriting regex functionality in Python >> terms), and it seemed to go over a very definite cliff in terms of >> difficulty. Could someone provide a concise example of decorator use? >> >> Someone suggested projecteuler.net and I started blazing through >> things I had done before, when I realized this would be a good time to >> start more efficient practices instead of code that just happens to >> work and may be very inefficient. >> >> #sum all even fib seq integers under 4 million >> fibs = [1,2] >> sum = 0 >> while fibs[-1] < 4000000: >> nexty = fibs[-1] + fibs[-2] >> fibs.append(nexty) >> >> for xer in fibs: >> if xer%2 == 0: >> sum += xer >> print sum >> >> This gets the correct solution, but what would be ways to improve >> speed or use more complicated parts of Python to do the same thing. >> Thanks in advance >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor From illusiontechniques at gmail.com Thu May 8 02:42:31 2014 From: illusiontechniques at gmail.com (C Smith) Date: Wed, 7 May 2014 20:42:31 -0400 Subject: [Tutor] How inefficient is this code? In-Reply-To: References: Message-ID: I guess intuiting efficiency doesn't work in Python because it is such high-level? Or is there much more going on there? From akleider at sonic.net Thu May 8 02:32:12 2014 From: akleider at sonic.net (Alex Kleider) Date: Wed, 07 May 2014 17:32:12 -0700 Subject: [Tutor] =?utf-8?q?How_inefficient_is_this_code=3F?= In-Reply-To: References: Message-ID: <8bf76fc7651e4a9082b881d593c39dca@sonic.net> On 2014-05-07 16:27, C Smith wrote: > #sum all even fib seq integers under 4 million > fibs = [1,2] > sum = 0 > while fibs[-1] < 4000000: > nexty = fibs[-1] + fibs[-2] > fibs.append(nexty) > > for xer in fibs: > if xer%2 == 0: > sum += xer > print sum > > This gets the correct solution, but what would be ways to improve > speed or use more complicated parts of Python to do the same thing. > Thanks in advance I think you could eliminate the second part, could you not, by inserting the (slightly modified) if statement into the while statement? while fibs[-1] < 4000000: nexty = fibs[-1] + fibs[-2] if nexty%2 ==0: sum += nexty fibs.append(nexty) Also you might want to initialize 'sum' to 1 rather than 0 since the second element in your 'fibs' won't get counted. From brian.van.den.broek at gmail.com Thu May 8 02:49:42 2014 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Wed, 7 May 2014 20:49:42 -0400 Subject: [Tutor] How inefficient is this code? In-Reply-To: References: Message-ID: On May 7, 2014 8:42 PM, "C Smith" wrote: > > I guess intuiting efficiency doesn't work in Python because it is such > high-level? Or is there much more going on there? Hi, The level is a good part, yes. If, like me, you don't know what C constructs are under python constructs, you are guessing shapes of objects by the shadows they cast. But, that's not the sole issue. Ever write code that you thought did what you wanted only to run it and find out you were wrong? That happens because your intuitive judgements aren't giving you perfectly reliable insight into what code does :-) Best, Brian vdB -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu May 8 03:07:48 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 08 May 2014 02:07:48 +0100 Subject: [Tutor] How inefficient is this code? In-Reply-To: References: Message-ID: On 08/05/14 01:42, C Smith wrote: > I guess intuiting efficiency doesn't work in Python because it is such > high-level? Or is there much more going on there? Efficiency is usually language independent because its the algorithm, or design, that determines efficiency in most cases. Your example is a good case in point. The best efficiency improvement is to eliminate the second loop by doing the sum inside the first loop. That would be true in any language. If you find yourself doing language specific tweaks to improve efficiency then you are probably doing the wrong thing. And you should definitely be using the profiler to prove that what you are doing is the correct thing. But mostly you will want to change the algorithm before attempting to tweak the inner workings of the code. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From scott.w.d at cox.net Thu May 8 05:49:11 2014 From: scott.w.d at cox.net (Scott W Dunning) Date: Wed, 7 May 2014 20:49:11 -0700 Subject: [Tutor] Final review In-Reply-To: References: <7D3512F1-CC80-47D6-B9DD-C26837EFE3D3@cox.net> Message-ID: On May 5, 2014, at 10:13 PM, meenu ravi wrote: > Likewise, the index of d, which is the last word in the word "Hello world" is 10. > > So, the maximum index you can access in the word "Hello world" is 10. But when you try to give the command, > > >>> greeting [len(greeting)] > > It is trying to access the character at the position "11", where the string "Hello world" doesn't contain any value in the index "11" and the maximum index is 10. So it throws the following error. I think this is where I am getting confused. I guess I don?t understand why/how it?s trying to access the character at the index 11? > > Traceback (most recent call last): > > File "", line 1, in > > IndexError: string index out of range > > So always the maximum index will be length - 1. So if you want to access the last character of the string "Hello world", give the command: > > >>> greeting[len(greeting)-1] > > 'd' > > Hope this helps From dyoo at hashcollision.org Thu May 8 06:34:49 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 7 May 2014 21:34:49 -0700 Subject: [Tutor] Final review In-Reply-To: References: <7D3512F1-CC80-47D6-B9DD-C26837EFE3D3@cox.net> Message-ID: On Wed, May 7, 2014 at 8:49 PM, Scott W Dunning wrote: > > On May 5, 2014, at 10:13 PM, meenu ravi wrote: > >> Likewise, the index of d, which is the last word in the word "Hello world" is 10. >> >> So, the maximum index you can access in the word "Hello world" is 10. But when you try to give the command, >> >> >>> greeting [len(greeting)] >> >> It is trying to access the character at the position "11", where the string "Hello world" doesn't contain any value in the index "11" and the maximum index is 10. So it throws the following error. > > I think this is where I am getting confused. I guess I don?t understand why/how it?s trying to access the character at the index 11? Do you mean: why 11? If so: how many characters long is the greeting? From escozzia at gmail.com Thu May 8 01:55:57 2014 From: escozzia at gmail.com (Joe Cortes) Date: Wed, 7 May 2014 19:55:57 -0400 Subject: [Tutor] How inefficient is this code? In-Reply-To: References: Message-ID: Welcome to the wonderful world of generators! Looking at your code, you'll notice two things. First, you're iterating over all the numbers twice: once to calculate them, and then another time to actually do the sum. What's worse, at any given point in time, you're only really using fibs[-1] and fibs[-2], and yet you're still building this huge list with all the numbers. This is bad in terms of memory efficiency. Luckily, it's such a common problem that Python offers a really nice, really Pythonic solution. You can define the following function: def fib(top): first = 1 # As an aside, consider starting the sequence from 0, 1. next = 2 yield first while next < top: yield next # For kicks, try rewriting the following three lines as a tuple assignment. new_next = next + first first = next next = new_next Now, how do you use this? This being Python, there is a super simple way to do this: sum = 0 for num in fib(4000000): if num % 2 == 0: sum += num print sum So, what's going on here? fib is mostly straightforward, but that yield keyword might look new. Here's the deal: when you have a function that uses the yield keyword, that function becomes usable in the context of a for loop. Every time it hits a yield it will, well, yield that value out to the for loop. At the end of an iteration, control will be handed back to the function, which will do its magic, and yield another value. Again, the value will be sent out to the for loop, in this case as the "num" variable, for use in that iteration. When the function hits a return (or reaches the end of its body), the for loop ends. This is called a generator function, and is a really nice way to simplify and optimize your loops. You can read more about generators here: https://wiki.python.org/moin/Generators On Wed, May 7, 2014 at 7:27 PM, C Smith wrote: > A topic came up on slashdot concerning "intermediate" programming, > where the poster expressed the feeling that the easy stuff is too easy > and the hard stuff is too hard. > > Someone did point out that 'intermediate' programming would still > involve actually selling code or at least some professional > experience, so this would probably fall into the category of 'novice'. > > I feel similarly right now and the cs 101 classes from udacity or > coursera or the opencourseware stuff are very easy and boring. I tried > the class "design of computer programs" on udacity and made it about > halfway before being overwhelmed. The class involved making an api for > a regex-like function to parse text (if I am communicating that > correctly, it was sort of rewriting regex functionality in Python > terms), and it seemed to go over a very definite cliff in terms of > difficulty. Could someone provide a concise example of decorator use? > > Someone suggested projecteuler.net and I started blazing through > things I had done before, when I realized this would be a good time to > start more efficient practices instead of code that just happens to > work and may be very inefficient. > > #sum all even fib seq integers under 4 million > fibs = [1,2] > sum = 0 > while fibs[-1] < 4000000: > nexty = fibs[-1] + fibs[-2] > fibs.append(nexty) > > for xer in fibs: > if xer%2 == 0: > sum += xer > print sum > > This gets the correct solution, but what would be ways to improve > speed or use more complicated parts of Python to do the same thing. > Thanks in advance > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From escozzia at gmail.com Thu May 8 04:03:28 2014 From: escozzia at gmail.com (Joe Cortes) Date: Wed, 7 May 2014 22:03:28 -0400 Subject: [Tutor] How inefficient is this code? In-Reply-To: References: Message-ID: > I see. Thanks that is exactly what I was looking for. Would your > example be an instance of an object-oriented approach, > compartmentalizing things into functions? That's an interesting question. It's an example of abstraction, which is one of the pillars of object oriented design. You're abstracting the principle of calculating the fibonacci numbers, into the function fib, and separating it from the concrete use cases for the numbers themselves. This is probably the pattern you're detecting here and associating with OOP: you take a self-contained piece of functionality and throw it somewhere where its logic can be used in an abstract manner. Now, there are more pillars to object oriented design: encapsulation, inheritance, and polymorphism, and this example demonstrates neither. For example, it doesn't really feature polymorphism: the fib function is very concrete, and has really only one "form", namely the one you're seeing. So, while these generator functions introduce a pretty neat possibility for abstraction, namely abstracting out the concept of iteration and separating from what happens during iteration, I'm not really sure you can call it OOP. Perhaps it depends on the way you look at it. A more complex generator (maybe implemented as an object) might, for example, be polymorphic in nature. These design questions can often blur, so I'm not sure how better to answer your question, maybe someone can chime in. On Wed, May 7, 2014 at 8:39 PM, C Smith wrote: > On Wed, May 7, 2014 at 7:55 PM, Joe Cortes wrote: >> Welcome to the wonderful world of generators! >> >> Looking at your code, you'll notice two things. First, you're >> iterating over all the numbers twice: once to calculate them, and then >> another time to actually do the sum. What's worse, at any given point >> in time, you're only really using fibs[-1] and fibs[-2], and yet >> you're still building this huge list with all the numbers. This is bad >> in terms of memory efficiency. Luckily, it's such a common problem >> that Python offers a really nice, really Pythonic solution. You can >> define the following function: >> >> def fib(top): >> first = 1 # As an aside, consider starting the sequence from 0, 1. >> next = 2 >> yield first >> while next < top: >> yield next >> # For kicks, try rewriting the following three lines as a >> tuple assignment. >> new_next = next + first >> first = next >> next = new_next >> >> Now, how do you use this? This being Python, there is a super simple >> way to do this: >> >> sum = 0 >> for num in fib(4000000): >> if num % 2 == 0: >> sum += num >> print sum >> >> >> So, what's going on here? fib is mostly straightforward, but that >> yield keyword might look new. >> Here's the deal: when you have a function that uses the yield keyword, >> that function becomes usable in the context of a for loop. Every time >> it hits a yield it will, well, yield that value out to the for loop. >> At the end of an iteration, control will be handed back to the >> function, which will do its magic, and yield another value. Again, the >> value will be sent out to the for loop, in this case as the "num" >> variable, for use in that iteration. >> When the function hits a return (or reaches the end of its body), the >> for loop ends. >> >> This is called a generator function, and is a really nice way to >> simplify and optimize your loops. >> >> You can read more about generators here: https://wiki.python.org/moin/Generators >> >> >> On Wed, May 7, 2014 at 7:27 PM, C Smith wrote: >>> A topic came up on slashdot concerning "intermediate" programming, >>> where the poster expressed the feeling that the easy stuff is too easy >>> and the hard stuff is too hard. >>> >>> Someone did point out that 'intermediate' programming would still >>> involve actually selling code or at least some professional >>> experience, so this would probably fall into the category of 'novice'. >>> >>> I feel similarly right now and the cs 101 classes from udacity or >>> coursera or the opencourseware stuff are very easy and boring. I tried >>> the class "design of computer programs" on udacity and made it about >>> halfway before being overwhelmed. The class involved making an api for >>> a regex-like function to parse text (if I am communicating that >>> correctly, it was sort of rewriting regex functionality in Python >>> terms), and it seemed to go over a very definite cliff in terms of >>> difficulty. Could someone provide a concise example of decorator use? >>> >>> Someone suggested projecteuler.net and I started blazing through >>> things I had done before, when I realized this would be a good time to >>> start more efficient practices instead of code that just happens to >>> work and may be very inefficient. >>> >>> #sum all even fib seq integers under 4 million >>> fibs = [1,2] >>> sum = 0 >>> while fibs[-1] < 4000000: >>> nexty = fibs[-1] + fibs[-2] >>> fibs.append(nexty) >>> >>> for xer in fibs: >>> if xer%2 == 0: >>> sum += xer >>> print sum >>> >>> This gets the correct solution, but what would be ways to improve >>> speed or use more complicated parts of Python to do the same thing. >>> Thanks in advance >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> https://mail.python.org/mailman/listinfo/tutor From kevin.johnson2711 at gmail.com Thu May 8 12:00:11 2014 From: kevin.johnson2711 at gmail.com (Kevin Johnson) Date: Thu, 8 May 2014 10:00:11 +0000 Subject: [Tutor] (no subject) Message-ID: Hi, Total beginner to python and am working my way through Michael Dawsons 'Absolute beginner' book. Just got stuck on the last bit of the challenges from chapter 3. Essentially need to create a game where the user picks a number between 1 and 100 and the computer has to guess, program should indicate to the computer if the guess need to be higher or lower, it should also count the number of attempts and call a halt to the game if a set number of attempts is reached. The highlighted bit is where I think I'm going wrong but I just can't think how to make the computer remember the previously closest highest and lowest guesses, not sure if you have all read Micheal Dawsons book but I've not covered a whole lot by chapter 3 and so any hints/solutions need to be pretty basic as I don't want to get ahead of myself. Thanks, Kevin #numbers game again but... #user picks number between 1 and 100 #and computer guesses import random user_number = int(input("Pick a number between 1 and 100: ")) computer_guess = random.randint(1, 100) guesses = 1 while computer_guess != user_number: print("The computers guess is", computer_guess) if computer_guess > user_number: print("Lower...") else: print("Higher...") if guesses == 10: print("You lose computer!") break if computer_guess > user_number: computer_guess = random.randrange(1, (computer_guess-1)) else: computer_guess = random.randint((computer_guess + 1), 100) guesses += 1 if computer_guess == user_number: print("Well done you guessed the number was", user_number) print("It took you", guesses, "tries") input("Press the enter key to exit.") -------------- next part -------------- An HTML attachment was scrubbed... URL: From jslozier at gmail.com Thu May 8 15:27:40 2014 From: jslozier at gmail.com (Jay Lozier) Date: Thu, 08 May 2014 09:27:40 -0400 Subject: [Tutor] (no subject) - (added) Number guessing game In-Reply-To: References: Message-ID: <536B864C.9010301@gmail.com> An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu May 8 15:30:49 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 8 May 2014 23:30:49 +1000 Subject: [Tutor] Final review In-Reply-To: References: <7D3512F1-CC80-47D6-B9DD-C26837EFE3D3@cox.net> Message-ID: <20140508133048.GF4273@ando> On Wed, May 07, 2014 at 08:49:11PM -0700, Scott W Dunning wrote: [...] > > >>> greeting [len(greeting)] > > > > It is trying to access the character at the position "11", where the > > string "Hello world" doesn't contain any value in the index "11" and > > the maximum index is 10. So it throws the following error. > > I think this is where I am getting confused. I guess I don?t > understand why/how it?s trying to access the character at the index > 11? The value of greeting is "Hello world". So let's write it out, showing the index of each character. Remember that Python starts counting from zero, not one: Index 0: H Index 1: e Index 2: l Index 3: l Index 4: o Index 5: space Index 6: w Index 7: o Index 8: r Index 9: l Index 10: d So the indexes start from 0, and go up to 10. How many characters are there? Count them, and you get 11. Which makes sense: one character per index, there are at least ten indexes (1 through 10), plus one extra (index 0) makes 11. So the length of the string is 11, but the highest index is 10. So greeting[0] gives "H", greeting[1] gives "e", greeting[2] gives "l", and so on, until you get to greeting[10] which gives "d", and greeting[len(greeting)] => greeting[11] which is an error. -- Steven From alan.gauld at btinternet.com Thu May 8 15:46:10 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 08 May 2014 14:46:10 +0100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 08/05/14 11:00, Kevin Johnson wrote: > user picks a number between 1 and 100 and the computer has to guess, Hmm, usually its the other way around, the computer picks a number and the user guesses it. Interesting twist! > The highlighted bit is where I think I'm going wrong but I just can't > think how to make the computer remember the previously closest highest > and lowest guesses, I'm not sure why you think you need to do that? But simply you create a new variable called last_guess or somesuch and store computer_guess there before you change it. But the biggest problem is that the computer is using random values between 1 and the current guess(+/-1). That's a very inefficient way to get to the place you want. A more efficient technique is something called a binary chop: You know the users number is between 1 and 100 so you start in the middle at 50. If its too high then the users guess is between 1 and 49 so you pick the new middle - say 25 If its too high the target is between 1 and 24m, so you pick the middle -> 12 and so on. Similarly if the first guess is too low you know the user is between 51 and 100 so you pick the middle - 75 If its too high you pick midway between 75 and 100 and so on. That should pretty much always get the computer to the right answer within its 10 guesses. The only values you need to keep track of are the lower and upper bounds - starting at 1 & 100 respectively. > user_number = int(input("Pick a number between 1 and 100: ")) > computer_guess = random.randint(1, 100) > guesses = 1 > > while computer_guess != user_number: ... > if computer_guess > user_number: > computer_guess = random.randrange(1, (computer_guess-1)) > else: > computer_guess = random.randint((computer_guess + 1), 100) > guesses += 1 > if computer_guess == user_number: > print("Well done you guessed the number was", user_number) > print("It took you", guesses, "tries") There is also a bug in the above code: in the case that the computer guesses the correct number the else clause causes the computer to generate a new guess. So the computer can never win on the first guess!! The else line should really be: elif computer_guess < user_number: Also why use randint() on one line but randrange() on the other? They are subtly different and only one is correct in this situation. I'll let you figure out which! :-) And if you use binary chop you won't need either! HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Thu May 8 15:55:31 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 8 May 2014 23:55:31 +1000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <20140508135531.GG4273@ando> Hello Kevin, and welcome! My responses are below, interleaved between yours. On Thu, May 08, 2014 at 10:00:11AM +0000, Kevin Johnson wrote: > Hi, > > Total beginner to python and am working my way through Michael Dawsons > 'Absolute beginner' book. Just got stuck on the last bit of the challenges > from chapter 3. Essentially need to create a game where the user picks a > number between 1 and 100 and the computer has to guess, program should > indicate to the computer if the guess need to be higher or lower, it should > also count the number of attempts and call a halt to the game if a set > number of attempts is reached. > > The highlighted bit is where I think I'm going wrong Many programmers -- myself included -- operate exclusively with "plain text", no formatting. I won't go into all the reasons, but let's just assume we have a good reason for it. Unfortunately that means that many of us can't see your highlighting. If you want to maximize the number of tutors here who can answer your questions, you may like to find a way to comment your code other than coloured highlighting. A good way is to insert a comment, like # This is the section I'm having problems with. [code goes here] # This is the end of the section. Or similar. > but I just can't think > how to make the computer remember the previously closest highest and lowest > guesses, Any time you want the computer to remember something, the most obvious way is to give it a variable. I'm not quite sure what you mean by "previously closest highest and lowest guesses", but I'm going to take a stab at it. You want the computer to compare the current guess to your number, and if the guess is *closer* to the previous closest guess, remember it. E.g. suppose you pick the number 50, and the computer guesses 25. Since this is the first guess, it's automatically the closest. Then the computer guesses 60. Since 60-50 = 10 is smaller than 50-25 = 25, 60 is the closest. Then the computer guesses 30. Since 50-30 = 20 is greater than 10, 60 remains the closest. Am I on the right track? Assuming I am, how might I program this? The first thing is to have a variable that holds the closest value: closest = ... what? Here's a trick. Since we know that the guesses are always between 1 and 100, if we set the first "closest" to something greater than 100, the first guess will always count as closer! So: closest = 9999 Then, as you check each guess: if abs(computer_guess - user_number) < abs(closest - user_number): # this guess is closer than the previous closest closest = computer_guess With a little thought you should be able to extend this idea to keeping two variables, the closest_high number and the closest_low number. If the computer's guess is too low, you check and update closest_low; if the guess is too high, you check and update closest_high. Regards, -- Steven From neilc at norwich.edu Thu May 8 15:59:05 2014 From: neilc at norwich.edu (Neil D. Cerutti) Date: Thu, 08 May 2014 09:59:05 -0400 Subject: [Tutor] How inefficient is this code? In-Reply-To: References: Message-ID: On 5/7/2014 8:39 PM, C Smith wrote: > I see. Thanks that is exactly what I was looking for. Would your > example be an instance of an object-oriented approach, > compartmentalizing things into functions? One of the fun parts of Project Euler is using mathematical analysis and other tricks to improve your algorithm. You have a nice, general Fibonacci number generator, but you can use a less general function to increase efficiency [1]. In this case, it doesn't matter--your initial solution was fast enough--but you'll soon be solving problems where the most general solution fails to be fast enough. [1] Your code is calculating about twice as many Fibonacci numbers as it needs to. See if you can create a way to calculate Fib(n+2) from Fib(n). -- Neil Cerutti From zebra05 at gmail.com Thu May 8 16:02:07 2014 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Thu, 8 May 2014 16:02:07 +0200 Subject: [Tutor] PyCountry currency formatting woes In-Reply-To: References: Message-ID: Thank you all, babel works just fine. I also tried ccy, which isn't bad either - except that it returns non-unicode currency letters for countries in the Eurozone. On Mon, May 5, 2014 at 10:10 AM, Peter Otten <__peter__ at web.de> wrote: > Sithembewena Lloyd Dube wrote: > > > Thanks, i was actually getting the error information to update the post. > > Apoligies to waste your time posting here - I could not find an > > appropriate PyCountry discussion list and my next best bet seemed to be a > > Python users' list. > > > > For those who care to look, the error is as follows (a concise example > > from an interactive shell: > > > > import pycountry > > country = pycountry.countries.get(alpha2='DE') > > currency = pycountry.currencies.get(numeric=country.numeric) > > Traceback (most recent call last): > > File "", line 1, in > > File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get > > return self.indices[field][value] > > KeyError: '276' > > > > The obvious issue here is that the pycountry.countries collection does > not > > contain a currency with a numeric of 276 (Germany's numeric) - yet it > does > > contain the Euro. Any ideas as to what the way around this may be? > > It looks like the development version of babel > > > http://babel.pocoo.org/docs/api/numbers/#babel.numbers.get_territory_currencies > > can do what you want: > > $ LANG=en_US.UTF-8 python > Python 2.7.5+ (default, Feb 27 2014, 19:37:08) > [GCC 4.8.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import babel.numbers as bn > >>> bn.get_territory_currencies("DE") > ['EUR'] > >>> print bn.format_currency(1.234, "EUR") > ?1.23 > >>> print bn.format_currency(1.234, "EUR", locale="DE") > 1,23 ? > >>> import babel > >>> babel.__version__ > '2.0-dev' > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Regards, Sithu Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: From epalmer at richmond.edu Thu May 8 21:04:44 2014 From: epalmer at richmond.edu (Palmer, Eric) Date: Thu, 8 May 2014 19:04:44 +0000 Subject: [Tutor] preferred httprequest library Message-ID: Sorry, I?m sure this has been asked before: I?m new to python but not so much to programming. I need to construct a set or programs to test a forms poster that has been enhanced (it is in php). I mostly need http get and post. This is a hands on set of tests and does not have to be bullet proof. What would be a good http request library to use for this work? Thanks in advance Eric Palmer From martin at linux-ip.net Fri May 9 00:54:04 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Thu, 8 May 2014 18:54:04 -0400 Subject: [Tutor] preferred httprequest library In-Reply-To: References: Message-ID: Hello, : I?m new to python but not so much to programming. I need to : construct a set or programs to test a forms poster that has been : enhanced (it is in php). I mostly need http get and post. This : is a hands on set of tests and does not have to be bullet proof. : : What would be a good http request library to use for this work? There are many options. When you can afford to suck the entire remote resource into memory (as with many applications), you will probably find the 'requests' library very handy. I'd start here, avoiding grabbing for the standard library tools (urllib, and urllib2) unless you need that finer control. This has a nice abstraction and, from your description, I think this would be a good fit: http://docs.python-requests.org/en/latest/ -Martin -- Martin A. Brown http://linux-ip.net/ From kevin.johnson2711 at gmail.com Fri May 9 01:42:38 2014 From: kevin.johnson2711 at gmail.com (Kevin Johnson) Date: Thu, 8 May 2014 23:42:38 +0000 Subject: [Tutor] Tutor Digest, Vol 123, Issue 27 In-Reply-To: References: Message-ID: Alan and Steven, Many thanks for both your help. *Alan* Although I understand that the method you described is the most efficient way of guessing a random number, I was trying so that my program would have the computer making repeated random guesses between a decreasing range of its last two closest guesses. That is if the user chooses 55 as the number and the computers first guess is 98 then its next guess would be between 1 and 97, if the 2nd guess was 23 the 3rd guess would between 24 and 97, if the 4th guess was 50 then the 5th guess would be between 51 and 98, the 6th guess being 60 then the 7th guess would be between 51 and 59, etc etc. I was hoping that this may mean the computer won't get it every time in 10 guesses and be a bit more as a human might guess (at least the first few times they played before they cottoned on to the method you described). And thank you for the bug spot! Also yes there is a typo in that I didn't mean to have have that random.randrange in there ( was meant to be a randint) *Steven* Thank you for the info re highlighting! Yes I think your example below describes what I was trying to do, >E.g. suppose you pick the number 50, and the computer guesses 25. Since >this is the first guess, it's automatically the closest. >Then the computer guesses 60. Since 60-50 = 10 is smaller than 50-25 = >25, 60 is the closest. >Then the computer guesses 30. Since 50-30 = 20 is greater than 10, 60 >remains the closest. >Am I on the right track? Assuming I am, how might I program this? The >first thing is to have a variable that holds the closest value: >closest = ... what? Here's a trick. Since we know that the guesses are >always between 1 and 100, if we set the first "closest" to something >greater than 100, the first guess will always count as closer! So: >closest = 9999 >Then, as you check each guess: > if abs(computer_guess - user_number) < abs(closest - user_number): > # this guess is closer than the previous closest > closest = computer_guess But (just to clarify) as I put the simple example you gave through the code you supplied I get a little confused as it doesn't seem to save the closest? For example this is how i worked it: if (25 - 50) < (9999 - 50) closest = 25 if (60-50) < (25 - 50) closest = 25? As you stated for yor example a guess of 60 would be closer than the guess of 25. As I said I really am a beginner and I apologise if my questions are frustrating but I do appreciate any help offered. Thanks again. Kevin On Thu, May 8, 2014 at 2:02 PM, wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://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: Final review (Steven D'Aprano) > 2. Re: (no subject) (Alan Gauld) > 3. Re: (no subject) (Steven D'Aprano) > 4. Re: How inefficient is this code? (Neil D. Cerutti) > 5. Re: PyCountry currency formatting woes (Sithembewena Lloyd Dube) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Thu, 8 May 2014 23:30:49 +1000 > From: Steven D'Aprano > To: tutor at python.org > Subject: Re: [Tutor] Final review > Message-ID: <20140508133048.GF4273 at ando> > Content-Type: text/plain; charset=utf-8 > > On Wed, May 07, 2014 at 08:49:11PM -0700, Scott W Dunning wrote: > [...] > > > >>> greeting [len(greeting)] > > > > > > It is trying to access the character at the position "11", where the > > > string "Hello world" doesn't contain any value in the index "11" and > > > the maximum index is 10. So it throws the following error. > > > > I think this is where I am getting confused. I guess I don?t > > understand why/how it?s trying to access the character at the index > > 11? > > The value of greeting is "Hello world". So let's write it out, showing > the index of each character. Remember that Python starts counting from > zero, not one: > > Index 0: H > Index 1: e > Index 2: l > Index 3: l > Index 4: o > Index 5: space > Index 6: w > Index 7: o > Index 8: r > Index 9: l > Index 10: d > > So the indexes start from 0, and go up to 10. How many characters are > there? Count them, and you get 11. Which makes sense: one character per > index, there are at least ten indexes (1 through 10), plus one extra > (index 0) makes 11. So the length of the string is 11, but the highest > index is 10. > > So greeting[0] gives "H", greeting[1] gives "e", greeting[2] gives "l", > and so on, until you get to greeting[10] which gives "d", and > greeting[len(greeting)] => greeting[11] which is an error. > > > -- > Steven > > > ------------------------------ > > Message: 2 > Date: Thu, 08 May 2014 14:46:10 +0100 > From: Alan Gauld > To: tutor at python.org > Subject: Re: [Tutor] (no subject) > Message-ID: > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > On 08/05/14 11:00, Kevin Johnson wrote: > > user picks a number between 1 and 100 and the computer has to guess, > > Hmm, usually its the other way around, the computer picks a number and > the user guesses it. Interesting twist! > > > The highlighted bit is where I think I'm going wrong but I just can't > > think how to make the computer remember the previously closest highest > > and lowest guesses, > > I'm not sure why you think you need to do that? > > But simply you create a new variable called last_guess > or somesuch and store computer_guess there before you > change it. > > But the biggest problem is that the computer is using random values > between 1 and the current guess(+/-1). That's a very inefficient > way to get to the place you want. > > A more efficient technique is something called a binary chop: > > You know the users number is between 1 and 100 so you start > in the middle at 50. > > If its too high then the users guess is between 1 and 49 > so you pick the new middle - say 25 > > If its too high the target is between 1 and 24m, so you pick > the middle -> 12 > > and so on. > > Similarly if the first guess is too low you know the user is > between 51 and 100 so you pick the middle - 75 > > If its too high you pick midway between 75 and 100 and so on. > > That should pretty much always get the computer to the > right answer within its 10 guesses. > > The only values you need to keep track of are the lower > and upper bounds - starting at 1 & 100 respectively. > > > user_number = int(input("Pick a number between 1 and 100: ")) > > computer_guess = random.randint(1, 100) > > guesses = 1 > > > > while computer_guess != user_number: > ... > > > if computer_guess > user_number: > > computer_guess = random.randrange(1, (computer_guess-1)) > > else: > > computer_guess = random.randint((computer_guess + 1), 100) > > guesses += 1 > > if computer_guess == user_number: > > print("Well done you guessed the number was", user_number) > > print("It took you", guesses, "tries") > > There is also a bug in the above code: in the case that the > computer guesses the correct number the else clause causes the > computer to generate a new guess. > > So the computer can never win on the first guess!! > > The else line should really be: > > elif computer_guess < user_number: > > Also why use randint() on one line but randrange() on the other? > They are subtly different and only one is correct in this situation. > I'll let you figure out which! :-) > > And if you use binary chop you won't need either! > > > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > > ------------------------------ > > Message: 3 > Date: Thu, 8 May 2014 23:55:31 +1000 > From: Steven D'Aprano > To: tutor at python.org > Subject: Re: [Tutor] (no subject) > Message-ID: <20140508135531.GG4273 at ando> > Content-Type: text/plain; charset=us-ascii > > Hello Kevin, and welcome! > > My responses are below, interleaved between yours. > > On Thu, May 08, 2014 at 10:00:11AM +0000, Kevin Johnson wrote: > > Hi, > > > > Total beginner to python and am working my way through Michael Dawsons > > 'Absolute beginner' book. Just got stuck on the last bit of the > challenges > > from chapter 3. Essentially need to create a game where the user picks a > > number between 1 and 100 and the computer has to guess, program should > > indicate to the computer if the guess need to be higher or lower, it > should > > also count the number of attempts and call a halt to the game if a set > > number of attempts is reached. > > > > The highlighted bit is where I think I'm going wrong > > Many programmers -- myself included -- operate exclusively with "plain > text", no formatting. I won't go into all the reasons, but let's just > assume we have a good reason for it. Unfortunately that means that many > of us can't see your highlighting. If you want to maximize the number of > tutors here who can answer your questions, you may like to find a way to > comment your code other than coloured highlighting. A good way is to > insert a comment, like > > # This is the section I'm having problems with. > [code goes here] > # This is the end of the section. > > Or similar. > > > > but I just can't think > > how to make the computer remember the previously closest highest and > lowest > > guesses, > > Any time you want the computer to remember something, the most obvious > way is to give it a variable. I'm not quite sure what you mean by > "previously closest highest and lowest guesses", but I'm going to take a > stab at it. You want the computer to compare the current guess to your > number, and if the guess is *closer* to the previous closest guess, > remember it. > > E.g. suppose you pick the number 50, and the computer guesses 25. Since > this is the first guess, it's automatically the closest. > > Then the computer guesses 60. Since 60-50 = 10 is smaller than 50-25 = > 25, 60 is the closest. > > Then the computer guesses 30. Since 50-30 = 20 is greater than 10, 60 > remains the closest. > > Am I on the right track? Assuming I am, how might I program this? The > first thing is to have a variable that holds the closest value: > closest = ... what? Here's a trick. Since we know that the guesses are > always between 1 and 100, if we set the first "closest" to something > greater than 100, the first guess will always count as closer! So: > > closest = 9999 > > Then, as you check each guess: > > if abs(computer_guess - user_number) < abs(closest - user_number): > # this guess is closer than the previous closest > closest = computer_guess > > With a little thought you should be able to extend this idea to keeping > two variables, the closest_high number and the closest_low number. If > the computer's guess is too low, you check and update closest_low; if > the guess is too high, you check and update closest_high. > > > Regards, > > > -- > Steven > > > ------------------------------ > > Message: 4 > Date: Thu, 08 May 2014 09:59:05 -0400 > From: "Neil D. Cerutti" > To: tutor at python.org > Subject: Re: [Tutor] How inefficient is this code? > Message-ID: > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > > On 5/7/2014 8:39 PM, C Smith wrote: > > I see. Thanks that is exactly what I was looking for. Would your > > example be an instance of an object-oriented approach, > > compartmentalizing things into functions? > > One of the fun parts of Project Euler is using mathematical analysis and > other tricks to improve your algorithm. > > You have a nice, general Fibonacci number generator, but you can use a > less general function to increase efficiency [1]. In this case, it > doesn't matter--your initial solution was fast enough--but you'll soon > be solving problems where the most general solution fails to be fast > enough. > > [1] Your code is calculating about twice as many Fibonacci numbers as it > needs to. See if you can create a way to calculate Fib(n+2) from Fib(n). > > -- > Neil Cerutti > > > > ------------------------------ > > Message: 5 > Date: Thu, 8 May 2014 16:02:07 +0200 > From: Sithembewena Lloyd Dube > To: Python , django-users at googlegroups.com > Subject: Re: [Tutor] PyCountry currency formatting woes > Message-ID: > < > CAH-SnCDmUJPseY7q27xifgkaKJou5Xbvo5_OWf-DFBsrP85m0A at mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > Thank you all, babel works just fine. I also tried ccy, which isn't bad > either - except that it returns non-unicode currency letters for countries > in the Eurozone. > > > On Mon, May 5, 2014 at 10:10 AM, Peter Otten <__peter__ at web.de> wrote: > > > Sithembewena Lloyd Dube wrote: > > > > > Thanks, i was actually getting the error information to update the > post. > > > Apoligies to waste your time posting here - I could not find an > > > appropriate PyCountry discussion list and my next best bet seemed to > be a > > > Python users' list. > > > > > > For those who care to look, the error is as follows (a concise example > > > from an interactive shell: > > > > > > import pycountry > > > country = pycountry.countries.get(alpha2='DE') > > > currency = pycountry.currencies.get(numeric=country.numeric) > > > Traceback (most recent call last): > > > File "", line 1, in > > > File "/usr/lib/pymodules/python2.6/pycountry/db.py", line 83, in get > > > return self.indices[field][value] > > > KeyError: '276' > > > > > > The obvious issue here is that the pycountry.countries collection does > > not > > > contain a currency with a numeric of 276 (Germany's numeric) - yet it > > does > > > contain the Euro. Any ideas as to what the way around this may be? > > > > It looks like the development version of babel > > > > > > > http://babel.pocoo.org/docs/api/numbers/#babel.numbers.get_territory_currencies > > > > can do what you want: > > > > $ LANG=en_US.UTF-8 python > > Python 2.7.5+ (default, Feb 27 2014, 19:37:08) > > [GCC 4.8.1] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> import babel.numbers as bn > > >>> bn.get_territory_currencies("DE") > > ['EUR'] > > >>> print bn.format_currency(1.234, "EUR") > > ?1.23 > > >>> print bn.format_currency(1.234, "EUR", locale="DE") > > 1,23 ? > > >>> import babel > > >>> babel.__version__ > > '2.0-dev' > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > > > > > -- > Regards, > Sithu Lloyd Dube > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tutor/attachments/20140508/4c054982/attachment.html > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > End of Tutor Digest, Vol 123, Issue 27 > ************************************** > -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Fri May 9 10:54:21 2014 From: stefan_ml at behnel.de (Stefan Behnel) Date: Fri, 09 May 2014 10:54:21 +0200 Subject: [Tutor] preferred httprequest library In-Reply-To: References: Message-ID: Martin A. Brown, 09.05.2014 00:54: > : I?m new to python but not so much to programming. I need to > : construct a set or programs to test a forms poster that has been > : enhanced (it is in php). I mostly need http get and post. This > : is a hands on set of tests and does not have to be bullet proof. > : > : What would be a good http request library to use for this work? > > There are many options. When you can afford to suck the entire > remote resource into memory (as with many applications), you will > probably find the 'requests' library very handy. I'd start here, > avoiding grabbing for the standard library tools (urllib, and > urllib2) unless you need that finer control. > > This has a nice abstraction and, from your description, I think this > would be a good fit: > > http://docs.python-requests.org/en/latest/ Agreed that "requests" is a good external tool with a (mostly) nice interface, but if you really just need to do GET/POST, there's nothing wrong with the stdlib's urllib.request module (called urllib2 in Python 2). https://docs.python.org/3.4/library/urllib.request.html Basically, you just say result = urllib.request.urlopen(some_url) for a GET request and result = urllib.request.urlopen(some_url, data=post_data) for POST. It returns a file-like object that you can use to read the response, ask for headers, etc. Advantage is that it runs out of the box on whatever Python installation you have. If installing external packages before running the tests is not an issue, then the "requests" library will make your life a bit easier for the more involved cases that you might encounter at some point in the future. Stefan From bothenta at gmail.com Sat May 10 08:35:07 2014 From: bothenta at gmail.com (1 2) Date: Sat, 10 May 2014 14:35:07 +0800 Subject: [Tutor] HELP! How do I remove the black after "s=" Message-ID: In the result it shows "s= 8" pls tell me how to remove the blank? s,t,n = 0,0,1 while t <= s: s,t,n = s+2,t+n,n+1 else: print('s=',s,n) -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwpolska at gmail.com Sat May 10 10:13:54 2014 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Sat, 10 May 2014 10:13:54 +0200 Subject: [Tutor] HELP! How do I remove the black after "s=" In-Reply-To: References: Message-ID: On Sat, May 10, 2014 at 8:35 AM, 1 2 wrote: > In the result it shows "s= 8" pls tell me how to remove the blank? > > s,t,n = 0,0,1 > while t <= s: > s,t,n = s+2,t+n,n+1 > else: > print('s=',s,n) You must use something else. For example: print('s={0} {1}'.format(s, n)) This will produce "s=8 5". If you want "s=85", remove the space in the format string. -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From alan.gauld at btinternet.com Sat May 10 10:15:15 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 10 May 2014 09:15:15 +0100 Subject: [Tutor] HELP! How do I remove the black after "s=" In-Reply-To: References: Message-ID: On 10/05/14 07:35, 1 2 wrote: > In the result it shows "s= 8" pls tell me how to remove the blank? > > s,t,n = 0,0,1 > while t <= s: > s,t,n = s+2,t+n,n+1 > else: > print('s=',s,n) Assuming you are using Python version 3 you need to specify the sep option to print: print('s=',s,n,sep='') >>> help(print) Help on built-in function print in module builtins: print(...) print(value, ..., sep=' ', end='\n', file=sys.stdout) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. (END) hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From stefan_ml at behnel.de Sat May 10 10:57:18 2014 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 10 May 2014 10:57:18 +0200 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: Danny Yoo, 07.05.2014 22:39: > If you don't want to deal with a event-driven approach that SAX > emphasizes, you may still be able to do this problem with an XML-Pull > parser. You mention that your input is hundreds of megabytes long, in > which case you probably really do need to be careful about memory > consumption. See: > > https://wiki.python.org/moin/PullDom Since the OP mentioned that the file is quite large (800 MB), not only memory consumption should matter but also processing time. If that is the case, PullDOM isn't something to recommend since it's MiniDOM based, which makes it quite slow overall. Stefan From steve at pearwood.info Sat May 10 11:29:30 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 10 May 2014 19:29:30 +1000 Subject: [Tutor] HELP! How do I remove the black after "s=" In-Reply-To: References: Message-ID: <20140510092930.GI4273@ando> Hello "1 2", and welcome! (By the way, I feel quite silly calling you by the name you show in your email address. Do you have another name you would prefer to be known by?) My response below. On Sat, May 10, 2014 at 02:35:07PM +0800, 1 2 wrote: > In the result it shows "s= 8" pls tell me how to remove the blank? There are a few different ways. The easiest is to tell the print() function not to use a space as seperator between terms. (This only works in Python 3.) py> print("s=", 8, 9) s= 8 9 py> print("s=", 8, 9, sep='') s=89 But note that the separator applies between all the items, so that's not very useful in your case. Instead, you can build up a string, then print the string. Here are two different ways to do it, the first may be familiar with you if you know how to program in C. py> print("s=%s %s" % (8, 9)) s=8 9 py> print("s={} {}".format(8, 9)) s=8 9 Hope this helps. -- Steven From stefan_ml at behnel.de Sat May 10 11:52:10 2014 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 10 May 2014 11:52:10 +0200 Subject: [Tutor] xml parsing from xml In-Reply-To: References: Message-ID: Stefan Behnel, 10.05.2014 10:57: > Danny Yoo, 07.05.2014 22:39: >> If you don't want to deal with a event-driven approach that SAX >> emphasizes, you may still be able to do this problem with an XML-Pull >> parser. You mention that your input is hundreds of megabytes long, in >> which case you probably really do need to be careful about memory >> consumption. See: >> >> https://wiki.python.org/moin/PullDom > > Since the OP mentioned that the file is quite large (800 MB), not only > memory consumption should matter but also processing time. If that is the > case, PullDOM isn't something to recommend since it's MiniDOM based, which > makes it quite slow overall. To back that by some numbers, here are three memory efficient implementations, using PullDOM, cElementTree and lxml.etree: $ cat lx.py from lxml.etree import iterparse, tostring doc = iterparse('input.xml', tag='country') root = None for _, node in doc: print("--------------") print("This is the node for " + node.get('name')) print("--------------") print(tostring(node)) print("\n\n") if root is None: root = node.getparent() else: sib = node.getprevious() if sib is not None: root.remove(sib) $ cat et.py from xml.etree.cElementTree import iterparse, tostring doc = iterparse('input.xml') for _, node in doc: if node.tag == "country": print("--------------") print("This is the node for " + node.get('name')) print("--------------") print(tostring(node)) print("\n\n") node.clear() $ cat pdom.py from xml.dom.pulldom import START_ELEMENT, parse doc = parse('input.xml') for event, node in doc: if event == START_ELEMENT and node.localName == "country": doc.expandNode(node) print("--------------") print("This is the node for " + node.getAttribute('name')) print("--------------") print(node.toxml()) print("\n\n") I ran all three against a 400 MB XML file generated by repeating the data snippet the OP provided. Here are the system clock timings in minutes:seconds, on 64bit Linux, using CPython 3.4.0: $ time python3 lx.py > /dev/null time: 0:31 $ time python3 et.py > /dev/null time: 3:33 $ time python3 pdom.py > /dev/null time: 9:51 Adding to that another bit of actual tree processing, if I had to choose between 2 minutes and well over 20 minutes processing time for my 800MB, I'd tend to prefer the 2 minutes. Note that the reason why cElementTree performs so poorly here is that its serialiser is fairly slow, and the code writes the entire 400 MB of XML back out. If the test was more like "parse 400 MB and generate CSV from it", then it should perform similar to lxml. PullDOM/MiniDOM, on the other hand, are slow on parsing, serialisation *and* tree processing. Stefan From gchan401 at msn.com Sun May 11 05:16:53 2014 From: gchan401 at msn.com (Glen Chan) Date: Sat, 10 May 2014 23:16:53 -0400 Subject: [Tutor] Help with Python Message-ID: Hello, I am a student trying to figure out Python. I am getting errors that I don't know how to fix. What do you do after you get the error message and something is highlighted? Does that have to be deleted? Anyway, here is what I mean... #>>> The Dice Game #add libraries needed import random #the main function def main(): print #initialize variables playerOne = 'No Name' playerTwo = 'No Name' #call to inputNames playerOne, playerTwo = inputNames(playerOne, playerTwo) #while loop to run program again while endProgram == 'no': #initialize variables winnersName = 'NO NAME' p1number = 0 p2number = 0 #call to rollDice winnerName = rollDice(p1number, p2number, playerOne, playerTwo, winnerName) #call to displayInfo winnerName endProgram = raw_input('Do you want to end program? (Enter yes or no): ') #this function gets the players names def inputNames(playerOne, playerTwo): playerOne = raw_input("Enter Name") playerTwo = raw_input("Enter Name") return playerOne, playerTwo #this function will get the random values def rollDice(p1numer, p2numer, playerOne, playerTwo, winnerName): p1number = random.randint(1, 6) p1number = random.randint(1, 6) #this function displays the winner if p1number == p2number: winnerName = "TIE" elif p1number > p2number: winnerName = playerOne else: winnerName = playerTwo return winnerName # calls main main() -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Sun May 11 09:58:19 2014 From: illusiontechniques at gmail.com (C Smith) Date: Sun, 11 May 2014 03:58:19 -0400 Subject: [Tutor] Help with Python In-Reply-To: References: Message-ID: Hey Glen, include the error you are getting. It will make answering your question easier. How are you running this program, in an IDE? On Sat, May 10, 2014 at 11:16 PM, Glen Chan wrote: > Hello, I am a student trying to figure out Python. I am getting errors that > I don't know how to fix. What do you do after you get the error message and > something is highlighted? Does that have to be deleted? Anyway, here is what > I mean... > > > #>>> The Dice Game > #add libraries needed > import random > #the main function > def main(): > print > #initialize variables > playerOne = 'No Name' > playerTwo = 'No Name' > > #call to inputNames > playerOne, playerTwo = inputNames(playerOne, playerTwo) > #while loop to run program again > while endProgram == 'no': > #initialize variables > winnersName = 'NO NAME' > p1number = 0 > p2number = 0 > #call to rollDice > winnerName = rollDice(p1number, p2number, playerOne, playerTwo, > winnerName) > > #call to displayInfo > winnerName > endProgram = raw_input('Do you want to end program? (Enter yes or > no): ') > > > > #this function gets the players names > def inputNames(playerOne, playerTwo): > playerOne = raw_input("Enter Name") > playerTwo = raw_input("Enter Name") > > return playerOne, playerTwo > #this function will get the random values > def rollDice(p1numer, p2numer, playerOne, playerTwo, winnerName): > p1number = random.randint(1, 6) > p1number = random.randint(1, 6) > > #this function displays the winner > > if p1number == p2number: > winnerName = "TIE" > elif p1number > p2number: > winnerName = playerOne > else: > winnerName = playerTwo > return winnerName > > # calls main > main() > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at btinternet.com Sun May 11 11:20:26 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 11 May 2014 10:20:26 +0100 Subject: [Tutor] Help with Python In-Reply-To: References: Message-ID: On 11/05/14 04:16, Glen Chan wrote: > Hello, I am a student trying to figure out Python. I am getting errors > that I don't know how to fix. What do you do after you get the error > message and something is highlighted? Does that have to be deleted? The error doesn't need to be deleted because it appears in the interpreter, not in your code. However,... > #>>> The Dice Game Are you trying to type the whole program into the interactive Python shell? If so that's the wrong approach. The >>> prompt is only intended for entering a few lines to experiment and find out how things work. You should create a new text file and write the code into that. You can then run the file in the interpreter. If you are using an IDE (such as IDLE) that usually means you use the File->New menu and an editor window will appear. There will then be a Run menu somewhere (in IDLE its Run->Run Module) to run the program. If you are using IDLE take a look at Danny's tutorial here: http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/index.html As to your error please copy the en tire error message and send it to us, otherwise we have to guess what the problem is... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From d at davea.name Sun May 11 13:37:14 2014 From: d at davea.name (Dave Angel) Date: Sun, 11 May 2014 07:37:14 -0400 Subject: [Tutor] Help with Python In-Reply-To: References: Message-ID: <536F60EA.9030007@davea.name> On 05/10/2014 11:16 PM, Glen Chan wrote: > Hello, I am a student trying to figure out Python. I am getting errors that I don't know how to fix. What do you do after you get the error message and something is highlighted? Does that have to be deleted? Anyway, here is what I mean... > > > def main(): > print > #initialize variables > playerOne = 'No Name' > playerTwo = 'No Name' > > #call to inputNames > playerOne, playerTwo = inputNames(playerOne, playerTwo) > #while loop to run program again > while endProgram == 'no': > #initialize variables > winnersName = 'NO NAME' > p1number = 0 > p2number = 0 > #call to rollDice > winnerName = rollDice(p1number, p2number, playerOne, playerTwo, winnerName) > > #call to displayInfo > winnerName > endProgram = raw_input('Do you want to end program? (Enter yes or no): ') > When posting a question, you should always specify the Python version and OS, though it probably doesn't matter here. As others have said, please paste the exact error message. This is a text mailing list, so any highlighting you may have tried to include is lost on most of us. Please post in a text message, not html, as many things can go wrong in the re-interpretation, especially in any source code. I pasted your code into a text editor, saved the file, and ran it a terminal window in Python 2.7 under Linux, davea at think2:~/temppython$ python glen.py File "glen.py", line 25 endProgram = raw_input('Do you want to end program? (Enter yes or no): ') ^ IndentationError: unexpected indent davea at think2:~/temppython$ As you can see the callstack shows the line that has a problem, and shows s a specific error message. The problem is that you indented a line improperly. You only add to the indentation within a function, an if/else statement statement block, class, with clause, etc. Ordinary statemdents have to line up with the ones before, not counting comments. If you line up the endProgram line with the previous winnername line, this particular error will go away. But you have others. I notice you mix spaces and tabs for indentation, which is dangerously confusing. You should stick to one or the other, and I prefer spaces. I configured my text editor (emacs) to turn any tabs into 4 spaces, so that I won't get into trouble. in some places you only indent by one space. That's hard to read and you can wind up with problems from that. Better to stick with 4, though some people seem to prefer 2. Next problem is: davea at think2:~/temppython$ python glen.py File "glen.py", line 44 elif p1number > p2number: ^ IndentationError: unindent does not match any outer indentation level This error is because the elif clause does not line up with the if clause. But a bigger problem in that portion of code is that you're apparently starting a new function, but never define it. No def line follows the comment: #this function displays the winner If you add that def xxx(): line, and indent the if, then the elif will line up as expected. After fixing that, the next error is: Traceback (most recent call last): File "glen.py", line 51, in main() File "glen.py", line 15, in main while endProgram == 'no': UnboundLocalError: local variable 'endProgram' referenced before assignment That comes because you have a while statement that refers to the variable endProgram, which has not been yet bound to a value. You need endProgram = "no" before that if statement. In each of these cases, the error message tells you pretty closely what's wrong, and where. You will need to learn to read the error messages, the tracebacks. Your next problem is one of program logic, and I leave it to you. -- DaveA From illusiontechniques at gmail.com Mon May 12 02:12:04 2014 From: illusiontechniques at gmail.com (C Smith) Date: Sun, 11 May 2014 20:12:04 -0400 Subject: [Tutor] Real world experience Message-ID: I have never known anyone that works in this industry. I got one job transforming xml (should have used xslt, ended up using sed and python regex scripts) where the guy asked me how much I wanted and I threw 200 bucks out there because I could get a room for two weeks at that cost. He just laughed and handed me the money. That is the only professional experience I have and no formal education whatsoever (some high school). I have been doing online stuff and hit a wall in a 'design of computer programs' class on udacity. I made it about halfway through but started back at square one to sharpen my skills before trying to come at it again from a different angle. I started to feel overwhelmed when trying to basically recode regex (functions for '*' and '.' for instance) in python and make an api to interact easily with the text parser. I am still completely in the dark about what level of difficulty I would be facing in the professional world. If this is difficult at all for me, is there hope to think about making money in this field? I am pretty persistent and can keep up a level of work if I am not even close yet, but I don't know if I am a year off or 10 years off. Are basic scripting skills employable at even a very low rate (10 bucks an hour)? What level of experience are the people at who make 100k a year? Sorry if this is off-topic for the list, but I am trying to get past a psychological hurdle or two before reapplying myself and hopefully it would be valuable to others as well. From tdkrupinski at gmail.com Mon May 12 03:43:19 2014 From: tdkrupinski at gmail.com (Tim Krupinski) Date: Sun, 11 May 2014 20:43:19 -0500 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: Probably off-topic for the list but i'll let some of the others weigh in on that. This is more for help with the python language itself. But i'll weigh in. Programming is difficult work. It's definitely a profitable career. Its hard to say how much you'll make since it varies depending on location, but in general a combination of experience and your ability to solve difficult problems and provide solutions consistently command higher salaries. However, many companies wont even consider you without a degree, or *a significant *contribution to the industry. If you want to pursue a career in IT, you need to finish high school. You would be wise to get a degree. My $0.02. Tim On Sun, May 11, 2014 at 7:12 PM, C Smith wrote: > I have never known anyone that works in this industry. I got one job > transforming xml (should have used xslt, ended up using sed and python > regex scripts) where the guy asked me how much I wanted and I threw > 200 bucks out there because I could get a room for two weeks at that > cost. He just laughed and handed me the money. That is the only > professional experience I have and no formal education whatsoever > (some high school). I have been doing online stuff and hit a wall in a > 'design of computer programs' class on udacity. I made it about > halfway through but started back at square one to sharpen my skills > before trying to come at it again from a different angle. I started to > feel overwhelmed when trying to basically recode regex (functions for > '*' and '.' for instance) in python and make an api to interact easily > with the text parser. > > I am still completely in the dark about what level of difficulty I > would be facing in the professional world. > If this is difficult at all for me, is there hope to think about > making money in this field? > I am pretty persistent and can keep up a level of work if I am not > even close yet, but I don't know if I am a year off or 10 years off. > Are basic scripting skills employable at even a very low rate (10 > bucks an hour)? > What level of experience are the people at who make 100k a year? > Sorry if this is off-topic for the list, but I am trying to get past a > psychological hurdle or two before reapplying myself and hopefully it > would be valuable to others as well. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From illusiontechniques at gmail.com Mon May 12 04:10:55 2014 From: illusiontechniques at gmail.com (C Smith) Date: Sun, 11 May 2014 22:10:55 -0400 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: What is a difficult problem that if I could solve it would indicate I am ready to begin looking for a job? I realize that solving just ONE problem isn't too meaningful, but I am looking for a level of difficulty or some sort of gauge as to what a good programmer would consider difficult. What would a junior programmer for a company be expected to be able to do? This way I could have an idea of the range between a beginner (still a paid employee) and someone with many years of experience. I am in the south, USA. thanks On Sun, May 11, 2014 at 9:43 PM, Tim Krupinski wrote: > Probably off-topic for the list but i'll let some of the others weigh in on > that. This is more for help with the python language itself. > > But i'll weigh in. Programming is difficult work. It's definitely a > profitable career. Its hard to say how much you'll make since it varies > depending on location, but in general a combination of experience and your > ability to solve difficult problems and provide solutions consistently > command higher salaries. However, many companies wont even consider you > without a degree, or a significant contribution to the industry. > > If you want to pursue a career in IT, you need to finish high school. You > would be wise to get a degree. > > My $0.02. > > Tim > > > On Sun, May 11, 2014 at 7:12 PM, C Smith > wrote: >> >> I have never known anyone that works in this industry. I got one job >> transforming xml (should have used xslt, ended up using sed and python >> regex scripts) where the guy asked me how much I wanted and I threw >> 200 bucks out there because I could get a room for two weeks at that >> cost. He just laughed and handed me the money. That is the only >> professional experience I have and no formal education whatsoever >> (some high school). I have been doing online stuff and hit a wall in a >> 'design of computer programs' class on udacity. I made it about >> halfway through but started back at square one to sharpen my skills >> before trying to come at it again from a different angle. I started to >> feel overwhelmed when trying to basically recode regex (functions for >> '*' and '.' for instance) in python and make an api to interact easily >> with the text parser. >> >> I am still completely in the dark about what level of difficulty I >> would be facing in the professional world. >> If this is difficult at all for me, is there hope to think about >> making money in this field? >> I am pretty persistent and can keep up a level of work if I am not >> even close yet, but I don't know if I am a year off or 10 years off. >> Are basic scripting skills employable at even a very low rate (10 >> bucks an hour)? >> What level of experience are the people at who make 100k a year? >> Sorry if this is off-topic for the list, but I am trying to get past a >> psychological hurdle or two before reapplying myself and hopefully it >> would be valuable to others as well. >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor > > From syedamunazah at hotmail.com Sun May 11 17:50:15 2014 From: syedamunazah at hotmail.com (munazah zaidi) Date: Sun, 11 May 2014 20:50:15 +0500 Subject: [Tutor] (no subject) Message-ID: i want help -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon May 12 14:14:59 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 12 May 2014 13:14:59 +0100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 11/05/14 16:50, munazah zaidi wrote: > i want help 3 and -3 are square roots of 9. Did that help? If not, you will need to be a lot more specific about what kind of help you want. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Mon May 12 14:16:53 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 12 May 2014 22:16:53 +1000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <20140512121653.GL4273@ando> On Sun, May 11, 2014 at 08:50:15PM +0500, munazah zaidi wrote: > i want help Obviously. What would you like help about? We cannot read your mind. -- Steven From alan.gauld at btinternet.com Mon May 12 14:34:18 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 12 May 2014 13:34:18 +0100 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: On 12/05/14 01:12, C Smith wrote: > I have never known anyone that works in this industry. I got one job > transforming xml (should have used xslt, ended up using sed and python > regex scripts) where the guy asked me how much I wanted and I threw > 200 bucks out there because I could get a room for two weeks at that > cost. He just laughed and handed me the money. I'm sure he did. That would be a reasonable rate per day for contract IT staff. > I am still completely in the dark about what level of difficulty I > would be facing in the professional world. You would be expected to work on bigger projects that you have likely seen before (think 10,000 lines of code to upwards of 10 million lines.) You could be expected to work as pat of a team - anything from 4 to 400 other developers. That will involve following standardized practices, using standard notations and naming conventions and so on. You could be expected to work on things that, if they go wrong, could kill people - think aircraft control software, or railroad signalling systems - or bring the world finance system to its knees - think high speed stock trading systems... You might be expected to write comprehensive documentation - requirements, project plans, quality plans, architecture, design, test specifications, user guides, installation guides, tech support notes etc. on top of writing the code. > If this is difficult at all for me, is there hope to think about > making money in this field? Sure, there are lots of small web type companies building sites for other small businesses that churn out pretty standard online presence. Many of those folks are not University trained (although they probably have done a lot of coding) > What level of experience are the people at who make 100k a year? Usually experience of many different types of project from small 1-4 man teams up to very large 100+ teams and short (few days or weeks) to long term (several years). They probably also have management experience, looking after teams of programmers. They should also have some experience of dealing with non technical business types and of managing budgets. And of course a wide knowledge of the IT industry and its technologies (networks, databases, hardware interfaces...) as well as probably a specialised area of knowledge in business - banking, call centres, telecomms, gaming etc > Sorry if this is off-topic for the list Way off topic! :-) But probably of interest to a significant minority of the readers. Maybe you could look at newsgroups like comp.software-eng to see the kind of stuff pros discuss. The kinds of problems they are asking questions about. Also look at the job sites and read the job specs carefully. Ask how much of whats being asked could I do? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Mon May 12 14:43:38 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 12 May 2014 13:43:38 +0100 Subject: [Tutor] Please give a sensible subject (was (no subject)) In-Reply-To: References: Message-ID: On 11/05/2014 16:50, munazah zaidi wrote: > i want help > http://www.catb.org/esr/faqs/smart-questions.html http://www.sscce.org/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From jitu.icfai at gmail.com Mon May 12 16:08:21 2014 From: jitu.icfai at gmail.com (jitendra gupta) Date: Mon, 12 May 2014 19:38:21 +0530 Subject: [Tutor] Help with Python In-Reply-To: References: Message-ID: Hi This will solve your purpose: Yes we can write in better way also : ------------------------------ #The Dice Game #add libraries needed import random #the main function def main(): print #initialize variables playerOne = 'No Name' playerTwo = 'No Name' endProgram ="no" #call to inputNames playerOne, playerTwo = inputNames(playerOne, playerTwo) #while loop to run program again while endProgram == 'no': #call to rollDice winnersName = rollDice(playerOne, playerTwo) #call to displayInfo print "Winner is ", winnersName endProgram = raw_input('Do you want to end program? (Enter yes or no): ') #this function gets the players names def inputNames(playerOne, playerTwo): playerOne = raw_input("Enter Name") playerTwo = raw_input("Enter Name") return playerOne, playerTwo #this function will get the random values def rollDice(playerOne, playerTwo): p1number = random.randint(1, 6) p2number = random.randint(1, 6) #this function displays the winner if p1number == p2number: winnerName = "TIE" elif p1number > p2number: winnerName = playerOne else: winnerName = playerTwo return winnerName if __name__ == "__main__": # calls main main() On Sun, May 11, 2014 at 8:46 AM, Glen Chan wrote: > Hello, I am a student trying to figure out Python. I am getting errors > that I don't know how to fix. What do you do after you get the error > message and something is highlighted? Does that have to be deleted? Anyway, > here is what I mean... > > > #>>> The Dice Game > #add libraries needed > import random > #the main function > def main(): > print > #initialize variables > playerOne = 'No Name' > playerTwo = 'No Name' > > #call to inputNames > playerOne, playerTwo = inputNames(playerOne, playerTwo) > #while loop to run program again > while endProgram == 'no': > #initialize variables > winnersName = 'NO NAME' > p1number = 0 > p2number = 0 > #call to rollDice > winnerName = rollDice(p1number, p2number, playerOne, playerTwo, > winnerName) > > #call to displayInfo > winnerName > endProgram = raw_input('Do you want to end program? (Enter yes or > no): ') > > > > #this function gets the players names > def inputNames(playerOne, playerTwo): > playerOne = raw_input("Enter Name") > playerTwo = raw_input("Enter Name") > > return playerOne, playerTwo > #this function will get the random values > def rollDice(p1numer, p2numer, playerOne, playerTwo, winnerName): > p1number = random.randint(1, 6) > p1number = random.randint(1, 6) > > #this function displays the winner > > if p1number == p2number: > winnerName = "TIE" > elif p1number > p2number: > winnerName = playerOne > else: > winnerName = playerTwo > return winnerName > > # calls main > main() > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Mon May 12 19:47:06 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 12 May 2014 10:47:06 -0700 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: >> I have never known anyone that works in this industry. Just as a side comment: there are probably several folks on this mailing list whose job description would match "working in industry". > You could be expected to work as pat of a team - anything from 4 to 400 > other developers. That will involve following standardized practices, using > standard notations and naming conventions and so on. Seconded. Being able to work well with others is one of the biggest skills of the professional programmer. It's a matter of exposure and practice. That programming doesn't have to be a solitary thing needs to be strongly emphasized, because the media likes to exaggerate, especially for the sake of a sexy story: it seems really enamored to portray programming as some lone, antisocial activity. In industry, you work with other people: if the problems are small, they still involve interacting with others. Mailing lists like this one are one way to get some social interaction and experience. Programmer user groups are also a valuable way to get in contact with experienced folks. If you have an organization nearby, like Hacker Dojo, you may want to look into those kinds of things. http://www.hackerdojo.com/About I don't know where you're geographically located, but if you are close to Hacker Dojo, they're good people. From leamhall at gmail.com Mon May 12 20:08:36 2014 From: leamhall at gmail.com (leam hall) Date: Mon, 12 May 2014 14:08:36 -0400 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: On Mon, May 12, 2014 at 1:47 PM, Danny Yoo wrote: > >> I have never known anyone that works in this industry. > > Just as a side comment: there are probably several folks on this > mailing list whose job description would match "working in industry". > One big step is "program for someone else". Whatever you do scratches your own interests and itches. Once you start programming for someone else's changing requirements and lack of understanding of coding limitations you really find out if you "know" the language. Leam -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon May 12 22:55:45 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 12 May 2014 21:55:45 +0100 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: On 12/05/14 18:47, Danny Yoo wrote: > practice. That programming doesn't have to be a solitary thing needs > to be strongly emphasized, because the media likes to exaggerate, Yes, This can't be stressed too much. Industrial coding is a team activity not a solo process. In fact one well known and respected management book(*) had as a maxiom - "Beware of the man in a darkened room". What it meant was be suspicious of the lone wolf coder who doesn't tell you what he'sd doing, where he's at etc. His code will likely not work with any one else's (and he'll blame them of course!) and it will be late because its always 95% complete... There are a few lone shark geniuses out there, I've met maybe 2 in my 40 years in IT. But 99.9% of coders work best in a team. (*)Software Project Survival Guide by Steve McConnell (also author of Code Complete etc) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From illusiontechniques at gmail.com Tue May 13 00:53:39 2014 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 12 May 2014 18:53:39 -0400 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: Thanks to everyone. >> practice. That programming doesn't have to be a solitary thing needs >> to be strongly emphasized, because the media likes to exaggerate, >Yes, This can't be stressed too much. Industrial coding is a team activity not a solo process. This is particularly good advice for me. Although I realized that a big project would involve many people, I was thinking of myself as "coding alone in dark room " without being able to go over ideas with a team. I would probably get less frustrated that way. >One big step is "program for someone else" This is something I need to practice. >Just as a side comment: there are probably several folks on this >mailing list whose job description would match "working in industry". I was thinking IRL. Thanks for the sense of community though. >I don't know where you're geographically located, but if you are close >to Hacker Dojo, they're good people. That looks pretty amazing. I am in Atlanta, but I may take a bus out there just to check it out. I lived in LA for a little while and venice beach, santa monica, and the desert-y hills around mulholland drive were beautiful. >or a significant contribution to the industry. I will keep this in mind. >involve following standardized practices, using standard notations and naming conventions and so on. This is another very relevant thing for me to keep in mind. My variables are usually interesting animals or spells from final fantasy 1. Thanks On Mon, May 12, 2014 at 4:55 PM, Alan Gauld wrote: > On 12/05/14 18:47, Danny Yoo wrote: > >> practice. That programming doesn't have to be a solitary thing needs >> to be strongly emphasized, because the media likes to exaggerate, > > > Yes, This can't be stressed too much. Industrial coding is a team activity > not a solo process. > > In fact one well known and respected management book(*) had as a maxiom - > "Beware of the man in a darkened room". What it meant was be suspicious of > the lone wolf coder who doesn't tell you what he'sd doing, where he's at > etc. > His code will likely not work with any one else's (and he'll blame > them of course!) and it will be late because its always 95% > complete... > > There are a few lone shark geniuses out there, I've met maybe 2 in > my 40 years in IT. But 99.9% of coders work best in a team. > > > (*)Software Project Survival Guide by Steve McConnell > (also author of Code Complete etc) > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From kirands at yahoo.com Mon May 12 23:44:14 2014 From: kirands at yahoo.com (KIRAN D.S.) Date: Mon, 12 May 2014 14:44:14 -0700 (PDT) Subject: [Tutor] Finding Hostname-IP DNS mappings and if machine is dead or alive Message-ID: <1399931054.5993.YahooMailNeo@web140506.mail.bf1.yahoo.com> Hi, I have a UNIX shell script that: a. ?lists out the Hostname-IP DNS mappings b. ?checks whether the machine is pingable, and prints "dead" or "alive" depending on status. I want to write this program in Python but am struggling. ?Can someone help please ? Here's my script and output run =============================== (I have changed my company name to "company", and machine names for privacy reasons) pqr49: / > pqr49: / > cat 72-network-IP-DNS-checker #!/bin/bash i=1 while [ $i -lt 255 ]; do ? hostip=10.145.72.$i ? echo -n "$hostip " ? hn=None ? host $hostip > /dev/null 2>&1 ? if [ "$?" -ne 0 ]; then ? ? echo -n NoDNS ? ?else ? ? echo -n DNS ? ? hn=`host $hostip | awk '{print $NF}'` ? fi ? ping $hostip 1 > /dev/null 2>&1 ? if [ "$?" -eq 0 ]; then ? ? echo -n " alive " ? ?else ? ? echo -n " dead " ? fi ? echo $hn ? i=`expr $i + 1` done exit pqr49: / > pqr49: / > ./72-network-IP-DNS-checker 10.145.72.1 DNS alive piedmont22-1257-rtr-1-v3007.us.company.com. 10.145.72.2 NoDNS dead None 10.145.72.3 NoDNS dead None 10.145.72.4 NoDNS dead None 10.145.72.5 NoDNS alive None 10.145.72.6 DNS dead golf9999-nwk1k.us.company.com. 10.145.72.7 DNS alive golf9999-nwk2a.us.company.com. 10.145.72.8 DNS alive golf9999-nwk2c.us.company.com. 10.145.72.9 NoDNS dead None 10.145.72.10 DNS alive pqr585.us.company.com. 10.145.72.11 DNS alive pqr585-sp.us.company.com. 10.145.72.12 DNS alive pqr585-2540-1.us.company.com. 10.145.72.13 DNS alive pqr585-2540-2.us.company.com. 10.145.72.14 DNS alive xyz-efgh101.us.company.com. 10.145.72.15 DNS alive xyz-efgh101-pqrst.us.company.com. 10.145.72.16 DNS alive xyz-efgh102.us.company.com. 10.145.72.17 DNS alive xyz-efgh102-pqrst.us.company.com. 10.145.72.18 DNS alive xyz-def-ib102.us.company.com. 10.145.72.19 DNS dead pqr294-sp.us.company.com. 10.145.72.20 DNS alive pqr582.us.company.com. 10.145.72.21 DNS alive pqr582-sp.us.company.com. 10.145.72.22 DNS alive pqr580.us.company.com. 10.145.72.23 DNS alive pqr580-sp.us.company.com. 10.145.72.24 DNS alive pqr550.us.company.com. 10.145.72.25 DNS dead pqr551.us.company.com. 10.145.72.26 DNS alive pqr551-sp.us.company.com. 10.145.72.27 DNS^C pqr49: / > pqr49: / > Thank you and regards, Kiran -------------- next part -------------- An HTML attachment was scrubbed... URL: From cacreman at austin.rr.com Tue May 13 01:10:54 2014 From: cacreman at austin.rr.com (Chris Acreman) Date: Mon, 12 May 2014 18:10:54 -0500 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: From: munazah zaidi Sent: Sunday, May 11, 2014 10:50 AM To: tutor at python.org Subject: [Tutor] (no subject) i want help Could you be a little more vague? -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Tue May 13 01:26:35 2014 From: emile at fenx.com (Emile van Sebille) Date: Mon, 12 May 2014 16:26:35 -0700 Subject: [Tutor] Finding Hostname-IP DNS mappings and if machine is dead or alive In-Reply-To: <1399931054.5993.YahooMailNeo@web140506.mail.bf1.yahoo.com> References: <1399931054.5993.YahooMailNeo@web140506.mail.bf1.yahoo.com> Message-ID: How far have you gotten? Post your code to show us where you're at and we can point you in the right direction. Emile On 5/12/2014 2:44 PM, KIRAN D.S. wrote: > Hi, > > I have a UNIX shell script that: > a. lists out the Hostname-IP DNS mappings > b. checks whether the machine is pingable, and prints "dead" or "alive" > depending on status. > > I want to write this program in Python but am struggling. Can someone > help please ? > > Here's my script and output run > =============================== > (I have changed my company name to "company", and machine names for > privacy reasons) > > pqr49: / > > pqr49: / > cat 72-network-IP-DNS-checker > #!/bin/bash > i=1 > while [ $i -lt 255 ]; do > hostip=10.145.72.$i > echo -n "$hostip " > hn=None > host $hostip > /dev/null 2>&1 > if [ "$?" -ne 0 ]; then > echo -n NoDNS > else > echo -n DNS > hn=`host $hostip | awk '{print $NF}'` > fi > ping $hostip 1 > /dev/null 2>&1 > if [ "$?" -eq 0 ]; then > echo -n " alive " > else > echo -n " dead " > fi > echo $hn > i=`expr $i + 1` > done > exit > pqr49: / > > pqr49: / > ./72-network-IP-DNS-checker > 10.145.72.1 DNS alive piedmont22-1257-rtr-1-v3007.us.company.com. > 10.145.72.2 NoDNS dead None > 10.145.72.3 NoDNS dead None > 10.145.72.4 NoDNS dead None > 10.145.72.5 NoDNS alive None > 10.145.72.6 DNS dead golf9999-nwk1k.us.company.com. > 10.145.72.7 DNS alive golf9999-nwk2a.us.company.com. > 10.145.72.8 DNS alive golf9999-nwk2c.us.company.com. > 10.145.72.9 NoDNS dead None > 10.145.72.10 DNS alive pqr585.us.company.com. > 10.145.72.11 DNS alive pqr585-sp.us.company.com. > 10.145.72.12 DNS alive pqr585-2540-1.us.company.com. > 10.145.72.13 DNS alive pqr585-2540-2.us.company.com. > 10.145.72.14 DNS alive xyz-efgh101.us.company.com. > 10.145.72.15 DNS alive xyz-efgh101-pqrst.us.company.com. > 10.145.72.16 DNS alive xyz-efgh102.us.company.com. > 10.145.72.17 DNS alive xyz-efgh102-pqrst.us.company.com. > 10.145.72.18 DNS alive xyz-def-ib102.us.company.com. > 10.145.72.19 DNS dead pqr294-sp.us.company.com. > 10.145.72.20 DNS alive pqr582.us.company.com. > 10.145.72.21 DNS alive pqr582-sp.us.company.com. > 10.145.72.22 DNS alive pqr580.us.company.com. > 10.145.72.23 DNS alive pqr580-sp.us.company.com. > 10.145.72.24 DNS alive pqr550.us.company.com. > 10.145.72.25 DNS dead pqr551.us.company.com. > 10.145.72.26 DNS alive pqr551-sp.us.company.com. > 10.145.72.27 DNS^C > pqr49: / > > pqr49: / > > > > Thank you and regards, > Kiran > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From dyoo at hashcollision.org Tue May 13 01:36:17 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 12 May 2014 16:36:17 -0700 Subject: [Tutor] Finding Hostname-IP DNS mappings and if machine is dead or alive In-Reply-To: <1399931054.5993.YahooMailNeo@web140506.mail.bf1.yahoo.com> References: <1399931054.5993.YahooMailNeo@web140506.mail.bf1.yahoo.com> Message-ID: On Mon, May 12, 2014 at 2:44 PM, KIRAN D.S. wrote: > Hi, > > I have a UNIX shell script that: > a. lists out the Hostname-IP DNS mappings > b. checks whether the machine is pingable, and prints "dead" or "alive" > depending on status. There will be a few things you'll want to look at. In particular, if you can avoid calling out to the shell, that would be nice. To get the host name given its IP address, you can use socket.gethostbyaddr(), which does that lookup for you: https://docs.python.org/2/library/socket.html#socket.gethostbyaddr http://stackoverflow.com/questions/2575760/python-lookup-hostname-from-ip-with-1-second-timeout To check dead-or-liveness of the site, you could shell out to ping. See the subprocess library for details on how to do that: https://docs.python.org/2/library/subprocess.html But alternatively, there may be native ping libraries out there for Python that you can reuse. A Google search for the term "Python Ping" hits a few pure-Python ping libraries that people have written. For command line processing, look into argparse: https://docs.python.org/2/howto/argparse.html#id1 If you can say more of where you're getting stuck, we can give more specific advice. From dyoo at hashcollision.org Tue May 13 01:42:18 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 12 May 2014 16:42:18 -0700 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: >>I don't know where you're geographically located, but if you are close >>to Hacker Dojo, they're good people. > > That looks pretty amazing. I am in Atlanta, but I may take a bus out > there just to check it out. I lived in LA for a little while and > venice beach, santa monica, and the desert-y hills around mulholland > drive were beautiful. Scanning Atlanta.... ... ... I do not know what the status of this is in real life, since I don't live in Atlanta, but you might want to check on: https://wiki.freesideatlanta.org/fs/Info Alternatively, check your local meetups in the area. For example: http://www.meetup.com/Geekspace-Gwinnett/ http://www.meetup.com/python-atlanta/ There are probably many others out there in your local area. Start searching. :P Good luck to you! From illusiontechniques at gmail.com Tue May 13 01:47:54 2014 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 12 May 2014 19:47:54 -0400 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: Freeside is more makers. I haven't gone but have known people that have. You might find some arduino supposedly, but not much coding otherwise and you have to pay membership fees. It is more social than technical, I think. And your car will probably be broken into. I will check out the python-atlanta group though, thanks. On Mon, May 12, 2014 at 7:42 PM, Danny Yoo wrote: >>>I don't know where you're geographically located, but if you are close >>>to Hacker Dojo, they're good people. >> >> That looks pretty amazing. I am in Atlanta, but I may take a bus out >> there just to check it out. I lived in LA for a little while and >> venice beach, santa monica, and the desert-y hills around mulholland >> drive were beautiful. > > Scanning Atlanta.... > > ... > ... > > I do not know what the status of this is in real life, since I don't > live in Atlanta, but you might want to check on: > > https://wiki.freesideatlanta.org/fs/Info > > > Alternatively, check your local meetups in the area. For example: > > http://www.meetup.com/Geekspace-Gwinnett/ > > http://www.meetup.com/python-atlanta/ > > > There are probably many others out there in your local area. Start > searching. :P > > > Good luck to you! From martin at linux-ip.net Tue May 13 02:25:29 2014 From: martin at linux-ip.net (Martin A. Brown) Date: Mon, 12 May 2014 20:25:29 -0400 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: Hello, 10 Pick one favorite specific topic, any topic (XML parsing; Unix process handling; databases). The topic matters for you. Learn it deeply. Keep learning it. The topic matters less for others (unless it is specifically within the computer science discipline). You have just begun. [0] 20 Pick a toolkit (minidom, etree; os, multiprocessing, threading, subprocess; sqlite, psycopg, sqlalchemy, mysql). Learn why people solved the problem differently each time. Ask. 30 Write a program that does something interesting with this. Try a different toolkit. What was hard? What was easy? What could you simply not accomplish? Look at the source code for the tool you used? Why couldn't you accomplish what you wanted? 40 Read all of the documentation. Read it again. Read papers and books listed in the documentation footnotes. Read the documentation again. Realize that all of this is a (fallible) human endeavor. 45 Find other, related mailing lists. Subscribe. Listen. Post. 46 Talk to somebody who has solved the problem. How? What tools did that person use [1]? 48 If reference to something that was new or you did not understand, GOTO 40. 50 Write a brand new program to solve the same problem. Examine what you did differently. Ask somebody to review your code. 52 Read your old code. 53 Talk to somebody who has solved the problem in his/her own way, potentially with different tools. [2] 59 If MASTERY and BORED, GOTO 10. 60 GOTO 20 [3] This discipline can be approached in depth-first or breadth-first traversal pattern. Most people on more technical mailing lists appreciate the depth-first traversal. Time waits for nobody (Oh! I need to go eat!), -Martin [0] For these purposes, mine was IP networking. [1] What!?! Not Python?! Why? There are reasons to choose something else. Do not be blind to those resaons. [2] Find people who are motivated as you are and are working on similar problems. Work for them. Keep reading. Hire them. Keep writing. Keep reading. [3] Oops. I learned on BASIC. I hope I do not get banned from the list. -- Martin A. Brown http://linux-ip.net/ From akleider at sonic.net Tue May 13 02:30:44 2014 From: akleider at sonic.net (Alex Kleider) Date: Mon, 12 May 2014 17:30:44 -0700 Subject: [Tutor] Finding Hostname-IP DNS mappings and if machine is dead or alive In-Reply-To: References: <1399931054.5993.YahooMailNeo@web140506.mail.bf1.yahoo.com> Message-ID: On 2014-05-12 16:36, Danny Yoo wrote: > For command line processing, look into argparse: > > https://docs.python.org/2/howto/argparse.html#id1 Although the above is the standard library recommended solution, I would recommend you try docopt instead. http://docopt.org/ https://github.com/docopt/docopt From illusiontechniques at gmail.com Tue May 13 02:36:23 2014 From: illusiontechniques at gmail.com (C Smith) Date: Mon, 12 May 2014 20:36:23 -0400 Subject: [Tutor] Real world experience In-Reply-To: References: Message-ID: I think that is going to be my new wallpaper. On Mon, May 12, 2014 at 8:25 PM, Martin A. Brown wrote: > > Hello, > > 10 Pick one favorite specific topic, any topic (XML parsing; Unix > process handling; databases). The topic matters for you. > Learn it deeply. Keep learning it. The topic matters less for > others (unless it is specifically within the computer science > discipline). You have just begun. [0] > > 20 Pick a toolkit (minidom, etree; os, multiprocessing, threading, > subprocess; sqlite, psycopg, sqlalchemy, mysql). Learn why > people solved the problem differently each time. Ask. > > 30 Write a program that does something interesting with this. > Try a different toolkit. What was hard? What was easy? > What could you simply not accomplish? Look at the source code > for the tool you used? Why couldn't you accomplish what you > wanted? > > 40 Read all of the documentation. Read it again. Read papers and > books listed in the documentation footnotes. Read the > documentation again. Realize that all of this is a (fallible) > human endeavor. > > 45 Find other, related mailing lists. Subscribe. Listen. Post. > > 46 Talk to somebody who has solved the problem. How? What tools > did that person use [1]? > > 48 If reference to something that was new or you did not > understand, GOTO 40. > > 50 Write a brand new program to solve the same problem. Examine > what you did differently. Ask somebody to review your code. > > 52 Read your old code. > > 53 Talk to somebody who has solved the problem in his/her own > way, potentially with different tools. [2] > > 59 If MASTERY and BORED, GOTO 10. > > 60 GOTO 20 [3] > > This discipline can be approached in depth-first or breadth-first > traversal pattern. Most people on more technical mailing lists > appreciate the depth-first traversal. > > Time waits for nobody (Oh! I need to go eat!), > > -Martin > > [0] For these purposes, mine was IP networking. > [1] What!?! Not Python?! Why? There are reasons to choose > something else. Do not be blind to those resaons. > [2] Find people who are motivated as you are and are working on > similar problems. Work for them. Keep reading. Hire them. > Keep writing. Keep reading. > [3] Oops. I learned on BASIC. I hope I do not get banned from > the list. > > -- > Martin A. Brown > http://linux-ip.net/ From northriptl at s.dcsdk12.org Tue May 13 06:18:11 2014 From: northriptl at s.dcsdk12.org (student Tyler Northrip) Date: Mon, 12 May 2014 22:18:11 -0600 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: Cant say i was disappointed to see this in my inbox. For help, my suggestion would be to head over to google.com On Mon, May 12, 2014 at 5:10 PM, Chris Acreman wrote: > > > *From:* munazah zaidi > *Sent:* Sunday, May 11, 2014 10:50 AM > *To:* tutor at python.org > *Subject:* [Tutor] (no subject) > > i want help > > > Could you be a little more vague? > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From samball72 at hotmail.com Wed May 14 11:45:07 2014 From: samball72 at hotmail.com (Sam Ball) Date: Wed, 14 May 2014 20:15:07 +1030 Subject: [Tutor] While Loop? Message-ID: I'm attempting to create a program where the user inputs their account number (which must be 8 digits) and if what the user enters is not 8 digits in length I want python to tell the user this is invalid and then keep asking for the account number until a suitable number has been entered. I'm guessing it's a sort of while loop but I'm not 100% sure as I'm quite new to Python. So far I have... userAccountNumber = eval(input("Please Input Your Account Number:")) while userAccountNumber > 100000000 or <=9999999: print userAccountNumber ("Invalid Account Number! Account Must Be Eight Digits") When I run this however it gives me an invalid syntax and highlights the = in <=9999999: Would appreciate any help with sorting this out. Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From zxl_tao at 126.com Wed May 14 11:42:42 2014 From: zxl_tao at 126.com (Tao Zhu) Date: Wed, 14 May 2014 17:42:42 +0800 (CST) Subject: [Tutor] for the error about 'import site' failed; use -v for traceback Message-ID: <32cd7f7c.d758.145fa1ccb02.Coremail.zxl_tao@126.com> Hi everyone, when I use python, the problem occured. when I used the command "python -v", the results are listed as follows. could you tell me what wrong? $ python -v # installing zipimport hook import zipimport # builtin # installed zipimport hook # /usr/local/lib/python2.4/site.pyc matches /usr/local/lib/python2.4/site.py import site # precompiled from /usr/local/lib/python2.4/site.pyc # /usr/local/lib/python2.4/os.pyc matches /usr/local/lib/python2.4/os.py import os # precompiled from /usr/local/lib/python2.4/os.pyc import posix # builtin # /usr/local/lib/python2.4/posixpath.pyc matches /usr/local/lib/python2.4/posixpath.py import posixpath # precompiled from /usr/local/lib/python2.4/posixpath.pyc # /usr/local/lib/python2.4/stat.pyc matches /usr/local/lib/python2.4/stat.py import stat # precompiled from /usr/local/lib/python2.4/stat.pyc # /usr/local/lib/python2.4/UserDict.pyc matches /usr/local/lib/python2.4/UserDict.py import UserDict # precompiled from /usr/local/lib/python2.4/UserDict.pyc # /usr/local/lib/python2.4/copy_reg.pyc matches /usr/local/lib/python2.4/copy_reg.py import copy_reg # precompiled from /usr/local/lib/python2.4/copy_reg.pyc # /usr/local/lib/python2.4/types.pyc matches /usr/local/lib/python2.4/types.py import types # precompiled from /usr/local/lib/python2.4/types.pyc # /usr/local/lib/python2.4/warnings.pyc matches /usr/local/lib/python2.4/warnings.py import warnings # precompiled from /usr/local/lib/python2.4/warnings.pyc # /usr/local/lib/python2.4/linecache.pyc matches /usr/local/lib/python2.4/linecache.py import linecache # precompiled from /usr/local/lib/python2.4/linecache.pyc import encodings # directory /usr/local/lib/python2.4/encodings # /usr/local/lib/python2.4/encodings/__init__.pyc matches /usr/local/lib/python2.4/encodings/__init__.py import encodings # precompiled from /usr/local/lib/python2.4/encodings/__init__.pyc # /usr/local/lib/python2.4/codecs.pyc matches /usr/local/lib/python2.4/codecs.py import codecs # precompiled from /usr/local/lib/python2.4/codecs.pyc import _codecs # builtin # /usr/local/lib/python2.4/encodings/aliases.pyc matches /usr/local/lib/python2.4/encodings/aliases.py import encodings.aliases # precompiled from /usr/local/lib/python2.4/encodings/aliases.pyc # /usr/local/lib/python2.4/encodings/utf_8.pyc matches /usr/local/lib/python2.4/encodings/utf_8.py import encodings.utf_8 # precompiled from /usr/local/lib/python2.4/encodings/utf_8.pyc Python 2.4.4 (#3, May 15 2014, 00:22:35) [GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. dlopen("/usr/local/lib/python2.4/lib-dynload/readline.so", 2); import readline # dynamically loaded from /usr/local/lib/python2.4/lib-dynload/readline.so >>> Tao -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed May 14 18:14:35 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 14 May 2014 17:14:35 +0100 Subject: [Tutor] While Loop? In-Reply-To: References: Message-ID: On 14/05/14 10:45, Sam Ball wrote: > I'm attempting to create a program where the user inputs their account > number (which must be 8 digits) and if what the user enters is not 8 > digits in length I want python to tell the user this is invalid and then > keep asking for the account number until a suitable number has been entered. > I'm guessing it's a sort of while loop You guess correct > userAccountNumber = eval(input("Please Input Your Account Number:")) But please don;t do this. it is a huge security hole since whatever your user enters will be executed as Python code - thats what eval() does. Your user can ytype anything in there. The correct approach is to use raw_input (since you seem to be using Pthon v2) and a conversion function such as int() to get the data type that you want. > while userAccountNumber > 100000000 or <=9999999: Python sees this as: while (userAccountNumber > 100000000) or <=9999999: Which makes no sense. In most programming languages you test this like this: (userAccountNumber > 100000000) or (userAccountNumber <= 9999999): But in Python you can do these compound checks more succinctly like this: > while not (9999999 < userAccountNumber < 100000000): And the other way is to check the length of the input at source: userAccount = '' while len(userAccount) != 8: userAccount = raw_input("Please Input Your Account Number:") userAccountNumber = int(userAccount) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Wed May 14 18:16:10 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 14 May 2014 09:16:10 -0700 Subject: [Tutor] While Loop? In-Reply-To: References: Message-ID: On Wed, May 14, 2014 at 2:45 AM, Sam Ball wrote: > I'm attempting to create a program where the user inputs their account > number (which must be 8 digits) and if what the user enters is not 8 digits > in length I want python to tell the user this is invalid and then keep > asking for the account number until a suitable number has been entered. > I'm guessing it's a sort of while loop but I'm not 100% sure as I'm quite > new to Python. > > So far I have... > > > > userAccountNumber = eval(input("Please Input Your Account Number:")) Don't use eval here. If you want to turn a string that's full of digits into a number, use int() https://docs.python.org/3/library/functions.html#int eval() is dangerous. Don't use it. > while userAccountNumber > 100000000 or <=9999999: > print userAccountNumber ("Invalid Account Number! Account Must Be Eight > Digits") > > When I run this however it gives me an invalid syntax and highlights the = > in <=9999999: > > Would appreciate any help with sorting this out. The compiler is technically correct: it's invalid syntax. You've written something that scans as English. But it does not scan as a Python program. "or" does not have the English meaning in Python. To put it another way: what are you comparing to be less than or equal to 99999999? You've left that implicit. Many programming languages do not allow implicit statements because it's too easy to misunderstand or misinterpret. Make what you're comparing explicit. From illusiontechniques at gmail.com Wed May 14 18:24:04 2014 From: illusiontechniques at gmail.com (C Smith) Date: Wed, 14 May 2014 12:24:04 -0400 Subject: [Tutor] for the error about 'import site' failed; use -v for traceback In-Reply-To: <32cd7f7c.d758.145fa1ccb02.Coremail.zxl_tao@126.com> References: <32cd7f7c.d758.145fa1ccb02.Coremail.zxl_tao@126.com> Message-ID: That looks pretty normal. I don't see any errors. On Wed, May 14, 2014 at 5:42 AM, Tao Zhu wrote: > Hi everyone, > when I use python, the problem occured. when I used the command "python -v", > the results are listed as follows. could you tell me what wrong? > > $ python -v > # installing zipimport hook > import zipimport # builtin > # installed zipimport hook > # /usr/local/lib/python2.4/site.pyc matches /usr/local/lib/python2.4/site.py > import site # precompiled from /usr/local/lib/python2.4/site.pyc > # /usr/local/lib/python2.4/os.pyc matches /usr/local/lib/python2.4/os.py > import os # precompiled from /usr/local/lib/python2.4/os.pyc > import posix # builtin > # /usr/local/lib/python2.4/posixpath.pyc matches > /usr/local/lib/python2.4/posixpath.py > import posixpath # precompiled from /usr/local/lib/python2.4/posixpath.pyc > # /usr/local/lib/python2.4/stat.pyc matches /usr/local/lib/python2.4/stat.py > import stat # precompiled from /usr/local/lib/python2.4/stat.pyc > # /usr/local/lib/python2.4/UserDict.pyc matches > /usr/local/lib/python2.4/UserDict.py > import UserDict # precompiled from /usr/local/lib/python2.4/UserDict.pyc > # /usr/local/lib/python2.4/copy_reg.pyc matches > /usr/local/lib/python2.4/copy_reg.py > import copy_reg # precompiled from /usr/local/lib/python2.4/copy_reg.pyc > # /usr/local/lib/python2.4/types.pyc matches > /usr/local/lib/python2.4/types.py > import types # precompiled from /usr/local/lib/python2.4/types.pyc > # /usr/local/lib/python2.4/warnings.pyc matches > /usr/local/lib/python2.4/warnings.py > import warnings # precompiled from /usr/local/lib/python2.4/warnings.pyc > # /usr/local/lib/python2.4/linecache.pyc matches > /usr/local/lib/python2.4/linecache.py > import linecache # precompiled from /usr/local/lib/python2.4/linecache.pyc > import encodings # directory /usr/local/lib/python2.4/encodings > # /usr/local/lib/python2.4/encodings/__init__.pyc matches > /usr/local/lib/python2.4/encodings/__init__.py > import encodings # precompiled from > /usr/local/lib/python2.4/encodings/__init__.pyc > # /usr/local/lib/python2.4/codecs.pyc matches > /usr/local/lib/python2.4/codecs.py > import codecs # precompiled from /usr/local/lib/python2.4/codecs.pyc > import _codecs # builtin > # /usr/local/lib/python2.4/encodings/aliases.pyc matches > /usr/local/lib/python2.4/encodings/aliases.py > import encodings.aliases # precompiled from > /usr/local/lib/python2.4/encodings/aliases.pyc > # /usr/local/lib/python2.4/encodings/utf_8.pyc matches > /usr/local/lib/python2.4/encodings/utf_8.py > import encodings.utf_8 # precompiled from > /usr/local/lib/python2.4/encodings/utf_8.pyc > Python 2.4.4 (#3, May 15 2014, 00:22:35) > [GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > dlopen("/usr/local/lib/python2.4/lib-dynload/readline.so", 2); > import readline # dynamically loaded from > /usr/local/lib/python2.4/lib-dynload/readline.so >>>> > Tao > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From illusiontechniques at gmail.com Wed May 14 18:28:34 2014 From: illusiontechniques at gmail.com (C Smith) Date: Wed, 14 May 2014 12:28:34 -0400 Subject: [Tutor] for the error about 'import site' failed; use -v for traceback In-Reply-To: References: <32cd7f7c.d758.145fa1ccb02.Coremail.zxl_tao@126.com> Message-ID: What are you trying to do? On Wed, May 14, 2014 at 12:24 PM, C Smith wrote: > That looks pretty normal. I don't see any errors. > > On Wed, May 14, 2014 at 5:42 AM, Tao Zhu wrote: >> Hi everyone, >> when I use python, the problem occured. when I used the command "python -v", >> the results are listed as follows. could you tell me what wrong? >> >> $ python -v >> # installing zipimport hook >> import zipimport # builtin >> # installed zipimport hook >> # /usr/local/lib/python2.4/site.pyc matches /usr/local/lib/python2.4/site.py >> import site # precompiled from /usr/local/lib/python2.4/site.pyc >> # /usr/local/lib/python2.4/os.pyc matches /usr/local/lib/python2.4/os.py >> import os # precompiled from /usr/local/lib/python2.4/os.pyc >> import posix # builtin >> # /usr/local/lib/python2.4/posixpath.pyc matches >> /usr/local/lib/python2.4/posixpath.py >> import posixpath # precompiled from /usr/local/lib/python2.4/posixpath.pyc >> # /usr/local/lib/python2.4/stat.pyc matches /usr/local/lib/python2.4/stat.py >> import stat # precompiled from /usr/local/lib/python2.4/stat.pyc >> # /usr/local/lib/python2.4/UserDict.pyc matches >> /usr/local/lib/python2.4/UserDict.py >> import UserDict # precompiled from /usr/local/lib/python2.4/UserDict.pyc >> # /usr/local/lib/python2.4/copy_reg.pyc matches >> /usr/local/lib/python2.4/copy_reg.py >> import copy_reg # precompiled from /usr/local/lib/python2.4/copy_reg.pyc >> # /usr/local/lib/python2.4/types.pyc matches >> /usr/local/lib/python2.4/types.py >> import types # precompiled from /usr/local/lib/python2.4/types.pyc >> # /usr/local/lib/python2.4/warnings.pyc matches >> /usr/local/lib/python2.4/warnings.py >> import warnings # precompiled from /usr/local/lib/python2.4/warnings.pyc >> # /usr/local/lib/python2.4/linecache.pyc matches >> /usr/local/lib/python2.4/linecache.py >> import linecache # precompiled from /usr/local/lib/python2.4/linecache.pyc >> import encodings # directory /usr/local/lib/python2.4/encodings >> # /usr/local/lib/python2.4/encodings/__init__.pyc matches >> /usr/local/lib/python2.4/encodings/__init__.py >> import encodings # precompiled from >> /usr/local/lib/python2.4/encodings/__init__.pyc >> # /usr/local/lib/python2.4/codecs.pyc matches >> /usr/local/lib/python2.4/codecs.py >> import codecs # precompiled from /usr/local/lib/python2.4/codecs.pyc >> import _codecs # builtin >> # /usr/local/lib/python2.4/encodings/aliases.pyc matches >> /usr/local/lib/python2.4/encodings/aliases.py >> import encodings.aliases # precompiled from >> /usr/local/lib/python2.4/encodings/aliases.pyc >> # /usr/local/lib/python2.4/encodings/utf_8.pyc matches >> /usr/local/lib/python2.4/encodings/utf_8.py >> import encodings.utf_8 # precompiled from >> /usr/local/lib/python2.4/encodings/utf_8.pyc >> Python 2.4.4 (#3, May 15 2014, 00:22:35) >> [GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2 >> Type "help", "copyright", "credits" or "license" for more information. >> dlopen("/usr/local/lib/python2.4/lib-dynload/readline.so", 2); >> import readline # dynamically loaded from >> /usr/local/lib/python2.4/lib-dynload/readline.so >>>>> >> Tao >> >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> From dragriesti at comcast.net Wed May 14 21:08:21 2014 From: dragriesti at comcast.net (Charles Agriesti) Date: Wed, 14 May 2014 14:08:21 -0500 Subject: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text Message-ID: <5373BF25.2050106@comcast.net> Practical programming, 2nd Edition, Paul Gries, Jennifer Campbell, Jason Montojo (Python 3) P 184, the last half of chapter 10 requires the time_series module, which is no longer available, apparently replaced by Pandas. Looked into installing Pandas. Installing Anaconda is supposed to accomplish that, or help with it. Install Anaconda, It contains its own Python27, requires uninstall of old Python 2 . Done. Anaconda website says it allows easy switching between Python26, Python27, Python33. I have Python34 installed. Windows Powershell C:\Users\Charles enter: < conda > (shows a help file which I found less than helpful) C:\Users\Charles enter: < ipython notebook> This opens a browser window at http://127.0.0.1:8888/ , clicking 'new notebook' opens an unfamiliar looking ide called ipython notebook. searching hard drive for pandas found a couple of things that I don't think are the pandas program. same for scipy and numpy. Is this Anaconda thing any part of being able to run the scripts from the textbook with time_series? Is it a complete wild goose chase? Should I install SciPy? Is Pandas separate from that? Should I install Python33? Will this conflict with the 27 and 34 already on this computer? Give up on the Gries book and try a different one? Thanks in advance if anybody can help. From fomcl at yahoo.com Wed May 14 21:38:12 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 14 May 2014 12:38:12 -0700 (PDT) Subject: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text In-Reply-To: <5373BF25.2050106@comcast.net> References: <5373BF25.2050106@comcast.net> Message-ID: <1400096292.50731.YahooMailNeo@web163806.mail.gq1.yahoo.com> ________________________________ > From: Charles Agriesti >To: Tutor at python.org >Sent: Wednesday, May 14, 2014 9:08 PM >Subject: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text > > >Practical programming, 2nd Edition, Paul Gries, Jennifer Campbell, Jason >Montojo >(Python 3) > >P 184, the last half of chapter 10 requires the time_series module, >which is no longer available, apparently replaced by Pandas. > >Looked into installing Pandas. Installing Anaconda is supposed to >accomplish that, or help with it. >Install Anaconda, It contains its own Python27, requires uninstall of >old Python 2 . Done. >Anaconda website says it allows easy switching between Python26, >Python27, Python33. I have Python34 installed. >Windows Powershell >C:\Users\Charles >enter: < conda > >(shows a help file which I found less than helpful) >C:\Users\Charles >enter: < ipython notebook> > >This opens a browser window? at? http://127.0.0.1:8888/ ? , clicking >'new notebook' opens an unfamiliar looking ide called ipython notebook. Ipython = interactive python. Ipython notebook allows you to save interactive Python sessions as html. So you see input and output in one html file. Try entering "import pandas", and then "help(pandas)" >searching hard drive for pandas found a couple of things that I don't >think are the pandas program. same for scipy and numpy. Pandas uses the numpy library, among others. >Is this Anaconda thing any part of being able to run the scripts from >the textbook with time_series? Is it a complete wild goose chase? >Should I install SciPy? Is Pandas separate from that? Should I install >Python33? Will this conflict with the 27 and 34 already on this computer? >Give up on the Gries book and try a different one? >Thanks in advance if anybody can help. Anaconda is a bundle of useful python goodies, so next to a basic python installation (which contains tons of cool stuff already), even more, non-standard libraries are installed. Like numoy, scipy, pandas, numba, etc etc. From marc.tompkins at gmail.com Wed May 14 22:27:23 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Wed, 14 May 2014 13:27:23 -0700 Subject: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text In-Reply-To: <5373BF25.2050106@comcast.net> References: <5373BF25.2050106@comcast.net> Message-ID: On Wed, May 14, 2014 at 12:08 PM, Charles Agriesti wrote: > Is this Anaconda thing any part of being able to run the scripts from the > textbook with time_series? Is it a complete wild goose chase? > First off - I know nothing about using Python in a scientific setting. Second (before anyone else points this out) this is the Python tutor list; your question is a bit off-topic (i.e. relating to specific packages/distributions rather than Python itself.) This is not a criticism or rebuke; it's just that you're likely to get better help on a list where most of the people actually use the stuff you're asking about. Having said that, Pandas appears to be a pretty normal Python package, meaning that you could install it by itself; Anaconda is a distribution of scientific Python that aims to do it all for you in one swell foop - but it's not necessary. If you've installed pip, it's as simple as "pip install pandas". If you haven't installed pip and you use Windows, the simplest thing is to install pip-Win: https://sites.google.com/site/pydatalog/python/pip-for-windows Anaconda actually installs a virtual environment that doesn't rely on or interfere with other versions of Python that are already installed, so no worries there. As long as you work inside Anaconda, the fact that you also have 3.4 installed elsewhere is not an issue. The fact that Anaconda presents you with a radically different working environment than you're used to - that might be an issue. > Should I install SciPy? Is Pandas separate from that? Can't answer that. SciPy is a "stack", meaning that it contains multiple packages meant to work together (Pandas is one of them), and the SciPy people themselves recommend Anaconda as one of the easiest ways to install SciPy. Do you need it? No idea. Should I install Python33? Will this conflict with the 27 and 34 already on > this computer? > The chief source of conflict would be file associations and the system path - i.e. what happens when you double-click on a .py file or type "python" at a prompt. Here's where Python's "virtualenv" comes in handy, or a Python distribution that uses it - like Anaconda. As long as you work inside Anaconda, you don't have to worry about other versions. > Give up on the Gries book and try a different one? > It seems well-reviewed; a discontinued package doesn't seem like a good reason to abandon it, as long as there's a workaround... It does look like Pandas is the replacement for scikits.timeseries (although you're writing that as "time_series", which makes me wonder whether it's the same package at all!) Only trial and error will tell whether the syntax for using Pandas is the same as what's in your book. Good luck! -------------- next part -------------- An HTML attachment was scrubbed... URL: From jf_byrnes at comcast.net Wed May 14 22:40:49 2014 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Wed, 14 May 2014 15:40:49 -0500 Subject: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text In-Reply-To: <5373BF25.2050106@comcast.net> References: <5373BF25.2050106@comcast.net> Message-ID: On 05/14/2014 02:08 PM, Charles Agriesti wrote: > Practical programming, 2nd Edition, Paul Gries, Jennifer Campbell, Jason > Montojo > (Python 3) > > P 184, the last half of chapter 10 requires the time_series module, > which is no longer available, apparently replaced by Pandas. Read the first paragraph on page 185. The two def's on page 184 are saved in a file called time_series.py, this is what is being imported. Of course the paragraph gets confusing when it talks about importing tsdl. If I remember right once the time_series file was in the current directory the examples then worked. Regards, Jim From fomcl at yahoo.com Wed May 14 22:52:38 2014 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 14 May 2014 13:52:38 -0700 (PDT) Subject: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text In-Reply-To: References: <5373BF25.2050106@comcast.net> Message-ID: <1400100758.13403.YahooMailNeo@web163806.mail.gq1.yahoo.com> ----- Original Message ----- > From: Jim Byrnes > To: tutor at python.org > Cc: > Sent: Wednesday, May 14, 2014 10:40 PM > Subject: Re: [Tutor] substituting for time_series, Pandas, Anaconda. Practical Programming, intro Comp Sci, Gries text > > On 05/14/2014 02:08 PM, Charles Agriesti wrote: >> Practical programming, 2nd Edition, Paul Gries, Jennifer Campbell, Jason >> Montojo >> (Python 3) >> >> P 184, the last half of chapter 10 requires the time_series module, >> which is no longer available, apparently replaced by Pandas. > > Read the first paragraph on page 185. The two def's on page 184 are > saved in a file called time_series.py, this is what is being imported. > Of course the paragraph gets confusing when it talks about importing > tsdl.? If I remember right once the time_series file was in the current > directory the examples then worked. pandas = PANel DAta analysis (not sure about the S). So time series, orignally. CRAN R on steroids. From d at davea.name Wed May 14 22:16:45 2014 From: d at davea.name (Dave Angel) Date: Wed, 14 May 2014 16:16:45 -0400 Subject: [Tutor] While Loop? In-Reply-To: References: Message-ID: <5373CF2D.4020002@davea.name> On 05/14/2014 05:45 AM, Sam Ball wrote: > I'm attempting to create a program where the user inputs their account number (which must be 8 digits) and if what the user enters is not 8 digits in length I want python to tell the user this is invalid and then keep asking for the account number until a suitable number has been entered. > I'm guessing it's a sort of while loop but I'm not 100% sure as I'm quite new to Python. > Welcome to Python, and to the Tutor mailing list. Please post in text form, as the html you're now using is quite error prone, especially since this is a text list. There's no errors I can see with your present message, but it's a good habit to learn early - tell your email program to use "plain text". Next, it's very important to specify what version of Python you're using. In particular there are differences between Python version 2.x and version 3.x And those differences affect the input statement. From your print syntax, it appears you're aiming at Python 2.x But it's not valid there either, so it's better if you be explicit in your question. > So far I have... > > > > > userAccountNumber = eval(input("Please Input Your Account Number:")) Since I'm asssuming Python 2.x, you want int(raw_input( instead. input() in 2.x is a security mistake, and eval() is for either version of Python. > while userAccountNumber > 100000000 or <=9999999: Syntax error, as you've discovered. Alan has told you how to fix it. But it would be much better to get in the habit of quoting the entire error message (it's called a traceback) instead of paraphrasing it. > print userAccountNumber ("Invalid Account Number! Account Must Be Eight Digits") Once you've fixed the above, you'll get an error on this line. But how to fix it depends on what Python version you're using. userAccountNumber isn't a function, so why are you trying to call it? Next problem is that your loop doesn't ask the user again, it just loops around trying to print its error message. > > > > When I run this however it gives me an invalid syntax and highlights the = in <=9999999: > > Would appreciate any help with sorting this out. > Once you've gone as far as you can get with these suggestions from Alan, Danny, and me, be sure and use reply-list to post your next revision and query. (Or, if your email program doesn't support that, use Reply-All, and remove from the header everyone you don't want to get the response). And of course, you'll tell us what version of Python you're taqrgeting. In particular, keep the tutor at python.org recipient. Once your program works, you need to consider other things, possible bugs. For example, what happends if a user types "charlie" instead of a number? Or if they type a floating point value? What would you like to happen? -- DaveA From jeanrmichel at gmail.com Wed May 14 22:48:59 2014 From: jeanrmichel at gmail.com (JEAN MICHEL) Date: Wed, 14 May 2014 13:48:59 -0700 Subject: [Tutor] (no subject) Message-ID: I'm a Python beginner trying write a program that reads outside txt files, takes the data like the name and test grades of students then calculate the average and also assigns a grade and writes the data into a new txt file. I'm having difficulties writing the program so far I've been able to write up half of the program but I am noticing there might be bugs. I've tried running the program but every time I do, my program output is blank and there is no error messages here is the program def calcaverage(test1,test2,test3): for count in range(line): curraverage=0 curraverage=((test1[count]+ test2[count]+ test3[count])/3) currentaverage.append(curraverage) if curraverage>= 90: grade= "A" lettergrades.append(grade) else: if curraverage >= 80 and curraverage < 90: grade= "B" lettergrades.append(grade) else: if curraverage >= 70 and curraverage < 80: grade= "C" lettergrades.append(grade) else: if curraverage < 70: grade= "F" lettergrades.append(grade) name=[] test1=[] test2=[] test3=[] averagescore=[] lettergrades=[] with open ('/period1.txt', 'r') as infile: line = infile.readline() while line in infile: values = line.split() name.append(values[0] + ','+ values[1]) while line in infile: values = line.split() score1=float(value[2]) test1.append(score1) while line in infile: values = line.split() score2=float(value[3]) test2.append(score2) while line in infile: values = line.split() score3=float(value[4]) test3.append(score3) averagescore=calcaverage(test1,test2,test3) infile.close() print(line) -------------- next part -------------- An HTML attachment was scrubbed... URL: From mariopy at gmx.com Wed May 14 19:30:05 2014 From: mariopy at gmx.com (Mario Py) Date: Wed, 14 May 2014 11:30:05 -0600 Subject: [Tutor] Translator - multiple choice answer Message-ID: <5373A81D.6070008@gmx.com> Hi all, my first post here and my first ever programing project! (actually stealing code from all over the internet for now) So please be easy on me & hints probably will not work with me, I'm complete beginner... I'm trying to create something useful, small program which will help my wife to refresh her foreign knowledge of once already memorized words. Program works. But I would like to expand it a little bit. Program read TXT file (c:\\slo3.txt) In this file there are two words per line separated by tab. First word is foreign language and second word is proper translation, like this: pivo beer kruh bread rdeca red krompir potatoe hisa house cesta road auto car (not even trying to mess with special characters for now, lol) I was going to read content into dictionary, each pair as tuple but I gave up, couldn't figure it out. Looks like it is working with the list so no problem. Question 1: would be better to use dictionary, than list? Question 2: slo3.txt is just small sample for now and before I type in all words, I would like to know is it better to use some other separator such as coma or empty space instead of TAB? I found on the internet example for TAB only, so this is what I'm using for now. OK here is working code: from random import shuffle print('Write translation of Slovene word ') print() with open('c:\\slo3.txt') as f: lines = f.readlines() shuffle(lines) for line in lines: question, rightAnswer = line.strip().split('\t') # words are two per line separated by TAB. answer = input(question + ' ') if answer.lower() == rightAnswer: a = 0 # please ingore this for now, IF wants something here and I don't want to print extra line, output looks OK for now... # print() else: print('Wrong, correct is: %s.' % rightAnswer,) print() I need help with two things. First one is simple, basic, but I couldn't figure it out. If I want to print out 'Wrong,,,,' part in the same line next to wrong answer, how do I do it? Now, big, huge help request. I would like to make it easy on my wife :-) instead of her needing to type in answer I would like that she could choose (click on) multiple choice. Say she get 4 or 5 possible answers and one of them is correct. Then she need to click on correct answer... What do I need to do? I understand there will be some graphic/windows things involved. I don't have any additional packages or libraries installed, nor do I know what/how do do it. Complete noob.... I'm using Python 3.4 and Windows 7. Mario P.s. I'm not even sure if this will show up on Tutor's forum... From alan.gauld at btinternet.com Thu May 15 00:51:08 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 14 May 2014 23:51:08 +0100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 14/05/14 21:48, JEAN MICHEL wrote: > I'm a Python beginner Welcome to the list. Please always include a subject line relating to your question. It makes searching the archives much easier! > .... I'm having difficulties writing the program so far I've > been able to write up half of the program but I am noticing there might > be bugs. Get used to it, it's impossible to write more than a few lines of code without introducing bugs. The trick is to get rid of them before you give the code to anyone else! ;-) I've tried running the program but every time I do, my program > output is blank and there is no error messages Try inserting some print statements to track where you have reached. For example one just as you enter your function could show you how often the function is being called and with what values.... Although you only seem to call it once I notice... Thee are a couple of things below that can drive you crazy, please don't do them. See comments below > def calcaverage(test1,test2,test3): > for count in range(line): Where is line coming from? What is its value. Maybe a print here would surprise you? And what does range(line) produce? Again maybe not what you expect? If you want to use a value in a function pass it in as a parameter... > curraverage=0 This does nothing useful since you overwrite curraverage in the very next line. > curraverage=((test1[count]+ test2[count]+ test3[count])/3) > currentaverage.append(curraverage) Where is currentaverage defined? Apparently its a list but I don't see it below... > if curraverage>= 90: > grade= "A" > lettergrades.append(grade) > else: > if curraverage >= 80 and curraverage < 90: You can save a little typing by saying this as: if 80 <= curraverage < 90: > grade= "B" > lettergrades.append(grade) > else: > if curraverage >= 70 and curraverage < 80: And you can save some more as well as some indenting by using elif: instead of else:... if: > name=[] > test1=[] > test2=[] > test3=[] > averagescore=[] > lettergrades=[] > with open ('/period1.txt', 'r') as infile: > line = infile.readline() > while line in infile: The easiest way to process lines in Python is to use a for loop: for line in infile: > values = line.split() > name.append(values[0] + ','+ values[1]) > while line in infile: This definition of line will overwrite the value you had previously. Is that really what you want? Using the same variable to hold two different values in a single loop is usually a bad idea. > values = line.split() > score1=float(value[2]) > test1.append(score1) > while line in infile: And doing it 3 times is just asking for trouble! Especially since you are trying to iterate over the same file each time! > values = line.split() > score2=float(value[3]) > test2.append(score2) > while line in infile: And now 4 times. Eeek! How do you keep track of what line's value is? I suspect you need to unwind all these loops and rethink the structure here. Too complicated for me to try and read... > values = line.split() > score3=float(value[4]) > test3.append(score3) > averagescore=calcaverage(test1,test2,test3) > infile.close() You don't need to close a file you opened using 'with' Part of the 'with' magic is that it closes the file for you. > print(line) I suspect this print is way to late to help you. Try printing line (along with a label saying which while loop it is) after each while statement. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Thu May 15 01:00:58 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 14 May 2014 16:00:58 -0700 Subject: [Tutor] Translator - multiple choice answer In-Reply-To: <5373A81D.6070008@gmx.com> References: <5373A81D.6070008@gmx.com> Message-ID: > Program read TXT file (c:\\slo3.txt) > In this file there are two words per line separated by tab. > First word is foreign language and second word is proper translation, like > this: > > pivo beer > kruh bread > rdeca red > krompir potatoe > hisa house > cesta road > auto car > > (not even trying to mess with special characters for now, lol) Do look at: http://www.joelonsoftware.com/articles/Unicode.html because the fact that you're dealing with foreign language means you want to get it right. If it were me, I'd just say that the input is UTF-8 encoded text, and always open up the file in utf-8 mode. myfile = open("slo3.txt", "r", encoding="utf8") and just know that we're working with Unicode from that point forward. > I was going to read content into dictionary, each pair as tuple but I gave > up, couldn't figure it out. Looks like it is working with the list so no > problem. > > Question 1: would be better to use dictionary, than list? It depends. If you're picking out a random entry, then having a dictionary in hand is not going to need the key lookup support that dictionaries give you. For the application you're describing right now, it doesn't sound like you need this yet. > Question 2: slo3.txt is just small sample for now and before I type in all > words, I would like to know is it better to use some other separator such as > coma or empty space instead of TAB? I found on the internet example for TAB > only, so this is what I'm using for now. TAB is a reasonable separator. You might also consider comma, as in Comma-Separated Values (CSV). If your data starts having more structure, then check back with folks on the tutor mailing list. There are richer formats you can use, but your program's description suggests that you probably don't need the complexity yet. > > I need help with two things. First one is simple, basic, but I couldn't > figure it out. If I want to print out 'Wrong,,,,' part in the same line next > to wrong answer, how do I do it? The print function puts a newline at the end. You can change this default behavior by providing an "end" keyword to it. The documentation mentions it here: https://docs.python.org/3/library/functions.html#print Small test program to demonstrate: ################ print("Hello ", end="") print("world ") ################ > Now, big, huge help request. > I would like to make it easy on my wife :-) instead of her needing to type > in answer I would like that she could choose (click on) multiple choice. Say > she get 4 or 5 possible answers and one of them is correct. Then she need to > click on correct answer... > > What do I need to do? I understand there will be some graphic/windows things > involved. I don't have any additional packages or libraries installed, nor > do I know what/how do do it. Complete noob.... How about printing them with numbers, so that entry is just a number rather than the typed word? You can put a graphical user interface on the program, though it does take a bit more effort to get it to work. Look into "Tkinter", which is a library for producing graphical user interfaces: https://wiki.python.org/moin/TkInter Another option might be to turn your program into a web site, so that the interface is the web browser, which everyone is getting used to these days. But this, too, is also... involved. :P From illusiontechniques at gmail.com Thu May 15 01:25:11 2014 From: illusiontechniques at gmail.com (C Smith) Date: Wed, 14 May 2014 19:25:11 -0400 Subject: [Tutor] Translator - multiple choice answer In-Reply-To: References: <5373A81D.6070008@gmx.com> Message-ID: This might be useful for reading values from a text value into a dictionary: https://stackoverflow.com/questions/17775273/how-to-read-and-store-values-from-a-text-file-into-a-dictionary-python On Wed, May 14, 2014 at 7:00 PM, Danny Yoo wrote: >> Program read TXT file (c:\\slo3.txt) >> In this file there are two words per line separated by tab. >> First word is foreign language and second word is proper translation, like >> this: >> >> pivo beer >> kruh bread >> rdeca red >> krompir potatoe >> hisa house >> cesta road >> auto car >> >> (not even trying to mess with special characters for now, lol) > > Do look at: > > http://www.joelonsoftware.com/articles/Unicode.html > > because the fact that you're dealing with foreign language means you > want to get it right. > > > If it were me, I'd just say that the input is UTF-8 encoded text, and > always open up the file in utf-8 mode. > > myfile = open("slo3.txt", "r", encoding="utf8") > > and just know that we're working with Unicode from that point forward. > > > >> I was going to read content into dictionary, each pair as tuple but I gave >> up, couldn't figure it out. Looks like it is working with the list so no >> problem. >> >> Question 1: would be better to use dictionary, than list? > > It depends. > > If you're picking out a random entry, then having a dictionary in hand > is not going to need the key lookup support that dictionaries give > you. > > For the application you're describing right now, it doesn't sound like > you need this yet. > > > >> Question 2: slo3.txt is just small sample for now and before I type in all >> words, I would like to know is it better to use some other separator such as >> coma or empty space instead of TAB? I found on the internet example for TAB >> only, so this is what I'm using for now. > > TAB is a reasonable separator. You might also consider comma, as in > Comma-Separated Values (CSV). > > If your data starts having more structure, then check back with folks > on the tutor mailing list. There are richer formats you can use, but > your program's description suggests that you probably don't need the > complexity yet. > > > > >> >> I need help with two things. First one is simple, basic, but I couldn't >> figure it out. If I want to print out 'Wrong,,,,' part in the same line next >> to wrong answer, how do I do it? > > The print function puts a newline at the end. You can change this > default behavior by providing an "end" keyword to it. The > documentation mentions it here: > > https://docs.python.org/3/library/functions.html#print > > Small test program to demonstrate: > > ################ > print("Hello ", end="") > print("world ") > ################ > > > >> Now, big, huge help request. >> I would like to make it easy on my wife :-) instead of her needing to type >> in answer I would like that she could choose (click on) multiple choice. Say >> she get 4 or 5 possible answers and one of them is correct. Then she need to >> click on correct answer... >> >> What do I need to do? I understand there will be some graphic/windows things >> involved. I don't have any additional packages or libraries installed, nor >> do I know what/how do do it. Complete noob.... > > How about printing them with numbers, so that entry is just a number > rather than the typed word? > > > You can put a graphical user interface on the program, though it does > take a bit more effort to get it to work. > > Look into "Tkinter", which is a library for producing graphical user interfaces: > > https://wiki.python.org/moin/TkInter > > > > Another option might be to turn your program into a web site, so that > the interface is the web browser, which everyone is getting used to > these days. But this, too, is also... involved. :P > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From davea at davea.name Thu May 15 03:51:09 2014 From: davea at davea.name (Dave Angel) Date: Wed, 14 May 2014 21:51:09 -0400 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <53741D8D.60804@davea.name> On 05/14/2014 04:48 PM, JEAN MICHEL wrote: > I'm a Python beginner Welcome to python-tutor > trying write a program that reads outside txt files, What's an "outside txt file"? And since you only one file, perhaps you left out the othertwo? > takes the data like the name and test grades of students then calculate the > average and also assigns a grade and writes the data into a new txt file. > I'm having difficulties writing the program so far I've been able to write > up half of the program but I am noticing there might be bugs. I've tried > running the program but every time I do, my program output is blank and > there is no error messages I'm quite surprised at that, since you're calling range with a parameter of line, and line is some text string, not an int. > here is the program > > def calcaverage(test1,test2,test3): > for count in range(line): > curraverage=0 > curraverage=((test1[count]+ test2[count]+ test3[count])/3) > currentaverage.append(curraverage) > if curraverage>= 90: > grade= "A" > lettergrades.append(grade) > else: > if curraverage >= 80 and curraverage < 90: > grade= "B" > lettergrades.append(grade) > else: > if curraverage >= 70 and curraverage < 80: > grade= "C" > lettergrades.append(grade) > else: > if curraverage < 70: > grade= "F" > lettergrades.append(grade) You forgot to start a function here. Thus all the following code is at top=level, a poor practice. > name=[] > test1=[] > test2=[] > test3=[] > averagescore=[] > lettergrades=[] > with open ('/period1.txt', 'r') as infile: > line = infile.readline() If you're using this statement to skip the first line of the file, you should not bother to assign the result anywhere. Or assign it to a meaningful variable like "ignore" ignore = infile.readline() > while line in infile: > values = line.split() > name.append(values[0] + ','+ values[1]) > while line in infile: Whoops. By the time this loop finishes, the outer one will find there are no more lines left in the file. > values = line.split() > score1=float(value[2]) > test1.append(score1) > while line in infile: > values = line.split() > score2=float(value[3]) > test2.append(score2) > while line in infile: > values = line.split() > score3=float(value[4]) > test3.append(score3) This is the place where you should call the above data-gathering function (the one you didn't make a function) Then once you've gathered the data, you should call calcaverage() with test1, test2,test3, and at least one more argument. > averagescore=calcaverage(test1,test2,test3) > infile.close() Not needed. infile is closed once the with clause completes. > print(line) Which of the many meanings of line do you mean to print out?? Those triply nested reads of the same file are doomed to failure. Once the innermost one of them reaches the end of file, the others will see nothing more. We can't debug it much further without knowing much more about what the data file (files ???) looks like. For example, perhaps there are supposed to be three files, corresponding to the three lists: test1, test2, and test3. -- DaveA From dyoo at hashcollision.org Thu May 15 06:15:47 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 14 May 2014 21:15:47 -0700 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: > if curraverage>= 90: > grade= "A" > lettergrades.append(grade) > else: > if curraverage >= 80 and curraverage < 90: > grade= "B" > lettergrades.append(grade) > else: > if curraverage >= 70 and curraverage < 80: > grade= "C" > lettergrades.append(grade) > else: > if curraverage < 70: > grade= "F" > lettergrades.append(grade) Just wanted to note that this style of cascading if statements is a little unusual here. Since you know that only one of the tests is going to be true, you can use a more parallel structure. That is, if you're doing something like: ################## if test1: ... else: if test2: ... else: if test3: ... else: ... ################## and if you know that test1, test2, and test3 don't overlap, you can express this as: ################## if test1: ... elif test2: ... elif test3: ... else: ... ################## and avoid the Tower of Pisa-style code. From dyoo at hashcollision.org Thu May 15 06:20:44 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 14 May 2014 21:20:44 -0700 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On Wed, May 14, 2014 at 9:15 PM, Danny Yoo wrote: >> if curraverage>= 90: >> grade= "A" >> lettergrades.append(grade) >> else: >> if curraverage >= 80 and curraverage < 90: >> grade= "B" >> lettergrades.append(grade) >> else: >> if curraverage >= 70 and curraverage < 80: >> grade= "C" >> lettergrades.append(grade) >> else: >> if curraverage < 70: >> grade= "F" >> lettergrades.append(grade) > > > Just wanted to note that this style of cascading if statements is a > little unusual here. Since you know that only one of the tests is > going to be true, you can use a more parallel structure. Sorry, I had combined two ideas in my post, one of which (the exclusivity hint) is totally not relevant in this situation. We can do the code transformation unconditionally. The exclusivity of the tests doesn't matter here. So when we see ourselves doing: ################## if test1: ... else: if test2: ... else: if test3: ... else: ... ################## we should always be able to flatten this out to this shape instead. ################## if test1: ... elif test2: ... elif test3: ... else: ... ################## Sorry about the confusion. From cs at zip.com.au Thu May 15 01:05:59 2014 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 15 May 2014 09:05:59 +1000 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: <20140514230559.GA90282@cskk.homeip.net> Before we begin, two etiquette requests: - please supply a meaningful subject line; it helps everyone keep track of the discussions; this one might have used something like "help with simple grading problem" - please see if your mailer (GMail?) can be told to send just plain text to this list Anyway, to the topic... On 14May2014 13:48, JEAN MICHEL wrote: >I'm a Python beginner trying write a program that reads outside txt files, >takes the data like the name and test grades of students then calculate the >average and also assigns a grade and writes the data into a new txt file. >I'm having difficulties writing the program so far I've been able to write >up half of the program but I am noticing there might be bugs. I've tried >running the program but every time I do, my program output is blank and >there is no error messages In there circumstances it can be helpful to insert "print" statements in various places in the program to see what parts of it are actually being run, and with what values. >here is the program > >def calcaverage(test1,test2,test3): > for count in range(line): The first thing that catches my eye is that "line" is not a variable in this function. You clearly mean it to have a value, but it is not supplied. Normally it would come in as a parameter with "test1, test2, test3". On further inspection, you don't use "line", and the function does not return "grade". In Python, all variables in a function are, generally, "local" variables; this is good practice in most other languages too - it means that you can look at a function on its own without knowing much about the rest of the program, and also means that what happens inside the function stay inside the function and does not accidentally affect the rest of the program. It looks like your function here calculates values from the [count] element of the three test lists. So I would advocate: - get rid of the "for" loop - it belongs in your main program, not here - pass in "count" and "lettergrades" as parameters to the function - it needs to be given them - return "grade" from the function, by putting: return grade at the bottom [...snip...] >name=[] >test1=[] >test2=[] >test3=[] >averagescore=[] >lettergrades=[] >with open ('/period1.txt', 'r') as infile: You may mean just "period1.txt" here? On a UNIX system "/period1.txt" would be at the top of the system directory tree, not in your current directory. > line = infile.readline() > while line in infile: I suspect this is where your problem with no output lies. I would expect you want to iterate over every line in the input file. What you are actually doing above is two things: - reading the first line of the file, just once - examining the rest of the file to see if the line occurs later (presumably not) Because an open text file is iterable, your while condition: line in infile actually loads the rest of the file to see if "line" is "in" it. That reads everything. And becuase (probably) the line does not occur a second time, the while loop does not every run once, thus no output. Replace these two lines with this: for line in infile: which will read each line in turn for processing. Then see what happens. Note that you do this again lower down. I am not sure what you intend to happen here. Put it "print" statements to see what actually happens - it should help you figure out what you really need to do. [...snip...] >infile.close() You do not need to "infile.close()". The original: with open ('/period1.txt', 'r') as infile: will do that for you. >print(line) This is at the bottom, outside all the loops. It can at best print the last line of the file. I do not know what you actaully intend here. See if any of these suggestions help you progress with your program, and report back (by replying to this thread) when you have new problems or new questions. Cheers, Cameron Simpson From jeanrmichel at gmail.com Thu May 15 06:57:36 2014 From: jeanrmichel at gmail.com (JEAN MICHEL) Date: Wed, 14 May 2014 21:57:36 -0700 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: thank you for the all advice I really appreciate it. I probably wasn't wasn't too clear when i first sent my email asking for help, my apologies so let me better explain what i'm doing. I'm trying to write a program that reads 3 class data files such as period1.txt, period2.txt. Inside those files are the first name, last name, and 3 test scores from 3 different students. The format of the data files looks like this Meagan. Hesse. 99. 99. 99 My program is then suppose to take the average assign a grade and write the results and also the students names in another txt folder I've tried fixing some of the problems but it looks like it's still a work in progress I'm not to sure whether i'm opening the data files right, can you take a look at that part def calcaverage(test1,test2,test3): for count in range(test1,test2,test3): curraverage=0 curraverage=((test1[count]+ test2[count]+ test3[count])/3) currentaverage.append(curraverage) if curraverage>= 90: grade= "A" lettergrades.append(grade) elif curraverage >= 80 and curraverage < 90: grade= "B" lettergrades.append(grade) elif curraverage >= 70 and curraverage < 80: grade= "C" lettergrades.append(grade) elif curraverage < 70: grade= "F" lettergrades.append(grade) name=[] test1=[] test2=[] test3=[] averagescore=[] lettergrades=[] with open ("period1.txt", 'r') as infile: for line in infile: values = line.split() name.append(values[0] + ','+ values[1]) for line in infile: values = line.split() score1=float(values[2]) test1.append(score1) for line in infile: values = line.split() score2=float(values[3]) test2.append(score2) for line in inline: values = line.split() score3=float(values[4]) test3.append(score3) averagescore=calcaverage(test1,test2,test3) print(line) On Wed, May 14, 2014 at 9:15 PM, Danny Yoo wrote: > > if curraverage>= 90: > > grade= "A" > > lettergrades.append(grade) > > else: > > if curraverage >= 80 and curraverage < 90: > > grade= "B" > > lettergrades.append(grade) > > else: > > if curraverage >= 70 and curraverage < 80: > > grade= "C" > > lettergrades.append(grade) > > else: > > if curraverage < 70: > > grade= "F" > > lettergrades.append(grade) > > > Just wanted to note that this style of cascading if statements is a > little unusual here. Since you know that only one of the tests is > going to be true, you can use a more parallel structure. > > That is, if you're doing something like: > > ################## > if test1: > ... > else: > if test2: > ... > else: > if test3: > ... > else: > ... > ################## > > and if you know that test1, test2, and test3 don't overlap, you can > express this as: > > > ################## > if test1: > ... > elif test2: > ... > elif test3: > ... > else: > ... > ################## > > and avoid the Tower of Pisa-style code. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu May 15 08:05:37 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 15 May 2014 07:05:37 +0100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 15/05/14 05:57, JEAN MICHEL wrote: > with open ("period1.txt", 'r') as infile: > for line in infile: > values = line.split() > name.append(values[0] + ','+ values[1]) > for line in infile: > values = line.split() > score1=float(values[2]) > test1.append(score1) I've just realised (I think) why you insist on nesting loops. You are trying to get the lines in groups? There are various ways to do this but the easiest here is to use next(): with open(period.txt') as infile: for line in infile: process first line line = next(infile) # get second line process second line line = next(infile) # get third line etc... That way you don't read the whole file at one go which your nested loop "solution" does. The for loop will effectively read every 5th line (if you have 4 next() calls inside it) But you may have to use a try/except clause to catch a StopIteration(?) exception if the file does not have a multiple of 5 lines in it. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From emile at fenx.com Thu May 15 19:30:01 2014 From: emile at fenx.com (Emile van Sebille) Date: Thu, 15 May 2014 10:30:01 -0700 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 5/14/2014 9:57 PM, JEAN MICHEL wrote: > def calcaverage(test1,test2,test3): > for count in range(test1,test2,test3): > curraverage=0 > curraverage=((test1[count]+ test2[count]+ test3[count])/3) > currentaverage.append(curraverage) > if curraverage>= 90: > grade= "A" > lettergrades.append(grade) Note that once the above if fails such that the below elif is tested, you already know that curraverage>= 90 from the if above failed, so on the elif below you really don't need to test if curraverage < 90. > elif curraverage >= 80 and curraverage < 90: > grade= "B" > lettergrades.append(grade) > elif curraverage >= 70 and curraverage < 80: > grade= "C" > lettergrades.append(grade) > elif curraverage < 70: > grade= "F" > lettergrades.append(grade) You can also move the lettergrades.append(grade) outside the if tests. Combining both simplifies this to: if curraverage>= 90: grade="A" elif curraverage >= 80: grade = "B" elif curraverage >= 70: grade = "C" elif curraverage >= 60: grade = "D" else: grade = "F" lettergrades.append(grade) HTH, Emile From dragriesti at comcast.net Fri May 16 04:24:38 2014 From: dragriesti at comcast.net (Charles Agriesti, DDS) Date: Thu, 15 May 2014 21:24:38 -0500 Subject: [Tutor] Tutor Digest, Vol 120, Issue 27 In-Reply-To: References: Message-ID: <537576E6.1060909@comcast.net> On 2/6/2014 1:48 AM, tutor-request at python.org wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://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: Which computer operating system is best for Python (Danny Yoo) > 2. Re: Which computer operating system is best for Python (Danny Yoo) > 3. Re: Which computer operating system is best for Python > (Dave Angel) > 4. Re: Which computer operating system is best for Python > (Sacha Rook) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Wed, 5 Feb 2014 20:19:55 -0800 > From: Danny Yoo > To: colin.chinsammy at erickson.com > Cc: "tutor at python.org" > Subject: Re: [Tutor] Which computer operating system is best for > Python > Message-ID: > > Content-Type: text/plain; charset=UTF-8 > > ... reading through the rest of this thread... I'm surprised no one > has mentioned: > > http://www.codecademy.com/tracks/python > > > Depending on your child's level, you might even look at Scratch: > > http://scratch.mit.edu/ > > which is not Python at all, but it is programming, and it is very much > focused on the beginner experience. > > > ------------------------------ > > Message: 2 > Date: Wed, 5 Feb 2014 20:42:12 -0800 > From: Danny Yoo > To: Colin Chinsammy > Cc: "tutor at python.org" > Subject: Re: [Tutor] Which computer operating system is best for > Python > Message-ID: > > Content-Type: text/plain; charset="utf-8" > > On Wed, Feb 5, 2014 at 10:34 AM, Colin Chinsammy < > colin.chinsammy at erickson.com> wrote: > >> I am considering purchasing the Acer c720 chromebook for my 13yo to >> begin learning Python for Kids. Obviously I am on a budget. >> >> Is this a good choice for a complete beginner? Any particular challenges >> that she might encounter using a chromebook OS? >> >> Any advice would be greatly appreciated. As I have researched my way into >> confusion. >> >> > Hi Colin, > > Ok, I read this question a little more carefully. > > You're planning to use the book "Python for Kids", right? > > http://shop.oreilly.com/product/9781593274078.do > > If using that book is a requirement, then I have to take back what I said > about web environments being appropriate for this particular situation. > > > That book requires a computer running either Windows, Mac OS X, or Linux. > It will have some exercises that assume that it can have graphical control > over the desktop with the "Tk" graphics library, a capability that you will > not likely have in a web-based environment. > > Apologies for not reading the requirements more closely. I saw "learning > Python for kids", and did not think that you were referring to a specific > book! :P > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > > ------------------------------ > > Message: 3 > Date: Thu, 6 Feb 2014 00:38:19 -0500 (EST) > From: Dave Angel > To: tutor at python.org > Subject: Re: [Tutor] Which computer operating system is best for > Python > Message-ID: > > Steven D'Aprano Wrote in message: >> >> >> >> But still complex. And you're limited by the (lack of) stability of >> Windows. >> >> If you don't *need* Windows, there is no point in running Linux on top >> of Windows in a virtual machine. It just means you're using twice as >> much memory, and you are still bound by the stability of Windows. >> > Right. I repartitioned my drive, shrank the installed Windows > partition to a minimum, and installed linux as a dual boot. If I > really need a Windows program, I run it in a Virtual box with > XP. >> test >> > From gchan401 at msn.com Fri May 16 03:58:07 2014 From: gchan401 at msn.com (Glen Chan) Date: Thu, 15 May 2014 21:58:07 -0400 Subject: [Tutor] Help with Python Message-ID: Hello, I am student trying to fugure out why when I enter any number it says error. It's only suppose to do that if it's out the 1-10 range. Please help. Thank you. number = input('Enter a number between 1 and 10: ') while number < 1 or number > 10: print 'Please enter a number between 1 and 10' number = input('Enter a number between 1 and 10: ') number = input('Enter a number between 1 and 10: ') while number > 1 or number < 10: print 'Error' number = input('Enter a number between 1 and 10: ') endProgram = raw_input('Do you want to end program? (Enter no or yes): ') while not (endProgram == 'yes' or endProgram == 'no'): print 'Please enter a yes or no' endProgram = raw_input('Do you want to end program? (Enter no to process a new set of scores): ') -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri May 16 10:07:07 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 16 May 2014 09:07:07 +0100 Subject: [Tutor] Help with Python In-Reply-To: References: Message-ID: On 16/05/14 02:58, Glen Chan wrote: > Hello, I am student trying to fugure out why when I enter any number it > says error. Because that's what you programmed it to do. Almost. If you enter 1 or 10 you won't get an error. Look at your logic: > number = input('Enter a number between 1 and 10: ') > while number < 1 or number > 10: > print 'Please enter a number between 1 and 10' > number = input('Enter a number between 1 and 10: ') > > number = input('Enter a number between 1 and 10: ') > while number > 1 or number < 10: > print 'Error' > number = input('Enter a number between 1 and 10: ') If the first number you enter is say 7. It skips the first while loop and asks for another number. If you enter 7 again It then goes into the second while loop and reports an error. The only numbers which don't fall into one of the two loops are 1 and 10. BTW It's bad practice to use input() in Python v2 becauise it has security issues. Instead use raw_input() and convert to an int() or float() explicitly. > while not (endProgram == 'yes' or endProgram == 'no'): > print 'Please enter a yes or no' There is a bug here too since you don't provide a way for the user to enter a new number. It will loop forever. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Sat May 17 05:55:10 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 17 May 2014 13:55:10 +1000 Subject: [Tutor] Help with Python In-Reply-To: References: Message-ID: <20140517035510.GR4273@ando> Hi Glen, and welcome! My responses below. On Thu, May 15, 2014 at 09:58:07PM -0400, Glen Chan wrote: > Hello, I am student trying to fugure out why when I enter any number > it says error. It's only suppose to do that if it's out the 1-10 > range. Please help. Thank you. You've made an mistake in your logic below. You print an error for *every* number: > number = input('Enter a number between 1 and 10: ') > while number < 1 or number > 10: > print 'Please enter a number between 1 and 10' > number = input('Enter a number between 1 and 10: ') This part is okay. You check that if number is out of range, and if it is, it prints a message. Check for yourself: "Suppose I choose 5. Is 5 less than 1? No. Is it larger than 10? No. So the while loop immediately ends, and I move on." So at this stage, you have now successfully asked the user for a number between 1 and 10. But here you make the mistake: > number = input('Enter a number between 1 and 10: ') Hmmm. You've already asked the user for a number. But then you ignore it, and ask for another one. But even that's not really the mistake: > while number > 1 or number < 10: > print 'Error' > number = input('Enter a number between 1 and 10: ') And this is where you get the logic backwards. This while loop runs forever, or until you manually cancel it. Check for yourself: "Suppose I choose 5. Is 5 larger than 1? Yes. So the while loop continues, and 'Error' is printed. Suppose I choose 0. Is 0 larger than 1? No. Is 0 less than 10? Yes. So again, the while loop continues, and 'Error' is printed. Are there *any* numbers *smaller* than 1 AND *larger* than 10 at the same time? No, of course not. So the while loop condition is always true, and the while loop will always run no matter what number I choose." This second while loop has the logic backwards. You should compare this one, the faulty one, with the first one, the correct one. Can you see the difference? A couple of other comments about your code: * In a few places, you use "input", but in other places, you use "raw_input". You should never use "input". It was a mistake, and has been removed from newer versions of Python. Instead, you should write int(raw_input("Enter a number...")). * You check that number is in range like this: number < 1 or number > 10 There's an easier way: 1 <= number <= 10 which is not only less typing, but makes it more obvious what you are trying to do. You want number to be in the range 1 through 10 inclusive. That makes it harder to screw up the logic: 1 >= number >= 10 # Wrong! "What do you mean, 1 is BIGGER than number, which is bigger than 10? That's impossible!" -- Steven From fantasticfaces2 at gmail.com Sat May 17 09:01:17 2014 From: fantasticfaces2 at gmail.com (ani) Date: Sat, 17 May 2014 07:01:17 +0000 (UTC) Subject: [Tutor] List of Lists for three Frames Message-ID: So I thought it would be cool to read a sequence at three different frames, which I have pasted below. However, I've come across a conundrum: how to make a list of lists. See, I'd like a final output that displays data of the type of frame with a + or a - to signify the direction of the read (+1,+2, +3, -1, -2, -3), the numerical position of each start codon along with its stop codon, and the length between the start and stop codons. (I am only looking for the longest ORFs) An example of the output would look like this: +1 57166..61908 4743. The reason why I'd like a list of lists is so that I can take all the Starts I found in the code below, store it, then be able to place it in to the output somehow. I thought that maybe a list of lists would be the best way of getting this information from three different frames. However, I'm not sure how to go about this... could someone give me a way to start? The while loops work fine, last time I checked. Thanks. def codons(self, frame_one, frame_two, frame_three): while frame_one <=len(self.seq): yield (self.seq[frame_one:frame_one+3]) frame_one += 3 while frame_two <=len(self.seq): yield (self.seq[frame_two:frame_two+3]) frame_two += 3 while frame_three <=len(self.seq): yield (self.seq[frame_three:frame_three+3]) frame_three += 3 val = seq.codons(0,1,2) for i in val: return(i) self.startlist.append[[frame_one], [frame_two], [frame_three]] From __peter__ at web.de Sat May 17 10:46:22 2014 From: __peter__ at web.de (Peter Otten) Date: Sat, 17 May 2014 10:46:22 +0200 Subject: [Tutor] List of Lists for three Frames References: Message-ID: ani wrote: > So I thought it would be cool to read a sequence at three different > frames, which I have pasted below. However, I've come across a conundrum: > how to make a list of lists. See, I'd like a final output that displays > data of the type of frame with a + or a - to signify the direction of the > read (+1,+2, +3, -1, -2, -3), the numerical position of each start codon > along with its stop codon, and the length between the start and stop > codons. (I am only looking for the longest ORFs) An example of the output > would look like this: > > +1 57166..61908 4743. > The reason why I'd like a list of lists is so that I can take all the > Starts I found in the code below, store it, then be able to place it in to > the output somehow. I thought that maybe a list of lists would be the best > way of getting this information from three different frames. However, I'm > not sure how to go about this... could someone give me a way to start? The > while loops work fine, last time I checked. Thanks. > > def codons(self, frame_one, frame_two, frame_three): > while frame_one <=len(self.seq): > yield (self.seq[frame_one:frame_one+3]) > frame_one += 3 > > while frame_two <=len(self.seq): > yield (self.seq[frame_two:frame_two+3]) > frame_two += 3 > > while frame_three <=len(self.seq): > yield (self.seq[frame_three:frame_three+3]) > frame_three += 3 > > val = seq.codons(0,1,2) > > for i in val: > return(i) > > self.startlist.append[[frame_one], [frame_two], [frame_three]] While making a list of lists is easy, just append the inner lists to the outer, >>> items = [] >>> items.append([1, 2, 3]) >>> items.append([4, 5, 6]) >>> items [[1, 2, 3], [4, 5, 6]] I'd say it is far too early for you to write any "real" code. Go through a tutorial to get familiar with Python's syntax first. Then write a detailed description in plain English of what you are trying to do. We can help you with Python, but most of us lack the domain knowledge to even decipher what you are up to from the exposition you give above. More importantly, a detailed plan will help you writing the corresponding script. Now for the best part: once you have written working code you will usually throw it away and switch to a library like biopython. See From ani.benabbas at gmail.com Sat May 17 09:00:31 2014 From: ani.benabbas at gmail.com (ani ben) Date: Sat, 17 May 2014 03:00:31 -0400 Subject: [Tutor] ORFs Python question Message-ID: Hello, So I thought it would be cool to read a sequence at three different frames, which I have pasted below. However, I've come across a conundrum: how to make a list of lists. See, I'd like a final output that displays data of the type of frame with a + or a - to signify the direction of the read (+1,+2, +3, -1, -2, -3), the numerical position of each start codon along with its stop codon, and the length between the start and stop codons. (I am only looking for the longest ORFs) An example of the output would look like this: +1 57166..61908 4743. The reason why I'd like a list of lists is so that I can take all the Starts I found in the code below, store it, then be able to place it in to the output somehow. I thought that maybe a list of lists would be the best way of getting this information from three different frames. However, I'm not sure how to go about this... could someone give me a way to start? The while loops work fine, last time I checked. Thanks. def codons(self, frame_one, frame_two, frame_three): while frame_one <=len(self.seq): yield (self.seq[frame_one:frame_one+3]) frame_one += 3 while frame_two <=len(self.seq): yield (self.seq[frame_two:frame_two+3]) frame_two += 3 while frame_three <=len(self.seq): yield (self.seq[frame_three:frame_three+3]) frame_three += 3 val = seq.codons(0,1,2) for i in val: return(i) self.startlist.append[[frame_one], [frame_two], [frame_three]] -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat May 17 15:26:28 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 17 May 2014 14:26:28 +0100 Subject: [Tutor] List of Lists for three Frames In-Reply-To: References: Message-ID: On 17/05/14 08:01, ani wrote: > So I thought it would be cool to read a sequence at three different frames, A sequence of what? And whats a frame in this context? >...I've come across a conundrum: how to make a list of lists. outerlist = [] innerlist = [1,2,3] outerlist.append(innerlist) outerlist.append([4,5,6]) print (outerlist[1][1]) # prints 5 > type of frame with a + or a - to signify the direction of the read (+1,+2, > +3, -1, -2, -3), What does 'direction of the read mean'? > the numerical position of each start codon a Whats a codon? This is a list for programmers learning python. Do not assume any specialized domain knowledge. If you can explain the problem in terms of plain English and basic math then we can probably help. If you use terms that are either ambiguous or too specialized we will be lost and unable to assist. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From mariopy at gmx.com Sun May 18 03:31:43 2014 From: mariopy at gmx.com (Mario Py) Date: Sat, 17 May 2014 19:31:43 -0600 Subject: [Tutor] Translator - multiple choice answer Message-ID: <53780D7F.3030408@gmx.com> Danny and C Smith, Thank you very much for your answers. And sorry for late reply I was away. I will start including encoding="utf8" right away. Printing them with numbers is also great idea and decent compromise, thanks.... >> The print function puts a newline at the end. You can change this default behavior by providing an "end" keyword to it. << I'm familiar with end from examples from the internet, but I was not successful with how to implement it into my example... Or something else get merged or I get an error... I will keep try and if I don't succeed I will come back here :-) Thanks for all your help! ========================================================================= This might be useful for reading values from a text value into a dictionary: https://stackoverflow.com/questions/17775273/how-to-read-and-store-values-from-a-text-file-into-a-dictionary-python On Wed, May 14, 2014 at 7:00 PM, Danny Yoo wrote: >> Program read TXT file (c:\\slo3.txt) >> In this file there are two words per line separated by tab. >> First word is foreign language and second word is proper translation, like >> this: >> >> pivo beer >> kruh bread >> rdeca red >> krompir potatoe >> hisa house >> cesta road >> auto car >> >> (not even trying to mess with special characters for now, lol) > > Do look at: > > http://www.joelonsoftware.com/articles/Unicode.html > > because the fact that you're dealing with foreign language means you > want to get it right. > > > If it were me, I'd just say that the input is UTF-8 encoded text, and > always open up the file in utf-8 mode. > > myfile = open("slo3.txt", "r", encoding="utf8") > > and just know that we're working with Unicode from that point forward. > > > >> I was going to read content into dictionary, each pair as tuple but I gave >> up, couldn't figure it out. Looks like it is working with the list so no >> problem. >> >> Question 1: would be better to use dictionary, than list? > > It depends. > > If you're picking out a random entry, then having a dictionary in hand > is not going to need the key lookup support that dictionaries give > you. > > For the application you're describing right now, it doesn't sound like > you need this yet. > > > >> Question 2: slo3.txt is just small sample for now and before I type in all >> words, I would like to know is it better to use some other separator such as >> coma or empty space instead of TAB? I found on the internet example for TAB >> only, so this is what I'm using for now. > > TAB is a reasonable separator. You might also consider comma, as in > Comma-Separated Values (CSV). > > If your data starts having more structure, then check back with folks > on the tutor mailing list. There are richer formats you can use, but > your program's description suggests that you probably don't need the > complexity yet. > > > > >> >> I need help with two things. First one is simple, basic, but I couldn't >> figure it out. If I want to print out 'Wrong,,,,' part in the same line next >> to wrong answer, how do I do it? > > The print function puts a newline at the end. You can change this > default behavior by providing an "end" keyword to it. The > documentation mentions it here: > > https://docs.python.org/3/library/functions.html#print > > Small test program to demonstrate: > > ################ > print("Hello ", end="") > print("world ") > ################ > > > >> Now, big, huge help request. >> I would like to make it easy on my wife instead of her needing to type >> in answer I would like that she could choose (click on) multiple choice. Say >> she get 4 or 5 possible answers and one of them is correct. Then she need to >> click on correct answer... >> >> What do I need to do? I understand there will be some graphic/windows things >> involved. I don't have any additional packages or libraries installed, nor >> do I know what/how do do it. Complete noob.... > > How about printing them with numbers, so that entry is just a number > rather than the typed word? > > > You can put a graphical user interface on the program, though it does > take a bit more effort to get it to work. > > Look into "Tkinter", which is a library for producing graphical user interfaces: > > https://wiki.python.org/moin/TkInter > > > > Another option might be to turn your program into a web site, so that > the interface is the web browser, which everyone is getting used to > these days. But this, too, is also... involved. :P > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From samball72 at hotmail.com Mon May 19 00:44:19 2014 From: samball72 at hotmail.com (Sam Ball) Date: Mon, 19 May 2014 09:14:19 +1030 Subject: [Tutor] While Loop? In-Reply-To: <5373CF2D.4020002@davea.name> References: , <5373CF2D.4020002@davea.name> Message-ID: > Firstly thanks everyone for the help, I definitely learned something. > The current version of Python I'm using is actually version 3, sorry for not specifying that earlier. > I ended up going with Alan's suggestion and created the below code which works perfectly fine. > I however would like to add in another line that tells the user their account is invalid > before looping around and asking them to re enter their account. > > userAccount = '' > while len (userAccount) !=8: > userAccount = input ("Please Input Your Account Number:") > userAccountNumber = int(userAccount) > > > As for users typing in a name like "Charlie" or a float I would like that to spit out > an error also, but I haven't learned how to accomplish that yet. > > Thanks again. ---------------------------------------- > Date: Wed, 14 May 2014 16:16:45 -0400 > From: d at davea.name > To: tutor at python.org > Subject: Re: [Tutor] While Loop? > > On 05/14/2014 05:45 AM, Sam Ball wrote: >> I'm attempting to create a program where the user inputs their account number (which must be 8 digits) and if what the user enters is not 8 digits in length I want python to tell the user this is invalid and then keep asking for the account number until a suitable number has been entered. >> I'm guessing it's a sort of while loop but I'm not 100% sure as I'm quite new to Python. >> > > Welcome to Python, and to the Tutor mailing list. Please post in text > form, as the html you're now using is quite error prone, especially > since this is a text list. There's no errors I can see with your > present message, but it's a good habit to learn early - tell your email > program to use "plain text". > > Next, it's very important to specify what version of Python you're > using. In particular there are differences between Python version 2.x > and version 3.x And those differences affect the input statement. > > From your print syntax, it appears you're aiming at Python 2.x But > it's not valid there either, so it's better if you be explicit in your > question. > > >> So far I have... >> >> >> >> >> userAccountNumber = eval(input("Please Input Your Account Number:")) > > Since I'm asssuming Python 2.x, you want int(raw_input( instead. > input() in 2.x is a security mistake, and eval() is for either version > of Python. > > >> while userAccountNumber> 100000000 or <=9999999: > > Syntax error, as you've discovered. Alan has told you how to fix it. > But it would be much better to get in the habit of quoting the entire > error message (it's called a traceback) instead of paraphrasing it. > >> print userAccountNumber ("Invalid Account Number! Account Must Be Eight Digits") > > Once you've fixed the above, you'll get an error on this line. But how > to fix it depends on what Python version you're using. > userAccountNumber isn't a function, so why are you trying to call it? > > Next problem is that your loop doesn't ask the user again, it just loops > around trying to print its error message. >> >> >> >> When I run this however it gives me an invalid syntax and highlights the = in <=9999999: >> >> Would appreciate any help with sorting this out. >> > > Once you've gone as far as you can get with these suggestions from Alan, > Danny, and me, be sure and use reply-list to post your next revision and > query. (Or, if your email program doesn't support that, use Reply-All, > and remove from the header everyone you don't want to get the response). > And of course, you'll tell us what version of Python you're > taqrgeting. In particular, keep the tutor at python.org recipient. > > Once your program works, you need to consider other things, possible > bugs. For example, what happends if a user types "charlie" instead of > a number? Or if they type a floating point value? What would you like > to happen? > > -- > DaveA > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Mon May 19 09:39:40 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 19 May 2014 08:39:40 +0100 Subject: [Tutor] While Loop? In-Reply-To: References: , <5373CF2D.4020002@davea.name> Message-ID: On 18/05/14 23:44, Sam Ball wrote: >> I however would like to add in another line that tells the user their account is invalid >> before looping around and asking them to re enter their account. OK, Time to introdusce another Pyhon idioM, the break clause. while True: # loop forever get input from user check/convert input if no errors: break # exits the loop print error message here "Please Input Your Account Number:") >> userAccountNumber = int(userAccount) > >> As for users typing in a name like "Charlie" or a float I would like that to spit out >> an error also, but I haven't learned how to accomplish that yet. The code you have will detect the error and throw an exception. To stay in the loop you need to catch those exceptions in a try/except clause. while True: try: get input check/convert input break except error1, error2,....: print error messages here If you haven't covered try/except yet then you should read up on it as it has several variations and subtleties. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From wolfgang.maier at biologie.uni-freiburg.de Mon May 19 11:42:17 2014 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Mon, 19 May 2014 11:42:17 +0200 Subject: [Tutor] List of Lists for three Frames In-Reply-To: References: Message-ID: <5379D1F9.4000607@biologie.uni-freiburg.de> On 17.05.2014 15:26, Alan Gauld wrote: > On 17/05/14 08:01, ani wrote: >> So I thought it would be cool to read a sequence at three different >> frames, > > A sequence of what? > And whats a frame in this context? > >> ...I've come across a conundrum: how to make a list of lists. > > outerlist = [] > innerlist = [1,2,3] > outerlist.append(innerlist) > outerlist.append([4,5,6]) > > > print (outerlist[1][1]) # prints 5 > >> type of frame with a + or a - to signify the direction of the read >> (+1,+2, >> +3, -1, -2, -3), > > What does 'direction of the read mean'? > >> the numerical position of each start codon a > > Whats a codon? > > This is a list for programmers learning python. > Do not assume any specialized domain knowledge. > If you can explain the problem in terms of > plain English and basic math then we can probably help. > If you use terms that are either ambiguous or too > specialized we will be lost and unable to assist. > > The OP is asking about a representation of DNA sequences in Python. More specifically, his question is about protein coding segments of DNA that are transcribed into messenger RNA, then translated into protein using the so-called triplet code (three RNA nucleotides are encoding one amino acid of the protein). So the question involves a lot of basic biology (Alan's questions: since three nucleotides stand for one amino acid there are three different reading frames for any messenger RNA starting with the first, second and third nucleotide, respectively; the orignal DNA is an anti-parallel double strand and either strand may be transcribed, so 'direction' refers to this; a codon is a specific triplet of nucleotides). Ani: While I could help you with your question, I agree with Peter that you need to define much more clearly what you are trying to do before we can discuss any code here. In principle, he's also right that you should look into biopython because that package might well be the right thing for you, but that, again, is hard to tell without knowing the details of what you are trying to do. Best wishes, Wolfgang From duxbuz at hotmail.com Tue May 20 10:25:48 2014 From: duxbuz at hotmail.com (Ian D) Date: Tue, 20 May 2014 08:25:48 +0000 Subject: [Tutor] While truth Message-ID: I was reading a tutorial that had these examples in it: >>> while False: print("False is the new True.") >>> while 6: print("Which numbers are True?") while -1: print("Which numbers are True?") while 0: print("Which numbers are True?") Unfortunately the author never explained these statements. I was wondering if the gist of a while statement could be explained in the context of these examples. e.g. while False: means while True is False, which is never True because True is of course True not False. but while 6: means..... err while 6 is True? and this is True because... err. Anyway I am a bit lost with this. Can anyone shed any light please? Thanks. From duxbuz at hotmail.com Tue May 20 11:36:54 2014 From: duxbuz at hotmail.com (Ian D) Date: Tue, 20 May 2014 09:36:54 +0000 Subject: [Tutor] While truth In-Reply-To: References: Message-ID: Or should I have said While False is True, which is never True, because False is False not True ---------------------------------------- > From: duxbuz at hotmail.com > To: tutor at python.org > Date: Tue, 20 May 2014 08:25:48 +0000 > Subject: [Tutor] While truth > > I was reading a tutorial that had these examples in it: > > >>>> while False: > > print("False is the new True.") > > >>>> while 6: > > print("Which numbers are True?") > > > while -1: > > print("Which numbers are True?") > > > while 0: > > print("Which numbers are True?") > > > > Unfortunately the author never explained these statements. > > > I was wondering if the gist of a while statement could be explained in the context of these examples. > > > e.g. while False: > > > means while True is False, which is never True because True is of course True not False. > > > but while 6: > > > means..... err while 6 is True? and this is True because... err. > > > Anyway I am a bit lost with this. > > > Can anyone shed any light please? > > > Thanks. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From dpalao.python at gmail.com Tue May 20 11:37:42 2014 From: dpalao.python at gmail.com (David Palao) Date: Tue, 20 May 2014 11:37:42 +0200 Subject: [Tutor] While truth In-Reply-To: References: Message-ID: Hi, 6, -1 or 0 are not bools (True or False): >>> 6 is True False >>> 0 is False False If you had to design a language and want to think about using numbers in a logical context you could do at least two things: 1) convert the number to bool, ie define a set of rules to assign to each number a logical value, or 2) don't convert and raise an error. In python, like in many other languages, option 1) has been chosen. The rules are roughly: when using a number in a logical context 0 is casted to False, and the other numbers are considered True. The "while" statement expects an expression that returns a logical value. Put both things together and I think you get your answer, if I well understood. Best 2014-05-20 10:25 GMT+02:00 Ian D : > I was reading a tutorial that had these examples in it: > > >>>> while False: > > print("False is the new True.") > > >>>> while 6: > > print("Which numbers are True?") > > > while -1: > > print("Which numbers are True?") > > > while 0: > > print("Which numbers are True?") > > > > Unfortunately the author never explained these statements. > > > I was wondering if the gist of a while statement could be explained in the context of these examples. > > > e.g. while False: > > > means while True is False, which is never True because True is of course True not False. > > > but while 6: > > > means..... err while 6 is True? and this is True because... err. > > > Anyway I am a bit lost with this. > > > Can anyone shed any light please? > > > Thanks. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From cs at zip.com.au Tue May 20 12:00:12 2014 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 20 May 2014 20:00:12 +1000 Subject: [Tutor] While truth In-Reply-To: References: Message-ID: <20140520100012.GA79201@cskk.homeip.net> On 20May2014 08:25, Ian D wrote: >I was reading a tutorial that had these examples in it: > >>>> while False: > print("False is the new True.") > >>>> while 6: > print("Which numbers are True?") > >while -1: > print("Which numbers are True?") > >while 0: > print("Which numbers are True?") > >Unfortunately the author never explained these statements. That is a pity. Sounds badly written. I would imagine the intent is that you could try these and see what happens. I think that exercise would be more effective with if-statements instead of while-statements. Basicly, the point is likely to show that you do not need to use a "bool" as the while condition; any value considered "truthy" by Python will do if it matches what you are working with. Broadly, None and 0 and False and "empty" collections (empty lists, empty sets, zero length strings, etc) are "false", and most other things are "true". Cheers, Cameron Simpson Rugby is a beastly game played by gentlemen; soccer is a gentleman's game played by beasts; football is a beastly game played by beasts. - Henry Blaha From steve at pearwood.info Tue May 20 14:46:23 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 20 May 2014 22:46:23 +1000 Subject: [Tutor] While truth In-Reply-To: References: Message-ID: <20140520124623.GD10355@ando> On Tue, May 20, 2014 at 08:25:48AM +0000, Ian D wrote: > I was reading a tutorial that had these examples in it: > > >>> while False: > > print("False is the new True.") [... snip examples ...] > I was wondering if the gist of a while statement could be explained in > the context of these examples. > > e.g. while False: > > means while True is False, which is never True because True is of > course True not False. > > but while 6: > > means..... err while 6 is True? and this is True because... err. Not quite "6 is True", except figuratively speaking. It might be easier to consider if...else rather than while, although the rules are exactly the same. An if...else block looks at a value, and then decides which branch to take: the "if" part or the "else" part. So we might have something like: if condition: do_this() else: do_that() While-loops are similar, except that they repeat so long as the condition is a true value, rather than just once. while condition: do_this() Now, why did I say "the condition is a true value" instead of "the condition is True"? To explain, let me contrast Python with some other programming languages. In some programming languages, the condition in an if or while statement is restricted to one of exactly two values, usually called True and False. These values are called "Booleans" (named after the mathematician who first worked on them, George Boole) or just "bools". Any other value, like 6, or None, or "Hello World", is an error. In those languages, "if condition" will take one branch if condition is True, and the other branch if False, and there are no other possibilities: if condition: print("condition is True") else: print("condition is False") *Python is not like this.* In languages like Python, the value being tested can have many different values. We can describe this in various ways: # This is what Python does. if condition: print("condition is a truthy value") print("condition is true-ish") print("in a boolean context, condition is true") print("condition is something") else: print("condition is a falsey value") print("condition is false-ish") print("in a boolean context, condition is false") print("condition is nothing") Think of this as "duck-typing for bools". Most of the time, we don't care if condition is *actually* True or False, only whether it is true-like or false-like. Many other languages do something similar to this, e.g. Javascript, PHP, Ruby, and so on. Notice that I use (big T) True and (big F) False to refer to the actual Python constants True and False, and (small t) true and (small f) false to refer to things which are truthy or falsey. Back to our if...else statement, or while statement: the condition doesn't have to be an actual bool True or False, it can be any value at all. Try running this bit of code and see what it prints: for condition in ("Hello World", 23, [], "", None, 1.5, 0.0, [1, 2, 3]): if condition: print("%r is a truthy value" % condition) else: print("%r is a falsey value" % condition) Can you see the pattern? I suggest you run the above before reading on. Don't worry, I'll wait... W E A R E W A I T I N G Can you see the pattern? In Python, values which represent "something" are considered truthy. Those which represent "nothing" or "empty" are considered falsey. So we have: Falsey values: None 0 0.0 empty string "" empty list [] empty tuple () empty dict {} and of course False Truthy values: any non-zero integer, like 1, 2, -3, ... any non-zero float, like 2.5, 17.8, -100.1, ... any non-empty string, like "spam", "eggs", ... any non-empty list, like [1, 2, 3] any non-empty tuple, like (0, 1) any non-empty dict, like {23: "twenty-three"} and of course True 99% of the time, you shouldn't care whether something is actually True or False. Well, perhaps 90% of the time. But, if you do care, you can convert any object you like to True or False by calling bool() on it: bool(None) => returns False, because None is falsey bool(101) => returns True, because 101 is truthy What do you think bool("False") will return? Since it's a string, and it is not the empty string "", it will return True. If you need to convert the string "False" to False, you need to test for it yourself. A few words of advice. Never write something like this: while bool(value): ... since the call to bool() is redundant. Python already checks to see whether value is truthy, calling bool() just does it twice. But even worse is this: while bool(value) is True: ... That just displays unfamiliarity with boolean logic and makes you look ignorant. bool(value) will return True or False, so comparing it to True is redundant. If you don't trust that, where do you stop? while bool(value): ... while bool(value) is True: ... while (bool(value) is True) is True: ... while ((bool(value) is True) is True) is True: ... while (((bool(value) is True) is True) is True) is True: ... while ((((bool(value) is True) is True) is True) is True) is True: ... while (((((bool(value) is True) is True) is True) is True) is True) is True: ... -- Steven From rsm at imap.cc Tue May 20 14:33:50 2014 From: rsm at imap.cc (rsm) Date: Tue, 20 May 2014 14:33:50 +0200 Subject: [Tutor] Return Variable from Function. Message-ID: <537B4BAE.5090608@imap.cc> Hello, (im using python 2.7) I've been having some problems to return the variable from a function so i decided to try to do it with with global variables, but the script is not working good. I've been reading tutorials but i just don't get how to do it and i would like to ask for some help >.< ( at the end, it's the part of the script that is not working, you can copy it to easily understand the problem) what i would like create a function that creates a window for taking a choice and returns a variable in order to proceed using the choice of that variable. ( i've removed the variable from the script posted below because i tried to do it with the global variables ) The problem that i have using the global variables is that the: if Prompt_Desition==1: if os.path.exists(Script_Location+"/program-data"): os.system("rm -r "+Script_Location+"/program-data") print "Program-data deleted" else: print "No program-data" if os.path.exists(Script_Location+"/computer-data"): os.system("rm -r "+Script_Location+"/computer-data") print "Computer-Data Deleted" else: print "No computer-data" Prompt_Desition=0 is executed when the main_window_gui() window is closed. i don't understand why. It should be executed when the def cancel_label_desition() is executed. because thePompt_desition variable changes to 1. Also i would like to ask if is there any way to put a definition with arguments on the command of a button? when i do this i get errors >.< i define message, proceed_label, cancel_label then: b100 = Button(window1, text="Delete",command=prompt_desition(message, proceed_label, cancel_label)) b100.pack() ___ Thanks so much for helping me! I appreciate it a lot !! ___ import Tkinter, tkFileDialog, os, csv, shutil, time, getpass, pwd from os import stat from Tkinter import * ################## Global Variables #################### Script_Location=os.path.dirname(os.path.realpath(__file__)) Script_Title="Fresh Install v1.1" Prompt_Desition=0 ################################################# def prompt_desition(message, proceed_label, cancel_label): "Display a window with the selected message with two buttons in way to take a desition. If cancel_label: return_var=0 , if proceed_label: return_var=1." def cancel_label_desition(): prompt_window.destroy() def proceed_label_desition(): global Prompt_Desition Prompt_Desition=1 prompt_window.destroy() prompt_window=Tk() prompt_window.configure() prompt_window.wm_title() prompt_window.resizable(0,0) lb1 = Label(prompt_window, text=message) lb1.pack() button_proceed_label = Button(prompt_window, text=proceed_label,command=proceed_label_desition) button_proceed_label.pack() button_cancel_label = Button(prompt_window, text=cancel_label,command=cancel_label_desition) button_cancel_label.pack() prompt_window.mainloop() def delete_program_data(): "Delete all the data stored by this program" global Prompt_Desition message="Do you want to delete all the data stored by this program?" prompt_desition(message, "Proceed", "Cancel") if Prompt_Desition==1: if os.path.exists(Script_Location+"/program-data"): os.system("rm -r "+Script_Location+"/program-data") print "Program-data deleted" else: print "No program-data" if os.path.exists(Script_Location+"/computer-data"): os.system("rm -r "+Script_Location+"/computer-data") print "Computer-Data Deleted" else: print "No computer-data" Prompt_Desition=0 def main_window_gui(): window1=Tk() window1.wm_title(Script_Title) window1.resizable(0,0) # dont resize b100 = Button(window1, text="Delete",command=delete_program_data) b100.pack() window1.mainloop() main_window_gui() -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue May 20 15:15:55 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 20 May 2014 14:15:55 +0100 Subject: [Tutor] Return Variable from Function. In-Reply-To: <537B4BAE.5090608@imap.cc> References: <537B4BAE.5090608@imap.cc> Message-ID: On 20/05/14 13:33, rsm wrote: > I've been having some problems to return the variable from a function so > i decided to try to do it with with global variables, That's usually a bad idea. However, what you have not shown us is what you did try that didn't work. (Unless thats what the snippet below is?) In principle its easy you just type return variable and the function terminates at that point and returns the variable. > what i would like create a function that creates a window for taking a > choice and returns a variable in order to proceed using the choice of > that variable. However, that is usually a standard feature of any GUI toolkit so you should probably look a little closer at your widget set and seee if three are any standard dialogs available. You don;t say which GUI toolkit you are using but if it were Tkinter (and it looks like it may be) you'd do something like (for python3) import tkinter.messagebox as mbox or (for Python2) import tkmessagebox result = mbox.askyesno("Your title here", "Your yes/no question here") if result: print ("You said yes!") else: print("Sorry, no go") With several other options available... You can see more in my GUI topic of my tutor...(look in Version 2) Most GUIs will have similar facilities. > if Prompt_Desition==1: > > if os.path.exists(Script_Location+"/program-data"): > os.system("rm -r "+Script_Location+"/program-data") > print "Program-data deleted" > else: > print "No program-data" You probably shouldn't mix print statements and GUI input it tends to be look confusing. GUI output from text input is often OK but if the user is interacting with a GUI they tend not to expect output in a terminal. > > if os.path.exists(Script_Location+"/computer-data"): > os.system("rm -r "+Script_Location+"/computer-data") > print "Computer-Data Deleted" > else: > print "No computer-data" > > Prompt_Desition=0 > > is executed when the main_window_gui() window is closed. i don't > understand why. See below > b100 = Button(window1, text="Delete",command=prompt_desition(message, > proceed_label, cancel_label)) > b100.pack() Here you are calling the function prompt_desition() rather than referencing it. Also the button command function must not take any a.rguments They way rtound that is to do this: > b100 = Button(window1, text="Delete", command=lambda m=message, pl=proceed_label, cl=cancel_label : prompt_desition(m,pl,cl)) > def prompt_desition(message, proceed_label, cancel_label): > "Display a window with the selected message with two buttons in way > to take a desition. If cancel_label: return_var=0 , if proceed_label: > return_var=1." those should probably be triple quotes... > def cancel_label_desition(): > prompt_window.destroy() > > def proceed_label_desition(): > global Prompt_Desition > Prompt_Desition=1 > prompt_window.destroy() > > prompt_window=Tk() > prompt_window.configure() > prompt_window.wm_title() > prompt_window.resizable(0,0) > > lb1 = Label(prompt_window, text=message) > lb1.pack() > > button_proceed_label = Button(prompt_window, > text=proceed_label,command=proceed_label_desition) > button_proceed_label.pack() > > button_cancel_label = Button(prompt_window, > text=cancel_label,command=cancel_label_desition) > button_cancel_label.pack() > > prompt_window.mainloop() > > > def delete_program_data(): > "Delete all the data stored by this program" > > global Prompt_Desition > > message="Do you want to delete all the data stored by this program?" > prompt_desition(message, "Proceed", "Cancel") > > if Prompt_Desition==1: > > if os.path.exists(Script_Location+"/program-data"): > os.system("rm -r "+Script_Location+"/program-data") > print "Program-data deleted" > else: > print "No program-data" > > if os.path.exists(Script_Location+"/computer-data"): > os.system("rm -r "+Script_Location+"/computer-data") > print "Computer-Data Deleted" > else: > print "No computer-data" > > Prompt_Desition=0 > > > def main_window_gui(): > > window1=Tk() > window1.wm_title(Script_Title) > window1.resizable(0,0) # dont resize > > > b100 = Button(window1, text="Delete",command=delete_program_data) > b100.pack() > > window1.mainloop() > > > main_window_gui() > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Tue May 20 17:04:26 2014 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Tue, 20 May 2014 16:04:26 +0100 (BST) Subject: [Tutor] Return Variable from Function. In-Reply-To: <537B6764.9080008@imap.cc> References: <537B4BAE.5090608@imap.cc> <537B6764.9080008@imap.cc> Message-ID: <1400598266.30346.YahooMailNeo@web186003.mail.ir2.yahoo.com> >Thanks so much for your answer Alan! it gave me something to work with :), unfortunately it stills not working :/ > >I've made several comments in the code... ? ? ? ? ? >def prompt_desition(message, proceed_label, cancel_label,return_value): >??? "Display a window with the selected message with two buttons in way to take a desition. If cancel_label: return_var=0 , if proceed_label: return_var=1." >? >??? def cancel_label_desition(return_value): >??????? prompt_window.destroy() >??????? return_value=0 >??????? >??????? print "no" >??????? >??????? return return_value >????????????? >??? def proceed_label_desition(return_value): >??????? prompt_window.destroy() >??????? return_value=1 >??????? >??????? print "yes" >??????? >??????? return return_value > >These two functions should probably be left outside your prompt_desition function.? There is no real need to define these every time this function is called. They don't change. Also note that although they both define return_value and indeed return it that does? not make the variable known to the outer function, it is still only local to the two functions. ? ? prompt_window=Tk() > >You already have a top level window (Tk) so you should either use it or instead create? a dialog window using Toplevel() instead of Tk() ? ? button_proceed_label = Button(prompt_window, text=proceed_label,command=lambda :proceed_label_desition(return_value)) > >The lambda calls the function with a return_value argument but return_value? is not known at this point. It is not a defined name within this functions scope. ? ? button_cancel_label = Button(prompt_window, text=cancel_label,command=lambda :cancel_label_desition(return_value)) > > >Same here ? ? prompt_window.mainloop() > > > >You really only want a single mainloop() running in your GUI, and that's? >the top level one defined in main() >def delete_program_data(): >??? "Delete all the data stored by this program" >? ? message="Do you want to delete all the data stored by this program?" >??? prompt_desition(message, "Proceed", "Cancel", "choice") > >You shouldn't really mix event handler/command functions with functions you call directly.? It can get awfully confusing. Sometimes it works but often it gets messy. Better to define? the general function and then have an event handler call that indirectly. (Or use a lambda such as you are almost doing...) But the real issue here is that you are not storing the return value from the? prompt_desition() function anywhere, so what is your choice (in the next line)? based on? ??? if choice==1: >????? >??????? if os.path.exists(Script_Location+"/program-data"): >??????????? os.system("rm -r "+Script_Location+"/program-data") >??????????? print "Program-data deleted" >??????? else: >??????????? print "No program-data" >? >??????? if os.path.exists(Script_Location+"/computer-data"): >??????????? os.system("rm -r "+Script_Location+"/computer-data") >??????????? print "Computer-Data Deleted" >??????? else: >??????????? print "No computer-data" >??? ??? > >? I think you need to rethink the structure of your GUI. Lookingt at the code below it defines a window containing nothing? but a single button. That button then calls another function which? then builds another window. Can you think of any popular GUI? application that works like that? I can't. Try to envision what the GUI should look and feel like to the user,? Then build your code around that. Think about the data displayed? and the ?actions that your user will do to perform work. Then link those actions to the functions that perform it. This design feels like you are trying to build a text oriented? CLI using a GUI rather than design an event driven true GUI program. def main_window_gui(): > >??? window1=Tk() >??? window1.wm_title() >??? window1.resizable(0,0) # dont resize >??? >??? b100 = Button(window1, text="Delete", command=delete_program_data) >??? b100.pack() > >??? window1.mainloop() > > >main_window_gui() > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Tue May 20 20:00:41 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 20 May 2014 11:00:41 -0700 Subject: [Tutor] While truth In-Reply-To: References: Message-ID: On Tue, May 20, 2014 at 1:25 AM, Ian D wrote: > I was reading a tutorial that had these examples in it: > > >>>> while False: > print("False is the new True.") > > >>>> while 6: > print("Which numbers are True?") > > > while -1: > print("Which numbers are True?") > > > while 0: > print("Which numbers are True?") > > Unfortunately the author never explained these statements. The statements above are trying to talk about what Python considers to be "true". In some languages, there is a single distinguished true value. Python chooses a broader definition that allows everything to be considered true, with the exception of the following values: False None Numeric zero Empty collection Empty string Reference: https://docs.python.org/3/reference/expressions.html#booleans We care about what values are true, because they are the switch that controls which way we're flowing through a conditional statement like "if" or "while". As people are pointing out, the examples above are a bit disappointing. They are demonstrating this with infinite while loops, and that's probably not a great idea because the output will be so overwhelming to be actively distracting. From illusiontechniques at gmail.com Tue May 20 20:44:41 2014 From: illusiontechniques at gmail.com (C Smith) Date: Tue, 20 May 2014 14:44:41 -0400 Subject: [Tutor] While truth In-Reply-To: References: Message-ID: You can test out a condition like this in IDLE like so: while 6: print "yes its true" break while 0: print "yes its true" break while -1: print "yes its true" break emptyList = [] while emtpyList: print "yes its true" break This way you don't have to deal with an infinite loop. It will print "yes its true" once, if it IS true and then "break" will break you out of the loop. On Tue, May 20, 2014 at 2:00 PM, Danny Yoo wrote: > On Tue, May 20, 2014 at 1:25 AM, Ian D wrote: >> I was reading a tutorial that had these examples in it: >> >> >>>>> while False: >> print("False is the new True.") >> >> >>>>> while 6: >> print("Which numbers are True?") >> >> >> while -1: >> print("Which numbers are True?") >> >> >> while 0: >> print("Which numbers are True?") >> >> Unfortunately the author never explained these statements. > > > The statements above are trying to talk about what Python considers to > be "true". In some languages, there is a single distinguished true > value. Python chooses a broader definition that allows everything to > be considered true, with the exception of the following values: > > False > None > Numeric zero > Empty collection > Empty string > > Reference: https://docs.python.org/3/reference/expressions.html#booleans > > We care about what values are true, because they are the switch that > controls which way we're flowing through a conditional statement like > "if" or "while". > > As people are pointing out, the examples above are a bit > disappointing. They are demonstrating this with infinite while loops, > and that's probably not a great idea because the output will be so > overwhelming to be actively distracting. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From illusiontechniques at gmail.com Tue May 20 20:50:35 2014 From: illusiontechniques at gmail.com (C Smith) Date: Tue, 20 May 2014 14:50:35 -0400 Subject: [Tutor] While truth In-Reply-To: References: Message-ID: Of course that isn't very useful code. I thought it might be a useful quick test for someone learning how while loops treat different values. On Tue, May 20, 2014 at 2:46 PM, Danny Yoo wrote: > On Tue, May 20, 2014 at 11:44 AM, C Smith wrote: >> You can test out a condition like this in IDLE like so: >> while 6: >> print "yes its true" >> break >> >> >> while 0: >> print "yes its true" >> break >> >> >> while -1: >> print "yes its true" >> break >> >> >> emptyList = [] >> while emtpyList: >> print "yes its true" >> break >> >> This way you don't have to deal with an infinite loop. >> It will print "yes its true" once, if it IS true and then "break" will >> break you out of the loop. > > > That being said, if we're going to go through these contortions, we > might as well use "if", right? :P > > > The infinite loops in the examples above are a complete distraction > from the main point, I think. From dyoo at hashcollision.org Tue May 20 20:46:55 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 20 May 2014 11:46:55 -0700 Subject: [Tutor] While truth In-Reply-To: References: Message-ID: On Tue, May 20, 2014 at 11:44 AM, C Smith wrote: > You can test out a condition like this in IDLE like so: > while 6: > print "yes its true" > break > > > while 0: > print "yes its true" > break > > > while -1: > print "yes its true" > break > > > emptyList = [] > while emtpyList: > print "yes its true" > break > > This way you don't have to deal with an infinite loop. > It will print "yes its true" once, if it IS true and then "break" will > break you out of the loop. That being said, if we're going to go through these contortions, we might as well use "if", right? :P The infinite loops in the examples above are a complete distraction from the main point, I think. From wheelerg at seattleu.edu Tue May 20 20:00:43 2014 From: wheelerg at seattleu.edu (Wheeler, Gabriel) Date: Tue, 20 May 2014 18:00:43 +0000 Subject: [Tutor] Main function Message-ID: <1400608842163.37295@seattleu.edu> I am a beginner at python programming and right now we have to write the text to design a program that will make a histogram for Benford's law which says that in a natural set of data 1 will appear more than 2 which will appear more than 3 and so on. So given a set of data I want a list showing how many times each number appears. Since I'm new to python and don't really know how to write programs yet, my first question would be what exactly is the main function, because we did a similar assignment before this one that included it, and I'm not sure what exactly it does. Gabe Wheeler (512)964-5421 -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Tue May 20 21:28:00 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 20 May 2014 12:28:00 -0700 Subject: [Tutor] Main function In-Reply-To: <1400608842163.37295@seattleu.edu> References: <1400608842163.37295@seattleu.edu> Message-ID: > Since I'm new to python and don't really know how to write programs yet, my > first question would be what exactly is the main function, because we did a > similar assignment before this one that included it, and I'm not sure what > exactly it does. When you write a program, you write a collection of function and variable definitions. But they don't have to be in too particular of an order. For example, we might have something like ######################### def f(x): return x * x def g(x): return x**0.5 def h(a, b): return g(f(a) + f(b)) ######################### But nothing stops us from writing the definitions in a different order: ######################### def h(a, b): return g(f(a) + f(b)) def g(x): return x**0.5 def f(x): return x * x ######################### However, it does leave the question of: how do you start a program, and from where do things begin? In most programming languages, you choose a particular function and designate it as the "main" entry point into your program. ######################### def h(a, b): return g(f(a) + f(b)) def g(x): return x**0.5 def f(x): return x * x def main(): print("The hypotenuse is: %d" % h(3, 4)) ######################### In Python, you don't have to call it "main", but in several other languages, you do. There's one more piece you add to the bottom of the program to start things going, a snippet that looks like this: if __name__ == '__main__': main() This is a purely Python issue. It says that if the file is being run as the main program (in which case, the __name__ is '__main__'), then kick things off by calling the main function. You put this at the bottom of your program. ######################### def h(a, b): return g(f(a) + f(b)) def g(x): return x**0.5 def f(x): return x * x def main(): print("The hypotenuse is: %d" % h(3, 4)) if __name__ == '__main__': main() ######################### See: http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm From mccombs at imperium.org Tue May 20 22:00:22 2014 From: mccombs at imperium.org (P McCombs) Date: Tue, 20 May 2014 13:00:22 -0700 Subject: [Tutor] How to create web interface? Message-ID: On May 14, Danny Yoo wrote: > > Another option might be to turn your program into a web site, so that > > the interface is the web browser, which everyone is getting used to > > these days. But this, too, is also... involved. :P I have a little volunteer scheduling application I've written as a module, with about a dozen functions, that reads and writes to a Sqlite database. I'd like to run a web server on my machine just for my local use. I don't understand how precisely the web page would communicate with the python program. If anyone has a suggestion on specific modules, and tutorials to use to create a web interface for a python program, I'd love to hear it. I'm using python 2.7 on OSX Mavericks. I'm fairly new to python. I have some experience with apache, and php, I comfortable with HTML. I'm also happy to know where a better forum for this question would be. Thank you, Paul McCombs From amonroe at columbus.rr.com Tue May 20 22:22:51 2014 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Tue, 20 May 2014 16:22:51 -0400 Subject: [Tutor] How to create web interface? In-Reply-To: References: Message-ID: <7110484311.20140520162251@columbus.rr.com> > I don't understand how precisely the web page would communicate with > the python program. In the simplest case, the webserver software executes your script. Whatever you print() _is_ the webpage. The webserver sends whatever you print() to the user's browser. http://en.wikipedia.org/wiki/Common_Gateway_Interface Alan From dyoo at hashcollision.org Tue May 20 22:41:47 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 20 May 2014 13:41:47 -0700 Subject: [Tutor] How to create web interface? In-Reply-To: References: Message-ID: > I don't understand how precisely the web page would communicate with > the python program. Whenever you're communicating to a web site, you are contacting a live program, a "web server". In typical usage, a web server delivers static files from its file system. But it doesn't have to be that way. The server is running an arbitrary program. When we use the term "web page", we're hiding a lot of the fundamental, dynamic details: there's a program that's backing the generation of a web page. See: https://developers.google.com/appengine/docs/python/gettingstartedpython27/helloworld for example. In contrast to traditional programs, in web programs, the "entry point" is no longer a main function. Instead, your program is continuously running, and you're in a loop that responds to web requests. In the example above, the loop is implicit, because the framework is doing the looping for us, and calling into our code for each request. Other frameworks make the loop a lot more explicit. e.g. https://docs.python.org/2/library/basehttpserver.html#more-examples where you can see that there's a while loop that handles "requests". A "web request" will happen when someone is visiting your "web page", or when you're submitting a form to the "web page". Those requests hit the server, and that's where you can do something to interact with the user. You treat the values of form elements as if they were parameters, and you treat the request handling as if it were a regular function call. The output of the "function call" will be the response that's sent back to the web browser. From wprins at gmail.com Wed May 21 00:27:42 2014 From: wprins at gmail.com (Walter Prins) Date: Tue, 20 May 2014 23:27:42 +0100 Subject: [Tutor] How to create web interface? In-Reply-To: References: Message-ID: Hi, On 20 May 2014 21:00, P McCombs wrote: > On May 14, Danny Yoo wrote: >> > Another option might be to turn your program into a web site, so that >> > the interface is the web browser, which everyone is getting used to >> > these days. But this, too, is also... involved. :P > > I have a little volunteer scheduling application I've written as a > module, with about a dozen functions, that reads and writes to a > Sqlite database. I'd like to run a web server on my machine just for > my local use. > > I don't understand how precisely the web page would communicate with > the python program. > > If anyone has a suggestion on specific modules, and tutorials to use > to create a web interface for a python program, I'd love to hear it. You might want to work through this: http://pythonpaste.org/do-it-yourself-framework.html Or this, a newer version: http://docs.webob.org/en/latest/do-it-yourself.html (but it contains a lot more detail and a lot more advanced Python concepts...) Alternatively you can have a look at one of the many small and micro web frameworks available for Python, e.g web2py, flask, bottle, cherrypy etc. Links: http://www.web2py.com/ (tutorial: http://mherman.org/blog/2012/11/27/crash-course-in-web2py-part-1/#.U3vVpvldVSk also: http://web2py.com/books/default/chapter/29/01/introduction) http://flask.pocoo.org/ (tutorial: http://flask.pocoo.org/docs/tutorial/ also: http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world ) http://bottlepy.org/docs/dev/index.html (tutorial: http://bottlepy.org/docs/dev/tutorial.html) http://www.cherrypy.org/ (tutorial: http://docs.cherrypy.org/en/latest/tutorial/index.html) Cherrypy is perhaps a nice one for you to start with. Hope that helps! Walter From alan.gauld at btinternet.com Wed May 21 01:36:12 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 21 May 2014 00:36:12 +0100 Subject: [Tutor] How to create web interface? In-Reply-To: References: Message-ID: On 20/05/14 21:00, P McCombs wrote: > On May 14, Danny Yoo wrote: >>> Another option might be to turn your program into a web site, so that >>> the interface is the web browser, which everyone is getting used to >>> these days. But this, too, is also... involved. :P > > I have a little volunteer scheduling application I've written as a > module, with about a dozen functions, that reads and writes to a > Sqlite database. I'd like to run a web server on my machine just for > my local use. > > I don't understand how precisely the web page would communicate with > the python program. If you want to understand the basics then the cgi module in the standard lib is a good starting point. It makes life a bit easier than nothing but exposes the underlying technology. But if you want something to keep you sane and make web programming easy then Pyhon has more web frameworks than you can shake a stick at. From very simple (Flask, CherryPy) to very complex (Django, Zope). There is a good page on the python.org web site that summarises the technology and many of the most popular frameworks but ultimately you just pick one and work through its tutorial. Start here for theory: https://docs.python.org/3/howto/webservers.html and go here for the list: https://wiki.python.org/moin/WebFrameworks HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From mccombs at imperium.org Wed May 21 02:41:16 2014 From: mccombs at imperium.org (P McCombs) Date: Tue, 20 May 2014 17:41:16 -0700 Subject: [Tutor] How to create web interface? In-Reply-To: References: Message-ID: Thank you all for your responses. This is exactly the information I was looking for. Paul McCombs On Tue, May 20, 2014 at 4:36 PM, Alan Gauld wrote: > On 20/05/14 21:00, P McCombs wrote: >> >> On May 14, Danny Yoo wrote: >>>> >>>> Another option might be to turn your program into a web site, so that >>>> the interface is the web browser, which everyone is getting used to >>>> these days. But this, too, is also... involved. :P >> >> >> I have a little volunteer scheduling application I've written as a >> module, with about a dozen functions, that reads and writes to a >> Sqlite database. I'd like to run a web server on my machine just for >> my local use. >> >> I don't understand how precisely the web page would communicate with >> the python program. > > > If you want to understand the basics then the cgi module in the standard lib > is a good starting point. It makes life a bit > easier than nothing but exposes the underlying technology. > > But if you want something to keep you sane and make web > programming easy then Pyhon has more web frameworks than > you can shake a stick at. From very simple (Flask, CherryPy) > to very complex (Django, Zope). > > There is a good page on the python.org web site that summarises > the technology and many of the most popular frameworks but > ultimately you just pick one and work through its tutorial. > > Start here for theory: > > https://docs.python.org/3/howto/webservers.html > > and go here for the list: > > https://wiki.python.org/moin/WebFrameworks > > > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From questions.anon at gmail.com Wed May 21 05:22:25 2014 From: questions.anon at gmail.com (questions anon) Date: Wed, 21 May 2014 13:22:25 +1000 Subject: [Tutor] Find Daily max - create lists using date and add hourly data to that list for the day Message-ID: I have hourly 2D temperature data in a monthly netcdf and I would like to find the daily maximum temperature. The shape of the netcdf is (744, 106, 193) I would like to use the year-month-day as a new list name (i.e. 2009-03-01, 2009-03-02....2009-03-31) and then add each of the hours worth of temperature data to each corresponding list. Therefore each new list should contain 24 hours worth of data and the shape should be (24,106,193) . This is the part I cannot seem to get to work. I am using datetime and then groupby to group by date but I am not sure how to use the output to make a new list name and then add the data for that day into that list. see below and attached for my latest attempt. Any feedback will be greatly appreciated. from netCDF4 import Dataset import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap from netcdftime import utime from datetime import datetime as dt import os import gc from numpy import * import pytz from itertools import groupby MainFolder=r"/DATA/2009/03" dailydate=[] alltime=[] lists={} ncvariablename='T_SFC' for (path, dirs, files) in os.walk(MainFolder): for ncfile in files: print ncfile fileext='.nc' if ncfile.endswith(ncvariablename+'.nc'): print "dealing with ncfiles:", path+ncfile ncfile=os.path.join(path,ncfile) ncfile=Dataset(ncfile, 'r+', 'NETCDF4') variable=ncfile.variables[ncvariablename][:,:,:] TIME=ncfile.variables['time'][:] ncfile.close() for temp, time in zip((variable[:]),(TIME[:])): cdftime=utime('seconds since 1970-01-01 00:00:00') ncfiletime=cdftime.num2date(time) timestr=str(ncfiletime) utc_dt = dt.strptime(timestr, '%Y-%m-%d %H:%M:%S') au_tz = pytz.timezone('Australia/Sydney') local_dt = utc_dt.replace(tzinfo=pytz.utc).astimezone(au_tz) alltime.append(local_dt) for k, g in groupby(alltime, key=lambda d: d.date()): kstrp_local=k.strftime('%Y-%m-%d_%H') klocal_date=k.strftime('%Y-%m-%d') dailydate.append(klocal_date) for n in dailydate: lists[n]=[] lists[n].append(temp) big_array=np.ma.concatenate(lists[n]) DailyTemp=big_array.max(axis=0) -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: DailyMaxtemp_help.py Type: text/x-python-script Size: 1888 bytes Desc: not available URL: From bothenta at gmail.com Wed May 21 12:32:34 2014 From: bothenta at gmail.com (1 2) Date: Wed, 21 May 2014 18:32:34 +0800 Subject: [Tutor] How do I create a "loop until" looping structure? In-Reply-To: References: Message-ID: Hi there, As shown in the codes below, I want to end the loop until the s is no greater than -5 but I found there is no "do... loop until..." like C so what should I do? I will write it in C's style s,n = 0,1 do: import math s=s+(math.log((n+1)/(n+2))/math.log(2)) loop until s < -5 print(s) -------------- next part -------------- An HTML attachment was scrubbed... URL: From k-shweta at hcl.com Wed May 21 14:39:01 2014 From: k-shweta at hcl.com (Shweta Kaushik) Date: Wed, 21 May 2014 12:39:01 +0000 Subject: [Tutor] Set values from list as per user input Message-ID: <7955538EC35A414B9B0A5DDD7DA0404716D8EA32@chn-hclt-mbs08.HCLT.CORP.HCL.IN> Hi All, I am new to python. Please help me. I have to create one function which can set values sent by user from a list of values. For eg: I have a list having values between 1 to 100. List = ['1', '2', ... '100'] I have to write a function to set values from the list based on user input. If user input = 15 in one script, then it should enter this function, take value 15 from list and set this value. How to do this?? Please help Thanks & Regards, Shweta ::DISCLAIMER:: ---------------------------------------------------------------------------------------------------------------------------------------------------- The contents of this e-mail and any attachment(s) are confidential and intended for the named recipient(s) only. E-mail transmission is not guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or may contain viruses in transmission. The e mail and its contents (with or without referred errors) shall therefore not attach any liability on the originator or HCL or its affiliates. Views or opinions, if any, presented in this email are solely those of the author and may not necessarily reflect the views or opinions of HCL or its affiliates. Any form of reproduction, dissemination, copying, disclosure, modification, distribution and / or publication of this message without the prior written consent of authorized representative of HCL is strictly prohibited. If you have received this email in error please delete it and notify the sender immediately. Before opening any email and/or attachments, please check them for viruses and other defects. ---------------------------------------------------------------------------------------------------------------------------------------------------- From amonroe at columbus.rr.com Wed May 21 15:39:07 2014 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Wed, 21 May 2014 09:39:07 -0400 Subject: [Tutor] Set values from list as per user input In-Reply-To: <7955538EC35A414B9B0A5DDD7DA0404716D8EA32@chn-hclt-mbs08.HCLT.CORP.HCL.IN> References: <7955538EC35A414B9B0A5DDD7DA0404716D8EA32@chn-hclt-mbs08.HCLT.CORP.HCL.IN> Message-ID: <186473280.20140521093907@columbus.rr.com> > take value 15 from list Hint: use square brackets to choose a particular item from a list. test = ['first', 'second', 'third'] test[0] would refer to 'first' for example. Alan From alan.gauld at btinternet.com Wed May 21 15:41:10 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 21 May 2014 14:41:10 +0100 Subject: [Tutor] How do I create a "loop until" looping structure? In-Reply-To: References: Message-ID: On 21/05/14 11:32, 1 2 wrote: > Hi there, > As shown in the codes below, I want to end the loop until the s is no > greater than -5 but I found there is no "do... loop until..." like C so > what should I do? I will write it in C's style Normally you just reverse the logic and use a while loop instead. > s,n = 0,1 > do: > import math > s=s+(math.log((n+1)/(n+2))/math.log(2)) > loop until s < -5 import math s = 100 # or whatever while s >= -5: s = ..... print(s) But that doesn't work if the variant can not be suitably initialized before the loop starts - as is the case here. So instead you can use a while True with a break condition: import math s,n = 0,1 while True: s = ... if s < -5: break hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From gmschroeder at gmail.com Wed May 21 15:09:23 2014 From: gmschroeder at gmail.com (Greg Schroeder) Date: Wed, 21 May 2014 20:09:23 +0700 Subject: [Tutor] How do I create a "loop until" looping structure? In-Reply-To: References: Message-ID: <1400677763.11455.2.camel@greg-Lenovo-G480> https://wiki.python.org/moin/WhileLoop Greg On Wed, 2014-05-21 at 18:32 +0800, 1 2 wrote: > Hi there, > As shown in the codes below, I want to end the loop until the s is > no greater than -5 but I found there is no "do... loop until..." like > C so what should I do? I will write it in C's style > > s,n = 0,1 > do: > import math > s=s+(math.log((n+1)/(n+2))/math.log(2)) > loop until s < -5 > print(s) > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From wolfgang.maier at biologie.uni-freiburg.de Wed May 21 15:13:11 2014 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Wed, 21 May 2014 15:13:11 +0200 Subject: [Tutor] How do I create a "loop until" looping structure? In-Reply-To: References: Message-ID: <537CA667.6020803@biologie.uni-freiburg.de> On 21.05.2014 12:32, 1 2 wrote: > Hi there, > As shown in the codes below, I want to end the loop until the s is no > greater than -5 but I found there is no "do... loop until..." like C so > what should I do? I will write it in C's style > > s,n = 0,1 > do: > import math > s=s+(math.log((n+1)/(n+2))/math.log(2)) > loop until s < -5 > print(s) > Hi, there's no do .. until in Python, but while exists. So just invert your condition and use it at the top of a while loop (as you could do in C as well). Note also that your importing math inside the loop doesn't make sense. You want to import the module once in the beginning instead. Best, Wolfgang From davea at davea.name Wed May 21 15:48:36 2014 From: davea at davea.name (Dave Angel) Date: Wed, 21 May 2014 09:48:36 -0400 (EDT) Subject: [Tutor] Set values from list as per user input References: <7955538EC35A414B9B0A5DDD7DA0404716D8EA32@chn-hclt-mbs08.HCLT.CORP.HCL.IN> Message-ID: Shweta Kaushik Wrote in message: > Hi All, > > I am new to python. Please help me. > I have to create one function which can set values sent by user from a list of values. > > For eg: > I have a list having values between 1 to 100. > List = ['1', '2', ... '100'] > > I have to write a function to set values from the list based on user input. > If user input = 15 in one script, then it should enter this function, take value 15 from list and set this value. > > How to do this?? Could out quote the assignment exactly? It'd be a lot easier for you to solve if you had a clearer problem statement. Next tell us what version of Python your class is using. input () works differently between 2.x and 3.x Next you should show what's going to happen when this function works. Is the list always going to contain string versions of the counting numbers, in order? Or is it going to change while the program runs? When you "take" the value from the list is the list now one item shorter? And where is this value to be "put"? What value is to be returned? Show us code you've tried and tell us in what way is it wrong. -- DaveA From alan.gauld at btinternet.com Wed May 21 15:44:58 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 21 May 2014 14:44:58 +0100 Subject: [Tutor] Set values from list as per user input In-Reply-To: <7955538EC35A414B9B0A5DDD7DA0404716D8EA32@chn-hclt-mbs08.HCLT.CORP.HCL.IN> References: <7955538EC35A414B9B0A5DDD7DA0404716D8EA32@chn-hclt-mbs08.HCLT.CORP.HCL.IN> Message-ID: On 21/05/14 13:39, Shweta Kaushik wrote: > Hi All, > > I am new to python. Please help me. > I have to create one function which can set values sent by user from a list of values. > > For eg: > I have a list having values between 1 to 100. > List = ['1', '2', ... '100'] > > I have to write a function to set values from the list based on user input. > If user input = 15 in one script, then it should enter this function, > take value 15 from list and set this value. Sorry I didn't understand the last line at all. Which function should it enter? Where should the value be set? When you say "take 15 from list" what happens to the original list? Does it lose 15, so that 15 can only be used once? Or does the original list remain fully populated? Can you give a more detailed explanation? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From mydayfabu at gmail.com Wed May 21 17:50:27 2014 From: mydayfabu at gmail.com (fabu desay) Date: Wed, 21 May 2014 16:50:27 +0100 Subject: [Tutor] Option on How to create web Interface Message-ID: If I got the problem correctly,you can equally create + open a file file = open(filename,+wr) The file should have the HTML extension You can now pace your web contents in the file.html you created that should work by trigering the python file you programed in and it inturn creates the website or pages of your choice an example like this could help webpage = open('index.html', 'w+') webpage.write("My web page"); webpage.write("This the body"); webpage.close() From alan.gauld at btinternet.com Thu May 22 00:54:07 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 21 May 2014 23:54:07 +0100 Subject: [Tutor] Option on How to create web Interface In-Reply-To: References: Message-ID: On 21/05/14 16:50, fabu desay wrote: > If I got the problem correctly,you can equally create + open a file > file = open(filename,+wr) You wouldn't normally use +wr as a mode; that introduces a lot of complications. Usually you just open to read, or open to write. In your case w is all you need. > The file should have the HTML extension > You can now pace your web contents in the file.html you created > that should work by trigering the python file you programed in and it > inturn creates the website or pages of your choice I'm not totally sure what you mean but I don;t think it works the way you believe. What you do in your code below is create an HTML file. That could be served up by a web server as a static HTML file. But for dynamic web content the program actually prints the HTML to stdout and the web server redirects stdout back to the web client(browser). > an example like this could help > webpage = open('index.html', 'w+') > webpage.write("My web page"); > webpage.write("This the body"); > webpage.close() You don't need the w+, w on its own is adequate. And you don't need semi-colons at the end of lines. But basically this just creates a text file in a folder somewhere. Its only when that is accessed via a web server that it gets sent to a browser, and only as a static file. If you wanted to do it dynamically you'd have a file called something like mypage.cgi which looked like: print("My web page") print("This the body") And provided the web server was configured to run python on cgi files you could access it as http://myserver.addresss/mypage.cgi HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From questions.anon at gmail.com Thu May 22 02:25:27 2014 From: questions.anon at gmail.com (questions anon) Date: Thu, 22 May 2014 10:25:27 +1000 Subject: [Tutor] Move files to a new directory with matching folder names Message-ID: I have files contained separately in folders labelled by years and months and I would like to copy them into another directory where they have matching folder names of years and months. I can do this for one folder at a time (see below) but I want it to be able to go through all the folders (2002/01,2002/02.....2012/11,2012/12) and copy files from one directory to another directory with matching subdir names. thanks import os import shutil source="/Data/allclimatedata/2002/01" destination="/Data/rainfall/2002/01" for file in os.listdir(source): path = os.path.join(source, file) shutil.copy(path, destination) -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Thu May 22 02:27:33 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 21 May 2014 17:27:33 -0700 Subject: [Tutor] Option on How to create web Interface In-Reply-To: References: Message-ID: As a web server, you would not even be trying to open a file on disk. You've most likely got the network socket as a file-like object in hand: you'd probably write directly to that file-like object and avoid touching any disk whatsoever. You want to avoid touching disk if possible, especially on high-traffic web sites. See: https://wiki.python.org/moin/BaseHttpServer for an example. If we use the standard library's web server, there's a "wfile" that's the file-like object that we can write to. (https://docs.python.org/3/library/http.server.html?highlight=wfile#http.server.BaseHTTPRequestHandler.wfile) If we use the standard library, what might be unfortunately confusing is that our output is depending on a field in the "request" object. Personally, I think that's a design flaw of the standard library web server. Many other web server frameworks will provide a "response" argument that is conceptually separate from the "request" argument. From dyoo at hashcollision.org Thu May 22 02:30:50 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 21 May 2014 17:30:50 -0700 Subject: [Tutor] Move files to a new directory with matching folder names In-Reply-To: References: Message-ID: On Wed, May 21, 2014 at 5:25 PM, questions anon wrote: > I have files contained separately in folders labelled by years and months > and I would like to copy them into another directory where they have > matching folder names of years and months. > I can do this for one folder at a time (see below) but I want it to be able > to go through all the folders (2002/01,2002/02.....2012/11,2012/12) and copy > files from one directory to another directory with matching subdir names. Side note: do you have a name, or is your first name truly Questions? shutil contains a function for recursively copying entire trees. I think it's called copytree(). Checking... ok, found it. https://docs.python.org/3/library/shutil.html#shutil.copytree Would this be suitable for your situation? From steve at pearwood.info Thu May 22 03:11:27 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 22 May 2014 11:11:27 +1000 Subject: [Tutor] Move files to a new directory with matching folder names In-Reply-To: References: Message-ID: <20140522011127.GK10355@ando> On Thu, May 22, 2014 at 10:25:27AM +1000, questions anon wrote: > I have files contained separately in folders labelled by years and months > and I would like to copy them into another directory where they have > matching folder names of years and months. > I can do this for one folder at a time (see below) but I want it to be able > to go through all the folders (2002/01,2002/02.....2012/11,2012/12) and > copy files from one directory to another directory with matching subdir > names. You need a loop. Currently you do something like this: source="/Data/allclimatedata/2002/01" destination="/Data/rainfall/2002/01" copy files from source to destination The dumb way would be to manually list out every pair of directories: source="/Data/allclimatedata/2002/01" destination="/Data/rainfall/2002/01" copy files from source to destination source="/Data/allclimatedata/2002/02" destination="/Data/rainfall/2002/02" copy files from source to destination ... and so on, for 12 months per year and however many years you need The smart way is to get Python to do the work for you: source_prefix = "/Data/allclimatedata" destination_prefix = "/Data/rainfall" for year in range(2002, 2013): year = str(year) for month in range(1, 13): # convert to a string with leading 0 if needed month = "%02d" % month source = os.path.join(source_prefix, year, month) destination = os.path.join(destination_prefix, year, month) copy files from source to destination -- Steven From davea at davea.name Thu May 22 04:44:46 2014 From: davea at davea.name (Dave Angel) Date: Wed, 21 May 2014 22:44:46 -0400 (EDT) Subject: [Tutor] Option on How to create web Interface References: Message-ID: fabu desay Wrote in message: > If I got the problem correctly What problem is that? I see no context. > ,you can equally create + open a file > file = open(filename,+wr) But the second argument must be a string. I cannot imagine any content in the wrong variable that would produce a string there. > The file should have the HTML extension > You can now pace your web contents in the file.html you created So which is it? HTML or html? They are very different. > that should work by trigering the python file you programed in and it > inturn creates the website or pages of your choice > an example like this could help > webpage = open('index.html', 'w+') > webpage.write("My web page"); > webpage.write("This the body"); > webpage.close() > Which webserver is this intended for? Certainly not cgi. Was this message intended to be a reply to somebody? If you had done that, and maybe quoted some context, perhaps it'd make sense to me. -- DaveA From questions.anon at gmail.com Thu May 22 05:10:22 2014 From: questions.anon at gmail.com (questions anon) Date: Thu, 22 May 2014 13:10:22 +1000 Subject: [Tutor] Move files to a new directory with matching folder names In-Reply-To: <20140522011127.GK10355@ando> References: <20140522011127.GK10355@ando> Message-ID: Thank you for your response. I have created some sample folders and tried your method but I receive an error: IOError: [Errno 21] Is a directory: '/Data/test1/2011/01' The script I use is: destination_prefix="/Data/test/" source_prefix="/Data/test1/" for year in range(2011, 2012): year = str(year) for month in range(1, 13): # convert to a string with leading 0 if needed month = "%02d" % month source = os.path.join(source_prefix, year, month) destination = os.path.join(destination_prefix, year, month) shutil.copy(source, destination) On Thu, May 22, 2014 at 11:11 AM, Steven D'Aprano wrote: > On Thu, May 22, 2014 at 10:25:27AM +1000, questions anon wrote: > > I have files contained separately in folders labelled by years and months > > and I would like to copy them into another directory where they have > > matching folder names of years and months. > > I can do this for one folder at a time (see below) but I want it to be > able > > to go through all the folders (2002/01,2002/02.....2012/11,2012/12) and > > copy files from one directory to another directory with matching subdir > > names. > > You need a loop. > > Currently you do something like this: > > > source="/Data/allclimatedata/2002/01" > destination="/Data/rainfall/2002/01" > copy files from source to destination > > > The dumb way would be to manually list out every pair of directories: > > > source="/Data/allclimatedata/2002/01" > destination="/Data/rainfall/2002/01" > copy files from source to destination > source="/Data/allclimatedata/2002/02" > destination="/Data/rainfall/2002/02" > copy files from source to destination > ... and so on, for 12 months per year and however many years you need > > > > The smart way is to get Python to do the work for you: > > > source_prefix = "/Data/allclimatedata" > destination_prefix = "/Data/rainfall" > for year in range(2002, 2013): > year = str(year) > for month in range(1, 13): > # convert to a string with leading 0 if needed > month = "%02d" % month > source = os.path.join(source_prefix, year, month) > destination = os.path.join(destination_prefix, year, month) > copy files from source to destination > > > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Thu May 22 12:22:42 2014 From: davea at davea.name (Dave Angel) Date: Thu, 22 May 2014 06:22:42 -0400 (EDT) Subject: [Tutor] Move files to a new directory with matching folder names References: <20140522011127.GK10355@ando> Message-ID: questions anon Wrote in message: > Please use text mail to post, as html has a number of problems. In your current message, indentation is lost. Also don't top-post in this forum. Any selective quoting of previous context should precede your comments. Your logic presently tries to copy a directory with the shutil. copy function. In your first post, you used a loop. Why did you omit it now? -- DaveA From markos at c2o.pro.br Thu May 22 15:17:31 2014 From: markos at c2o.pro.br (Markos) Date: Thu, 22 May 2014 10:17:31 -0300 Subject: [Tutor] Doubts about installing python3.1 in squeeze Message-ID: <537DF8EB.30209@c2o.pro.br> Hi, I'm learning Python and I'm using Debian 6.0 (squeeze) The installed version is 2.6.6. (python -V) I have seen some recommendations for beginners to invest in version 3. I found package of Python 3.1 in repository for squeeze. I am considering installing Python 3.1 with apt-get install python3.1 But I found the site http://www.circuidipity.com/python2-and-python3.html information on how to keep the two versions using virtualenv. Also I found in the /usr/bin python2.5 and python2.6 And in /usr/lib python2.4, python2.5 and python2.6 Can I just run apt-get install python3.1 or should I do any other configuration? I'm confused. Are there any risk to install python3.1 and some programs stop working on my debian squeeze? Thanks for any tips? Markos From akleider at sonic.net Thu May 22 18:22:27 2014 From: akleider at sonic.net (Alex Kleider) Date: Thu, 22 May 2014 09:22:27 -0700 Subject: [Tutor] Doubts about installing python3.1 in squeeze In-Reply-To: <537DF8EB.30209@c2o.pro.br> References: <537DF8EB.30209@c2o.pro.br> Message-ID: <004aee0ff3eb6d8c4fd936b774f2675c@sonic.net> On 2014-05-22 06:17, Markos wrote: > Hi, > > I'm learning Python and I'm using Debian 6.0 (squeeze) > > The installed version is 2.6.6. (python -V) > > I have seen some recommendations for beginners to invest in version 3. > > I found package of Python 3.1 in repository for squeeze. > > I am considering installing Python 3.1 with > > apt-get install python3.1 > > But I found the site > http://www.circuidipity.com/python2-and-python3.html information on > how to keep the two versions using virtualenv. > > Also I found in the /usr/bin python2.5 and python2.6 > > And in /usr/lib python2.4, python2.5 and python2.6 > > Can I just run apt-get install python3.1 or should I do any other > configuration? > > I'm confused. > > > Are there any risk to install python3.1 and some programs stop working > on my debian squeeze? > > Thanks for any tips? > Markos On Ubuntu both v2 and v3 are installed by default. Have you tried typing python3 on the command line? If you get the interpreter, it's installed. Then you just have to use a different shebang line in your code files: #!/usr/bin/env python3 I don't have a debian system close at hand to test this myself. alex From dyoo at hashcollision.org Thu May 22 18:51:51 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 22 May 2014 09:51:51 -0700 Subject: [Tutor] Fwd: Option on How to create web Interface In-Reply-To: References: Message-ID: (Forwarding to Python-tutor; currently I'm a bit busy at this moment.) ---------- Forwarded message ---------- From: fabu desay Date: Thu, May 22, 2014 at 1:26 AM Subject: Re: [Tutor] Option on How to create web Interface To: Danny Yoo Thanks A bunch But is there like a sandbox for me to try it on my local server? From illusiontechniques at gmail.com Thu May 22 20:41:25 2014 From: illusiontechniques at gmail.com (C Smith) Date: Thu, 22 May 2014 14:41:25 -0400 Subject: [Tutor] Doubts about installing python3.1 in squeeze In-Reply-To: References: <537DF8EB.30209@c2o.pro.br> <004aee0ff3eb6d8c4fd936b774f2675c@sonic.net> Message-ID: Sorry, typing is hard. *You will need to use virtualenv On Thu, May 22, 2014 at 2:40 PM, C Smith wrote: > You can't use apt-get or similar to install 3.1. > You will need to virtualenv. > > On Thu, May 22, 2014 at 12:22 PM, Alex Kleider wrote: >> On 2014-05-22 06:17, Markos wrote: >>> >>> Hi, >>> >>> I'm learning Python and I'm using Debian 6.0 (squeeze) >>> >>> The installed version is 2.6.6. (python -V) >>> >>> I have seen some recommendations for beginners to invest in version 3. >>> >>> I found package of Python 3.1 in repository for squeeze. >>> >>> I am considering installing Python 3.1 with >>> >>> apt-get install python3.1 >>> >>> But I found the site >>> http://www.circuidipity.com/python2-and-python3.html information on >>> how to keep the two versions using virtualenv. >>> >>> Also I found in the /usr/bin python2.5 and python2.6 >>> >>> And in /usr/lib python2.4, python2.5 and python2.6 >>> >>> Can I just run apt-get install python3.1 or should I do any other >>> configuration? >>> >>> I'm confused. >>> >>> >>> Are there any risk to install python3.1 and some programs stop working >>> on my debian squeeze? >>> >>> Thanks for any tips? >>> Markos >> >> >> On Ubuntu both v2 and v3 are installed by default. >> Have you tried typing >> python3 >> on the command line? >> If you get the interpreter, it's installed. >> Then you just have to use a different shebang line in your code files: >> #!/usr/bin/env python3 >> >> I don't have a debian system close at hand to test this myself. >> >> alex >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor From illusiontechniques at gmail.com Thu May 22 20:40:44 2014 From: illusiontechniques at gmail.com (C Smith) Date: Thu, 22 May 2014 14:40:44 -0400 Subject: [Tutor] Doubts about installing python3.1 in squeeze In-Reply-To: <004aee0ff3eb6d8c4fd936b774f2675c@sonic.net> References: <537DF8EB.30209@c2o.pro.br> <004aee0ff3eb6d8c4fd936b774f2675c@sonic.net> Message-ID: You can't use apt-get or similar to install 3.1. You will need to virtualenv. On Thu, May 22, 2014 at 12:22 PM, Alex Kleider wrote: > On 2014-05-22 06:17, Markos wrote: >> >> Hi, >> >> I'm learning Python and I'm using Debian 6.0 (squeeze) >> >> The installed version is 2.6.6. (python -V) >> >> I have seen some recommendations for beginners to invest in version 3. >> >> I found package of Python 3.1 in repository for squeeze. >> >> I am considering installing Python 3.1 with >> >> apt-get install python3.1 >> >> But I found the site >> http://www.circuidipity.com/python2-and-python3.html information on >> how to keep the two versions using virtualenv. >> >> Also I found in the /usr/bin python2.5 and python2.6 >> >> And in /usr/lib python2.4, python2.5 and python2.6 >> >> Can I just run apt-get install python3.1 or should I do any other >> configuration? >> >> I'm confused. >> >> >> Are there any risk to install python3.1 and some programs stop working >> on my debian squeeze? >> >> Thanks for any tips? >> Markos > > > On Ubuntu both v2 and v3 are installed by default. > Have you tried typing > python3 > on the command line? > If you get the interpreter, it's installed. > Then you just have to use a different shebang line in your code files: > #!/usr/bin/env python3 > > I don't have a debian system close at hand to test this myself. > > alex > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From kwpolska at gmail.com Thu May 22 20:47:33 2014 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Thu, 22 May 2014 20:47:33 +0200 Subject: [Tutor] Option on How to create web Interface In-Reply-To: References: Message-ID: On Thu, May 22, 2014 at 12:54 AM, Alan Gauld wrote: > But basically this just creates a text file in a folder somewhere. > Its only when that is accessed via a web server that it gets > sent to a browser, and only as a static file. > > If you wanted to do it dynamically you'd have a file called > something like mypage.cgi which looked like: > > print("My web page") > print("This the body") > > And provided the web server was configured to run python > on cgi files you could access it as > > http://myserver.addresss/mypage.cgi Sadly, this is not the modern way to do it. These days, it?s recommended to use something like Flask, or Django, or whatnot. CGI is ancient, and problematic. -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From kwpolska at gmail.com Thu May 22 20:46:05 2014 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Thu, 22 May 2014 20:46:05 +0200 Subject: [Tutor] Doubts about installing python3.1 in squeeze In-Reply-To: References: <537DF8EB.30209@c2o.pro.br> <004aee0ff3eb6d8c4fd936b774f2675c@sonic.net> Message-ID: On Thu, May 22, 2014 at 8:41 PM, C Smith wrote: > Sorry, typing is hard. > *You will need to use virtualenv > > On Thu, May 22, 2014 at 2:40 PM, C Smith wrote: >> You can't use apt-get or similar to install 3.1. >> You will need to virtualenv. ?or maybe not. The correct way is compiling it on your own (the usual Unix way, but using `make altinstall` for the install step). virtualenv won?t help in this case, it?s not what virtualenv is for. -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From alan.gauld at btinternet.com Thu May 22 21:11:04 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 22 May 2014 20:11:04 +0100 Subject: [Tutor] Option on How to create web Interface In-Reply-To: References: Message-ID: On 22/05/14 19:47, Chris ?Kwpolska? Warrick wrote: > On Thu, May 22, 2014 at 12:54 AM, Alan Gauld wrote: >> But basically this just creates a text file in a folder somewhere. >> Its only when that is accessed via a web server that it gets >> sent to a browser, and only as a static file. >> >> If you wanted to do it dynamically you'd have a file called >> something like mypage.cgi which looked like: >> >> print("My web page") >> print("This the body") >> >> And provided the web server was configured to run python >> on cgi files you could access it as >> >> http://myserver.addresss/mypage.cgi > > Sadly, this is not the modern way to do it. These days, it?s > recommended to use something like Flask, or Django, or whatnot. CGI > is ancient, and problematic. > CGI is still how many frameworks operate under the covers. They just hide it well. But I agree that using a framework is the only sane way to build a significant web app nowadays and I was not promoting original CGI as a solution. However, the OP seemed to be asking about the low level details of how a web app works and using a framework (both by design and intent) hides all of that from you whereas an old fashioned CGI app exposes it. Hence my illustration. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From felipe146 at hotmail.com Fri May 23 14:23:41 2014 From: felipe146 at hotmail.com (Felipe Melo) Date: Fri, 23 May 2014 09:23:41 -0300 Subject: [Tutor] Read a matrix with lines in different behavior Message-ID: Hello, I want to read the below matrix, identify when the characters in front of "want = " are equal to "1" and then save in an array and in an output file the characters above. But I don't know how to identify the second line and store in a variable: alpha=0 beta=2 gamma=50 want = 0 alpha=0 beta=2 gamma=50 want = 1 alpha=0 beta=2 gamma=50 want = 0 alpha=0 beta=2 gamma=50 want = 1 alpha=0 beta=2 gamma=50 want = 0 This is part of the code: try: datadir = '/home/me/Test_python/' fileHandle = open( "%s/teste.txt"%datadir, 'r' ) vector = [ ] for line in fileHandle.readlines(): line=line.strip() a = line.split(" ")[0] print a b = line.split(" ")[1] print b c = line.split(" ")[2] print c d = line.split(" ")[3] print d if d == "1": vector.append([a,b,c]) n = n + 1 fileHandle.close() file = open("saida.txt","w") for cont in range(n): file.write = vector except: print "Exception" sys.exit( 1 ) When I execute the code there is an error when the loop finds the second line [felipe at grumari Test_python]$ python verificar.py alpha=0 beta=2 gamma=50 Exception Thanks, Felipe -------------- next part -------------- An HTML attachment was scrubbed... URL: From markos at c2o.pro.br Fri May 23 13:50:10 2014 From: markos at c2o.pro.br (Markos) Date: Fri, 23 May 2014 08:50:10 -0300 Subject: [Tutor] Doubts about installing python3.1 in squeeze In-Reply-To: <004aee0ff3eb6d8c4fd936b774f2675c@sonic.net> References: <537DF8EB.30209@c2o.pro.br> <004aee0ff3eb6d8c4fd936b774f2675c@sonic.net> Message-ID: <537F35F2.3040306@c2o.pro.br> On 22-05-2014 13:22, Alex Kleider wrote: > On 2014-05-22 06:17, Markos wrote: >> Hi, >> >> I'm learning Python and I'm using Debian 6.0 (squeeze) >> >> The installed version is 2.6.6. (python -V) >> >> I have seen some recommendations for beginners to invest in version 3. >> >> I found package of Python 3.1 in repository for squeeze. >> >> I am considering installing Python 3.1 with >> >> apt-get install python3.1 >> >> But I found the site >> http://www.circuidipity.com/python2-and-python3.html information on >> how to keep the two versions using virtualenv. >> >> Also I found in the /usr/bin python2.5 and python2.6 >> >> And in /usr/lib python2.4, python2.5 and python2.6 >> >> Can I just run apt-get install python3.1 or should I do any other >> configuration? >> >> I'm confused. >> >> >> Are there any risk to install python3.1 and some programs stop working >> on my debian squeeze? >> >> Thanks for any tips? >> Markos > > On Ubuntu both v2 and v3 are installed by default. > Have you tried typing > python3 > on the command line? > If you get the interpreter, it's installed. > Then you just have to use a different shebang line in your code files: > #!/usr/bin/env python3 > > I don't have a debian system close at hand to test this myself. > > alex > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Dear, Thanks everyone for the tips. I tried to install python3.1 with the command apt-get install python3.1 and use the shebang #!/usr/bin/env python3 in the source code header. It works. But to use Tkinter I also had to install the package python3-tk apt-get intall python3-tk Now I have in /usr/bin python python3 python2.5 python2.6 python3.1 python is a link to python2.6 and python3 is a link to python3.1 Until now everything is working. Markos From ss3141 at att.com Fri May 23 13:57:17 2014 From: ss3141 at att.com (SABARWAL, SHAL) Date: Fri, 23 May 2014 11:57:17 +0000 Subject: [Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock Message-ID: Wondering if anyone came across this error in using form = cgi.FieldStorage() import tempfile File /tempfile.py", line 83, in _once_lock = _allocate_lock() thread.error: can't allocate lock puthon version 2.7, on HP-UX 11.11 -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Fri May 23 16:39:55 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 23 May 2014 15:39:55 +0100 Subject: [Tutor] Read a matrix with lines in different behavior In-Reply-To: References: Message-ID: On 23/05/2014 13:23, Felipe Melo wrote: > Hello, > > I want to read the below matrix, identify when the characters in front > of "want = " are equal to "1" and then save in an array and in an output > file the characters above. But I don't know how to identify the second > line and store in a variable: > > alpha=0 beta=2 gamma=50 > want = 0 > alpha=0 beta=2 gamma=50 > want = 1 > alpha=0 beta=2 gamma=50 > want = 0 > alpha=0 beta=2 gamma=50 > want = 1 > alpha=0 beta=2 gamma=50 > want = 0 > > > This is part of the code: > > try: > datadir = '/home/me/Test_python/' > > fileHandle = open( "%s/teste.txt"%datadir, 'r' > ) > vector = [ ] > for line in fileHandle.readlines(): > line=line.strip() > a = line.split(" ")[0] > print a > b = line.split(" ")[1] > print b > c = line.split(" ")[2] > print c > d = line.split(" ")[3] > print d > if d == "1": > vector.append([a,b,c]) > n = n + 1 > fileHandle.close() > file = open("saida.txt","w") > for cont in range(n): > file.write = vector > > > except: > print "Exception" > sys.exit( 1 ) > > > When I execute the code there is an error when the loop finds the second > line > > > [felipe at grumari Test_python]$ python verificar.py > alpha=0 > beta=2 > gamma=50 > Exception > You've made a classic newbie mistake by catching all exceptions. Take the try/except out of your code and Python will tell you precisely where your problem actually is. Try this and if you can't make any progress please get back to us. You also only need one call to line.split, not four, and I'm unsure whather or not the call to vector.append is what you actually want. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From __peter__ at web.de Fri May 23 15:55:47 2014 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 May 2014 15:55:47 +0200 Subject: [Tutor] Read a matrix with lines in different behavior References: Message-ID: Felipe Melo wrote: > Hello, > > I want to read the below matrix, identify when the characters in front of > "want = " are equal to "1" and then save in an array and in an output file > the characters above. But I don't know how to identify the second line and > store in a variable: > > alpha=0 beta=2 gamma=50 > want = 0 > alpha=0 beta=2 gamma=50 > want = 1 > alpha=0 beta=2 gamma=50 > want = 0 > alpha=0 beta=2 gamma=50 > want = 1 > alpha=0 beta=2 gamma=50 > want = 0 > > > This is part of the code: > > try: > datadir = '/home/me/Test_python/' > > fileHandle = open( "%s/teste.txt"%datadir, 'r' > ) > vector = [ ] > for line in fileHandle.readlines(): > line=line.strip() > a = line.split(" ")[0] > print a > b = line.split(" ")[1] > print b > c = line.split(" ")[2] > print c > d = line.split(" ")[3] > print d > if d == "1": > vector.append([a,b,c]) > n = n + 1 > fileHandle.close() > file = open("saida.txt","w") > for cont in range(n): > file.write = vector > > > except: > print "Exception" > sys.exit( 1 ) > > > When I execute the code there is an error when the loop finds the second > line > > > [felipe at grumari Test_python]$ python verificar.py > alpha=0 > beta=2 > gamma=50 > Exception Felipe you're shooting yourself in the foot when you put the try: ... # error occurs here except: # catch everything ... # show a generic message around the part of the code that does the actual work. Remove the try...except, and see if you can make sense of the traceback and the error message you get. The message will say what exactly went wrong, and the traceback will show where it went wrong. If with this valuable information you still cannot fix the code yourself come back here, but this time post the traceback (use copy and paste, don't rephrase) to make it easier for us to help you fix the bug. Random hints: - your code seems to expect > alpha=0 beta=2 gamma=50 > want = 0 on a single line. Either adjust the data or the code. - writing to a file is spelt file.write(some_string) - you can iterate over lists, no need for range(): for item in items: ... # do something with item - you can iterate over files, no need for readlines(): for line in file: ... # do something with line From alan.gauld at btinternet.com Fri May 23 17:19:14 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 23 May 2014 16:19:14 +0100 Subject: [Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock In-Reply-To: References: Message-ID: On 23/05/14 12:57, SABARWAL, SHAL wrote: > Wondering if anyone came across this error in using form = > cgi.FieldStorage() > > import tempfile > > File /tempfile.py", line 83, in _once_lock > = _allocate_lock() > > thread.error: can't allocate lock > > puthon version 2.7, on HP-UX 11.11 Since we can't see the code in tempfile.py we could ony guess Also please include the full error traceback, that looks like you have summarised it. As it stands it is impossible to tell how tempfile.py relates to cgi.FieldStorage() or even if the error is related to that at all. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Fri May 23 18:33:30 2014 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 May 2014 18:33:30 +0200 Subject: [Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock References: Message-ID: Alan Gauld wrote: > On 23/05/14 12:57, SABARWAL, SHAL wrote: >> Wondering if anyone came across this error in using form = >> cgi.FieldStorage() >> >> import tempfile >> >> File /tempfile.py", line 83, in _once_lock >> = _allocate_lock() >> >> thread.error: can't allocate lock >> >> puthon version 2.7, on HP-UX 11.11 > > Since we can't see the code in tempfile.py we could ony guess > Also please include the full error traceback, that looks like > you have summarised it. > > As it stands it is impossible to tell how tempfile.py relates to > cgi.FieldStorage() or even if the error is related to that > at all. Here's what the cgi.FieldStorage.make_file() method looks like def make_file(self, binary=None): # docstring omitted import tempfile return tempfile.TemporaryFile("w+b") and importing from within a method is generally a bad idea when threads are involved. However, I'm unable to provoke the error the OP is seeing. Shal, does the error occur every time or sporadically? Can you provide code to reproduce the error? From davea at davea.name Sun May 25 00:03:49 2014 From: davea at davea.name (Dave Angel) Date: Sat, 24 May 2014 18:03:49 -0400 (EDT) Subject: [Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock References: Message-ID: Peter Otten <__peter__ at web.de> Wrote in message: > Alan Gauld wrote: > >> >> As it stands it is impossible to tell how tempfile.py relates to >> cgi.FieldStorage() or even if the error is related to that >> at all. > > Here's what the cgi.FieldStorage.make_file() method looks like > > def make_file(self, binary=None): > # docstring omitted > import tempfile > return tempfile.TemporaryFile("w+b") > > and importing from within a method is generally a bad idea when threads are > involved. However, I'm unable to provoke the error the OP is seeing. > > Shal, does the error occur every time or sporadically? Can you provide code > to reproduce the error? > If indeed that's the problem, the solution could be as easy as doing another import of that module BEFORE starting threads. -- DaveA From diliupg at gmail.com Sun May 25 09:18:40 2014 From: diliupg at gmail.com (diliup gabadamudalige) Date: Sun, 25 May 2014 12:48:40 +0530 Subject: [Tutor] Pygame related question Message-ID: I need to random pick a pygame sprite from a sprite class. The random module does not allow this to be used on a group. Is the shortest way to raed in the attributes thatr I need into a list and then random pick that or is there a shorter way? Sample code is given below. scalelist=[] for scale in all_scales: scalelist.append( [scale.name, scale.scaletype] ) scale = random.choice(scalelist) where all_scales is a sprite class with 45 objects. I append the 2 attributes of each sprite to a scalelist with the above function and then random pick an item from the scalelist. Is this the shortest way? Thank you to all in advance. -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ********************************************************************************************** This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. ********************************************************************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sun May 25 10:56:26 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 25 May 2014 18:56:26 +1000 Subject: [Tutor] Pygame related question In-Reply-To: References: Message-ID: <20140525085626.GV10355@ando> On Sun, May 25, 2014 at 12:48:40PM +0530, diliup gabadamudalige wrote: > I need to random pick a pygame sprite from a sprite class. > The random module does not allow this to be used on a group. > Is the shortest way to raed in the attributes thatr I need into a list and > then random pick that or is there a shorter way? Sample code is given below. > > scalelist=[] > for scale in all_scales: > scalelist.append( [scale.name, scale.scaletype] > ) > > scale = random.choice(scalelist) > > where all_scales is a sprite class with 45 objects. Try this: scalelist = list(all_scales) scale = random.choice(scalelist) which hopefully should do what you want. -- Steven From davea at davea.name Sun May 25 13:19:03 2014 From: davea at davea.name (Dave Angel) Date: Sun, 25 May 2014 07:19:03 -0400 (EDT) Subject: [Tutor] Pygame related question References: <20140525085626.GV10355@ando> Message-ID: Steven D'Aprano Wrote in message: > On Sun, May 25, 2014 at 12:48:40PM +0530, diliup gabadamudalige wrote: >> I need to random pick a pygame sprite from a sprite class. >> The random module does not allow this to be used on a group. >> Is the shortest way to raed in the attributes thatr I need into a list and >> then random pick that or is there a shorter way? Sample code is given below. >> >> scalelist=[] >> for scale in all_scales: >> scalelist.append( [scale.name, scale.scaletype] >> ) >> >> scale = random.choice(scalelist) >> >> where all_scales is a sprite class with 45 objects. > > Try this: > > scalelist = list(all_scales) > scale = random.choice(scalelist) > > which hopefully should do what you want. Or temp = random.choice ( list (all_scales) ) scale = [temp.name, temp.scaletype] if the original format of scale is important. -- DaveA From matbioinfo at gmail.com Sun May 25 17:39:22 2014 From: matbioinfo at gmail.com (rahmad akbar) Date: Sun, 25 May 2014 17:39:22 +0200 Subject: [Tutor] class to function Message-ID: Hi guys i am trying to understand this code: http://nbviewer.ipython.org/gist/BenLangmead/6665861 i understand functions quite alright . but i have no idea about classes yet. the code is written using class and i could not make much sense out of it. all this init and self thingy drive me crazy. can you guys help explain. super thanks -- many thanks mat From amonroe at columbus.rr.com Sun May 25 18:20:30 2014 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Sun, 25 May 2014 12:20:30 -0400 Subject: [Tutor] class to function In-Reply-To: References: Message-ID: <1412489101.20140525122030@columbus.rr.com> > can you > guys help explain. super thanks A class is like a blueprint. An instance of that class is like a house built from that blueprint. Think about it. An infinite number of houses could be constructed using those blueprints. But the architect only had to draw them once. __init__() is like the "foundation" of each house that is built. In otherwords, the stuff you do first when construction commences. Alan From akleider at sonic.net Sun May 25 18:48:10 2014 From: akleider at sonic.net (Alex Kleider) Date: Sun, 25 May 2014 09:48:10 -0700 Subject: [Tutor] class to function In-Reply-To: <1412489101.20140525122030@columbus.rr.com> References: <1412489101.20140525122030@columbus.rr.com> Message-ID: <01225afe7e91f919e542e30e7f79fb47@sonic.net> On 2014-05-25 09:20, R. Alan Monroe wrote: >> can you >> guys help explain. super thanks > > A class is like a blueprint. > An instance of that class is like a house built from that blueprint. > > Think about it. An infinite number of houses could be constructed > using those blueprints. But the architect only had to draw them once. > > __init__() is like the "foundation" of each house that is built. In > otherwords, the stuff you do first when construction commences. > > Alan I very much like this analogy but might I suggest a slight modification? __init__() builds the house, and possibly provides minimum furnishings[1] (with parameters passed to init;) then your code (usually using the associated methods) flushes out the furnishings[1] (and makes use of them) as the needs of your program demand. [1] 'furnishings' in this analogy are DATA Does this fit? Comments welcome. Respectively submitted, ak From alan.gauld at btinternet.com Sun May 25 19:48:56 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 25 May 2014 18:48:56 +0100 Subject: [Tutor] class to function In-Reply-To: References: Message-ID: On 25/05/14 16:39, rahmad akbar wrote: > Hi guys > > i am trying to understand this code: > http://nbviewer.ipython.org/gist/BenLangmead/6665861 > > i understand functions quite alright . but i have no idea about > classes yet. Do you understand modules? Modules contain functions and data that you can reuse across programs. Are you comfortable with the idea of importing and using modules? Classes are one step on from modules. A module can contain data but that data is restricted in that you can only have a single instance of it. For example if you had a module full of functions for processing a data tree and the root was a module variable. You can load the tree and use it just fine. But if you want to work with more than one tree you either have to throw away (or cache) the old tree before populating the new one or combine both trees under a single root (which probably won't work well). To solve that you build the module functions and data into a Tree class. Then you can create multiple Trees each with their own root. To do that you take each of the module functions and put then into the class as "methods". You then put the global root variable into the class as instance data. You do that by creating an __init__() method that accepts the root data as a parameter and populates the instance root variable (self.root). When you create an instance of a tree you pass in the root variable and Python "magically" calls the __init__() method, passing your root in to it. You probably have been doing this with things like strings for ages. You create a string by assigning a string literal s = 'my string' instead of explicitly calling s = str('my string') But the effect is the same. You create an instance of a string object which has methods attached, like upper(), split() etc, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Sun May 25 14:34:42 2014 From: __peter__ at web.de (Peter Otten) Date: Sun, 25 May 2014 14:34:42 +0200 Subject: [Tutor] Pygame related question References: Message-ID: diliup gabadamudalige wrote: > I need to random pick a pygame sprite from a sprite class. > The random module does not allow this to be used on a group. > Is the shortest way to raed in the attributes thatr I need into a list and > then random pick that or is there a shorter way? Sample code is given > below. > > scalelist=[] > for scale in all_scales: > scalelist.append( [scale.name, > scale.scaletype] > ) > > scale = random.choice(scalelist) > > where all_scales is a sprite class with 45 objects. > I append the 2 attributes of each sprite to a scalelist with the above > function and then random pick an item from the scalelist. > > Is this the shortest way? Not the shortest, but probably an efficient way: subclass pygame.sprite.OrderedUpdates and use the subclass to group your sprites: # untested class MyGroup(pygame.sprite.OrderedUpdates): def __getitem__(self, index): return self._spritelist[index] group = MyGroup(sprite1, sprite2, ...) some_sprite = random.choice(group) From davidmarino838 at gmail.com Sun May 25 19:32:52 2014 From: davidmarino838 at gmail.com (Marino David) Date: Mon, 26 May 2014 01:32:52 +0800 Subject: [Tutor] what is the cores? Message-ID: Hi all: I am a newpie at python. I read a python toolbox in which there is code line:*import cores as co. *I wantta know what the cores is. When I type "*import cores as co*", error occur as follows: *Traceback (most recent call last):* * File "", line 1, in * *ImportError: No module named cores* I am sure this cores module is related to probability distributions since there are a lot of codes like these: co.alpha, co.beta, co.uniform, etc. I google "cores python", and I did get useful information. Can anyone help me out? Any information will be greatly appreciated. David -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Sun May 25 20:21:48 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Sun, 25 May 2014 11:21:48 -0700 Subject: [Tutor] what is the cores? In-Reply-To: References: Message-ID: On Sun, May 25, 2014 at 10:32 AM, Marino David wrote: > Hi all: > I am a newpie at python. > I read a python toolbox in which there is code line:import cores as co. I > wantta know what the cores is. > When I type "import cores as co", error occur as follows: > Traceback (most recent call last): > File "", line 1, in > ImportError: No module named cores > > I am sure this cores module is related to probability distributions since > there are a lot of codes like these: co.alpha, co.beta, co.uniform, etc. > > I google "cores python", and I did get useful information. Can anyone help > me out? "cores" is not one of the standard Python modules, nor is it part of the PyPi package index. I suspect that it's part of the "toolbox" (I'm not sure what you mean by that - is it a book, a utility package...?) that you're using. Take a closer look; it's probably referenced elsewhere. Give us a bit more information about the "toolbox" you're using, and we should be able to help you further. From davidmarino838 at gmail.com Sun May 25 21:46:42 2014 From: davidmarino838 at gmail.com (Marino David) Date: Mon, 26 May 2014 03:46:42 +0800 Subject: [Tutor] what is the cores? In-Reply-To: References: Message-ID: Thanks Marc. After I have close look at the toolbox, I found that cores.py is in the toolbox. Thanks again. David 2014-05-26 2:21 GMT+08:00 Marc Tompkins : > On Sun, May 25, 2014 at 10:32 AM, Marino David > wrote: > > Hi all: > > I am a newpie at python. > > I read a python toolbox in which there is code line:import cores as co. I > > wantta know what the cores is. > > When I type "import cores as co", error occur as follows: > > Traceback (most recent call last): > > File "", line 1, in > > ImportError: No module named cores > > > > I am sure this cores module is related to probability distributions since > > there are a lot of codes like these: co.alpha, co.beta, co.uniform, etc. > > > > I google "cores python", and I did get useful information. Can anyone > help > > me out? > > "cores" is not one of the standard Python modules, nor is it part of > the PyPi package index. I suspect that it's part of the "toolbox" > (I'm not sure what you mean by that - is it a book, a utility > package...?) that you're using. Take a closer look; it's probably > referenced elsewhere. > > Give us a bit more information about the "toolbox" you're using, and > we should be able to help you further. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dyoo at hashcollision.org Mon May 26 03:08:53 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 25 May 2014 18:08:53 -0700 Subject: [Tutor] class to function In-Reply-To: References: Message-ID: > i am trying to understand this code: > http://nbviewer.ipython.org/gist/BenLangmead/6665861 I'm slightly familiar with the purpose of the code. It's constructing a Suffix Tree, though not in linear time. Reading the code... ah. I see. This is enumerating through all suffixes, and building it by repeated insertion into a trie. http://en.wikipedia.org/wiki/Trie The classes are used here to represent structured data and operations to be performed on that structured data. The code fundamentally has a structured value called a Node, with two fields to represent the string label and the links to other nodes. Aside: there is a very good book by Dan Gusfield that talks about suffix trees and how to construct them called "Algorithms on Strings, Trees and Sequences: Computer Science and Computational Biology" http://www.amazon.com/Algorithms-Strings-Trees-Sequences-Computational/dp/0521585198 which you may want to look at if you're interested in these algorithms. It is probably not a good idea to try to intermix trying to understand an algorithm like this at the same time as you're learning basic features in your programming language. Consider a slightly simpler example to learn about "classes", apart from the algorithms you are studying. Most good Python tutorials should cover how to use classes to build structured data and manipulate it. Alan Gauld's Python tutorial, for example, should have a section on this. http://www.alan-g.me.uk/tutor/tutclass.htm From dyoo at hashcollision.org Mon May 26 09:04:47 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 26 May 2014 00:04:47 -0700 Subject: [Tutor] Fwd: class to function In-Reply-To: References: Message-ID: ---------- Forwarded message ---------- From: *diliup gabadamudalige* Date: Sunday, May 25, 2014 Subject: [Tutor] class to function To: Danny Yoo Is it wrong to say that __init__() is to initialize all the conditions you setupin the class? That's how i see it. I give below the way I see a class and it's workings. Please Correct if any mistakes. Say you have a class for a Spaceship Class Spaceship(): under the def __init__(): you set up all the details you want the spaceship to have. For example, self.engine=100 self. guns=100 self.shield=250 self.life=5 self if the Spaceship which will be built using the details given under the init in the Class Spaceship. So a class is a blu print to build the space ship and the init function contains all the things the space ship will contain. Now when you want a ne space ship say a RED one you can call the class with RedShip=Spaceship() and you can now giv e a new character to the space ship. A colour! like this RedShip.colour=Red Now along with the RedShip.enging=100 RedShip.guns=100 RedShip.shield=250 RedShip.life=5 the RedShip also has a NEW ATTRIBUTE. Colour. RedShip.colour=Red The def__init__() can also be setup so that you can give all the attributes its values like this. def__init__(power,bullets,energy,life) RedShip.enging=power RedShip.guns=bullets RedShip.shield=energy RedShip.life=life this way each ship can have different attributes. But they are all Spaceships. On Mon, May 26, 2014 at 6:38 AM, Danny Yoo > wrote: > > i am trying to understand this code: > > http://nbviewer.ipython.org/gist/BenLangmead/6665861 > > I'm slightly familiar with the purpose of the code. It's constructing > a Suffix Tree, though not in linear time. > > Reading the code... ah. I see. This is enumerating through all > suffixes, and building it by repeated insertion into a trie. > > http://en.wikipedia.org/wiki/Trie > > The classes are used here to represent structured data and operations > to be performed on that structured data. The code fundamentally has a > structured value called a Node, with two fields to represent the > string label and the links to other nodes. > > > > Aside: there is a very good book by Dan Gusfield that talks about > suffix trees and how to construct them called "Algorithms on Strings, > Trees and Sequences: Computer Science and Computational Biology" > > > http://www.amazon.com/Algorithms-Strings-Trees-Sequences-Computational/dp/0521585198 > > which you may want to look at if you're interested in these algorithms. > > > It is probably not a good idea to try to intermix trying to understand > an algorithm like this at the same time as you're learning basic > features in your programming language. Consider a slightly simpler > example to learn about "classes", apart from the algorithms you are > studying. Most good Python tutorials should cover how to use classes > to build structured data and manipulate it. Alan Gauld's Python > tutorial, for example, should have a section on this. > > http://www.alan-g.me.uk/tutor/tutclass.htm > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Diliup Gabadamudalige http://www.diliupg.com http://soft.diliupg.com/ ********************************************************************************************** This e-mail is confidential. It may also be legally privileged. If you are not the intended recipient or have received it in error, please delete it and all copies from your system and notify the sender immediately by return e-mail. Any unauthorized reading, reproducing, printing or further dissemination of this e-mail or its contents is strictly prohibited and may be unlawful. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. The sender does not accept liability for any errors or omissions. ********************************************************************************************** -------------- next part -------------- An HTML attachment was scrubbed... URL: From matbioinfo at gmail.com Mon May 26 09:27:34 2014 From: matbioinfo at gmail.com (rahmad akbar) Date: Mon, 26 May 2014 09:27:34 +0200 Subject: [Tutor] Fwd: class to function In-Reply-To: References: Message-ID: thanks guys for the replies, Danny, your explanation helped alot, i'll have a go on Alan Gaud's tutorial. super thanks On Mon, May 26, 2014 at 9:04 AM, Danny Yoo wrote: > > > ---------- Forwarded message ---------- > From: diliup gabadamudalige > Date: Sunday, May 25, 2014 > Subject: [Tutor] class to function > To: Danny Yoo > > > Is it wrong to say that __init__() is to initialize all the conditions you > setupin the class? That's how i see it. > I give below the way I see a class and it's workings. Please Correct if any > mistakes. > > Say you have a class for a Spaceship > Class Spaceship(): > > under the def __init__(): > you set up all the details you want the spaceship to have. For example, > self.engine=100 > self. guns=100 > self.shield=250 > self.life=5 > > self if the Spaceship which will be built using the details given under the > init in the Class Spaceship. So a class is a blu print to build the space > ship and the init function contains all the things the space ship will > contain. > Now when you want a ne space ship say a RED one > you can call the class with > RedShip=Spaceship() > and you can now giv e a new character to the space ship. A colour! like this > RedShip.colour=Red > Now along with the > RedShip.enging=100 > RedShip.guns=100 > RedShip.shield=250 > RedShip.life=5 > the RedShip also has a NEW ATTRIBUTE. Colour. > RedShip.colour=Red > > The def__init__() can also be setup so that you can give all the attributes > its values like this. > def__init__(power,bullets,energy,life) > > RedShip.enging=power > RedShip.guns=bullets > RedShip.shield=energy > RedShip.life=life > > this way each ship can have different attributes. But they are all > Spaceships. > > > > > On Mon, May 26, 2014 at 6:38 AM, Danny Yoo wrote: >> >> > i am trying to understand this code: >> > http://nbviewer.ipython.org/gist/BenLangmead/6665861 >> >> I'm slightly familiar with the purpose of the code. It's constructing >> a Suffix Tree, though not in linear time. >> >> Reading the code... ah. I see. This is enumerating through all >> suffixes, and building it by repeated insertion into a trie. >> >> http://en.wikipedia.org/wiki/Trie >> >> The classes are used here to represent structured data and operations >> to be performed on that structured data. The code fundamentally has a >> structured value called a Node, with two fields to represent the >> string label and the links to other nodes. >> >> >> >> Aside: there is a very good book by Dan Gusfield that talks about >> suffix trees and how to construct them called "Algorithms on Strings, >> Trees and Sequences: Computer Science and Computational Biology" >> >> >> http://www.amazon.com/Algorithms-Strings-Trees-Sequences-Computational/dp/0521585198 >> >> which you may want to look at if you're interested in these algorithms. >> >> >> It is probably not a good idea to try to intermix trying to understand >> an algorithm like this at the same time as you're learning basic >> features in your programming language. Consider a slightly simpler >> example to learn about "classes", apart from the algorithms you are >> studying. Most good Python tutorials should cover how to use classes >> to build structured data and manipulate it. Alan Gauld's Python >> tutorial, for example, should have a section on this. >> >> http://www.alan-g.me.uk/tutor/tutclass.htm >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor > > > > > -- > Diliup Gabadamudalige > > http://www.diliupg.com > http://soft.diliupg.com/ > > ********************************************************************************************** > This e-mail is confidential. It may also be legally privileged. If you are > not the intended recipient or have received it in error, please delete it > and all copies from your system and notify the sender immediately by return > e-mail. Any unauthorized reading, reproducing, printing or further > dissemination of this e-mail or its contents is strictly prohibited and may > be unlawful. Internet communications cannot be guaranteed to be timely, > secure, error or virus-free. The sender does not accept liability for any > errors or omissions. > ********************************************************************************************** > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- many thanks mat From dyoo at hashcollision.org Mon May 26 20:30:05 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 26 May 2014 11:30:05 -0700 Subject: [Tutor] Fwd: class to function In-Reply-To: References: Message-ID: On Mon, May 26, 2014 at 12:27 AM, rahmad akbar wrote: > thanks guys for the replies, > > Danny, your explanation helped alot, i'll have a go on Alan Gaud's > tutorial. super thanks You should probably thank Diliup Gabadamudalige instead; I just forwarded his message. Best of wishes to you! From theemmasoules at gmail.com Mon May 26 17:40:15 2014 From: theemmasoules at gmail.com (Emma Soules) Date: Mon, 26 May 2014 11:40:15 -0400 Subject: [Tutor] How does this work? Is there anyway to receive live Python help? Message-ID: -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon May 26 20:45:52 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 26 May 2014 19:45:52 +0100 Subject: [Tutor] How does this work? Is there anyway to receive live Python help? In-Reply-To: References: Message-ID: On 26/05/14 16:40, Emma Soules wrote: > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor This is a mailing list, so you send emails to it with questions and the people on the list respond with answers (and probably some questions back). And yes, we are all alive... But the time to reply to a mail depends on several factors so you might not get a response for a few hours (usually within 2 or 3, often less). The better the question you ask the better the answer you get. So try to be specific, precise, and include code and error messages (don't just summarize). Telling us the Python version and OS often helps too. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Mon May 26 21:15:26 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 26 May 2014 12:15:26 -0700 Subject: [Tutor] How does this work? Is there anyway to receive live Python help? In-Reply-To: References: Message-ID: Hi Emma, You can think of the mailing list as a public community forum. You may, in fact, see other questions and messages that aren't related to your particular question. If you see a question that you think you can answer, feel free to contribute. We try to keep the signal-to-noise ratio as high as possible. Sometimes that effort falters, but we try. :P Good luck to you. From jeanpierreda at gmail.com Mon May 26 22:04:55 2014 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Mon, 26 May 2014 13:04:55 -0700 Subject: [Tutor] How does this work? Is there anyway to receive live Python help? In-Reply-To: References: Message-ID: If you want python help "live", try the #python IRC channel at irc.freenode.net You'll need to register a nick though, see https://freenode.net/faq.shtml#nicksetup -- Devin On Mon, May 26, 2014 at 8:40 AM, Emma Soules wrote: > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Tue May 27 02:15:41 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 May 2014 10:15:41 +1000 Subject: [Tutor] How does this work? Is there anyway to receive live Python help? In-Reply-To: References: Message-ID: <20140527001540.GW10355@ando> On Mon, May 26, 2014 at 01:04:55PM -0700, Devin Jeanpierre wrote: > If you want python help "live", try the #python IRC channel at irc.freenode.net > > You'll need to register a nick though, see > https://freenode.net/faq.shtml#nicksetup Ouch! #python is not very friendly to beginners. (Or anyone else for that matter.) It's well-suited to questions which can be asked, and answered, in one or two sentences, but is completely unsuitable for extended discussion. Also they have a bad habit of banning people without warning or explanation. And, I have to say, in my opinion often their answers are actually *wrong*, but if you try to tell them so they ban you. [Disclaimer: I haven't bothered with #python for over a year. It's possible the culture there has completely changed.] -- Steven From steve at pearwood.info Tue May 27 02:59:48 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 May 2014 10:59:48 +1000 Subject: [Tutor] How does this work? Is there anyway to receive live Python help? In-Reply-To: References: Message-ID: <20140527005948.GX10355@ando> Hi Emma, As well as everything else others have said, remember that we're scattered all over the world, so you can ask a question at any time of the day or night and chances are good that *someone* will read it soon. When you joined this mailing list, you had a choice of whether to receive individual messages, or a daily digest. Make sure you pick individual messages, otherwise you will have to wait anything up to 24 hours before seeing all the replies at once, instead of as them come in. You can change your settings here: https://mail.python.org/mailman/listinfo/tutor When asking questions, the more detail you can provide, the better our answers will be. E.g: "I need help with my homework!" (Our answer: Yes, you probably do. Next.) "I need help with my homework. I have to write a guess-the-number game." (Our answer: Okay, at least now we know what your homework is. How far have you got? What are you having trouble with?) "I'm writing a guess-the-number game using Python version 3.3. I'm stuck at writing this loop, I expect it to ask the user for Yes or No repeatedly, but it just loops forever even if the user types No. Here is a copy of my code. Oh, and this is homework, so I'm just looking for direction and explanation, please don't write my code for me." (Our answer: Great! That's just about the perfect question, now we can answer it.) If you're unsure about something ("how do I find out what version of Python I'm running?") please ask. That includes basic questions like "what's a function?". No question about Python is too simple for us to answer (although for advanced users, sometimes questions can be too specialized for us). There are no stupid questions although sometimes people ask good questions in a stupid manner. Ask questions well and we'll try very hard to answer them well. -- Steven From jeanpierreda at gmail.com Tue May 27 03:10:56 2014 From: jeanpierreda at gmail.com (Devin Jeanpierre) Date: Mon, 26 May 2014 18:10:56 -0700 Subject: [Tutor] How does this work? Is there anyway to receive live Python help? In-Reply-To: <20140527001540.GW10355@ando> References: <20140527001540.GW10355@ando> Message-ID: FWIW I am an operator on the channel, so, obviously biased. (ssbr) On Mon, May 26, 2014 at 5:15 PM, Steven D'Aprano wrote: > On Mon, May 26, 2014 at 01:04:55PM -0700, Devin Jeanpierre wrote: >> If you want python help "live", try the #python IRC channel at irc.freenode.net >> >> You'll need to register a nick though, see >> https://freenode.net/faq.shtml#nicksetup > > Ouch! > > #python is not very friendly to beginners. (Or anyone else for that > matter.) There are many things that are unfriendly about #python, and many things that are friendly about it. I think it's unfair to casually dismiss it as a place to learn. If Emma was who I think they were, their experience seemed fine to me, and their questions seemed suitable for IRC. > It's well-suited to questions which can be asked, and > answered, in one or two sentences, but is completely unsuitable for > extended discussion. On the contrary, I prefer #python to the mailing list for smallish but extended discussion. Because the unit of conversation is very small, and the turnaround time is so short, small misunderstandings can be quickly corrected, and the conversation progresses quickly. It is very often the case that one question leads to another, which leads to another. In IRC this is one relatively quick (but "extended") session. This is its core advantage over something like a mailing list (with its slow round-trip time), and one that I think #python uses fairly well. On the other hand, mailing lists are amazing for esoteric questions that need the right pair of eyes or they can't be answered, or for requests for extended monologues about how something works, etc. -- IRC can do these, especially the latter, but not as well. I think the fact that #python is so popular (for extended discussion, no less) shows that it is not bad, but that it might be a matter of preference. > Also they have a bad habit of banning people without warning or > explanation. And, I have to say, in my opinion often their answers are > actually *wrong*, but if you try to tell them so they ban you. You would not be banned for saying an answer is wrong. That happens all the time, because answers often are wrong. But, for example, you might be banned if you keep telling everyone "lol wow, you have no idea what you are talking about", and insulting things like that (without so much as giving an actual answer yourself). But you would probably be warned first. If you have more details, I can offer more help and/or apologies as warranted. I am only talking in the abstract, and it's possible you saw somebody be wronged. -- Devin From jarod_v6 at libero.it Tue May 27 10:05:30 2014 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Tue, 27 May 2014 10:05:30 +0200 (CEST) Subject: [Tutor] Help on best way to check resence of item inside list Message-ID: <975274200.13645911401177930495.JavaMail.actor@webmail48> Dear All clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra"," electra"] clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", "sally"," Castiel","Sam"] I have a list of names that I would to annotate in function of presence in different clubs: my input files is a long file where I have this : mary luke luigi jane jessica rebecca luis ################################################? with open("file.in") as p: mit = [] for i in p: lines =i.strip("\n").split("\t") if (lines[0] in clubA: G =lines[-1] +["clubA"] else: G = lines[-1] +["no"] mit.append(G) for i in mit: if i.strip("\n").split("\t")[0] in clubB: G =lines[-1] +["clubB"] else: G = lines[-1] +["no"] finale.append(G) ############################################################### I just wonder if is appropriate to use a loops to check if is present the value on a list. Is it the right way? I can use a dictionary because I have many repeated names. In the end I wan to have mary clubA clubB luke clubA luigi no Thanks in advance for any help From breamoreboy at yahoo.co.uk Tue May 27 10:48:27 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 27 May 2014 09:48:27 +0100 Subject: [Tutor] Help on best way to check resence of item inside list In-Reply-To: <975274200.13645911401177930495.JavaMail.actor@webmail48> References: <975274200.13645911401177930495.JavaMail.actor@webmail48> Message-ID: On 27/05/2014 09:05, jarod_v6 at libero.it wrote: > Dear All > > clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra"," > electra"] > clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", "sally"," > Castiel","Sam"] > > I have a list of names that I would to annotate in function of presence in > different clubs: > > my input files is a long file where I have this : > > mary > luke > luigi > jane > jessica > rebecca > luis > ################################################? > > with open("file.in") as p: > mit = [] > for i in p: > lines =i.strip("\n").split("\t") > if (lines[0] in clubA: > G =lines[-1] +["clubA"] > else: > G = lines[-1] +["no"] > mit.append(G) > > > for i in mit: > if i.strip("\n").split("\t")[0] in clubB: > G =lines[-1] +["clubB"] > else: > G = lines[-1] +["no"] > finale.append(G) > ############################################################### > I just wonder if is appropriate to use a loops to check if is present the > value on a list. Is it the right way? I can use a dictionary because I have > many repeated names. > > In the end I wan to have > > > mary clubA clubB > luke clubA > luigi no > Thanks in advance for any help You can use the in keyword to check for an item in a list. However a very quick glance at your code suggests that you could cut out the list completely and do the same using the in keyword against your dict. Better still I think the defaultdict is what you need here, I'll leave you to look it up as I must dash :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From davea at davea.name Tue May 27 12:17:03 2014 From: davea at davea.name (Dave Angel) Date: Tue, 27 May 2014 06:17:03 -0400 (EDT) Subject: [Tutor] Help on best way to check resence of item inside list References: <975274200.13645911401177930495.JavaMail.actor@webmail48> Message-ID: "jarod_v6 at libero.it" Wrote in message: > Dear All > > clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra"," > electra"] > clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", "sally"," > Castiel","Sam"] > > I have a list of names that I would to annotate in function of presence in > different clubs: > > my input files is a long file where I have this : > > mary > luke > luigi > jane > jessica > rebecca > luis > ################################################? > > with open("file.in") as p: > mit = [] > for i in p: > lines =i.strip("\n").split("\t") > if (lines[0] in clubA: > G =lines[-1] +["clubA"] > else: > G = lines[-1] +["no"] > mit.append(G) > > > for i in mit: > if i.strip("\n").split("\t")[0] in clubB: > G =lines[-1] +["clubB"] > else: > G = lines[-1] +["no"] > finale.append(G) > ############################################################### > I just wonder if is appropriate to use a loops to check if is present the > value on a list. Is it the right way? I can use a dictionary because I have > many repeated names. > > In the end I wan to have > > > mary clubA clubB > luke clubA > luigi no > Thanks in advance for any help There are numerous errors in the above code. You should use copy/paste, so we don't waste energy identifying errors that don't even exist in your actual code. As it stands, it wouldn't even compile. But even if you fix the typos and indentation errors and initialization errors, you still have logic errors if you want the output you specify. First, the second loop doesn?t set the lines variable at all, but just uses the value from the last iteration of the first loop. Second, even if you untangle that, luigi would end up with two 'no's, not one. You don't say what list could have repeats. I don't see any in your sample data. You also don't say how they should be treated. For example, Are all seventeen mary's in clubA? Now to your specific question. You aren't using the loops to check the lists for a name. You're quite reasonably using 'in'. You can accomplish your apparent goal much more reasonably by using a single loop and more complex if elif and else. -- DaveA From steve at pearwood.info Tue May 27 14:05:45 2014 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 May 2014 22:05:45 +1000 Subject: [Tutor] Help on best way to check resence of item inside list In-Reply-To: <975274200.13645911401177930495.JavaMail.actor@webmail48> References: <975274200.13645911401177930495.JavaMail.actor@webmail48> Message-ID: <20140527120545.GY10355@ando> On Tue, May 27, 2014 at 10:05:30AM +0200, jarod_v6 at libero.it wrote: [...] > with open("file.in") as p: > mit = [] You have lost the indentation, which makes this code incorrect. But the rest of the code is too complicated. > for i in p: > lines =i.strip("\n").split("\t") > if (lines[0] in clubA: > G =lines[-1] +["clubA"] > else: > G = lines[-1] +["no"] > mit.append(G) > > for i in mit: > if i.strip("\n").split("\t")[0] in clubB: > G =lines[-1] +["clubB"] > else: > G = lines[-1] +["no"] > finale.append(G) Look at the result you want to get: > mary clubA clubB > luke clubA > luigi no That suggests to me that the best data structure is a dict with sets: {'mary': set(['clubA', 'clubB']), 'luke': set(['clubA']), 'luigi': set(), } Something like this should work: names = {} with open("file.in") as p: # This assumes the data file is one name per line. for line in p: name = line.strip() s = names.get(name, set()) # If name not in the names, # return an empty set. if name in clubA: s.add("clubA") if name in clubB: s.add("clubB") names[name] = s print(names) And I think that should work. -- Steven From __peter__ at web.de Tue May 27 11:13:23 2014 From: __peter__ at web.de (Peter Otten) Date: Tue, 27 May 2014 11:13:23 +0200 Subject: [Tutor] Help on best way to check resence of item inside list References: <975274200.13645911401177930495.JavaMail.actor@webmail48> Message-ID: jarod_v6 at libero.it wrote: > Dear All > > clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra"," > electra"] > clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", > "sally"," Castiel","Sam"] > > I have a list of names that I would to annotate in function of presence > in different clubs: > > my input files is a long file where I have this : > > mary > luke > luigi > jane > jessica > rebecca > luis > ################################################? > > with open("file.in") as p: > mit = [] > for i in p: > lines =i.strip("\n").split("\t") > if (lines[0] in clubA: > G =lines[-1] +["clubA"] > else: > G = lines[-1] +["no"] > mit.append(G) > > > for i in mit: > if i.strip("\n").split("\t")[0] in clubB: > G =lines[-1] +["clubB"] > else: > G = lines[-1] +["no"] > finale.append(G) > ############################################################### > I just wonder if is appropriate to use a loops to check if is present the > value on a list. Is it the right way? I can use a dictionary because I > have many repeated names. You mean you have people who are members in more than one club? You can still use a dictionary if you make the value a list: # untested code! membership = {} club = "club_of_people_who_are_not_in_any_club" members = ["Erwin", "Kurt", "Groucho"] for name in members: # can be simplified with dict.setdefault() or collections.defaultdict if name in membership: membership[name].append(club) else: membership[name] = [club] Put that in a loop over (club, members) pairs, and you'll end up with a dict that maps name --> list_of_clubs. Then iterate over the lines in the file: with open("file.in") as source: for line in source: name = line.strip() if name in membership: print(name, *membership[name]) else: print(name, "no") > > In the end I wan to have > > > mary clubA clubB > luke clubA > luigi no > Thanks in advance for any help > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From jarod_v6 at libero.it Tue May 27 18:20:22 2014 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Tue, 27 May 2014 18:20:22 +0200 (CEST) Subject: [Tutor] R: Tutor Digest, Vol 123, Issue 65 Message-ID: <902304994.14083681401207622146.JavaMail.actor@webmail2> Thanks so much for the help! and suggestions I not use dictionary because I have some items are repeated inside the list >----Messaggio originale---- >Da: tutor-request at python.org >Data: 27/05/2014 12.00 >A: >Ogg: Tutor Digest, Vol 123, Issue 65 > >Send Tutor mailing list submissions to > tutor at python.org > >To subscribe or unsubscribe via the World Wide Web, visit > https://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. Help on best way to check resence of item inside list > (jarod_v6 at libero.it) > 2. Re: Help on best way to check resence of item inside list > (Mark Lawrence) > > >---------------------------------------------------------------------- > >Message: 1 >Date: Tue, 27 May 2014 10:05:30 +0200 (CEST) >From: "jarod_v6 at libero.it" >To: tutor at python.org >Subject: [Tutor] Help on best way to check resence of item inside > list >Message-ID: <975274200.13645911401177930495.JavaMail.actor at webmail48> >Content-Type: text/plain;charset="UTF-8" > >Dear All > >clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra"," >electra"] >clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", "sally"," >Castiel","Sam"] > >I have a list of names that I would to annotate in function of presence in >different clubs: > >my input files is a long file where I have this : > >mary >luke >luigi >jane >jessica >rebecca >luis >################################################? > >with open("file.in") as p: >mit = [] >for i in p: > lines =i.strip("\n").split("\t") > if (lines[0] in clubA: > G =lines[-1] +["clubA"] > else: > G = lines[-1] +["no"] >mit.append(G) > > >for i in mit: > if i.strip("\n").split("\t")[0] in clubB: > G =lines[-1] +["clubB"] > else: > G = lines[-1] +["no"] > finale.append(G) >############################################################### >I just wonder if is appropriate to use a loops to check if is present the >value on a list. Is it the right way? I can use a dictionary because I have >many repeated names. > >In the end I wan to have > > >mary clubA clubB >luke clubA >luigi no >Thanks in advance for any help > > >------------------------------ > >Message: 2 >Date: Tue, 27 May 2014 09:48:27 +0100 >From: Mark Lawrence >To: tutor at python.org >Subject: Re: [Tutor] Help on best way to check resence of item inside > list >Message-ID: >Content-Type: text/plain; charset=UTF-8; format=flowed > >On 27/05/2014 09:05, jarod_v6 at libero.it wrote: >> Dear All >> >> clubA= ["mary","luke","amyr","marco","franco","lucia", "sally","genevra"," >> electra"] >> clubB= ["mary","rebecca","jane","jessica","judit","sharon","lucia", "sally"," >> Castiel","Sam"] >> >> I have a list of names that I would to annotate in function of presence in >> different clubs: >> >> my input files is a long file where I have this : >> >> mary >> luke >> luigi >> jane >> jessica >> rebecca >> luis >> ################################################? >> >> with open("file.in") as p: >> mit = [] >> for i in p: >> lines =i.strip("\n").split("\t") >> if (lines[0] in clubA: >> G =lines[-1] +["clubA"] >> else: >> G = lines[-1] +["no"] >> mit.append(G) >> >> >> for i in mit: >> if i.strip("\n").split("\t")[0] in clubB: >> G =lines[-1] +["clubB"] >> else: >> G = lines[-1] +["no"] >> finale.append(G) >> ############################################################### >> I just wonder if is appropriate to use a loops to check if is present the >> value on a list. Is it the right way? I can use a dictionary because I have >> many repeated names. >> >> In the end I wan to have >> >> >> mary clubA clubB >> luke clubA >> luigi no >> Thanks in advance for any help > >You can use the in keyword to check for an item in a list. However a >very quick glance at your code suggests that you could cut out the list >completely and do the same using the in keyword against your dict. >Better still I think the defaultdict is what you need here, I'll leave >you to look it up as I must dash :) > > >-- >My fellow Pythonistas, ask not what our language can do for you, ask >what you can do for our language. > >Mark Lawrence > >--- >This email is free from viruses and malware because avast! Antivirus protection is active. >http://www.avast.com > > > > >------------------------------ > >Subject: Digest Footer > >_______________________________________________ >Tutor maillist - Tutor at python.org >https://mail.python.org/mailman/listinfo/tutor > > >------------------------------ > >End of Tutor Digest, Vol 123, Issue 65 >************************************** > From awg1 at gmx.com Tue May 27 22:01:51 2014 From: awg1 at gmx.com (Adam Gold) Date: Tue, 27 May 2014 21:01:51 +0100 Subject: [Tutor] pipes and redirecting Message-ID: <5384EF2F.4040902@gmx.com> I'm trying to run the following unix command from within Python as opposed to calling an external Bash script (the reason being I'm doing it multiple times within a for loop which is running through a list): "dd if=/home/adam/1 bs=4k conv=noerror,notrunc,sync | pbzip2 > 1.img.bz2" The first thing I do is break it into two assignments (I know this isn't strictly necessary but it makes the code easier to deal with): ddIf = shlex.split("dd if=/home/adam/1 bs=4k conv=noerror,notrunc,sync") compress = shlex.split("pbzip2 > /home/adam/1.img.bz2") I have looked at the docs here (and the equivalent for Python 3) https://docs.python.org/2/library/subprocess.html. I can get a 'simple' pipe like the following to work: p1 = subprocess.Popen(["ps"], stdout=PIPE) p2 = subprocess.Popen(["grep", "ssh"], stdin=p1.stdout, stdout=subprocess.PIPE) p1.stdout.close() output = p2.communicate()[0] I then try to adapt it to my example: p1 = subprocess.Popen(ddIf, stdout=subprocess.PIPE) p2 = subprocess.Popen(compress, stdin=p1.stdout, stdout=subprocess.PIPE) p1.stdout.close() output = p2.communicate()[0] I get the following error: pbzip2: *ERROR: File [>] NOT found! Skipping... ------------------------------------------- pbzip2: *ERROR: Input file [/home/adam/1.img.bz2] already has a .bz2 extension! Skipping I think that the '>' redirect needs to be dealt with using the subprocess module as well but I can't quite put the pieces together. I'd appreciate any guidance. Thanks. From yarteydegreat2 at gmail.com Tue May 27 21:27:42 2014 From: yarteydegreat2 at gmail.com (Degreat Yartey) Date: Tue, 27 May 2014 15:27:42 -0400 Subject: [Tutor] I am having difficulty grasping 'generators' Message-ID: I am studying python on my own (i.e. i am between the beginner and intermediate level) and i haven't met any difficulty until i reached the topic 'Generators and Iterators'. I need an explanation so simple as using the expression 'print ()', in this case 'yield'. Python 2.6 here! Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From amonroe at columbus.rr.com Wed May 28 00:52:46 2014 From: amonroe at columbus.rr.com (R. Alan Monroe) Date: Tue, 27 May 2014 18:52:46 -0400 Subject: [Tutor] I am having difficulty grasping 'generators' In-Reply-To: References: Message-ID: <605989166.20140527185246@columbus.rr.com> > I need an explanation so simple as using the expression 'print ()', in this case 'yield'. > Python 2.6 here! Ever write any C programs with static variables? Generators can be explained in those terms if you have experience with them. Alan From dyoo at hashcollision.org Wed May 28 03:08:45 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 27 May 2014 18:08:45 -0700 Subject: [Tutor] I am having difficulty grasping 'generators' In-Reply-To: References: Message-ID: On Tue, May 27, 2014 at 12:27 PM, Degreat Yartey wrote: > I am studying python on my own (i.e. i am between the beginner and > intermediate level) and i haven't met any difficulty until i reached the > topic 'Generators and Iterators'. > I need an explanation so simple as using the expression 'print ()', in this > case 'yield'. You can think of a generator as almost like a function, except it can return, not just once, but multiple times. Because it can return multiple times, if we squint at it enough, it acts like a _sequence_, just like the other sequence-like things in Python like files and lists and tuples. That is, as a sequence, it's something that we can walk down, element by element. We can loop over it. For example, let's say that we wanted to represent the same sequences as that of range(5). Here's one way we can do it with a generator: ################# def upToFive(): yield 0 yield 1 yield 2 yield 3 yield 4 ################# Let's try it. ################# >>> sequence = upToFive() >>> next(sequence) 0 >>> next(sequence) 1 >>> next(sequence) 2 >>> next(sequence) 3 >>> next(sequence) 4 >>> next(sequence) Traceback (most recent call last): File "", line 1, in StopIteration >>> >>> >>> for x in upToFive(): ... print("I see %d" % x) ... I see 0 I see 1 I see 2 I see 3 I see 4 ################# Now this is a toy example. If we wanted range(5), we'd just say "range(5)" and be done with it. What's neat about generators is that they make it easy to build these sequences while pretending that we're writing a plain function. All of the even numbers, for examples, is a sequence that we can make with a generator: ################# def onlyEvens(): n = 0 while True: yield n n = n + 2 ################# Let's try running it: ################# >>> sequence = onlyEvens() >>> next(sequence) 0 >>> next(sequence) 2 >>> next(sequence) 4 >>> next(sequence) 6 ################# And note that this sequence doesn't stop! We can keep calling next() on it and it will continue to run. We _can_ write a loop to run over such infinite sequences, but we'll also have to make sure to stop it manually: it won't exhaust otherwise, so doing something like: ################# for n in onlyEvens(): ... ################# better have something in the "..." that interrupts or returns, or else that loop will never end. From marc.tompkins at gmail.com Wed May 28 03:26:12 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 27 May 2014 18:26:12 -0700 Subject: [Tutor] I am having difficulty grasping 'generators' In-Reply-To: References: Message-ID: On Tue, May 27, 2014 at 12:27 PM, Degreat Yartey wrote: > I am studying python on my own (i.e. i am between the beginner and > intermediate level) and i haven't met any difficulty until i reached the > topic 'Generators and Iterators'. > I need an explanation so simple as using the expression 'print ()', in this > case 'yield'. > Python 2.6 here! > Thank you. > Simply put, a generator is just like a function - except that a function returns a value and then dies, but a generator yields a value and sticks around, waiting to yield another one - and another, and another, etc. I had some difficulty grasping the concept of generators at first because the examples you get in the tutorials are either trivial (you could do the same thing some other way, and it would be simpler) or horribly complex (and you get bogged down in the problem, rather than getting to grips with generators themselves.) So here's a real-life example - the first time I actually used generators... and they actually saved me a LOT of hard work and confusion. I needed to read some database files (from a very old database, for which I don't have an API - so I basically needed to read each record and interpret it byte-by-byte). I wrote a bunch of classes for the different tables in the database (I'll use Patient() as my example), but the heart of the operation is a generator. def RecordGenerator(office=None, fileType=None): obj = fTypes[fileType]() # create an empty record so we can read its attributes recLen = obj.RecordLength # such as the record length, the filename, etc. with open(getattr(office, obj.TLA) + '.dat','rb') as inFile: tmpIn = inFile.read() buf = StringIO.StringIO(tmpIn) inRec = buf.read(recLen) # initialize inRec and burn the header record while not (len(inRec) < recLen): inRec = buf.read(recLen) obj = fTypes[fileType](inRec) if (obj.Valid): yield obj buf.close() Now I'm writing a program that needs to process all the patients in office 01. I write the following: for obj in RecordGenerator(01, "Patient"): doStuffPart1 doStuffPart2 doStuffPart3 etc. The generator creates an empty Patient() object, looks at its attributes, and finds that the filename is "01PAT.dat" and the records are 1024 bytes long. It opens the file and reads it into tmpIn, then grabs 1024-byte chunks of tmpIn and passes them to Patient() until it finds a valid (not deleted, not corrupted) patient record. It "yields" that record to the caller, and waits. When "doStuffPart3" has completed, I go back to the top of the "for" loop and ask for the next patient. RecordGenerator steps through tmpIn, 1024 bytes at a time, until it finds a valid patient, then yields it and waits. When it hits the end of the file (or, more precisely, when the remaining length of tmpIn is less than a full record length), it quits. Essentially, you write a generator as if it were a function - but you use it as if it were a sequence. Best of both worlds, baby! From questions.anon at gmail.com Wed May 28 05:56:39 2014 From: questions.anon at gmail.com (questions anon) Date: Wed, 28 May 2014 13:56:39 +1000 Subject: [Tutor] Move files to a new directory with matching folder names In-Reply-To: References: <20140522011127.GK10355@ando> Message-ID: Thanks all, I got it to work using: import os import shutil destination_prefix="/Data/test/" source_prefix="/Data/test1/" for year in range(2011, 2013): year = str(year) for month in range(1, 13): # convert to a string with leading 0 if needed month = "%02d" % month source = os.path.join(source_prefix, year, month+"/") #print source destination = os.path.join(destination_prefix, year, month+"/") for files in os.listdir(source): print files filepath=os.path.join(source, files) print "copying", files, "from:", source, "to:", destination shutil.copy(filepath, destination) -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed May 28 10:29:42 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 28 May 2014 09:29:42 +0100 Subject: [Tutor] pipes and redirecting In-Reply-To: <5384EF2F.4040902@gmx.com> References: <5384EF2F.4040902@gmx.com> Message-ID: On 27/05/14 21:01, Adam Gold wrote: > "dd if=/home/adam/1 bs=4k conv=noerror,notrunc,sync | pbzip2 > 1.img.bz2" > > The first thing I do is break it into two assignments And that's the start of the problem because it should be three: The first command, the second command and the output file. > ddIf = shlex.split("dd if=/home/adam/1 bs=4k conv=noerror,notrunc,sync") > compress = shlex.split("pbzip2 > /home/adam/1.img.bz2") compress = "pbzip2" outfile = open('/home/adam/1.img.bz2','w') The redirection symbol is not something subprocess can use as an argument. > p1 = subprocess.Popen(ddIf, stdout=subprocess.PIPE) > p2 = subprocess.Popen(compress, stdin=p1.stdout, stdout=subprocess.PIPE) Use the output file here. p2 = subprocess.Popen(compress, stdin=p1.stdout, stdout=outfile) > I think that the '>' redirect needs to be dealt with using the > subprocess module as well but I can't quite put the pieces together. > I'd appreciate any guidance. Thanks. Alternatively read the output into a variable using communicate but then write it out to the file manually at the end. [You might also be able to use shell=TRUE but that introduces other issues. But I don't know whether using shell includes redirection.] -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From cs at zip.com.au Wed May 28 01:54:56 2014 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 28 May 2014 09:54:56 +1000 Subject: [Tutor] I am having difficulty grasping 'generators' In-Reply-To: References: Message-ID: <20140527235456.GA93181@cskk.homeip.net> On 27May2014 15:27, Degreat Yartey wrote: >I am studying python on my own (i.e. i am between the beginner and >intermediate level) and i haven't met any difficulty until i reached the >topic 'Generators and Iterators'. >I need an explanation so simple as using the expression 'print ()', in this >case 'yield'. >Python 2.6 here! >Thank you. Generators are functions that do a bit of work and then yield a value, then a bit more and so on. This means that you "call" them once. What you get back is an iterator, not the normal function return value. Whenever you use the iterator, the generator function runs until it hits a "yield" statement, and the value in theyield statement is what you get for that iteration. Next time you iterate, the function runs a bit more, until it yields again, or returns (end of function, and that causes end of iteration). So the function doesn't even run until you ask for a value, and then it only runs long enough to find the next value. Example (all code illstrative only, untested): Suppose you need to process every second line of a file. You might write it directly like this: def munge_lines(fp): ''' Do stuff with every second line of the already-open file `fp`. ''' lineno = 0 for line in fp: lineno += 1 if lineno % 2 == 0: print lineno, line, That should read lines from the file and print every second one with the line number. Now suppose you want something more complex than "every second line", especially something that requires keeping track of some state. In the example above you only need the line number, and using it still consumes 2 of the 3 lines in the loop body. A more common example might be "lines between two markers". The more of that you embed in the "munge_lines" function, the more it will get in the way of seeing what the function actually does. So a reasonable thing might be to write a function that gets the requested lines: def wanted_lines(fp): wanted = [] between = False for line in fp: if between: if 'end_marker' in line: between = False else: wanted.append(line) elif 'start_maker' in line: between = True return wanted This reads the whole file and returns a line of the wanted lines, and "munge_lines: might then look like this: for line in wanted_lines(fp): print line However: - that reads the whole file before returning anything - has to keep all the lines in the list "wanted" Slow in response, heavy in memory cost, and unworkable if "fp" actually doesn't end (eg reading from a terminal, or a pipeline, or...) What you'd really like is to get each line as needed. We can rewrite "wanted_lines" as a generator: def wanted_lines(fp): between = False for line in fp: if between: if 'end_marker' in line: between = False else: yield line elif 'start_maker' in line: between = True All we've done is used "yield" instead of the "append" and removed the "wanted" list and the return statement. The calling code is the same. To see the difference, put a "print" in "wanted_lines" as the first line of the for loop. With the "list" version you will see all the prints run before you get the array back. With the generator you will see the print run just before each value you get back. Cheers, Cameron Simpson From cs at zip.com.au Wed May 28 04:35:02 2014 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 28 May 2014 12:35:02 +1000 Subject: [Tutor] pipes and redirecting In-Reply-To: <5384EF2F.4040902@gmx.com> References: <5384EF2F.4040902@gmx.com> Message-ID: <20140528023502.GA56060@cskk.homeip.net> On 27May2014 21:01, Adam Gold wrote: >I'm trying to run the following unix command from within Python as >opposed to calling an external Bash script (the reason being I'm doing >it multiple times within a for loop which is running through a list): > >"dd if=/home/adam/1 bs=4k conv=noerror,notrunc,sync | pbzip2 > 1.img.bz2" First off, one expedient way to do this is to generate a shell script and pipe into "sh" (or "sh -uex", my preferred error sensitive invocation). p1 = subprocess.Popen(["sh", "-uex"], stdin=PIPE) for num in range(1,11): print("dd if=/home/adam/%d bs=4k conv=noerror,notrunc,sync | pbzip2 > %d.img.bz2", % (num, num), file=p1.stdin) p1.stdin.close() p1.wait() Any quoting issues aside, this is surprisingly useful. Let the shell do what it is good it. And NOTHING you've said here requires using bash. Use "sh" and say "sh", it is very portable and bash is rarely needed for most stuff. However, I gather beyond expediency, you want to know how to assemble pipelines using subprocess anyway. So... >The first thing I do is break it into two assignments (I know this isn't >strictly necessary but it makes the code easier to deal with): > >ddIf = shlex.split("dd if=/home/adam/1 bs=4k conv=noerror,notrunc,sync") >compress = shlex.split("pbzip2 > /home/adam/1.img.bz2") This is often worth doing regardless. Longer lines are harder to read. >I have looked at the docs here (and the equivalent for Python 3) >https://docs.python.org/2/library/subprocess.html. I can get a 'simple' >pipe like the following to work: > >p1 = subprocess.Popen(["ps"], stdout=PIPE) >p2 = subprocess.Popen(["grep", "ssh"], stdin=p1.stdout, stdout=subprocess.PIPE) >p1.stdout.close() >output = p2.communicate()[0] If you don't care about the stdout of p2 (and you don't, based on your "dd|pbzip2" example above) and you have left p2's stdout alone so that it goes to your normal stdout (eg the terminal) then you don't need to waste time with .communicate. I almost never use it myself. As the doco says, prone to deadlock. I prefer to just do the right thing explicitly myself, as needed. >I then try to adapt it to my example: > >p1 = subprocess.Popen(ddIf, stdout=subprocess.PIPE) >p2 = subprocess.Popen(compress, stdin=p1.stdout, stdout=subprocess.PIPE) >p1.stdout.close() >output = p2.communicate()[0] > >I get the following error: > >pbzip2: *ERROR: File [>] NOT found! Skipping... >------------------------------------------- >pbzip2: *ERROR: Input file [/home/adam/1.img.bz2] already has a .bz2 >extension! Skipping > >I think that the '>' redirect needs to be dealt with using the >subprocess module as well but I can't quite put the pieces together. >I'd appreciate any guidance. Thanks. It is as you expect. Consider what the shell does with: pbzip2 > 1.img.bz2 It invokes the command "pbzip2" (no arguments) with its output attached to the file "1.img.bz2". So first up: stay away form "shlex". It does _not_ do what you need. Shlex knows about shell string quoting. It does not know about redirections. It is handy for parsing minilanguages on your own concoction where you want to be able to quote strings with spaces. It is not a full on shell parser. So it (may) serve you well for the dd invocation because there are no redirections. But for your usage, so would the .split() method on a string, or even better: don't you already know the arguments for your "dd"? Just fill them out directly rather than backtracking from a string. However, your recipe is very close. Change: p2 = subprocess.Popen(compress, stdin=p1.stdout, stdout=subprocess.PIPE) into: p2 = subprocess.Popen(["pbzip2"], stdin=p1.stdout, stdout=open("1.img.bz2", "w")) Because p2 is writing to "1.img.bz2" you don't need to much about with .communicate either. No output to collect, no input to supply. See where that takes you. Cheers, Cameron Simpson From mitesh.budhabhatti at gmail.com Wed May 28 12:42:18 2014 From: mitesh.budhabhatti at gmail.com (Mitesh H. Budhabhatti) Date: Wed, 28 May 2014 16:12:18 +0530 Subject: [Tutor] HTML Parsing Message-ID: Hello Friends, I am using Python 3.3.3 on Windows 7. I would like to know what is the best method to do HTML parsing? For example, I want to connect to www.yahoo.com and get all the tags and their values. Thanks. Warm Regards, *Mitesh H. Budhabhatti* Cell# +91 99040 83855 -------------- next part -------------- An HTML attachment was scrubbed... URL: From yarteydegreat2 at gmail.com Wed May 28 12:52:49 2014 From: yarteydegreat2 at gmail.com (Degreat Yartey) Date: Wed, 28 May 2014 06:52:49 -0400 Subject: [Tutor] I am having difficulty grasping 'generators' In-Reply-To: <605989166.20140527185246@columbus.rr.com> References: <605989166.20140527185246@columbus.rr.com> Message-ID: I am completely new to programming! On May 27, 2014 10:54 PM, "R. Alan Monroe" wrote: > > I need an explanation so simple as using the expression 'print ()', in > this case 'yield'. > > Python 2.6 here! > > Ever write any C programs with static variables? Generators can be > explained in those terms if you have experience with them. > > Alan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From yarteydegreat2 at gmail.com Wed May 28 12:52:49 2014 From: yarteydegreat2 at gmail.com (Degreat Yartey) Date: Wed, 28 May 2014 06:52:49 -0400 Subject: [Tutor] I am having difficulty grasping 'generators' In-Reply-To: <20140527235456.GA93181@cskk.homeip.net> References: <20140527235456.GA93181@cskk.homeip.net> Message-ID: I really love this explanation... that means functions just run till it finishes its duty, then return...and generators just generate one at a time until the 'for' statement asks for __next__(). On May 28, 2014 8:37 AM, "Cameron Simpson" wrote: > On 27May2014 15:27, Degreat Yartey wrote: > >> I am studying python on my own (i.e. i am between the beginner and >> intermediate level) and i haven't met any difficulty until i reached the >> topic 'Generators and Iterators'. >> I need an explanation so simple as using the expression 'print ()', in >> this >> case 'yield'. >> Python 2.6 here! >> Thank you. >> > > Generators are functions that do a bit of work and then yield a value, > then a bit more and so on. This means that you "call" them once. What you > get back is an iterator, not the normal function return value. > > Whenever you use the iterator, the generator function runs until it hits a > "yield" statement, and the value in theyield statement is what you get for > that iteration. Next time you iterate, the function runs a bit more, until > it yields again, or returns (end of function, and that causes end of > iteration). > > So the function doesn't even run until you ask for a value, and then it > only runs long enough to find the next value. > > Example (all code illstrative only, untested): > > Suppose you need to process every second line of a file. > > You might write it directly like this: > > def munge_lines(fp): > ''' Do stuff with every second line of the already-open file `fp`. > ''' > lineno = 0 > for line in fp: > lineno += 1 > if lineno % 2 == 0: > print lineno, line, > > That should read lines from the file and print every second one with the > line number. > > Now suppose you want something more complex than "every second line", > especially something that requires keeping track of some state. In the > example above you only need the line number, and using it still consumes 2 > of the 3 lines in the loop body. > > A more common example might be "lines between two markers". > > The more of that you embed in the "munge_lines" function, the more it will > get in the way of seeing what the function actually does. > > So a reasonable thing might be to write a function that gets the requested > lines: > > def wanted_lines(fp): > wanted = [] > between = False > for line in fp: > if between: > if 'end_marker' in line: > between = False > else: > wanted.append(line) > elif 'start_maker' in line: > between = True > return wanted > > This reads the whole file and returns a line of the wanted lines, and > "munge_lines: might then look like this: > > for line in wanted_lines(fp): > print line > > However: > > - that reads the whole file before returning anything > > - has to keep all the lines in the list "wanted" > > Slow in response, heavy in memory cost, and unworkable if "fp" actually > doesn't end (eg reading from a terminal, or a pipeline, or...) > > What you'd really like is to get each line as needed. > > We can rewrite "wanted_lines" as a generator: > > def wanted_lines(fp): > between = False > for line in fp: > if between: > if 'end_marker' in line: > between = False > else: > yield line > elif 'start_maker' in line: > between = True > > All we've done is used "yield" instead of the "append" and removed the > "wanted" list and the return statement. The calling code is the same. > > To see the difference, put a "print" in "wanted_lines" as the first line > of the for loop. With the "list" version you will see all the prints run > before you get the array back. With the generator you will see the print > run just before each value you get back. > > Cheers, > Cameron Simpson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From yarteydegreat2 at gmail.com Wed May 28 12:52:49 2014 From: yarteydegreat2 at gmail.com (Degreat Yartey) Date: Wed, 28 May 2014 06:52:49 -0400 Subject: [Tutor] I am having difficulty grasping 'generators' In-Reply-To: References: Message-ID: This means that '...' should generally contain a manipulator then yield generates from where it stopped...*getting it* Thanks for the explanation though! Its seems so simple to digest. Thank you... On May 28, 2014 1:09 AM, "Danny Yoo" wrote: > On Tue, May 27, 2014 at 12:27 PM, Degreat Yartey > wrote: > > I am studying python on my own (i.e. i am between the beginner and > > intermediate level) and i haven't met any difficulty until i reached the > > topic 'Generators and Iterators'. > > I need an explanation so simple as using the expression 'print ()', in > this > > case 'yield'. > > > You can think of a generator as almost like a function, except it can > return, not just once, but multiple times. > > > Because it can return multiple times, if we squint at it enough, it > acts like a _sequence_, just like the other sequence-like things in > Python like files and lists and tuples. That is, as a sequence, it's > something that we can walk down, element by element. We can loop over > it. > > > For example, let's say that we wanted to represent the same sequences > as that of range(5). Here's one way we can do it with a generator: > > ################# > def upToFive(): > yield 0 > yield 1 > yield 2 > yield 3 > yield 4 > ################# > > > Let's try it. > > ################# > >>> sequence = upToFive() > >>> next(sequence) > 0 > >>> next(sequence) > 1 > >>> next(sequence) > 2 > >>> next(sequence) > 3 > >>> next(sequence) > 4 > >>> next(sequence) > Traceback (most recent call last): > File "", line 1, in > StopIteration > >>> > >>> > >>> for x in upToFive(): > ... print("I see %d" % x) > ... > I see 0 > I see 1 > I see 2 > I see 3 > I see 4 > ################# > > > Now this is a toy example. If we wanted range(5), we'd just say > "range(5)" and be done with it. > > > What's neat about generators is that they make it easy to build these > sequences while pretending that we're writing a plain function. All > of the even numbers, for examples, is a sequence that we can make with > a generator: > > ################# > def onlyEvens(): > n = 0 > while True: > yield n > n = n + 2 > ################# > > > Let's try running it: > > ################# > >>> sequence = onlyEvens() > >>> next(sequence) > 0 > >>> next(sequence) > 2 > >>> next(sequence) > 4 > >>> next(sequence) > 6 > ################# > > And note that this sequence doesn't stop! We can keep calling next() > on it and it will continue to run. > > > We _can_ write a loop to run over such infinite sequences, but we'll > also have to make sure to stop it manually: it won't exhaust > otherwise, so doing something like: > > ################# > for n in onlyEvens(): > ... > ################# > > better have something in the "..." that interrupts or returns, or else > that loop will never end. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed May 28 13:50:57 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 28 May 2014 12:50:57 +0100 Subject: [Tutor] HTML Parsing In-Reply-To: References: Message-ID: On 28/05/14 11:42, Mitesh H. Budhabhatti wrote: > Hello Friends, > > I am using Python 3.3.3 on Windows 7. I would like to know what is the > best method to do HTML parsing? For example, I want to connect to > www.yahoo.com and get all the tags and their values. The standard library contains a parser module: html.parser Which can do what you want, although its a non-trivial exercise. Basically you define event handler functions for each type of parser event. In your case you need handlers for starttag and data, and maybe, endtag. Within start-tag you can read the attributes to determine the tag type so it typically looks like def handle_starttag(self, name, attributes): if name == 'p': # process paragraph tag elif name == 'tr': # process table row etc... However, you might find it easier to use BeautifulSoup which is a third-party package you need to download. Soup tends to handle badly formed HTML better than the standard parser and works by reading the whole HTML document into a tree like structure which you can access, search or traverse... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Wed May 28 13:57:41 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 28 May 2014 12:57:41 +0100 Subject: [Tutor] I am having difficulty grasping 'generators' In-Reply-To: References: Message-ID: On 28/05/14 11:52, Degreat Yartey wrote: > This means that '...' should generally contain a manipulator then yield > generates from where it stopped...*getting it* It would help if you deleted the irrelevent bits so we can see which '...' you mean. I'm guessing it was this comment, right at the end, by Danny: > We _can_ write a loop to run over such infinite sequences, but we'll > also have to make sure to stop it manually: it won't exhaust > otherwise, so doing something like: > > ################# > for n in onlyEvens(): > ... > ################# > > better have something in the "..." that interrupts or returns, or else > that loop will never end. Notice that the ... here is not part of the generator. So the ... here references to how you process the output of the generator - the yielded values. So in this example case you'd have something like ################# for n in onlyEvens(): if n > 1000: break # prevent infinite loop # now process the evens that we are interested in. ################# The 'break' could also be a 'return' if the loop were inside a function. And it doesn't have to be a direct value check as shown the condition could be based on some external condition or even user input. The important point is to make sure that you have some way to exit the loop if the generator is infinite. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From hgfernan at gmail.com Wed May 28 17:35:32 2014 From: hgfernan at gmail.com (Hilton Fernandes) Date: Wed, 28 May 2014 12:35:32 -0300 Subject: [Tutor] I am having difficulty grasping 'generators' In-Reply-To: References: Message-ID: Dear all, i'd like to thank every answer in this list. Alan Gauld is a fine writer of excellent introductory material on Pyton, and so are a few other members of this list. So, it is always enlightening to read what you all write. Keep up the good work. All the best, hilton On Wed, May 28, 2014 at 8:57 AM, Alan Gauld wrote: > On 28/05/14 11:52, Degreat Yartey wrote: > >> This means that '...' should generally contain a manipulator then yield >> generates from where it stopped...*getting it* >> > > > It would help if you deleted the irrelevent bits so we can see > which '...' you mean. > > I'm guessing it was this comment, right at the end, by Danny: > > > We _can_ write a loop to run over such infinite sequences, but we'll >> also have to make sure to stop it manually: it won't exhaust >> otherwise, so doing something like: >> >> ################# >> for n in onlyEvens(): >> ... >> ################# >> >> better have something in the "..." that interrupts or returns, or else >> that loop will never end. >> > > > Notice that the ... here is not part of the generator. > So the ... here references to how you process the output of the generator > - the yielded values. So in this example case you'd > have something like > > > ################# > for n in onlyEvens(): > if n > 1000: break # prevent infinite loop > # now process the evens that we are interested in. > ################# > > The 'break' could also be a 'return' if the loop were > inside a function. > > And it doesn't have to be a direct value check as shown the > condition could be based on some external condition or even > user input. The important point is to make sure that you > have some way to exit the loop if the generator is infinite. > > hth > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From awg1 at gmx.com Wed May 28 14:42:11 2014 From: awg1 at gmx.com (Adam Gold) Date: Wed, 28 May 2014 13:42:11 +0100 Subject: [Tutor] pipes and redirecting In-Reply-To: References: <5384EF2F.4040902@gmx.com> Message-ID: <5385D9A3.2000709@gmx.com> > On 27/05/14 21:01, Adam Gold wrote: > >> "dd if=/home/adam/1 bs=4k conv=noerror,notrunc,sync | pbzip2 > 1.img.bz2" >> >> The first thing I do is break it into two assignments > > And that's the start of the problem because it should be three: > The first command, the second command and the output file. > >> ddIf = shlex.split("dd if=/home/adam/1 bs=4k conv=noerror,notrunc,sync") >> compress = shlex.split("pbzip2 > /home/adam/1.img.bz2") > > compress = "pbzip2" > outfile = open('/home/adam/1.img.bz2','w') > > The redirection symbol is not something subprocess can > use as an argument. > >> p1 = subprocess.Popen(ddIf, stdout=subprocess.PIPE) >> p2 = subprocess.Popen(compress, stdin=p1.stdout, stdout=subprocess.PIPE) > > Use the output file here. > > p2 = subprocess.Popen(compress, stdin=p1.stdout, stdout=outfile) > > >> I think that the '>' redirect needs to be dealt with using the >> subprocess module as well but I can't quite put the pieces together. >> I'd appreciate any guidance. Thanks. > > Alternatively read the output into a variable using communicate but then > write it out to the file manually at the end. > > [You might also be able to use shell=TRUE but that introduces other > issues. But I don't know whether using shell includes redirection.] > Thanks Alan, yes, I realise now I needed a third assignment to make this work. I actually had an exchange with subscriber 'eryksun' yesterday who did a great job of pointing me in the right direction. As a bit of a noob, I think I replied to the individual rather than the list, hence it doesn't seem to be in the thread. For the benefit of the archives I append below eryksun's initial (there was a bit of follow up but nothing too important) reply to me. ===================================== Send p2's stdout to a file: import subprocess import shlex ddIf = shlex.split("dd if=/home/adam/1 bs=4k " "conv=noerror,notrunc,sync") compress = "pbzip2" filename = "/home/adam/1.img.bz2" p1 = subprocess.Popen(ddIf, stdout=subprocess.PIPE) with p1.stdout as fin, open(filename, "w") as fout: p2 = subprocess.Popen(compress, stdin=fin, stdout=fout) ret1 = p1.wait() ret2 = p2.wait() Does this work? From dyoo at hashcollision.org Wed May 28 19:49:26 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 28 May 2014 10:49:26 -0700 Subject: [Tutor] HTML Parsing In-Reply-To: References: Message-ID: > I am using Python 3.3.3 on Windows 7. I would like to know what is the best > method to do HTML parsing? For example, I want to connect to www.yahoo.com > and get all the tags and their values. For this purpose, you may want to look at the APIs that the search engines provide, rather than try to web-scrape the human-focused web pages. Otherwise, your program will probably be fragile to changes in the structure of the web site. A search for search APIs comes up with hits like this: https://developer.yahoo.com/boss/search/ https://developers.google.com/web-search/docs/#fonje_snippets http://datamarket.azure.com/dataset/bing/search https://pypi.python.org/pypi/duckduckgo2 If you can say more about what you're planning to do, perhaps someone has already provided a programmatic interface to it. From davea at davea.name Wed May 28 20:40:01 2014 From: davea at davea.name (Dave Angel) Date: Wed, 28 May 2014 14:40:01 -0400 (EDT) Subject: [Tutor] HTML Parsing References: Message-ID: "Mitesh H. Budhabhatti" Wrote in message: (please post in text email, not html. Doesn't matter for most people on this particular message, but it's the polite thing to do) I see others have answered the programming question, but there's a separate one. What is the license of the particular ste, yahoo in this case. For an occasional scrape, nobody's likely to mind. But if you plan any volume, using the official api is more polite. -- DaveA From jarod_v6 at libero.it Wed May 28 21:16:44 2014 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Wed, 28 May 2014 21:16:44 +0200 (CEST) Subject: [Tutor] How parse files in function of number of lines Message-ID: <1028415800.874461401304604023.JavaMail.defaultUser@defaultHost> Dear all! I have two example files: tmp.csv: name value root mark 34 yes tmp2.csv name value root I want to print a different text if I have more than one row and if I have only one row. My code is this: with open("tmp.csv") as p: header =p.next() for i in p: print i g = () if not g: print header mark 34 yes no I want to obtain only where I have only the header the header string? How can I do this?Thnks for your great patience and help! From wescpy at gmail.com Wed May 28 22:46:36 2014 From: wescpy at gmail.com (wesley chun) Date: Wed, 28 May 2014 13:46:36 -0700 Subject: [Tutor] I am having difficulty grasping 'generators' In-Reply-To: References: Message-ID: I'm not going to add too much more to all the replies here already, but one of my students did record a quick <6-minute video in one of my courses where i explained generators. hopefully you find it useful! It's about halfway down the page at http://cyberwebconsulting.com. (Also for those learning Python and in the San Francisco area, I'm offering another intensive 3-day course mid-summer -- more info on the same page. Ping me privately for more details or if you have questions!) Cheers, --Wesley On Tue, May 27, 2014 at 12:27 PM, Degreat Yartey wrote: > I am studying python on my own (i.e. i am between the beginner and > intermediate level) and i haven't met any difficulty until i reached the > topic 'Generators and Iterators'. > I need an explanation so simple as using the expression 'print ()', in > this case 'yield'. > Python 2.6 here! > Thank you. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "A computer never does what you want... only what you tell it." +wesley chun : wescpy at gmail : @wescpy Python training & consulting : http://CyberwebConsulting.com "Core Python" books : http://CorePython.com Python blog: http://wescpy.blogspot.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From ss3141 at att.com Wed May 28 23:06:37 2014 From: ss3141 at att.com (SABARWAL, SHAL) Date: Wed, 28 May 2014 21:06:37 +0000 Subject: [Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock In-Reply-To: References: Message-ID: Hi, Sorry for getting back so late on this. First time user of the mailing list, so was not aware of the protocol. Anyway the problem occurs not frequently. Haven't been able to recreate it on demand. The calling application(applicationCode.py) has a basic - form = cgi.FieldStorage() I have attached cgi.py, mimetools.py and tempfile.py The error is pointed out at the import statement as below File "applicationCode.py", line 4, in import cgi File "/usr/local/lib/python2.7/cgi.py", line 51, in import mimetools File "/usr/local/lib/python2.7/mimetools.py", line 6, in import tempfile File "/usr/local/lib/python2.7/tempfile.py", line 83, in _once_lock = _allocate_lock() Thanks Shal From: SABARWAL, SHAL Sent: Friday, May 23, 2014 7:57 AM To: tutor at python.org Subject: cgi.FieldStorage() causing thread.error: can't allocate lock Wondering if anyone came across this error in using form = cgi.FieldStorage() import tempfile File /tempfile.py", line 83, in _once_lock = _allocate_lock() thread.error: can't allocate lock puthon version 2.7, on HP-UX 11.11 -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: cgi.py Type: application/octet-stream Size: 34505 bytes Desc: cgi.py URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: mimetools.py Type: application/octet-stream Size: 7168 bytes Desc: mimetools.py URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: tempfile.py Type: application/octet-stream Size: 18061 bytes Desc: tempfile.py URL: From alan.gauld at btinternet.com Thu May 29 02:11:20 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 29 May 2014 01:11:20 +0100 Subject: [Tutor] How parse files in function of number of lines In-Reply-To: <1028415800.874461401304604023.JavaMail.defaultUser@defaultHost> References: <1028415800.874461401304604023.JavaMail.defaultUser@defaultHost> Message-ID: On 28/05/14 20:16, jarod_v6 at libero.it wrote: > Dear all! > I have two example files: > tmp.csv: > name value root > mark 34 yes > > tmp2.csv > name value root > I understood down to here. > I want to print a different text if I have more than one row and if I have > only one row. This is not clear. Where do you want to print this text? What kind of text? More than one row where? In file 1? file 2? or both? My code is this: > with open("tmp.csv") as p: > header =p.next() Probably easier to use readline() > for i in p: > print i > g = () I've no idea what you think this is doing? It creates an empty tuple so will always evaluate to False > if not g: > print header > mark 34 yes > > no huh ???? > I want to obtain only where I have only the header the header string? How can > I do this?Thnks for your great patience and help! You might want to investigate the csv module and in particular the DictReader class. It might be easier for your purposes. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From davea at davea.name Thu May 29 09:07:11 2014 From: davea at davea.name (Dave Angel) Date: Thu, 29 May 2014 03:07:11 -0400 (EDT) Subject: [Tutor] How parse files in function of number of lines References: <1028415800.874461401304604023.JavaMail.defaultUser@defaultHost> Message-ID: "jarod_v6 at libero.it" Wrote in message: > Dear all! > I have two example files: > tmp.csv: > name value root > mark 34 yes > > tmp2.csv > name value root > > > I want to print a different text if I have more than one row and if I have > only one row. My code is this: > with open("tmp.csv") as p: > header =p.next() > for i in p: > print i > g = () > if not g: > print header > mark 34 yes > > no > > I want to obtain only where I have only the header the header string? How can > I do this?Thnks for your great patience and help! > The most straightforward way is to define a flag that starts False, and becomes True if there are any lines after the header. has_body = False for line in infile: print line has_body = True if has_body: print header else: print "something else" -- DaveA From Jude.Mudannayake at nccuk.com Thu May 29 11:48:05 2014 From: Jude.Mudannayake at nccuk.com (Jude Mudannayake) Date: Thu, 29 May 2014 09:48:05 +0000 Subject: [Tutor] Python Code Error Message-ID: <08015478AB52E7428A243922102705D742E4EBA0@NCC-EXCHANGE-01.ncc.local> Dear Sir/Madam I am currently having some trouble with a python script that I developed and I would like to know if you could have a look. I basically am attaching the script as part of .py file in this email. The script in its essence is set up to take a set of data from a CSV file (containing x,y points) and then arrange them into coordinates points i.e. gather the two sets of x and y to form coordinates (x1,y1) and (x2, y2) etc... to be plotted as a sketch within ABAQUS FE software.. As I run the script I get an error stating the following: 'TypeError: point1; found int, expecting tuple' - This is referring to line 57 of the code which is essentially the critical command. It is a command that instructs to join coordinate points thus creating a line. The thing is I am pretty sure I have changed inputs into tuples from line 16 of the code to 19. I am not too sure what the issue is here. Could you get back to me with what I should look to do in this case. Thanks in Advance Jude Mudannayake MSc BEng DIC Stress Engineer The National Composites Centre Feynman Way Central, Bristol & Bath Science Park, Emersons Green, Bristol, BS16 7FS Project part financed by the European Union with ?9M from the European Regional Development Fund 2007-2013, through the South West Regional Development Agency under the Competitiveness Operating Programme. --------------------------------------------------------------------------------------------------- This email and any attachments to it may be confidential and are intended solely for the use of the individual to whom it is addressed. Any views or opinions expressed are solely those of the author and do not necessarily represent those of the National Composites Centre (NCC), the National Composites Centre Operations Limited (NCCOL) or any of its members or associates. If you are not the intended recipient of this email, you must neither take any action based upon its contents, nor copy or show it to anyone. Please contact the sender if you believe you have received this email in error. -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: csv_coordinatesgen_part.py Type: application/octet-stream Size: 1901 bytes Desc: csv_coordinatesgen_part.py URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: trial.csv Type: application/octet-stream Size: 137 bytes Desc: trial.csv URL: From wolfgang.maier at biologie.uni-freiburg.de Thu May 29 11:12:02 2014 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Thu, 29 May 2014 11:12:02 +0200 Subject: [Tutor] How parse files in function of number of lines In-Reply-To: <1028415800.874461401304604023.JavaMail.defaultUser@defaultHost> References: <1028415800.874461401304604023.JavaMail.defaultUser@defaultHost> Message-ID: On 28.05.2014 21:16, jarod_v6 at libero.it wrote: > Dear all! > I have two example files: > tmp.csv: > name value root > mark 34 yes > > tmp2.csv > name value root > > > I want to print a different text if I have more than one row and if I have > only one row. My code is this: > with open("tmp.csv") as p: > header =p.next() in general, DON'T use the next method, but the next() function (or readline as Alan suggested): header = next(p) this will work also in python3, where the next method is gone. Wolfgang From alan.gauld at btinternet.com Thu May 29 18:00:08 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 29 May 2014 17:00:08 +0100 Subject: [Tutor] Python Code Error In-Reply-To: <08015478AB52E7428A243922102705D742E4EBA0@NCC-EXCHANGE-01.ncc.local> References: <08015478AB52E7428A243922102705D742E4EBA0@NCC-EXCHANGE-01.ncc.local> Message-ID: On 29/05/14 10:48, Jude Mudannayake wrote: > its essence is set up to take a set of data from a CSV file (containing > x,y points) and then arrange them into coordinates points > get an error stating the following: > > ?TypeError: point1; found int, expecting tuple? ? This is referring to > line 57 of the code which is essentially the critical command. It is a > command that instructs to join coordinate points thus creating a line. Don't summarize the traceback. Send the complete error text. Summarizing usually tells us what you *think* the error is rather than what Python is actually telling you. > The thing is I am pretty sure I have changed inputs into tuples from > line 16 of the code to 19. Maybe... If the code is not too long (<100 lines) its usually better to embed it in the message rather than send an attachment... import csv f = open ('trial.csv') csv_f = csv.reader(f) Ay1 = [] Az1 = [] Ay2 = [] Az2 = [] for row in csv_f: Ay1.append(eval(row[1])) Az1.append(eval(row[2])) Ay2.append(eval(row[3])) Az2.append(eval(row[4])) As a general rule don't use eval(). It is very insecure and potentially masks errors because if the code is not what you expect strange things can silently happen. If you expect integer inputs use int(). Then if its not an int, python can tell you. As it stands there is a universe of badness that eval can interpret and not tell you a thing. Ay11 = tuple(Ay1) Az11 = tuple(Az1) Ay22 = tuple(Ay2) Az22 = tuple(Az2) Here are the lines where you say the error occurs: for i in range(len(xyCoordsInner)-1): mySketch.Line(point1=xyCoordsInner[i], point2=xyCoordsInner[i+1]) for i in range(len(xyCoordsOuter)-1): mySketch.Line(point1=xyCoordsOuter[i], point2=xyCoordsOuter[i+1]) Now its not obvious how we get from your lists/tuples Ay11 etc to xyCoordsInner/Outer? Ok I found it here: for j in range(0,len(Ay22)): k2 = (Ay22[j],Az22[j]) #print k2 myList1=k2 xyCoordsOuter = tuple(myList1) #print xyCoordsOuter This is a bizarre bit of code. It loops over a pair of tuples. It creates a tuple from the tuples. It sets a second variable to point to the same tuple It then sets a third variable to point at the same tuple converted to a tuple(ie the same as it started) It then goes round the loop again throwing away the previous tuple until you finally wind up with the last two elements as a tuple. You could replace the entire loop with xyXoordOuter = (Ay22[-1],Az22[-1]) Which is faster and clearer. Of course if Ay and Az have different lengths you get into difficulties... I'm not sure if any of that helps but I'd definitely get rid of the evals. And I'd take a close look at the loop above to see if that's really what you want to do. And finally I'd print Point1 to see what exactly the error is talking about. And if you need more help, send the complete error trace and embed the code in the message. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From s.shall at virginmedia.com Thu May 29 18:12:27 2014 From: s.shall at virginmedia.com (Sydney Shall) Date: Thu, 29 May 2014 17:12:27 +0100 Subject: [Tutor] pylab axis Message-ID: <53875C6B.6000901@virginmedia.com> I would like to start by thanking all the tutors for their wonderful genourosity and excellent advice. My problem concerns forcing pylab to give me the y axis I want. My code is as follows: pylab.figure(10) pylab.title("Ratio of realised capital to advanced capital.") pylab.xlabel("Time [Cycles of Capital reproduction]") pylab.ylabel("Ratio of realised capital to advanced capital.") pylab.xlim = ((0.0, 1.5)) pylab.plot(cycles, ratiocrtoca) pylab.show(10) My pylab output is as follows: I regret I do not know how to put the pdf file in the message, so I have attached the pdf file of the graph. Please guide me on this too. My problem is that I wish to force the y axis to simply be from 0.0 to 1.5 without the detail which is just arithmetic noise, I think. With thanks, Sydney -- Sydney Shall -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Figure10.pdf Type: application/pdf Size: 13725 bytes Desc: not available URL: From dyoo at hashcollision.org Thu May 29 20:09:22 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 29 May 2014 11:09:22 -0700 Subject: [Tutor] pylab axis In-Reply-To: <53875C6B.6000901@virginmedia.com> References: <53875C6B.6000901@virginmedia.com> Message-ID: > My problem concerns forcing pylab to give me the y axis I want. > > My code is as follows: > > pylab.figure(10) > pylab.title("Ratio of realised capital to advanced capital.") > pylab.xlabel("Time [Cycles of Capital reproduction]") > pylab.ylabel("Ratio of realised capital to advanced capital.") > pylab.xlim = ((0.0, 1.5)) > pylab.plot(cycles, ratiocrtoca) > pylab.show(10) > > > My problem is that I wish to force the y axis to simply be from 0.0 to 1.5 > without the detail which is just arithmetic noise, I think. It appears that you limit the x-axis's bounds by using pylab.xlim(). Have you tried doing the same with pylab.ylim()? I'm unfamilar with pylab, but have found a tutorial in: http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut1.html. I think that 'ylim' is what you're looking for, but I'm not positive yet. From breamoreboy at yahoo.co.uk Thu May 29 21:26:58 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 29 May 2014 20:26:58 +0100 Subject: [Tutor] pylab axis In-Reply-To: <53875C6B.6000901@virginmedia.com> References: <53875C6B.6000901@virginmedia.com> Message-ID: On 29/05/2014 17:12, Sydney Shall wrote: > I would like to start by thanking all the tutors for their wonderful > genourosity and excellent advice. > > My problem concerns forcing pylab to give me the y axis I want. > > My code is as follows: > > pylab.figure(10) > > pylab.title("Ratio of realised capital to advanced capital.") > > pylab.xlabel("Time [Cycles of Capital reproduction]") > > pylab.ylabel("Ratio of realised capital to advanced capital.") > > pylab.xlim = ((0.0, 1.5)) > > pylab.plot(cycles, ratiocrtoca) > > pylab.show(10) > > > My pylab output is as follows: > > > I regret I do not know how to put the pdf file in the message, so I have > attached the pdf file of the graph. Please guide me on this too. > > My problem is that I wish to force the y axis to simply be from 0.0 to > 1.5 without the detail which is just arithmetic noise, I think. > > With thanks, > > Sydney > > -- > Sydney Shall > The joys of the interactive prompt. In [3]: help(pylab.ylim) Help on function ylim in module matplotlib.pyplot: ylim(*args, **kwargs) Get or set the *y*-limits of the current axes. :: ymin, ymax = ylim() # return the current ylim ylim( (ymin, ymax) ) # set the ylim to ymin, ymax ylim( ymin, ymax ) # set the ylim to ymin, ymax If you do not specify args, you can pass the *ymin* and *ymax* as kwargs, e.g.:: ylim(ymax=3) # adjust the max leaving min unchanged ylim(ymin=1) # adjust the min leaving max unchanged Setting limits turns autoscaling off for the y-axis. The new axis limits are returned as a length 2 tuple. Aside, you appear to be using matplotlib in a strange way. If your code is in a script, you'd use a line like this. import matplotlib.pyplot as plt If you're working interactively you'd use. from pylab import * You have a half way house. Finally this mailing list is mainly aimed at people learning the Python language and not third party modules. For matplotlib you can find a very helpful group here gmane.comp.python.matplotlib.general (and other places), I know they don't bite as I've been there :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From breamoreboy at yahoo.co.uk Thu May 29 21:33:41 2014 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 29 May 2014 20:33:41 +0100 Subject: [Tutor] Python Code Error In-Reply-To: <08015478AB52E7428A243922102705D742E4EBA0@NCC-EXCHANGE-01.ncc.local> References: <08015478AB52E7428A243922102705D742E4EBA0@NCC-EXCHANGE-01.ncc.local> Message-ID: On 29/05/2014 10:48, Jude Mudannayake wrote: [snipped to pieces] for i in range(0,len(Ay11)): k1 = (Ay11[i],Az11[i]) #print k1 myList=k1 xyCoordsInner = tuple(myList) #print xyCoordsInner Further to the reply from Alan Gauld, if you're writing code like this in Python you're almost certainly doing it wrong. For loops usually look like this. for ay in Ay11: doSomeThingWithAy(ay) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com From dyoo at hashcollision.org Thu May 29 22:44:48 2014 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 29 May 2014 13:44:48 -0700 Subject: [Tutor] Fwd: pylab axis In-Reply-To: <53877F1E.1080701@virginmedia.com> References: <53875C6B.6000901@virginmedia.com> <53877F1E.1080701@virginmedia.com> Message-ID: [Forwarding to tutor at python.org. Unfortunately, I don't have time to look at this personally at the moment. Please use CC to keep the conversation on the mailing list, so that others can continue to follow up.] ---------- Forwarded message ---------- From: Sydney Shall Date: Thu, May 29, 2014 at 11:40 AM Subject: Re: [Tutor] pylab axis To: Danny Yoo Dear Danny, I have tried ylim and it does not present it correctly. It present the axis on an enlarged with +XXXe-y on the scale, as can be seen from the pdf of the output. I want to get rid of this extra bit. The strange part is that when I first used ylim it did what I wanted, but now does not. I am flummoxed. But thanks anyway. Sydney On 29/05/2014 19:09, Danny Yoo wrote: >> >> My problem concerns forcing pylab to give me the y axis I want. >> >> My code is as follows: >> >> pylab.figure(10) >> pylab.title("Ratio of realised capital to advanced capital.") >> pylab.xlabel("Time [Cycles of Capital reproduction]") >> pylab.ylabel("Ratio of realised capital to advanced capital.") >> pylab.xlim = ((0.0, 1.5)) >> pylab.plot(cycles, ratiocrtoca) >> pylab.show(10) >> >> >> My problem is that I wish to force the y axis to simply be from 0.0 to 1.5 >> without the detail which is just arithmetic noise, I think. > > It appears that you limit the x-axis's bounds by using pylab.xlim(). > Have you tried doing the same with pylab.ylim()? > > > I'm unfamilar with pylab, but have found a tutorial in: > http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut1.html. > > I think that 'ylim' is what you're looking for, but I'm not positive yet. > -- Sydney Shall From mitesh.budhabhatti at gmail.com Fri May 30 07:39:55 2014 From: mitesh.budhabhatti at gmail.com (Mitesh H. Budhabhatti) Date: Fri, 30 May 2014 11:09:55 +0530 Subject: [Tutor] HTML Parsing In-Reply-To: References: Message-ID: Alan Gauld thanks for the reply. I'll try that out. Warm Regards, *Mitesh H. Budhabhatti* Cell# +91 99040 83855 On Wed, May 28, 2014 at 11:19 PM, Danny Yoo wrote: > > I am using Python 3.3.3 on Windows 7. I would like to know what is the > best > > method to do HTML parsing? For example, I want to connect to > www.yahoo.com > > and get all the tags and their values. > > > For this purpose, you may want to look at the APIs that the search > engines provide, rather than try to web-scrape the human-focused web > pages. Otherwise, your program will probably be fragile to changes in > the structure of the web site. > > > A search for search APIs comes up with hits like this: > > https://developer.yahoo.com/boss/search/ > > https://developers.google.com/web-search/docs/#fonje_snippets > > http://datamarket.azure.com/dataset/bing/search > > https://pypi.python.org/pypi/duckduckgo2 > > > If you can say more about what you're planning to do, perhaps someone > has already provided a programmatic interface to it. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ritwikraghav14 at gmail.com Fri May 30 15:14:38 2014 From: ritwikraghav14 at gmail.com (Ritwik Raghav) Date: Fri, 30 May 2014 18:44:38 +0530 Subject: [Tutor] self keyword in recursive function Message-ID: I joined the topcoder community tomorrow and tried solving the PersistentNumber problem: "Given a number x, we can define p(x) as the product of the digits of x. We can then form a sequence x, p(x), p(p(x))... The persistence of x is then defined as the index (0-based) of the first single digit number in the sequence. For example, using 99, we get the sequence 99, 9*9 = 81, 8*1 = 8. Thus, the persistence of 99 is 2. You will be given n, and you must return its persistence." It asks to define a function def getPersistence(self, n). I solved the problem in IDLE. My code is: def getPersistence(n,count = 0): product = 1 if len(str(n)) == 1: return count else: a = str(n) for i in range(len(a)): product *= int(a[i]) count += 1 return getPersistence(product,count) Now plz help me to convert the above code in specified format. Or help me understand how to recreate the function as specified. -- Ritwik Raghav -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri May 30 17:45:20 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 30 May 2014 16:45:20 +0100 Subject: [Tutor] self keyword in recursive function In-Reply-To: References: Message-ID: On 30/05/14 14:14, Ritwik Raghav wrote: > I joined the topcoder community tomorrow and tried solving the > PersistentNumber problem: Time travel! I love it already... :-) > 8*1 = 8. Thus, the persistence of 99 is 2. You will be given n, and you > must return its persistence." > > It asks to define a function def getPersistence(self, n). I solved the > problem in IDLE. My code is: You seem to have solved the problem. Your code could be cleaned up a little but it seems to work. The fact that the exercise asks for a self argument suggests that it is supposed to be part of a class definition. Is there a class definition anywhere that you are supposed to extend? |Some comments on the code below: > def getPersistence(n,count = 0) Since you never get passed count as an argument you could just make it a variable. You only need it as an argument if you use recursion but the problem didn't ask for that... > product = 1 > if len(str(n)) == 1: > return count > else: > a = str(n) > for i in range(len(a)): > product *= int(a[i]) This is not good Python style. Its better to use for c in a: product += int(c) > count += 1 > return getPersistence(product,count) Rather than using recursion you could have used a while loop (untested code!): if n < 10: return 0 product = 1 while True: count += 1 a = str(n) for c in a: product *= int(c) if product < 10: break return count > Now plz help me to convert the above code in specified format. Or help > me understand how to recreate the function as specified. You have created a function that does what is needed, it just doesn't have a self parameter. self is only used when the function is part of a class definition. Without sight of the class that it should be part of we can't offer much more help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Fri May 30 15:44:31 2014 From: __peter__ at web.de (Peter Otten) Date: Fri, 30 May 2014 15:44:31 +0200 Subject: [Tutor] self keyword in recursive function References: Message-ID: Ritwik Raghav wrote: > I joined the topcoder community tomorrow and tried solving the > PersistentNumber problem: > "Given a number x, we can define p(x) as the product of the digits of x. > We can then form a sequence x, p(x), p(p(x))... The persistence of x is > then defined as the index (0-based) of the first single digit number in > the sequence. For example, using 99, we get the sequence 99, 9*9 = 81, 8*1 > = 8. Thus, the persistence of 99 is 2. You will be given n, and you must > return its persistence." > > It asks to define a function def getPersistence(self, n). I solved the > problem in IDLE. My code is: > > def getPersistence(n,count = 0): > product = 1 > if len(str(n)) == 1: > return count > else: > a = str(n) > for i in range(len(a)): > product *= int(a[i]) > count += 1 > return getPersistence(product,count) > > Now plz help me to convert the above code in specified format. Or help me > understand how to recreate the function as specified. The "topcoder" site is probably infested with the world view of Java where every function is a method inside a class. You may have to wrap your code into something like class PersistentNumber: def getPersistence(self, n, count=0): ... # your code with a minor change (*) (*) Inside the method the recursive call becomes self.getPersistence(product, count) instead of just getPersistence(product, count) From aaronmisquith at gmail.com Fri May 30 11:41:19 2014 From: aaronmisquith at gmail.com (Aaron Misquith) Date: Fri, 30 May 2014 15:11:19 +0530 Subject: [Tutor] Library for .ppt to .txt conversion Message-ID: Like pypdf is used to convert pdf to text; is there any library that is used in converting .ppt files to .txt? Even some sample programs will be helpful. -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Fri May 30 18:57:03 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Fri, 30 May 2014 09:57:03 -0700 Subject: [Tutor] Library for .ppt to .txt conversion In-Reply-To: References: Message-ID: On Fri, May 30, 2014 at 2:41 AM, Aaron Misquith wrote: > Like pypdf is used to convert pdf to text; is there any library that is > used in converting .ppt files to .txt? Even some sample programs will be > helpful. > I suspect you'd need to use PowerPoint itself to do that cleanly; you can definitely drive PowerPoint from Python if you so desire, though: http://www.s-anand.net/blog/automating-powerpoint-with-python/ If anybody's written a package to brute-force the text out of a .ppt file without using PowerPoint, though, I'm unaware of it. That way lies madness, I suspect. (The new MS Office formats - .docx, .xlsx, .pptx - are XML files inside of a renamed ZIP container; it should be fairly easy to get the text out of a .pptx file using any one of Python's XML libraries. But the older format is proprietary and extremely scary.) -------------- next part -------------- An HTML attachment was scrubbed... URL: From chigga101 at gmail.com Fri May 30 19:25:15 2014 From: chigga101 at gmail.com (Matthew Ngaha) Date: Fri, 30 May 2014 18:25:15 +0100 Subject: [Tutor] Question about scraping Message-ID: Hey all. I've been meaning to get into web scraping and was pointed to the directions of lxml (library) and scrapy (framework). Can I ask in terms of web scraping, what's the difference between a library and a framework? Surely everyone should use a framework but I get the idea more people use the available libraries like lxml over scrapy. Any advantages or disadvantages for each? And which do you use out of the 2? I also have another question due to reading this: "[Tutor] HTML Parsing" . It seems some experienced coders don't find scraping as useful since web sites offer apis for their data. Is the idea/concept here the same as scraping? And is there any use of scraping anymore when sites are now offering their data? From alan.gauld at btinternet.com Fri May 30 20:20:50 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 30 May 2014 19:20:50 +0100 Subject: [Tutor] Question about scraping In-Reply-To: References: Message-ID: On 30/05/14 18:25, Matthew Ngaha wrote: > Hey all. I've been meaning to get into web scraping and was pointed to > the directions of lxml (library) and scrapy (framework). Can I ask in > terms of web scraping, what's the difference between a library and a > framework? I don;t know of anything web specific. A framework tends to be a much bigger thing than a library. It dictates the architecture of the solution rather than just providing a few functions/classes. > Surely everyone should use a framework Why? A framework is usually the fastest way to get started from zero but if you are integrating with an existing solution then a framework can add layers of unneeded complexity. As always the correct solution depends on the problem. > I also have another question due to reading this: "[Tutor] HTML > Parsing" . It seems some experienced coders don't find scraping as > useful since web sites offer apis for their data. Is the idea/concept > here the same as scraping? No, its completely different. Scraping means trying to decipher a public web page that is designed for display in a browser. Web pages are prone to frequent change and the data often moves around within the page meaning constant updates to your scraper. Also web pages are increasingly dynamically generated which makes scraping much harder. An API is relatively stable and returns just the data elements of the page. As such its usually easier to use, more secure, more stable, faster (lower bandwidth required) and has much less impact on the providers network/servers thus improving performance for everyone. > And is there any use of scraping anymore > when sites are now offering their data? If a site offers an API that returns the data you need then use it, If not you have few alternatives to scraping (although scraping may be 'illegal' anyway due to the impact on other users). But scraping, whether a web page or a GUI or an old mainframe terminal is always a fragile and unsatisfactory solution. An API will always be better in the long term if it exists. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Fri May 30 20:24:35 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 30 May 2014 19:24:35 +0100 Subject: [Tutor] Library for .ppt to .txt conversion In-Reply-To: References: Message-ID: On 30/05/14 10:41, Aaron Misquith wrote: > Like pypdf is used to convert pdf to text; is there any library that is > used in converting .ppt files to .txt? Even some sample programs will be > helpful. Bearing in mind that Powerpoint is intended for graphical presentations the text elements are not necessarily going to be useful. Often Powerpoint text is actually part of a graphic anyway. If the Powerpoint is just a set of bullet points (shame on the presenter!) you probably don't want the text unless you can also get the notes. I don't know of any libraries that can do that. But one option is that Open/Libre office can import Powerpoint and apparently has a Python API which you could use to drive an export from there. Just a thought... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Fri May 30 22:21:27 2014 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Fri, 30 May 2014 21:21:27 +0100 (BST) Subject: [Tutor] Question about scraping In-Reply-To: References: Message-ID: <1401481287.81823.YahooMailNeo@web186005.mail.ir2.yahoo.com> On Fri, May 30, 2014 at 7:20 PM, Alan Gauld wrote: > > >> If a site offers an API that returns the data you need then use it, >> If not you have few alternatives to scraping (although scraping >> may be 'illegal' anyway due to the impact on other users). But scraping, >> whether a web page or a GUI or an old mainframe terminal >> is always a fragile and unsatisfactory solution. > >Okay I think learning how to scrap (library or framework) is not worth >the trouble. Especially if some people consider it illegal. Thanks for >the input. > > >As I say, sometimes you have no choice but to scrape. Its only 'illegal' if the site owner says so, in other words if their terms of use? prohibit web scraping. If they have gone to the effort (and cost) ?of providing? an API?then it probably means scraping is prohibited. But many (most!) sites don't offer APIs ?and most smaller sites don't prohibit scraping, so it is still a? valid technique. But before you try its always worth checking whether an API? exists and whether scraping is permitted. And by 'illegal' I mean you are unlikely to be prosecuted in a court but? you are likely to find your IP address and/or account closed. The systems? generally monitor activity and if an account is navigating through pages? too quickly to be a human they often close the account down. Alan g. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chigga101 at gmail.com Sat May 31 04:11:45 2014 From: chigga101 at gmail.com (Matthew Ngaha) Date: Sat, 31 May 2014 03:11:45 +0100 Subject: [Tutor] Question about scraping In-Reply-To: <1401481287.81823.YahooMailNeo@web186005.mail.ir2.yahoo.com> References: <1401481287.81823.YahooMailNeo@web186005.mail.ir2.yahoo.com> Message-ID: Thanks for the response Alan. I forgot to reply to tutor on my 2nd comment. Just incase someone might want to see it, here it is: "Okay I think learning how to scrap (library or framework) is not worth the trouble. Especially if some people consider it illegal. Thanks for the input." From ritwikraghav14 at gmail.com Sat May 31 06:41:18 2014 From: ritwikraghav14 at gmail.com (Ritwik Raghav) Date: Sat, 31 May 2014 10:11:18 +0530 Subject: [Tutor] self keyword in recursive function Message-ID: Alan Gauld wrote: >On 30/05/14 14:14, Ritwik Raghav wrote: >> I joined the topcoder community tomorrow and tried solving the >> PersistentNumber problem: > >Time travel! I love it already... :-) > >> 8*1 = 8. Thus, the persistence of 99 is 2. You will be given n, and you > >must return its persistence." >> >> It asks to define a function def getPersistence(self, n). I solved the >> problem in IDLE. My code is: > >You seem to have solved the problem. >Your code could be cleaned up a little but it seems >to work. The fact that the exercise asks for a self >argument suggests that it is supposed to be part of >a class definition. That much I figured out. But I have never worked with classes in Python. Neither have I read about them. Please suggest one book I should read to understand class and objects in Python. > >Is there a class definition anywhere that you are >supposed to extend? > I have read that Topcoder implements the code inside a class. So, yes this code is supposed to be the part of that class and public. >|Some comments on the code below: > >> def getPersistence(n,count = 0) > >Since you never get passed count as an argument you >could just make it a variable. You only need it as >an argument if you use recursion but the problem >didn't ask for that... > >> product = 1 >> if len(str(n)) == 1: >> return count >> else: >> a = str(n) >> for i in range(len(a)): >> product *= int(a[i]) > >This is not good Python style. >Its better to use > >for c in a: > product += int(c) > Thanks, I will implement so. >> count += 1 >> return getPersistence(product,count) > >Rather than using recursion you could have used >a while loop (untested code!): > >if n < 10: > return 0 >product = 1 >while True: > count += 1 > a = str(n) > for c in a: > product *= int(c) > if product < 10: > break >return count > I thought recursion would be better. >> Now plz help me to convert the above code in specified format. Or help >> me understand how to recreate the function as specified. > >You have created a function that does what is needed, it just doesn't >have a self parameter. self is only used when the function is part of a >class definition. > >Without sight of the class that it should be part of we can't offer much >more help. Thanks for your help Alan. -- Ritwik Raghav -------------- next part -------------- An HTML attachment was scrubbed... URL: From ritwikraghav14 at gmail.com Sat May 31 07:16:33 2014 From: ritwikraghav14 at gmail.com (Ritwik Raghav) Date: Sat, 31 May 2014 10:46:33 +0530 Subject: [Tutor] self keyword in recursive function Message-ID: Peter Otten wrote: >Ritwik Raghav wrote: > >> I joined the topcoder community tomorrow and tried solving the >> PersistentNumber problem: >> "Given a number x, we can define p(x) as the product of the digits of x. >> We can then form a sequence x, p(x), p(p(x))... The persistence of x is >> then defined as the index (0-based) of the first single digit number in >> the sequence. For example, using 99, we get the sequence 99, 9*9 = 81, 8*1 >> = 8. Thus, the persistence of 99 is 2. You will be given n, and you must >> return its persistence." >> >> It asks to define a function def getPersistence(self, n). I solved the >> problem in IDLE. My code is: >> >> def getPersistence(n,count = 0): >> product = 1 >> if len(str(n)) == 1: >> return count >> else: >> a = str(n) >> for i in range(len(a)): >> product *= int(a[i]) >> count += 1 >> return getPersistence(product,count) >> >> Now plz help me to convert the above code in specified format. Or help me >> understand how to recreate the function as specified. > >The "topcoder" site is probably infested with the world view of Java where >every function is a method inside a class. You may have to wrap your code >into something like > >class PersistentNumber: > def getPersistence(self, n, count=0): > ... # your code with a minor change (*) > > >(*) Inside the method the recursive call becomes > > self.getPersistence(product, count) > >instead of just > > getPersistence(product, count) It has again given some error I do not understand. This time my code is: count = 0 def getPersistence(self,n): product = 1 if len(str(n)) == 1: return self.count else: a = str(n) for i in a: product *= int(i) self.count += 1 return self.getPersistence(product) and the error is: Correct Return Value: No Answer check result: Result must be not null. Execution Time: 0.017s Peak memory used: 24.551MB abnormal termination (exit 1) Standard Output: Standard Error: Traceback (most recent call last): File "Wrapper.py", line 182, in AttributeError: 'module' object has no attribute 'PersistentNumber' I do not understand what it is trying to tell me? I tried to test it for 99. -- Ritwik Raghav -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Sat May 31 07:55:53 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Fri, 30 May 2014 22:55:53 -0700 Subject: [Tutor] self keyword in recursive function In-Reply-To: References: Message-ID: On Fri, May 30, 2014 at 10:16 PM, Ritwik Raghav wrote: > It has again given some error I do not understand. This time my code is: > > count = 0 > def getPersistence(self,n): > > product = 1 > if len(str(n)) == 1: > return self.count > else: > a = str(n) > for i in a: > product *= int(i) > self.count += 1 > return self.getPersistence(product) > > and the error is: > > Correct Return Value: No > > Answer check result: > Result must be not null. > > Execution Time: 0.017s > > Peak memory used: 24.551MB > > abnormal termination (exit 1) > > Standard Output: > > > Standard Error: > Traceback (most recent call last): > File "Wrapper.py", line 182, in > AttributeError: 'module' object has no attribute 'PersistentNumber' > > > I do not understand what it is trying to tell me? I tried to test it for > 99. > > The error is in some part of your code that you _haven't_ posted here - please post your entire script (as an attachment, if that's more convenient) and we'll be better able to help. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ritwikraghav14 at gmail.com Sat May 31 08:06:39 2014 From: ritwikraghav14 at gmail.com (Ritwik Raghav) Date: Sat, 31 May 2014 11:36:39 +0530 Subject: [Tutor] self keyword in recursive function In-Reply-To: References: Message-ID: That's all the code I'm writing. The complete problem statement is: http://pastebin.com/E970qYXk On Sat, May 31, 2014 at 11:25 AM, Marc Tompkins wrote: > On Fri, May 30, 2014 at 10:16 PM, Ritwik Raghav > wrote: > > >> It has again given some error I do not understand. This time my code is: >> >> count = 0 >> def getPersistence(self,n): >> >> product = 1 >> if len(str(n)) == 1: >> return self.count >> else: >> a = str(n) >> for i in a: >> product *= int(i) >> self.count += 1 >> return self.getPersistence(product) >> >> and the error is: >> >> Correct Return Value: No >> >> Answer check result: >> Result must be not null. >> >> Execution Time: 0.017s >> >> Peak memory used: 24.551MB >> >> abnormal termination (exit 1) >> >> Standard Output: >> >> >> Standard Error: >> Traceback (most recent call last): >> File "Wrapper.py", line 182, in >> AttributeError: 'module' object has no attribute 'PersistentNumber' >> >> >> I do not understand what it is trying to tell me? I tried to test it for >> 99. >> >> > The error is in some part of your code that you _haven't_ posted here - > please post your entire script (as an attachment, if that's more > convenient) and we'll be better able to help. > -- Ritwik Raghav -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Sat May 31 08:23:35 2014 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Fri, 30 May 2014 23:23:35 -0700 Subject: [Tutor] self keyword in recursive function In-Reply-To: References: Message-ID: On Fri, May 30, 2014 at 11:06 PM, Ritwik Raghav wrote: > That's all the code I'm writing. > That can't be true - the 11 lines of code you posted doesn't include anything that would give you "Correct Return Value: No", let alone any reference to PersistentNumber. From the error message, it would appear that you've written (at least) 182 lines, and that the problem is on line 182 - but that's not what you're showing us. > The complete problem statement is: > http://pastebin.com/E970qYXk > Yes, but that's not _your_ code, so it tells us nothing about your error. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat May 31 10:32:09 2014 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 31 May 2014 09:32:09 +0100 Subject: [Tutor] self keyword in recursive function In-Reply-To: References: Message-ID: > On Fri, May 30, 2014 at 10:16 PM, Ritwik Raghav > > wrote: > and the error is: > > Correct Return Value: No > > Answer check result: > Result must be not null. > > Execution Time: 0.017s > > Peak memory used: 24.551MB > > abnormal termination (exit 1) > > Standard Output: > > > Standard Error: > Traceback (most recent call last): > File "Wrapper.py", line 182, in > AttributeError: 'module' object has no attribute 'PersistentNumber' This is not standard Python error output so I assume it has something to do with how you submit code to the site you are using. It looks like it has some kkind of testing framework that your code must comply with. You probably need to read their guidance on code submission closely. One possible bug I did notice in your code is here: count = 0 You define count outside the method but not as part of self. def getPersistence(self,n): product = 1 if len(str(n)) == 1: return self.count then you return self.count. That's a different value, which may be defaulting to None in your test environment? If you insist on using recursion I'd bring the count back as an argument defaulted to zero as you did in your original code. else: a = str(n) for i in a: product *= int(i) self.count += 1 return self.getPersistence(product) But without knowing how toploader tests your code its hard to say for sure what's going wrong. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos From aaronmisquith at gmail.com Sat May 31 10:15:20 2014 From: aaronmisquith at gmail.com (Aaron Misquith) Date: Sat, 31 May 2014 13:45:20 +0530 Subject: [Tutor] Library for .ppt to .txt conversion In-Reply-To: References: Message-ID: The only thing i want from the ppt's is text and ignoring all graphical representations. I need the text to perform various nltk operations. On Fri, May 30, 2014 at 11:54 PM, Alan Gauld wrote: > On 30/05/14 10:41, Aaron Misquith wrote: > >> Like pypdf is used to convert pdf to text; is there any library that is >> used in converting .ppt files to .txt? Even some sample programs will be >> helpful. >> > > Bearing in mind that Powerpoint is intended for graphical presentations > the text elements are not necessarily going to be useful. Often Powerpoint > text is actually part of a graphic anyway. > > If the Powerpoint is just a set of bullet points (shame on the presenter!) > you probably don't want the text unless you can > also get the notes. I don't know of any libraries that can do that. > > But one option is that Open/Libre office can import Powerpoint and > apparently has a Python API which you could use to drive an export > from there. Just a thought... > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mitesh.budhabhatti at gmail.com Sat May 31 10:38:34 2014 From: mitesh.budhabhatti at gmail.com (Mitesh H. Budhabhatti) Date: Sat, 31 May 2014 14:08:34 +0530 Subject: [Tutor] HTML Parsing In-Reply-To: References: Message-ID: > > I see others have answered the programming question, but there's > a separate one. What is the license of the particular ste, yahoo > in this case. For an occasional scrape, nobody's likely to mind. > But if you plan any volume, using the official api is more > polite. Thanks for the reply. We have a legacy intranet site that hosts reports in html. I need to convert reports in CSV format. So I wanted to connect to the site and parse to CSV. On Thu, May 29, 2014 at 12:10 AM, Dave Angel wrote: > "Mitesh H. Budhabhatti" Wrote in > message: > > (please post in text email, not html. Doesn't matter for most > people on this particular message, but it's the polite thing to > do) > > I see others have answered the programming question, but there's > a separate one. What is the license of the particular ste, yahoo > in this case. For an occasional scrape, nobody's likely to mind. > But if you plan any volume, using the official api is more > polite. > > -- > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Sat May 31 13:32:59 2014 From: davea at davea.name (Dave Angel) Date: Sat, 31 May 2014 07:32:59 -0400 (EDT) Subject: [Tutor] Library for .ppt to .txt conversion References: Message-ID: Aaron Misquith Wrote in message: > The only thing i want from the ppt's is text and ignoring all graphical representations. I > need the text to perform various nltk operations. > On Fri, May 30, 2014 at 11:54 PM, Alan Gauld wrote: >> Bearing in mind that Powerpoint is intended for graphical presentations the text >> elements are not necessarily going to be useful. Often Powerpoint text >> is actually part of a graphic anyway. 1. please don't top-post. Place your comments after the quoted text from the previous message. Please tell your mail program to use text, not html when posting here. 2. Alan has pointed out that there may not be any text in the ppt file, but just image data that represents the text. Similar to the way a scanned piece of paper has no text till you try to ocr it. -- DaveA From joel.goldstick at gmail.com Sat May 31 20:47:49 2014 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 31 May 2014 14:47:49 -0400 Subject: [Tutor] Question about scraping In-Reply-To: References: <1401481287.81823.YahooMailNeo@web186005.mail.ir2.yahoo.com> Message-ID: On May 30, 2014 10:12 PM, "Matthew Ngaha" wrote: > > Thanks for the response Alan. I forgot to reply to tutor on my 2nd > comment. Just incase someone might want to see it, here it is: > > "Okay I think learning how to scrap (library or framework) is not > worth the trouble. Especially if some people consider it illegal. > Thanks for the input." > _______________ Check out beautiful soup ________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From loki85 at outlook.com Sat May 31 11:23:21 2014 From: loki85 at outlook.com (Sasi -) Date: Sat, 31 May 2014 17:23:21 +0800 Subject: [Tutor] Break outside the loop error (line 23) Message-ID: Hi, i tried to make a code to execute the functions that i described below. However i got a break outside the loop error and i have tried to tab and all that but i still get the same error for line 23. I'm using python 2.7 Please help me. I'm completely new to coding (just started last week)Thank you for your time, i have pasted the code below. ## open both filesfile1 = open('human_brain_proteins.csv')file2 = open('human_plasma_proteins.csv')#file1 = open('log1')#file2 = open('log2') ## createdd a counter to count linescount1 = 0count2 = 0 ## define 3 lists to be filled in latercommon = list()brain = list()plasma = list() ## Created the lists for brain and plasma before searching for common bioseq.while True: count1 += 1 s1=file1.readline().partition(',')[0]if s1 and count1 > 1: brain.append(s1) if not s1: breakwhile True: count2 += 1 s2=file2.readline().partition(',')[0]if s2 and count2 > 1: plasma.append(s2) if not s2: break ## Compared the lists to find out common bioseq., add the common bioseq. into the common list,## then remove the common bioseq. from both listsfor item1 in brain: for item2 in plasma: if item1 == item2: common.append(item1) brain.remove(item1) plasma.remove(item2) ## closed both filesfile1.close()file2.close() ## print out the listsprint "Common biosequence:"print common,"\n"print "Brain specific biosequence:"print brain,"\n"print "Plasma specific biosequence:"print plasma,"\n" -------------- next part -------------- An HTML attachment was scrubbed... URL: