From eowens0124 at gmx.com Sat Dec 1 00:12:21 2012 From: eowens0124 at gmx.com (Ed Owens) Date: Fri, 30 Nov 2012 18:12:21 -0500 Subject: [Tutor] FW: (no subject) In-Reply-To: References: Message-ID: <008b01cdcf50$274c2f50$75e48df0$@com> Hi, im trying to write a script which randomly generates 10,000 points(x,y) in the unit square(so range between 0 and 1 for both x and y). so far I have written the code below in red, however it only produces one random point. How do I get it to repeat this so it produces 10,000 different random points? Thankyouuu, Tara. import math import random random.seed() x=random.uniform(0,1) y=random.uniform(0,1) - Doesn't the above just produce a single X and Y? for i in range(0,1): for j in range(0,1): print (x,y) - And this only gives a single I, and single j, resulting in one 'print' execution. If you want 10K points, then the point-generating statements (X=., Y=.) needs to be enclosed in a loop that increments the number of times you want. Where's that range(0,10000) loop? Ed -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sat Dec 1 00:29:53 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 01 Dec 2012 10:29:53 +1100 Subject: [Tutor] how to struct.pack a unicode string? In-Reply-To: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> References: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: <50B94171.10408@pearwood.info> On 01/12/12 03:43, Albert-Jan Roskam wrote: > Hi, > > How can I pack a unicode string using the struct module? If I >simply use packed = struct.pack(fmt, hello) in the code below >(and 'hello' is a unicode string), I get this: >"error: argument for 's' must be a string". To be precise, it must be a *byte* string, not a Unicode string. > I keep reading that I have to encode it to a utf-8 bytestring, To be precise, you can use any encoding you like, with the following provisos: * not all encodings are capable of representing every character (e.g. the ASCII encoding only represents 127 characters); * some encodings may not quite round-trip exactly, that is, they may lose some information; * some encodings are more compact than others (e.g. Latin-1 uses one byte per character, while UTF-32 uses four bytes per character). > but this does not work (it yields mojibake and tofu output for >some of the languages). It would be useful to see an example of this. But if you do your encoding/decoding correctly, using the right codecs, you should never get mojibake. You only get that when you have a mismatch between the encoding you think you have and the encoding you actually have. > It's annoying if one needs to know the encoding in which each >individual language should be represented. I was hoping >"unicode-internal" was the way to do it, but this does not >reproduce the original string when I unpack it.. :-( Yes, encodings are annoying. The sooner that all encodings other than UTF-8 and UTF-32 disappear the better :) The beauty of using UTF-8 instead of one of the many legacy encodings is that UTF-8 can represent any character, so you don't need to care about the individual language, and it is compact (at least for Western European languages). Why are you using struct for this? If you want to convert Unicode strings into a sequence of bytes, that's exactly what the encode method does. There's no need for struct. greetings = [ ('Arabic', u'\u0627\u0644\u0633\u0644\u0627\u0645\u0020\u0639\u0644\u064a\u0643\u0645', 'cp1256'), ('Assamese', u'\u09a8\u09ae\u09b8\u09cd\u0995\u09be\u09f0', 'utf-8'), ('Bengali', u'\u0986\u09b8\u09b8\u09be\u09b2\u09be\u09ae\u09c1 \u0986\u09b2\u09be\u0987\u0995\u09c1\u09ae', 'utf-8'), ('English', u'Greetings and salutations', 'ascii'), ('Georgian', u'\u10d2\u10d0\u10db\u10d0\u10e0\u10ef\u10dd\u10d1\u10d0', 'utf-8'), ('Kazakh', u'\u0421\u04d9\u043b\u0435\u043c\u0435\u0442\u0441\u0456\u0437 \u0431\u0435', 'utf-8'), ('Russian', u'\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435', 'utf-8'), ('Spanish', u'\xa1Hola!', 'cp1252'), ('Swiss German', u'Gr\xfcezi', 'cp1252'), ('Thai', u'\u0e2a\u0e27\u0e31\u0e2a\u0e14\u0e35', 'cp874'), ('Walloon', u'Bondjo\xfb', 'cp1252'), ] for language, greet, encoding in greetings: print u"Hello in %s: %s" % (language, greet) for enc in ('utf-8', 'utf-16', 'utf-32', encoding): bytestring = greet.encode(enc) print "encoded as %s gives %r" % (enc, bytestring) if bytestring.decode(enc) != greet: print "*** round-trip encoding/decoding failed ***" Any of the byte strings can then be written directly to a file: f.write(bytestring) or embedded into a struct. You need a variable-length struct, of course. My advice: stick to Python unicode strings internally, and always write them to files as UTF-8. -- Steven From eryksun at gmail.com Sat Dec 1 02:28:24 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 30 Nov 2012 20:28:24 -0500 Subject: [Tutor] how to struct.pack a unicode string? In-Reply-To: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> References: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: On Fri, Nov 30, 2012 at 11:43 AM, Albert-Jan Roskam wrote: > > How can I pack a unicode string using the struct module? struct.pack is for packing an arbitrary sequence of data into a C-like struct. You have to manually add pad bytes. Alternatively you can use a ctypes.Structure. The struct module supports plain byte strings, not Unicode. UTF-8 was designed to encode all of Unicode in a way that can seamlessly pass through libraries that process C strings (i.e. an array of non-null bytes terminated by a null byte). Byte values less than 128 are ASCII; beyond ASCII, UTF-8 uses 2-4 bytes, and all byte values are greater than 127, with standardized byte order. In contrast, UTF-16 and UTF-32 have null bytes in the string and platform-determined byte order. The length and order of the optional byte order mark (BOM) distinguishes UTF-16LE, UTF-16BE, UTF-32LE, and UTF-32BE. There's also a UTF-8 BOM used on Windows. Python calls this encoding "utf-8-sig". > fmt = endianness + str(len(hello)) + "s" That's the wrong length. Use the length of the encoded string. From eryksun at gmail.com Sat Dec 1 03:02:32 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 30 Nov 2012 21:02:32 -0500 Subject: [Tutor] how to struct.pack a unicode string? In-Reply-To: References: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: A clarification: in the default mode ('@'), struct uses native alignment padding, but not if you override this with <, >, =, or !, as you did. >> fmt = endianness + str(len(hello)) + "s" > > That's the wrong length. Use the length of the encoded string. Generally, however, you'd use a fixed size set by the struct definition. For example: typedef struct _point { unsigned int x; unsigned int y; char label[8]; } point; Python: >>> struct.pack('II8s', *[1, 2, b'12345678This is ignored']) b'\x01\x00\x00\x00\x02\x00\x00\x0012345678' Null termination may or may not be required. Python will pad out the rest of the string with nulls if it's less than the specified length: >>> struct.pack('II8s', *[1, 2, b'1234']) b'\x01\x00\x00\x00\x02\x00\x00\x001234\x00\x00\x00\x00' From steve at pearwood.info Sat Dec 1 08:30:55 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 01 Dec 2012 18:30:55 +1100 Subject: [Tutor] how to struct.pack a unicode string? In-Reply-To: References: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: <50B9B22F.3080605@pearwood.info> On 01/12/12 12:28, eryksun wrote: > UTF-8 was > designed to encode all of Unicode in a way that can seamlessly pass > through libraries that process C strings (i.e. an array of non-null > bytes terminated by a null byte). Byte values less than 128 are ASCII; > beyond ASCII, UTF-8 uses 2-4 bytes, and all byte values are greater > than 127, with standardized byte order. In contrast, UTF-16 and UTF-32 > have null bytes in the string and platform-determined byte order. The > length and order of the optional byte order mark (BOM) distinguishes > UTF-16LE, UTF-16BE, UTF-32LE, and UTF-32BE. That's not quite right. The UTF-16BE and UTF-16LE character sets do not take BOMs, because the encoding already specifies the byte order: py> s = u'ab??' py> s.encode('utf-16LE') 'a\x00b\x00\xe7\x00\x19\x04' py> s.encode('utf-16BE') '\x00a\x00b\x00\xe7\x04\x19' In contrast, plain ol' UTF-16 with no BE or LE suffix is ambiguous without a BOM, so it uses one: py> s.encode('utf-16') '\xff\xfea\x00b\x00\xe7\x00\x19\x04' The same applies to UTF-32. > There's also a UTF-8 BOM used on Windows. Python calls this encoding > "utf-8-sig". UTF-8-sig, an abomination, but sadly not just a Microsoft abomination. Google Docs also uses it. Although the Unicode standard does allow using a BOM (not actually a Byte Order Mark, more of a "UTF-8 signature"), doing so is annoying and silly. -- Steven From eryksun at gmail.com Sat Dec 1 10:56:56 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 1 Dec 2012 04:56:56 -0500 Subject: [Tutor] how to struct.pack a unicode string? In-Reply-To: <50B9B22F.3080605@pearwood.info> References: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> <50B9B22F.3080605@pearwood.info> Message-ID: On Sat, Dec 1, 2012 at 2:30 AM, Steven D'Aprano wrote: > >> The length and order of the optional byte order mark (BOM) >> distinguishes UTF-16LE, UTF-16BE, UTF-32LE, and UTF-32BE. > > That's not quite right. The UTF-16BE and UTF-16LE character sets do > not take BOMs, because the encoding already specifies the byte order: Right, that was as clear as mud. What I meant is that the BOM is added to distinguish UTF-16 from UTF-32 and little vs big endian in a generic text stream. It's the nature of the stream itself to which I was referring, not to specific names assigned in the Unicode standard. For example, adding a BOM to a string encoded as UTF-16LE for a Windows registry REG_SZ value would be redundant and wrong. Encoding U+FEFF (zero width no-break space) also determines the transform format in addition to byte order. So I do think of it more like a signature than just a byte order mark. Digressions about the UTF BOM aside, the more salient point I wanted to make is that the transform formats are multibyte encodings (except ASCII in UTF-8), which means the expression str(len(hello)) is using the wrong length; it needs to use the length of the encoded string. Also, UTF-16 and UTF-32 typically have very many null bytes. Together, these two observations explain the error: "unicode_internal' codec can't decode byte 0x00 in position 12: truncated input". From richkappler at gmail.com Sat Dec 1 16:40:50 2012 From: richkappler at gmail.com (richard kappler) Date: Sat, 1 Dec 2012 10:40:50 -0500 Subject: [Tutor] reverse diagonal Message-ID: I'm working through Mark Lutz's "Python," reviewing the section on lists. I understand the list comprehension so far, but ran into a snag with the matrix. I've created the matrix M as follows: M = [[1, 2, 3[, [4, 5, 6], [7, 8, 9]] then ran through the various comprehension examples, including: diag = [M[i][i] for i in [0, 1, 2]] which, of course, gave me [1, 5, 9]. Then I tried creating revdiag, wanting to return [3, 5, 7], tried several different ways, never quite got what I was looking for, so I'm looking for guidance as I'm stuck on this idea. Here's the various attempts I made and the tracebacks: >>> revdiag = [M[i][i] for i in [2, 1, 0]] >>> revdiag [9, 5, 1] # once I saw the output, this one made sense to me. >>> revdiag = [M[i][j] for i in [0, 1, 2] and for j in [2, 1, 0]] File "", line 1 revdiag = [M[i][j] for i in [0, 1, 2] and for j in [2, 1, 0]] ^ SyntaxError: invalid syntax >>> revdiag = [M[i][j] for i in [0, 1, 2] and j in [2, 1, 0]] Traceback (most recent call last): File "", line 1, in NameError: name 'j' is not defined >>> revdiag = [M[i][j] for i in [0, 1, 2], for j in [2, 1, 0]] File "", line 1 revdiag = [M[i][j] for i in [0, 1, 2], for j in [2, 1, 0]] ^ SyntaxError: invalid syntax >>> revdiag = [M[i][j] for i in [0, 1, 2], and for j in [2, 1, 0]] File "", line 1 revdiag = [M[i][j] for i in [0, 1, 2], and for j in [2, 1, 0]] ^ SyntaxError: invalid syntax I see where the errors are occurring, but I'm not sure I fully understand why they are occurring or how to get what I'm looking for with what I presently know, which admittedly is not much. Any help would be appreciated. regards, Richard -- quando omni flunkus moritati -------------- next part -------------- An HTML attachment was scrubbed... URL: From quazi.ashfaq at gmail.com Sat Dec 1 17:01:52 2012 From: quazi.ashfaq at gmail.com (Ashfaq) Date: Sat, 1 Dec 2012 22:01:52 +0600 Subject: [Tutor] reverse diagonal In-Reply-To: References: Message-ID: >>> revdiag = [M[i][i] for i in [2, 1, 0]] >>> revdiag [9, 5, 1] The reverse diag entries (that you are seeking to get) are not correct. They should be M[0][2], M[1][1], M[2][0]. So the code could be -- revdiag = [] for i in [0, 1, 2]: j = 2 - i revdiag.append( M[i][j] ) I hope it helps. -- Ashfaq -------------- next part -------------- An HTML attachment was scrubbed... URL: From jguadamuz at gmail.com Sat Dec 1 17:21:39 2012 From: jguadamuz at gmail.com (=?ISO-8859-1?Q?Jonat=E1n_Guadamuz_Espinoza?=) Date: Sat, 1 Dec 2012 10:21:39 -0600 Subject: [Tutor] reverse diagonal In-Reply-To: References: Message-ID: On Sat, Dec 1, 2012 at 9:40 AM, richard kappler wrote: > I'm working through Mark Lutz's "Python," reviewing the section on lists. I > understand the list comprehension so far, but ran into a snag with the > matrix. I've created the matrix M as follows: > > M = [[1, 2, 3[, [4, 5, 6], [7, 8, 9]] > > then ran through the various comprehension examples, including: > > diag = [M[i][i] for i in [0, 1, 2]] > > which, of course, gave me [1, 5, 9]. > > Then I tried creating revdiag, wanting to return [3, 5, 7], tried several > different ways, never quite got what I was looking for, so I'm looking for > guidance as I'm stuck on this idea. Here's the various attempts I made and > the tracebacks: > >>>> revdiag = [M[i][i] for i in [2, 1, 0]] >>>> revdiag > [9, 5, 1] > # once I saw the output, this one made sense to me. > >>>> revdiag = [M[i][j] for i in [0, 1, 2] and for j in [2, 1, 0]] > File "", line 1 > revdiag = [M[i][j] for i in [0, 1, 2] and for j in [2, 1, 0]] > ^ > SyntaxError: invalid syntax > >>>> revdiag = [M[i][j] for i in [0, 1, 2] and j in [2, 1, 0]] > Traceback (most recent call last): > File "", line 1, in > NameError: name 'j' is not defined > >>>> revdiag = [M[i][j] for i in [0, 1, 2], for j in [2, 1, 0]] > File "", line 1 > revdiag = [M[i][j] for i in [0, 1, 2], for j in [2, 1, 0]] > ^ > SyntaxError: invalid syntax > >>>> revdiag = [M[i][j] for i in [0, 1, 2], and for j in [2, 1, 0]] > File "", line 1 > revdiag = [M[i][j] for i in [0, 1, 2], and for j in [2, 1, 0]] > ^ > SyntaxError: invalid syntax > The way you are trying to do it would be >>> revdiag = [M[i][j] for i,j in [(0,2),(1,1),(2,0)]] From d at davea.name Sat Dec 1 17:31:53 2012 From: d at davea.name (Dave Angel) Date: Sat, 01 Dec 2012 11:31:53 -0500 Subject: [Tutor] reverse diagonal In-Reply-To: References: Message-ID: <50BA30F9.1010507@davea.name> On 12/01/2012 10:40 AM, richard kappler wrote: > I'm working through Mark Lutz's "Python," reviewing the section on lists. I > understand the list comprehension so far, but ran into a snag with the > matrix. I've created the matrix M as follows: > > M = [[1, 2, 3[, [4, 5, 6], [7, 8, 9]] There's an obvious typo in that line. You really need to copy/paste into a message, since some retyping errors could seriously mislead us. > then ran through the various comprehension examples, including: > > diag = [M[i][i] for i in [0, 1, 2]] > > which, of course, gave me [1, 5, 9]. > > Then I tried creating revdiag, wanting to return [3, 5, 7], tried several > different ways, never quite got what I was looking for, so I'm looking for > guidance as I'm stuck on this idea. Here's the various attempts I made and > the tracebacks: > >>>> revdiag = [M[i][i] for i in [2, 1, 0]] >>>> revdiag > [9, 5, 1] > # once I saw the output, this one made sense to me. > >>>> revdiag = [M[i][j] for i in [0, 1, 2] and for j in [2, 1, 0]] > File "", line 1 > revdiag = [M[i][j] for i in [0, 1, 2] and for j in [2, 1, 0]] > ^ > SyntaxError: invalid syntax > There's no such syntax. If you want to assign two variables, then use tuple-unpacking, like: revdiag = [M[i][j] for i,j in [(0,2), (1,1), (2,0)]] Or even better: revdiag = [M[i][2-i] for i in [0, 1, 2]] Notice I compute the j value, since it's very dependent on i or even revdiag = [M[i][len(M)-1-i] for i in range(len(M)) ] which would still work for other sizes of M (All my code untested, as I have just run out of time) -- DaveA From brian.van.den.broek at gmail.com Sat Dec 1 17:28:48 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Sat, 1 Dec 2012 11:28:48 -0500 Subject: [Tutor] reverse diagonal In-Reply-To: References: Message-ID: On 1 December 2012 10:40, richard kappler wrote: > I'm working through Mark Lutz's "Python," reviewing the section on lists. I > understand the list comprehension so far, but ran into a snag with the > matrix. I've created the matrix M as follows: > > M = [[1, 2, 3[, [4, 5, 6], [7, 8, 9]] > > then ran through the various comprehension examples, including: > > diag = [M[i][i] for i in [0, 1, 2]] > > which, of course, gave me [1, 5, 9]. > > Then I tried creating revdiag, wanting to return [3, 5, 7], tried several > different ways, never quite got what I was looking for, so I'm looking for > guidance as I'm stuck on this idea. Here's the various attempts I made and > the tracebacks: Richard, It is good you copy and pasted everything I snipped. But, you typed in the line defining M. Better to also copy paste that, as you typed it in wrong :-) Here's one way that assumes of M only that it is an n-by-n matrix: >>> M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> for i in reversed(range(len(M))): M[i][i] 9 5 1 Best, Brian vdB From quazi.ashfaq at gmail.com Sat Dec 1 17:46:39 2012 From: quazi.ashfaq at gmail.com (Ashfaq) Date: Sat, 1 Dec 2012 22:46:39 +0600 Subject: [Tutor] FW: (no subject) In-Reply-To: <008b01cdcf50$274c2f50$75e48df0$@com> References: <008b01cdcf50$274c2f50$75e48df0$@com> Message-ID: Run your code 10,000 times. :) ** ** > > import math**** > > import random**** > > random.seed()**** > > points = [] for i in range(0, 10000): x=random.uniform(0,1) y=random.uniform(0,1) points.append ( (x, y) ) I hope it helps. -- Sincerely, Ashfaq -------------- next part -------------- An HTML attachment was scrubbed... URL: From lukepdev at gmail.com Sun Dec 2 00:19:46 2012 From: lukepdev at gmail.com (Luke Paireepinart) Date: Sat, 1 Dec 2012 17:19:46 -0600 Subject: [Tutor] FW: (no subject) In-Reply-To: References: <008b01cdcf50$274c2f50$75e48df0$@com> Message-ID: Ashfaq, On Sat, Dec 1, 2012 at 10:46 AM, Ashfaq wrote: > Run your code 10,000 times. :) > > > ** ** >> >> import math**** >> >> import random**** >> >> random.seed()**** >> >> > there is no need to call seed() with no parameters. From the docs: """ If *x* is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported.""" So you see when you"import random" the random number generator will seed automatically with the system time. points = [] > for i in range(0, 10000): > > x=random.uniform(0,1) > > y=random.uniform(0,1) > > points.append ( (x, y) ) > Also you could use a generator for all these lines, points = [(random.uniform(0,1), random.uniform(0,1)) for i in range(10000)] -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Sun Dec 2 02:12:07 2012 From: d at davea.name (Dave Angel) Date: Sat, 01 Dec 2012 20:12:07 -0500 Subject: [Tutor] reverse diagonal In-Reply-To: References: Message-ID: <50BAAAE7.7080102@davea.name> On 12/01/2012 11:28 AM, Brian van den Broek wrote: > On 1 December 2012 10:40, richard kappler wrote: >> I'm working through Mark Lutz's "Python," reviewing the section on lists. I >> understand the list comprehension so far, but ran into a snag with the >> matrix. I've created the matrix M as follows: >> >> M = [[1, 2, 3[, [4, 5, 6], [7, 8, 9]] >> >> then ran through the various comprehension examples, including: >> >> diag = [M[i][i] for i in [0, 1, 2]] >> >> which, of course, gave me [1, 5, 9]. >> >> Then I tried creating revdiag, wanting to return [3, 5, 7], tried several >> different ways, never quite got what I was looking for, so I'm looking for >> guidance as I'm stuck on this idea. Here's the various attempts I made and >> the tracebacks: > > Richard, > > It is good you copy and pasted everything I snipped. But, you typed in > the line defining M. Better to also copy paste that, as you typed it > in wrong :-) > > Here's one way that assumes of M only that it is an n-by-n matrix: > >>>> M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>>> for i in reversed(range(len(M))): > M[i][i] > > > 9 > 5 > 1 > The only catch to that is it's not what he wants. He said he wants 3, 5, 7 -- DaveA From brian.van.den.broek at gmail.com Sun Dec 2 02:29:16 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Sat, 1 Dec 2012 20:29:16 -0500 Subject: [Tutor] reverse diagonal In-Reply-To: <50BAAAE7.7080102@davea.name> References: <50BAAAE7.7080102@davea.name> Message-ID: On 1 December 2012 20:12, Dave Angel wrote: > On 12/01/2012 11:28 AM, Brian van den Broek wrote: >> On 1 December 2012 10:40, richard kappler wrote: >>> I'm working through Mark Lutz's "Python," reviewing the section on lists. I >>> understand the list comprehension so far, but ran into a snag with the >>> matrix. I've created the matrix M as follows: >>> >>> M = [[1, 2, 3[, [4, 5, 6], [7, 8, 9]] >>> >>> then ran through the various comprehension examples, including: >>> >>> diag = [M[i][i] for i in [0, 1, 2]] >>> >>> which, of course, gave me [1, 5, 9]. >>> >>> Then I tried creating revdiag, wanting to return [3, 5, 7], tried several >>> different ways, never quite got what I was looking for, so I'm looking for > The only catch to that is it's not what he wants. He said he wants 3, 5, 7 That does seem true. I would suggest that calling the desired function `revdiag' invited the mistake I made. But, it is still on me for not reading closely enough. Best, Brian vdB From eryksun at gmail.com Sun Dec 2 03:19:57 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 1 Dec 2012 21:19:57 -0500 Subject: [Tutor] reverse diagonal In-Reply-To: <50BA30F9.1010507@davea.name> References: <50BA30F9.1010507@davea.name> Message-ID: On Sat, Dec 1, 2012 at 11:31 AM, Dave Angel wrote: > > revdiag = [M[i][len(M)-1-i] for i in range(len(M)) ] You might sometimes see this using the bitwise invert operator ~ (i.e. __invert__, operator.invert): >>> M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> [M[i][~i] for i in xrange(len(M))] [3, 5, 7] ~i returns the value (-i - 1): >>> [~i for i in range(4)] [-1, -2, -3, -4] If a sequence index is negative, Python normalizes it by adding the sequence length. For example, seq[-1] == seq[3], where len(seq) == 4. You can think of the sequence index on a ring: 2 3 1 -4 0 -3 -1 -2 The corresponding negative index of the sequence is 180 degrees (pi radians) around the ring. So the bitwise complement of index i traverses the sequence in reverse order. From d at davea.name Sun Dec 2 03:35:27 2012 From: d at davea.name (Dave Angel) Date: Sat, 01 Dec 2012 21:35:27 -0500 Subject: [Tutor] reverse diagonal In-Reply-To: References: <50BA30F9.1010507@davea.name> Message-ID: <50BABE6F.805@davea.name> On 12/01/2012 09:19 PM, eryksun wrote: > On Sat, Dec 1, 2012 at 11:31 AM, Dave Angel wrote: >> >> revdiag = [M[i][len(M)-1-i] for i in range(len(M)) ] > > You might sometimes see this using the bitwise invert operator ~ (i.e. > __invert__, operator.invert): > > >>> M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] > > >>> [M[i][~i] for i in xrange(len(M))] Folks, notice the symbol in front of the 'i' is a tilda, not a minus-sign. > [3, 5, 7] > > ~i returns the value (-i - 1): > > >>> [~i for i in range(4)] > [-1, -2, -3, -4] > > If a sequence index is negative, Python normalizes it by adding the > sequence length. For example, seq[-1] == seq[3], where len(seq) == 4. > You can think of the sequence index on a ring: > > 2 > 3 1 > > -4 0 > > -3 -1 > -2 > > > The corresponding negative index of the sequence is 180 degrees (pi > radians) around the ring. So the bitwise complement of index i > traverses the sequence in reverse order. > > Thanks eryksun, I knew all those facts, but didn't connect them together for this purpose. One's complement arithmetic works great with the indexing rules to reverse an order. Good job. Next level of subtlety: [M[i][~i] for i,dummy in enumerate(M) ] -- DaveA From eryksun at gmail.com Sun Dec 2 03:55:17 2012 From: eryksun at gmail.com (eryksun) Date: Sat, 1 Dec 2012 21:55:17 -0500 Subject: [Tutor] reverse diagonal In-Reply-To: <50BABE6F.805@davea.name> References: <50BA30F9.1010507@davea.name> <50BABE6F.805@davea.name> Message-ID: On Sat, Dec 1, 2012 at 9:35 PM, Dave Angel wrote: > > [M[i][~i] for i,dummy in enumerate(M) ] Since enumerate() iterates the rows, you could skip the first index: >>> [row[~i] for i,row in enumerate(M)] [3, 5, 7] From d at davea.name Sun Dec 2 05:18:44 2012 From: d at davea.name (Dave Angel) Date: Sat, 01 Dec 2012 23:18:44 -0500 Subject: [Tutor] reverse diagonal In-Reply-To: References: <50BA30F9.1010507@davea.name> <50BABE6F.805@davea.name> Message-ID: <50BAD6A4.1020701@davea.name> On 12/01/2012 09:55 PM, eryksun wrote: > On Sat, Dec 1, 2012 at 9:35 PM, Dave Angel wrote: >> >> [M[i][~i] for i,dummy in enumerate(M) ] > > Since enumerate() iterates the rows, you could skip the first index: > > >>> [row[~i] for i,row in enumerate(M)] > [3, 5, 7] > > Great job. And I can't see any way to improve on that. -- DaveA From itsursujit at gmail.com Sun Dec 2 05:39:19 2012 From: itsursujit at gmail.com (Sujit Baniya) Date: Sun, 2 Dec 2012 10:24:19 +0545 Subject: [Tutor] To Find the Answers Message-ID: *Write a function named countRepresentations that returns the number*>* of ways that an amount of money in rupees can be represented as rupee*>* notes. For this problem we only use rupee notes in denominations of*>* 1, 2, 5, 10 and 20 rupee notes.*>**>* The signature of the function is:*>* def countRepresentations(int numRupees)*>**>* For example, countRepresentations(12) should return 15 because 12*>* rupees can be represented in the following 15 ways.*>* 1. 12 one rupee notes*>* 2. 1 two rupee note plus 10 one rupee notes*>* 3. 2 two rupee notes plus 8 one rupee notes*>* 4. 3 two rupee notes plus 6 one rupee notes*>* 5. 4 two rupee notes plus 4 one rupee notes*>* 6. 5 two rupee notes plus 2 one rupee notes*>* 7. 6 two rupee notes*>* 8. 1 five rupee note plus 7 one rupee notes*>* 9. 1 five rupee note, 1 two rupee note and 5 one rupee notes*>* 10. 1 five rupee note, 2 two rupee notes and 3 one rupee notes*>* 11. 1 five rupee note, 3 two notes and 1 one rupee note*>* 12. 2 five rupee notes and 2 one rupee notes*>* 13. 2 five rupee notes and 1 two rupee note*>* 14. 1 ten rupee note and 2 one rupee notes*>* 15. 1 ten rupee note and 1 two rupee note*>**>* Hint: Use a nested loop that looks like this. Please fill in the*>* blanks intelligently, i.e. minimize the number of times that the if*>* statement is executed.*>* for (int rupee20=0; rupee20<=__; rupee20++)*>* for (int rupee10=0; rupee10<=__; rupee10++)*>* for (int rupee5=0; rupee5<=__; rupee5++)*>* for (int rupee2=0; rupee2<=__; rupee2++)*>* for (int rupee1=0; rupee1<=__; rupee1++)*>* {*>* if (___)*>* count++*>* }* -- Sujit Baniya -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Sun Dec 2 06:27:26 2012 From: d at davea.name (Dave Angel) Date: Sun, 02 Dec 2012 00:27:26 -0500 Subject: [Tutor] To Find the Answers In-Reply-To: References: Message-ID: <50BAE6BE.4070007@davea.name> On 12/01/2012 11:39 PM, Sujit Baniya wrote: > *Write a function named countRepresentations that returns the > number*>* of ways that an amount of money in rupees can be represented > as rupee*>* notes. For this problem we only use rupee notes in > denominations of*>* 1, 2, 5, 10 and 20 rupee notes.*>**>* The > signature of the function is:*>* def countRepresentations(int > numRupees)*>**>* For example, countRepresentations(12) should return > 15 because 12*>* rupees can be represented in the following 15 > ways.*>* 1. 12 one rupee notes*>* 2. 1 two rupee note plus 10 one > rupee notes*>* 3. 2 two rupee notes plus 8 one rupee notes*>* 4. 3 > two rupee notes plus 6 one rupee notes*>* 5. 4 two rupee notes plus > 4 one rupee notes*>* 6. 5 two rupee notes plus 2 one rupee notes*>* > 7. 6 two rupee notes*>* 8. 1 five rupee note plus 7 one rupee > notes*>* 9. 1 five rupee note, 1 two rupee note and 5 one rupee > notes*>* 10. 1 five rupee note, 2 two rupee notes and 3 one rupee > notes*>* 11. 1 five rupee note, 3 two notes and 1 one rupee note*>* > 12. 2 five rupee notes and 2 one rupee notes*>* 13. 2 five rupee > notes and 1 two rupee note*>* 14. 1 ten rupee note and 2 one rupee > notes*>* 15. 1 ten rupee note and 1 two rupee note*>**>* Hint: Use a > nested loop that looks like this. Please fill in the*>* blanks > intelligently, i.e. minimize the number of times that the if*>* > statement is executed.*>* for (int rupee20=0; rupee20<=__; > rupee20++)*>* for (int rupee10=0; rupee10<=__; rupee10++)*>* > for (int rupee5=0; rupee5<=__; rupee5++)*>* for (int > rupee2=0; rupee2<=__; rupee2++)*>* for (int rupee1=0; > rupee1<=__; rupee1++)*>* {*>* if (___)*>* > count++*>* }* > > 1) Please don't leave html messages here. Frequently, the formatting is totally messed up, as you can see here. This is a text mailing list. 2) If you have a Python question, please ask it. Posting a query here about C or C++ doesn't seem to be very effective. -- DaveA From steve at pearwood.info Sun Dec 2 08:32:52 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 2 Dec 2012 18:32:52 +1100 Subject: [Tutor] reverse diagonal In-Reply-To: References: <50BA30F9.1010507@davea.name> Message-ID: <20121202073252.GA32473@ando> On Sat, Dec 01, 2012 at 09:19:57PM -0500, eryksun wrote: > On Sat, Dec 1, 2012 at 11:31 AM, Dave Angel wrote: > > > > revdiag = [M[i][len(M)-1-i] for i in range(len(M)) ] > > You might sometimes see this using the bitwise invert operator ~ (i.e. > __invert__, operator.invert): > > >>> M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] > > >>> [M[i][~i] for i in xrange(len(M))] > [3, 5, 7] Ew! There's something... smelly... about code using a bitwise-invert for indexing. Using it for bitwise operations is one thing. Using it to save writing two characters is just nasty. http://www.joelonsoftware.com/articles/Wrong.html http://c2.com/cgi/wiki?CodeSmell It smacks of low-level optimization tricks which make no sense in a high-level language like Python. You aren't writing optimized assembler, if you want -i-1 write -i-1 ! > ~i returns the value (-i - 1): Assuming certain implementation details about how integers are stored, namely that they are two-compliment rather than one-compliment or something more exotic. Okay, just about every computer made since 1960 uses two-compliment integers, but still, the effect of ~i depends on the way integers are represented internally rather than some property of integers as an abstract number. That makes it a code smell. And there is the risk that ~i will be misread as -i, which would be bad. -- Steven From spectralnone at yahoo.com.sg Sun Dec 2 09:53:43 2012 From: spectralnone at yahoo.com.sg (Spectral None) Date: Sun, 2 Dec 2012 16:53:43 +0800 (SGT) Subject: [Tutor] 1 to N searches in files Message-ID: <1354438423.33849.YahooMailNeo@web190604.mail.sg3.yahoo.com> Hi all I have two files (File A and File B) with strings of data in them (each string on a separate line). Basically, each string in File B will be compared with all the strings in File A and the resulting output is to show a list of matched/unmatched lines and optionally to write to a third File C File A: Unique strings File B: Can have duplicate strings (that is, "string1" may appear more than once) My code currently looks like this: ----------------- FirstFile = open('C:\FileA.txt', 'r') SecondFile = open('C:\FileB.txt', 'r') ThirdFile = open('C:\FileC.txt', 'w') a = FirstFile.readlines() b = SecondFile.readlines() mydiff = difflib.Differ() results = mydiff(a,b) print("\n".join(results)) #ThirdFile.writelines(results) FirstFile.close() SecondFile.close() ThirdFile.close() --------------------- However, it seems that the results do not correctly reflect the matched/unmatched lines. As an example, if FileA contains "string1" and FileB contains multiple occurrences of "string1", it seems that the first occurrence matches correctly but subsequent "string1"s are treated as unmatched strings. I am thinking perhaps I don't understand Differ() that well and that it is not doing what I hoped to do? Is Differ() comparing first line to first line and second line to second line etc in contrast to what I wanted to do? Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sun Dec 2 10:34:24 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 02 Dec 2012 20:34:24 +1100 Subject: [Tutor] 1 to N searches in files In-Reply-To: <1354438423.33849.YahooMailNeo@web190604.mail.sg3.yahoo.com> References: <1354438423.33849.YahooMailNeo@web190604.mail.sg3.yahoo.com> Message-ID: <50BB20A0.5090301@pearwood.info> On 02/12/12 19:53, Spectral None wrote: > However, it seems that the results do not correctly reflect the >matched/unmatched lines. As an example, if FileA contains "string1" > and FileB contains multiple occurrences of "string1", it seems that > the first occurrence matches correctly but subsequent "string1"s >are treated as unmatched strings. > > I am thinking perhaps I don't understand Differ() that well and that > it is not doing what I hoped to do? Is Differ() comparing first line > to first line and second line to second line etc in contrast to what > I wanted to do? No, and yes. No, it is not comparing first line to first line. And yes, it is acting in contrast to what you hope to do, otherwise you wouldn't be asking the question :-) Unfortunately, you don't explain what it is that you hope to do, so I'm going to have to guess. See below. difflib is used for find differences between two files. It will try to find a set of changes which will turn file A into file B, e.g: insert this line here delete this line there ... and repeated as many times as needed. Except that difflib.Differ uses a shorthand of "+" and "-" to indicate adding and deleting lines. You can find out more about difflib and Differ objects by reading the Fine Manual. Open a Python interactive shell, and do this: import difflib help(difflib.Differ) If you have any questions, please feel free to ask. In the code sample you give, you say you do this: mydiff = difflib.Differ() results = mydiff(a,b) but that doesn't work, Differ objects are not callable. Please do not paraphrase your code. Copy and paste the exact code you have actually run, don't try to type it out from memory. Now, I *guess* that what you are trying to do is something like this... given files A and B: # file A spam ham eggs tomato # file B tomato spam eggs cheese spam spam you want to generate three lists: # lines in B that were also in A: tomato spam eggs # lines in B that were not in A: cheese # lines in A that were not found in B: ham Am I close? If not, please explain with an example what you are trying to do. -- Steven From d at davea.name Sun Dec 2 13:05:56 2012 From: d at davea.name (Dave Angel) Date: Sun, 02 Dec 2012 07:05:56 -0500 Subject: [Tutor] 1 to N searches in files In-Reply-To: <1354438423.33849.YahooMailNeo@web190604.mail.sg3.yahoo.com> References: <1354438423.33849.YahooMailNeo@web190604.mail.sg3.yahoo.com> Message-ID: <50BB4424.4020903@davea.name> On 12/02/2012 03:53 AM, Spectral None wrote: > Hi all > > I have two files (File A and File B) with strings of data in them (each string on a separate line). Basically, each string in File B will be compared with all the strings in File A and the resulting output is to show a list of matched/unmatched lines and optionally to write to a third File C > > File A: Unique strings > File B: Can have duplicate strings (that is, "string1" may appear more than once) > > My code currently looks like this: > > ----------------- > FirstFile = open('C:\FileA.txt', 'r') > SecondFile = open('C:\FileB.txt', 'r') > ThirdFile = open('C:\FileC.txt', 'w') > > a = FirstFile.readlines() > b = SecondFile.readlines() > > mydiff = difflib.Differ() > results = mydiff(a,b) > print("\n".join(results)) > > #ThirdFile.writelines(results) > > FirstFile.close() > SecondFile.close() > ThirdFile.close() > --------------------- > > However, it seems that the results do not correctly reflect the matched/unmatched lines. As an example, if FileA contains "string1" and FileB contains multiple occurrences of "string1", it seems that the first occurrence matches correctly but subsequent "string1"s are treated as unmatched strings. > > I am thinking perhaps I don't understand Differ() that well and that it is not doing what I hoped to do? Is Differ() comparing first line to first line and second line to second line etc in contrast to what I wanted to do? > > Regards > > Let me guess your goal, and then, on that assumption, discuss your code. I think your File A is supposed to be a dictionary of valid words (strings). You want to process File B, checking each line against that dictionary, and make a list of which lines are "valid" (in the dictionary), and another of which lines are not (missing from the dictionary). That's one list for matched lines, and one for unmatched. That isn't even close to what difflib does. This can be solved with minimal code, but not by starting with difflib. What you should do is to loop through File A, adding all the lines to a set called valid_dictionary. Calling set(FirstFile) can do that in one line, without even calling readlines(). Then a simple loop can build the desired lists. The matched_lines is simply all lines which are in the dictionary, while unmatched_lines are those which are not. The heart of the comparison could simply look like: if line in valid_dictionary: matched_lines.append(line) else: unmatched_lines.append(line) -- DaveA From fomcl at yahoo.com Sun Dec 2 14:00:05 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sun, 2 Dec 2012 05:00:05 -0800 (PST) Subject: [Tutor] how to struct.pack a unicode string? References: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: <1354453205.9643.YahooMailNeo@web163803.mail.gq1.yahoo.com> >> How can I pack a unicode string using the struct module? If I simply use >> packed = struct.pack(fmt, hello) in the code below (and 'hello' is a >> unicode string), I get this: "error: argument for 's' must be a string". I >> keep reading that I have to encode it to a utf-8 bytestring, but this does >> not work (it yields mojibake and tofu output for some of the languages). > >You keep reading it because it is the right approach. You will not get >mojibake if you decode the "packed" data before using it. > >Your code basically becomes > >for greet in greetings: >? ? language, chars, encoding = greet >? ? hello = "".join([unichr(i) for i in chars]) >? ? packed = hello.encode("utf-8") >? ? unpacked = packed.decode("utf-8") >? ? print unpacked > >I don't know why you mess with byte order, perhaps you can tell a bit about >your actual use-case. Hi Peter, Thanks for helping me. I am writing binary files and I wanted to create test data for this. --this has been a good test case, such that (a) it demonstrated a defect in my program (b) idem, my knowledge. I realize how cp2152-ish I am; for instance, I wrongly tend to assume that len(someUnicodeString) == nbytes_of_that_unicode_string. --re: messing with byte order: I read in M. Summerfield's "Programming in Python 3" that it's advisable to always specify the byte order, for portability of the data. But, now that you mention it, the way I did it, I might as well omit it. Or, given that the binary format I am writing contains information about the byte order, I might hard-code the byte order (e.g. always write LE). That would follow Mark Summerfield's advise, if I understand it correctly. --(Aside from your advise to use utf-8) Given that sys.maxunicode == 65535 on my system (ie, that many unicode points can be represented in my compilation of Python) I'd expect that I not only could write u'blaah'.encode("unicode-internal"), but also u'blaah'.encode("ucs-2") Traceback (most recent call last): ? File "", line 1, in ??? u'blaah'.encode("ucs-2") LookupError: unknown encoding: ucs-2 Why is the label "unicode-internal" to indicate both ucs-2 and ucs-4? And why does the same Python version on my Linux computer use 1114111 code points? Can we conclude that Linux users are better equiped to write a letter in Birmese or Aleut? ;-) Thanks again! Regards, Albert-Jan From fomcl at yahoo.com Sun Dec 2 14:27:16 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sun, 2 Dec 2012 05:27:16 -0800 (PST) Subject: [Tutor] how to struct.pack a unicode string? In-Reply-To: <50B94171.10408@pearwood.info> References: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> <50B94171.10408@pearwood.info> Message-ID: <1354454836.5198.YahooMailNeo@web163804.mail.gq1.yahoo.com> > > * some encodings are more compact than others (e.g. Latin-1 uses > ? one byte per character, while UTF-32 uses four bytes per > ? character). I read that performance of UTF32 is better ("UTF-32 advantage: you don't need to decode stored data to the 32-bit Unicode code point for e.g. character by character handling. The code point is already available right there in your array/vector/string."). http://stackoverflow.com/questions/496321/utf8-utf16-and-utf32 But given that utf-32 is a memory hog, should one conclude that it's usually not a good idea to use it (esp. in Python)? ? >> but this does not work (it yields mojibake and tofu output for >> some of the languages). > > It would be useful to see an example of this. > > But if you do your encoding/decoding correctly, using the right > codecs, you should never get mojibake. You only get that when > you have a mismatch between the encoding you think you have and > the encoding you actually have. > > >> It's annoying if one needs to know the encoding in which each >> individual language should be represented. I was hoping >> "unicode-internal" was the way to do it, but this does not >> reproduce the original string when I unpack it.. :-( > > Yes, encodings are annoying. The sooner that all encodings other > than UTF-8 and UTF-32 disappear the better :) So true ;-) > The beauty of using UTF-8 instead of one of the many legacy > encodings is that UTF-8 can represent any character, so you don't > need to care about the individual language, and it is compact (at > least for Western European languages). Later you write "You need a variable-length struct, of course.". Is this because ASCII is a subset of UTF-8? The thing is, the the binary format I am writing (spss .sav), uses *fixed* column widths. This means that, even when I only use the ascii subset of utf-8, I still need to assume the worst-case-scenario, namely 3 bytes per symbol, right? ? > Why are you using struct for this? If you want to convert Unicode > strings into a sequence of bytes, that's exactly what the encode > method does. There's no need for struct. ? I am using struct to read/write binary data. I created the ' greetings' code to test my program (and my knowledge). As I said to Peter Otten, both were/are imperfect ;-). Struct needs a bytestring, not a unicode string, hence I needed to convert my unicode strings first. I used these languages because I suspected I often get away with errors because 'my' encoding (cp1252) is fairly easy. ? > greetings = [ > ? ? ? ? ('Arabic', > u'\u0627\u0644\u0633\u0644\u0627\u0645\u0020\u0639\u0644\u064a\u0643\u0645', > 'cp1256'), > ? ? ? ? ('Assamese', > u'\u09a8\u09ae\u09b8\u09cd\u0995\u09be\u09f0', > 'utf-8'), > ? ? ? ? ('Bengali', > u'\u0986\u09b8\u09b8\u09be\u09b2\u09be\u09ae\u09c1 > \u0986\u09b2\u09be\u0987\u0995\u09c1\u09ae', > 'utf-8'), > ? ? ? ? ('English', u'Greetings and salutations', > 'ascii'), > ? ? ? ? ('Georgian', > u'\u10d2\u10d0\u10db\u10d0\u10e0\u10ef\u10dd\u10d1\u10d0', > 'utf-8'), > ? ? ? ? ('Kazakh', > u'\u0421\u04d9\u043b\u0435\u043c\u0435\u0442\u0441\u0456\u0437 > \u0431\u0435', 'utf-8'), > ? ? ? ? ('Russian', > u'\u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435', > 'utf-8'), > ? ? ? ? ('Spanish', u'\xa1Hola!', 'cp1252'), > ? ? ? ? ('Swiss German', u'Gr\xfcezi', 'cp1252'), > ? ? ? ? ('Thai', > u'\u0e2a\u0e27\u0e31\u0e2a\u0e14\u0e35', > 'cp874'), > ? ? ? ? ('Walloon', u'Bondjo\xfb', 'cp1252'), > ? ? ? ? ] > for language, greet, encoding in greetings: > ? ? print u"Hello in %s: %s" % (language, greet) > ? ? for enc in ('utf-8', 'utf-16', 'utf-32', encoding): > ? ? ? ? bytestring = greet.encode(enc) > ? ? ? ? print "encoded as %s gives %r" % (enc, bytestring) > ? ? ? ? if bytestring.decode(enc) != greet: > ? ? ? ? ? ? print "*** round-trip encoding/decoding failed ***" > > > Any of the byte strings can then be written directly to a file: > > f.write(bytestring) > > or embedded into a struct. You need a variable-length struct, of course. ? See above. I believe I've got it working for character data already; now I still need to check whether I can also store e.g. Chinese metadata in my spss file. > My advice: stick to Python unicode strings internally, and always write > them to files as UTF-8. Thanks Steven, I appreciate it! From fomcl at yahoo.com Sun Dec 2 14:34:52 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sun, 2 Dec 2012 05:34:52 -0800 (PST) Subject: [Tutor] how to struct.pack a unicode string? In-Reply-To: References: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> <50B9B22F.3080605@pearwood.info> Message-ID: <1354455292.11754.YahooMailNeo@web163805.mail.gq1.yahoo.com> ? > to make is that the transform formats are multibyte encodings (except > ASCII in UTF-8), which means the expression str(len(hello)) is using > the wrong length; it needs to use the length of the encoded string. > Also, UTF-16 and UTF-32 typically have very many null bytes. Together, > these two observations explain the error: "unicode_internal' codec > can't decode byte 0x00 in position 12: truncated input". Hi Eryksun, Observation #1: Yes, makes perfect sense. I should have thought about that. Observation #2: As I emailed earlier today to Peter Otten, I thought unicode_internal means UCS-2 or UCS-4, depending on the size of sys.maxunicode? How is this related to UTF-16 and UTF-32? Thank you! Best regards, Albert-Jan From d at davea.name Sun Dec 2 21:59:10 2012 From: d at davea.name (Dave Angel) Date: Sun, 02 Dec 2012 15:59:10 -0500 Subject: [Tutor] how to struct.pack a unicode string? In-Reply-To: <1354455292.11754.YahooMailNeo@web163805.mail.gq1.yahoo.com> References: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> <50B9B22F.3080605@pearwood.info> <1354455292.11754.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: <50BBC11E.7090007@davea.name> On 12/02/2012 08:34 AM, Albert-Jan Roskam wrote: > > > > > > > Hi Eryksun, > > Observation #1: Yes, makes perfect sense. I should have thought about that. Observation #2: > As I emailed earlier today to Peter Otten, I thought unicode_internal means UCS-2 or UCS-4, > depending on the size of sys.maxunicode? How is this related to UTF-16 and UTF-32? How is maxunicode relevant? Are you stuck on 3.2 or something? Python 3.3 uses 1 byte, 2 bytes or 4 for internal storage of a string depending only upon the needs of that particular string. -- DaveA From quazi.ashfaq at gmail.com Mon Dec 3 03:41:49 2012 From: quazi.ashfaq at gmail.com (Ashfaq) Date: Mon, 3 Dec 2012 08:41:49 +0600 Subject: [Tutor] FW: (no subject) In-Reply-To: References: <008b01cdcf50$274c2f50$75e48df0$@com> Message-ID: Luke, Thanks. The generator syntax is really cool. -- Ashfaq -------------- next part -------------- An HTML attachment was scrubbed... URL: From fantasticrm at gmail.com Mon Dec 3 04:59:21 2012 From: fantasticrm at gmail.com (rajesh mullings) Date: Sun, 2 Dec 2012 22:59:21 -0500 Subject: [Tutor] Help with writing a program Message-ID: Hello, I am trying to write a program which takes two lines of input, one called "a", and one called "b", which are both strings, then outputs the number of times a is a substring of b. If you could give me an algorithm/pseudo code of what I should do to create this program, I would greatly appreciate that. Thank you for using your time to consider my request. -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Mon Dec 3 05:19:51 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 03 Dec 2012 04:19:51 +0000 Subject: [Tutor] Help with writing a program In-Reply-To: References: Message-ID: On 03/12/2012 03:59, rajesh mullings wrote: > Hello, I am trying to write a program which takes two lines of input, one > called "a", and one called "b", which are both strings, then outputs the > number of times a is a substring of b. If you could give me an > algorithm/pseudo code of what I should do to create this program, I would > greatly appreciate that. Thank you for using your time to consider my > request. > Start here http://docs.python.org/2/library/string.html -- Cheers. Mark Lawrence. From eryksun at gmail.com Mon Dec 3 05:35:33 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 2 Dec 2012 23:35:33 -0500 Subject: [Tutor] reverse diagonal In-Reply-To: <20121202073252.GA32473@ando> References: <50BA30F9.1010507@davea.name> <20121202073252.GA32473@ando> Message-ID: On Sun, Dec 2, 2012 at 2:32 AM, Steven D'Aprano wrote: > >> ~i returns the value (-i - 1): > > Assuming certain implementation details about how integers are stored, > namely that they are two-compliment rather than one-compliment or > something more exotic. Yes, the result is platform dependent, at least for the 2.x int type. I saw it in someone else's code or blog a while ago and thought I'd pass it along as a novelty and something to keep an eye out for. A multiprecision long might qualify as exotic. It uses sign-magnitude form. The sign of the number and the length of ob_digit are both stored in ob_size. For the invert op, it adds 1 and negates the sign to emulate 2's complement: http://hg.python.org/cpython/file/70274d53c1dd/Objects/longobject.c#l3566 Further along is more 2's complement emulation for bitwise &, |, and ^: http://hg.python.org/cpython/file/70274d53c1dd/Objects/longobject.c#l3743 > Okay, just about every computer made since 1960 uses two-compliment > integers, but still, the effect of ~i depends on the way integers are > represented internally rather than some property of integers as an > abstract number. That makes it a code smell. It relies on integer modulo arithmetic. The internal base is arbitrary and not apparent. It could be 10s complement on some hypothetical base 10 computer. In terms of a Python sequence, you could use unsigned indices such as [0,1,2,3,4,5,6,7] or the N=8 complement indices [0,1,2,3,-4,-3,-2,-1], where -1 % 8 == 7, and so on. The invert op can be generalized as N-1-i for any N-length window on the integers (e.g. 5-digit base 10, where N=10**5, subtract i from N-1 == 99999), which just inverts the sequence order. The interpretation of this as negative number depends on a signed type that represents negative values as modulo N. That's common because it's a simple shift of the window to be symmetric about 0 (well, almost symmetric for even N); the modulo arithmetic is easy and there's no negative 0. However, with a multiprecision integer type, it's simpler to use a sign magnitude representation. That said, I don't want to give the impression that I disagree with you. You're right that it isn't generally advisable to use a single operation instead of two or three if it sacrifices clarity and portability. It didn't jump out at me as a problem since I take 2s complement for granted and have a bias to favor symmetry and minimalism. From fantasticrm at gmail.com Mon Dec 3 06:31:03 2012 From: fantasticrm at gmail.com (fantasticrm) Date: Mon, 3 Dec 2012 00:31:03 -0500 Subject: [Tutor] Help with writing a program In-Reply-To: References: Message-ID: The Python version, is Python 3. On Sun, Dec 2, 2012 at 10:59 PM, rajesh mullings wrote: > Hello, I am trying to write a program which takes two lines of input, one > called "a", and one called "b", which are both strings, then outputs the > number of times a is a substring of b. If you could give me an > algorithm/pseudo code of what I should do to create this program, I would > greatly appreciate that. Thank you for using your time to consider my > request. -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Mon Dec 3 06:56:56 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 3 Dec 2012 00:56:56 -0500 Subject: [Tutor] how to struct.pack a unicode string? In-Reply-To: <1354455292.11754.YahooMailNeo@web163805.mail.gq1.yahoo.com> References: <1354293788.96740.YahooMailNeo@web163803.mail.gq1.yahoo.com> <50B9B22F.3080605@pearwood.info> <1354455292.11754.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: On Sun, Dec 2, 2012 at 8:34 AM, Albert-Jan Roskam wrote: > > As I emailed earlier today to Peter Otten, I thought unicode_internal means > UCS-2 or UCS-4, depending on the size of sys.maxunicode? How is this related > to UTF-16 and UTF-32? UCS is the universal character set. Some highlights of the Basic Multilingual Plane (BMP): U+0000-U+00FF is Latin-1 (including the C0 and C1 control codes). U+D800-U+DFFF is reserved for UTF-16 surrogate pairs. U+E000-U+F8FF is reserved for private use. Most of U+F900-U+FFFF is assigned. Notably U+FEFF (zero width no-break space) doubles as the BOM/signature in the transformation formats. UTF-16 encodes the supplementary planes by using 2 codes as a surrogate pair. This uses a reserved 11-bit block (U+D800-U+DFFF), which is split into two 10-bit ranges: U+D800-U+DBFF for the lead surrogate and U+DC00-U+DFFF for the trail surrogate. Together that's the required 20 bits for the 16 supplementary planes. Including the BMP, this scheme covers the complete UCS range of 17 * 2**16 == 1114112 codes (on a wide build, that's sys.maxunicode + 1). For encoding text, use one of the transformation formats such as UTF-8, UTF-16, or UTF-32. Unless you have a requirement to use UTF-16 or UTF-32, it's best to stick to encoding to UTF-8. It's the default encoding in 3.x. It's also generally the most compact representation (especially if there's a lot of ASCII) and compatible with null-terminated byte strings (i.e. C array of char, terminated by NUL). Regardless of narrow vs wide build, you can always encode to one of these formats. The encoders for UTF-8 and UTF-32 first recombine any surrogate pairs in the internal representation. CPython 3.3 has a new implementation that angles for the best of all worlds, opting for a 1-byte, 2 byte, or 4-byte representation depending on the maximum code in the string. The internal representation doesn't use surrogates, so there's no more narrow vs wide build distinction. From lukepdev at gmail.com Mon Dec 3 07:11:18 2012 From: lukepdev at gmail.com (Luke Paireepinart) Date: Mon, 3 Dec 2012 00:11:18 -0600 Subject: [Tutor] FW: (no subject) In-Reply-To: References: <008b01cdcf50$274c2f50$75e48df0$@com> Message-ID: On Sun, Dec 2, 2012 at 8:41 PM, Ashfaq wrote: > Luke, > > Thanks. The generator syntax is really cool. > I misspoke, the correct term is "list comprehension". A generator is something totally different! Sorry about the confusion, my fault. I type too fast sometimes :) Glad you liked it though. -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From lukepdev at gmail.com Mon Dec 3 07:17:37 2012 From: lukepdev at gmail.com (Luke Paireepinart) Date: Mon, 3 Dec 2012 00:17:37 -0600 Subject: [Tutor] Help with writing a program In-Reply-To: References: Message-ID: There is an equivalent page in the documentation for Python 3 as well, regarding strings. This sounds a lot like a homework problem so you are unlikely to get a lot of help. You certainly won't get exact code. What have you tried so far? Where are you getting stuck? We're not here to write code for you, this list is meant to help you learn something yourself. If you just want someone to write code for you there are plenty of sites that will do that. But if you want to figure it out I'd be happy to give you some hints if I can see that you're making some effort. One effort you could make would be to find the relevant Python 3 document discussing strings and check if it has some references to finding substrings. Let me know what you try and I'll help you if you get stuck. Thanks, -Luke On Sun, Dec 2, 2012 at 11:31 PM, fantasticrm wrote: > The Python version, is Python 3. > > > On Sun, Dec 2, 2012 at 10:59 PM, rajesh mullings wrote: > >> Hello, I am trying to write a program which takes two lines of input, one >> called "a", and one called "b", which are both strings, then outputs the >> number of times a is a substring of b. If you could give me an >> algorithm/pseudo code of what I should do to create this program, I would >> greatly appreciate that. Thank you for using your time to consider my >> request. > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Dec 3 11:17:43 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 03 Dec 2012 21:17:43 +1100 Subject: [Tutor] Help with writing a program In-Reply-To: References: Message-ID: <50BC7C47.2090501@pearwood.info> On 03/12/12 14:59, rajesh mullings wrote: > Hello, I am trying to write a program which takes two lines of input, one > called "a", and one called "b", which are both strings, then outputs the > number of times a is a substring of b. If you could give me an > algorithm/pseudo code of what I should do to create this program, I would > greatly appreciate that. Thank you for using your time to consider my > request. Are you talking about something like this? a = "ing" b = """\ Our ingenious plan worked, and the Laughing Prince, last seen capering madly while his Motley Monks sat gibbering, was soon vanquished, though not without some achingly painful experiences. """ count(b, a) => should return 5 If that is what you want, try reading the Fine Manual about string methods, and you can count on finding something to help solve this problem. https://duckduckgo.com/?q=python%20string%20methods -- Steven From spectralnone at yahoo.com.sg Mon Dec 3 14:55:35 2012 From: spectralnone at yahoo.com.sg (Spectral None) Date: Mon, 3 Dec 2012 21:55:35 +0800 (SGT) Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: References: Message-ID: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> From: "tutor-request at python.org" To: tutor at python.org Sent: Sunday, 2 December 2012, 17:34 Subject: Tutor Digest, Vol 106, Issue 5 Send Tutor mailing list submissions to ??? tutor at python.org To subscribe or unsubscribe via the World Wide Web, visit ??? http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to ??? tutor-request at python.org You can reach the person managing the list at ??? tutor-owner at python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: ? 1. Re: reverse diagonal (Dave Angel) ? 2. To Find the Answers (Sujit Baniya) ? 3. Re: To Find the Answers (Dave Angel) ? 4. Re: reverse diagonal (Steven D'Aprano) ? 5. 1 to N searches in files (Spectral None) ? 6. Re: 1 to N searches in files (Steven D'Aprano) ---------------------------------------------------------------------- Message: 1 Date: Sat, 01 Dec 2012 23:18:44 -0500 From: Dave Angel To: eryksun Cc: tutor at python.org Subject: Re: [Tutor] reverse diagonal Message-ID: <50BAD6A4.1020701 at davea.name> Content-Type: text/plain; charset=UTF-8 On 12/01/2012 09:55 PM, eryksun wrote: > On Sat, Dec 1, 2012 at 9:35 PM, Dave Angel wrote: >> >> [M[i][~i] for i,dummy in enumerate(M) ] > > Since enumerate() iterates the rows, you could skip the first index: > >? ? >>> [row[~i] for i,row in enumerate(M)] >? ? [3, 5, 7] > > Great job.? And I can't see any way to improve on that. -- DaveA ------------------------------ Message: 2 Date: Sun, 2 Dec 2012 10:24:19 +0545 From: Sujit Baniya To: tutor at python.org Subject: [Tutor] To Find the Answers Message-ID: ??? Content-Type: text/plain; charset="iso-8859-1" *Write a function named countRepresentations that returns the number*>* of ways that an amount of money in rupees can be represented as rupee*>* notes. For this problem we only use? rupee notes in denominations of*>* 1, 2, 5, 10 and 20 rupee notes.*>**>* The signature of the function is:*>*? ? def countRepresentations(int numRupees)*>**>* For example, countRepresentations(12) should return 15 because 12*>* rupees can be represented in the following 15 ways.*>*? 1. 12 one rupee notes*>*? 2. 1 two rupee note plus 10 one rupee notes*>*? 3. 2 two rupee notes plus 8 one rupee notes*>*? 4. 3 two rupee notes plus 6 one rupee notes*>*? 5. 4 two rupee notes plus 4 one rupee notes*>*? 6. 5 two rupee notes plus 2 one rupee notes*>* 7. 6 two rupee notes*>*? 8. 1 five rupee note plus 7 one rupee notes*>*? 9. 1 five rupee note, 1 two rupee note and 5 one rupee notes*>*? 10. 1 five rupee note, 2 two rupee notes and 3 one rupee notes*>*? 11. 1 five rupee note, 3 two notes and 1 one rupee note*>* 12. 2 five rupee notes and 2 one rupee notes*>*? 13. 2 five rupee notes and 1 two rupee note*>*? 14. 1 ten rupee note and 2 one rupee notes*>*? 15. 1 ten rupee note and 1 two rupee note*>**>* Hint: Use a nested loop that looks like this. Please fill in the*>* blanks intelligently, i.e. minimize the number of times that the if*>* statement is executed.*>* for (int rupee20=0; rupee20<=__; rupee20++)*>*? ? for (int rupee10=0; rupee10<=__; rupee10++)*>* for (int rupee5=0; rupee5<=__; rupee5++)*>*? ? ? ? ? for (int rupee2=0; rupee2<=__; rupee2++)*>*? ? ? ? ? ? for (int rupee1=0; rupee1<=__; rupee1++)*>*? ? ? ? ? ? {*>*? ? ? ? ? ? ? ? if (___)*>* ? ? ? ? ? ? ? ? ? count++*>*? ? ? ? ? ? }* -- Sujit Baniya -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 3 Date: Sun, 02 Dec 2012 00:27:26 -0500 From: Dave Angel To: Sujit Baniya Cc: tutor at python.org Subject: Re: [Tutor] To Find the Answers Message-ID: <50BAE6BE.4070007 at davea.name> Content-Type: text/plain; charset=ISO-8859-1 On 12/01/2012 11:39 PM, Sujit Baniya wrote: > *Write a function named countRepresentations that returns the > number*>* of ways that an amount of money in rupees can be represented > as rupee*>* notes. For this problem we only use? rupee notes in > denominations of*>* 1, 2, 5, 10 and 20 rupee notes.*>**>* The > signature of the function is:*>*? ? def countRepresentations(int > numRupees)*>**>* For example, countRepresentations(12) should return > 15 because 12*>* rupees can be represented in the following 15 > ways.*>*? 1. 12 one rupee notes*>*? 2. 1 two rupee note plus 10 one > rupee notes*>*? 3. 2 two rupee notes plus 8 one rupee notes*>*? 4. 3 > two rupee notes plus 6 one rupee notes*>*? 5. 4 two rupee notes plus > 4 one rupee notes*>*? 6. 5 two rupee notes plus 2 one rupee notes*>* >? 7. 6 two rupee notes*>*? 8. 1 five rupee note plus 7 one rupee > notes*>*? 9. 1 five rupee note, 1 two rupee note and 5 one rupee > notes*>*? 10. 1 five rupee note, 2 two rupee notes and 3 one rupee > notes*>*? 11. 1 five rupee note, 3 two notes and 1 one rupee note*>* >? 12. 2 five rupee notes and 2 one rupee notes*>*? 13. 2 five rupee > notes and 1 two rupee note*>*? 14. 1 ten rupee note and 2 one rupee > notes*>*? 15. 1 ten rupee note and 1 two rupee note*>**>* Hint: Use a > nested loop that looks like this. Please fill in the*>* blanks > intelligently, i.e. minimize the number of times that the if*>* > statement is executed.*>* for (int rupee20=0; rupee20<=__; > rupee20++)*>*? ? for (int rupee10=0; rupee10<=__; rupee10++)*>* > for (int rupee5=0; rupee5<=__; rupee5++)*>*? ? ? ? ? for (int > rupee2=0; rupee2<=__; rupee2++)*>*? ? ? ? ? ? for (int rupee1=0; > rupee1<=__; rupee1++)*>*? ? ? ? ? ? {*>*? ? ? ? ? ? ? ? if (___)*>* >? ? ? ? ? ? ? ? ? count++*>*? ? ? ? ? ? }* > > 1) Please don't leave html messages here.? Frequently, the formatting is totally messed up, as you can see here.? This is a text mailing list. 2) If you have a Python question, please ask it.? Posting a query here about C or C++ doesn't seem to be very effective. -- DaveA ------------------------------ Message: 4 Date: Sun, 2 Dec 2012 18:32:52 +1100 From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] reverse diagonal Message-ID: <20121202073252.GA32473 at ando> Content-Type: text/plain; charset=us-ascii On Sat, Dec 01, 2012 at 09:19:57PM -0500, eryksun wrote: > On Sat, Dec 1, 2012 at 11:31 AM, Dave Angel wrote: > > > > revdiag = [M[i][len(M)-1-i] for i? in range(len(M)) ] > > You might sometimes see this using the bitwise invert operator ~ (i.e. > __invert__, operator.invert): > >? ? >>> M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] > >? ? >>> [M[i][~i] for i in xrange(len(M))] >? ? [3, 5, 7] Ew! There's something... smelly... about code using a bitwise-invert for indexing. Using it for bitwise operations is one thing. Using it to save writing two characters is just nasty. http://www.joelonsoftware.com/articles/Wrong.html http://c2.com/cgi/wiki?CodeSmell It smacks of low-level optimization tricks which make no sense in a high-level language like Python. You aren't writing optimized assembler, if you want -i-1 write -i-1 ! > ~i returns the value (-i - 1): Assuming certain implementation details about how integers are stored, namely that they are two-compliment rather than one-compliment or something more exotic. Okay, just about every computer made since 1960 uses two-compliment integers, but still, the effect of ~i depends on the way integers are represented internally rather than some property of integers as an abstract number. That makes it a code smell. And there is the risk that ~i will be misread as -i, which would be bad. -- Steven ------------------------------ Message: 5 Date: Sun, 2 Dec 2012 16:53:43 +0800 (SGT) From: Spectral None To: "tutor at python.org" Subject: [Tutor] 1 to N searches in files Message-ID: ??? <1354438423.33849.YahooMailNeo at web190604.mail.sg3.yahoo.com> Content-Type: text/plain; charset="utf-8" Hi all I have two files (File A and File B) with strings of data in them (each string on a separate line). Basically, each string in File B will be compared with all the strings in File A and the resulting output is to show a list of matched/unmatched lines and optionally to write to a third File C File A: Unique strings File B: Can have duplicate strings (that is, "string1" may appear more than once) My code currently looks like this: ----------------- FirstFile = open('C:\FileA.txt', 'r') SecondFile = open('C:\FileB.txt', 'r') ThirdFile = open('C:\FileC.txt', 'w') a = FirstFile.readlines() b = SecondFile.readlines() mydiff = difflib.Differ() results = mydiff(a,b) print("\n".join(results)) #ThirdFile.writelines(results) FirstFile.close() SecondFile.close() ThirdFile.close() --------------------- However, it seems that the results do not correctly reflect the matched/unmatched lines. As an example, if FileA contains "string1" and FileB contains multiple occurrences of "string1", it seems that the first occurrence matches correctly but subsequent "string1"s are treated as unmatched strings. I am thinking perhaps I don't understand Differ() that well and that it is not doing what I hoped to do? Is Differ() comparing first line to first line and second line to second line etc in contrast to what I wanted to do? Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 6 Date: Sun, 02 Dec 2012 20:34:24 +1100 From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] 1 to N searches in files Message-ID: <50BB20A0.5090301 at pearwood.info> Content-Type: text/plain; charset=UTF-8; format=flowed On 02/12/12 19:53, Spectral None wrote: > However, it seems that the results do not correctly reflect the >matched/unmatched lines. As an example, if FileA contains "string1" > and FileB contains multiple occurrences of "string1", it seems that >? the first occurrence matches correctly but subsequent "string1"s >are treated as unmatched strings. > > I am thinking perhaps I don't understand Differ() that well and that >? it is not doing what I hoped to do? Is Differ() comparing first line >? to first line and second line to second line etc in contrast to what >? I wanted to do? No, and yes. No, it is not comparing first line to first line. And yes, it is acting in contrast to what you hope to do, otherwise you wouldn't be asking the question :-) Unfortunately, you don't explain what it is that you hope to do, so I'm going to have to guess. See below. difflib is used for find differences between two files. It will try to find a set of changes which will turn file A into file B, e.g: insert this line here delete this line there ... and repeated as many times as needed. Except that difflib.Differ uses a shorthand of "+" and "-" to indicate adding and deleting lines. You can find out more about difflib and Differ objects by reading the Fine Manual. Open a Python interactive shell, and do this: import difflib help(difflib.Differ) If you have any questions, please feel free to ask. In the code sample you give, you say you do this: mydiff = difflib.Differ() results = mydiff(a,b) but that doesn't work, Differ objects are not callable. Please do not paraphrase your code. Copy and paste the exact code you have actually run, don't try to type it out from memory. Now, I *guess* that what you are trying to do is something like this... given files A and B: # file A spam ham eggs tomato # file B tomato spam eggs cheese spam spam you want to generate three lists: # lines in B that were also in A: tomato spam eggs # lines in B that were not in A: cheese # lines in A that were not found in B: ham Am I close? If not, please explain with an example what you are trying to do. -- Steven ------------------------------ Hi Steven I was searching for strings comparison and saw this article and decided to try it. There was no error when I ran the code (http://stackoverflow.com/questions/11008519/detecting-and-printing-the-difference-between-two-text-files-using-python-3-2) In another reply by Dave about matching list of valid words, that is similar to what I want to do. I guess I probably misunderstood the usage of Differ(). Thanks for the help! Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Dec 3 16:24:51 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 04 Dec 2012 02:24:51 +1100 Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> Message-ID: <50BCC443.4000402@pearwood.info> On 04/12/12 00:55, Spectral None wrote: > From:"tutor-request at python.org" > To:tutor at python.org > Sent: Sunday, 2 December 2012, 17:34 > Subject: Tutor Digest, Vol 106, Issue 5 > > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-request at python.org > > You can reach the person managing the list at > tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Re: reverse diagonal (Dave Angel) > 2. To Find the Answers (Sujit Baniya) > 3. Re: To Find the Answers (Dave Angel) > 4. Re: reverse diagonal (Steven D'Aprano) > 5. 1 to N searches in files (Spectral None) > 6. Re: 1 to N searches in files (Steven D'Aprano) Okay, this is where I stopped reading. You should pay attention to the emails that other people are sending. Notice how they reply to individual emails, not to a mass digest? Then pay attention to *your* email. Imagine you were receiving it. Did you not notice that your reply was SIX PAGES LONG? Can you imagine if everyone did what you just did? The first reply would be six pages, then the reply to that would be 12 pages, the reply to that would be 24 pages, then 48 pages... If you want a response to your question, please try again. This time: - only reply to a SINGLE email at a time, not six; - only quote the parts of the email that are relevant; - use a meaningful subject line that summarizes your question. If you don't do these things, you will soon find that nobody will be interested in digging through the piles and piles of dross looking for your comments buried deep in your reply. [snipped 200-odd lines] -- Steven From spectralnone at yahoo.com.sg Mon Dec 3 16:46:19 2012 From: spectralnone at yahoo.com.sg (Spectral None) Date: Mon, 3 Dec 2012 23:46:19 +0800 (SGT) Subject: [Tutor] 1 to N searches in files In-Reply-To: <50BB4424.4020903@davea.name> References: <1354438423.33849.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BB4424.4020903@davea.name> Message-ID: <1354549579.28023.YahooMailNeo@web190605.mail.sg3.yahoo.com> From: Dave Angel To: Spectral None Cc: "tutor at python.org" Sent: Sunday, 2 December 2012, 20:05 Subject: Re: [Tutor] 1 to N searches in files On 12/02/2012 03:53 AM, Spectral None wrote: > Hi all > > I have two files (File A and File B) with strings of data in them (each string on a separate line). Basically, each string in File B will be compared with all the strings in File A and the resulting output is to show a list of matched/unmatched lines and optionally to write to a third File C > > File A: Unique strings > File B: Can have duplicate strings (that is, "string1" may appear more than once) > > My code currently looks like this: > > ----------------- > FirstFile = open('C:\FileA.txt', 'r') > SecondFile = open('C:\FileB.txt', 'r') > ThirdFile = open('C:\FileC.txt', 'w') > > a = FirstFile.readlines() > b = SecondFile.readlines() > > mydiff = difflib.Differ() > results = mydiff(a,b) > print("\n".join(results)) > > #ThirdFile.writelines(results) > > FirstFile.close() > SecondFile.close() > ThirdFile.close() > --------------------- > > However, it seems that the results do not correctly reflect the matched/unmatched lines. As an example, if FileA contains "string1" and FileB contains multiple occurrences of "string1", it seems that the first occurrence matches correctly but subsequent "string1"s are treated as unmatched strings. > > I am thinking perhaps I don't understand Differ() that well and that it is not doing what I hoped to do? Is Differ() comparing first line to first line and second line to second line etc in contrast to what I wanted to do? > > Regards > > > Let me guess your goal, and then, on that assumption, discuss your code. > I think your File A is supposed to be a dictionary of valid words > (strings).? You want to process File B, checking each line against that > dictionary, and make a list of which lines are "valid" (in the > dictionary), and another of which lines are not (missing from the > dictionary).? That's one list for matched lines, and one for unmatched. > That isn't even close to what difflib does.? This can be solved with > minimal code, but not by starting with difflib. > What you should do is to loop through File A, adding all the lines to a > set called valid_dictionary.? Calling set(FirstFile) can do that in one > line, without even calling readlines(). > Then a simple loop can build the desired lists.? The matched_lines is > simply all lines which are in the dictionary, while unmatched_lines are > those which are not. > The heart of the comparison could simply look like: > ? ? if line in valid_dictionary: >??????????? matched_lines.append(line) >? ? ? else: >? ? ? ? ? ? unmatched_lines.append(line) > -- > DaveA --------------------- Hi Dave Your solution seems to work: setA = set(FileA) setB = set(FileB) for line in setB: ? if line in setA: ??? matched_lines.writelines(line) ? else: ??? non_matched_lines.writelines(line) There are no?duplicates in the results as well.?Thanks for helping out Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From spectralnone at yahoo.com.sg Mon Dec 3 16:52:41 2012 From: spectralnone at yahoo.com.sg (Spectral None) Date: Mon, 3 Dec 2012 23:52:41 +0800 (SGT) Subject: [Tutor] 1 to N searches in files In-Reply-To: References: Message-ID: <1354549961.31486.YahooMailNeo@web190605.mail.sg3.yahoo.com> From: "tutor-request at python.org" To: tutor at python.org Sent: Monday, 3 December 2012, 21:57 Subject: Tutor Digest, Vol 106, Issue 9 Send Tutor mailing list submissions to ??? tutor at python.org To subscribe or unsubscribe via the World Wide Web, visit ??? http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to ??? tutor-request at python.org You can reach the person managing the list at ??? tutor-owner at python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: ? 1. Re: Tutor Digest, Vol 106, Issue 5 (Spectral None) ---------------------------------------------------------------------- Message: 1 Date: Mon, 3 Dec 2012 21:55:35 +0800 (SGT) From: Spectral None To: "tutor at python.org" Subject: Re: [Tutor] Tutor Digest, Vol 106, Issue 5 Message-ID: ??? <1354542935.11347.YahooMailNeo at web190604.mail.sg3.yahoo.com> Content-Type: text/plain; charset="iso-8859-1" From: "tutor-request at python.org" To: tutor at python.org Sent: Sunday, 2 December 2012, 17:34 Subject: Tutor Digest, Vol 106, Issue 5 Send Tutor mailing list submissions to ??? tutor at python.org To subscribe or unsubscribe via the World Wide Web, visit ??? http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to ??? tutor-request at python.org You can reach the person managing the list at ??? tutor-owner at python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: ? 1. Re: reverse diagonal (Dave Angel) ? 2. To Find the Answers (Sujit Baniya) ? 3. Re: To Find the Answers (Dave Angel) ? 4. Re: reverse diagonal (Steven D'Aprano) ? 5. 1 to N searches in files (Spectral None) ? 6. Re: 1 to N searches in files (Steven D'Aprano) ---------------------------------------------------------------------- Message: 1 Date: Sat, 01 Dec 2012 23:18:44 -0500 From: Dave Angel To: eryksun Cc: tutor at python.org Subject: Re: [Tutor] reverse diagonal Message-ID: <50BAD6A4.1020701 at davea.name> Content-Type: text/plain; charset=UTF-8 On 12/01/2012 09:55 PM, eryksun wrote: > On Sat, Dec 1, 2012 at 9:35 PM, Dave Angel wrote: >> >> [M[i][~i] for i,dummy in enumerate(M) ] > > Since enumerate() iterates the rows, you could skip the first index: > >? ? >>> [row[~i] for i,row in enumerate(M)] >? ? [3, 5, 7] > > Great job.? And I can't see any way to improve on that. -- DaveA ------------------------------ Message: 2 Date: Sun, 2 Dec 2012 10:24:19 +0545 From: Sujit Baniya To: tutor at python.org Subject: [Tutor] To Find the Answers Message-ID: ??? Content-Type: text/plain; charset="iso-8859-1" *Write a function named countRepresentations that returns the number*>* of ways that an amount of money in rupees can be represented as rupee*>* notes. For this problem we only use? rupee notes in denominations of*>* 1, 2, 5, 10 and 20 rupee notes.*>**>* The signature of the function is:*>*? ? def countRepresentations(int numRupees)*>**>* For example, countRepresentations(12) should return 15 because 12*>* rupees can be represented in the following 15 ways.*>*? 1. 12 one rupee notes*>*? 2. 1 two rupee note plus 10 one rupee notes*>*? 3. 2 two rupee notes plus 8 one rupee notes*>*? 4. 3 two rupee notes plus 6 one rupee notes*>*? 5. 4 two rupee notes plus 4 one rupee notes*>*? 6. 5 two rupee notes plus 2 one rupee notes*>* 7. 6 two rupee notes*>*? 8. 1 five rupee note plus 7 one rupee notes*>*? 9. 1 five rupee note, 1 two rupee note and 5 one rupee notes*>*? 10. 1 five rupee note, 2 two rupee notes and 3 one rupee notes*>*? 11. 1 five rupee note, 3 two notes and 1 one rupee note*>* 12. 2 five rupee notes and 2 one rupee notes*>*? 13. 2 five rupee notes and 1 two rupee note*>*? 14. 1 ten rupee note and 2 one rupee notes*>*? 15. 1 ten rupee note and 1 two rupee note*>**>* Hint: Use a nested loop that looks like this. Please fill in the*>* blanks intelligently, i.e. minimize the number of times that the if*>* statement is executed.*>* for (int rupee20=0; rupee20<=__; rupee20++)*>*? ? for (int rupee10=0; rupee10<=__; rupee10++)*>* for (int rupee5=0; rupee5<=__; rupee5++)*>*? ? ? ? ? for (int rupee2=0; rupee2<=__; rupee2++)*>*? ? ? ? ? ? for (int rupee1=0; rupee1<=__; rupee1++)*>*? ? ? ? ? ? {*>*? ? ? ? ? ? ? ? if (___)*>* ? ? ? ? ? ? ? ? ? count++*>*? ? ? ? ? ? }* -- Sujit Baniya -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 3 Date: Sun, 02 Dec 2012 00:27:26 -0500 From: Dave Angel To: Sujit Baniya Cc: tutor at python.org Subject: Re: [Tutor] To Find the Answers Message-ID: <50BAE6BE.4070007 at davea.name> Content-Type: text/plain; charset=ISO-8859-1 On 12/01/2012 11:39 PM, Sujit Baniya wrote: > *Write a function named countRepresentations that returns the > number*>* of ways that an amount of money in rupees can be represented > as rupee*>* notes. For this problem we only use? rupee notes in > denominations of*>* 1, 2, 5, 10 and 20 rupee notes.*>**>* The > signature of the function is:*>*? ? def countRepresentations(int > numRupees)*>**>* For example, countRepresentations(12) should return > 15 because 12*>* rupees can be represented in the following 15 > ways.*>*? 1. 12 one rupee notes*>*? 2. 1 two rupee note plus 10 one > rupee notes*>*? 3. 2 two rupee notes plus 8 one rupee notes*>*? 4. 3 > two rupee notes plus 6 one rupee notes*>*? 5. 4 two rupee notes plus > 4 one rupee notes*>*? 6. 5 two rupee notes plus 2 one rupee notes*>* >? 7. 6 two rupee notes*>*? 8. 1 five rupee note plus 7 one rupee > notes*>*? 9. 1 five rupee note, 1 two rupee note and 5 one rupee > notes*>*? 10. 1 five rupee note, 2 two rupee notes and 3 one rupee > notes*>*? 11. 1 five rupee note, 3 two notes and 1 one rupee note*>* >? 12. 2 five rupee notes and 2 one rupee notes*>*? 13. 2 five rupee > notes and 1 two rupee note*>*? 14. 1 ten rupee note and 2 one rupee > notes*>*? 15. 1 ten rupee note and 1 two rupee note*>**>* Hint: Use a > nested loop that looks like this. Please fill in the*>* blanks > intelligently, i.e. minimize the number of times that the if*>* > statement is executed.*>* for (int rupee20=0; rupee20<=__; > rupee20++)*>*? ? for (int rupee10=0; rupee10<=__; rupee10++)*>* > for (int rupee5=0; rupee5<=__; rupee5++)*>*? ? ? ? ? for (int > rupee2=0; rupee2<=__; rupee2++)*>*? ? ? ? ? ? for (int rupee1=0; > rupee1<=__; rupee1++)*>*? ? ? ? ? ? {*>*? ? ? ? ? ? ? ? if (___)*>* >? ? ? ? ? ? ? ? ? count++*>*? ? ? ? ? ? }* > > 1) Please don't leave html messages here.? Frequently, the formatting is totally messed up, as you can see here.? This is a text mailing list. 2) If you have a Python question, please ask it.? Posting a query here about C or C++ doesn't seem to be very effective. -- DaveA ------------------------------ Message: 4 Date: Sun, 2 Dec 2012 18:32:52 +1100 From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] reverse diagonal Message-ID: <20121202073252.GA32473 at ando> Content-Type: text/plain; charset=us-ascii On Sat, Dec 01, 2012 at 09:19:57PM -0500, eryksun wrote: > On Sat, Dec 1, 2012 at 11:31 AM, Dave Angel wrote: > > > > revdiag = [M[i][len(M)-1-i] for i? in range(len(M)) ] > > You might sometimes see this using the bitwise invert operator ~ (i.e. > __invert__, operator.invert): > >? ? >>> M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] > >? ? >>> [M[i][~i] for i in xrange(len(M))] >? ? [3, 5, 7] Ew! There's something... smelly... about code using a bitwise-invert for indexing. Using it for bitwise operations is one thing. Using it to save writing two characters is just nasty. http://www.joelonsoftware.com/articles/Wrong.html http://c2.com/cgi/wiki?CodeSmell It smacks of low-level optimization tricks which make no sense in a high-level language like Python. You aren't writing optimized assembler, if you want -i-1 write -i-1 ! > ~i returns the value (-i - 1): Assuming certain implementation details about how integers are stored, namely that they are two-compliment rather than one-compliment or something more exotic. Okay, just about every computer made since 1960 uses two-compliment integers, but still, the effect of ~i depends on the way integers are represented internally rather than some property of integers as an abstract number. That makes it a code smell. And there is the risk that ~i will be misread as -i, which would be bad. -- Steven ------------------------------ Message: 5 Date: Sun, 2 Dec 2012 16:53:43 +0800 (SGT) From: Spectral None To: "tutor at python.org" Subject: [Tutor] 1 to N searches in files Message-ID: ??? <1354438423.33849.YahooMailNeo at web190604.mail.sg3.yahoo.com> Content-Type: text/plain; charset="utf-8" Hi all I have two files (File A and File B) with strings of data in them (each string on a separate line). Basically, each string in File B will be compared with all the strings in File A and the resulting output is to show a list of matched/unmatched lines and optionally to write to a third File C File A: Unique strings File B: Can have duplicate strings (that is, "string1" may appear more than once) My code currently looks like this: ----------------- FirstFile = open('C:\FileA.txt', 'r') SecondFile = open('C:\FileB.txt', 'r') ThirdFile = open('C:\FileC.txt', 'w') a = FirstFile.readlines() b = SecondFile.readlines() mydiff = difflib.Differ() results = mydiff(a,b) print("\n".join(results)) #ThirdFile.writelines(results) FirstFile.close() SecondFile.close() ThirdFile.close() --------------------- However, it seems that the results do not correctly reflect the matched/unmatched lines. As an example, if FileA contains "string1" and FileB contains multiple occurrences of "string1", it seems that the first occurrence matches correctly but subsequent "string1"s are treated as unmatched strings. I am thinking perhaps I don't understand Differ() that well and that it is not doing what I hoped to do? Is Differ() comparing first line to first line and second line to second line etc in contrast to what I wanted to do? Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 6 Date: Sun, 02 Dec 2012 20:34:24 +1100 From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] 1 to N searches in files Message-ID: <50BB20A0.5090301 at pearwood.info> Content-Type: text/plain; charset=UTF-8; format=flowed On 02/12/12 19:53, Spectral None wrote: > However, it seems that the results do not correctly reflect the >matched/unmatched lines. As an example, if FileA contains "string1" > and FileB contains multiple occurrences of "string1", it seems that >? the first occurrence matches correctly but subsequent "string1"s >are treated as unmatched strings. > > I am thinking perhaps I don't understand Differ() that well and that >? it is not doing what I hoped to do? Is Differ() comparing first line >? to first line and second line to second line etc in contrast to what >? I wanted to do? No, and yes. No, it is not comparing first line to first line. And yes, it is acting in contrast to what you hope to do, otherwise you wouldn't be asking the question :-) Unfortunately, you don't explain what it is that you hope to do, so I'm going to have to guess. See below. difflib is used for find differences between two files. It will try to find a set of changes which will turn file A into file B, e.g: insert this line here delete this line there ... and repeated as many times as needed. Except that difflib.Differ uses a shorthand of "+" and "-" to indicate adding and deleting lines. You can find out more about difflib and Differ objects by reading the Fine Manual. Open a Python interactive shell, and do this: import difflib help(difflib.Differ) If you have any questions, please feel free to ask. In the code sample you give, you say you do this: mydiff = difflib.Differ() results = mydiff(a,b) but that doesn't work, Differ objects are not callable. Please do not paraphrase your code. Copy and paste the exact code you have actually run, don't try to type it out from memory. Now, I *guess* that what you are trying to do is something like this... given files A and B: # file A spam ham eggs tomato # file B tomato spam eggs cheese spam spam you want to generate three lists: # lines in B that were also in A: tomato spam eggs # lines in B that were not in A: cheese # lines in A that were not found in B: ham Am I close? If not, please explain with an example what you are trying to do. -- Steven ------------------------------ > Hi Steven > I was searching for strings comparison and saw this article and decided to try it. There was no error when I ran the code > (http://stackoverflow.com/questions/11008519/detecting-and-printing-the-difference-between-two-text-files-using-python-3-2) > In another reply by Dave about matching list of valid words, that is similar to what I want to do. I guess I probably misunderstood the usage of Differ(). Thanks for the help! > Regards Hi Steven My apologies as well for not being clear in my explanation. Regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Mon Dec 3 16:58:32 2012 From: d at davea.name (Dave Angel) Date: Mon, 03 Dec 2012 10:58:32 -0500 Subject: [Tutor] 1 to N searches in files In-Reply-To: <1354549579.28023.YahooMailNeo@web190605.mail.sg3.yahoo.com> References: <1354438423.33849.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BB4424.4020903@davea.name> <1354549579.28023.YahooMailNeo@web190605.mail.sg3.yahoo.com> Message-ID: <50BCCC28.9040302@davea.name> On 12/03/2012 10:46 AM, Spectral None wrote: > > Hi Dave > > Your solution seems to work: > > setA = set(FileA) > setB = set(FileB) > > for line in setB: > if line in setA: > matched_lines.writelines(line) > else: > non_matched_lines.writelines(line) > > There are no duplicates in the results as well. Thanks for helping out > You didn't specify whether you wanted dups to be noticed. You had said that there were none in A, but B was unspecified. The other question is order. If you want original order, you'd have to omit the setB step, and iterate on FileB. And then eliminate dups as you go. Or if you want sorted order without dups, you could simply iterate on sorted(setB). -- DaveA From dadfar.narguess at gmail.com Mon Dec 3 17:17:12 2012 From: dadfar.narguess at gmail.com (Narguess Dadfar) Date: Mon, 3 Dec 2012 08:17:12 -0800 Subject: [Tutor] writing a program Message-ID: Hi everyone, I need help to write a program to change the label size when change zoom level of my map in ArcGIS. Thank you, Narguess -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Dec 3 22:23:41 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 04 Dec 2012 08:23:41 +1100 Subject: [Tutor] writing a program In-Reply-To: References: Message-ID: <50BD185D.6030305@pearwood.info> On 04/12/12 03:17, Narguess Dadfar wrote: > Hi everyone, > > I need help to write a program to change the label size when change zoom > level of my map in ArcGIS. Please read this web page carefully: http://www.catb.org/esr/faqs/smart-questions.html it will provide you with the help you need. If anything is still unclear after reading that page, please feel free to ask additional questions here. Thank you, -- Steven From sganeshhcu at gmail.com Mon Dec 3 23:04:34 2012 From: sganeshhcu at gmail.com (sree ganesh) Date: Mon, 3 Dec 2012 23:04:34 +0100 Subject: [Tutor] for Python tutor at Hyderabad Message-ID: hi all, Are there any python expert in Hyderabad? please let me know as early as possible. -- Cheers, Sree -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Tue Dec 4 00:09:27 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 3 Dec 2012 23:09:27 +0000 Subject: [Tutor] Pypi entry points In-Reply-To: <1354268302.58416.YahooMailNeo@web163802.mail.gq1.yahoo.com> References: <1354268302.58416.YahooMailNeo@web163802.mail.gq1.yahoo.com> Message-ID: On 30 November 2012 09:38, Albert-Jan Roskam wrote: > I am reading The Hitchhiker's Guide to Packaging 1.0 documentation, specifically http://guide.python-distribute.org/creation.html. I am struggling to understand the section about Entry Points. This appears to be a way to include extra functionality in a program, without the need to actually include it in the tar.gz file. Or, as the website says: "[A] pretty easy and simple way to allow other packages to register something > that you want to know. Extra plugins, extra render methods, extra functionality > you want to register in your web application, etcetera." I find the code examples hard to understand. I'd expect that it'd be steps like "in the middle of the setup, download and unzip file xxxx.zip from website http://blaaah.eu. How does this work? There are currently a lot of problems with documentation for features available in setuptools/distribute/distutils2 and so on. The page you linked to certainly seems incomplete to me and the text concerning entry points seems to be lifted from here http://reinout.vanrees.org/weblog/2010/01/06/zest-releaser-entry-points.html Try reading this http://packages.python.org/distribute/setuptools.html#extending-and-reusing-distribute If that doesn't help then further questions are probably more appropiate on the distutils-sig mailing list http://mail.python.org/mailman/listinfo/distutils-sig Oscar From alan.gauld at btinternet.com Tue Dec 4 00:36:29 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 03 Dec 2012 23:36:29 +0000 Subject: [Tutor] for Python tutor at Hyderabad In-Reply-To: References: Message-ID: On 03/12/12 22:04, sree ganesh wrote: > hi all, > Are there any python expert in Hyderabad? please let me know as early as > possible. I've no idea if any are in Hyderabad but the internet is full of them and you can access many of them via this list. The purpose is not to allocate individual tutors to students but for students to ask questions and interact online via the list. So if you have any questions about learning to program in Python, feel free to ask. Try to tell us which version of Python you re using, which OS and if you get any error messages send us a cut 'n paste of the actual error text. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From lukepdev at gmail.com Tue Dec 4 00:52:05 2012 From: lukepdev at gmail.com (Luke Paireepinart) Date: Mon, 3 Dec 2012 17:52:05 -0600 Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: <50BCC443.4000402@pearwood.info> References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> Message-ID: On Mon, Dec 3, 2012 at 9:24 AM, Steven D'Aprano wrote: > On 04/12/12 00:55, Spectral None wrote: > >> From:"tutor-request at python.org**" >> To:tutor at python.org >> Sent: Sunday, 2 December 2012, 17:34 >> Subject: Tutor Digest, Vol 106, Issue 5 >> >> Send Tutor mailing list submissions to >> tutor at python.org >> >> To subscribe or unsubscribe via the World Wide Web, visit >> http://mail.python.org/**mailman/listinfo/tutor >> or, via email, send a message with subject or body 'help' to >> tutor-request at python.org >> >> You can reach the person managing the list at >> tutor-owner at python.org >> >> When replying, please edit your Subject line so it is more specific >> than "Re: Contents of Tutor digest..." >> >> >> Today's Topics: >> >> 1. Re: reverse diagonal (Dave Angel) >> 2. To Find the Answers (Sujit Baniya) >> 3. Re: To Find the Answers (Dave Angel) >> 4. Re: reverse diagonal (Steven D'Aprano) >> 5. 1 to N searches in files (Spectral None) >> 6. Re: 1 to N searches in files (Steven D'Aprano) >> > > > Okay, this is where I stopped reading. > > You should pay attention to the emails that other people are sending. > Notice how they reply to individual emails, not to a mass digest? Then > pay attention to *your* email. Imagine you were receiving it. Did you > not notice that your reply was SIX PAGES LONG? > > Can you imagine if everyone did what you just did? The first reply would > be six pages, then the reply to that would be 12 pages, the reply to that > would be 24 pages, then 48 pages... > > If you want a response to your question, please try again. This time: > > - only reply to a SINGLE email at a time, not six; > > - only quote the parts of the email that are relevant; > > - use a meaningful subject line that summarizes your question. > > If you don't do these things, you will soon find that nobody will be > interested in digging through the piles and piles of dross looking for > your comments buried deep in your reply. > > [snipped 200-odd lines] > > > > -- > Steven > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From lukepdev at gmail.com Tue Dec 4 00:56:30 2012 From: lukepdev at gmail.com (Luke Paireepinart) Date: Mon, 3 Dec 2012 17:56:30 -0600 Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> Message-ID: I apologize for the blank message I just sent, the "send" button is near the "..." button. > >> Okay, this is where I stopped reading. >> >> You should pay attention to the emails that other people are sending. >> Notice how they reply to individual emails, not to a mass digest? Then >> pay attention to *your* email. Imagine you were receiving it. Did you >> not notice that your reply was SIX PAGES LONG? >> [...] >> If you don't do these things, you will soon find that nobody will be >> interested in digging through the piles and piles of dross looking for >> your comments buried deep in your reply. >> >> I just wanted to make the observation that, at least in gmail, the default behavior is to hide the entire quoted text behind an innocuous "..." button. So when writing a reply you really DO NOT see that it has 6 pages of quotes in some mail readers. In fact I would (were I not familiar with mailing lists) assume that "..." was not hiding much behind it at all. Now once someone knows that this is the case, I'm sure they can take steps to avoid it. But I can easily see how someone new to the list would get confused. I also did not read the original e-mail due to its cluttered nature. Thanks, -Luke -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Tue Dec 4 04:31:44 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 4 Dec 2012 14:31:44 +1100 Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> Message-ID: <20121204033144.GA15150@ando> On Mon, Dec 03, 2012 at 05:56:30PM -0600, Luke Paireepinart wrote: > I just wanted to make the observation that, at least in gmail, the default > behavior is to hide the entire quoted text behind an innocuous "..." > button. Good lord, the more I hear about Gmail, the more horrible I discover it to be. Why does anyone use this crappy, anti-social product? -- Steven From breamoreboy at yahoo.co.uk Tue Dec 4 06:23:12 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 04 Dec 2012 05:23:12 +0000 Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: <20121204033144.GA15150@ando> References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> <20121204033144.GA15150@ando> Message-ID: On 04/12/2012 03:31, Steven D'Aprano wrote: > On Mon, Dec 03, 2012 at 05:56:30PM -0600, Luke Paireepinart wrote: > >> I just wanted to make the observation that, at least in gmail, the default >> behavior is to hide the entire quoted text behind an innocuous "..." >> button. > > Good lord, the more I hear about Gmail, the more horrible I discover it > to be. Why does anyone use this crappy, anti-social product? > > Sales and marketing? :) -- Cheers. Mark Lawrence. From mumair_masood at hotmail.com Tue Dec 4 09:38:39 2012 From: mumair_masood at hotmail.com (Umair Masood) Date: Tue, 4 Dec 2012 13:38:39 +0500 Subject: [Tutor] Information Message-ID: Hi all, I would be working on a small project, which will include the SOAP library to use the web services using python. Also I want to know how to link XML to python. I mean suppose i am a client and i am accessing a server and suddenly it gives me an error which is being displayed on the website. I am working on something like this. I would code the client and server and when the client faces some problem it gets an error and uses the soap library to access the web services. I hope everybody can understand, if not clear I will ask my professor again and tell you exactly what does he want. Regards, Umair -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Tue Dec 4 10:18:35 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 4 Dec 2012 01:18:35 -0800 (PST) Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: <20121204033144.GA15150@ando> References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> <20121204033144.GA15150@ando> Message-ID: <1354612715.9694.YahooMailNeo@web163806.mail.gq1.yahoo.com> >On Mon, Dec 03, 2012 at 05:56:30PM -0600, Luke Paireepinart wrote: > >> I just wanted to make the observation that, at least in gmail, the default >> behavior is to hide the entire quoted text behind an innocuous "..." >> button. > >Good lord, the more I hear about Gmail, the more horrible I discover it >to be. Why does anyone use this crappy, anti-social product? > To promote Google's information obesity? ;-) From wayne at waynewerner.com Tue Dec 4 16:18:48 2012 From: wayne at waynewerner.com (Wayne Werner) Date: Tue, 4 Dec 2012 09:18:48 -0600 (CST) Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: <20121204033144.GA15150@ando> References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> <20121204033144.GA15150@ando> Message-ID: On Tue, 4 Dec 2012, Steven D'Aprano wrote: > On Mon, Dec 03, 2012 at 05:56:30PM -0600, Luke Paireepinart wrote: > >> I just wanted to make the observation that, at least in gmail, the default >> behavior is to hide the entire quoted text behind an innocuous "..." >> button. > > Good lord, the more I hear about Gmail, the more horrible I discover it > to be. Why does anyone use this crappy, anti-social product? Because it didn't use to suck. Also protip: you can use with gmail. Like alpine ;) -Wayne From doark at mail.com Tue Dec 4 19:20:21 2012 From: doark at mail.com (frank ernest) Date: Tue, 04 Dec 2012 13:20:21 -0500 Subject: [Tutor] insufficient control of ossaudiodev and or wave modules Message-ID: <20121204182022.55720@gmx.com> Opensuse 12.2 python3.2 I would like to set the to get better control of the play back of wave files. I would like to set the play back volume, send the audio out only one of the two speakers and ajust the play back speed. I am completely blank on ideas of how to accomplish this and reading the recommendation in the documentation suggesting that you should not try to rewrite part of the standard library by your self I have decided to ask how to go about this. Perhaps instead of reimplementing the library a new class should be created for basic audio manipulation. If so then how would I go about this? The wav file format is not exactly human readable. I have not seen anything similer to what I desire in the repo of 3rd party packages at the python web site. -------------- next part -------------- An HTML attachment was scrubbed... URL: From doark at mail.com Tue Dec 4 19:29:53 2012 From: doark at mail.com (frank ernest) Date: Tue, 04 Dec 2012 13:29:53 -0500 Subject: [Tutor] On understanding defintions Message-ID: <20121204182953.55700@gmx.com> Opensuse 12.2 python3.2 I discoverd that some of the examples for definitions in the tutorial are not valid. I am reporting this as a bug. In the mean time I tried to look up definitions in the Language Referance part of the python documentation but I'm still confused. The if, for, etc. are statements of evaluation, comparing one item to another; so what does that make definitions? What is self? How are definitions properly used? What are the mandetory parts? What are the optional parts? (in if statements the optional parts are or, and, elif, else, etc.) I also tried looking into the demos directory, I found lots of definitions but little help in understanding them. I am confused on all types of definitions not just functions or classes. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Dec 4 20:06:22 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 04 Dec 2012 19:06:22 +0000 Subject: [Tutor] On understanding defintions In-Reply-To: <20121204182953.55700@gmx.com> References: <20121204182953.55700@gmx.com> Message-ID: On 04/12/12 18:29, frank ernest wrote: > Opensuse 12.2 python3.2 > I discoverd that some of the examples for definitions in the tutorial > are not valid. I am reporting this as a bug. It's not clear what you mean by definitions here. > In the mean time I tried to look up definitions in the Language > Referance part of the python documentation but I'm still confused. The > if, for, etc. are statements of evaluation, comparing one item to > another; so what does that make definitions? I don't know what you mean by definitions? Can you give examples? > What is self? See the OOP topic in my tutorial. But given your other remarks I suspect its a topic you should leave until later. > How are definitions properly used? That depends on what you mean by definitions. > What are the mandetory parts? > What are the optional parts? > (in if statements the optional parts are > or, and, elif, else, etc.) No, 'or' and 'and' are not part of the if statement they are part of the *expression*. The expression is a mandatory part of the if statement. elif and else are optional. An expression is also mandatory with an elif > I also tried looking into the demos directory, I found lots of > definitions but little help in understanding them. > I am confused on all types of definitions not just functions or classes. It sounds like you need to step back and start at the beginning. Do you know any other programming languages? If so, work through the official python tutor and ask specific questions here when you get stuck (with a reference to the relevant tutor section). http://docs.python.org/3/tutorial/index.html If not start with one of the Non Programmers tutorials - perhaps mine(see below)! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wprins at gmail.com Tue Dec 4 20:13:45 2012 From: wprins at gmail.com (Walter Prins) Date: Tue, 4 Dec 2012 19:13:45 +0000 Subject: [Tutor] insufficient control of ossaudiodev and or wave modules In-Reply-To: <20121204182022.55720@gmx.com> References: <20121204182022.55720@gmx.com> Message-ID: Hi, On 4 December 2012 18:20, frank ernest wrote: > Opensuse 12.2 python3.2 > I would like to set the to get better control of the play back of wave > files. I would like to set the play back volume, send the audio out only > one of the two speakers and ajust the play back speed. I am completely > blank on ideas of how to accomplish this and reading the recommendation in > the documentation suggesting that you should not try to rewrite part of the > standard library by your self I have decided to ask how to go about this. Perhaps instead of reimplementing the library a new class should be created > for basic audio manipulation. > If so then how would I go about this? > The wav file format is not exactly human readable. > I have not seen anything similer to what I desire in the repo of 3rd party > packages at the python web site. > Manipulating a Wav file or the data in a wav file is not the same thing as controlling the audio hardware & audio mixer on your computer. You can change the playback volume of a file by either changing the file data before playback, or by controlling the mixer during playback. You seem to be slightly conflating these 2 things. So what is it you're trying to do in reality? Control the volume of playback or manipulate an existing WAV file in terms of the volume (amplitude) of the audio waveform in the file? Anyway, the mixer volume can be controlled with the oss_mixer_device object (using methods set() and get()) which is part of the ossaudiodev model you've already found. As for actually getting data into and out of wave files (reading data from them, and writing them), that's what the wave module is useful for. However it doesn't provide much if anything in the way of actually manipulating audio -- it's only use is to hide the detail of the Wav file, allowing you to get the raw data in the file and write it without having to worry about the actual Wav format. For more complex audio transformations you might consider the pysox module in conjunction with the sox library: http://pypi.python.org/pypi/pysox As a bit of background: "Sox" is a very capable sound manipulation program which is basically a wrapper over the underlying programming library called "libsox". "pysox" then is a Python wrapper for libsox that allows you to use it from within Python. It's probably the easiest way to do "standard"-ish transformations on sound files. Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Dec 4 20:19:13 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 04 Dec 2012 19:19:13 +0000 Subject: [Tutor] Information In-Reply-To: References: Message-ID: On 04/12/12 08:38, Umair Masood wrote: > Hi all, > I would be working on a small project, which will include the SOAP > library to use the web services using python. This list is for teaching the basics of the Python language and standard library. There is no SOAP library in Python. You will need to be more specific about what you are trying to do and what libraries you are using. > link XML to python. I mean suppose i am a client and i am accessing a > server and suddenly it gives me an error which is being displayed on the > website. I am working on something like this. I would code the client > and server and when the client faces some problem it gets an error and > uses the soap library to access the web services. There are libraries to call web services that you can download. There are also libraries for parsing XML. Of the ones in the standard library etree is probably the best option. If you can already program in Python you may find these useful as a starter: http://www.diveintopython.net/soap_web_services/index.html http://docs.python.org/3/library/xml.etree.elementtree.html#module-xml.etree.ElementTree -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From d at davea.name Tue Dec 4 20:19:20 2012 From: d at davea.name (Dave Angel) Date: Tue, 04 Dec 2012 14:19:20 -0500 Subject: [Tutor] On understanding defintions In-Reply-To: <20121204182953.55700@gmx.com> References: <20121204182953.55700@gmx.com> Message-ID: <50BE4CB8.5060709@davea.name> On 12/04/2012 01:29 PM, frank ernest wrote: > Opensuse 12.2 python3.2 > I discoverd that some of the examples for definitions in the tutorial are not valid. I am reporting this as a bug. And what are the bug numbers? Or links to the bug reports? Otherwise why are you telling us about them without mentioning any details? > In the mean time I tried to look up definitions in the Language Referance part of the python documentation but I'm still confused. The if, for, etc. are statements of evaluation, comparing one item to another; so what does that make definitions? "Definitions" is an English word. How do you mean it here? Are you perhaps referring to the 'def' keyword? Or to assignment? Or to the 'with' statement. Or to the 'lambda' expression. > What is self? 'self' is a convention used in naming the special instance argument in instance methods. This allows a special syntax for supplying that first argument. 'cls' is a similar convention for the class argument in class methods. Static methods have no special argument. 'self' is roughly analogous to 'this used in C++ and Java, with the main difference being that it's never implied. > How are definitions properly used? > What are the mandetory parts? > What are the optional parts? (in if statements the optional parts are or, and, elif, else, etc.) 'if', 'elif' and 'else' are statements. 'and' and 'or' are entirely different; they are operators, and have nothing to do with those three statements. The first two of those three statements take an expression, which may or may not include an operator, or twenty five operators. Nothing special about 'and' nor 'or' (nor '+', or '^' nor ...) > I also tried looking into the demos directory, I found lots of definitions but little help in understanding them. > I am confused on all types of definitions not just functions or classes. > > Perhaps if you gave us some idea of your background, we'd have a better idea at what level to aim our discussion. For questions like this, I'd personally start with the bnf for the language, if you have the background to understand it. I first learned bnf over 40 years ago, and it's still useful for learning a new language. http://docs.python.org/3.4/reference/grammar.html On the other hand, if this is your first language, it's totally the wrong level to start. I had 4 under my belt before taking the compiler design class that introduced bnf. -- DaveA From doark at mail.com Tue Dec 4 22:51:55 2012 From: doark at mail.com (frank ernest) Date: Tue, 04 Dec 2012 16:51:55 -0500 Subject: [Tutor] I'm trying to wrap my head around defs Message-ID: <20121204215156.55720@gmx.com> Opensuse 12.2 python3.2 I'm having trouble understanding them. Python is my first language... excluding English. I've already read ALL OF THE PYTHON Language Referance and I still don't "get it." What do they do? How do they work? How do you define self? Is self an object, a variable, or what? How about some over commented examples? The if statement is a statement of comparison. what is a def and more specifically a class? -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Tue Dec 4 23:09:22 2012 From: d at davea.name (Dave Angel) Date: Tue, 04 Dec 2012 17:09:22 -0500 Subject: [Tutor] On understanding defintions In-Reply-To: <20121204211813.55680@gmx.com> References: <20121204211813.55680@gmx.com> Message-ID: <50BE7492.1070408@davea.name> (responding on-list to an offline question) On 12/04/2012 04:18 PM, doark at mail.com wrote: > Here's a better question. When is it appropriate to use a def statement? > Welcome to the Python tutor mailing list. A function is usually defined by a def statement and the suite of indented lines that follow. (There are other ways to get a function object, including lambda, but that's an advanced topic) A function may be called from other places in the code, and optionally arguments may be passed to that function and/or returned from that function. There are many reasons to wrap one or more lines of code into a function and call it, but the most important is one of clarity. If you give it a good name, and understand what it does, then the place where it is called becomes easier to understand. For example, somebody wrote the function math.tan(). It takes one argument, and returns one result. And the work it does is to calculate a tangent of an angle given in radians. The function is not part of python the language, but it is part of the standard library. When I write a program that needs that work done, rather than including the complexity in each place, I refer to the one that was written (by someone else), and my code is much clearer for it, because anyone reading my code knows (or can find out) what math.tan() does. The same applies for some code you write strictly for your own use. Even if the whole program is in one file, it's still useful to encapsulate it into distinct parts, most of which perform some fairly simple set of operations. A second reason is shortening of the code. If you write a (non-trivial) function once, and call it from ten places, then your code is shorter by 9 copies of the function's body, more or less. That might not sound like a big deal, but it also means that if you find a problem with the algorithm, you can fix it in one place, and not have to hope that you got all ten places where you made the same error. Your next question is apparently what the elements of the 'def' statement mean. The simplest type of function might be like: def print_separator(): print ("----------") This function takes no arguments, and returns no results. But it does something useful. You call it with print_separator() So you might have a set of code something like print(value1) print_separator() print(value2) print_separator() print(math.tan(value99) Let's suppose you later decided to change the dashes to some other character. You could just change the body of the def, and all the calls would use the new string. But most functions take one or more arguments. So let's add some formal parameters to our definition. For now, we'll just use positional parameters (leaving keyword parameters for chapter 6). def print_separator(size): print("-"*size) #print 'size' copies of the character '-' This takes one argument and binds it to the formal parameter size. Then size can be used inside the function, without caring how it was produced. Now you can produce a different sized separator just by varying the argument to this function. print(value1) print_separator(40) print(value2) print_separator(10 * len(value2)) print(math.tan(value99) Now, you could add a second parameter to the function, by just adding a comma and name): def print_separator(size, char): print(char * size) #print 'size' copies of the specified character Now the caller can specify both. And in fact MUST supply both. If that's undesirable, then you can give defaults to one or both. def print_separator(size=10, char="-"): Now, if it's called with no arguments, it works like the first one. If it's called with one, it better be an integer. And if it's called with two, the first better be an integer and the second a string. No idea if that's what you wanted, but if you want much more, you should be working through a tutorial. Be sure the version of tutorial matches the version of Python you're using. Same is true of those examples you found "bugs" in. Could be the page they're on was meant for a different version of Python than what you're running. For example, in Python 2.x, 'print' was a statement, while in 3.x it's a function. >> ----- Original Message ----- >> From: Dave Angel >> Sent: 12/04/12 02:19 PM >> To: frank ernest >> Subject: Re: [Tutor] On understanding defintions >> >> On 12/04/2012 01:29 PM, frank ernest wrote: >>> Opensuse 12.2 python3.2 >>> I discoverd that some of the examples for definitions in the tutorial are not valid. I am reporting this as a bug. >> >> And what are the bug numbers? Or links to the bug reports? Otherwise >> why are you telling us about them without mentioning any details? >> To log a bug, or search for an existing one, go to http://bugs.python.org/ . Searching there, i don't see any bugs logged by you. So what did you mean by report? If there are legitimate bugs, they certainly should be reported. But perhaps you might want to describe them here, to make sure they're not just misunderstandings. -- DaveA From steve at pearwood.info Tue Dec 4 23:11:26 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 05 Dec 2012 09:11:26 +1100 Subject: [Tutor] On understanding defintions In-Reply-To: <20121204182953.55700@gmx.com> References: <20121204182953.55700@gmx.com> Message-ID: <50BE750E.3040802@pearwood.info> On 05/12/12 05:29, frank ernest wrote: > Opensuse 12.2 python3.2 > I discoverd that some of the examples for definitions in the >tutorial are not valid. I am reporting this as a bug. Don't be shy, let us know the ticket numbers :) I've looked on the bug tracker and can't see any sign of your bug report: http://bugs.python.org/ But I think that before you report this as a bug, you ought to get agreement from others that it is a bug. How about you link to the page in the tutorial that you think is not valid, and tell us what parts are not valid? > In the mean time I tried to look up definitions in the >Language Referance part of the python documentation but I'm >still confused. The if, for, etc. are statements of evaluation, >comparing one item to another; so what does that make definitions? Frank, are you a native English speaker? This is a multinational forum and we have many people here to whom English is their second, or third, language. It isn't clear to me whether you are having trouble understanding the English word "definition", or whether you don't understand some of the *specific definitions* of Python terms. Perhaps you could try explaining your problem again, in a little more detail. > What is self? "self" is a function parameter which is automatically provided by the Python interpreter when you call a method. Does that help? Have you programmed using object-oriented programming before? Do you have any previous programming experience? It is hard to know what level of explanation you need. > How are definitions properly used? I'm sorry, I don't understand your question. > What are the mandetory parts? "Mandatory" means that something is compulsory, that it must be present. For example, Python has an "if...elif...else" statement: if x > 100: print("x is larger than one hundred") elif x < 10: print("x is smaller than ten") else: print("x is between ten and one hundred") In this case, the "if" part is *mandatory* -- it must be there, or you don't have an "if" statement at all. But the other two parts, the "elif" and "else" parts, are optional. # "elif" or "else" on its own is NOT allowed, since "if" is # mandatory but not given. This will give a SyntaxError: else x < 0: print("x is negative") # On the other hand, "if" on its own is allowed, since the # other parts are optional. if x < 0: print("x is negative") > I am confused on all types of definitions not just >functions or classes. I'm not going to try to guess what things confuse you. Please pick *one* concept that confuses you, one at a time, and ask about it. We can that explain that. -- Steven From francois.dion at gmail.com Tue Dec 4 23:21:23 2012 From: francois.dion at gmail.com (Francois Dion) Date: Tue, 4 Dec 2012 17:21:23 -0500 Subject: [Tutor] I'm trying to wrap my head around defs In-Reply-To: <20121204215156.55720@gmx.com> References: <20121204215156.55720@gmx.com> Message-ID: On Tue, Dec 4, 2012 at 4:51 PM, frank ernest wrote: > Opensuse 12.2 python3.2 > I'm having trouble understanding them. > Python is my first language... excluding English. > I've already read ALL OF THE PYTHON Language Referance and I still don't > "get it." > What do they do? Let's start simple. It is a Python keyword to define a function. > How do they work? Just like a function in mathematics, they take some arguments, do stuff with it (hopefully) and more than likely return something. For example, cos(angle) defines the cosinus calculation for a given angle. This function is an object that has been defined at some point by a def cos(angle): So there is a difference between *def*ining a function and using it, hence the def keyword. > How do you define self? You do not. It is related to scope, > The if statement is a statement of comparison. what is a def and more > specifically a class? You really need to understand functions first, then scope, then we can talk class. So, my question to you before we get too far, are you ok with the concept of mathematical functions? Francois -- www.pyptug.org - raspberry-python.blogspot.com From alan.gauld at btinternet.com Tue Dec 4 23:31:25 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 04 Dec 2012 22:31:25 +0000 Subject: [Tutor] I'm trying to wrap my head around defs In-Reply-To: <20121204215156.55720@gmx.com> References: <20121204215156.55720@gmx.com> Message-ID: On 04/12/12 21:51, frank ernest wrote: > Opensuse 12.2 python3.2 > I'm having trouble understanding them. > Python is my first language... excluding English. OK, in that case go back to basics. Don't read the reference manual, that's like trying to learn English by reading the Oxford English Dictionary. Its a great reference but its not a tutorial. Go to the Non Programmers page and use one of the tutorials there. They all have slightly different styles so pick the one that looks to make most sense to you. Mine if you like it! :-) > I've already read ALL OF THE PYTHON Language Referance and I still don't > "get it." I'm not surprised, it's designed for people who know how to program in Python to understand the internal details. Even the official tutorial assumes you already know the basic concepts of programming. That's why you need to go to the Non programmers page. They assume you know nothing beyond computer basics (how to create a text file, navigate the file structure and run a program). > What do they do? > How do they work? The tutorials will explain. > How do you define self? The tutorials should explain. Dave already has, but I doubt you followed his explanation. > Is self an object, a variable, or what? Yes, its both an object and a variable. Specifically its a name that refers to an object. But you don't need to worry about self yet, you need to go back to the beginning. > How about some over commented examples? The tutorials should provide that. > The if statement is a statement of comparison. what is a def and more > specifically a class? A def is how to define a function, which is a reusable piece of code. A class is how to define a class which is a template for a set of objects. But the tutorials should explain all of that. Seriously, go through some of the tutorials. If you get stuck come back here with specific questions and references to the tutorial material you are stuck with. It may seem tedious but it is the fastest way in the long run. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From d at davea.name Tue Dec 4 23:37:00 2012 From: d at davea.name (Dave Angel) Date: Tue, 04 Dec 2012 17:37:00 -0500 Subject: [Tutor] I'm trying to wrap my head around defs In-Reply-To: <20121204215156.55720@gmx.com> References: <20121204215156.55720@gmx.com> Message-ID: <50BE7B0C.8070309@davea.name> On 12/04/2012 04:51 PM, frank ernest wrote: > Opensuse 12.2 python3.2 > I'm having trouble understanding them. > Python is my first language... excluding English. > I've already read ALL OF THE PYTHON Language Referance and I still don't "get it." You usually should start with a tutorial, not with a reference. A reference assumes some working knowledge, either from previously doing a tutorial, or from many other languages where you're really just looking for differences. You also need to make sure that the version of both tutorial and reference matches your version. Otherwise you'll needlessly confuse yourself with differences. This is especially true between Python 2.x and 3.x. http://docs.python.org/3.2/tutorial/ > What do they do? > How do they work? A function definition defines a section of the code that can be called from multiple places. Each time it's called, control transfers to the function's first line, and continues till the function returns. Then control continues on the line that contained that function call, possibly returning a value to be used in that expression. > How do you define self? > Is self an object, a variable, or what? 'self' is a common convention for the first formal parameter to an ordinary class method. But since 'class' is an advanced topic, you'd do far better to defer thinking about it. > How about some over commented examples? Using the first example of section 4.6 ("Defining Functions") from that tutorial I gave you a link for. def fib(n): # write Fibonacci series up to n """Print a Fibonacci series up to n.""" a, b = 0, 1 #build a tuple of (0,1). Unpack it to the names a and b, respectively #equiv. to a=0; b=1 while a < n: #starts a loop, which will repeat until a >= n print(a, end=' ') #print current value of n, but without a newline a, b = b, a+b #build a tuple (b, a+b), and then unpack it to a and b print() #print a newline > The if statement is a statement of comparison. what is a def and more specifically a class? > The 'if' statement has nothing to do with comparison. It simply arranges conditional change in the order of execution, based on the "truthness" of an expression. In other words, instead of proceeding in sequential order, the interpreter skips over either the then-portion or the else-portion, depending on how the expression evaluates. The most common USE of an 'if' statement will be of some form like: if x<10: But understand that 'if' doesn't care, as long as the expression can produce a boolean. -- DaveA From eman_no1 at yahoo.com Wed Dec 5 08:13:27 2012 From: eman_no1 at yahoo.com (Erik Martinson) Date: Tue, 4 Dec 2012 23:13:27 -0800 (PST) Subject: [Tutor] Help with web.py error Message-ID: <1354691607.72271.YahooMailNeo@web125703.mail.ne1.yahoo.com> Hi, I need some help tracking the cause of this error. I have been looking at Python off and on for the last year or so. I write ASP classic and VB6 as my day job (yeah, I know.., it pays the bills). So some of this may be me have to think differently. I have tried the web.py group with no answer. I am probably not asking the right question or they think it is a SQLlite error. Here is the complete traceback Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 239, in process ?return self.handle() File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 230, in handle ?return self._delegate(fn, self.fvars, args) File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 420, in _delegate ?return handle_class(cls) File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/application.py", line 396, in handle_class ?return tocall(*args) File "Code.py", line 24, in GET ?todos = db.select('todo') File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/db.py", line 682, in select ?return self.query(qout, processed=True) File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/db.py", line 1030, in query ?out = DB.query(self, *a, **kw) File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/db.py", line 644, in query ?self._db_execute(db_cursor, sql_query) File "/usr/local/lib/python2.7/dist-packages/web.py-0.37-py2.7.egg/web/db.py", line 587, in _db_execute ?out = cur.execute(query, params) File "/usr/lib/python2.7/sqlite3/dbapi2.py", line 63, in convert_date ?return datetime.date(*map(int, val.split("-"))) ValueError: invalid literal for int() with base 10: '21 01:47:43' 127.0.0.1:59850 - - [04/Dec/2012 22:47:35] "HTTP/1.1 GET /" - 500 Internal Server Error I understand the error, there is no way '21 01:47:43' can become an int. What I can not figure out is the correlation between the last two lines in the traceback. What is actually calling the last command 'datetime.date'? Here is another kicker, I have tesed this on 4 computers, all with Python 2.7 and the lastest web.py egg. One of the computers works without an error. I did some additional testing by hijacking db.py and adding text to line 587 that would normally cause a meltdown. I can say that on the working computer the function datetime.date is not being called. Why on one computer and not the others. I know the code is the same becuse the files are synced via DropBox on all 4 machines. I have also written a test script that calls the SQLite lib directly and opens a connection and recordset. This works on all 4 machines. All of my code for this is here on this shared dropbox folder. Code.py is the stater file for web.py https://www.dropbox.com/sh/1zqjgtj86q033r8/LBgzPx-kLq Thanks, ? Erik Martinson -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Wed Dec 5 09:36:30 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 5 Dec 2012 03:36:30 -0500 Subject: [Tutor] Help with web.py error In-Reply-To: <1354691607.72271.YahooMailNeo@web125703.mail.ne1.yahoo.com> References: <1354691607.72271.YahooMailNeo@web125703.mail.ne1.yahoo.com> Message-ID: On Wed, Dec 5, 2012 at 2:13 AM, Erik Martinson wrote: > > File "/usr/lib/python2.7/sqlite3/dbapi2.py", line 63, in convert_date > return datetime.date(*map(int, val.split("-"))) > > ValueError: invalid literal for int() with base 10: '21 01:47:43' > 127.0.0.1:59850 - - [04/Dec/2012 22:47:35] "HTTP/1.1 GET /" - 500 Internal > Server Error > > I understand the error, there is no way '21 01:47:43' can become an int. > What I can not figure out is the correlation between the last two lines in > the traceback. What is actually calling the last command 'datetime.date'? See the web.py SqliteDB class: https://github.com/webpy/webpy/blob/master/web/db.py#L1010 Notice it sets 'detect_types'. More here: http://docs.python.org/2/library/sqlite3.html#module-functions-and-constants http://docs.python.org/2/library/sqlite3.html#default-adapters-and-converters The issue then is the "created" field in your todo table is type "date", but contains a "timestamp". From norman at khine.net Wed Dec 5 14:47:40 2012 From: norman at khine.net (Norman Khine) Date: Wed, 5 Dec 2012 14:47:40 +0100 Subject: [Tutor] correct way to encode Message-ID: hello, i have this code from the google fusion table api: (zmgc)? python * master 9e4be39 ?zmgc" Python 2.7.2 (default, Jan 28 2012, 14:53:22) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import csv >>> import urllib2, urllib >>> request_url = 'https://www.google.com/fusiontables/api/query' >>> query = 'SELECT * FROM 3027809' >>> url = "%s?%s" % (request_url, urllib.urlencode({'sql': query})) >>> serv_req = urllib2.Request(url=url) >>> serv_resp = urllib2.urlopen(serv_req) >>> reader = csv.DictReader(serv_resp) >>> for row in reader: ... print row ... {'Name': 'Portugal', 'Contact': 'info at zeitgeistportugal.org', 'Link': 'http://www.zeitgeistportugal.org/', 'Location': 'Portugal', 'Type': 'Country', 'Icon': '1'} {'Name': 'Porto', 'Contact': 'porto at zeitgeistportugal.org', 'Link': 'http://porto.zeitgeistportugal.org', 'Location': 'Porto, Portugal', 'Type': 'Region', 'Icon': '2'} {'Name': 'Lisboa', 'Contact': 'lisboa at zeitgeistportugal.org', 'Link': 'http://lisboa.zeitgeistportugal.org', 'Location': 'Lisbon, Portugal', 'Type': 'Region', 'Icon': '2'} {'Name': '\xd0\x91\xd1\x8a\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd0\xb8\xd1\x8f', 'Contact': 'zgeistbg at gmail.com', 'Link': 'http://thezeitgeistmovement.bg/', 'Location': 'Bulgaria', 'Type': 'Country', 'Icon': '1'} the table has a mix of charecters: https://www.google.com/fusiontables/DataSource?docid=1epTUiUlv5NQK5x4sgdy1K47ACDTpHH60hbng1qw what will be the correct way to encode the items in each dictionary row? thanks norman -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From hugo.yoshi at gmail.com Wed Dec 5 15:24:49 2012 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 5 Dec 2012 15:24:49 +0100 Subject: [Tutor] correct way to encode In-Reply-To: References: Message-ID: On Wed, Dec 5, 2012 at 2:47 PM, Norman Khine wrote: > hello, i have this code from the google fusion table api: > > (zmgc)? python > * master 9e4be39 ?zmgc" > Python 2.7.2 (default, Jan 28 2012, 14:53:22) > [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > >>> import csv > >>> import urllib2, urllib > >>> request_url = 'https://www.google.com/fusiontables/api/query' > >>> query = 'SELECT * FROM 3027809' > >>> url = "%s?%s" % (request_url, urllib.urlencode({'sql': query})) > >>> serv_req = urllib2.Request(url=url) > >>> serv_resp = urllib2.urlopen(serv_req) > >>> reader = csv.DictReader(serv_resp) > >>> for row in reader: > ... print row > ... > {'Name': 'Portugal', 'Contact': 'info at zeitgeistportugal.org', 'Link': > 'http://www.zeitgeistportugal.org/', 'Location': 'Portugal', 'Type': > 'Country', 'Icon': '1'} > {'Name': 'Porto', 'Contact': 'porto at zeitgeistportugal.org', 'Link': > 'http://porto.zeitgeistportugal.org', 'Location': 'Porto, Portugal', > 'Type': 'Region', 'Icon': '2'} > {'Name': 'Lisboa', 'Contact': 'lisboa at zeitgeistportugal.org', 'Link': > 'http://lisboa.zeitgeistportugal.org', 'Location': 'Lisbon, Portugal', > 'Type': 'Region', 'Icon': '2'} > {'Name': > '\xd0\x91\xd1\x8a\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd0\xb8\xd1\x8f', > 'Contact': 'zgeistbg at gmail.com', 'Link': > 'http://thezeitgeistmovement.bg/', 'Location': 'Bulgaria', 'Type': > 'Country', 'Icon': '1'} > > > the table has a mix of charecters: > > https://www.google.com/fusiontables/DataSource?docid=1epTUiUlv5NQK5x4sgdy1K47ACDTpHH60hbng1qw > > what will be the correct way to encode the items in each dictionary row? > > The data you're getting back is almost certainly encoded in UTF-8. Googling around, the csv reader doesn't seem to work well at all when unicode is involved, but there are some people around trying to make it work. This stackoverflow thread might be helpful: http://stackoverflow.com/questions/1846135/python-csv-library-with-unicode-utf-8-support-that-just-works HTH, Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From malcolm.newsome at gmail.com Wed Dec 5 16:38:00 2012 From: malcolm.newsome at gmail.com (Malcolm Newsome) Date: Wed, 5 Dec 2012 09:38:00 -0600 Subject: [Tutor] Get/Set/Attribute Accessors in Python? Message-ID: Hey Tutors, Python is/was my first language. Yet, I've recently begun learning C# for my new job. One thing I've come across in C# (and, quite frankly, am having a difficult time grasping) is Get and Set (Accessors). Since, I don't ever recall reading about this in Python, I'm wondering if they exist. If not, what is the "thinking" behind why they are not included in the language? Additionally, a separate, but perhaps related question is that I have not seen public/private classes in Python. How might this factor into the whole accessor scenario? (Or, am I trying to relate two topics that have nothing to do with each other?) Hopefully these questions are clear enough... Thanks in advance, Malcolm Newsome -------------- next part -------------- An HTML attachment was scrubbed... URL: From francois.dion at gmail.com Wed Dec 5 17:30:47 2012 From: francois.dion at gmail.com (Francois Dion) Date: Wed, 5 Dec 2012 11:30:47 -0500 Subject: [Tutor] Get/Set/Attribute Accessors in Python? In-Reply-To: References: Message-ID: On Wed, Dec 5, 2012 at 10:38 AM, Malcolm Newsome wrote: > Hey Tutors, > > Python is/was my first language. Yet, I've recently begun learning C# for > my new job. My condolences. > One thing I've come across in C# (and, quite frankly, am having a difficult > time grasping) is Get and Set (Accessors). > Since, I don't ever recall reading about this in Python, I'm wondering if > they exist. If not, what is the "thinking" behind why they are not included > in the language? Nothing prevents you from creating getter and setter methods for all your values in your class in Python, but really, you don't need to most of the time. Refactoring and re-engineering your code later on to use them if you have to, is pretty much transparent to the rest of the code. For example: class Thing: colorinfo = "#f00:red" box = Thing() print(box.colorinfo) This will print: #f00:red You are happy about how short the code is and start using box.colorinfo all over the place. later, you decide, you should have used colorinfo as a method (a getter, ie getcolorinfo() ) instead of a property, because you want to add arguments to the constructor and use them in the getter. So you would then have to go and replace every box.colorinfo by box.colorinfo(), if you were not using python. But since you are, you end up doing a getter, and keep the property: class Thing: def __init__(self,color,name): self.color = color self.name = name def getcolorinfo(self): return self.color + ':' + self.name colorinfo = property(getcolorinfo) box = Thing("#f00","red") print(box.colorinfo) box = Thing("#00f","blue") print(box.colorinfo) The output of which is #f00:red #00f:blue The key here is that you are now wrapping the string inside a getter function, where you could do all kinds of other stuff, but yet, you never had to modify the print line since it still refers to a property of the box instance of the Thing object. All because of the property keyword. Best of both worlds. Francois -- www.pyptug.org - raspberry-python.blogspot.com From d at davea.name Wed Dec 5 17:39:26 2012 From: d at davea.name (Dave Angel) Date: Wed, 05 Dec 2012 11:39:26 -0500 Subject: [Tutor] Get/Set/Attribute Accessors in Python? In-Reply-To: References: Message-ID: <50BF78BE.90401@davea.name> On 12/05/2012 10:38 AM, Malcolm Newsome wrote: > Hey Tutors, > > Python is/was my first language. Yet, I've recently begun learning C# for > my new job. > > One thing I've come across in C# (and, quite frankly, am having a difficult > time grasping) is Get and Set (Accessors). > > Since, I don't ever recall reading about this in Python, I'm wondering if > they exist. If not, what is the "thinking" behind why they are not > included in the language? I don't know C#, but it's probably quite similar to Java in these things. Python doesn't need get and set, because it has a better mechanism. The whole idea of get and set in java is that a user of a class shouldn't make any assumption about internal details of a class. The user calls these two functions to read and write a property. Now, if the implementor changes his mind about what data format is actually used, he can still implement the original get and set function interfaces in terms of the new data fields. So, you might ask, how does the implementor decide which fields need such abstraction, and which ones can just be used, safe from likely change. Java's answer is that all fields should be wrapped in the getter and setter paradigm. In Python, the user of a class uses the data fields in the obvious way. Then when the implementor decides that a particular field needs "abstraction," he doesn't have to make the user change his use. Instead the implementor defines those get and set functions, and binds them to look like a simple field. That is most easily done by the @property decorator. > Additionally, a separate, but perhaps related question is that I have not > seen public/private classes in Python. How might this factor into the > whole accessor scenario? (Or, am I trying to relate two topics that have > nothing to do with each other?) > > Python doesn't have any such thing as private classes or private attributes. And it doesn't have friends, protected inheritance, etc. Instead it's a naming convention, that nobody is supposed to use any names with a leading underscore. The theory is that we're all adults here. Many times in C++ and Java, you need to cheat the private/protected schemes, to get something tricky accomplished. One time is when you're using a library for which you don't have permission to modify the source of a library. Another place is in implementing a library, where you need to follow different rules than the user of the library. In Python, instead of cheating (eg. by casting), you just access the item. I see that Francois has responded while I'm composing this. Please read that response as well as mine. -- DaveA From steve at pearwood.info Wed Dec 5 17:58:03 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 06 Dec 2012 03:58:03 +1100 Subject: [Tutor] Get/Set/Attribute Accessors in Python? In-Reply-To: References: Message-ID: <50BF7D1B.2030303@pearwood.info> On 06/12/12 02:38, Malcolm Newsome wrote: > Hey Tutors, > > Python is/was my first language. Yet, I've recently begun learning C# for > my new job. > > One thing I've come across in C# (and, quite frankly, am having a difficult > time grasping) is Get and Set (Accessors). > > Since, I don't ever recall reading about this in Python, I'm wondering if > they exist. If not, what is the "thinking" behind why they are not > included in the language? They exist, if you program them. Here's a class with getter and setter accessors: class Example(object): def __init__(self, value): self.setx(value) def getx(self): return self.x def setx(self, value) self.x = value instance = Example(42) print instance.getx() # gives 42 instance.setx(23) And here is how it should be written in Python: class Example(object): def __init__(self, value): self.x = value instance = Example(42) print instance.x # gives 42 instance.setx(23) Accessors are methods that you must call to get or set a data value. Some languages (C# and Java, for example) strongly encourage you to use accessors instead of exposing the data value directly. Why? "Just in case." Notice how trivially simple my accessors were? Well, they're not always that trivial. Often you want the getter to calculate a value, or the setter to enforce some restriction. Here's a toy example: class Example(object): def __init__(self, value): self.setx(value) def getx(self): print "I'm the x value!" return self.x def setx(self, value) if value < 0: raise ValueError('x must not be negative') self.x = value If you just exposed self.x as Python does, you can't perform any calculations unless you use accessor methods. So Java and C# have the philosophy: - Some day you might need to do a calculation when getting or setting x. - But if you expose data attribute x directly to the caller, you are committed to *always* exposing x to the caller. - So you better not expose x directly, you should always use accessor methods, just in case some day you need them. But with Python, you never need accessor methods, because we can wrap attribute access with methods behind the scene! This work is done using the "property" function. You start by trivially exposing data attribute x directly to the caller: class Example(object): def __init__(self, value): self.x = value then later on, if you need to turn x into a computed attribute, you simply use property to wrap some accessor methods with the same name as the data attribute: class Example(object): def __init__(self, value): self.x = value @property def x(self): print "I'm the x value!" return self._x # see below @x.setter def x(self, value): if value < 0: raise ValueError('x must not be negative') self._x = value As far as the caller is concerned, there is no change, she still writes the same direct attribute code: instance = Example(42) print instance.x instance.x = 23 as before. Did you notice the mysterious reference to self._x above? That leads neatly to your next question. > Additionally, a separate, but perhaps related question is that I have not > seen public/private classes in Python. How might this factor into the > whole accessor scenario? (Or, am I trying to relate two topics that have > nothing to do with each other?) Private versus Public (of classes, methods, attributes, whatever) has to do with information hiding. Information hiding is a very useful idea in programming. In a nutshell, you might have a class that promises to provide certain features, the interface, but everything else is an implementation detail that might go away if you find a better way to program the class. For example, everything inside a function -- the local variables -- are utterly private. The caller cannot peek inside a method and grab the local variables, they can only see the return result: def hundred(): x = 25 # this is local to the function and invisible from outside return x + x + x + x # but this is exposed publicly If I now improve this function: def hundred(): return 100 from the outside you have no way of knowing that local variable x has disappeared. This is a Good Thing. This philosophy of hiding implementation details is extended to classes, methods, and attributes. Hence, languages like C#, C++, Java and others distinguish between Public and Private attributes. Public attributes are visible to the caller, private ones are not. The problem is, like all good things, it is possible to have too much data hiding. People go mad and make everything private, forcing the caller to reinvent the wheel. Consequently you will often find programmers plaintively asking "I need to do foo, and this class does exactly what I want, but the foo() method is private, how do I get access to it?" For most languages, it is possible to to defeat the compiler and get access to supposedly private attributes. It's just messy and complicated, but people can and do. (Even local variables are not *truly* hidden. If a debugger can peek inside a function, so can you, if you do what the debugger does.) So Python takes a different approach. The Python philosophy is: "We're all adults here. If you want to shoot yourself in the foot, we won't stop you." Python doesn't enforce hiding of public and private attributes. If it did, people would find ways to break it, so instead we have a simple convention for public and private. Anything starting with a single underscore, like self._x, is considered to be private. If you mess with it, you have no-one to blame but yourself if the code shoots you in the foot. You have been warned. Anything not starting with a single underscore is public. (Names with double leading and trailing underscores, like __init__, are reserved for Python's use as special methods or attributes. You can use them if Python defines them, but don't use them for your own stuff.) -- Steven From cmcaine at googlemail.com Wed Dec 5 19:11:12 2012 From: cmcaine at googlemail.com (C M Caine) Date: Wed, 5 Dec 2012 18:11:12 +0000 Subject: [Tutor] Unexpected results with obj.method().method() Message-ID: Dear all, I've written a class State that subclasses tuple. The class has a method move_state that takes a move and returns a new state object representing the new state of the game. I would expect S1 and S3 to be equal on the last line here, but they are not. >>> import game >>> S = game.State() >>> S1 = S.move_state(1).move_state("SWAP") >>> S2 = S.move_state(1) >>> S3 = S2.move_state("SWAP") >>> S1 == S3 False Printing the two states shows that they have very different internal states. >>> print S1 (8, 8, 8, 8, 8, 8, 0) 1 0 (7, 7, 7, 7, 7, 7, 7) >>> print S3 (7, 7, 7, 7, 7, 7, 7) 0 1 (0, 8, 8, 8, 8, 8, 8) If anyone is interested, State represents the state of a 7 7 Kalah board. The full code is on pastebin http://pastebin.com/tUh0W5Se Are my expectations faulty? (I hope not) Have I made some mistake in my code to get these results? Thanks in advance, Colin Caine -------------- next part -------------- An HTML attachment was scrubbed... URL: From cmcaine at googlemail.com Wed Dec 5 19:17:24 2012 From: cmcaine at googlemail.com (C M Caine) Date: Wed, 5 Dec 2012 18:17:24 +0000 Subject: [Tutor] Unexpected results with obj.method().method() In-Reply-To: References: Message-ID: I edited the output of Lines 109-111 from my source code out of the interpreter transcripts above, by the by. On 5 December 2012 18:11, C M Caine wrote: > Dear all, > > I've written a class State that subclasses tuple. The class has a method > move_state that takes a move and returns a new state object representing > the new state of the game. > > I would expect S1 and S3 to be equal on the last line here, but they are > not. > > >>> import game > >>> S = game.State() > >>> S1 = S.move_state(1).move_state("SWAP") > >>> S2 = S.move_state(1) > >>> S3 = S2.move_state("SWAP") > >>> S1 == S3 > False > > Printing the two states shows that they have very different internal > states. > > >>> print S1 > (8, 8, 8, 8, 8, 8, 0) > 1 0 > (7, 7, 7, 7, 7, 7, 7) > >>> print S3 > (7, 7, 7, 7, 7, 7, 7) > 0 1 > (0, 8, 8, 8, 8, 8, 8) > > If anyone is interested, State represents the state of a 7 7 Kalah board. > > The full code is on pastebin http://pastebin.com/tUh0W5Se > > Are my expectations faulty? (I hope not) > Have I made some mistake in my code to get these results? > > Thanks in advance, > Colin Caine > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Wed Dec 5 21:50:38 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 5 Dec 2012 20:50:38 +0000 Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: <20121204033144.GA15150@ando> References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> <20121204033144.GA15150@ando> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418050D74@SCACMX008.exchad.jpmchase.net> Steven D'Aprano wrote: > > On Mon, Dec 03, 2012 at 05:56:30PM -0600, Luke Paireepinart wrote: > > > I just wanted to make the observation that, at least in gmail, the default > > behavior is to hide the entire quoted text behind an innocuous "..." > > button. > > Good lord, the more I hear about Gmail, the more horrible I discover it > to be. Why does anyone use this crappy, anti-social product? > I think Gmail is a great product and wish more desktop clients would act in the same manner. The main problems I have seen with are specific to dealing with mailing lists/newsgroup. It [Gmail] happens to be aimed at the masses and Python tutors tend not be a part of the masses when it comes to technology. :) Not to mention that as social conventions regarding email have moved on--e.g. top posting--for the majority of email users, members of mailing lists seem to have kept their traditions and habits. I am not saying there are not valid reasons or that I disagree with the reasons for mailing lists to keep their method of posting. It is just that this behavior does not apply to the average email user. Even in-line posting tends to be either copied with comments above the original message or a top post saying to look at the original message below for comments in a different color. Maybe I am in the minority, but the only people I know who regularly bottom/in-line post are regularly on mailing lists. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From d at davea.name Wed Dec 5 22:04:27 2012 From: d at davea.name (Dave Angel) Date: Wed, 05 Dec 2012 16:04:27 -0500 Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47418050D74@SCACMX008.exchad.jpmchase.net> References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> <20121204033144.GA15150@ando> <5B80DD153D7D744689F57F4FB69AF47418050D74@SCACMX008.exchad.jpmchase.net> Message-ID: <50BFB6DB.9020608@davea.name> On 12/05/2012 03:50 PM, Prasad, Ramit wrote: > >> >> Not to mention that as social conventions regarding email have moved >> on--e.g. top posting--for the majority of email users A bug that somehow convinced people that it was "normal." So other implementers copied the bug. -- DaveA From alan.gauld at btinternet.com Wed Dec 5 22:22:56 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 05 Dec 2012 21:22:56 +0000 Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47418050D74@SCACMX008.exchad.jpmchase.net> References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> <20121204033144.GA15150@ando> <5B80DD153D7D744689F57F4FB69AF47418050D74@SCACMX008.exchad.jpmchase.net> Message-ID: On 05/12/12 20:50, Prasad, Ramit wrote: > Maybe I am in the minority, but the only people I know who regularly > bottom/in-line post are regularly on mailing lists. This is a bad practice picked up from group-ware tools from the 80s/90s which unfortunately called themselves email tools, but didn't follow email standards - Lotus Notes and MS Outlook being the most obvious culprits. Unfortunately the corporate universe adopted these tools and many people's expectations of "email" were formed using these glorified message databases. The result is grossly inefficient use (abuse?) of email. But Ramit is right, the only people I see using email as it was intended are the long-term email users on the internet, the great non-technical masses use the standards set by the user defaults of Outlook and Notes. But that doesn't mean we have to accept gross inefficiency without protest. Even at work (using Outlook) I use inline posting as my default and one or two others have begun to adopt it too. :-) PS. Until recently I insisted on using plain text for my mails too but eventually I got so many complaints about my mails being hard to format for replies that I've relented and switched to HTML... PPS. There is one advantage to lazy top posting. When I return from vacation I sort by subject and only open the most recent in a thread. That way I can read up from the bottom and get all the others in one go. I then just delete the rest unread... Thanks to that trick I was able to "read" 700 emails in two days after returning from a weeks holiday. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From d at davea.name Wed Dec 5 22:31:05 2012 From: d at davea.name (Dave Angel) Date: Wed, 05 Dec 2012 16:31:05 -0500 Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> <20121204033144.GA15150@ando> <5B80DD153D7D744689F57F4FB69AF47418050D74@SCACMX008.exchad.jpmchase.net> Message-ID: <50BFBD19.4000206@davea.name> On 12/05/2012 04:22 PM, Alan Gauld wrote: > > > > PPS. > There is one advantage to lazy top posting. When I return from > vacation I sort by subject and only open the most recent in a thread. > That way I can read up from the bottom and get all the others in one > go. I then just delete the rest unread... Thanks to that trick I was > able to "read" 700 emails in two days after returning from a weeks > holiday. > For any email exchange that involves more than two people, you can easily lose content that way. Less common, if someone replies twice to the same message, they won't be captured in the default "final mail". -- DaveA From norman at khine.net Wed Dec 5 22:40:34 2012 From: norman at khine.net (Norman Khine) Date: Wed, 5 Dec 2012 22:40:34 +0100 Subject: [Tutor] correct way to encode In-Reply-To: References: Message-ID: On Wed, Dec 5, 2012 at 3:24 PM, Hugo Arts wrote: > On Wed, Dec 5, 2012 at 2:47 PM, Norman Khine wrote: >> >> hello, i have this code from the google fusion table api: >> >> (zmgc)? python >> * master 9e4be39 ?zmgc" >> Python 2.7.2 (default, Jan 28 2012, 14:53:22) >> [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin >> Type "help", "copyright", "credits" or "license" for more information. >> >>> import csv >> >>> import urllib2, urllib >> >>> request_url = 'https://www.google.com/fusiontables/api/query' >> >>> query = 'SELECT * FROM 3027809' >> >>> url = "%s?%s" % (request_url, urllib.urlencode({'sql': query})) >> >>> serv_req = urllib2.Request(url=url) >> >>> serv_resp = urllib2.urlopen(serv_req) >> >>> reader = csv.DictReader(serv_resp) >> >>> for row in reader: >> ... print row >> ... >> {'Name': 'Portugal', 'Contact': 'info at zeitgeistportugal.org', 'Link': >> 'http://www.zeitgeistportugal.org/', 'Location': 'Portugal', 'Type': >> 'Country', 'Icon': '1'} >> {'Name': 'Porto', 'Contact': 'porto at zeitgeistportugal.org', 'Link': >> 'http://porto.zeitgeistportugal.org', 'Location': 'Porto, Portugal', >> 'Type': 'Region', 'Icon': '2'} >> {'Name': 'Lisboa', 'Contact': 'lisboa at zeitgeistportugal.org', 'Link': >> 'http://lisboa.zeitgeistportugal.org', 'Location': 'Lisbon, Portugal', >> 'Type': 'Region', 'Icon': '2'} >> {'Name': >> '\xd0\x91\xd1\x8a\xd0\xbb\xd0\xb3\xd0\xb0\xd1\x80\xd0\xb8\xd1\x8f', >> 'Contact': 'zgeistbg at gmail.com', 'Link': >> 'http://thezeitgeistmovement.bg/', 'Location': 'Bulgaria', 'Type': >> 'Country', 'Icon': '1'} >> >> >> the table has a mix of charecters: >> >> https://www.google.com/fusiontables/DataSource?docid=1epTUiUlv5NQK5x4sgdy1K47ACDTpHH60hbng1qw >> >> what will be the correct way to encode the items in each dictionary row? >> > > The data you're getting back is almost certainly encoded in UTF-8. Googling > around, the csv reader doesn't seem to work well at all when unicode is > involved, but there are some people around trying to make it work. This > stackoverflow thread might be helpful: > > http://stackoverflow.com/questions/1846135/python-csv-library-with-unicode-utf-8-support-that-just-works > thanks, in fact i did not have to do anything special to write the file to a csv, all the fields are correctly encoded as you say: import csv import urllib2, urllib request_url = 'https://www.google.com/fusiontables/api/query' query = 'SELECT * FROM 3027809' url = "%s?%s" % (request_url, urllib.urlencode({'sql': query})) serv_req = urllib2.Request(url=url) serv_resp = urllib2.urlopen(serv_req) reader = csv.DictReader(serv_resp) c = csv.writer(open("entries.csv", "wb"), delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL) for row in reader: c.writerow(row.values()) > HTH, > Hugo > -- %>>> "".join( [ {'*':'@','^':'.'}.get(c,None) or chr(97+(ord(c)-83)%26) for c in ",adym,*)&uzq^zqf" ] ) From alan.gauld at btinternet.com Wed Dec 5 23:01:57 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 05 Dec 2012 22:01:57 +0000 Subject: [Tutor] Tutor Digest, Vol 106, Issue 5 In-Reply-To: <50BFBD19.4000206@davea.name> References: <1354542935.11347.YahooMailNeo@web190604.mail.sg3.yahoo.com> <50BCC443.4000402@pearwood.info> <20121204033144.GA15150@ando> <5B80DD153D7D744689F57F4FB69AF47418050D74@SCACMX008.exchad.jpmchase.net> <50BFBD19.4000206@davea.name> Message-ID: On 05/12/12 21:31, Dave Angel wrote: >> That way I can read up from the bottom and get all the others in one >> go. I then just delete the rest unread... Thanks to that trick I was > > For any email exchange that involves more than two people, you can > easily lose content that way. Less common, if someone replies twice to > the same message, they won't be captured in the default "final mail". True, but I've found that by the time I get round to reading them the number of cases where the missed message is significant is very low, and the time it saves is worth the risk! :-) And it is the only advantage I've found to top posting! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eowens0124 at gmx.com Thu Dec 6 01:13:22 2012 From: eowens0124 at gmx.com (Ed Owens) Date: Wed, 05 Dec 2012 19:13:22 -0500 Subject: [Tutor] Regular expressions question Message-ID: <50BFE322.9010308@gmx.com> >>> str(string) '[
\n
Updated: Dec 5, 2012, 5:08pm EST
\n
]' >>> m = re.search('":\b(\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', str(string)) >>> print m None >>> I'm sort of embarrassed to ask this, but I've been staring at this regular expression for hours and can't see why it doesn't work. I'm trying to recover the date string from the string, with no success. Thanks in advance for the help. Ed From swiftone at swiftone.org Thu Dec 6 01:24:20 2012 From: swiftone at swiftone.org (Brett Ritter) Date: Wed, 5 Dec 2012 16:24:20 -0800 Subject: [Tutor] Regular expressions question In-Reply-To: <50BFE322.9010308@gmx.com> References: <50BFE322.9010308@gmx.com> Message-ID: On Wed, Dec 5, 2012 at 4:13 PM, Ed Owens wrote: > >>> str(string) > '[
\n
Updated: Dec 5, 2012, 5:08pm EST
\n
]' > >>> m = re.search('":\b(\w+\s+\d+,\s+\**d+,\s+\d+:\d+.m\s+\w+)<', > str(string)) > >>> print m > None > It starts with ": which doesn't appear in your string. -- Brett Ritter / SwiftOne swiftone at swiftone.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Thu Dec 6 01:50:41 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 6 Dec 2012 00:50:41 +0000 Subject: [Tutor] Unexpected results with obj.method().method() In-Reply-To: References: Message-ID: On 5 December 2012 18:11, C M Caine wrote: > Dear all, > > I've written a class State that subclasses tuple. The class has a method > move_state that takes a move and returns a new state object representing the > new state of the game. > > I would expect S1 and S3 to be equal on the last line here, but they are > not. > >>>> import game >>>> S = game.State() >>>> S1 = S.move_state(1).move_state("SWAP") >>>> S2 = S.move_state(1) >>>> S3 = S2.move_state("SWAP") >>>> S1 == S3 > False > > Printing the two states shows that they have very different internal states. > >>>> print S1 > (8, 8, 8, 8, 8, 8, 0) > 1 0 > (7, 7, 7, 7, 7, 7, 7) >>>> print S3 > (7, 7, 7, 7, 7, 7, 7) > 0 1 > (0, 8, 8, 8, 8, 8, 8) >From your description above I have very little idea what you're trying to do. You have specified what you were expecting to happen why you're not happy with what actually happened, which is good. I still don't understand the problem, though. What is the *relevant* code that didn't do what you expected? > If anyone is interested, State represents the state of a 7 7 Kalah board. I don't know what a Kalah board is. > The full code is on pastebin http://pastebin.com/tUh0W5Se You were right not to post this code directly in your email as it's too big. For the same reason, though, I'm not prepared to read it through and try to understand the problem. It would be better if you could trim your problem down to a short example so that you can then post the full example. An important side effect of this process is that you will often discover the cause of the problem yourself before completing your email to the list. > Are my expectations faulty? (I hope not) > Have I made some mistake in my code to get these results? Probably at least one of the above is true, but I can't say much more than that. Have a read of http://sscce.org/ for some advice about how to post problems to a mailing list. If you follow the advice there you will find that 1) You will often be able to solve your problem yourself. 2) If you do post your problem to a mailing list you will be more likely to get a helpful response. Oscar From eowens0124 at gmx.com Thu Dec 6 02:04:34 2012 From: eowens0124 at gmx.com (Ed Owens) Date: Wed, 05 Dec 2012 20:04:34 -0500 Subject: [Tutor] Regular expressions question In-Reply-To: References: <50BFE322.9010308@gmx.com> Message-ID: <50BFEF22.1050001@gmx.com> On 12/5/12 7:24 PM, Brett Ritter wrote: > On Wed, Dec 5, 2012 at 4:13 PM, Ed Owens > wrote: > > >>> str(string) > '[
\n
Updated: Dec 5, 2012, 5:08pm EST
\n
]' > >>> m = re.search('":\b(\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', > str(string)) > >>> print m > None > > > It starts with ": > which doesn't appear in your string. the : is at the end of 'Updated: ', I think. That did make me think about the " in the regular expression above, which I didn't remember. Just for completeness, here it is again, as copied: >>> m = re.search(':\b(\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', str(string)) >>> print m None >>> Ed -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 6 02:10:51 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 06 Dec 2012 01:10:51 +0000 Subject: [Tutor] Regular expressions question In-Reply-To: <50BFE322.9010308@gmx.com> References: <50BFE322.9010308@gmx.com> Message-ID: On 06/12/12 00:13, Ed Owens wrote: > >>> str(string) > '[
\n
Updated: Dec 5, 2012, 5:08pm EST
\n
]' > >>> m = re.search('":\b(\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', > str(string)) > >>> print m > None > >>> > > I'm sort of embarrassed to ask this, but I've been staring at this > regular expression for hours and can't see why it doesn't work. When using regex I always try the simplest things first. Now, I'm not sure how much of the time element you want but if its just the 'Dec 5, 2012, 5:08pm EST' bit You can do it thusly: >>> s = '[
\n
Updated: Dec 5, 2012, 5:08pm EST
\n
]' >>> m = re.search('Updated:(.+)<', s) >>> m <_sre.SRE_Match object at 0x7f48d412d5d0> >>> m.groups() (' Dec 5, 2012, 5:08pm EST',) >>> m.groups()[0].strip() 'Dec 5, 2012, 5:08pm EST' Now, that might be too simplistic for some of your other scenarios, but I'd be inclined to start small and work out rather than trying to be too generic too soon. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Dec 6 02:11:57 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 06 Dec 2012 01:11:57 +0000 Subject: [Tutor] Regular expressions question In-Reply-To: References: <50BFE322.9010308@gmx.com> Message-ID: On 06/12/12 00:24, Brett Ritter wrote: > '[
\n
Updated: Dec 5, 2012, 5:08pm EST
\n
]' > >>> m = re.search('":\b(\w+\s+\d+,\s+\__d+,\s+\d+:\d+.m\s+\w+)<', > > It starts with ": > which doesn't appear in your string. At the end of Updated: -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From swiftone at swiftone.org Thu Dec 6 02:30:23 2012 From: swiftone at swiftone.org (Brett Ritter) Date: Wed, 5 Dec 2012 17:30:23 -0800 Subject: [Tutor] Regular expressions question In-Reply-To: <50BFEF22.1050001@gmx.com> References: <50BFE322.9010308@gmx.com> <50BFEF22.1050001@gmx.com> Message-ID: On Wed, Dec 5, 2012 at 5:04 PM, Ed Owens wrote: > >>> m = re.search(':\b(\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', str(string)) > >>> print m > None > >>> > Okay, without the double-quote (it wasn't the colon that I was worried about, it was the double-quote), I believe the issue now is that you have a space in the string (after the colon), but \b is a zero-width match. -- Brett Ritter / SwiftOne swiftone at swiftone.org -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Thu Dec 6 02:31:20 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 5 Dec 2012 20:31:20 -0500 Subject: [Tutor] Unexpected results with obj.method().method() In-Reply-To: References: Message-ID: On Wed, Dec 5, 2012 at 1:11 PM, C M Caine wrote: > > The full code is on pastebin http://pastebin.com/tUh0W5Se > >>>> import game >>>> S = game.State() >>>> S1 = S.move_state(1).move_state("SWAP") >>>> S2 = S.move_state(1) >>>> S3 = S2.move_state("SWAP") >>>> S1 == S3 > False In lines 156-160 you change players by mutating the object. You need to use a local variable such as "next_player" and return State(board, self.turn_number + 1, next_player). Also, you're mixing tabs and 2-space indents. Please choose one or the other. The most popular style is 4-space indents, as recommended by the PEP 8 style guide. From eryksun at gmail.com Thu Dec 6 03:08:51 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 5 Dec 2012 21:08:51 -0500 Subject: [Tutor] Regular expressions question In-Reply-To: <50BFE322.9010308@gmx.com> References: <50BFE322.9010308@gmx.com> Message-ID: On Wed, Dec 5, 2012 at 7:13 PM, Ed Owens wrote: >>>> str(string) > '[
\n
Updated: > Dec 5, 2012, 5:08pm EST
\n
]' >>>> m = re.search('":\b(\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', str(string)) >>>> print m > None You need a raw string for the boundary marker \b (i.e the boundary between \w and \W), else it creates a backspace control character. Also, I don't see why you have ": at the start of the expression. This works: >>> s = 'Updated: Dec 5, 2012, 5:08pm EST' >>> m = re.search(r'\b(\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', s) >>> m.group(1) 'Dec 5, 2012, 5:08pm EST' But wouldn't it be simpler and more reliable to use an HTML parser? From malcolm.newsome at gmail.com Thu Dec 6 04:06:16 2012 From: malcolm.newsome at gmail.com (Malcolm Newsome) Date: Wed, 5 Dec 2012 21:06:16 -0600 Subject: [Tutor] Get/Set/Attribute Accessors in Python? Message-ID: Thanks gentlemen! Your answers were very thorough and helpful. I'll play with this some. Malcolm Newsome -------------- next part -------------- An HTML attachment was scrubbed... URL: From breamoreboy at yahoo.co.uk Thu Dec 6 05:42:29 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 06 Dec 2012 04:42:29 +0000 Subject: [Tutor] Get/Set/Attribute Accessors in Python? In-Reply-To: <50BF7D1B.2030303@pearwood.info> References: <50BF7D1B.2030303@pearwood.info> Message-ID: On 05/12/2012 16:58, Steven D'Aprano wrote: > > And here is how it should be written in Python: > > class Example(object): > def __init__(self, value): > self.x = value > > > instance = Example(42) > print instance.x # gives 42 > instance.setx(23) > > And introduce error handling while we're at it? Should we consider a CutAndPasteError in Python? :) c:\Users\Mark\MyPython>type mytest.py class Example(object): def __init__(self, value): self.x = value instance = Example(42) print(instance.x) # gives 42 instance.setx(23) c:\Users\Mark\MyPython>mytest.py 42 Traceback (most recent call last): File "C:\Users\Mark\MyPython\mytest.py", line 8, in instance.setx(23) AttributeError: 'Example' object has no attribute 'setx' -- Cheers. Mark Lawrence. From eryksun at gmail.com Thu Dec 6 07:21:03 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 6 Dec 2012 01:21:03 -0500 Subject: [Tutor] Get/Set/Attribute Accessors in Python? In-Reply-To: References: Message-ID: On Wed, Dec 5, 2012 at 10:38 AM, Malcolm Newsome wrote: > > One thing I've come across in C# (and, quite frankly, am having a difficult > time grasping) is Get and Set (Accessors). > > Since, I don't ever recall reading about this in Python, I'm wondering if > they exist. Python uses what it calls "descriptors" to create bound methods and handle special properties such as __dict__, __weakref__, and slots defined by __slots__. You can roll your own as you see fit. Raymond Hettinger's guide is a good place to start: http://docs.python.org/3/howto/descriptor.html From fomcl at yahoo.com Thu Dec 6 10:53:08 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 6 Dec 2012 01:53:08 -0800 (PST) Subject: [Tutor] Regular expressions question In-Reply-To: References: <50BFE322.9010308@gmx.com> Message-ID: <1354787588.54428.YahooMailNeo@web163805.mail.gq1.yahoo.com> _______________________________ >From: eryksun >To: Ed Owens >Cc: "tutor at python.org" >Sent: Thursday, December 6, 2012 3:08 AM >Subject: Re: [Tutor] Regular expressions question > >On Wed, Dec 5, 2012 at 7:13 PM, Ed Owens wrote: >>>>> str(string) >> '[
\n
Updated: >> Dec 5, 2012, 5:08pm EST
\n
]' >>>>> m = re.search('":\b(\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', str(string)) >>>>> print m >> None > >You need a raw string for the boundary marker \b (i.e the boundary >between \w and \W), else it creates a backspace control character. >Also, I don't see why you have ": at the start of the expression. This >works: > >? ? >>> s = 'Updated: Dec 5, 2012, 5:08pm EST' >? ? >>> m = re.search(r'\b(\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', s) >? ? >>> m.group(1) >? ? 'Dec 5, 2012, 5:08pm EST' Lately I started using named groups (after I didn't understand some of my own regexes I wrote several months earlier). The downside is that the regexes easily get quite long, but one could use the re.VERBOSE flag to make it more readable. m = re.search(r'\b(?P\w+\s+\d+,\s+\d+,\s+\d+:\d+.m\s+\w+)<', s) >>> m.group("date") 'Dec 5, 2012, 5:08pm EST' From steve at pearwood.info Thu Dec 6 12:31:08 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 06 Dec 2012 22:31:08 +1100 Subject: [Tutor] Regular expressions question In-Reply-To: <1354787588.54428.YahooMailNeo@web163805.mail.gq1.yahoo.com> References: <50BFE322.9010308@gmx.com> <1354787588.54428.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: <50C081FC.8050302@pearwood.info> On 06/12/12 20:53, Albert-Jan Roskam wrote: > Lately I started using named groups (after I didn't understand > some of my own regexes I wrote several months earlier). Months? I can't understand my own regexes about six seconds after I look away from the screen. > The downside is that the regexes easily get quite long, but one >could use the re.VERBOSE flag to make it more readable. +1 -- Steven From steve at pearwood.info Thu Dec 6 12:54:24 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 06 Dec 2012 22:54:24 +1100 Subject: [Tutor] Get/Set/Attribute Accessors in Python? In-Reply-To: References: <50BF7D1B.2030303@pearwood.info> Message-ID: <50C08770.7090101@pearwood.info> On 06/12/12 15:42, Mark Lawrence wrote: > On 05/12/2012 16:58, Steven D'Aprano wrote: >> >> And here is how it should be written in Python: >> >> class Example(object): >> def __init__(self, value): >> self.x = value >> >> >> instance = Example(42) >> print instance.x # gives 42 >> instance.setx(23) >> >> > > And introduce error handling while we're at it? Should we consider a CutAndPasteError in Python? :) Oh the shame! I am disgraced! /me hangs head in shame Thanks for catching the error. -- Steven From breamoreboy at yahoo.co.uk Thu Dec 6 16:37:09 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 06 Dec 2012 15:37:09 +0000 Subject: [Tutor] Regular expressions question In-Reply-To: <50C081FC.8050302@pearwood.info> References: <50BFE322.9010308@gmx.com> <1354787588.54428.YahooMailNeo@web163805.mail.gq1.yahoo.com> <50C081FC.8050302@pearwood.info> Message-ID: On 06/12/2012 11:31, Steven D'Aprano wrote: > On 06/12/12 20:53, Albert-Jan Roskam wrote: > >> Lately I started using named groups (after I didn't understand >> some of my own regexes I wrote several months earlier). > > Months? I can't understand my own regexes about six seconds > after I look away from the screen. +1 YOTW > >> The downside is that the regexes easily get quite long, but one >> could use the re.VERBOSE flag to make it more readable. > > +1 -- Cheers. Mark Lawrence. From breamoreboy at yahoo.co.uk Thu Dec 6 16:56:31 2012 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 06 Dec 2012 15:56:31 +0000 Subject: [Tutor] Get/Set/Attribute Accessors in Python? In-Reply-To: <50C08770.7090101@pearwood.info> References: <50BF7D1B.2030303@pearwood.info> <50C08770.7090101@pearwood.info> Message-ID: On 06/12/2012 11:54, Steven D'Aprano wrote: > On 06/12/12 15:42, Mark Lawrence wrote: >> On 05/12/2012 16:58, Steven D'Aprano wrote: >>> >>> And here is how it should be written in Python: >>> >>> class Example(object): >>> def __init__(self, value): >>> self.x = value >>> >>> instance = Example(42) >>> print instance.x # gives 42 >>> instance.setx(23) >>> >> >> And introduce error handling while we're at it? Should we consider a >> CutAndPasteError in Python? :) > > Oh the shame! I am disgraced! > > /me hangs head in shame > > Thanks for catching the error. > I can't say I'll lose too much sleep over it. IIRC 30 years ago an emergency release was done, all signed off by the appropriate people, and the integration team couldn't load the software onto the system. Why? It handn't compiled, someone had taken out one line too many with the editor!!! Now we're giving advice in relatively real time, and sometimes we get it wrong. Thankfully not so costly to the UK taxpayer. -- Cheers. Mark Lawrence. From fomcl at yahoo.com Thu Dec 6 17:04:40 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 6 Dec 2012 08:04:40 -0800 (PST) Subject: [Tutor] how can I use unicode in ctypes? Message-ID: <1354809880.98718.YahooMailNeo@web163803.mail.gq1.yahoo.com> Hi, ? I am using ctypes to get and set data, among which, sometimes, unicode data. I was looking for a clean way? to encode and decode basestrings. The code below illustrates the problem. ? import ctypes s = u'\u0627\u0644\u0633\u0644\u0627\u0645 \u0639\u0644\u064a\u0643\u0645' good = ctypes.c_char_p(s.encode("utf-8")) bad = ctypes.c_char_p(s) print good, bad # prints: c_char_p('\xd8\xa7\xd9\x84\xd8\xb3\xd9\x84\xd8\xa7\xd9\x85 \xd8\xb9\xd9\x84\xd9\x8a\xd9\x83\xd9\x85') c_char_p('?????? ?????') I find it ugly to encode and decode the strings everywhere in my code. Moreover, the strings are usually contained in dictionaries, which would make it even uglier/ more cluttered. So I wrote a @transcode decorator: http://pastecode.org/index.php/view/29608996?... only to discover that brrrrrr, this is so complicated! (it works though). Is there a simpler solution? Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? From fomcl at yahoo.com Thu Dec 6 20:39:46 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 6 Dec 2012 11:39:46 -0800 (PST) Subject: [Tutor] how can I use unicode in ctypes? In-Reply-To: <1354809880.98718.YahooMailNeo@web163803.mail.gq1.yahoo.com> References: <1354809880.98718.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: <1354822786.83350.YahooMailNeo@web163802.mail.gq1.yahoo.com> > Hi, >? > I am using ctypes to get and set data, among which, sometimes, unicode data. I > was looking for a clean way? to encode and decode basestrings. > The code below illustrates the problem. >? > import ctypes > > s = u'\u0627\u0644\u0633\u0644\u0627\u0645 > \u0639\u0644\u064a\u0643\u0645' > good = ctypes.c_char_p(s.encode("utf-8")) > bad = ctypes.c_char_p(s) > print good, bad > # prints: > c_char_p('\xd8\xa7\xd9\x84\xd8\xb3\xd9\x84\xd8\xa7\xd9\x85 > \xd8\xb9\xd9\x84\xd9\x8a\xd9\x83\xd9\x85') > c_char_p('?????? ?????') > > I find it ugly to encode and decode the strings everywhere in my code. Moreover, > the strings are usually contained in dictionaries, which would make it even > uglier/ more cluttered. > So I wrote a @transcode decorator: > http://pastecode.org/index.php/view/29608996 ... only to discover that brrrrrr, > this is so complicated! (it works though). > Is there a simpler solution? Hmmm, I just simply used c_wchar_p, instead of c_char_p. And that seems to work. I thought the C prototype "const char *s" corresponds with c_char_p only (c_wchar_p corresponds to wchar_t * (NUL terminated) http://docs.python.org/2/library/ctypes.html). Weird. "Well, at least I learnt something from that juicy decorator code I wrote: http://pastecode.org/index.php/view/29608996 ;-) import ctypes s = u'\u0627\u0644\u0633\u0644\u0627\u0645' v = ctypes.c_wchar_p(s) print v? # prints c_wchar_p(u'\u0627\u0644\u0633\u0644\u0627\u0645') v.value? # prints u'\u0627\u0644\u0633\u0644\u0627\u0645' From fomcl at yahoo.com Fri Dec 7 09:02:41 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 7 Dec 2012 00:02:41 -0800 (PST) Subject: [Tutor] OT: best comments in source code Message-ID: <1354867361.63052.YahooMailNeo@web163802.mail.gq1.yahoo.com> Somebody on another mailing list posted this and I thought I'd share this with you -- Merry Christmas! http://stackoverflow.com/questions/184618/what-is-the-best-comment-in-source-code-you-have-ever-encountered?page=1&tab=votes#tab-top // // Dear maintainer: // // Once you are done trying to 'optimize' this routine, // and have realized what a terrible mistake that was, // please increment the following counter as a warning // to the next guy: // // total_hours_wasted_here = 42 // Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? From malcolm.newsome at gmail.com Fri Dec 7 14:19:45 2012 From: malcolm.newsome at gmail.com (Malcolm Newsome) Date: Fri, 7 Dec 2012 07:19:45 -0600 Subject: [Tutor] Get/Set/Attribute Accessors in Python? Message-ID: On 06/12/2012 11:54, Steven D'Aprano wrote: > On 06/12/12 15:42, Mark Lawrence wrote: >> On 05/12/2012 16:58, Steven D'Aprano wrote: >>> >>> And here is how it should be written in Python: >>> >>> class Example(object): >>> def __init__(self, value): >>> self.x = value >>> >>> instance = Example(42) >>> print instance.x # gives 42 >>> instance.setx(23) >>> >> >> And introduce error handling while we're at it? Should we consider a >> CutAndPasteError in Python? :) > > Oh the shame! I am disgraced! > > /me hangs head in shame > > Thanks for catching the error. > >I can't say I'll lose too much sleep over it. IIRC 30 years ago an >emergency release was done, all signed off by the appropriate >people, >and the integration team couldn't load the software onto the system. >Why? It handn't compiled, someone had taken out one line too many >with >the editor!!! Now we're giving advice in relatively real time, and >sometimes we get it wrong. Thankfully not so costly to the UK >taxpayer. > >-- >Cheers. > >Mark Lawrence. It's akin to Michael Jordan missing a three-pointer at the buzzer to lose a game...it can happen to the best of us. :-) Best, Malcolm Newsome -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Fri Dec 7 19:39:27 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 7 Dec 2012 13:39:27 -0500 Subject: [Tutor] how can I use unicode in ctypes? In-Reply-To: <1354822786.83350.YahooMailNeo@web163802.mail.gq1.yahoo.com> References: <1354809880.98718.YahooMailNeo@web163803.mail.gq1.yahoo.com> <1354822786.83350.YahooMailNeo@web163802.mail.gq1.yahoo.com> Message-ID: On Thu, Dec 6, 2012 at 2:39 PM, Albert-Jan Roskam wrote: > > http://pastecode.org/index.php/view/29608996 > > import ctypes > s = u'\u0627\u0644\u0633\u0644\u0627\u0645' > v = ctypes.c_wchar_p(s) > print v # prints c_wchar_p(u'\u0627\u0644\u0633\u0644\u0627\u0645') > v.value # prints u'\u0627\u0644\u0633\u0644\u0627\u0645' Your decorator could end up encoding str or decoding unicode. Typically this gets routed through the default encoding (i.e. ASCII) and probably triggers a UnicodeDecodeError or UnicodeEncodeError. I'd limit encoding to unicode and decoding to bytes/str. On the subject of wchar_t, here's a funny rant: http://losingfight.com/blog/2006/07/28/wchar_t-unsafe-at-any-size/ The base type for Unicode in CPython isn't wchar_t on all platforms/builds. It depends on Py_UNICODE_SIZE (2 or 4 bytes) vs sizeof(wchar_t) (also on whether wchar_t is unsigned, but that's not relevant here). 3.3 is in its own flexible universe. I recently came across a bug in create_unicode_buffer on Windows Python 3.3. The new flexible string implementation uses Py_UCS4 instead of creating surrogate pairs on Windows. However, given that the size of c_wchar is 2 [bytes] on Windows, create_unicode_buffer still needs to factor in the surrogate pairs by calculating the target size = len(init) + sum(ord(c) > 0xffff for c in init) + 1. Naively it uses size = len(init) + 1, which fails if the string has multiple non-BMP characters. Here's another ctypes related issue. On a narrow build prior to 3.2, PyUnicode_AsWideChar returns a wide-character string that may contain surrogate pairs even if wchar_t is 32-bit. That isn't well-formed UTF-32. This was fixed in 3.2 as part of fixing a ctypes bug. ctypes u_set (type 'u' is c_wchar) was modified to use an updated PyUnicode_AsWideChar and Z_set (type 'Z' is c_wchar_p) was modified to use the new PyUnicode_AsWideCharString. 3.2.3 links: u_set: http://hg.python.org/cpython/file/3d0686d90f55/Modules/_ctypes/cfield.c#l1202 Z_set: http://hg.python.org/cpython/file/3d0686d90f55/Modules/_ctypes/cfield.c#l1401 The new PyUnicode_AsWideChar and PyUnicode_AsWideCharString call the helper function unicode_aswidechar. This was added in 3.2 to handle the different cases of Py_UNICODE_SIZE more carefully: http://hg.python.org/cpython/file/3d0686d90f55/Objects/unicodeobject.c#l1187 Py_UNICODE_SIZE == SIZEOF_WCHAR_T Py_UNICODE_SIZE == 2 && SIZEOF_WCHAR_T == 4 Py_UNICODE_SIZE == 4 && SIZEOF_WCHAR_T == 2 The 2nd case takes advantage of the larger wchar_t to recombine surrogate pairs. The 3rd case creates surrogate pairs instead of truncating the character code. (Note: this helper was updated in 3.3 to use the new function PyUnicode_AsUnicodeAndSize.) Prior to 3.2, PyUnicode_AsWideChar wasn't nearly as careful. See the version in 3.1.5: http://hg.python.org/cpython/file/7395330e495e/Objects/unicodeobject.c#l1085 From steve at pearwood.info Sat Dec 8 08:35:21 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 08 Dec 2012 18:35:21 +1100 Subject: [Tutor] OT: best comments in source code In-Reply-To: <1354867361.63052.YahooMailNeo@web163802.mail.gq1.yahoo.com> References: <1354867361.63052.YahooMailNeo@web163802.mail.gq1.yahoo.com> Message-ID: <50C2EDB9.2080708@pearwood.info> On 07/12/12 19:02, Albert-Jan Roskam wrote: > > > Somebody on another mailing list posted this and I thought I'd share this with you -- Merry Christmas! > > > http://stackoverflow.com/questions/184618/what-is-the-best-comment-in-source-code-you-have-ever-encountered?page=1&tab=votes#tab-top > > > // > // Dear maintainer: > // > // Once you are done trying to 'optimize' this routine, > // and have realized what a terrible mistake that was, > // please increment the following counter as a warning > // to the next guy: > // > // total_hours_wasted_here = 42 > // +1 QOTW! So true. Unfortunately, many programers follow the strategy "optimize by blind faith" -- they write code which they think will be fast, without ever measuring whether or not it is fast. That's because optimization is hard. But imagining you have optimized is easy. -- Steven From xchimeras at gmail.com Sat Dec 8 20:39:49 2012 From: xchimeras at gmail.com (Mike) Date: Sat, 8 Dec 2012 14:39:49 -0500 Subject: [Tutor] Better way to insert items into a list Message-ID: Hello everyone, I was wondering if someone could show me a better way to achieve what I am trying to do. Here is my test code: d=[] c="00" a="A,B,C,D" b=a.split(',') for item in b: d.append(item) d.append(c) print tuple(d) Basically what I want to end up with is a tuple that looks like this: ("A","00","B","00","C","00")... As you can see I want to insert 00 in-between each element in the list. I believe this could be done using a list comprehension? I realize I achieved my goal, but I was wondering what other alternates could be done to achieve the same results. Thanks in advance for your assistance. Mike. From bgailer at gmail.com Sat Dec 8 22:18:08 2012 From: bgailer at gmail.com (bob gailer) Date: Sat, 08 Dec 2012 16:18:08 -0500 Subject: [Tutor] Better way to insert items into a list In-Reply-To: References: Message-ID: <50C3AE90.90109@gmail.com> On 12/8/2012 2:39 PM, Mike wrote: > Hello everyone, > > I was wondering if someone could show me a better way to achieve what > I am trying to do. Here is my test code: > > d=[] > c="00" > a="A,B,C,D" > b=a.split(',') > for item in b: > d.append(item) > d.append(c) > print tuple(d) > > Basically what I want to end up with is a tuple that looks like this: > ("A","00","B","00","C","00")... > > As you can see I want to insert 00 in-between each element in the > list. I believe this could be done using a list comprehension? > > I realize I achieved my goal, but I was wondering what other > alternates could be done to achieve the same results. a = 'ABCD' c = 'OO' tuple(sum([list(x) for x in zip(a,[c]*len(a))],[])) This assumes that a can be just letters. Otherwise add the split('.') -- Bob Gailer 919-636-4239 Chapel Hill NC From xchimeras at gmail.com Sun Dec 9 00:45:56 2012 From: xchimeras at gmail.com (Mike) Date: Sat, 8 Dec 2012 18:45:56 -0500 Subject: [Tutor] Better way to insert items into a list In-Reply-To: <50C3AE90.90109@gmail.com> References: <50C3AE90.90109@gmail.com> Message-ID: Thank you. Works well. Mike On Sat, Dec 8, 2012 at 4:18 PM, bob gailer wrote: > tuple(sum([list(x) for x in zip(a,[c]*len(a))],[])) From steve at pearwood.info Sun Dec 9 01:18:37 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 09 Dec 2012 11:18:37 +1100 Subject: [Tutor] Better way to insert items into a list In-Reply-To: References: Message-ID: <50C3D8DD.7050807@pearwood.info> On 09/12/12 06:39, Mike wrote: > Hello everyone, > > I was wondering if someone could show me a better way to achieve what > I am trying to do. Here is my test code: > > d=[] > c="00" > a="A,B,C,D" > b=a.split(',') > for item in b: > d.append(item) > d.append(c) > print tuple(d) > > Basically what I want to end up with is a tuple that looks like this: > ("A","00","B","00","C","00")... I'm not so sure about a "better way" -- the way you have works, it is pretty simple and obvious. I might argue about the one-letter names. > As you can see I want to insert 00 in-between each element in the > list. I believe this could be done using a list comprehension? Not really. List comprehensions don't solve every list problem. > I realize I achieved my goal, but I was wondering what other > alternates could be done to achieve the same results. Here are a couple of alternatives: result = [] insert = "00" items = "A,B,C,D".split(',') for a, b in zip(items, [insert]*len(items)): result.append(a) result.append(b) print tuple(result) # === cut === from itertools import repeat result = [] insert = repeat("00") items = "A,B,C,D".split(',') for a, b in zip(items, insert): result.append(a) result.append(b) print tuple(result) Here is a more heavyweight solution to interleave any number of sequences. I make no promise that this is the most efficient way to write this: from itertools import izip_longest def interleave(*args): """Interleave sequences into one list. With equal-length sequences, the items are taken alternatively from one to the other: >>> interleave([1, 2, 3, 4, 5], [0, 0, 0, 0, 0]) [1, 0, 2, 0, 3, 0, 4, 0, 5, 0] More than two sequences can be interleaved, of different types: >>> interleave([1, 2, 3], (0, 0, 0), iter('abc')) [1, 0, 'a', 2, 0, 'b', 3, 0, 'c'] If the sequences are of different lengths, they alternate as long as possible, then the items from the longer sequence are included: >>> interleave([1, 2, 3, 4, 5], [0, 0, 0]) [1, 0, 2, 0, 3, 0, 4, 5] >>> interleave([1, 2, 3], [0, 0, 0, 0, 0]) [1, 0, 2, 0, 3, 0, 0, 0] """ sentinel = object() result = [] for t in izip_longest(*args, fillvalue=sentinel): result.extend([x for x in t if x is not sentinel]) return result items = "A,B,C,D".split(',') print interleave(items, ["00"]*len(items)) Another name for interleaving is muxing, from "multiplexing" in signal processing: from itertools import izip_longest def longmux(*iterables, **kwargs): """longmux(*iterables, [fillvalue=None]) -> iterator Muxer which yields items interleaved from each iterator or sequence argument, stopping when all of them are exhausted. Missing values are replaced by optional keyword argument fillvalue, or None if not given. >>> list( longmux([1,2,3], xrange(10, 15), "ABCD") ) [1, 10, 'A', 2, 11, 'B', 3, 12, 'C', None, 13, 'D', None, 14, None] >>> list( longmux([1,2,3], xrange(10, 15), "ABCD", fillvalue=-1) ) [1, 10, 'A', 2, 11, 'B', 3, 12, 'C', -1, 13, 'D', -1, 14, -1] """ # This is much easier in Python 3. if kwargs == {}: fillvalue = None elif kwargs.keys() != ['fillvalue']: raise TypeError('bad keyword argument') else: fillvalue = kwargs['fillvalue'] for i in izip_longest(*iterables, fillvalue=fillvalue): for item in i: yield item print tuple(longmux("A,B,C,D".split(","), [], fillvalue="00")) -- Steven From steve at pearwood.info Sun Dec 9 02:53:49 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 09 Dec 2012 12:53:49 +1100 Subject: [Tutor] Better way to insert items into a list In-Reply-To: References: <50C3AE90.90109@gmail.com> Message-ID: <50C3EF2D.9050205@pearwood.info> On 09/12/12 10:45, Mike wrote: > Thank you. Works well. Actually, no, it doesn't work well. You only think it works well because you've only tested it with tiny amounts of data. > On Sat, Dec 8, 2012 at 4:18 PM, bob gailer wrote: >> tuple(sum([list(x) for x in zip(a,[c]*len(a))],[])) If you test that with large amounts of data, you will notice that this is horribly, painfully slow and inefficient. Here, I set up some sample data for testing: py> from string import ascii_letters py> input = [] py> for a in ascii_letters: ... for b in ascii_letters: ... input.append(a+b) ... py> input = input*10 py> len(input) 27040 py> print input[:5], input[-5:] ['aa', 'ab', 'ac', 'ad', 'ae'] ['ZV', 'ZW', 'ZX', 'ZY', 'ZZ'] Now I time how long it takes to interleave values using the two general purpose functions I gave earlier: py> with Timer(): ... t = tuple(interleave(input, ['00']*len(input))) ... time taken: 0.033014 seconds py> with Timer(): ... t = tuple(longmux(input, [], fillvalue='00')) ... time taken: 0.019947 seconds So on my old and not terribly fast computer, we should be able to process at least 1.3 million items per second. What does the solution using sum do? py> with Timer(): ... t = tuple(sum([list(x) for x in zip(input, ['00']*len(input))], [])) ... time taken: 4.607549 seconds That is not a mistake -- it is over two hundred times slower, processing fewer than 6000 items per second. And it actually gets slower as you use more items. So, what's going on here? The problem is that sum() adds lists, and repeatedly adding lists is slow. Suppose we wanted to add four lists, each with two items: [a, b] + [c, d] + [e, f] + [g, h] Now, you or I would simply create one new list and append each item in turn: [a, b] + [c, d] + [e, f] + [g, h] => [a, b, c, d, e, f, g, h] so we would need to visit each item once. Since there are four lists with two items each, we end up making *one* new list with *eight* items. But that's not what Python can do. Python can only inspect one pair of lists at a time. So what it ends up doing is this: [a, b] + [c, d] + [e, f] + [g, h] => [a, b, c, d] + [e, f] + [g, h] one new list with four items => [a, b, c, d, e, f] + [g, h] second new list with six items => [a, b, c, d, e, f, g, h] third new list with eight items which gives, all up, 18 items that need to be touched. That's more than double the number of items in the final list. As the number of lists gets bigger, the amount of excess work increases even more. With 100 lists being added, each of two items, the amount of work Python has to do is enormous: Ninety-eight intermediate lists are created, plus the final list which is kept, and (4+6+8+...+198+200) items = 10100 items touched in total. Except perhaps for a once-off, throw-away script, you should never use sum() for adding lists, tuples, or strings. In fact, Python prohibits you from using sum with string arguments: py> sum("aaa".split(), "") Traceback (most recent call last): File "", line 1, in TypeError: sum() can't sum strings [use ''.join(seq) instead] specifically to avoid this same sort of painfully slow code. If anyone is interested in the code for Timer() that I use above, you can find it here: http://code.activestate.com/recipes/577896 -- Steven From eryksun at gmail.com Sun Dec 9 08:43:00 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 9 Dec 2012 02:43:00 -0500 Subject: [Tutor] Better way to insert items into a list In-Reply-To: <50C3D8DD.7050807@pearwood.info> References: <50C3D8DD.7050807@pearwood.info> Message-ID: On Sat, Dec 8, 2012 at 7:18 PM, Steven D'Aprano wrote: > > from itertools import izip_longest > def longmux(*iterables, **kwargs): > # This is much easier in Python 3. For comparison, here it is in 3.x with fillvalue as a keyword-only argument: from itertools import zip_longest def longmux(*iterables, fillvalue='00'): for i in zip_longest(*iterables, fillvalue=fillvalue): for item in i: yield item I'd use zip (or itertools.izip in 2.x) and itertools repeat/chain: >>> from itertools import chain, repeat >>> a = 'ABCD' >>> c = '00' >>> tuple(chain.from_iterable(zip(a, repeat(c)))) ('A', '00', 'B', '00', 'C', '00', 'D', '00') From kbailey at howlermonkey.net Sun Dec 9 09:49:00 2012 From: kbailey at howlermonkey.net (Kirk Bailey) Date: Sun, 09 Dec 2012 03:49:00 -0500 Subject: [Tutor] how to play an mp3 file Message-ID: <50C4507C.6070906@howlermonkey.net> How to play a .mp3 audio file in python is the quest of the moment; any prior art, off the shelf solutions? -- -Shaboom. Kirk Bailey CEO, Freehold Marketing LLC http://www.OneBuckHosting.com/ Fnord! From steve at pearwood.info Sun Dec 9 10:39:39 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 09 Dec 2012 20:39:39 +1100 Subject: [Tutor] how to play an mp3 file In-Reply-To: <50C4507C.6070906@howlermonkey.net> References: <50C4507C.6070906@howlermonkey.net> Message-ID: <50C45C5B.8060503@pearwood.info> On 09/12/12 19:49, Kirk Bailey wrote: > How to play a .mp3 audio file in python is the quest of the moment; >any prior art, off the shelf solutions? DuckDuckGo is your friend. https://duckduckgo.com/?q=python+%22play+mp3%22 https://duckduckgo.com/?q=python+play+mp3 -- Steven From __peter__ at web.de Sun Dec 9 10:48:53 2012 From: __peter__ at web.de (Peter Otten) Date: Sun, 09 Dec 2012 10:48:53 +0100 Subject: [Tutor] Better way to insert items into a list References: Message-ID: Mike wrote: > Hello everyone, > > I was wondering if someone could show me a better way to achieve what > I am trying to do. Here is my test code: > > d=[] > c="00" > a="A,B,C,D" > b=a.split(',') > for item in b: > d.append(item) > d.append(c) > print tuple(d) > > Basically what I want to end up with is a tuple that looks like this: > ("A","00","B","00","C","00")... > > As you can see I want to insert 00 in-between each element in the > list. I believe this could be done using a list comprehension? > > I realize I achieved my goal, but I was wondering what other > alternates could be done to achieve the same results. > > Thanks in advance for your assistance. I think your code is straight-forward, and that's a virtue. I suggest you keep it and just factor out the loop: >>> def interleave(items, separator): ... for item in items: ... yield item ... yield separator ... >>> print list(interleave("A,B,C,D".split(","), "00")) ['A', '00', 'B', '00', 'C', '00', 'D', '00'] If the length of the sequence (len(b) in your example) is known in advance the following approach is very efficient as it moves the iteration over the list items into C code: >>> def interleave(items, separator): ... result = 2 * len(items) * [separator] ... result[::2] = items ... return result ... >>> interleave("abcde", "00") ['a', '00', 'b', '00', 'c', '00', 'd', '00', 'e', '00'] From eryksun at gmail.com Sun Dec 9 11:49:00 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 9 Dec 2012 05:49:00 -0500 Subject: [Tutor] Better way to insert items into a list In-Reply-To: References: Message-ID: On Sun, Dec 9, 2012 at 4:48 AM, Peter Otten <__peter__ at web.de> wrote: > >>>> def interleave(items, separator): > ... for item in items: > ... yield item > ... yield separator > ... +1 for this solution if a generator function is desired. It only requires basic Python syntax and performs well. I still prefer itertools.chain/repeat if it's for a one-off expression. >>>> def interleave(items, separator): > ... result = 2 * len(items) * [separator] > ... result[::2] = items > ... return result This is fast if a list will do. The question asks for a tuple, though. Including the time to build a tuple makes this approach a bit slower than the first one. From fomcl at yahoo.com Mon Dec 10 11:15:16 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Mon, 10 Dec 2012 02:15:16 -0800 (PST) Subject: [Tutor] how can I use unicode in ctypes? In-Reply-To: References: <1354809880.98718.YahooMailNeo@web163803.mail.gq1.yahoo.com> <1354822786.83350.YahooMailNeo@web163802.mail.gq1.yahoo.com> Message-ID: <1355134516.23946.YahooMailNeo@web163802.mail.gq1.yahoo.com> ----- Original Message ----- > From: eryksun > To: Albert-Jan Roskam > Cc: Python Mailing List > Sent: Friday, December 7, 2012 7:39 PM > Subject: Re: [Tutor] how can I use unicode in ctypes? > > On Thu, Dec 6, 2012 at 2:39 PM, Albert-Jan Roskam wrote: >> >> http://pastecode.org/index.php/view/29608996 >> >> import ctypes >> s = u'\u0627\u0644\u0633\u0644\u0627\u0645' >> v = ctypes.c_wchar_p(s) >> print v? # prints > c_wchar_p(u'\u0627\u0644\u0633\u0644\u0627\u0645') >> v.value? # prints > u'\u0627\u0644\u0633\u0644\u0627\u0645' > > Your decorator could end up encoding str or decoding unicode. > Typically this gets routed through the default encoding (i.e. ASCII) > and probably triggers a UnicodeDecodeError or UnicodeEncodeError. I'd > limit encoding to unicode and decoding to bytes/str. Thanks. I modified it. Kinda annoying that the default encoding is ascii, but I read that it could be changed with sys.setdefaultencoding and reload(sys) ? > On the subject of wchar_t, here's a funny rant: > > http://losingfight.com/blog/2006/07/28/wchar_t-unsafe-at-any-size/ > Interesting article. I experimented with a little in ctypes and also concluded that I couldn't use it for my purposes. Apparently this is still not an entirely mature area of Python. Very useful to know. Thank you very much for this detailed information! > The base type for Unicode in CPython isn't wchar_t on all > platforms/builds. It depends on Py_UNICODE_SIZE (2 or 4 bytes) vs > sizeof(wchar_t) (also on whether wchar_t is unsigned, but that's not > relevant here). 3.3 is in its own flexible universe. > > I recently came across a bug in create_unicode_buffer on Windows > Python 3.3. The new flexible string implementation uses Py_UCS4 > instead of creating surrogate pairs on Windows. However, given that > the size of c_wchar is 2 [bytes] on Windows, create_unicode_buffer > still needs to factor in the surrogate pairs by calculating the target > size = len(init) + sum(ord(c) > 0xffff for c in init) + 1. Naively it > uses size = len(init) + 1, which fails if the string has multiple > non-BMP characters. > > Here's another ctypes related issue. On a narrow build prior to 3.2, > PyUnicode_AsWideChar returns a wide-character string that may contain > surrogate pairs even if wchar_t is 32-bit. That isn't well-formed > UTF-32. This was fixed in 3.2 as part of fixing a ctypes bug. ctypes > u_set (type 'u' is c_wchar) was modified to use an updated > PyUnicode_AsWideChar and Z_set (type 'Z' is c_wchar_p) was modified to > use the new PyUnicode_AsWideCharString. > > 3.2.3 links: > > u_set: > http://hg.python.org/cpython/file/3d0686d90f55/Modules/_ctypes/cfield.c#l1202 > > Z_set: > http://hg.python.org/cpython/file/3d0686d90f55/Modules/_ctypes/cfield.c#l1401 > > The new PyUnicode_AsWideChar and PyUnicode_AsWideCharString call the > helper function unicode_aswidechar. This was added in 3.2 to handle > the different cases of Py_UNICODE_SIZE more carefully: > > http://hg.python.org/cpython/file/3d0686d90f55/Objects/unicodeobject.c#l1187 > > ? ? Py_UNICODE_SIZE == SIZEOF_WCHAR_T > ? ? Py_UNICODE_SIZE == 2 && SIZEOF_WCHAR_T == 4 > ? ? Py_UNICODE_SIZE == 4 && SIZEOF_WCHAR_T == 2 > > The 2nd case takes advantage of the larger wchar_t to recombine > surrogate pairs. The 3rd case creates surrogate pairs instead of > truncating the character code. (Note: this helper was updated in 3.3 > to use the new function PyUnicode_AsUnicodeAndSize.) > > Prior to 3.2, PyUnicode_AsWideChar wasn't nearly as careful. See the > version in 3.1.5: > > http://hg.python.org/cpython/file/7395330e495e/Objects/unicodeobject.c#l1085 > From fomcl at yahoo.com Mon Dec 10 11:19:16 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Mon, 10 Dec 2012 02:19:16 -0800 (PST) Subject: [Tutor] memoize, lookup, or KIS? In-Reply-To: <50AA2087.80600@pearwood.info> References: <1353322967.87760.YahooMailNeo@web110715.mail.gq1.yahoo.com> <50AA2087.80600@pearwood.info> Message-ID: <1355134756.25459.YahooMailNeo@web163804.mail.gq1.yahoo.com> (this thread may be closed already but...) while looking for something else, I found another cool way of memoizing: http://effbot.org/zone/default-values.htm It uses an empty dictionary as a function argument. Usually a source of confusion (for me at least!), but in this case I find it quite useful. Regards, Albert-Jan From eryksun at gmail.com Mon Dec 10 13:22:31 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 10 Dec 2012 07:22:31 -0500 Subject: [Tutor] how can I use unicode in ctypes? In-Reply-To: <1355134516.23946.YahooMailNeo@web163802.mail.gq1.yahoo.com> References: <1354809880.98718.YahooMailNeo@web163803.mail.gq1.yahoo.com> <1354822786.83350.YahooMailNeo@web163802.mail.gq1.yahoo.com> <1355134516.23946.YahooMailNeo@web163802.mail.gq1.yahoo.com> Message-ID: On Mon, Dec 10, 2012 at 5:15 AM, Albert-Jan Roskam wrote: > > Thanks. I modified it. Kinda annoying that the default encoding is ascii, but > I read that it could be changed with sys.setdefaultencoding and reload(sys) I wouldn't go against the grain here, especially for library code. From cmcaine at googlemail.com Mon Dec 10 15:43:38 2012 From: cmcaine at googlemail.com (C M Caine) Date: Mon, 10 Dec 2012 14:43:38 +0000 Subject: [Tutor] Unexpected results with obj.method().method() In-Reply-To: References: Message-ID: Thanks eryksun, that was the bug. Thanks for pointing out the tabs as well, they were added by vim's autoindent. I've set expandtab for python files now. I decided to change the code such that current_player and turn_number are hidden behind properties meaning I won't overwrite them accident or stupidity later. On 6 December 2012 01:31, eryksun wrote: > On Wed, Dec 5, 2012 at 1:11 PM, C M Caine wrote: > > > > The full code is on pastebin http://pastebin.com/tUh0W5Se > > > >>>> import game > >>>> S = game.State() > >>>> S1 = S.move_state(1).move_state("SWAP") > >>>> S2 = S.move_state(1) > >>>> S3 = S2.move_state("SWAP") > >>>> S1 == S3 > > False > > In lines 156-160 you change players by mutating the object. You need > to use a local variable such as "next_player" and return State(board, > self.turn_number + 1, next_player). > > Also, you're mixing tabs and 2-space indents. Please choose one or the > other. The most popular style is 4-space indents, as recommended by > the PEP 8 style guide. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cmcaine at googlemail.com Mon Dec 10 15:49:14 2012 From: cmcaine at googlemail.com (C M Caine) Date: Mon, 10 Dec 2012 14:49:14 +0000 Subject: [Tutor] Unexpected results with obj.method().method() In-Reply-To: References: Message-ID: Thanks for the advice. As is often the case with these things, eryksun pointed out a stupid mistake I'd made (mutating part of an immutable class) that I should have seen. On 6 December 2012 00:50, Oscar Benjamin wrote: > On 5 December 2012 18:11, C M Caine wrote: > > Dear all, > > > > I've written a class State that subclasses tuple. The class has a method > > move_state that takes a move and returns a new state object representing > the > > new state of the game. > > > > I would expect S1 and S3 to be equal on the last line here, but they are > > not. > > > >>>> import game > >>>> S = game.State() > >>>> S1 = S.move_state(1).move_state("SWAP") > >>>> S2 = S.move_state(1) > >>>> S3 = S2.move_state("SWAP") > >>>> S1 == S3 > > False > > > > Printing the two states shows that they have very different internal > states. > > > >>>> print S1 > > (8, 8, 8, 8, 8, 8, 0) > > 1 0 > > (7, 7, 7, 7, 7, 7, 7) > >>>> print S3 > > (7, 7, 7, 7, 7, 7, 7) > > 0 1 > > (0, 8, 8, 8, 8, 8, 8) > > From your description above I have very little idea what you're trying > to do. You have specified what you were expecting to happen why you're > not happy with what actually happened, which is good. I still don't > understand the problem, though. What is the *relevant* code that > didn't do what you expected? > > > If anyone is interested, State represents the state of a 7 7 Kalah board. > > I don't know what a Kalah board is. > > > The full code is on pastebin http://pastebin.com/tUh0W5Se > > You were right not to post this code directly in your email as it's > too big. For the same reason, though, I'm not prepared to read it > through and try to understand the problem. > > It would be better if you could trim your problem down to a short > example so that you can then post the full example. An important side > effect of this process is that you will often discover the cause of > the problem yourself before completing your email to the list. > > > Are my expectations faulty? (I hope not) > > Have I made some mistake in my code to get these results? > > Probably at least one of the above is true, but I can't say much more > than that. Have a read of http://sscce.org/ for some advice about how > to post problems to a mailing list. If you follow the advice there you > will find that > 1) You will often be able to solve your problem yourself. > 2) If you do post your problem to a mailing list you will be more > likely to get a helpful response. > > > Oscar > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ilhs_hs at yahoo.com Tue Dec 11 16:54:50 2012 From: ilhs_hs at yahoo.com (Hs Hs) Date: Tue, 11 Dec 2012 07:54:50 -0800 (PST) Subject: [Tutor] regular expression wildcard search Message-ID: <1355241290.99564.YahooMailNeo@web163501.mail.gq1.yahoo.com> Dear group: I have 50 thousand lists. My aim is to search a pattern in the alphabetical strings (these are protein sequence strings). MMSASRLAGTLIPAMAFLSCVRPESWEPC VEVVP NITYQCMELNFYKIPDNLPFSTKNLDLSFNPLRHLGSYSFFSFPELQVLDLSRCEIQTIED my aim is to find the list of string that has V*VVP.? myseq = 'MMSASRLAGTLIPAMAFLSCVRPESWEPC VEVVP NITYQCMELNFYKIPDNLPFSTKNLDLSFNPLRHLGSYSFFSFPELQVLDLSRCEIQTIED' if re.search('V*VVP',myseq): print myseq? the problem with this is, I am also finding junk with just VVP or VP etc.? How can I strictly search for V*VVP only.? Thanks for help.? Hs -------------- next part -------------- An HTML attachment was scrubbed... URL: From embirath at gmail.com Tue Dec 11 18:02:46 2012 From: embirath at gmail.com (Emma Birath) Date: Tue, 11 Dec 2012 10:02:46 -0700 Subject: [Tutor] regular expression wildcard search In-Reply-To: <1355241290.99564.YahooMailNeo@web163501.mail.gq1.yahoo.com> References: <1355241290.99564.YahooMailNeo@web163501.mail.gq1.yahoo.com> Message-ID: Hi there Do you want your "*" to represent a single letter, or what is your intent? If you want only a single letter between the "V" and "VVP", use "\w" instead of "*". re.search('v\wVVP',myseq) Emma On Tue, Dec 11, 2012 at 8:54 AM, Hs Hs wrote: > Dear group: > > I have 50 thousand lists. My aim is to search a pattern in the > alphabetical strings (these are protein sequence strings). > > > MMSASRLAGTLIPAMAFLSCVRPESWEPC VEVVP > NITYQCMELNFYKIPDNLPFSTKNLDLSFNPLRHLGSYSFFSFPELQVLDLSRCEIQTIED > > my aim is to find the list of string that has V*VVP. > > myseq = 'MMSASRLAGTLIPAMAFLSCVRPESWEPC VEVVP > NITYQCMELNFYKIPDNLPFSTKNLDLSFNPLRHLGSYSFFSFPELQVLDLSRCEIQTIED' > > if re.search('V*VVP',myseq): > print myseq > > the problem with this is, I am also finding junk with just VVP or VP etc. > > How can I strictly search for V*VVP only. > > Thanks for help. > > Hs > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Tue Dec 11 18:03:07 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 11 Dec 2012 12:03:07 -0500 Subject: [Tutor] regular expression wildcard search In-Reply-To: <1355241290.99564.YahooMailNeo@web163501.mail.gq1.yahoo.com> References: <1355241290.99564.YahooMailNeo@web163501.mail.gq1.yahoo.com> Message-ID: On Tue, Dec 11, 2012 at 10:54 AM, Hs Hs wrote: > Dear group: > Please send mail as plain text. It is easier to read > > I have 50 thousand lists. My aim is to search a pattern in the > alphabetical strings (these are protein sequence strings). > > > MMSASRLAGTLIPAMAFLSCVRPESWEPC VEVVP > NITYQCMELNFYKIPDNLPFSTKNLDLSFNPLRHLGSYSFFSFPELQVLDLSRCEIQTIED > > my aim is to find the list of string that has V*VVP. > Asterisk The "*" matches 0 or more instances of the previous element. I am not sure what you want, but I don't think it is this. Do you want V then any characters followed by VVP? In that case perhaps V.+VP There are many tutorials about how to create regular expressions ** ** > > myseq = 'MMSASRLAGTLIPAMAFLSCVRPESWEPC VEVVP > NITYQCMELNFYKIPDNLPFSTKNLDLSFNPLRHLGSYSFFSFPELQVLDLSRCEIQTIED' > > if re.search('V*VVP',myseq): > print myseq > > the problem with this is, I am also finding junk with just VVP or VP etc. > > How can I strictly search for V*VVP only. > > Thanks for help. > > Hs > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Dec 11 18:56:24 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 11 Dec 2012 17:56:24 +0000 Subject: [Tutor] regular expression wildcard search In-Reply-To: <1355241290.99564.YahooMailNeo@web163501.mail.gq1.yahoo.com> References: <1355241290.99564.YahooMailNeo@web163501.mail.gq1.yahoo.com> Message-ID: On 11/12/12 15:54, Hs Hs wrote: > myseq = 'MMSASRLAGTLIPAMAFLSCVRPESWEPC VEVVP > NITYQCMELNFYKIPDNLPFSTKNLDLSFNPLRHLGSYSFFSFPELQVLDLSRCEIQTIED' > > if re.search('V*VVP',myseq): > print myseq I hope this is just a typo but you are printing your original string not the things found... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From lconrad at go2france.com Wed Dec 12 15:34:25 2012 From: lconrad at go2france.com (lconrad at go2france.com) Date: Wed, 12 Dec 2012 08:34:25 -0600 Subject: [Tutor] text processing, reformatting Message-ID: <50c895f1.460.2adc7600.5bb5b855@go2france.com> From an much larger, messy report file, I extracted these lines: OPM010 HUNT INGR FRI 16/11/12 00:00:00 QRTR HTGP PEG OVFL 0012 00000 00000 0022 00000 00000 0089 00000 00000 0379 00000 00000 OPM010 HUNT INGR FRI 16/11/12 00:15:00 QRTR HTGP PEG OVFL 0012 00000 00000 0022 00000 00000 0089 00000 00000 0379 00000 00000 OPM010 HUNT INGR FRI 16/11/12 00:30:00 QRTR HTGP PEG OVFL 0012 00000 00000 0022 00000 00000 0089 00000 00000 0379 00000 00000 OPM010 HUNT INGR FRI 16/11/12 00:45:00 QRTR HTGP PEG OVFL 0012 00000 00000 0022 00000 00000 0089 00000 00000 0379 00000 00000 OPM010 HUNT INGR FRI 16/11/12 01:00:00 QRTR HTGP PEG OVFL 0012 00000 00000 0022 00000 00000 0089 00000 00000 0379 00000 00000 OPM010 HUNT INGR FRI 16/11/12 01:15:00 QRTR HTGP PEG OVFL 0012 00000 00000 0022 00000 00000 0089 00000 00000 0379 00000 00000 the engineer needs that reformatted into the "log line" the original machine should have written anyway, for importing into Excel: yyyy-mm-dd;hh:mm:ss;;;; With Bourne shell, I could eventually whack this out as I usually do, but as a Python pupil, I'd like to see how, learn from you aces and python could do it. :) thanks Len From steve at pearwood.info Wed Dec 12 16:37:55 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 13 Dec 2012 02:37:55 +1100 Subject: [Tutor] text processing, reformatting In-Reply-To: <50c895f1.460.2adc7600.5bb5b855@go2france.com> References: <50c895f1.460.2adc7600.5bb5b855@go2france.com> Message-ID: <50C8A4D3.2020606@pearwood.info> On 13/12/12 01:34, lconrad at go2france.com wrote: > > From an much larger, messy report file, I extracted these lines: > > > OPM010 HUNT INGR FRI 16/11/12 00:00:00 QRTR > HTGP PEG OVFL > 0012 00000 00000 > 0022 00000 00000 > 0089 00000 00000 > 0379 00000 00000 > OPM010 HUNT INGR FRI 16/11/12 00:15:00 QRTR > HTGP PEG OVFL > 0012 00000 00000 > 0022 00000 00000 [snip] > the engineer needs that reformatted into the "log line" the original >machine should have written anyway, for importing into Excel: > > yyyy-mm-dd;hh:mm:ss;;;; > > With Bourne shell, I could eventually whack this out as I usually do, >but as a Python pupil, I'd like to see how, learn from you aces and >python could do it. :) Well, it's not entirely clear what the relationship between the "before" and "after" text should be. It's always useful to give an actual example so as to avoid misunderstandings. In the absence of a detailed specification, I will just have to guess, and then you can complain when I guess wrongly :-) My guess is that the above report lines should be reformatted into: 2012-11-16;00:00:00;0012;00000;00000; 2012-11-16;00:00:00;0022;00000;00000; 2012-11-16;00:00:00;0089;00000;00000; 2012-11-16;00:00:00;0379;00000;00000; 2012-11-16;00:15:00;0012;00000;00000; 2012-11-16;00:15:00;0022;00000;00000; Here is a quick and dirty version, with little in the way of error checking or sophistication, suitable for a throw-away script: # === cut === date, time = "unknown", "unknown" for line in open("input.txt"): line = line.strip() # remove whitespace if not line: # skip blanks continue if line.startswith("OPM010") and line.endswith("QRTR"): # extract the date from line similar to # OPM010 HUNT INGR FRI 16/11/12 00:15:00 QRTR date, time = line.split()[4:5] # convert date from DD/MM/YY to YYYY-MM-DD dd, mm, yy = date.split("/") date = "20%s-%s-%s" % (yy, mm, dd) elif line == "HTGP PEG OVFL": continue else: # output log lines htgp, peg, ovfl = line.split() print(";".join([date, time, htgp, peg, ovfl])) # === cut === -- Steven From joel.goldstick at gmail.com Wed Dec 12 16:56:36 2012 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 12 Dec 2012 10:56:36 -0500 Subject: [Tutor] text processing, reformatting In-Reply-To: <50c895f1.460.2adc7600.5bb5b855@go2france.com> References: <50c895f1.460.2adc7600.5bb5b855@go2france.com> Message-ID: On Wed, Dec 12, 2012 at 9:34 AM, wrote: > > ... > > OPM010 HUNT INGR FRI 16/11/12 01:00:00 QRTR > HTGP PEG OVFL > 0012 00000 00000 > 0022 00000 00000 > 0089 00000 00000 > 0379 00000 00000 > OPM010 HUNT INGR FRI 16/11/12 01:15:00 QRTR > HTGP PEG OVFL > 0012 00000 00000 > 0022 00000 00000 > 0089 00000 00000 > 0379 00000 00000 > > the engineer needs that reformatted into the "log line" the original > machine should have written anyway, for importing into Excel: > > yyyy-mm-dd;hh:mm:ss;;<**peg>;; > I'm guessing that you want output like this: 2012-11-16;01:15:00;0012;00000;00000 2012-11-16;01:15:00;0022;00000;00000 2012-11-16;01:15:00;0089;00000;00000 2012-11-16;01:15:00;0379;00000;00000 > With Bourne shell, I could eventually whack this out as I usually do, but > as a Python pupil, I'd like to see how, learn from you aces and python > could do it. :) > > The way the tutor list works generally is people come with specific coding problems, show their code, and get help understanding what is wrong. You would do better here if you could show us what you have so far. That being said, I hacked out about 15 lines of code that produced the results I've shown above. My code will work with any amount of data lines if they are interspersed with your two header lines. Python makes it easy to read a file line by line. The lines contain end of line characters, but python makes it easy to get rid of those (strip) Your lines seem to have each column separated by spaces. You can easily split a line into a list You can also identify the lines using str.startswith("HTGP"), etc. > thanks > Len > > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From doark at mail.com Wed Dec 12 22:09:25 2012 From: doark at mail.com (frank ernest) Date: Wed, 12 Dec 2012 16:09:25 -0500 Subject: [Tutor] Proper useage of "open" Message-ID: <20121212210925.55720@gmx.com> I want to open a file so I read the library useage because I could not recall the propor useage. I typed in to my script: a = open (dupli, r) and got an error stating that "dupli" is not deffined. I started the script from within to same directorie that the file "dupli" was in. Perhaps it needs me to tell it to generate a list of files in the directorie? How do I get it to open the file "dupli" -------------- next part -------------- An HTML attachment was scrubbed... URL: From chigga101 at gmail.com Wed Dec 12 22:28:59 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Wed, 12 Dec 2012 21:28:59 +0000 Subject: [Tutor] Proper useage of "open" In-Reply-To: <20121212210925.55720@gmx.com> References: <20121212210925.55720@gmx.com> Message-ID: > I typed in to my script: > a = open (dupli, r) > and got an error stating that "dupli" is not deffined. it needs the quotes around it. so try: a = open ("dupli", "r") From alan.gauld at btinternet.com Wed Dec 12 22:30:09 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 12 Dec 2012 21:30:09 +0000 Subject: [Tutor] Proper useage of "open" In-Reply-To: <20121212210925.55720@gmx.com> References: <20121212210925.55720@gmx.com> Message-ID: On 12/12/12 21:09, frank ernest wrote: > I want to open a file so I read the library useage because I could not > recall the propor useage. > I typed in to my script: > a = open (dupli, r) > and got an error stating that "dupli" is not deffined. It sounds as if dupli is the name of your file however you have passed it into open without quotes so Python thinks it is a variable. Except it has no variables called dupli.... Similarly the mode (r) should also be passed as a string. In future please always post the full error text from your session, its easier than us guessing and the messages often include subtle details that you may not have noticed... HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eowens0124 at gmx.com Thu Dec 13 02:47:58 2012 From: eowens0124 at gmx.com (Ed Owens) Date: Wed, 12 Dec 2012 20:47:58 -0500 Subject: [Tutor] reading web page with BeautifulSoup In-Reply-To: <50BFE322.9010308@gmx.com> References: <50BFE322.9010308@gmx.com> Message-ID: <50C933CE.5010503@gmx.com> >>> from urllib2 import urlopen >>> page = urlopen('w1.weather.gov/obhistory/KDCA.html') Traceback (most recent call last): File "", line 1, in File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 386, in open protocol = req.get_type() File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 248, in get_type raise ValueError, "unknown url type: %s" % self.__original ValueError: unknown url type: w1.weather.gov/obhistory/KDCA.html >>> Can anyone see what I'm doing wrong here? I have bs4 and urllib2 imported, and get the above error when trying to read that page. I can copy the url from the error message into my browser and get the page. Ed From shantanoo at gmail.com Thu Dec 13 03:01:51 2012 From: shantanoo at gmail.com (=?UTF-8?B?4KS24KSC4KSk4KSo4KWC?=) Date: Thu, 13 Dec 2012 13:01:51 +1100 Subject: [Tutor] reading web page with BeautifulSoup In-Reply-To: <50C933CE.5010503@gmx.com> References: <50BFE322.9010308@gmx.com> <50C933CE.5010503@gmx.com> Message-ID: <50C9370F.50602@gmail.com> On 13/12/12 12:47 PM, Ed Owens wrote: > >>> from urllib2 import urlopen > >>> page = urlopen('w1.weather.gov/obhistory/KDCA.html') > Traceback (most recent call last): > File "", line 1, in > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", > line 126, in urlopen > return _opener.open(url, data, timeout) > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", > line 386, in open > protocol = req.get_type() > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", > line 248, in get_type > raise ValueError, "unknown url type: %s" % self.__original > ValueError: unknown url type: w1.weather.gov/obhistory/KDCA.html > >>> > > Can anyone see what I'm doing wrong here? I have bs4 and urllib2 > imported, and get the above error when trying to read that page. I > can copy the url from the error message into my browser and get the page. You may try the URL with 'http://' or 'https://' instead of 'w1.'. HTH. -- ????? From d at davea.name Thu Dec 13 03:03:53 2012 From: d at davea.name (Dave Angel) Date: Wed, 12 Dec 2012 21:03:53 -0500 Subject: [Tutor] reading web page with BeautifulSoup In-Reply-To: <50C933CE.5010503@gmx.com> References: <50BFE322.9010308@gmx.com> <50C933CE.5010503@gmx.com> Message-ID: <50C93789.9010302@davea.name> On 12/12/2012 08:47 PM, Ed Owens wrote: > >>> from urllib2 import urlopen > >>> page = urlopen('w1.weather.gov/obhistory/KDCA.html') > Traceback (most recent call last): > File "", line 1, in > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", > line 126, in urlopen > return _opener.open(url, data, timeout) > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", > line 386, in open > protocol = req.get_type() > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", > line 248, in get_type > raise ValueError, "unknown url type: %s" % self.__original > ValueError: unknown url type: w1.weather.gov/obhistory/KDCA.html > >>> > > Can anyone see what I'm doing wrong here? I have bs4 and urllib2 > imported, and get the above error when trying to read that page. I > can copy the url from the error message into my browser and get the page. Like the error says, unknown type. Prepend the type of the url, and it should work fine: page = urlopen('http://w1.weather.gov/obhistory/KDCA.html') -- DaveA From eowens0124 at gmx.com Thu Dec 13 03:11:56 2012 From: eowens0124 at gmx.com (Ed Owens) Date: Wed, 12 Dec 2012 21:11:56 -0500 Subject: [Tutor] reading web page with BeautifulSoup In-Reply-To: <50C93789.9010302@davea.name> References: <50BFE322.9010308@gmx.com> <50C933CE.5010503@gmx.com> <50C93789.9010302@davea.name> Message-ID: <50C9396C.5020705@gmx.com> On 12/12/12 9:03 PM, Dave Angel wrote: > On 12/12/2012 08:47 PM, Ed Owens wrote: >>>>> from urllib2 import urlopen >>>>> page = urlopen('w1.weather.gov/obhistory/KDCA.html') >> Traceback (most recent call last): >> File "", line 1, in >> File >> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", >> line 126, in urlopen >> return _opener.open(url, data, timeout) >> File >> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", >> line 386, in open >> protocol = req.get_type() >> File >> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", >> line 248, in get_type >> raise ValueError, "unknown url type: %s" % self.__original >> ValueError: unknown url type: w1.weather.gov/obhistory/KDCA.html >> Can anyone see what I'm doing wrong here? I have bs4 and urllib2 >> imported, and get the above error when trying to read that page. I >> can copy the url from the error message into my browser and get the page. > Like the error says, unknown type. Prepend the type of the url, and it > should work fine: > > page = urlopen('http://w1.weather.gov/obhistory/KDCA.html') > > Yep, that was it. Thanks for the help. Now on to fight with BeautifulSoup Ed From dfjennings at gmail.com Thu Dec 13 03:58:03 2012 From: dfjennings at gmail.com (Don Jennings) Date: Wed, 12 Dec 2012 21:58:03 -0500 Subject: [Tutor] reading web page with BeautifulSoup In-Reply-To: References: Message-ID: <201FAD13-3286-476B-97B4-8FD2DCD59B71@gmail.com> On Dec 12, 2012, at 8:54 PM, tutor-request at python.org wrote: > Date: Wed, 12 Dec 2012 20:47:58 -0500 > From: Ed Owens > To: tutor at python.org > Subject: [Tutor] reading web page with BeautifulSoup > Message-ID: <50C933CE.5010503 at gmx.com> > Content-Type: text/plain; charset=ISO-8859-1; format=flowed > >>>> from urllib2 import urlopen >>>> page = urlopen('w1.weather.gov/obhistory/KDCA.html') > Traceback (most recent call last): > File "", line 1, in > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", > line 126, in urlopen > return _opener.open(url, data, timeout) > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", > line 386, in open > protocol = req.get_type() > File > "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", > line 248, in get_type > raise ValueError, "unknown url type: %s" % self.__original > ValueError: unknown url type: w1.weather.gov/obhistory/KDCA.html >>>> > > Can anyone see what I'm doing wrong here? Yes, you should pass the full url, including the scheme: urlopen('http://w1.weather.gov/obhistory/KDCA.html') By the way, your subject line would be better if it had something to do with url, as the problem is completely unrelated to BeautifulSoup :>) Take care, Don -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 13 09:02:37 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 13 Dec 2012 08:02:37 +0000 Subject: [Tutor] reading web page with BeautifulSoup In-Reply-To: <50C933CE.5010503@gmx.com> References: <50BFE322.9010308@gmx.com> <50C933CE.5010503@gmx.com> Message-ID: On 13/12/12 01:47, Ed Owens wrote: > >>> from urllib2 import urlopen > >>> page = urlopen('w1.weather.gov/obhistory/KDCA.html') > Traceback (most recent call last): > ValueError: unknown url type: w1.weather.gov/obhistory/KDCA.html > copy the url from the error message into my browser and get the page. Browsers have evolved to make all sorts of intelligent guesses about what the true URL is based on what the user types in. They try pre-pending various types and pre and post fixes (for example you can usually miss out the www part or the .com part). Urlopen makes no such assumptions, you must provide the full url (with the exception of the port) including the type (ftp, mail, http etc) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From jasminegao93 at gmail.com Thu Dec 13 23:39:01 2012 From: jasminegao93 at gmail.com (Jasmine Gao) Date: Thu, 13 Dec 2012 17:39:01 -0500 Subject: [Tutor] Using json to filter out results from a dictionary Message-ID: Hello, I've just started programming in python and haven't had much experience with json. I've written this simple script that makes a request to the Bitly API, specifically the /v3/realtime/bursting_phrasesendpoint, and prints the response into terminal. What I want to do is take the response and filter out a portion of it so I only see the data I find important. So far I've used json to load the results into a dictionary, however I'm stuck in terms of how you loop through the dictionary and only print out certain keys while dumping the rest. To illustrate, this is the script: import urllib2 CLIENT_ID = "0367d81a428968a57704a295a4378521af81c91b" CLIENT_SECRET = "404862446e88f391e8c411ca1ee912506d64fffd" ACCESS_TOKEN = "53a01f38b09c0463cb9e2b35b151beb127843bf3" BITLY_ENDPOINT = " https://api-ssl.bitly.com/v3/realtime/bursting_phrases?access_token= "+ACCESS_TOKEN def getServiceResponse(): url = BITLY_ENDPOINT request = urllib2.Request(url) response = urllib2.urlopen(request) d = json.loads(response.read()) getServiceResponse() In terminal I run this command: python /file/location/file.py | tr "," "\n" which returns a long list of results like this: "data": {"selectivity": 3.0 "phrase": "justin bieber" "mean": 0.089999999999999997} {"std": 0.046334721206249076 "ghashes": [ {"visitors": 440 "ghash": "QXfZfQ"} {"visitors": 215 "ghash": "W9sHrc"} {"visitors": 92 "ghash": "XY9PPX"}] "N": 203183.0 "rate": 0.53000000000000003 "urls": [ {"visitors": 440 "aggregate_url": "http://bit.ly/QXfZfQ"} {"visitors": 215 "aggregate_url": "http://bit.ly/W9sHrc"} {"visitors": 92 "aggregate_url": "http://bit.ly/XY9PPX"}] How do I use json to filter out the "ghashes" from what is printed into terminal so that the results look like this instead?: "data": {"selectivity": 3.0 "phrase": "justin bieber" "mean": 0.089999999999999997} {"std": 0.046334721206249076 "N": 203183.0 "rate": 0.53000000000000003 "urls": [ {"visitors": 440 "aggregate_url": "http://bit.ly/QXfZfQ"} {"visitors": 215 "aggregate_url": "http://bit.ly/W9sHrc"} {"visitors": 92 "aggregate_url": "http://bit.ly/XY9PPX"}] Any help would greatly be appreciated, thank you! - Jasmine -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Dec 14 00:21:38 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 14 Dec 2012 10:21:38 +1100 Subject: [Tutor] Using json to filter out results from a dictionary In-Reply-To: References: Message-ID: <50CA6302.2070707@pearwood.info> On 14/12/12 09:39, Jasmine Gao wrote: > Hello, > > I've just started programming in python and haven't had much experience > with json. This is not really a JSON problem. JSON just handles converting data in a dict to a string and back. As you say: > I'm stuck in terms of how you loop through the dictionary and only > print out certain keys while dumping the rest. That part is simple: for key, item in mydict.items(): if key != "rubbish": print key, item # In python 3, use "print(key, item)" instead Another way: if "rubbish" in mydict: del mydict print mydict # In python 3, use "print(mydict)" instead > To illustrate, this is the script: > > import urllib2 > CLIENT_ID = "0367d81a428968a57704a295a4378521af81c91b" > CLIENT_SECRET = "4...d" # secret redacted Should you have showed us this secret? Probably not a great idea. > ACCESS_TOKEN = "53a01f38b09c0463cb9e2b35b151beb127843bf3" > BITLY_ENDPOINT = "https://api-ssl.bitly.com/v3/realtime/bursting_phrases?access_token="+ACCESS_TOKEN > > def getServiceResponse(): > url = BITLY_ENDPOINT > request = urllib2.Request(url) > response = urllib2.urlopen(request) > d = json.loads(response.read()) > > getServiceResponse() > > > In terminal I run this command: python /file/location/file.py | tr "," "\n" > which returns a long list of results like this: Your script doesn't actually print the results or do anything with them. Perhaps the part of your script that actually handles output is missing? What other important parts are missing? It makes it hard for us to help when we have to guess what you are actually doing. My guess is that you are actually running: print getServiceResponse() or something similar. Since you are already using an external tool "tr", you could simply pipe the output through grep: python /file/location/file.py | tr "," "\n" | grep -v ghash However, instead of using the external "tr" command, you might like to do this: import pprint # ... rest of your script as before d = getServiceResponse() pprint.pprint(d) That my give you acceptable results without the "tr" command. In this case, you need to filter the dict returned by getServiceResponse. I suggest something like this may be acceptable: d = getServiceResponse() for item in d: if isinstance(item, dict) and 'ghash' in item: del item['ghash'] pprint.pprint(d) Does that help? -- Steven From mwaters at ITS.JNJ.com Fri Dec 14 14:39:25 2012 From: mwaters at ITS.JNJ.com (Waters, Mike [ITSCA Non-J&J]) Date: Fri, 14 Dec 2012 13:39:25 +0000 Subject: [Tutor] Tutor Digest, Vol 106, Issue 31 In-Reply-To: References: Message-ID: <3245CBCA99B596439C37EF53E1220BE418C8C149@ITSUSRAGMDGD04.jnj.com> Hi Python tutor I have a question I believe I have posted before: when you have filled the page with text (commands,list etc) how do you clear the page to allow a clean page to continue on writing script? Thanks appreciate your help. Mike -----Original Message----- From: Tutor [mailto:tutor-bounces+mwaters=its.jnj.com at python.org] On Behalf Of tutor-request at python.org Sent: Friday, December 14, 2012 6:00 AM To: tutor at python.org Subject: Tutor Digest, Vol 106, Issue 31 Send Tutor mailing list submissions to tutor at python.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/tutor or, via email, send a message with subject or body 'help' to tutor-request at python.org You can reach the person managing the list at tutor-owner at python.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Tutor digest..." Today's Topics: 1. Using json to filter out results from a dictionary (Jasmine Gao) 2. Re: Using json to filter out results from a dictionary (Steven D'Aprano) ---------------------------------------------------------------------- Message: 1 Date: Thu, 13 Dec 2012 17:39:01 -0500 From: Jasmine Gao To: tutor at python.org Subject: [Tutor] Using json to filter out results from a dictionary Message-ID: Content-Type: text/plain; charset="iso-8859-1" Hello, I've just started programming in python and haven't had much experience with json. I've written this simple script that makes a request to the Bitly API, specifically the /v3/realtime/bursting_phrasesendpoint, and prints the response into terminal. What I want to do is take the response and filter out a portion of it so I only see the data I find important. So far I've used json to load the results into a dictionary, however I'm stuck in terms of how you loop through the dictionary and only print out certain keys while dumping the rest. To illustrate, this is the script: import urllib2 CLIENT_ID = "0367d81a428968a57704a295a4378521af81c91b" CLIENT_SECRET = "404862446e88f391e8c411ca1ee912506d64fffd" ACCESS_TOKEN = "53a01f38b09c0463cb9e2b35b151beb127843bf3" BITLY_ENDPOINT = " https://api-ssl.bitly.com/v3/realtime/bursting_phrases?access_token= "+ACCESS_TOKEN def getServiceResponse(): url = BITLY_ENDPOINT request = urllib2.Request(url) response = urllib2.urlopen(request) d = json.loads(response.read()) getServiceResponse() In terminal I run this command: python /file/location/file.py | tr "," "\n" which returns a long list of results like this: "data": {"selectivity": 3.0 "phrase": "justin bieber" "mean": 0.089999999999999997} {"std": 0.046334721206249076 "ghashes": [ {"visitors": 440 "ghash": "QXfZfQ"} {"visitors": 215 "ghash": "W9sHrc"} {"visitors": 92 "ghash": "XY9PPX"}] "N": 203183.0 "rate": 0.53000000000000003 "urls": [ {"visitors": 440 "aggregate_url": "http://bit.ly/QXfZfQ"} {"visitors": 215 "aggregate_url": "http://bit.ly/W9sHrc"} {"visitors": 92 "aggregate_url": "http://bit.ly/XY9PPX"}] How do I use json to filter out the "ghashes" from what is printed into terminal so that the results look like this instead?: "data": {"selectivity": 3.0 "phrase": "justin bieber" "mean": 0.089999999999999997} {"std": 0.046334721206249076 "N": 203183.0 "rate": 0.53000000000000003 "urls": [ {"visitors": 440 "aggregate_url": "http://bit.ly/QXfZfQ"} {"visitors": 215 "aggregate_url": "http://bit.ly/W9sHrc"} {"visitors": 92 "aggregate_url": "http://bit.ly/XY9PPX"}] Any help would greatly be appreciated, thank you! - Jasmine -------------- next part -------------- An HTML attachment was scrubbed... URL: ------------------------------ Message: 2 Date: Fri, 14 Dec 2012 10:21:38 +1100 From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] Using json to filter out results from a dictionary Message-ID: <50CA6302.2070707 at pearwood.info> Content-Type: text/plain; charset=UTF-8; format=flowed On 14/12/12 09:39, Jasmine Gao wrote: > Hello, > > I've just started programming in python and haven't had much > experience with json. This is not really a JSON problem. JSON just handles converting data in a dict to a string and back. As you say: > I'm stuck in terms of how you loop through the dictionary and only > print out certain keys while dumping the rest. That part is simple: for key, item in mydict.items(): if key != "rubbish": print key, item # In python 3, use "print(key, item)" instead Another way: if "rubbish" in mydict: del mydict print mydict # In python 3, use "print(mydict)" instead > To illustrate, this is the script: > > import urllib2 > CLIENT_ID = "0367d81a428968a57704a295a4378521af81c91b" > CLIENT_SECRET = "4...d" # secret redacted Should you have showed us this secret? Probably not a great idea. > ACCESS_TOKEN = "53a01f38b09c0463cb9e2b35b151beb127843bf3" > BITLY_ENDPOINT = > "https://api-ssl.bitly.com/v3/realtime/bursting_phrases?access_token=" > +ACCESS_TOKEN > > def getServiceResponse(): > url = BITLY_ENDPOINT > request = urllib2.Request(url) > response = urllib2.urlopen(request) > d = json.loads(response.read()) > > getServiceResponse() > > > In terminal I run this command: python /file/location/file.py | tr "," "\n" > which returns a long list of results like this: Your script doesn't actually print the results or do anything with them. Perhaps the part of your script that actually handles output is missing? What other important parts are missing? It makes it hard for us to help when we have to guess what you are actually doing. My guess is that you are actually running: print getServiceResponse() or something similar. Since you are already using an external tool "tr", you could simply pipe the output through grep: python /file/location/file.py | tr "," "\n" | grep -v ghash However, instead of using the external "tr" command, you might like to do this: import pprint # ... rest of your script as before d = getServiceResponse() pprint.pprint(d) That my give you acceptable results without the "tr" command. In this case, you need to filter the dict returned by getServiceResponse. I suggest something like this may be acceptable: d = getServiceResponse() for item in d: if isinstance(item, dict) and 'ghash' in item: del item['ghash'] pprint.pprint(d) Does that help? -- Steven ------------------------------ Subject: Digest Footer _______________________________________________ Tutor maillist - Tutor at python.org http://mail.python.org/mailman/listinfo/tutor ------------------------------ End of Tutor Digest, Vol 106, Issue 31 ************************************** From oscar.j.benjamin at gmail.com Fri Dec 14 15:58:58 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 14 Dec 2012 14:58:58 +0000 Subject: [Tutor] Tutor Digest, Vol 106, Issue 31 In-Reply-To: <3245CBCA99B596439C37EF53E1220BE418C8C149@ITSUSRAGMDGD04.jnj.com> References: <3245CBCA99B596439C37EF53E1220BE418C8C149@ITSUSRAGMDGD04.jnj.com> Message-ID: If you reply to the digest email you should to do two things: 1) Change the subject line to match the email you are replying to (or in this case choose a new subject). 2) Delete any part of the message that you are not responding to (in this case all of it). In fact why are you replying to the digest email when you are not replying to any of the messages in it? If you want to send an email to the tutor list you can just do "new mail" or equivalent in your mail client and then type "tutor at python.org". In my mail client I only have to "t" and "u" for "tutor" and then the software will auto-complete the remainder of the address. Replying to the digest instead seems pretty lazy to me. On 14 December 2012 13:39, Waters, Mike [ITSCA Non-J&J] wrote: > Hi Python tutor > I have a question I believe I have posted before: when you have filled the page with text (commands,list etc) how do you clear the page to allow a clean page to continue on writing script? What page are you talking about? Where are your commands being typed? Are using a terminal or IDLE, or something else? Perhaps you could try typing the command "clear". Oscar From brian.van.den.broek at gmail.com Fri Dec 14 16:02:20 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Fri, 14 Dec 2012 10:02:20 -0500 Subject: [Tutor] Fwd: Re: Tutor Digest, Vol 106, Issue 31 In-Reply-To: References: <3245CBCA99B596439C37EF53E1220BE418C8C149@ITSUSRAGMDGD04.jnj.com> Message-ID: Forwarding to the list what I sent privately. Stupid android UI. ---------- Forwarded message ---------- From: "Brian van den Broek" Date: 14 Dec 2012 10:01 Subject: Re: [Tutor] Tutor Digest, Vol 106, Issue 31 To: "Waters, Mike [ITSCA Non-J&J]" On 14 Dec 2012 08:43, "Waters, Mike [ITSCA Non-J&J]" wrote: > > Hi Python tutor > I have a question I believe I have posted before: when you have filled the page with text (commands,list etc) how do you clear the page to allow a clean page to continue on writing script? > Thanks appreciate your help. > Mike Mike, Please start a new thread for a new question rather than replying to the digest as you have done. This avoids a tonne of text irrelevant to your question (which I have removed) and affords you the easy to avail yourself of oportunity to give your inquiry a meaningful subject line. What do you mean by 'page'? What software are you working with? What OS? You've not given enough information. If you are thinking of an interactive prompt (though you don't seem to be) you can clear it with print "\n" * 80 # or some appropriate value If that doesn't help, please ask again with enough information about your context to allow those willing to help you to do so. Best, Brian vdB -------------- next part -------------- An HTML attachment was scrubbed... URL: From mwaters at ITS.JNJ.com Fri Dec 14 16:25:56 2012 From: mwaters at ITS.JNJ.com (Waters, Mike [ITSCA Non-J&J]) Date: Fri, 14 Dec 2012 15:25:56 +0000 Subject: [Tutor] New Re: Clearing Python text Message-ID: <3245CBCA99B596439C37EF53E1220BE418C8C1CA@ITSUSRAGMDGD04.jnj.com> Hi Tutor, I am using Win 7 ,Python 2.7. Interpreter. To state my challenge : When I have filled a page with values and text until it reaches the bottom of the screen, how can I highlight this and remove to allow further entries? I have seen John Guttag do this but he seems to be using a MAC. Thanks Mike Michael Waters Business Unit Information Technology Global Data Center Data Center Services Team GDC 24/7 HotLine # 800-267-3471 McNeil Consumer Heathcare A division of Johnson & Johnson [cid:image001.png at 01CDD9E5.66F96150] Desk # 519-826-6226 ex 5860 Home# 519-767-2629 Cell 226-500-1776 Office Hours M-F 8:00am 4:00pm My hours 8:00AM-12:00PM ITS Guelph 890 Woodlawn Road West,Guelph,On. N1K 1A5 mwaters at its.jnj.com -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: image001.png Type: image/png Size: 23046 bytes Desc: image001.png URL: From oscar.j.benjamin at gmail.com Fri Dec 14 16:38:58 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 14 Dec 2012 15:38:58 +0000 Subject: [Tutor] New Re: Clearing Python text In-Reply-To: <3245CBCA99B596439C37EF53E1220BE418C8C1CA@ITSUSRAGMDGD04.jnj.com> References: <3245CBCA99B596439C37EF53E1220BE418C8C1CA@ITSUSRAGMDGD04.jnj.com> Message-ID: Further to previous comments, It is also preferred to send emails to this list in plain text rather than html. This is because it protects the formatting of Python code and works better with all possible mail-clients/news-readers/archives etc. On 14 December 2012 15:25, Waters, Mike [ITSCA Non-J&J] wrote: > > Hi Tutor, > > I am using Win 7 ,Python 2.7. Interpreter. > > To state my challenge : When I have filled a page with values and text until it reaches the bottom of the screen, how can I highlight this and remove to allow further entries? I have seen John Guttag do this but he seems to be using a MAC. It's still not clear what you are referring to. What piece of software is responsible for the "page" you are referring to? The Python interpreter itself does not have a "page". If you run Python in a terminal, then the terminal shows a page of commands and output. If you run Python commands in IDLE then you will be typing your commands and viewing their output in the IDLE page. If you edit a .py file then your editor will show you a page (although the file itself is not really divided into pages). Can you please clarify very specifically what this "page" is and/or what you are doing that leads to the page being there? Oscar From memilanuk at gmail.com Fri Dec 14 16:56:17 2012 From: memilanuk at gmail.com (Monte Milanuk) Date: Fri, 14 Dec 2012 07:56:17 -0800 Subject: [Tutor] New Re: Clearing Python text In-Reply-To: <3245CBCA99B596439C37EF53E1220BE418C8C1CA@ITSUSRAGMDGD04.jnj.com> References: <3245CBCA99B596439C37EF53E1220BE418C8C1CA@ITSUSRAGMDGD04.jnj.com> Message-ID: On 12/14/2012 07:25 AM, Waters, Mike [ITSCA Non-J&J] wrote: > Hi Tutor, > > I am using Win 7 ,Python 2.7. Interpreter. > > To state my challenge : When I have filled a page with values and text > until it reaches the bottom of the screen, how can I highlight this and > remove to allow further entries? I have seen John Guttag do this but he > seems to be using a MAC. > I'm assuming you're using the interpreter in IDLE? Try 'Control + L', if memory serves. From d at davea.name Fri Dec 14 18:12:56 2012 From: d at davea.name (Dave Angel) Date: Fri, 14 Dec 2012 12:12:56 -0500 Subject: [Tutor] New Re: Clearing Python text In-Reply-To: <3245CBCA99B596439C37EF53E1220BE418C8C1CA@ITSUSRAGMDGD04.jnj.com> References: <3245CBCA99B596439C37EF53E1220BE418C8C1CA@ITSUSRAGMDGD04.jnj.com> Message-ID: <50CB5E18.7010106@davea.name> On 12/14/2012 10:25 AM, Waters, Mike [ITSCA Non-J&J] wrote: > Hi Tutor, > I am using Win 7 ,Python 2.7. Interpreter. > To state my challenge : When I have filled a page with values and text until it reaches the bottom of the screen, how can I highlight this and remove to allow further entries? I have seen John Guttag do this but he seems to be using a MAC. > Thanks > Mike > Are you talking about the DOS box (CMD window, terminal, or any other alias)? If so, it could be any size, from a few lines up to maybe 50 (or more, depends on the screen resolution of your particular tube). And what do you mean by full? When your program is generating output with print statements, once it reaches the bottom of the DOS box, it'll scroll. Python has no control over that. If your version of CMD supports ANSI sequences, you could print ESC 2 J from your program when you want the screen cleared. But easier is just to print a boatload of newlines, and figure that'll scroll the old stuff off. -- DaveA From jacklittlemc at yahoo.com Sun Dec 2 02:37:15 2012 From: jacklittlemc at yahoo.com (Jack Little) Date: Sat, 1 Dec 2012 17:37:15 -0800 (PST) Subject: [Tutor] Help please! Message-ID: <1354412235.66240.YahooMailNeo@web124502.mail.ne1.yahoo.com> Hi Tutor, I'm getting this error Traceback (most recent call last): File "C:\Users\Jack\Desktop\python\g.py", line 45, in path_1pt1() NameError: name 'path_1pt1' is not defined With the attached file Please get back to me Thank you -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: g.py Type: application/octet-stream Size: 6843 bytes Desc: not available URL: From mark.rourke7 at gmail.com Fri Dec 7 00:43:03 2012 From: mark.rourke7 at gmail.com (Mark Rourke) Date: Thu, 06 Dec 2012 18:43:03 -0500 Subject: [Tutor] College student having python problems Message-ID: <50C12D87.8050303@gmail.com> The Following is my code: #Mark Rourke, Mitch Lavalle #Dec 04, 2012 #Global Constants #Constant Integer MAX_QTY MAX_QTY = 20 #Constant Real TAX_RATE TAX_RATE = .13 #Constant Real Burger Burger = .99 #Constant Real Fries Fries = .79 #Constant Real Soda Soda = 1.09 import sys def getOrderNumber(): #Get the order number from user #orderNumber >=0 goodOrder = True while goodOrder == True: try: orderNumber = int(input("Please enter order number, Enter 0 to Stop")) except ValueError: print("Non numeric input entered. Program terminating...") except: print("Input Error") else: if (orderNumber > 0): print ("Your order Number is %d" %(orderNumber)) return orderNumber elif (orderNumber < 0 or orderNumber != 0): goodOrder = True print ("Please enter a proper value") def showMenu(): #THIS IS ALL PART OF SHOWMENU: #keep getting items for this order until the user wants to end the order #set initial quantities of items numBurgers=20; numFries=20; numSodas=20; menuItem = showMenu()<---------ERROR THERE<---- #ordering stuff from the menu menuItem=-1 while (menuItem!= 0): #show user menu print(" ----------------------------------------------- M E N U ------------------------------------------------") print ("Your order Number is %d") print("Cost of Burger: %.2f" %Burger) print("Cost of Fries: %.2f" %Fries) print("Cost of Soda: %.2f" %Soda) print("Enter 1 for Yum Yum Burger") print("Enter 2 for Grease Yum Fries") print("Enter 3 for Soda Yum") print("Enter 0 to end order") #get menu selection from them itemType = int(input("Enter now->")) #inside the function, we get input #validate the input (1, 2, 3, 4) #return that value #menuItem 1 Yum Yum Burger #menuItem 2 Greasy Yum Fries #get how many of the item we want #i.e. if menuItem is 1 (Yum Yum Burger) and howManyOfItem is 2, then the person wants 2 Yum Yum Burgers #need to tell person how many they can order and prompt for number #need to check if quantity ordered is allowed if menuItem == 1: #burger print("You can order 0 through"+numBurgers+"burgers.") numBurgAsked=input("how many would you like?") #prompting taken care of in getitem #passes back number that they want if(numBurgAsked<=numBurgers): numBurgers = numBurgers - numBurgAsked elif menuItem == 2: print("You can order 0 through"+numFries+"fries.") numFriesAsked=input("how many would you like?") if(numFriesAsked<=numFries): numBurgers = numBurgers - numBurgAsked elif menuItem == 3: print("You can order 0 through"+numSodas+"sodas.") numSodasAsked=input("how many would you like?") if(numSodasAsked<=numFries): numSodas = numSodas - numSodasAsked #THIS IS PART OF CALCULATEFINALCOST: #Calculate the cost of each item costOfBurgers = calculateCost (1, burgersOrdered) costOfFries = calculateCost (2, friesOrdered) costOfSodas = calculateCost (3, sodasOrdered) totalCost = CalculateTotalCost(1,2,3 + totalTax) tax = calculateTax(totalCost * TAX_RATE) finalCost = calculateFinalPrice(TotalCost + tax) def main(): getOrderNumber() showMenu() main() Okay this is what I've got so far on like 47 i have stated menuItem = showMenu()<---------ERROR THERE<----, thats where I'm getting the error. tI goes into an infinate loop stating "File "C:\Users\Mark\Desktop\Assignment3_MarkRourke.py", line 45, in showMenu menuItem = showMenu()" can you give me some sort of way to get out of this? and if so, can you please show me where in the code and how?, I've been working on this for over 20 hours my teacher isn't helpful, can you please assist me ------------------------------------------------------------------------ Mark Rourke T: 705-728-6169 M: 705-331-0175 E: Mark.Rourke7 at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From mark.rourke7 at gmail.com Mon Dec 3 16:23:49 2012 From: mark.rourke7 at gmail.com (Mark Rourke) Date: Mon, 03 Dec 2012 10:23:49 -0500 Subject: [Tutor] First semester no coding experince, please help! Message-ID: <50BCC405.3010600@gmail.com> Hello, the following attachment (no virus I swear!)is an assignment I have for my programming class, I am having alot of difficulty completing this assignment, my teacher does not do a very good job of explaining this project to me, I desperately need some help/guidance/direction with this, please help! Thanks -- Mark Rourke T: 705-728-6169 M: 705-331-0175 E: Mark.Rourke7 at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: Assignment3-1.docx Type: application/octet-stream Size: 28929 bytes Desc: not available URL: From mumair_masood at hotmail.com Thu Dec 6 11:20:37 2012 From: mumair_masood at hotmail.com (Umair Masood) Date: Thu, 6 Dec 2012 15:20:37 +0500 Subject: [Tutor] Information Message-ID: How to create a server in python using SOAP? Please also tell the ports to be used. Kindly help me out. Regards,Umair -------------- next part -------------- An HTML attachment was scrubbed... URL: From worthez at googlemail.com Fri Dec 7 23:46:39 2012 From: worthez at googlemail.com (marcusw4988@hotmail.co.uk) Date: Fri, 7 Dec 2012 22:46:39 +0000 Subject: [Tutor] Learning Python through automating web application testing. Message-ID: Hello all, I'm new to posting on mailing lists so hopefully I've picked the correct one for my question(s). A little about my programming experience first. I work as a 'black box' software tester on a website/application so am familiar with the IT development process but when it comes to programming I'm very much a keenly interested newbie. To give you some idea of my current ability it would be (with a bit of revision) around chapter 8 of Wesley Chun's "Core Python programming". I do not in any way mean this as a criticism of "Core Python" but to learn, what I really need is a project. Although "Core Python" rigorously tackles it's subject; I've found I need a more tangible reason than "It would be great if I could program" to stay focused on working my way through the book. It took more than one attempt to get to chapter 8 and as I was stubbornly refusing to ask for help (madness I know) it became quite disheartening when I found some questions tough going. It will be an invaluable point of reference in the future, it's simply I don't currently need to understand it's subject with the depth it offers. All I need at the moment is "enough to get started" which by my rough calculation is around 4.35% (not including appendices) of what "Core Python" has to offer! So what I need is a small(ish) project that introduces thoroughly a useful part of the Python language while teaching me how to program. But for what will sound like a silly reason I've always managed to avoid undertaking this task. Basically a fear of failure has stopped me from starting. I mean I've always talked a good game but what if I'm not sharp enough to learn to competently program? Not attempting is better than failing surely? Because of this fear I've never admitted I've got a ready made project just waiting for me to tackle........... Until now! I would like to learn to automate the testing of a http(s) web site/applications but feel slightly overwhelmed by this task so would like to ask for some initial guidance. These are some of questions that I have. How do I go about this? Where do I start? There's just so much out there to help with learning Python I'm experiencing information overload! How do I stop myself from trying to run before I can walk? In a perfect world a step by step guide, in automating web tests, using Python is what I'm after but failing that(!) which sites/forums/mailing lists are of particular interest to someone who would like to learn Python programming initially through automating web tests? (By web tests, to begin with, I mean automated regression testing of a site by multiple users) At work we use Selenium and Java so I'm aware that Selenium comes with a Python driver. Would that be a good place to start? Apologies if I'm not supposed to ask more than one question per mail but these are all closely related and could be thought of as "newbie struggling to see the wood/forest for the trees!" Many thanks for your help Regards Marcus -------------- next part -------------- An HTML attachment was scrubbed... URL: From yi.molina at gmail.com Tue Dec 4 21:26:53 2012 From: yi.molina at gmail.com (Yi Molina) Date: Tue, 4 Dec 2012 12:26:53 -0800 Subject: [Tutor] Which version of activetcl to use for Mac OS X 10.7? Message-ID: Hi, all. I am new to python and just installed Python 2.7.3 on my Mac OS X 10.7.5 from python.org. I read that I am supposed to use ActiveTcl 8.5.11.1. So I clicked on the link on but found only ActiveTcl8.5.11.1.295590-macosx10.5-i386-x86_64-threaded.dmg, Which is for MAC OS X 10.5. I could not find anything for OS X 10.7, so I installed it. I tried a few simple programs in IDLE and they work fine. But I'd like to confirm if this is the right activetcl version to use? Thanks! Yi -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Dec 14 19:34:51 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 14 Dec 2012 18:34:51 +0000 Subject: [Tutor] New Re: Clearing Python text In-Reply-To: <3245CBCA99B596439C37EF53E1220BE418C8C1CA@ITSUSRAGMDGD04.jnj.com> References: <3245CBCA99B596439C37EF53E1220BE418C8C1CA@ITSUSRAGMDGD04.jnj.com> Message-ID: On 14/12/12 15:25, Waters, Mike [ITSCA Non-J&J] wrote: > To state my challenge : When I have filled a page with values and text > until it reaches the bottom of the screen, how can I highlight this and > remove to allow further entries? I have seen John Guttag do this but he > seems to be using a MAC. Like others I don't really understand what you mean by a page. But in general you don't need to do that. If you are using the Python prompt >>> You just keep adding stuff and it will scroll up. If you just want some clear space hit return a few times. Or as already suggested: print '\n' * 50 # prints 50 newlines... But it all depends on what tool you are using to enter your Python code. You may have something odd that has a fixed screen but if so I've never come across it! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Dec 14 20:11:58 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 14 Dec 2012 19:11:58 +0000 Subject: [Tutor] College student having python problems In-Reply-To: <50C12D87.8050303@gmail.com> References: <50C12D87.8050303@gmail.com> Message-ID: On 06/12/12 23:43, Mark Rourke wrote: > def showMenu(): > #THIS IS ALL PART OF SHOWMENU: > #keep getting items for this order until the user wants to end the order > #set initial quantities of items > numBurgers=20; > numFries=20; > numSodas=20; > menuItem = showMenu()<---------ERROR THERE<---- > #ordering stuff from the menu > def main(): > getOrderNumber() > showMenu() > main() main calls showMenu which calls showMenu which calls showMenu etc.... You need to break the cycle of showMenu calls. Thats usually done by inserting some kind of check before the call to showMenu inside showMenu so that you only call it when needed. However I suspect what you really need here is to use a loop instead of having showMenu calling itself. choice = None while not choice # or some other test condition here choice = showMenu() HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From kwpolska at gmail.com Fri Dec 14 20:41:36 2012 From: kwpolska at gmail.com (Kwpolska) Date: Fri, 14 Dec 2012 20:41:36 +0100 Subject: [Tutor] Help please! In-Reply-To: <1354412235.66240.YahooMailNeo@web124502.mail.ne1.yahoo.com> References: <1354412235.66240.YahooMailNeo@web124502.mail.ne1.yahoo.com> Message-ID: On Sun, Dec 2, 2012 at 2:37 AM, Jack Little wrote: > Hi Tutor, > I'm getting this error > > Traceback (most recent call last): File "C:\Users\Jack\Desktop\python\g.py", > line 45, in path_1pt1() NameError: name 'path_1pt1' is not defined > > With the attached file > > Please get back to me > Thank you > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > You need to define path_1pt1 *before* simpstart. Also, about not ?free source?: (a) did you mean: open source? (b) why did you publish it here? -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From ramit.prasad at jpmorgan.com Fri Dec 14 20:42:44 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 14 Dec 2012 19:42:44 +0000 Subject: [Tutor] Which version of activetcl to use for Mac OS X 10.7? In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47418091298@SCACMX008.exchad.jpmchase.net> Yi Molina wrote: > > Hi, all. I am new to python and just installed Python 2.7.3 on my Mac OS X 10.7.5 from python.org. I read that I > am supposed to use??ActiveTcl 8.5.11.1.?So I clicked on the link on but found only?ActiveTcl8.5.11.1.295590- > macosx10.5-i386-x86_64-threaded.dmg, Which is for MAC OS X 10.5. I could not > find anything for OS X 10.7, so I installed it. I tried a few simple programs in IDLE and they work fine. But > I'd like to confirm if this is the right activetcl version to use? > > Thanks! > Yi I usually recommend installing from a package management system and not manually installing. You can use MacPorts or Homebrew for OS X and they will take care of selecting an appropriate Tcl version. This (MacPorts) may require you to install some developer tools like XCode but the process is *very* simple. I am personally unfamiliar with Homebrew so I am not sure of their requirements. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com Fri Dec 14 20:37:18 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 14 Dec 2012 19:37:18 +0000 Subject: [Tutor] First semester no coding experince, please help! In-Reply-To: <50BCC405.3010600@gmail.com> References: <50BCC405.3010600@gmail.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418091270@SCACMX008.exchad.jpmchase.net> Mark Rourke wrote: > Sent: Monday, December 03, 2012 9:24 AM > To: tutor at python.org > Subject: [Tutor] First semester no coding experince, please help! > > Hello, the following attachment? (no virus I swear!)is an assignment I have for my programming class, I am > having alot of difficulty completing this assignment, my teacher does not do a very good job of explaining this > project to me, I desperately need some help/guidance/direction with this, please help! > > Thanks > -- > Mark Rourke > > T: 705-728-6169 > M: 705-331-0175 > E: Mark.Rourke7 at gmail.com Why bother attaching the file and not just include the relevant text in the email (summarizing as you can)? Many people are reluctant to open an attachment and I can see nothing in the attachment other than text. Also, you would be better served by posting in plain text (not "rich" mode or HTML) as HTML emails can mangle code causing it to be unreadable. As for your assignment, what is giving you trouble? To me this is clear and well documented. I wish my professors in college (or school) had given me such complete assignments! Do you have code that is giving you a problem? If not, why do you not have an attempt? How can we help you otherwise? ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From mwaters at ITS.JNJ.com Fri Dec 14 20:29:42 2012 From: mwaters at ITS.JNJ.com (Waters, Mike [ITSCA Non-J&J]) Date: Fri, 14 Dec 2012 19:29:42 +0000 Subject: [Tutor] Thanks Message-ID: <3245CBCA99B596439C37EF53E1220BE418C8C280@ITSUSRAGMDGD04.jnj.com> I just want to thank all who gave some input into my questions. And the points on convention ,hey I just learned about plain text! Thanks Mike Michael Waters Business Unit Information Technology Global Data Center Data Center Services Team GDC 24/7 HotLine # 800-267-3471 McNeil Consumer Heathcare A division of Johnson & Johnson Desk # 519-826-6226 ex 5860 Home# 519-767-2629 Cell 226-500-1776 Office Hours M-F 8:00am 4:00pm My hours 8:00AM-12:00PM ITS Guelph 890 Woodlawn Road West,Guelph,On. N1K 1A5 mwaters at its.jnj.com From ramit.prasad at jpmorgan.com Fri Dec 14 20:50:13 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 14 Dec 2012 19:50:13 +0000 Subject: [Tutor] Information In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474180912CC@SCACMX008.exchad.jpmchase.net> Umair Masood wrote: > > How to create a server in python using SOAP? Please also tell the ports to be used. Kindly help me out. > > Regards, > Umair The 4th link looks good: http://lmgtfy.com/?q=How+to+create+a+server+in+python+using+SOAP%3F+# First link: http://lmgtfy.com/?q=What+ports+are+used+in+SOAP%3F Kindly-helping-out, ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From steve at pearwood.info Fri Dec 14 23:04:36 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 15 Dec 2012 09:04:36 +1100 Subject: [Tutor] Help please! In-Reply-To: <1354412235.66240.YahooMailNeo@web124502.mail.ne1.yahoo.com> References: <1354412235.66240.YahooMailNeo@web124502.mail.ne1.yahoo.com> Message-ID: <50CBA274.5080602@pearwood.info> On 02/12/12 12:37, Jack Little wrote: > Hi Tutor, > I'm getting this error > > Traceback (most recent call last): File "C:\Users\Jack\Desktop\python\g.py", >line 45, in path_1pt1() > NameError: name 'path_1pt1' is not defined Names need to be defined before they are used. Code needs to be indented to be inside a function, if it is not indented then it is considered to be part of the "top level" code that runs immediately. So you begin a new function, simpstart: > def simpstart(): > global ammo > global health > global tech_parts > global radio_parts By the way, you can consolidate those four lines to one: global ammo, health, tech_parts, radio_parts But here you lose the indentation, so Python considers the following to be "top level" code and executes it immediately: > print "You awake in a haze. A crate,a door and a radio." > g1 = raw_input("Which do you choose ") Questions in English should end with a question mark, or people will consider you ignorant and illiterate. Unless you are a famous poet or artist, in which case they will fawn over how transgressive you are. > if g1 == "CRATE" or g1 == "Crate" or g1 == "crate": What if they type "cRAtE" or "crATE"? Much simpler to do this: if g1.lower() == "create": By the way, you will find programming much, much simpler if you always use meaningful variable names. "g1"? What does that mean? A better name would be something like "user_response", or even just "response". So, skipping ahead, we come to this bit: > elif g2 == "NORTH" or g2 == "North" or g2 == "north": > path_1pt1() Again, this is better written as "if g2.lower() == 'north':". At this point, Python then tries to call the path_1pt function, but it hasn't been defined yet. So it gives you a NameError. How do you fix this? Simple: this entire block of code needs to be indented level with the global declarations at the start of simstart. (Fixing this error may very well reveal further errors. Good luck!) Another comment: > #A Towel Production > # APOC > #------- > global ammo1 > global ammo2 > global ammo3 > global health > global tech_parts > global exp > global radio_parts These seven global lines don't do anything. They are literally pointless. One of the mysteries to me is why Python allows global declarations outside of functions, but these lines literally do nothing at all except fool the reader (you!) into thinking that they do something. Get rid of them. > ammo1=10 > ammo2=0 > ammo3=0 I note that you have three variables, "ammo1" through "ammo3", but in the simpstart function, you declare a global "ammo". -- Steven From chigga101 at gmail.com Sat Dec 15 01:46:31 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Sat, 15 Dec 2012 00:46:31 +0000 Subject: [Tutor] exercise Message-ID: i have an assignment. the example given showed the opposite way to achieve it but im a bit stuck, here's an explanation of the example. it's a mailing list that will be kept in a default dict with sets as value containers to ensure there are no duplicate email addresses. the key will hold the email address and the value will hold the email group in a set(). here is some code to better explain. self.sort_email = defaultdict(set) self.sort_email['mail at hotmail.com'].add('Friends') self.sort_email['mail2 at gmail.com'].add('Friends') self.sort_email['mail at hotmail.com'].add('Family') #duplicate in a different group self.sort_email['eddie at hotmail.com'].add('Family') self.sort_email['ebay at ebay.com'].add('work') so even if an email can be in different groups, using a set ensures the same mail won't be sent twice to the same address now sending the email, we have to make a list of the groups we want to send mail to and make it a set() group_list = ['Friends', 'Family'] group_list = set(group_list) return {e for (e, g) in self.sort_email.items() if g & groups_list} #this returns 'mail at hotmail.com', 'mail2 at hotmail.com' , 'eddie at hotmail.com' e represents email addresses and g represents groups. the condition is saying return only the email addresses that are in BOTH group_list and the dictionary sets. after getting the emails from the return statement, i send them off. that was the example, now my assignment is the opposite. it wants me to use groups as the keys in the dictionary and a list of email addresses as the values. it seems simple enough but i just cant get it. im on the verge of giving up but i wondered if anyone could show or tell me how to achieve this. the example said to do this just needs a small amount of modifying the above. From msg.ufo at gmail.com Sat Dec 15 02:10:59 2012 From: msg.ufo at gmail.com (Mike G) Date: Fri, 14 Dec 2012 17:10:59 -0800 Subject: [Tutor] Clearing Python text Message-ID: I use Windows XP, Python 2.7, Notepad++ as my editor, and generally run my .py files from cmd, this is how I toggle between running my .py file after an edit (and save) and subsequently clearing the screen - it's pretty easy. I "arrow-up/arrow-down" on the keyboard to reprint (type) the latest command in cmd, this toggles between running my .py file... runs my file ... C:\Python27\MyScripts>python helloworld.py and to clear whatever printed to the screen... clears the screen ... C:\Python27\MyScripts>cls Once you've typed both at the prompt just arrow up or down, then hit enter - file run or screen cleared, as well saves time retyping. Sorry if I missed it but this works for me because I too prefer to print to a cleared screen, don't know why, just do. Mike :) From chigga101 at gmail.com Sat Dec 15 02:51:01 2012 From: chigga101 at gmail.com (Matthew Ngaha) Date: Sat, 15 Dec 2012 01:51:01 +0000 Subject: [Tutor] exercise In-Reply-To: References: Message-ID: > return {e for (e, g) in self.sort_email.items() > if g & groups_list} > guys i think ive got it. The & in that comprehension was really confusing me, but i found out it means intersection, so i took the sets manually and saw the results i got using intersection and it became more clear from there. A Big Thanks if you took the time to help solve my issues. From robertvstepp at gmail.com Sat Dec 15 06:26:00 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 14 Dec 2012 23:26:00 -0600 Subject: [Tutor] Recommended texts for self-study to master software engineering? Message-ID: This is obviously not a directly python-related question; however, I do believe that it would be of interest to many aspiring programmers who are diligently learning python on their own. In my case what I would eventually like to be able to do is develop complex, graphically intensive educational software. An example of such an application might be a virtual chemistry laboratory that attempts to come as close as possible to mimicking in the virtual world what one would experience taking a chemistry lab course. Such a course might be either web-based or a strictly desktop application or a mixture of both. So, through self-study, I would like to be able to acquire the knowledge I would need to become a competent designer and programmer, using good software engineering practices. Now I could go online and look at typical courses taken by those pursuing software engineering or computer science degrees and even come up with the textbooks that such courses use. But I would have no idea of how well-suited such textbooks would be for self-study. So my question is what would be a sequence of books to acquire the knowledge I would need that are especially well-suited to self-study? I may be asking for the unanswerable, but I am hoping to be pointed to some good texts for my future studies. [As a reminder, I already have a B.Sc. in physics with some graduate work in same, so math, etc. is not an issue. I am not looking to replicate going back to college. ~(:>)) ] As always, many thanks in advance! boB From kwpolska at gmail.com Sat Dec 15 09:50:00 2012 From: kwpolska at gmail.com (Kwpolska) Date: Sat, 15 Dec 2012 09:50:00 +0100 Subject: [Tutor] Clearing Python text In-Reply-To: References: Message-ID: On Sat, Dec 15, 2012 at 2:10 AM, Mike G wrote: > I use Windows XP, Python 2.7, Notepad++ as my editor, and generally > run my .py files from cmd, this is how I toggle between running my .py > file after an edit (and save) and subsequently clearing the screen - > it's pretty easy. > > I "arrow-up/arrow-down" on the keyboard to reprint (type) the latest > command in cmd, this toggles between running my .py file... > runs my file ... C:\Python27\MyScripts>python helloworld.py > > and to clear whatever printed to the screen... > clears the screen ... C:\Python27\MyScripts>cls > > Once you've typed both at the prompt just arrow up or down, then hit > enter - file run or screen cleared, as well saves time retyping. > > Sorry if I missed it but this works for me because I too prefer to > print to a cleared screen, don't know why, just do. > > Mike :) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Or cls & python file.py. (clear; ./file.py for the 2 POSIX folks that want this behavior.) -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From alan.gauld at btinternet.com Sat Dec 15 10:27:45 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 15 Dec 2012 09:27:45 +0000 Subject: [Tutor] Recommended texts for self-study to master software engineering? In-Reply-To: References: Message-ID: On 15/12/12 05:26, boB Stepp wrote: > In my case what I would eventually like to be able to do is develop > complex, graphically intensive educational software. OK, the first thing to point out is that software engineering is not the same as programming. In the same way that civil engineering is not the same as bricklaying. SE is about the skills and practices needed to run repeatable software projects. The focus is on techniques that apply regardless of the problem domain. The focus is also mainly on large scale projects involving teams of developers rather than a single individual or even a few. This has typically led to practices that are overly onerous for small projects. The recent trend to Agile has partially addressed the needs of small projects by introducing a parallel world of software practice that is more suited to small projects (usually cited as up to about 30 developers). So if thats how you see your projects turning out then you should focus on material geared to Agile rather than traditional SE. Many of the techniques are similar of course - the use of version control, automated testing, good design patterns etc. But traditional SE has a lot more in the way of heavyweight project management too, quality gates, peer reviews, project control files etc and encourages use of heavyweight tools (CASE, IPSE, Projet planning, Requirement mgt etc etc) Agile is more about getting a team talking and collaborating directly with rapid development cycles and feedback. On the assumption you fit the agile model I'd start with the Agile Manifesto: http://agilemanifesto.org and then read the wikipedia article: http://en.wikipedia.org/wiki/Agile_software_development The software engineering link box at the bottom offers a cornucopia of topics to research after that. > textbooks would be for self-study. So my question is what would be a > sequence of books to acquire the knowledge I would need that are > especially well-suited to self-study? Now to turn to this aspect, I'd recommend a few books for general good practice: Structure and Interpretation of Computer Programs (SICP - available online) Code Complete Object Oriented Analysis and Design Programming Pearls (vols 1 & 2) Then there are more topic specific texts but I can't personally recommend anything in your topic space so I'll let others comment on that. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wayne at waynewerner.com Sat Dec 15 13:48:12 2012 From: wayne at waynewerner.com (Wayne Werner) Date: Sat, 15 Dec 2012 06:48:12 -0600 (CST) Subject: [Tutor] Recommended texts for self-study to master software engineering? In-Reply-To: References: Message-ID: On Fri, 14 Dec 2012, boB Stepp wrote: > This is obviously not a directly python-related question; however, I > do believe that it would be of interest to many aspiring programmers > who are diligently learning python on their own. > > So, through self-study, I would like to be able to acquire the > knowledge I would need to become a competent designer and programmer, > using good software engineering practices. Now I could go online and > look at typical courses taken by those pursuing software engineering > or computer science degrees and even come up with the textbooks that > such courses use. But I would have no idea of how well-suited such > textbooks would be for self-study. So my question is what would be a > sequence of books to acquire the knowledge I would need that are > especially well-suited to self-study? I may be asking for the > unanswerable, but I am hoping to be pointed to some good texts for my > future studies. Rather than simple self-study, why not take advantage of the offerings by such folks as Coursera, Edx, or Kahn Academy? They all have free courses in programming and software development that require a large degree of self-motivation (after all, you didn't pay anything for it so you don't have the same drive as regular college), but it has the added benefit of hundreds, perhaps thousands of other active participants who are also learning at the same time. *Most* of the Python books that I've read are at least as well-suited to self study as any of the rest of them. Although, my own personal experience is that contributing to this list has done more to help me really understand the basics of Python and development than anything else. HTH, Wayne From wrw at mac.com Sat Dec 15 15:34:48 2012 From: wrw at mac.com (wrw at mac.com) Date: Sat, 15 Dec 2012 09:34:48 -0500 Subject: [Tutor] Learning Python through automating web application testing. In-Reply-To: References: Message-ID: <6346464F-AF6E-4BCE-A2E7-42528B7FC2D9@mac.com> On Dec 7, 2012, at 5:46 PM, marcusw4988 at hotmail.co.uk wrote: > Hello all, > > I'm new to posting on mailing lists so hopefully I've picked the correct one for my question(s). > > A little about my programming experience first. > [byte] > Because of this fear I've never admitted I've got a ready made project just waiting for me to tackle........... > > Until now! > > I would like to learn to automate the testing of a http(s) web site/applications but feel slightly overwhelmed by this task so would like to ask for some initial guidance. > Wow (!) welcome the wonderful world of Python. I have several comments - but let me start (with apologies) by encouraging you to think about another initial project. Others may well disagree with me (and I'd welcome that), but from my point of view, your choice (although by starting small can probably be done with relatively little of YOUR python code to call libraries), does require a deeper understanding of several parallel (pun intended) aspects of programming for web, ssh, sockets, python, your OS, and your chosen python libraries than you may realize. Just to mention one - simulation of pounding on the test site by several users will require multi-processing from a pool of parallel jobs. The standard Python library has the tools to handle this, but it isn't an easy subject for someone new to programming to get your mental arms wrapped around. I'm not familiar with the Chun book, so my worries may be completely misplaced (and if so, I apologize), but if you aren't really comfortable yet with OOP, and class definitions that start with '__init__(self?)' - this project isn't a good place to start. You can get some feel for your level of achievement by looking at urllib, urllib2, and httplib in the python standard documentation (and maybe looking at beautifulsoup (either BS3 or BS4). Then, if you want to go ahead - the kind of questions you will be coming up with are probably better addressed on the more general python-list at python.org discussion group. (There are lots of folks here to read both, so you won't need to cross post.) > These are some of questions that I have. > > How do I go about this? > > Where do I start? > > There's just so much out there to help with learning Python I'm experiencing information overload! > > How do I stop myself from trying to run before I can walk? > My suggestion would be to pick a project that yields something useful around your house or apartment (flat?) or a single-function utility that would be useful to you (maybe some sort of backup utility that is customized for the way you work). > In a perfect world a step by step guide, in automating web tests, using Python is what I'm after but failing that(!) which sites/forums/mailing lists are of particular interest to someone who would like to learn Python programming initially through automating web tests? (By web tests, to begin with, I mean automated regression testing of a site by multiple users) > When you get to the point of actually starting to tackle this - there is one more question you need to answer before you start. Are you trying to check for correct functionality at all branch points when users respond appropriately, or are you wanting to check for possible ways the site can be crashed by users typing garbage or attempting to attack the site? The latter is a MUCH more complex project. Sorry to sound so discouraging (and as I said, others may have much more encouraging suggestions for ways to approach this), -Bill > At work we use Selenium and Java so I'm aware that Selenium comes with a Python driver. Would that be a good place to start? > > Apologies if I'm not supposed to ask more than one question per mail but these are all closely related and could be thought of as "newbie struggling to see the wood/forest for the trees!" > > Many thanks for your help > > Regards > Marcus > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From robertvstepp at gmail.com Sat Dec 15 18:19:55 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 15 Dec 2012 11:19:55 -0600 Subject: [Tutor] Recommended texts for self-study to master software engineering? In-Reply-To: References: Message-ID: On Sat, Dec 15, 2012 at 3:27 AM, Alan Gauld wrote: > OK, the first thing to point out is that software engineering is not the > same as programming. In the same way that civil engineering is not the same > as bricklaying. > > SE is about the skills and practices needed to run repeatable software > projects. The focus is on techniques that apply regardless of the problem > domain. The focus is also mainly on large scale projects involving teams of > developers rather than a single individual > or even a few. This has typically led to practices that are overly onerous > for small projects. > > The recent trend to Agile has partially addressed the needs of small > projects by introducing a parallel world of software practice that is more > suited to small projects (usually cited as up to about 30 developers). So if > thats how you see your projects turning out then you should focus on > material geared to Agile rather than traditional SE. > > Many of the techniques are similar of course - the use of version control, > automated testing, good design patterns etc. But traditional SE has a lot > more in the way of heavyweight project management too, quality gates, peer > reviews, project control files etc and encourages use of heavyweight tools > (CASE, IPSE, Projet planning, Requirement mgt etc etc) Agile is more about > getting a team talking and collaborating > directly with rapid development cycles and feedback. > I understood the points you make above, but was not aware of the "Agile trend". I have seen books on this topic, but never thought to take a glance at one. I will definitely have to look into this. Even though I do not think I am ever likely to be involved in a project involving teams of developers, I would still want to possess the knowledge to be able to do so if I had to. I guess I was not as clear to my intentions as I thought I was. Essentially I would like to acquire all of the expected knowledge that a person who graduates with a software engineering and/or computer science degree would be expected to have. I gave the example of the project I could envisage doing to exclude areas such as designing operating systems and such that I believe C.Sc. people typically take courses in if that is one of their emphasis tracks. Of course I would want to have whatever basic understandings in such realms that any C.Sc. graduate would be expected to have. With all this in mind, I was looking for books that are especially suitable for self-study to eventually acquire all of this knowledge. For instance, if someone were to ask me what textbooks to study to capture the knowledge that it would take to get a B.Sc. or M.Sc. in physics, I would definitely cull many of the books I had to wade through during my actual college studies! This is where I am hoping this list will save me from wasting time and money on difficult to use books that are not well-written expositions of their subject matter. As you all know, just like there are many college professors who know their subject matter very, very well, relatively few seem to be effective at teaching their students effectively. Textbooks often seem to follow the same trend. > >> textbooks would be for self-study. So my question is what would be a >> sequence of books to acquire the knowledge I would need that are >> especially well-suited to self-study? > > > Now to turn to this aspect, I'd recommend a few books for general good > practice: > > Structure and Interpretation of Computer Programs > (SICP - available online) > > Code Complete I own this book in its second edition. I had read the first edition a few years ago when I had briefly gotten the "want to program again bug". McConnell made me aware that what programming I had done in college was definitely lacking in many recommended software engineering practices. > Object Oriented Analysis and Design > > Programming Pearls (vols 1 & 2) > Thanks, Alan! It does help. boB From robertvstepp at gmail.com Sat Dec 15 18:34:55 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 15 Dec 2012 11:34:55 -0600 Subject: [Tutor] Recommended texts for self-study to master software engineering? In-Reply-To: References: Message-ID: On Sat, Dec 15, 2012 at 6:48 AM, Wayne Werner wrote: > > Rather than simple self-study, why not take advantage of the offerings by > such folks as Coursera, Edx, or Kahn Academy? > I had briefly looked at Kahn Academy quite a while back, but it did not seem (at that time) to have what I needed. I will re-investigate. I had recently looked at the MIT and Stanford free offerings. I liked the Stanford first course on Intro to C.Sc. and actually started to work through it, until I went into analysis paralysis! Somehow at work, which I am not hired to any programming whatsoever, I have become ever more deeply involved in doing scripts to extend the capabilities and usefulness of the software environment we do our work in, which is wonderful providing the users its own scripting language/environment which is further extendable with external scripts (to the software) using Perl. I say Perl, as this was the only language (beyond Solaris shell scripting) available on the system for doing these things... I am getting off topic! (I did not want to use C, C++ or Java to manipulate text files.) Anyway, I knew that when we eventually upgraded our software and hardware that Python would then become available and I thought that this would give me a better grounding, starting off, in good programming practices than continuing in Perl. So now I am trying learn Python as time becomes available. Anyway, I will look into the suggestions above and see if I can pursue a close to complete track in C.Sc./software engineering. > *Most* of the Python books that I've read are at least as well-suited to > self study as any of the rest of them. Although, my own personal experience > is that contributing to this list has done more to help me really understand > the basics of Python and development than anything else. > I wholeheartedly agree with you here! I now have a collection of Python books that I think will get me to where I want to be with Python. Now I just have to find the time to work through them! And this list teaches me every day even when I do not have any time at all for study. Just reading the posts, which are never onerous in their number each day, constantly teaches. The people on this list go above and beyond in the level of effort they put into their answers to questions. Thanks! boB From ashkan82r at gmail.com Sat Dec 15 20:50:31 2012 From: ashkan82r at gmail.com (Ashkan Rahmani) Date: Sat, 15 Dec 2012 23:20:31 +0330 Subject: [Tutor] Image Processing Message-ID: I have developed some simple c++/qt/opencv application with face detection functionality. for some reasons I'm going to start again them and I wan to use python 3. Unfortunately I found opencv not supported in python 3. 1- as I'm new in python programming, is python 3 good choice for me? 2- Opencv binding will be available for python 3? 3- Is there any other image processing library for python 3? 4- Basically python 3 or 2.7 is suitable for image processing? -- Best Regards, Ashkan R < ashkan82r at gmail.com > From alan.gauld at btinternet.com Sat Dec 15 22:44:45 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 15 Dec 2012 21:44:45 +0000 Subject: [Tutor] Image Processing In-Reply-To: References: Message-ID: On 15/12/12 19:50, Ashkan Rahmani wrote: > 3- Is there any other image processing library for python 3? > 4- Basically python 3 or 2.7 is suitable for image processing? PIL is the defacto standard for image processing on Python. It is officially only available on Python 2.7 (although I think I saw a web page on how to make it work with V3). But in general, if you want industrial strength third party libraries, it's better to stick with v2.7 for now. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From robertvstepp at gmail.com Sun Dec 16 05:16:45 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 15 Dec 2012 22:16:45 -0600 Subject: [Tutor] Trailing spaces affect output in a way I don't understand. Message-ID: In the following code: print( """ ______ ____ ___ ___ ______ / _____| / | / |/ | | ____| | | / /| | / /| /| | | |__ | | _ / ___ | / / |__/ | | | __| | |__| | / / | | / / | | | |___ \______/ /__/ |_| /_/ |_| |______| ______ __ _ ______ ______ / _ \ | | / / | ___| | _ \ | | | | | | / / | |__ | |_| | | | | | | | / / | __| | _ / | |_| | | | / / | |___ | | \ \ \______/ |_____/ |______| |__| \_\ """ ) All trailing spaces have been trimmed off the end of the lines. It results in the following undesired output: ______ ____ ___ ___ ______ / _____| / | / |/ | | ____| | | / /| | / /| /| | | |__ | | _ / ___ | / / |__/ | | | __| | |__| | / / | | / / | | | |___ \______/ /__/ |_| /_/ |_| |______| ______ __ _ ______ ______ / _ \ | | / / | ___| | _ | | | | | | / / | |__ | |_| | | | | | | | / / | __| | _ / | |_| | | | / / | |___ | | \ \______/ |_____/ |______| |__| \_ What greatly puzzles me is that "GAME" prints correctly, but "OVER" does not. Why? If I add trailing spaces to the lines that form "OVER" such that the final space in each such line lines up vertically with the others that form "OVER" then "OVER" prints correctly. I imagine I am being incredibly dense here, but I cannot see why "GAME" prints correctly with all trailing spaces removed, but "OVER" must have the correct number of spaces at the end of each line, so that it works. And the explanation is ??? boB From steve at pearwood.info Sun Dec 16 05:43:51 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 16 Dec 2012 15:43:51 +1100 Subject: [Tutor] Trailing spaces affect output in a way I don't understand. In-Reply-To: References: Message-ID: <50CD5187.7020402@pearwood.info> On 16/12/12 15:16, boB Stepp wrote: > In the following code: > > print( > """ > > > ______ ____ ___ ___ ______ > / _____| / | / |/ | | ____| > | | / /| | / /| /| | | |__ > | | _ / ___ | / / |__/ | | | __| > | |__| | / / | | / / | | | |___ > \______/ /__/ |_| /_/ |_| |______| > > > ______ __ _ ______ ______ > / _ \ | | / / | ___| | _ \ > | | | | | | / / | |__ | |_| | > | | | | | | / / | __| | _ / > | |_| | | | / / | |___ | | \ \ > \______/ |_____/ |______| |__| \_\ > > > > """ > > ) > > All trailing spaces have been trimmed off the end of the lines. It > results in the following undesired output: [...] > What greatly puzzles me is that "GAME" prints correctly, but "OVER" > does not. Why? Wow! This is a tricky question, but so obvious in hindsight. The problem is that you have three lines, all in "OVER", that end with a backslash. In Python string literals, backslash-newline is interpreted as a line continuation, so that the next physical line is joined to the current line. Two solutions are: * Add a space to the end of the backslashes. The space is invisible, and some editors may strip it out, so this is a fragile solution. * Change the string to a raw string, r"""...""" so that backslash interpolation is turned off. -- Steven From robertvstepp at gmail.com Sun Dec 16 05:55:11 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 15 Dec 2012 22:55:11 -0600 Subject: [Tutor] Trailing spaces affect output in a way I don't understand. In-Reply-To: <50CD5187.7020402@pearwood.info> References: <50CD5187.7020402@pearwood.info> Message-ID: On Sat, Dec 15, 2012 at 10:43 PM, Steven D'Aprano wrote: >> What greatly puzzles me is that "GAME" prints correctly, but "OVER" >> does not. Why? > > > Wow! This is a tricky question, but so obvious in hindsight. > > The problem is that you have three lines, all in "OVER", that end with > a backslash. In Python string literals, backslash-newline is interpreted > as a line continuation, so that the next physical line is joined to the > current line. Ah, ha!! It is obvious now that you pointed it out! Many thanks! This was driving me bonkers tonight. > Two solutions are: > > * Add a space to the end of the backslashes. The space is invisible, and > some editors may strip it out, so this is a fragile solution. In my efforts to understand what was going on, I discovered that PyScripter's (My current editor that I am playing around with.) default setting is to trim all line-ending spaces, running into exactly what you say. > * Change the string to a raw string, r"""...""" so that backslash > interpolation is turned off. And I suppose I could also escape the backslash (\\), too. But your suggestion sounds the best. It is funny in retrospect: If I had wanted to create a newline character I would do the correct thing, but seeing the backslashes as part of a picture, even after I went into PyScripter's options and turned on ALL special characters, I was still blind to the fact that a \n was staring me in the face. You are one sharp dude! As seem to be just about all of you... Many thanks! boB From robertvstepp at gmail.com Sun Dec 16 06:02:57 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 15 Dec 2012 23:02:57 -0600 Subject: [Tutor] Trailing spaces affect output in a way I don't understand. In-Reply-To: References: <50CD5187.7020402@pearwood.info> Message-ID: On Sat, Dec 15, 2012 at 10:55 PM, boB Stepp wrote: > It is funny in retrospect: If I had wanted to create a newline > character I would do the correct thing, but seeing the backslashes as > part of a picture, even after I went into PyScripter's options and > turned on ALL special characters, I was still blind to the fact that a > \n was staring me in the face. The above should have been: ...\newline... not \n. I almost missed your point, Steve! boB From eryksun at gmail.com Sun Dec 16 20:55:27 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 16 Dec 2012 14:55:27 -0500 Subject: [Tutor] Trailing spaces affect output in a way I don't understand. In-Reply-To: References: <50CD5187.7020402@pearwood.info> Message-ID: On Sat, Dec 15, 2012 at 11:55 PM, boB Stepp wrote: > On Sat, Dec 15, 2012 at 10:43 PM, Steven D'Aprano wrote: > >> Two solutions are: >> >> * Add a space to the end of the backslashes. The space is invisible, and >> some editors may strip it out, so this is a fragile solution. > > In my efforts to understand what was going on, I discovered that > PyScripter's (My current editor that I am playing around with.) > default setting is to trim all line-ending spaces, running into > exactly what you say. >From PEP 8: Don't write string literals that rely on significant trailing whitespace. Such trailing whitespace is visually indistinguishable and some editors (or more recently, reindent.py) will trim them. >> * Change the string to a raw string, r"""...""" so that backslash >> interpolation is turned off. > > And I suppose I could also escape the backslash (\\), too. But your > suggestion sounds the best. You could also store the string in a text file or database. P.S. For anyone using Gmail's new in-line reply box, the class to style it with a monospace font is .LW-avf. From marszydril at gmail.com Mon Dec 17 04:12:45 2012 From: marszydril at gmail.com (Marefe Serentas) Date: Mon, 17 Dec 2012 11:12:45 +0800 Subject: [Tutor] Get the structure values from a c file In-Reply-To: <50CE8CCD.3080900@gmail.com> References: <50CE8CCD.3080900@gmail.com> Message-ID: <50CE8DAD.2040705@gmail.com> Hi, I need help. I want to create a python script that will store the value of the structure /MainStruct/ from the c file. Suppose my c file looks like this: ---------------------------------------------------------- #define max (3) typedef struct A { int a; char b; }A; typedef struct MainStruct { A var1; int var2; int var3[max]; }Main; void generate() { MainStruct myMain = {0}; myMain.var1.a = 1 myMain.var1.b = 'a' myMain.var2 = 3 myMain.var3[0] = -3 myMain.var3[1] = 6 myMain.var3[2] = 18 } ------------------------------------------------------------- My python script accepts a c file as input. How can I get the values of the structure? Can I call the function generate() in python script, but it doesn't have a return statement, how can I get the values by calling the function? Help me, I'm really new in python. All I could think is just parse the c file. But it seems that it's not a good idea. -------------- next part -------------- An HTML attachment was scrubbed... URL: From robertvstepp at gmail.com Mon Dec 17 05:19:26 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 16 Dec 2012 22:19:26 -0600 Subject: [Tutor] Limitation of int() in converting strings Message-ID: >>> int('10.0') Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: '10.0' >>> int("10") 10 It is apparent that int() does not like strings with floating-point formats. None of my books (as far as my flipping can tell) or the below built-in help clarify this: Help on int object: class int(object) | int(x[, base]) -> integer | | Convert a string or number to an integer, if possible. A floating | point argument will be truncated towards zero (this does not include a | string representation of a floating point number!) When converting a | string, use the optional base. It is an error to supply a base when | converting a non-string. Of course if I type int(float('10.0')) I get the desired 10 . So, I am guessing that to convert strings to integers with int() that the string must already be of integer format? What is the rationale for setting up int() in this manner? Thanks as I continue to puzzle over the fine points of the basics... boB From msirenef at lightbird.net Mon Dec 17 05:41:05 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Sun, 16 Dec 2012 23:41:05 -0500 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: Message-ID: <50CEA261.4040609@lightbird.net> On 12/16/2012 11:19 PM, boB Stepp wrote: >>>> int('10.0') > Traceback (most recent call last): > File "", line 1, in > ValueError: invalid literal for int() with base 10: '10.0' >>>> int("10") > 10 > > It is apparent that int() does not like strings with floating-point > formats. None of my books (as far as my flipping can tell) or the > below built-in help clarify this: > > > Help on int object: > > class int(object) > | int(x[, base]) -> integer > | > | Convert a string or number to an integer, if possible. A floating > | point argument will be truncated towards zero (this does not include a > | string representation of a floating point number!) When converting a > | string, use the optional base. It is an error to supply a base when > | converting a non-string. > > Of course if I type int(float('10.0')) I get the desired 10 . > > So, I am guessing that to convert strings to integers with int() that > the string must already be of integer format? What is the rationale > for setting up int() in this manner? > > Thanks as I continue to puzzle over the fine points of the basics... > boB What would you want to happen for int("10.5")? If 10.0 was accepted, it would be consistent to accept 10.5, too. The issue, I think, is that a simple operation should not go too far beyond what it is supposed to do - if you are sure you are converting a float in a string, you need to do it explicitly, and if you're converting a string to an int and the string is not actually an int, then maybe it wasn't supposed to be a float and it's a mistake in the program -- and therefore python should alert you. -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From d at davea.name Mon Dec 17 05:48:46 2012 From: d at davea.name (Dave Angel) Date: Sun, 16 Dec 2012 23:48:46 -0500 Subject: [Tutor] Get the structure values from a c file In-Reply-To: <50CE8DAD.2040705@gmail.com> References: <50CE8CCD.3080900@gmail.com> <50CE8DAD.2040705@gmail.com> Message-ID: <50CEA42E.8090507@davea.name> On 12/16/2012 10:12 PM, Marefe Serentas wrote: > Hi, I need help. > > I want to create a python script that will store the value of the > structure /MainStruct/ from the c file. > Suppose my c file looks like this: > ---------------------------------------------------------- > #define max (3) > > typedef struct A > { > int a; > char b; > }A; > > typedef struct MainStruct > { > A var1; > int var2; > int var3[max]; > }Main; > > void generate() > { > MainStruct myMain = {0}; > myMain.var1.a = 1 > myMain.var1.b = 'a' > myMain.var2 = 3 > myMain.var3[0] = -3 > myMain.var3[1] = 6 > myMain.var3[2] = 18 > } > ------------------------------------------------------------- > My python script accepts a c file as input. > How can I get the values of the structure? > Can I call the function generate() in python script, but it doesn't > have a return statement, how can I get the values by calling the > function? > Help me, I'm really new in python. > All I could think is just parse the c file. But it seems that it's not > a good idea. Please use plain-text for your message. This time you seem to have gotten away with it, but html messages can cause serious formatting problems, among others garbling your sourcecode. They also inflate the message size by having two copies, only one of which is necessarily visible to the bulk of the mailing list. I can't begin to figure out what your constraints are, or why you are even attempting this problem. So let's see if I can clarify things a bit. First, is it an assignment? If so, could you quote it, so we might get the teacher's perspective? Or is it a work-problem, in which case you should be able to tell us why your customers need it? Or is it a personal challenge, in which case, we should know more about your background, and rationale for going this particular direction? Next, tell us about yourself. Are you an experienced C programmer, but new to Python? Are you experienced at some other language (Lisp ?) and trying to learn some C and more Python? Or what? What OS are you running on, what version of C do you have available, and what version(s) of Python are you going to use? You said you wanted to write a program "that will store the value of the structure /MainStruct/ from the c file" But MainStruct has no values, and never can. It's a structure definition, not a variable. The only values are in the instance myMain, which is instantiated in the function, and tossed at the end. So if you want to write a "Python program that will retrieve (not store) the values of myMain at some particular point in time," then it begins to make sense. Is this C file a single file, that looks exactly like you show us? In that case, simply call a meatware function (human brain) to translate the data according to whatever your rules are. If you're still here, perhaps you're saying that there are thousands of such C files, differing in some way,. and you want the Python code to somehow extract something from a particular one of them. Is there a C library module generated from each such file, so you could call it from Python? Do you have the tools and experience to rebuild it with some changes to the file (like changing the return type of the function)? Do you want help automating those changes and launching the gcc compile? How much are you permitted to change the source before compiling it? Are you allowed to rename each such file to a header file, after editing it? Can you use C++, or are you stuck in C? If someone is generating these thousands of source files, what is the commonality? Do they all define the function generate_and_return() or might some of them call it something else? Are the fields fixed, are the number of nestings fixed, are the rhs of the assignments inside generate() always literals? Are they constrained to either ints or chars? Is the code that's generating these files available to be customized? Perhaps it could just use some other format. Maybe you could write C code that includes one such file and when it's run, it generates a Python module. And finally, what do you mean by "storing" the value? Do you need to custom generate a set of classes that mimic the C structs, and give them similar names (unless you come up with a symbol that's legal in C, but not in Python), or are you looking for something that in some sense represents the concepts? For this example, I see no value in keeping the two structs separate if you're never using them anywhere else. I'd probably just coalesce them. What about types? A C int is constrained to 16 bit (or 32 or 64, depends on compiler version). But a Python int has no upper limit at all (in version 3). A char is not the same between C and Python 3, and even without considering Python, a C char is a compiler-specific type. Default seems to be a signed 8-bit value, but I believe the standard permits unsigned and/or other bit sizes. What about alignment? Quite relevant in C, but very seldom meaningful for Python. But since this may be just a fraction of some larger hunk of code, perhaps those C structs are intended to map to disk formats, where alignment, sizes and byte-sex are very important. Perhaps you want to look at ctypes. -- DaveA From robertvstepp at gmail.com Mon Dec 17 06:00:26 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 16 Dec 2012 23:00:26 -0600 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: <50CEA261.4040609@lightbird.net> References: <50CEA261.4040609@lightbird.net> Message-ID: On Sun, Dec 16, 2012 at 10:41 PM, Mitya Sirenef wrote: > What would you want to happen for int("10.5")? If 10.0 was accepted, > it would be consistent to accept 10.5, too. I was expecting int("10.5") to return 10 . > The issue, I think, is that a simple operation should not go too far > beyond what it is supposed to do - if you are sure you are converting a > float in a string, you need to do it explicitly, and if you're > converting a string to an int and the string is not actually an int, > then maybe it wasn't supposed to be a float and it's a mistake in the > program -- and therefore python should alert you. > And this is why I asked the question. If this is the rationale, it makes sense--an extra bit of double checking of the programmer's intent. Thanks, boB From msirenef at lightbird.net Mon Dec 17 06:16:13 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Mon, 17 Dec 2012 00:16:13 -0500 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: <50CEA261.4040609@lightbird.net> Message-ID: <50CEAA9D.9090403@lightbird.net> On 12/17/2012 12:00 AM, boB Stepp wrote: > On Sun, Dec 16, 2012 at 10:41 PM, Mitya Sirenef wrote: > >> What would you want to happen for int("10.5")? If 10.0 was accepted, >> it would be consistent to accept 10.5, too. > > I was expecting int("10.5") to return 10 . I just want to note that this is what you expect _now_, because this is what you're doing at the moment. If you were parsing a text and a float turned up at an unexpected spot, you may well be unpleasantly surprised if python silently changed it into a numerically quite different number! > > >> The issue, I think, is that a simple operation should not go too far >> beyond what it is supposed to do - if you are sure you are converting a >> float in a string, you need to do it explicitly, and if you're >> converting a string to an int and the string is not actually an int, >> then maybe it wasn't supposed to be a float and it's a mistake in the >> program -- and therefore python should alert you. >> > And this is why I asked the question. If this is the rationale, it > makes sense--an extra bit of double checking of the programmer's > intent. No problem. I'm not certain this is the main/only reason, but I'd expect this reason alone be enough to make this design choice.. -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From alan.gauld at btinternet.com Mon Dec 17 09:41:05 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 17 Dec 2012 08:41:05 +0000 Subject: [Tutor] Get the structure values from a c file In-Reply-To: <50CE8DAD.2040705@gmail.com> References: <50CE8CCD.3080900@gmail.com> <50CE8DAD.2040705@gmail.com> Message-ID: On 17/12/12 03:12, Marefe Serentas wrote: > I want to create a python script that will store the value of the > structure /MainStruct/ from the c file. You will need to be a lot more specific about your requirements. A structure is a data definition, it does not have a value. It is like saying you want to store the value of char. Secondly what do you mean by "from the C file"? Do you mean you want extract the value from the C source file? Or do you mean the data is stored elsewhere and you want to use the definition in the C file to interpret the data? In which case where/how is the data stored? > Suppose my c file looks like this: > ---------------------------------------------------------- snip > typedef struct MainStruct > { > }Main; > > void generate() > { > MainStruct myMain = {0}; Is this valid C? I'd have expected either struct MainStruct myMain or Main myMain But my C is a bit rusty... But if I'm right please post runnable code otherwise we are all guessing. > myMain.var1.a = 1 > myMain.var1.b = 'a' > myMain.var2 = 3 > myMain.var3[0] = -3 > myMain.var3[1] = 6 > myMain.var3[2] = 18 > } > My python script accepts a c file as input. > How can I get the values of the structure? I assume you really mean the values of the myMain variable? But taking the C file as input makes no sense since C files are normally compiled and the resulting binary file executed. So taking the C file as input would imply you want to parse the C source, which you can do more easily by just reading it! > Can I call the function generate() in python script, but it doesn't have > a return statement, how can I get the values by calling the function? You can't call generate() because its not compiled into executable form. All you have is a text file full of C source code. If you have access to the executable there are ways of calling the function but since it always stores the same values there seems little point. > Help me, I'm really new in python. Are you also new in C? Why are you doing this, there is presumably a bigger problem behind this because as it stands it makes no sense. > All I could think is just parse the c file. But it seems that it's not a > good idea. The best parser in this case is your brain. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Dec 17 09:55:05 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 17 Dec 2012 08:55:05 +0000 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: Message-ID: On 17/12/12 04:19, boB Stepp wrote: > It is apparent that int() does not like strings with floating-point > formats. None of my books (as far as my flipping can tell) or the > below built-in help clarify this: > ... > Of course if I type int(float('10.0')) I get the desired 10 . as indeed will int(10.0) So you are right, there is an inconsistency between how int() converts floating point numbers and how it converts strings. Even stranger since the underlying atoi() C function appears to handle float strings quite happily... > So, I am guessing that to convert strings to integers with int() that > the string must already be of integer format? What is the rationale > for setting up int() in this manner? No idea, you'd need to ask Guido, it's his language :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Mon Dec 17 13:56:30 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 17 Dec 2012 07:56:30 -0500 Subject: [Tutor] Get the structure values from a c file In-Reply-To: <50CE8DAD.2040705@gmail.com> References: <50CE8CCD.3080900@gmail.com> <50CE8DAD.2040705@gmail.com> Message-ID: On Sun, Dec 16, 2012 at 10:12 PM, Marefe Serentas wrote: > > I want to create a python script that will store the value of the structure > MainStruct from the c file. Try pycparser to parse C source code. To load a library and call its functions you can use ctypes. http://docs.python.org/2/library/ctypes http://docs.python.org/3/library/ctypes http://code.google.com/p/pycparser Below I adapted your example for ctypes. > #define max (3) > > typedef struct A > { > int a; > char b; > }A; > > typedef struct MainStruct > { > A var1; > int var2; > int var3[max]; > }Main; import ctypes max = 3 class A(ctypes.Structure): _fields_ = [ ('a', ctypes.c_int), ('b', ctypes.c_char), ] class Main(ctypes.Structure): _fields_ = [ ('var1', A), ('var2', ctypes.c_int), ('var3', ctypes.c_int * max), ] Per your typedef, the 2nd structure is named "Main". c_int * max creates an array. > void generate() > { > MainStruct myMain = {0}; > myMain.var1.a = 1 > myMain.var1.b = 'a' > myMain.var2 = 3 > myMain.var3[0] = -3 > myMain.var3[1] = 6 > myMain.var3[2] = 18 > } That would have to be "struct MainStruct myMain". Also, you're missing semicolons on all but the first line. It's simpler to let the caller manage memory. Here's the function written to take a pointer: void generate(Main *myMain) { myMain->var1.a = 1; myMain->var1.b = 'a'; myMain->var2 = 3; myMain->var3[0] = -3; myMain->var3[1] = 6; myMain->var3[2] = 18; } To call this, first load the library (e.g. "lib.so") and define the prototype in Python. Setting "argtypes" is helpful because ctypes type checks arguments. A TypeError is better than a segfault. lib = ctypes.cdll.LoadLibrary('./lib.so') generate = lib.generate generate.argtypes = [ctypes.POINTER(Main)] generate.restype = None >>> myMain = Main() >>> generate(myMain) >>> myMain.var1.a, myMain.var1.b, myMain.var2 (1, 'a', 3) >>> list(myMain.var3) [-3, 6, 18] It passes a pointer to myMain based on the argtypes prototype, but you can also explicitly use generate(ctypes.byref(myMain)). From ufukeskici at gmail.com Mon Dec 17 14:40:21 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Mon, 17 Dec 2012 15:40:21 +0200 Subject: [Tutor] ssh connection Message-ID: Hello All, Can we make an SSH connection with Pyhton 3.3 ? I want to connecto to my router, send commands and receive outputs. Thanks. Ufuk -------------- next part -------------- An HTML attachment was scrubbed... URL: From tktucker at gmail.com Mon Dec 17 14:49:14 2012 From: tktucker at gmail.com (Tom Tucker) Date: Mon, 17 Dec 2012 08:49:14 -0500 Subject: [Tutor] ssh connection In-Reply-To: References: Message-ID: One option is PyExpect http://www.noah.org/python/pexpect/ On Mon, Dec 17, 2012 at 8:40 AM, Ufuk Eskici wrote: > Hello All, > > Can we make an SSH connection with Pyhton 3.3 ? > > I want to connecto to my router, send commands and receive outputs. > > Thanks. > Ufuk > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Mon Dec 17 15:36:04 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 17 Dec 2012 14:36:04 +0000 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: Message-ID: On 17 December 2012 08:55, Alan Gauld wrote: > > On 17/12/12 04:19, boB Stepp wrote: > >> It is apparent that int() does not like strings with floating-point >> formats. None of my books (as far as my flipping can tell) or the >> below built-in help clarify this: >> ... >> >> Of course if I type int(float('10.0')) I get the desired 10 . > > > as indeed will > > int(10.0) > > So you are right, there is an inconsistency between how int() converts floating point numbers and how it converts strings. Even stranger since the underlying atoi() C function appears to handle float strings quite happily... The atoi() function like many of the older C functions has a major flaw in that it indicates an error by returning zero even though zero is actually a possible return value for the function. As far as I can tell it doesn't even set an error code on failure. As a result it is not safe to use without some additional checking either before or after the call. Python's int() function and C's atoi() function also accept and ignore whitespace around the number in the string: >>> int(' 123 ') 123 >>> int('\t\n \n 123 \n ') 123 >>> int('\t\n \n 123 \n 456') Traceback (most recent call last): File "", line 1, in ValueError: invalid literal for int() with base 10: '123 \n 456' In C, atoi() would have allowed that last example and given 123 as the result. Also, are you sure that atoi() is used in CPython? The int() function accepts an optional base argument and can process non-decimal strings: >>> int('0xff', 16) 255 >>> int('0o377', 8) 255 >>> int('0b11111111', 2) 255 >>> int('11111111', 2) 255 >> So, I am guessing that to convert strings to integers with int() that >> the string must already be of integer format? What is the rationale >> for setting up int() in this manner? I think it's unfortunate that Python's int() function combines two distinct behaviours in this way. In different situations int() is used to: 1) Coerce an object of some type other than int into an int without changing the value of the integer that the object represents. 2) Round an object with a non-integer value to an integer value. There are situations where behaviour 1) is required but behaviour 2) is definitely not wanted. The inability to do this safely in Python resulted in PEP 357 [1] that adds an __index__ method to objects that represent integers but are not of type int(). Unfortunately, this was intended for slicing and doesn't help when converting floats and strings to int(). I have often found myself writing awkward functions to prevent a rounding error from occurring when coercing an object with int(). Here's one: def make_int(obj): '''Coerce str, float and int to int without rounding error Accepts strings like '4.0' but not '4.1' ''' fnum = float('%s' % obj) inum = int(fnum) assert inum == fnum return inum References: [1] http://www.python.org/dev/peps/pep-0357/ Oscar From eryksun at gmail.com Mon Dec 17 16:39:51 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 17 Dec 2012 10:39:51 -0500 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: Message-ID: On Mon, Dec 17, 2012 at 3:55 AM, Alan Gauld wrote: > > So you are right, there is an inconsistency between how int() converts > floating point numbers and how it converts strings. Even stranger since the > underlying atoi() C function appears to handle float strings quite > happily... If you're using strtol in C it's up to you how to interpret an incomplete conversion due to an out-of-range number or bad literal for the given base. Python, on the other hand, automatically switches the type for big integers to a multiprecision long (2.x long) and raises ValueError for bad literals. Where you go from there is up to you. BTW, 2.x int() isn't using the libc atoi or strtol/strtoul functions. It has its own implementation in mystrtoul.c. PyOS_strtol wraps the main workhorse PyOS_strtoul (unsigned): http://hg.python.org/cpython/file/70274d53c1dd/Python/mystrtoul.c#l80 The conversion loop proceeds up to the first non-base character, as defined by the table _PyLong_DigitValue in longobject.c: http://hg.python.org/cpython/file/70274d53c1dd/Objects/longobject.c#l1604 A pointer to the last scanned character is set. In PyInt_FromString, if this points to the first character, or the previous character isn't alphanumeric, or removing trailing whitespace starting from this point doesn't end on a NUL terminator (e.g. *end == '.'), then the conversion raises a ValueError. See lines 364-383 in intobject.c: http://hg.python.org/cpython/file/70274d53c1dd/Objects/intobject.c#l340 From brad.hudson at gmail.com Mon Dec 17 17:38:20 2012 From: brad.hudson at gmail.com (Brad Hudson) Date: Mon, 17 Dec 2012 10:38:20 -0600 Subject: [Tutor] ssh connection In-Reply-To: References: Message-ID: Look at the subprocess module. You can send your SSH connections via subprocess and collect the output for parsing. On Mon, Dec 17, 2012 at 7:40 AM, Ufuk Eskici wrote: > Hello All, > > Can we make an SSH connection with Pyhton 3.3 ? > > I want to connecto to my router, send commands and receive outputs. > > Thanks. > Ufuk > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Dec 17 19:00:17 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 17 Dec 2012 18:00:17 +0000 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: Message-ID: On 17/12/12 14:36, Oscar Benjamin wrote: > > Even stranger since the underlying atoi() C function > Also, are you sure that atoi() is used in CPython? Nope, just making assumptions! :-0 As Eryksun points out Python uses its own C code for this. assume == ass u me :-( -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From ramit.prasad at jpmorgan.com Mon Dec 17 19:28:37 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 17 Dec 2012 18:28:37 +0000 Subject: [Tutor] ssh connection In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741809A40D@SCACMX008.exchad.jpmchase.net> Ufuk Eskici wrote: > > Hello All, > > Can we make an SSH connection with Pyhton 3.3 ? > > I want to connecto to my router, send commands and receive outputs. > > Thanks. > Ufuk You can try to use Paramiko. You may need to build it and it seems like it may work except for SFTP. https://github.com/paramiko/paramiko/issues/16 If you switch to Python 2.x you can use Fabric (which relies on Paramiko). ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From eike.welk.lists1 at gmx.de Mon Dec 17 22:39:14 2012 From: eike.welk.lists1 at gmx.de (Eike Welk) Date: Mon, 17 Dec 2012 22:39:14 +0100 Subject: [Tutor] Image Processing In-Reply-To: References: Message-ID: <1487427.rzHSfSsVhb@lixie> Hello Ashkan! On Saturday 15.12.2012 23:20:31 Ashkan Rahmani wrote: > I have developed some simple c++/qt/opencv application with face > detection functionality. > for some reasons I'm going to start again them and I wan to use python 3. > Unfortunately I found opencv not supported in python 3. > 1- as I'm new in python programming, is python 3 good choice for me? Python 2 and Python 3 are very similar. Use the latest version for which all libraries are available, possibly Python 2.7. The biggest difference between the languages for a beginner is: Python 2: print "Hello World" Python 3: print("Hello World") Furthermore Python is a very simple language. As you can already program in C++, it will take you two afternoons to learn Python. Use the tutorial from the official documentation: http://www.python.org/doc/ > 2- Opencv binding will be available for python 3? Ask the makers of Opencv. But I expect that they eventually will switch to Python 3, because Python 2 is not further developed. However not all Linux distributions have currently moved to Python 3: OpenSuse comes with Python 2.7 while Ubuntu has already moved to Python 3. > 3- Is there any other image processing library for python 3? * Numpy is an N-dimensional array library. It is much more low level than Opencv, but can be used to prototype algorithms, that you later implement in C, C++, or Cython. Opencv images can directly be converted to Numpy arrays and vice versa. http://www.numpy.org/ http://www.cython.org/ * Scipy (which needs Numpy) contains some image analysis algorithms, but is still more low level than most of Opencv. http://docs.scipy.org/doc/scipy/reference/tutorial/ndimage.html * PIL is not really suitable for what you want. It is intended for loading and saving images, converting image formats, adjusting colors, and similar tasks. PIL images can also be directly converted to Opencv and Numpy images. http://www.pythonware.com/library/pil/handbook/index.htm > 4- Basically python 3 or 2.7 is suitable for image processing? I think you should use Python 2.7 because Opencv is currently not ported to Python 3. Eike. From ramit.prasad at jpmorgan.com Mon Dec 17 22:54:55 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Mon, 17 Dec 2012 21:54:55 +0000 Subject: [Tutor] ssh connection In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741809A40D@SCACMX008.exchad.jpmchase.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741809A96E@SCACMX008.exchad.jpmchase.net> Please always include the list in your response (I have CC-ed the list). Ufuk Eskici wrote: > > Subprocess seems complicated, didnt understand. > > Also tried pexpect but couldnt install on Windows. Subprocess can be daunting to try and pick up, but it is not as bad as it looks at first glance. Subprocess gives you a way to run something from the cmd prompt (the Windows shell) and send/retrieve data from the prompt. It may be complicated depending on what commands you actually want to run and the type of output you are trying to retrieve. In order to make it work you need to be able to run the commands from cmd. Of course, you need to install an SSH client that will work from CMD prompt. Here are some links that might help. http://stackoverflow.com/questions/2407095/how-to-use-ssh-from-windows-cmd http://community.spiceworks.com/topic/205271-ssh-from-windows-command-line > > 2012/12/17 Prasad, Ramit > > Ufuk Eskici wrote: > > > > > > Hello All, > > > > > > Can we make an SSH connection with Pyhton 3.3 ? > > > > > > I want to connecto to my router, send commands and receive outputs. > > > > > > Thanks. > > > Ufuk > > You can try to use Paramiko. You may need to build it and it seems like > > it may work except for SFTP. > > > > https://github.com/paramiko/paramiko/issues/16 > > > > If you switch to Python 2.x you can use Fabric (which relies on Paramiko). ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From eryksun at gmail.com Mon Dec 17 23:23:04 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 17 Dec 2012 17:23:04 -0500 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: Message-ID: On Mon, Dec 17, 2012 at 1:00 PM, Alan Gauld wrote: > > Python uses its own C code for this. The important point here is that they use the strtol/strtod interface, however it's implemented. atoi and atof lack the end pointer argument that enables raising a ValueError for an incomplete conversion. For example, strtol("123e9", &end, 10) will merrily return 123, but with *end == 'e'. I think it's good that Python raises a ValueError in this case. Errors should never pass silently. Unless explicitly silenced. http://www.python.org/dev/peps/pep-0020/ From marszydril at gmail.com Tue Dec 18 05:18:57 2012 From: marszydril at gmail.com (Marefe Serentas) Date: Tue, 18 Dec 2012 12:18:57 +0800 Subject: [Tutor] Tutor Digest, Vol 106, Issue 42 In-Reply-To: References: Message-ID: <50CFEEB1.8060608@gmail.com> On 12/18/2012 12:38 AM, tutor-request at python.org wrote: > Re: Get the structure values from a c file I apologize for some syntax errors in my c file. I would like to correct my mistakes and append some lines in the c file to make it clearer. ---- #define max (3) #define max_data_size (9600*2*8) typedef unsigned char u8; typedef signed int u32; int some_data[] = { -288, -153, 31, 24, 205, 110, 39, 88, -281, 145, 35, 266, 63, -79, -103, -25, 53, 145, -114, -274, 46, 60, 220, 205 }; typedef struct A { u32 a; u8 b; }A; typedef struct MainStruct { A var1; u32 var2; u32 var3[max]; u32 var4; u32 var5[max]; }MainStruct; void generate() { MainStruct myMain = {0}; myMain.var1.a = 1; myMain.var1.b = 'a'; myMain.var2 = 3; myMain.var3[0] = -3; myMain.var3[1] = 6; myMain.var3[2] = 18; (void)memcpy((void *)myMain.var4, (void *)some_data, sizeof(some_data)); myMain.var5[0] = 1; } ---- This is a work-problem. Given a c file as input, the customer wants me to write a python script that will retrieve the values of myMain. He wants those values converted to binary data and write it in a .bin file. As a result, the size of the bin file is equal to the size of myMain. The c file input content might change in the future like different values assigned, added fields in the MainStruct, etc. Also I am not permitted to change the c file. About me, I'm a fresh graduate. I just started learning python a month ago. I learned c in school, we had it for 2 years. I'm using python 2.6. I'm running on Windows 7 64-bit OS. What I did so far is parse the C source code. But having a problem parsing the value of myMain.var4. From marszydril at gmail.com Tue Dec 18 05:25:22 2012 From: marszydril at gmail.com (Marefe Serentas) Date: Tue, 18 Dec 2012 12:25:22 +0800 Subject: [Tutor] Get the structure values from a c file In-Reply-To: <50CFEEB1.8060608@gmail.com> References: <50CFEEB1.8060608@gmail.com> Message-ID: <50CFF032.1000004@gmail.com> On 12/18/2012 12:38 AM, tutor-request at python.org wrote: > Re: Get the structure values from a c file I apologize for some syntax errors in my c file. I would like to correct my mistakes and append some lines in the c file to make it clearer. ---- #define max (3) #define max_data_size (9600*2*8) typedef unsigned char u8; typedef signed int u32; int some_data[] = { -288, -153, 31, 24, 205, 110, 39, 88, -281, 145, 35, 266, 63, -79, -103, -25, 53, 145, -114, -274, 46, 60, 220, 205 }; typedef struct A { u32 a; u8 b; }A; typedef struct MainStruct { A var1; u32 var2; u32 var3[max]; u32 var4; u32 var5[max]; }MainStruct; void generate() { MainStruct myMain = {0}; myMain.var1.a = 1; myMain.var1.b = 'a'; myMain.var2 = 3; myMain.var3[0] = -3; myMain.var3[1] = 6; myMain.var3[2] = 18; (void)memcpy((void *)myMain.var4, (void *)some_data, sizeof(some_data)); myMain.var5[0] = 1; } ---- This is a work-problem. Given a c file as input, the customer wants me to write a python script that will retrieve the values of myMain. He wants those values converted to binary data and write it in a .bin file. As a result, the size of the bin file is equal to the size of myMain. The c file input content might change in the future like different values assigned, added fields in the MainStruct, etc. Also I am not permitted to change the c file. About me, I'm a fresh graduate. I just started learning python a month ago. I learned c in school, we had it for 2 years. I'm using python 2.6. I'm running on Windows 7 64-bit OS. What I did so far is parse the C source code. But having a problem parsing the value of myMain.var4. From ashkan82r at gmail.com Tue Dec 18 07:56:17 2012 From: ashkan82r at gmail.com (Ashkan Rahmani) Date: Tue, 18 Dec 2012 10:26:17 +0330 Subject: [Tutor] Image Processing In-Reply-To: <1487427.rzHSfSsVhb@lixie> References: <1487427.rzHSfSsVhb@lixie> Message-ID: Hello Eike, Thank you for reply... On Tue, Dec 18, 2012 at 1:09 AM, Eike Welk wrote: > Hello Ashkan! > > > On Saturday 15.12.2012 23:20:31 Ashkan Rahmani wrote: >> I have developed some simple c++/qt/opencv application with face >> detection functionality. >> for some reasons I'm going to start again them and I wan to use python 3. >> Unfortunately I found opencv not supported in python 3. >> 1- as I'm new in python programming, is python 3 good choice for me? > > Python 2 and Python 3 are very similar. Use the latest version for which all > libraries are available, possibly Python 2.7. The biggest difference between > the languages for a beginner is: > > Python 2: > print "Hello World" > > Python 3: > print("Hello World") > > Furthermore Python is a very simple language. As you can already program in > C++, it will take you two afternoons to learn Python. Use the tutorial from > the official documentation: > > http://www.python.org/doc/ > since I asked these questions in this mailing list, I decided to start with 2.7, beside it, I installed 3.3 from source to port my learning to latest version. http://www.python.org/doc/ Yes, the best start point is this link! > >> 2- Opencv binding will be available for python 3? > > Ask the makers of Opencv. But I expect that they eventually will switch to > Python 3, because Python 2 is not further developed. > > However not all Linux distributions have currently moved to Python 3: OpenSuse > comes with Python 2.7 while Ubuntu has already moved to Python 3. > may be the best library/tools for industrial level image processing is OpenCV, Nothing an compare to it. > >> 3- Is there any other image processing library for python 3? > > * Numpy is an N-dimensional array library. It is much more low level than > Opencv, but can be used to prototype algorithms, that you later implement in > C, C++, or Cython. Opencv images can directly be converted to Numpy arrays and > vice versa. > > http://www.numpy.org/ > http://www.cython.org/ > > * Scipy (which needs Numpy) contains some image analysis algorithms, but is > still more low level than most of Opencv. > > http://docs.scipy.org/doc/scipy/reference/tutorial/ndimage.html > as you said, these are really low level. If I have to stick to python maybe I use theme. > * PIL is not really suitable for what you want. It is intended for loading and > saving images, converting image formats, adjusting colors, and similar tasks. > PIL images can also be directly converted to Opencv and Numpy images. > > http://www.pythonware.com/library/pil/handbook/index.htm > you are righ, it seems PIL is good for prepare input data befor processing. > >> 4- Basically python 3 or 2.7 is suitable for image processing? > > I think you should use Python 2.7 because Opencv is currently not ported to > Python 3. > I goggled, Beside python ruby have some good features, I don't know what to do, Now I want to learn python, it's really great for develop. > > Eike. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Best Regards, Ashkan R < ashkan82r at gmail.com > From __peter__ at web.de Tue Dec 18 08:56:47 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 18 Dec 2012 08:56:47 +0100 Subject: [Tutor] Tutor Digest, Vol 106, Issue 42 References: <50CFEEB1.8060608@gmail.com> Message-ID: Marefe Serentas wrote: > On 12/18/2012 12:38 AM, tutor-request at python.org wrote: >> Re: Get the structure values from a c file > I apologize for some syntax errors in my c file. I would like to correct > my mistakes and append some lines in the c file to make it clearer. > > > ---- > #define max (3) > #define max_data_size (9600*2*8) > > typedef unsigned char u8; > typedef signed int u32; > > int some_data[] = > { > -288, -153, 31, 24, 205, 110, 39, 88, > -281, 145, 35, 266, 63, -79, -103, -25, > 53, 145, -114, -274, 46, 60, 220, 205 > }; > > typedef struct A > { > u32 a; > u8 b; > }A; > > typedef struct MainStruct > { > A var1; > u32 var2; > u32 var3[max]; > u32 var4; > u32 var5[max]; > }MainStruct; > > void generate() > { > MainStruct myMain = {0}; > myMain.var1.a = 1; > myMain.var1.b = 'a'; > myMain.var2 = 3; > myMain.var3[0] = -3; > myMain.var3[1] = 6; > myMain.var3[2] = 18; > > (void)memcpy((void *)myMain.var4, > (void *)some_data, > sizeof(some_data)); > > myMain.var5[0] = 1; > } > > ---- > > > This is a work-problem. Given a c file as input, the customer wants me > to write a python script that will retrieve the values of myMain. > He wants those values converted to binary data and write it in a .bin > file. As a result, the size of the bin file is equal to the size of > myMain. The c file input content might change in the future like > different values assigned, added fields in the MainStruct, etc. Also I > am not permitted to change the c file. > > > About me, I'm a fresh graduate. I just started learning python a month > ago. I learned c in school, we had it for 2 years. > > > I'm using python 2.6. I'm running on Windows 7 64-bit OS. > > What I did so far is parse the C source code. > But having a problem parsing the value of myMain.var4. How about running the C source with an added dump() function dump(MainStruct *s, char *filename) { FILE * f = fopen(filename, "wb"); fwrite(f, sizeof(MainStruct), 1, f); fclose(f); } in the right places? From ufukeskici at gmail.com Tue Dec 18 09:03:52 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Tue, 18 Dec 2012 10:03:52 +0200 Subject: [Tutor] ssh connection In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741809A96E@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF4741809A40D@SCACMX008.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF4741809A96E@SCACMX008.exchad.jpmchase.net> Message-ID: I have Putty and I it runs from CMD successfully. *C:\Users\eufuesk>*"c:\Program Files\Putty\putty.exe" -ssh [username]@ 10.0.0.1 -pw [password] But I dont know how to send this command from Pyhton. :( 2012/12/17 Prasad, Ramit > Please always include the list in your response (I have CC-ed the list). > > Ufuk Eskici wrote: > > > > Subprocess seems complicated, didnt understand. > > > > Also tried pexpect but couldnt install on Windows. > > Subprocess can be daunting to try and pick up, but it is > not as bad as it looks at first glance. Subprocess gives you > a way to run something from the cmd prompt (the Windows shell) > and send/retrieve data from the prompt. It may be complicated > depending on what commands you actually want to run and the type > of output you are trying to retrieve. > > In order to make it work you need to be able to run the > commands from cmd. Of course, you need to install an SSH > client that will work from CMD prompt. > > Here are some links that might help. > http://stackoverflow.com/questions/2407095/how-to-use-ssh-from-windows-cmd > http://community.spiceworks.com/topic/205271-ssh-from-windows-command-line > > > > > > 2012/12/17 Prasad, Ramit > > > Ufuk Eskici wrote: > > > > > > > > Hello All, > > > > > > > > Can we make an SSH connection with Pyhton 3.3 ? > > > > > > > > I want to connecto to my router, send commands and receive outputs. > > > > > > > > Thanks. > > > > Ufuk > > > You can try to use Paramiko. You may need to build it and it seems like > > > it may work except for SFTP. > > > > > > https://github.com/paramiko/paramiko/issues/16 > > > > > > If you switch to Python 2.x you can use Fabric (which relies on > Paramiko). > > > ~Ramit > > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of > securities, accuracy and completeness of information, viruses, > confidentiality, legal privilege, and legal entity disclaimers, > available at http://www.jpmorgan.com/pages/disclosures/email. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Tue Dec 18 09:32:41 2012 From: d at davea.name (Dave Angel) Date: Tue, 18 Dec 2012 03:32:41 -0500 Subject: [Tutor] Get the structure values from a c file In-Reply-To: <50CFF032.1000004@gmail.com> References: <50CFEEB1.8060608@gmail.com> <50CFF032.1000004@gmail.com> Message-ID: <50D02A29.9090600@davea.name> On 12/17/2012 11:25 PM, Marefe Serentas wrote: > On 12/18/2012 12:38 AM, tutor-request at python.org wrote: >> Re: Get the structure values from a c file > I apologize for some syntax errors in my c file. I would like to correct > my mistakes and append some lines in the c file to make it clearer. > > > ---- > #define max (3) > #define max_data_size (9600*2*8) > > typedef unsigned char u8; > typedef signed int u32; > > int some_data[] = > { > -288, -153, 31, 24, 205, 110, 39, 88, > -281, 145, 35, 266, 63, -79, -103, -25, > 53, 145, -114, -274, 46, 60, 220, 205 > }; > > typedef struct A > { > u32 a; > u8 b; > }A; > > typedef struct MainStruct > { > A var1; > u32 var2; > u32 var3[max]; > u32 var4; > u32 var5[max]; > }MainStruct; > > void generate() > { > MainStruct myMain = {0}; > myMain.var1.a = 1; > myMain.var1.b = 'a'; > myMain.var2 = 3; > myMain.var3[0] = -3; > myMain.var3[1] = 6; > myMain.var3[2] = 18; > > (void)memcpy((void *)myMain.var4, > (void *)some_data, > sizeof(some_data)); > > myMain.var5[0] = 1; > } > > ---- > > > This is a work-problem. Given a c file as input, the customer wants me > to write a python script that will retrieve the values of myMain. > He wants those values converted to binary data and write it in a .bin > file. As a result, the size of the bin file is equal to the size of > myMain. The c file input content might change in the future like > different values assigned, added fields in the MainStruct, etc. Also I > am not permitted to change the c file. > > > About me, I'm a fresh graduate. I just started learning python a month > ago. I learned c in school, we had it for 2 years. > > > I'm using python 2.6. I'm running on Windows 7 64-bit OS. > > What I did so far is parse the C source code. > But having a problem parsing the value of myMain.var4. > I'd assume you might have trouble automatically deciding the values for myMain.var5[1] and myMain.var5[2]. (They are probably 31 and 24, respectively) And of course, if this function is actually called (assuming you supply a simple main.c elsewhere that calls it), it'll probably crash when the return address gets overwritten. Further, even the size of the binary file will depend on compiler version and switches. When I went to my first Boy Scout camporee, they sent me out to find a left-handed smoke-shifter. Your customer is pulling your leg. -- DaveA From alan.gauld at btinternet.com Tue Dec 18 09:51:18 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 18 Dec 2012 08:51:18 +0000 Subject: [Tutor] ssh connection In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741809A40D@SCACMX008.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF4741809A96E@SCACMX008.exchad.jpmchase.net> Message-ID: On 18/12/12 08:03, Ufuk Eskici wrote: > I have Putty and I it runs from CMD successfully. > > *C:\Users\eufuesk>*"c:\Program Files\Putty\putty.exe" -ssh > [username]@10.0.0.1 > -pw [password] > > But I dont know how to send this command from Pyhton. :( Have you read the subprocess module documentation? It provides many examples of running commands. The raw Popen class can be a bit intimidating but there are wrapper functions that make it easier for simple applications. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Dec 18 10:16:02 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 18 Dec 2012 09:16:02 +0000 Subject: [Tutor] Get the structure values from a c file In-Reply-To: <50CFF032.1000004@gmail.com> References: <50CFEEB1.8060608@gmail.com> <50CFF032.1000004@gmail.com> Message-ID: On 18/12/12 04:25, Marefe Serentas wrote: > int some_data[] = > { > -288, -153, 31, 24, 205, 110, 39, 88, > -281, 145, 35, 266, 63, -79, -103, -25, > 53, 145, -114, -274, 46, 60, 220, 205 > }; > > typedef struct MainStruct > { > A var1; > u32 var2; > u32 var3[max]; > u32 var4; > u32 var5[max]; > }MainStruct; > > void generate() > { > MainStruct myMain = {0}; ... > > (void)memcpy((void *)myMain.var4, > (void *)some_data, > sizeof(some_data)); Won't this probably crash? It's trying to copy a table of data into an uninitialised pointer. > This is a work-problem. Given a c file as input, the customer wants me > to write a python script that will retrieve the values of myMain. The values at what point in time? MyMain only exists for the lifetime of the generate function. It gets thrown away. > He wants those values converted to binary data and write it in a .bin > file. Is there a real purpose for this exercise? It sounds like somebody is trying to do something in a very convoluted way that almost certainly has a better solution. Parsing a C file to try and guess what its internal data structures contain and write it to a file - so that some other program can read it maybe? Do you even know what the machine architecture and compiler are? > myMain. The c file input content might change in the future like > different values assigned, added fields in the MainStruct, etc. Also I > am not permitted to change the c file. That explains the desire to write a parser. But unless the changes to the C file are very restricted it is a futile task. You would need to build a full C language parser that exactly matches the C compiler that's being used (presumably) to build the program to read the bin file. And that's nearly impossible because C is not well defined, being full of implementation specific details. > About me, I'm a fresh graduate. I just started learning python a month > ago. I learned c in school, we had it for 2 years. > > I'm using python 2.6. I'm running on Windows 7 64-bit OS. > What I did so far is parse the C source code. > But having a problem parsing the value of myMain.var4. var4 will be a pointer which is an essentially random memory location. I assume what you really want is the content that var4 points to, which should be a copy of some_data. If it weren't for the dynamic nature of the C file I'd still suggest using your brain as parser and just writing out the *intended* data using the struct module. But if its dynamic you will need to try to parse it and hope nobody changes it too much. Still sounds like the wrong design pattern to me! C is a terrible data file format:-( -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Dec 18 10:20:01 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 18 Dec 2012 09:20:01 +0000 Subject: [Tutor] Tutor Digest, Vol 106, Issue 42 In-Reply-To: References: <50CFEEB1.8060608@gmail.com> Message-ID: On 18/12/12 07:56, Peter Otten wrote: >> myMain. The c file input content might change in the future like >> different values assigned, added fields in the MainStruct, etc. Also I >> am not permitted to change the c file. >> > How about running the C source with an added dump() function > That would have been my suggestion but he says he can't change the file. Although one option might be to get Python to generate a *copy* of the C file with a dump function added. And then compile a C driver program to use that new C file and write out a copy of the data from generate(). Actually that might be the best approach... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From shantanoo at gmail.com Tue Dec 18 10:26:28 2012 From: shantanoo at gmail.com (=?UTF-8?B?4KS24KSC4KSk4KSo4KWC?=) Date: Tue, 18 Dec 2012 20:26:28 +1100 Subject: [Tutor] ssh connection In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741809A40D@SCACMX008.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF4741809A96E@SCACMX008.exchad.jpmchase.net> Message-ID: <50D036C4.4070700@gmail.com> On 18/12/12 7:51 PM, Alan Gauld wrote: > On 18/12/12 08:03, Ufuk Eskici wrote: >> I have Putty and I it runs from CMD successfully. >> >> *C:\Users\eufuesk>*"c:\Program Files\Putty\putty.exe" -ssh >> [username]@10.0.0.1 >> -pw [password] >> >> But I dont know how to send this command from Pyhton. :( > > Have you read the subprocess module documentation? > It provides many examples of running commands. > > The raw Popen class can be a bit intimidating but there are wrapper > functions that make it easier for simple applications. > You may also like to have a look at 'commands' module, especially when no user input is required. -- ????? From alan.gauld at btinternet.com Tue Dec 18 11:43:44 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 18 Dec 2012 10:43:44 +0000 Subject: [Tutor] ssh connection In-Reply-To: <50D036C4.4070700@gmail.com> References: <5B80DD153D7D744689F57F4FB69AF4741809A40D@SCACMX008.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF4741809A96E@SCACMX008.exchad.jpmchase.net> <50D036C4.4070700@gmail.com> Message-ID: On 18/12/12 09:26, ????? wrote: > You may also like to have a look at 'commands' module, especially when > no user input is required. I agree commands is easy to use but it is deprecated in favour of subprocess. And I suspect in this case the use will be interactive. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Tue Dec 18 13:42:20 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 18 Dec 2012 13:42:20 +0100 Subject: [Tutor] Tutor Digest, Vol 106, Issue 42 References: <50CFEEB1.8060608@gmail.com> Message-ID: Alan Gauld wrote: > On 18/12/12 07:56, Peter Otten wrote: > >>> myMain. The c file input content might change in the future like >>> different values assigned, added fields in the MainStruct, etc. Also I >>> am not permitted to change the c file. >>> >> How about running the C source with an added dump() function >> > > That would have been my suggestion but he says he can't change the file. > > Although one option might be to get Python to generate a *copy* of the C > file with a dump function added. And then compile a C driver program to > use that new C file and write out a copy of the data from generate(). > > Actually that might be the best approach... If the customer is real the OP should at least talk to him. Of course I don't know whose money is being burnt, but I expect that neither side can afford to build a C interpreter in Python... From fomcl at yahoo.com Tue Dec 18 14:13:58 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 18 Dec 2012 05:13:58 -0800 (PST) Subject: [Tutor] sys.getfilesystemencoding() Message-ID: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> Hi, ? I am trying to write a file with a 'foreign' unicode name (I am aware that this is a highly western-o-centric way of putting it). In Linux, I can encode it to utf-8 and the file name is displayed correctly. In windows xp, the characters can, apparently, not be represented in this encoding called 'mbcs'. How can I write file names that are always encoded correctly on any platform? Or is this a shortcoming of Windows? ? # Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on win32 import sys ? def _encodeFileName(fn): ??? """Helper function to encode unicode file names into system file names. ??? http://effbot.org/pyref/sys.getfilesystemencoding.htm""" ??? isWindows = sys.platform.startswith("win") ??? isUnicode = isinstance(fn, unicode) ??? if isUnicode:? # and not isWindows ??????? encoding = sys.getfilesystemencoding()? # 'mbcs' on Windows, 'utf-8'?on Linux ??????? encoding = "utf-8" if not encoding else encoding ??????? return fn.encode(encoding) ??? return fn ? fn = u'\u0c0f\u0c2e\u0c02\u0c21\u0c40' + '.txt'?? # Telugu language with open(_encodeFileName(fn), "wb") as w: ??? w.write("yaay!\n")?? # the characters of the FILE NAME can not be represented in the encoding (squares/tofu) ??? print "written: ", w.name ? Thank you very much in advance! Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? From __peter__ at web.de Tue Dec 18 14:40:51 2012 From: __peter__ at web.de (Peter Otten) Date: Tue, 18 Dec 2012 14:40:51 +0100 Subject: [Tutor] sys.getfilesystemencoding() References: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: Albert-Jan Roskam wrote: > I am trying to write a file with a 'foreign' unicode name (I am aware that > this is a highly western-o-centric way of putting it). In Linux, I can > encode it to utf-8 and the file name is displayed correctly. In windows > xp, the characters can, apparently, not be represented in this encoding > called 'mbcs'. How can I write file names that are always encoded > correctly on any platform? Or is this a shortcoming of Windows? > > # Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit > # (Intel)] on win32 > import sys > > > def _encodeFileName(fn): > """Helper function to encode unicode file names into system file names. > http://effbot.org/pyref/sys.getfilesystemencoding.htm""" > isWindows = sys.platform.startswith("win") > isUnicode = isinstance(fn, unicode) > if isUnicode: # and not isWindows > encoding = sys.getfilesystemencoding() # 'mbcs' on Windows, 'utf-8' on > Linux encoding = "utf-8" if not encoding else encoding > return fn.encode(encoding) > return fn > > fn = u'\u0c0f\u0c2e\u0c02\u0c21\u0c40' + '.txt' # Telugu language > with open(_encodeFileName(fn), "wb") as w: > w.write("yaay!\n") # the characters of the FILE NAME can not be > represented in the encoding (squares/tofu) print "written: ", w.name > > Thank you very much in advance! I don't understand your question. Do you get an error if you pass the unicde filename directly? fn = u'\u0c0f\u0c2e\u0c02\u0c21\u0c40.txt' with open(fn, "wb") as f: ... If so, I don't think there's a way around that. On the other hand, if your file manager displays the name as squares it may be that the characters aren't available in the font used the show the name. From eryksun at gmail.com Tue Dec 18 14:52:54 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 18 Dec 2012 08:52:54 -0500 Subject: [Tutor] sys.getfilesystemencoding() In-Reply-To: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On Tue, Dec 18, 2012 at 8:13 AM, Albert-Jan Roskam wrote: > > # Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) > [MSC v.1500 32 bit (Intel)] on win32 > > import sys > > def _encodeFileName(fn): > """Helper function to encode unicode file names into system file names. > http://effbot.org/pyref/sys.getfilesystemencoding.htm""" > isWindows = sys.platform.startswith("win") > isUnicode = isinstance(fn, unicode) > if isUnicode: # and not isWindows > encoding = sys.getfilesystemencoding() # 'mbcs' on Windows, 'utf-8' on Linux > encoding = "utf-8" if not encoding else encoding > return fn.encode(encoding) > return fn Use unicode filenames. CPython encodes them with the file system encoding, with an exception to use the internal UTF-16 on Windows. See fileobject.c (2.6.4 links): file_init (2170-2185): http://hg.python.org/cpython/file/8803c3d61da2/Objects/fileobject.c#l2150 open_the_file (269-283): http://hg.python.org/cpython/file/8803c3d61da2/Objects/fileobject.c#l230 From oscar.j.benjamin at gmail.com Tue Dec 18 15:18:30 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 18 Dec 2012 14:18:30 +0000 Subject: [Tutor] sys.getfilesystemencoding() In-Reply-To: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On 18 December 2012 13:13, Albert-Jan Roskam wrote: > I am trying to write a file with a 'foreign' unicode name (I am aware that this is a highly western-o-centric way of putting it). In Linux, I can encode it to utf-8 and the file name is displayed correctly. In windows xp, the characters can, apparently, not be represented in this encoding called 'mbcs'. How can I write file names that are always encoded correctly on any platform? Or is this a shortcoming of Windows? Eryksun has already explained the fix, but I'll answer this question. It is not a short coming of Windows itself but rather a shortcoming of the older ASCII based Win32 API that is being used by CPython. A long time ago Windows obtained a newer improved "Unicode API" that supports what at that time was considered to be Unicode. In Python, for backward compatibility, the older API is used when you pass a byte string as a filename. I believe this is the same in both recent 2.x versions and 3.x versions of CPython. The problem here is precisely that fact that you are encoding the filename, rather than passing the unicode string directly to open(). This also isn't necessary on Linux unless you want to encode the filename with something other than the sys.getfilesystemencoding(). Oscar From fomcl at yahoo.com Tue Dec 18 15:38:40 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 18 Dec 2012 06:38:40 -0800 (PST) Subject: [Tutor] sys.getfilesystemencoding() In-Reply-To: References: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: <1355841520.74729.YahooMailNeo@web163802.mail.gq1.yahoo.com> ----- Original Message ----- > From: eryksun > To: Albert-Jan Roskam > Cc: Python Mailing List > Sent: Tuesday, December 18, 2012 2:52 PM > Subject: Re: [Tutor] sys.getfilesystemencoding() > > On Tue, Dec 18, 2012 at 8:13 AM, Albert-Jan Roskam > wrote: >> >> # Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) >> ? [MSC v.1500 32 bit (Intel)] on win32 >> >> import sys >> >> def _encodeFileName(fn): >> ? ? """Helper function to encode unicode file names into > system file names. >> ? ? http://effbot.org/pyref/sys.getfilesystemencoding.htm""" >> ? ? isWindows = sys.platform.startswith("win") >> ? ? isUnicode = isinstance(fn, unicode) >> ? ? if isUnicode:? # and not isWindows >> ? ? ? ? encoding = sys.getfilesystemencoding()? # 'mbcs' on Windows, > 'utf-8' on Linux >> ? ? ? ? encoding = "utf-8" if not encoding else encoding >> ? ? ? ? return fn.encode(encoding) >> ? ? return fn > > > Use unicode filenames. CPython encodes them with the file system > encoding, with an exception to use the internal UTF-16 on Windows. See > fileobject.c (2.6.4 links): > > file_init (2170-2185): > http://hg.python.org/cpython/file/8803c3d61da2/Objects/fileobject.c#l2150 > > open_the_file (269-283): > http://hg.python.org/cpython/file/8803c3d61da2/Objects/fileobject.c#l230 ? ? Hi Peter, Eryksun, ? Thank you for your replies. My code example was simplified, maybe too simplified. The parameter actually is a parameter of?a C function?(c_char_p). So I have to encode it as a byte string. ? Good to know though that when writing files 'the regular way' things are a bit easier. Judging from the squares/tofu, the symbols can not be represented on my system. I installed a telugu.ttf to see it that would solve it, but unless I need to reboot first, it doesn't. ? Why I want to do this, you ask? Just 'because'. ;-) It's fun to try and make one's program crash and look for the solution. ? Thanks again guys, I appreciate it. ? Albert-Jan From eryksun at gmail.com Tue Dec 18 16:07:49 2012 From: eryksun at gmail.com (eryksun) Date: Tue, 18 Dec 2012 10:07:49 -0500 Subject: [Tutor] sys.getfilesystemencoding() In-Reply-To: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On Tue, Dec 18, 2012 at 8:13 AM, Albert-Jan Roskam wrote: > >In windows xp, the characters can, apparently, not be represented >in this encoding called 'mbcs'. MBCS (multibyte character set) refers to the locale encoding on Windows. CPython encodes to MBCS via the Win32 function WideCharToMultiByte, with the CP_ACP code page (i.e. the system default 'ANSI' encoding): unicodeobject.c, encode_mbcs (3877-3884): http://hg.python.org/cpython/file/8803c3d61da2/Objects/unicodeobject.c#l3843 WideCharToMultiByte: http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130 MBCS could be a double byte encoding such as code page 936 (simplified Chinese): http://msdn.microsoft.com/en-US/goglobal/cc305153 But generally if you're in the West it'll be Windows 1252: http://en.wikipedia.org/wiki/Windows-1252 The Windows API, registry, and NT file system use UTF-16, with support for legacy code pages. System calls that end in W are the (W)ide interface. Calls that end in "A" are the ANSI wrappers. For example, Kernel32 has both CreateFileW (UTF-16) and CreateFileA (ANSI). In C, one uses CreateFile without the suffix; it depends on preprocessor definitions. With ctypes you'd likely use windll.kernel32.CreatFileW with "utf-16le" or "utf-16be" encoded arguments. From rail.shafigulin at gmail.com Tue Dec 18 18:54:01 2012 From: rail.shafigulin at gmail.com (rail shafigulin) Date: Tue, 18 Dec 2012 12:54:01 -0500 Subject: [Tutor] optparse.OptionParser options in alphabetical order in help display Message-ID: Does anybody know if there is a way to make --help option to display options in alphabetical order? Right now it displays options in the order I added them. I'm using Python 3.1 Any help is appreciated. Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at alchemy.com Tue Dec 18 19:08:06 2012 From: steve at alchemy.com (Steve Willoughby) Date: Tue, 18 Dec 2012 10:08:06 -0800 Subject: [Tutor] optparse.OptionParser options in alphabetical order in help display In-Reply-To: References: Message-ID: Although it is probably too obvious to be the answer you're looking for, why can't you just add them in order in the source code? This way, you can arrange them however you want them to appear, instead of python arbitrarily enforcing its own order. Python likes being explicit about things like that. rail shafigulin wrote: >Does anybody know if there is a way to make --help option to display >options in alphabetical order? Right now it displays options in the >order I >added them. I'm using Python 3.1 > >Any help is appreciated. >Thanks. > > >------------------------------------------------------------------------ > >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. -------------- next part -------------- An HTML attachment was scrubbed... URL: From modulok at gmail.com Tue Dec 18 19:39:24 2012 From: modulok at gmail.com (Modulok) Date: Tue, 18 Dec 2012 11:39:24 -0700 Subject: [Tutor] optparse.OptionParser options in alphabetical order in help display In-Reply-To: References: Message-ID: >> Does anybody know if there is a way to make --help option to display >> options in alphabetical order? I know this sounds like spam and doesn't answer your question, (sorry!) but you should check out the third part module 'docopt'. It is *extremely* simple and a lot more flexible. It made me actually want to write command line interfaces again. http://www.youtube.com/watch?v=pXhcPJK5cMc http://docopt.org/ Again, sorry for the spam but everyone I've shown has switched and were glad they did. It's also on PyPi and github. In fact, the github page is probably the best place to check it out. -Modulok- On 12/18/12, rail shafigulin wrote: > Does anybody know if there is a way to make --help option to display > options in alphabetical order? Right now it displays options in the order I > added them. I'm using Python 3.1 > > Any help is appreciated. > Thanks. > From ramit.prasad at jpmorgan.com Tue Dec 18 21:06:35 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Tue, 18 Dec 2012 20:06:35 +0000 Subject: [Tutor] Image Processing In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741809DB25@SCACMX008.exchad.jpmchase.net> Ashkan Rahmani wrote: > > I have developed some simple c++/qt/opencv application with face > detection functionality. > for some reasons I'm going to start again them and I wan to use python 3. > Unfortunately I found opencv not supported in python 3. > 1- as I'm new in python programming, is python 3 good choice for me? > 2- Opencv binding will be available for python 3? > 3- Is there any other image processing library for python 3? > 4- Basically python 3 or 2.7 is suitable for image processing? > This link from the python mail list might help you understand the current state of image processing in python. http://article.gmane.org/gmane.comp.python.general/722247 Note the forum recommendation as well. Probably a good place to ask more detailed questions. Hopefully-helpful, Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From rail.shafigulin at gmail.com Tue Dec 18 22:08:30 2012 From: rail.shafigulin at gmail.com (rail shafigulin) Date: Tue, 18 Dec 2012 16:08:30 -0500 Subject: [Tutor] optparse.OptionParser options in alphabetical order in help display In-Reply-To: References: Message-ID: On Tue, Dec 18, 2012 at 1:39 PM, Modulok wrote: > >> Does anybody know if there is a way to make --help option to display > >> options in alphabetical order? > > I know this sounds like spam and doesn't answer your question, (sorry!) > but you > should check out the third part module 'docopt'. It is *extremely* simple > and a > lot more flexible. It made me actually want to write command line > interfaces > again. > > > http://www.youtube.com/watch?v=pXhcPJK5cMc > http://docopt.org/ > > Again, sorry for the spam but everyone I've shown has switched and were > glad > they did. It's also on PyPi and github. In fact, the github page is > probably the > best place to check it out. > > -Modulok- > > On 12/18/12, rail shafigulin wrote: > > Does anybody know if there is a way to make --help option to display > > options in alphabetical order? Right now it displays options in the > order I > > added them. I'm using Python 3.1 > > > > Any help is appreciated. > > Thanks. > > > Modulok, Thanks for the link. It is really a great module. I'll will incorporate it into my coding. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rail.shafigulin at gmail.com Tue Dec 18 21:39:17 2012 From: rail.shafigulin at gmail.com (rail shafigulin) Date: Tue, 18 Dec 2012 15:39:17 -0500 Subject: [Tutor] optparse.OptionParser options in alphabetical order in help display In-Reply-To: References: Message-ID: On Tue, Dec 18, 2012 at 1:08 PM, Steve Willoughby wrote: > Although it is probably too obvious to be the answer you're looking for, > why can't you just add them in order in the source code? > > This way, you can arrange them however you want them to appear, instead of > python arbitrarily enforcing its own order. Python likes being explicit > about things like that. > > rail shafigulin wrote: >> >> Does anybody know if there is a way to make --help option to display >> options in alphabetical order? Right now it displays options in the order I >> added them. I'm using Python 3.1 >> >> Any help is appreciated. >> Thanks. >> >> ------------------------------ >> >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > -- > Sent from my Android phone with K-9 Mail. Please excuse my brevity. > I currently have it done this way. It just seems a bit odd that I have to arrange it manually. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ginarf at comcast.net Wed Dec 19 02:05:37 2012 From: ginarf at comcast.net (Gina) Date: Tue, 18 Dec 2012 19:05:37 -0600 Subject: [Tutor] I cant fix this bug Message-ID: <50D112E1.7020209@comcast.net> I have Python version 3. when the program begins, it prints out the main menu and then asks you for your choice - just like it is supposed to you enter your choice, and the next menu pops up, and below the menu it says "None" on the line before your next choice is asked for i dont know why it does this or how to make it go away sorry the menu looks funny(they are supposed to be shapes) i didnt include the code for all of the functions but the same thing happens for each of the four choices CODE BEGINS HERE: def parallelogram_area(side1, side2): "find the area of a parallelogram""" solution1 = float(side1 * side2) return solution1 def quadrilateral_perimeter(side1, side2, side3, side4): """find the perimeter of a quadralateral""" solution1 = float(side1 + side2 + side3 + side4) return solution1 def trapezoid_area(side1, side2, height): """find the area of a trapazoid""" solution1 = float(side1 + side2) solution2 = float((height)/2) solution3 = float(solution1 * solution2) return solution3 def menu_quad(): """Display menu for quadrilaterals""" print( """ Quadrilaterals 0 - Return to main menu 1 - Parallelogram (area) 2 - Trapezoid (area) 3 - Quadrilateral(perimeter) """ ) choice = None while choice != "0": print( """ Menu 0 - Exit 1 - Quadrilaterals 3 - Circles _________________ ** | | ******** | | ************ | | ************** | | ************** | | ************ |_________________| ******** ** 2 - Triangles 4 - 3D /\ /|\ / \ / | \ / \ / | \ / \ / | / / \ / | / /__________\ /_____|/ """ ) choice = input("Choice: ") if choice == "0": print("Good-bye.") #main if choice == "1": #quadrilaterals print(menu_quad()) quad_choice = input("Choice: ") if quad_choice == "0": print() if quad_choice == "1": #parallelogram side1 = float(input("Side 1: ")) side2 = float(input("Side 2: ")) area = parallelogram_area(side1, side2) print("The area of the parallelogram is", area) if quad_choice == "2": #trapezoid side1 = float(input("Side 1: ")) side2 = float(input("Side 2: ")) height = float(input("height: ")) area = trapezoid_area(side1, side2, height) print("The area of the trapezoid is", area) if quad_choice == "3": #quadrilateral side1 = float(input("Side 1: ")) side2 = float(input("Side 2: ")) side3 = float(input("Side 3: ")) side4 = float(input("Side 4: ")) perim = quadrilateral_perimeter(side1, side2, side3, side4) print("The perimeter of the quadrilateral is", perim) else: print("Sorry, that is not a valid choice :(") input("Press enter to exit.") From oscar.j.benjamin at gmail.com Wed Dec 19 02:15:17 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 19 Dec 2012 01:15:17 +0000 Subject: [Tutor] I cant fix this bug In-Reply-To: <50D112E1.7020209@comcast.net> References: <50D112E1.7020209@comcast.net> Message-ID: On 19 December 2012 01:05, Gina wrote: > I have Python version 3. > when the program begins, it prints out the main menu and then asks you for > your choice - just like it is supposed to > you enter your choice, and the next menu pops up, and below the menu it says > "None" on the line before your next choice is asked for > i dont know why it does this or how to make it go away > > def menu_quad(): > """Display menu for quadrilaterals""" > print( > """ > Quadrilaterals > > 0 - Return to main menu > 1 - Parallelogram (area) > 2 - Trapezoid (area) > 3 - Quadrilateral(perimeter) > """ > ) > > print(menu_quad()) The menu_quad() function does not return anything. In Python this means it returns the special value None. Since you print menu_quad's return value, 'None' appears. Remove the call to print() around menu_quad() and the 'None' will go away. Oscar From msirenef at lightbird.net Wed Dec 19 02:17:11 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Tue, 18 Dec 2012 20:17:11 -0500 Subject: [Tutor] I cant fix this bug In-Reply-To: <50D112E1.7020209@comcast.net> References: <50D112E1.7020209@comcast.net> Message-ID: <50D11597.4050401@lightbird.net> On 12/18/2012 08:05 PM, Gina wrote: > I have Python version 3. > when the program begins, it prints out the main menu and then asks you > for your choice - just like it is supposed to > you enter your choice, and the next menu pops up, and below the menu > it says "None" on the line before your next choice is asked for > i dont know why it does this or how to make it go away > > sorry the menu looks funny(they are supposed to be shapes) > i didnt include the code for all of the functions but the same thing > happens for each of the four choices > > CODE BEGINS HERE: > > def parallelogram_area(side1, side2): > "find the area of a parallelogram""" > solution1 = float(side1 * side2) > return solution1 > > def quadrilateral_perimeter(side1, side2, side3, side4): > """find the perimeter of a quadralateral""" > solution1 = float(side1 + side2 + side3 + side4) > return solution1 > > def trapezoid_area(side1, side2, height): > """find the area of a trapazoid""" > solution1 = float(side1 + side2) > solution2 = float((height)/2) > solution3 = float(solution1 * solution2) > return solution3 > > def menu_quad(): > """Display menu for quadrilaterals""" > print( > """ > Quadrilaterals > > 0 - Return to main menu > 1 - Parallelogram (area) > 2 - Trapezoid (area) > 3 - Quadrilateral(perimeter) > """ > ) > > choice = None > while choice != "0": > print( > """ > Menu > > 0 - Exit > > 1 - Quadrilaterals 3 - Circles > _________________ ** > | | ******** > | | ************ > | | ************** > | | ************** > | | ************ > |_________________| ******** > ** > 2 - Triangles 4 - 3D > /\ /|\ > / \ / | \ > / \ / | \ > / \ / | / > / \ / | / > /__________\ /_____|/ > > """ > ) > > choice = input("Choice: ") > > if choice == "0": > print("Good-bye.") > > #main > if choice == "1": > #quadrilaterals > print(menu_quad()) > > quad_choice = input("Choice: ") > > if quad_choice == "0": > print() > > if quad_choice == "1": > #parallelogram > side1 = float(input("Side 1: ")) > side2 = float(input("Side 2: ")) > area = parallelogram_area(side1, side2) > print("The area of the parallelogram is", area) > > if quad_choice == "2": > #trapezoid > side1 = float(input("Side 1: ")) > side2 = float(input("Side 2: ")) > height = float(input("height: ")) > area = trapezoid_area(side1, side2, height) > print("The area of the trapezoid is", area) > > if quad_choice == "3": > #quadrilateral > side1 = float(input("Side 1: ")) > side2 = float(input("Side 2: ")) > side3 = float(input("Side 3: ")) > side4 = float(input("Side 4: ")) > perim = quadrilateral_perimeter(side1, side2, side3, side4) > print("The perimeter of the quadrilateral is", perim) > > else: > print("Sorry, that is not a valid choice :(") > > input("Press enter to exit.") > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > It looks like you have print(menu_quad()) which prints the return value of that function. When function has no explicit return value in python, it returns None. You probably mean to return the string in that function instead of printing it there. - mitya -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From steve at pearwood.info Wed Dec 19 02:13:18 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 19 Dec 2012 12:13:18 +1100 Subject: [Tutor] optparse.OptionParser options in alphabetical order in help display In-Reply-To: References: Message-ID: <50D114AE.8070201@pearwood.info> On 19/12/12 07:39, rail shafigulin wrote: > I currently have it done this way. It just seems a bit odd that I have to > arrange it manually. What's so special about alphabetical order? Wouldn't it make more sense for related options to be kept together? E.g.: Usage: make_sandwich [OPTIONS] --bread KIND white, multigrain, rye, black, sourdough or wholemeal. --style "light"|"dark" Defaults to "light" if not given. Ignored unless bread=rye. --meat KIND chicken, ham, turkey, pastrami, salami, spam or beef rather than: --bread KIND white, multigrain, rye, black, sourdough or wholemeal. --meat KIND chicken, ham, turkey, pastrami, salami, spam or beef --style "light"|"dark" Defaults to "light" if not given. Ignored unless bread=rye. Obviously with only three options it doesn't make much difference, but suppose you had two dozen options. Alphabetical order means that related options will be scattered all over the place. -- Steven From bgailer at gmail.com Wed Dec 19 02:38:17 2012 From: bgailer at gmail.com (bob gailer) Date: Tue, 18 Dec 2012 20:38:17 -0500 Subject: [Tutor] I cant fix this bug In-Reply-To: <50D112E1.7020209@comcast.net> References: <50D112E1.7020209@comcast.net> Message-ID: <50D11A89.6090105@gmail.com> On 12/18/2012 8:05 PM, Gina wrote: > print(menu_quad()) menu_quad() calls that function which prints the quadrilateral menu. The function has no return statement, so by default it returns None. The above print then prints None. Change menu_quad() to return rather than print the menu. BTW you really don't need a function menu_quad(). You could instead have a variable menu_quad which is assigned the menu, then just print menu_quad. There are many other opportunities to improve and simplify this program. But it is a good start. -- Bob Gailer 919-636-4239 Chapel Hill NC From rail.shafigulin at gmail.com Wed Dec 19 02:42:15 2012 From: rail.shafigulin at gmail.com (rail shafigulin) Date: Tue, 18 Dec 2012 20:42:15 -0500 Subject: [Tutor] optparse.OptionParser options in alphabetical order in help display In-Reply-To: <50D114AE.8070201@pearwood.info> References: <50D114AE.8070201@pearwood.info> Message-ID: On Tue, Dec 18, 2012 at 8:13 PM, Steven D'Aprano wrote: > On 19/12/12 07:39, rail shafigulin wrote: > > I currently have it done this way. It just seems a bit odd that I have to >> arrange it manually. >> > > What's so special about alphabetical order? Wouldn't it make more sense for > related options to be kept together? > > E.g.: > > Usage: make_sandwich [OPTIONS] > > --bread KIND white, multigrain, rye, black, sourdough or > wholemeal. > --style "light"|"dark" Defaults to "light" if not given. Ignored > unless bread=rye. > --meat KIND chicken, ham, turkey, pastrami, salami, spam > or beef > > rather than: > > --bread KIND white, multigrain, rye, black, sourdough or > wholemeal. > --meat KIND chicken, ham, turkey, pastrami, salami, spam > or beef > --style "light"|"dark" Defaults to "light" if not given. Ignored > unless bread=rye. > > > Obviously with only three options it doesn't make much difference, but > suppose > you had two dozen options. Alphabetical order means that related options > will be > scattered all over the place. > > > > -- > Steven > ______________________________**_________________ > > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > There is nothing special about alphabetical order. You are right, it is better to arrange related options together. For my script, I don't have related options and simply want to arrange them in alphabetical order. -------------- next part -------------- An HTML attachment was scrubbed... URL: From robertvstepp at gmail.com Wed Dec 19 04:54:50 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 18 Dec 2012 21:54:50 -0600 Subject: [Tutor] Your thoughts on using Python 2.4.x? Message-ID: At my workplace we have nearly completed the hardware and software upgrades. Up to this point I have been using Perl to write scripts that work with our planning software which has its own proprietary scripting environment. Generally speaking the scripts I write are always text files which run Solaris commands and/or create reload script files in the proprietary scripting format. Until our recent upgrades Perl was the best option; Python did not exist on our systems. Now after the upgrades some machines now have Python 2.4.4 and others Python 2.4.6. For the purposes of creating/manipulating text files and running Solaris-flavored Unix commands, is there anything I should be specially aware of? I have been working entirely in my Python studies with version 3.x up to this point. I understand that there are differences between 2.x versus 3.x such as print statements versus print functions, etc. BTW, does 2.4.x come with Tkinter standard? In case anyone has the thought that I should upgrade to a more recent version of Python at work, bear in mind that these are medical software systems that come with FDA compliant certification. I probably could get permission to upgrade, but only if I could demonstrate a compelling need, which at this time I would deem highly unlikely. Thanks! boB From merrittb7 at gmail.com Wed Dec 19 06:40:16 2012 From: merrittb7 at gmail.com (Brandon Merritt) Date: Tue, 18 Dec 2012 21:40:16 -0800 Subject: [Tutor] need help with python for counter Message-ID: I feel silly, but I'm having the darndest time trying to figure out why this for counter won't work. I know that there is the count method for the string class, but I was just trying to do it the syntactical way to prove myself that I know the very basics. As of right now, my script is just returning 1 or 0, even if I very clearly make sure that I specify at least 4 instances of the digit in my number string: number = raw_input('Enter a 7-unit number: ') digit = raw_input('Enter a single digit: ') for i in number: count = 0 if i == digit: count += 1 else: count = 0 print count Thanks, Brandon -- *Brandon Merritt** (707) 481-1744* * * -------------- next part -------------- An HTML attachment was scrubbed... URL: From msirenef at lightbird.net Wed Dec 19 07:12:32 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Wed, 19 Dec 2012 01:12:32 -0500 Subject: [Tutor] need help with python for counter In-Reply-To: References: Message-ID: <50D15AD0.5080302@lightbird.net> On 12/19/2012 12:40 AM, Brandon Merritt wrote: > I feel silly, but I'm having the darndest time trying to figure out > why this for counter won't work. I know that there is the count method > for the string class, but I was just trying to do it the syntactical > way to prove myself that I know the very basics. As of right now, my > script is just returning 1 or 0, even if I very clearly make sure that > I specify at least 4 instances of the digit in my number string: > > number = raw_input('Enter a 7-unit number: ') > > digit = raw_input('Enter a single digit: ') > > for i in number: > count = 0 > if i == digit: > count += 1 > else: > count = 0 Why do you have the else: clause? You can also do: count = sum(i==digit for i in number) I think this is very clear and short.. -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From doanviettrung at gmail.com Wed Dec 19 07:13:36 2012 From: doanviettrung at gmail.com (DoanVietTrungAtGmail) Date: Wed, 19 Dec 2012 17:13:36 +1100 Subject: [Tutor] need help with python for counter In-Reply-To: References: Message-ID: After incrementing for a little while, if the condition i == digit is not met for the current character, count is reset by the else: branch. Your count variable must feel frustrated like a dog which keeps being yanked back by a cord of length 0 when it tries to get free. Trung Doan ========== On Wed, Dec 19, 2012 at 4:40 PM, Brandon Merritt wrote: > I feel silly, but I'm having the darndest time trying to figure out why > this for counter won't work. I know that there is the count method for the > string class, but I was just trying to do it the syntactical way to prove > myself that I know the very basics. As of right now, my script is just > returning 1 or 0, even if I very clearly make sure that I specify at least > 4 instances of the digit in my number string: > > number = raw_input('Enter a 7-unit number: ') > > digit = raw_input('Enter a single digit: ') > > for i in number: > count = 0 > if i == digit: > count += 1 > else: > count = 0 > > print count > > Thanks, > Brandon > > > -- > *Brandon Merritt** > (707) 481-1744* > * > * > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From merrittb7 at gmail.com Wed Dec 19 07:19:31 2012 From: merrittb7 at gmail.com (Brandon Merritt) Date: Tue, 18 Dec 2012 22:19:31 -0800 Subject: [Tutor] still confused about for loops Message-ID: Sorry, I am just so confused and aggravated as to why this won't work - why doesn't it print out the whole list? : number = raw_input('Enter a 7-unit number: ') for i in number: count = [] count.append(i) print count >>> Enter a 7-unit number: 78953298 ['8'] -- *Brandon Merritt** (707) 481-1744* * * -------------- next part -------------- An HTML attachment was scrubbed... URL: From daedae11 at 126.com Wed Dec 19 06:52:14 2012 From: daedae11 at 126.com (Dae James) Date: Wed, 19 Dec 2012 13:52:14 +0800 Subject: [Tutor] How can I overwrite the previous version on linux ? Message-ID: <201212191352141136946@126.com> My linux distribution is CentOs 6.3. And python attached to the OS is 2.6. How can I overwrite the previous version with python 2.7 ? Or how can I uninstall the previous version? Dae James -------------- next part -------------- An HTML attachment was scrubbed... URL: From msirenef at lightbird.net Wed Dec 19 07:30:37 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Wed, 19 Dec 2012 01:30:37 -0500 Subject: [Tutor] still confused about for loops In-Reply-To: References: Message-ID: <50D15F0D.6040700@lightbird.net> On 12/19/2012 01:19 AM, Brandon Merritt wrote: > Sorry, I am just so confused and aggravated as to why this won't work > - why doesn't it print out the whole list? : > > number = raw_input('Enter a 7-unit number: ') > > for i in number: > count = [] > count.append(i) > > print count > > >>> Enter a 7-unit number: 78953298 > ['8'] > > Same exact problem as in your first question: you are resetting the list inside the loop. You want to move it outside the loop. You could also do: count = [i for i in number] -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From shantanoo at gmail.com Wed Dec 19 08:08:28 2012 From: shantanoo at gmail.com (=?UTF-8?B?4KS24KSC4KSk4KSo4KWC?=) Date: Wed, 19 Dec 2012 18:08:28 +1100 Subject: [Tutor] need help with python for counter In-Reply-To: <50D15AD0.5080302@lightbird.net> References: <50D15AD0.5080302@lightbird.net> Message-ID: <50D167EC.4060403@gmail.com> On 19/12/12 5:12 PM, Mitya Sirenef wrote: > You can also do: > > count = sum(i==digit for i in number) > > I think this is very clear and short.. Another... import re count = len(re.findall(digit, number)) -- shantanoo From kushal.kumaran+python at gmail.com Wed Dec 19 09:03:19 2012 From: kushal.kumaran+python at gmail.com (Kushal Kumaran) Date: Wed, 19 Dec 2012 13:33:19 +0530 Subject: [Tutor] How can I overwrite the previous version on linux ? In-Reply-To: <201212191352141136946@126.com> References: <201212191352141136946@126.com> Message-ID: <50d174ce.e9ea440a.1d52.ffffdf36@mx.google.com> Dae James writes: > My linux distribution is CentOs 6.3. And python attached to the OS is 2.6. > How can I overwrite the previous version with python 2.7 ? Or how can I uninstall the previous version? > This is not recommended. The python version supplied with your OS might be used by a number of programs and utilities that are shipped with the OS. If you want to run a different version of python, install it in addition to the version already there. You can build from source, following the instructions here: http://docs.python.org/2/using/unix.html#building-python -- regards, kushal From wprins at gmail.com Wed Dec 19 10:30:13 2012 From: wprins at gmail.com (Walter Prins) Date: Wed, 19 Dec 2012 09:30:13 +0000 Subject: [Tutor] Your thoughts on using Python 2.4.x? In-Reply-To: References: Message-ID: On 19 December 2012 03:54, boB Stepp wrote: > BTW, does 2.4.x come with Tkinter standard? > Yes: http://docs.python.org/release/2.4.4/lib/module-Tkinter.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Dec 19 11:08:38 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 19 Dec 2012 10:08:38 +0000 Subject: [Tutor] Your thoughts on using Python 2.4.x? In-Reply-To: References: Message-ID: On 19/12/12 03:54, boB Stepp wrote: > systems. Now after the upgrades some machines now have Python 2.4.4 > and others Python 2.4.6. For the purposes of creating/manipulating > text files and running Solaris-flavored Unix commands, is there > anything I should be specially aware of? The major versions of Python tend to be very good at backward compatibility. Obviously if you use new features not supported in 2.4 they won't work (although some might if you 'import future') Check the 'Whats new' file for each version between 2.4 and 2.7 to see what you should avoid. But I still have Python 1.5 programs that run happily under 2.6 (I don't have 2.7 installed yet) and the vast majority of code I write on a daily basis will work on any version post 2.2. > in my Python studies with version 3.x up to this point. I understand > that there are differences between 2.x versus 3.x such as print > statements versus print functions, etc. Yes, there is a big jump between v2.x and v3.x. Not massive, but more than in previous versions. > BTW, does 2.4.x come with Tkinter standard? Yes, Tkinter has been part of the standard library since at least v1.3 (when I started using python! :-) > probably could get permission to upgrade, but only if I could > demonstrate a compelling need, which at this time I would deem highly > unlikely. On our servers at work we are still on Python 2.3. We do not officially support Python (we officially use Ruby(v?) and Perl(v4) for scripting) so the version hasn't been updated in years. It's quite common in corporate environments, and many places have a policy of always staying 1 or 2 versions behind the bleeding edge. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From d at davea.name Wed Dec 19 11:12:18 2012 From: d at davea.name (Dave Angel) Date: Wed, 19 Dec 2012 05:12:18 -0500 Subject: [Tutor] need help with python for counter In-Reply-To: References: Message-ID: <50D19302.2020709@davea.name> On 12/19/2012 12:40 AM, Brandon Merritt wrote: > I feel silly, but I'm having the darndest time trying to figure out why > this for counter won't work. I know that there is the count method for the > string class, but I was just trying to do it the syntactical way to prove > myself that I know the very basics. As of right now, my script is just > returning 1 or 0, even if I very clearly make sure that I specify at least > 4 instances of the digit in my number string: > > number = raw_input('Enter a 7-unit number: ') > > digit = raw_input('Enter a single digit: ') > > for i in number: > count = 0 > if i == digit: > count += 1 > else: > count = 0 > > print count > > Two separate problems in that code, either of which would be enough to ruin the count value. 1) Your code sets the count to zero INSIDE the loop, so every time through, you trash whatever earlier value you had. This assures that only the last loop matters! You ought to be zeroing the counter just once, before the loop starts. 2) In your else clause, as soon as you encounter some other character, the code is zeroing the counter. count = 0 for i in number: if i == digit: count += 1 While I've got your attention, let me point out that nowhere in your description do you actually tell us what values you're entering, and what values you expect. You ought to supply sample values which illustrate the problem. The above code is really only a guess, as to what you wanted. But the notion of doing the initialization OUTSIDE the loop is an important one, and I'm sure that part is correct. -- DaveA From alan.gauld at btinternet.com Wed Dec 19 11:11:04 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 19 Dec 2012 10:11:04 +0000 Subject: [Tutor] need help with python for counter In-Reply-To: References: Message-ID: On 19/12/12 05:40, Brandon Merritt wrote: > the string class, but I was just trying to do it the syntactical way to > prove myself that I know the very basics. Which is not a bad thing. > just returning 1 or 0, even if I very clearly make sure that I specify > at least 4 instances of the digit in my number string: > > number = raw_input('Enter a 7-unit number: ') > > digit = raw_input('Enter a single digit: ') > > for i in number: > count = 0 you reset count to zero everytime round the loop. You need to move this outside the loop. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed Dec 19 11:15:05 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 19 Dec 2012 10:15:05 +0000 Subject: [Tutor] How can I overwrite the previous version on linux ? In-Reply-To: <201212191352141136946@126.com> References: <201212191352141136946@126.com> Message-ID: On 19/12/12 05:52, Dae James wrote: > My linux distribution is CentOs 6.3. And python attached to the OS is 2.6. > How can I overwrite the previous version with python 2.7 ? Or how can I > uninstall the previous version? > ------------------------------------------------------------------------ You should not change the standard install, its probably there for a reason! But you can install another version for your own use. I don;t know CentOS but I assume it has a package manager. Just install the latest package (apt-get, yum, yast or whatever). In the last resort you can download the tarball and build from source. But there is probably a package you can use -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Wed Dec 19 11:43:59 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 19 Dec 2012 02:43:59 -0800 (PST) Subject: [Tutor] sys.getfilesystemencoding() In-Reply-To: References: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: <1355913839.41344.YahooMailNeo@web163806.mail.gq1.yahoo.com> >________________________________ > From: eryksun >To: Albert-Jan Roskam >Cc: Python Mailing List >Sent: Tuesday, December 18, 2012 4:07 PM >Subject: Re: [Tutor] sys.getfilesystemencoding() > >On Tue, Dec 18, 2012 at 8:13 AM, Albert-Jan Roskam wrote: >> >>In windows xp, the characters can, apparently, not be represented >>in this encoding called 'mbcs'. > >MBCS (multibyte character set) refers to the locale encoding on >Windows. CPython encodes to MBCS via the Win32 function >WideCharToMultiByte, with the CP_ACP code page (i.e. the system >default 'ANSI' encoding): > >unicodeobject.c, encode_mbcs (3877-3884): >http://hg.python.org/cpython/file/8803c3d61da2/Objects/unicodeobject.c#l3843 > >WideCharToMultiByte: >http://msdn.microsoft.com/en-us/library/windows/desktop/dd374130 Hi again, Thank you for your reply. Oscar, also thanks for your reply; our e-mails yesterday. So MBCS is just a collective noun for whatever happens to be the installed/available codepage of the host computer (at least with CP_ACP)? I didn't know anything about wintypes and I find it quite hard to understand! I am trying to write a ctypes wrapper for WideCharToMultiByte. It takes a unicode path name and (is supposed to) returns a bytestring using the utf8 codepage: http://pastebin.com/SEr4Wec9 . The code is kinda verbose, but I hope this makes it easier to read. Does this makes sense at all? As for now, the program returns an error code (oddly, zero is an error code here). I chose CP_UTF8 instead of CP_ACP because I don't want the conversion to depend on some (arbitrary) system default Windows ANSI code page. Regards, Albert-Jan From steve at pearwood.info Wed Dec 19 12:22:38 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 19 Dec 2012 22:22:38 +1100 Subject: [Tutor] How can I overwrite the previous version on linux ? In-Reply-To: <201212191352141136946@126.com> References: <201212191352141136946@126.com> Message-ID: <50D1A37E.30203@pearwood.info> On 19/12/12 16:52, Dae James wrote: > My linux distribution is CentOs 6.3. And python attached to the OS is 2.6. > How can I overwrite the previous version with python 2.7 ? Or how can I >uninstall the previous version? Why? Do you like breaking your operating system? Never, ever mess with the operating system's installed version of Python. Unless you are an expert, you will probably break things. Even if you are an expert, it is best to just leave it for the operating system. Instead, install Python 2.7 separately. You could try: $ sudo yum install python2.7 from the command line, and see if that works. I'm still using Centos 5, so I don't know what Centos 6 will do, but there is a very slim chance that it will override the system Python 2.6. That's okay! If the package manager does it, you can assume it will be done safely. Chances are, unfortunately, that yum won't install Python 2.7 for you. In that case, you have to do it by hand. Do you need help with that? Once it is installed side-by-side with the system python, add this to your .bashrc file: alias python=python2.7 -- Steven From bgailer at gmail.com Wed Dec 19 06:57:40 2012 From: bgailer at gmail.com (bob gailer) Date: Wed, 19 Dec 2012 00:57:40 -0500 Subject: [Tutor] need help with python for counter In-Reply-To: References: Message-ID: <50D15754.6060802@gmail.com> On 12/19/2012 12:40 AM, Brandon Merritt wrote: > I feel silly, but I'm having the darndest time trying to figure out > why this for counter won't work. I know that there is the count method > for the string class, but I was just trying to do it the syntactical > way to prove myself that I know the very basics. As of right now, my > script is just returning 1 or 0, even if I very clearly make sure that > I specify at least 4 instances of the digit in my number string: > > number = raw_input('Enter a 7-unit number: ') > > digit = raw_input('Enter a single digit: ') > > for i in number: > count = 0 > if i == digit: > count += 1 Drop next two statements and try agan > else: > count = 0 > > print count > -- Bob Gailer 919-636-4239 Chapel Hill NC From ramit.prasad at jpmorgan.com Wed Dec 19 17:30:26 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Wed, 19 Dec 2012 16:30:26 +0000 Subject: [Tutor] How can I overwrite the previous version on linux ? In-Reply-To: <50D1A37E.30203@pearwood.info> References: <201212191352141136946@126.com> <50D1A37E.30203@pearwood.info> Message-ID: <5B80DD153D7D744689F57F4FB69AF474180A1773@SCACMX008.exchad.jpmchase.net> Steven D'Aprano wrote: > On 19/12/12 16:52, Dae James wrote: > > My linux distribution is CentOs 6.3. And python attached to the OS is 2.6. > > How can I overwrite the previous version with python 2.7 ? Or how can I > >uninstall the previous version? > > Why? Do you like breaking your operating system? > > Never, ever mess with the operating system's installed version of Python. > Unless you are an expert, you will probably break things. Even if you are > an expert, it is best to just leave it for the operating system. > > Instead, install Python 2.7 separately. > > You could try: > > $ sudo yum install python2.7 > > from the command line, and see if that works. I'm still using Centos 5, > so I don't know what Centos 6 will do, but there is a very slim chance that > it will override the system Python 2.6. That's okay! If the package manager > does it, you can assume it will be done safely. > > Chances are, unfortunately, that yum won't install Python 2.7 for you. In > that case, you have to do it by hand. Do you need help with that? Once it is > installed side-by-side with the system python, add this to your .bashrc file: > > alias python=python2.7 > > In addition to what Steven said, this webpage might be helpful http://toomuchdata.com/2012/06/25/how-to-install-python-2-7-3-on-centos-6-2/ I have no familiarity with CentOS so YMMV with that link. Google had lots of similar pages. http://lmgtfy.com/?q=centos+6+python2.7 > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From eryksun at gmail.com Wed Dec 19 18:20:41 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 19 Dec 2012 12:20:41 -0500 Subject: [Tutor] sys.getfilesystemencoding() In-Reply-To: <1355913839.41344.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> <1355913839.41344.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On Wed, Dec 19, 2012 at 5:43 AM, Albert-Jan Roskam wrote: > >So MBCS is just a collective noun for whatever happens to be the >installed/available codepage of the host computer (at least with >CP_ACP)? To be clear, the "mbcs" encoding in Python uses CP_ACP. MBCS means multibyte character set. The term ANSI gets thrown around, too, but Windows legacy code pages aren't ANSI standards. >I didn't know anything about wintypes and I find it quite hard to >understand! I am trying to write a ctypes wrapper for >WideCharToMultiByte. Just for the fun of it? >http://pastebin.com/SEr4Wec9 >The code is kinda verbose, but I hope this makes it easier to read. >Does this makes sense at all? As for now, the program returns an >error code (oddly, zero is an error code here). Use None for NULL. You shouldn't encode a string argument you've declared as c_wchar_p (i.e. wintypes.LPCWSTR, i.e. type 'Z'). If you initialize to a byte string, the setter Z_set calls PyUnicode_FromEncodedObject using the "mbcs" encoding (this is the default on Windows, set by set_conversion_mode("mbcs", "ignore")). This hands off to decode_mbcs, which produces nonsense for a UTF-16LE encoded string. GetLastError should be defined already, along with WinError, a convenience function that returns an instance of WindowsError. 2.6.4 source: http://hg.python.org/cpython/file/8803c3d61da2/Lib/ctypes/__init__.py#l448 Here's a quick hack that you should help you along: import ctypes from ctypes import wintypes _CP_UTF8 = 65001 _CP_ACP = 0 # ANSI _LPBOOL = ctypes.POINTER(ctypes.c_long) _wideCharToMultiByte = ctypes.windll.kernel32.WideCharToMultiByte _wideCharToMultiByte.restype = ctypes.c_int _wideCharToMultiByte.argtypes = [ wintypes.UINT, wintypes.DWORD, wintypes.LPCWSTR, ctypes.c_int, wintypes.LPSTR, ctypes.c_int, wintypes.LPCSTR, _LPBOOL] def wide2utf8(fn): codePage = _CP_UTF8 dwFlags = 0 lpWideCharStr = fn cchWideChar = len(fn) lpMultiByteStr = None cbMultiByte = 0 # zero requests size lpDefaultChar = None lpUsedDefaultChar = None # get size mbcssize = _wideCharToMultiByte( codePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte, lpDefaultChar, lpUsedDefaultChar) if mbcssize <= 0: raise ctypes.WinError(mbcssize) lpMultiByteStr = ctypes.create_string_buffer(mbcssize) # convert retcode = _wideCharToMultiByte( codePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, mbcssize, lpDefaultChar, lpUsedDefaultChar) if retcode <= 0: raise ctypes.WinError(retcode) return lpMultiByteStr.value From rail.shafigulin at gmail.com Wed Dec 19 18:37:32 2012 From: rail.shafigulin at gmail.com (rail shafigulin) Date: Wed, 19 Dec 2012 12:37:32 -0500 Subject: [Tutor] release a script with non-standard dependencies Message-ID: I'm attempting to write a script with command-line arguments. Based on some reading I have done online and some advice from this mailing list I'm going to use docopt and schema modules. The problem I'm facing is that I'd like to be able to give this script to anyone who needs it by just using one file, i.e. the script itself. Does anybody know if there is a way to achieve this without pasting code from docopt and schema into my script file. Any help is appreciated. -------------- next part -------------- An HTML attachment was scrubbed... URL: From walksloud at gmail.com Wed Dec 19 18:51:12 2012 From: walksloud at gmail.com (Andre' Walker-Loud) Date: Wed, 19 Dec 2012 09:51:12 -0800 Subject: [Tutor] release a script with non-standard dependencies In-Reply-To: References: Message-ID: Hi Rail, > I'm attempting to write a script with command-line arguments. Based on some reading I have done online and some advice from this mailing list I'm going to use docopt and schema modules. The problem I'm facing is that I'd like to be able to give this script to anyone who needs it by just using one file, i.e. the script itself. Does anybody know if there is a way to achieve this without pasting code from docopt and schema into my script file. > > Any help is appreciated. I would like to know the answer to your question as well. Hopefully, the people you work with are comfortable installing something like docopt - but I know that is often not the case. If you are unsuccessful, and consider going back to OptParse, I just wanted to point you to using Argparse instead. It is a bit better, and more importantly, is not deprecated (Optparse is no longer updated). However, it shares the same issues the creator of docopt railed against in his presentation that Modulok sent a link to earlier. Regards, Andre From brian.van.den.broek at gmail.com Wed Dec 19 19:06:04 2012 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Wed, 19 Dec 2012 13:06:04 -0500 Subject: [Tutor] still confused about for loops In-Reply-To: References: Message-ID: On 19 December 2012 01:19, Brandon Merritt wrote: > Sorry, I am just so confused and aggravated as to why this won't work - why > doesn't it print out the whole list? : > > number = raw_input('Enter a 7-unit number: ') > > for i in number: > count = [] > count.append(i) > > print count > >>>> Enter a 7-unit number: 78953298 > ['8'] > > -- > Brandon Merritt > (707) 481-1744 Brandon, Others have pointed out the problem here and in your counter thread. I have a bit of meta-advice that will help you to resolve these sorts of problems on your own. When confronted by a small chunk of code that is not behaving as you expect, it can help to grab a pen and paper and interpret the code by hand, going through it and updating the various data values as you run through the program step by step. Often, this will force you to see the point at which your mental model of what you have written diverges from what you have actually written. Less work, and often sufficient to expose the problem is to put in some print statements. Had you tried: for i in number: count = [] count.append(i) print count I suspect the problem would have become much more clear. If I am doing print statement debugging where I've multiple print statements exposing the same data structure, I will often tag them like so for i in number: print count, 11111111111111 count = [] count.append(i) print count, 222222222222 That helps figure out just where each printed instance of count came from. In the case at hand, this version would almost certainly have sorted you out; the first print will fail with a NameError, and this might have suggested to you that you have to define count before the loop. HTH, Brian vdB From steve at pearwood.info Thu Dec 20 00:45:53 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 20 Dec 2012 10:45:53 +1100 Subject: [Tutor] release a script with non-standard dependencies In-Reply-To: References: Message-ID: <50D251B1.2060306@pearwood.info> On 20/12/12 04:37, rail shafigulin wrote: > I'm attempting to write a script with command-line arguments. Based on some > reading I have done online and some advice from this mailing list I'm going > to use docopt and schema modules. The problem I'm facing is that I'd like > to be able to give this script to anyone who needs it by just using one > file, i.e. the script itself. Does anybody know if there is a way to > achieve this without pasting code from docopt and schema into my script > file. You cannot just copy and paste code from one module to another, unless the licence permits it, and you obey whatever obligations are laid on you by doing so. If you don't, then you are in breach of copyright and, worse, potentially acting in an unethical manner. Just because a programmer gives you access to source code doesn't mean you can appropriate it as your own without respecting his licence conditions. Also, copying and pasting code is one of the worst habits you can get into. Don't do it. The solution to this depends on how much effort you want to put into this. 1) The most basic option: dependencies are not your problem. It is up to the recipient of your software to install the prerequisite modules, not you. 2) Give them a download link where they can get the dependencies. 3) Actually install those dependencies for them: when your program runs, it tries to import the modules it needs, and if it cannot find them, it runs an installer to download the modules from the Internet and install them. (This is a lot of work and probably not a good idea.) 4) Bundle the modules together in one file. If you zip up the modules in a zip file as if it were a package, *without* the package directory, then change the file extension to .py, you can run it as if it were a single Python script (at least under Linux, I haven't tried Windows or Mac). But if you do this, you have to obey the licence conditions (if any) on the modules you use. For example, I create a Python package, consisting of a directory, two special files, and whatever modules are needed: test/ +- __init__.py +- __main__.py +- spam.py +- ham.py The file "__init__.py" is special, it is needed by Python to treat this as a package. It can be empty. The file "__main__.py" is special. It is used as the script when you give the command "python test". Whatever code is inside the __main__.py file will run. The two modules "spam.py" and "ham.py" are just examples, you can use any modules that you like or need. Inside __main__.py, type this: import ham, spam # or whatever modules you need print "Success!" # or do something useful Now test that this works correctly, as a Python package. From the command line: [steve at ando python]$ ls test ham.py __init__.py __main__.py spam.py [steve at ando python]$ python test Success! Now build a zip file, change the extension, and run it: [steve at ando python]$ zip -j myscript test/* adding: ham.py (stored 0%) adding: ham.pyc (deflated 33%) adding: __init__.py (stored 0%) adding: __main__.py (stored 0%) adding: spam.py (stored 0%) adding: spam.pyc (deflated 32%) [steve at ando python]$ mv myscript.zip myscript.py [steve at ando python]$ python myscript.py Success! 5) On Windows, you can bundle your script and dependencies using py2exe. I know nothing about it, but the search engine of your choice will have lots of information: https://duckduckgo.com/?q=py2exe I believe there is also a py2app for Mac users. Again, obey the licence conditions. 6) There are a few more options here: http://www.effbot.org/zone/python-compile.htm 7) Use a full Python packaging and distribution utility, like Easy Install or Pip: http://packages.python.org/distribute/easy_install.html http://pypi.python.org/pypi/pip There are wars being fought over which distribution utilities; I don't have an opinion on which is best. -- Steven From oscar.j.benjamin at gmail.com Thu Dec 20 01:59:19 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 20 Dec 2012 00:59:19 +0000 Subject: [Tutor] release a script with non-standard dependencies In-Reply-To: <50D251B1.2060306@pearwood.info> References: <50D251B1.2060306@pearwood.info> Message-ID: On 19 December 2012 23:45, Steven D'Aprano wrote: > On 20/12/12 04:37, rail shafigulin wrote: > >> I'm attempting to write a script with command-line arguments. Based on >> some >> reading I have done online and some advice from this mailing list I'm >> going >> to use docopt and schema modules. The problem I'm facing is that I'd like >> to be able to give this script to anyone who needs it by just using one >> file, i.e. the script itself. Does anybody know if there is a way to >> achieve this without pasting code from docopt and schema into my script >> file. I generally agree with what Steven has said but would like to add some more information on some of the points. > Also, copying and pasting code is one of the worst habits you can get into. > Don't do it. > > The solution to this depends on how much effort you want to put into this. > > 1) The most basic option: dependencies are not your problem. It is up to the > recipient of your software to install the prerequisite modules, not you. For a project consisting of a single file that is not going to be very widely used, this is a common method. Note that, if you are generally using and installing Python software packages it is a good idea to have pip (or something similar) installed. If you do have pip installed the two packages you referred to can be installed by typing $ pip install docopt schema or if you want to install them for one user only you can modify the above to $ pip install --user docopt schema which will install the packages somewhere hidden in your user directory (Python knows where to find them). > 2) Give them a download link where they can get the dependencies. > > > 3) Actually install those dependencies for them: when your program runs, > it tries to import the modules it needs, and if it cannot find them, it > runs an installer to download the modules from the Internet and install > them. > (This is a lot of work and probably not a good idea.) Please don't do this unless as part of an installer script that 1) is clearly an installer script 2) explicitly explains what it wants to do or is doing to the user's system 3) gives the option to cancel with no changes to the user's system Also generally don't do this. This kind of thing is typically done on systems that don't have software repositories (like Windows). Python programs never fall into this category since PyPI is usable on all platforms. > 4) Bundle the modules together in one file. If you zip up the modules in a > zip file as if it were a package, *without* the package directory, then > change > the file extension to .py, you can run it as if it were a single Python > script > (at least under Linux, I haven't tried Windows or Mac). > > But if you do this, you have to obey the licence conditions (if any) on the > modules you use. Both of these projects seem to indicate that this usage pattern is expected. You'll need to check the license properly and perhaps contact the authors to be clear about what conditions to follow when doing that, though. "you can just drop schema.py file into your project?it is self-contained." "you can just drop docopt.py file into your project--it is self-contained." >From here: https://github.com/halst/schema https://github.com/docopt/docopt > 5) On Windows, you can bundle your script and dependencies using py2exe. > I know nothing about it, but the search engine of your choice will have > lots of information: > > https://duckduckgo.com/?q=py2exe > > I believe there is also a py2app for Mac users. > > Again, obey the licence conditions. Things may have improved since the last time I attempted this, but I remember this being relatively painful and generating massive .exe files. Also pyinstaller worked better for me then py2exe but that was a while ago. I would say that this is overkill for a project consisting of three .py files. This option is really for the extreme, but not uncommon, case where your (Windows) user cannot be expected to install any dependencies, not even Python itself (which would probably be installed already on a non-Windows OS). > 6) There are a few more options here: > > http://www.effbot.org/zone/python-compile.htm > > > 7) Use a full Python packaging and distribution utility, like Easy Install > or Pip: > > http://packages.python.org/distribute/easy_install.html > > http://pypi.python.org/pypi/pip > > > There are wars being fought over which distribution utilities; I don't have > an opinion on which is best. I wouldn't say that there are wars (maybe I haven't looked in the right places), although I would personally recommend pip for installing such simple packages as these. There are a number of distribution utilities and they have some differences but they all have one thing in common which is that they mainly just download and install software that has been uploaded to PyPI. If your package is on PyPI then anyone can install it with their own chosen utility: http://pypi.python.org/pypi If your package is on PyPI then you can include a file that describes its dependencies and those will be automatically installed when someone does $ pip install railsscript I would rather install your script that way. This is the most common way that python projects are released. If I heard of a Python project and wanted to find it, PyPI would be the first place to look. Similarly if I was trying to comprehensively discover a Python project fulfilling a particular use I would try some search terms in PyPI. Oscar From eryksun at gmail.com Thu Dec 20 02:20:00 2012 From: eryksun at gmail.com (eryksun) Date: Wed, 19 Dec 2012 20:20:00 -0500 Subject: [Tutor] release a script with non-standard dependencies In-Reply-To: <50D251B1.2060306@pearwood.info> References: <50D251B1.2060306@pearwood.info> Message-ID: On Wed, Dec 19, 2012 at 6:45 PM, Steven D'Aprano wrote: > > 4) Bundle the modules together in one file. If you zip up the modules > in a zip file as if it were a package, *without* the package directory, > then change the file extension to .py, you can run it as if it were a > single Python script (at least under Linux, I haven't tried Windows or > Mac). This works in Windows. If it's a GUI app use the extension .pyw to run without a console. From ufukeskici at gmail.com Thu Dec 20 15:25:39 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Thu, 20 Dec 2012 16:25:39 +0200 Subject: [Tutor] how to control putty window Message-ID: Hello, I run this command and opens putty: import os import subprocess command = '"c:\Program Files\Putty\putty.exe" -ssh ufukeskici at 10.10.10.10-pw test subprocess.Popen(command) But then I want to input new commands to this Putty new window. How can I do it? Thanks. Ufuk -------------- next part -------------- An HTML attachment was scrubbed... URL: From david at graniteweb.com Thu Dec 20 15:56:38 2012 From: david at graniteweb.com (David Rock) Date: Thu, 20 Dec 2012 08:56:38 -0600 Subject: [Tutor] how to control putty window In-Reply-To: References: Message-ID: <20121220145638.GA5104@wdfs.bad> * Ufuk Eskici [2012-12-20 16:25]: > Hello, > > I run this command and opens putty: > > import os > import subprocess > command = '"c:\Program Files\Putty\putty.exe" -ssh > ufukeskici at 10.10.10.10-pw test > subprocess.Popen(command) > > But then I want to input new commands to this Putty new window. How can I > do it? Once you start putty, it is a separate application that python doesn't really know anything about. How many commands are you trying to send? If it's only one or two, you might be able to set up a putty profile with a couple auto-commands on connect, but that's not the same thing. If it's a long string of commands, you might be better to pscp a shell script to the target with one command, and then call that script with the putty profile. I would research automating putty first, then see if there are any options within python to accomplish the same things. -- David Rock david at graniteweb.com From d at davea.name Thu Dec 20 16:17:27 2012 From: d at davea.name (Dave Angel) Date: Thu, 20 Dec 2012 10:17:27 -0500 Subject: [Tutor] how to control putty window In-Reply-To: References: Message-ID: <50D32C07.2060508@davea.name> On 12/20/2012 09:25 AM, Ufuk Eskici wrote: > > I run this command and opens putty: > > import os > import subprocess > command = '"c:\Program Files\Putty\putty.exe" -ssh > ufukeskici at 10.10.10.10-pw test > subprocess.Popen(command) > > You really should use cut and paste when trying to tell us what you tried. But I'd like to point out that you were just lucky that the file pathname worked. You need to use either a raw string, double the backslashes, or use forward slashes. -- DaveA From kbailey at howlermonkey.net Thu Dec 20 16:35:23 2012 From: kbailey at howlermonkey.net (Kirk Bailey) Date: Thu, 20 Dec 2012 10:35:23 -0500 Subject: [Tutor] playing mp3 files Message-ID: <50D3303B.4060203@howlermonkey.net> Ok, I found something that works reliably: http://www.mailsend-online.com/blog/play-mp3-files-with-python-on-windows.html Here is the code as modified for my uses (beware text wrap!): -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #!c:\Python25\pythonw.exe # Copyright (c) 2011 by James K. Lawless # jimbo at radiks.net http://www.mailsend-online.com # License: MIT / X11 # See: http://www.mailsend-online.com/license.php # for full license details. # from ctypes import *; import os, os.path, sys import cgitb; cgitb.enable() # print "Content-Type: text/html \n" print "Robot Radio Station WHEN" print '' print '

Robot Radio Station WHEN


' # winmm = windll.winmm # def mciSend(s): i=winmm.mciSendStringA(s,0,0,0) if i<>0: print "Error %d in mciSendString %s" % ( i, s ) # def playMP3(mp3Name): mciSend("Close All") mciSend("Open \"%s\" Type MPEGVideo Alias theMP3" % mp3Name) mciSend("Play theMP3 Wait") mciSend("Close theMP3") # mp3file="Burns Allen 400529 Sweeping into Office.mp3" # test code. os.chdir("./RadioStation_WHEN/1") # test code. dir management will replace. print "playing",mp3file,"@ strftime("%a, %d %b %Y %H:%M:%S",gmtime())

" # Above Logs to the console screen what is played and when playMP3(mp3file) # Above plays a file named in the variable -mp3file- -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- The close all does not work. Run it again, it plays the file again, OVER the one already playing. Any input? -- -Shaboom. Kirk Bailey CEO, Freehold Marketing LLC http://www.OneBuckHosting.com/ Fnord! From alan.gauld at btinternet.com Thu Dec 20 18:39:52 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 20 Dec 2012 17:39:52 +0000 Subject: [Tutor] how to control putty window In-Reply-To: References: Message-ID: On 20/12/12 14:25, Ufuk Eskici wrote: > Hello, > > I run this command and opens putty: > > import os > import subprocess > command = '"c:\Program Files\Putty\putty.exe" -ssh > ufukeskici at 10.10.10.10 -pw test > subprocess.Popen(command) > > But then I want to input new commands to this Putty new window. How can > I do it? I don't know putty. But if it reads/writes to stdin/out/error then you can use the options to Popen as described in the subprocess docs. If it doesn't use stdin/out etc then its a much bigger problem. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Thu Dec 20 19:01:54 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 20 Dec 2012 10:01:54 -0800 (PST) Subject: [Tutor] sys.getfilesystemencoding() In-Reply-To: References: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> <1355913839.41344.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: <1356026514.60293.YahooMailNeo@web163806.mail.gq1.yahoo.com> > On Wed, Dec 19, 2012 at 5:43 AM, Albert-Jan Roskam > wrote: >> >> So MBCS is just a collective noun for whatever happens to be the >> installed/available codepage of the host computer (at least with >> CP_ACP)? > > To be clear, the "mbcs" encoding in Python uses CP_ACP. MBCS means > multibyte character set. The term ANSI gets thrown around, too, but > Windows legacy code pages aren't ANSI standards. > >> I didn't know anything about wintypes and I find it quite hard to >> understand! I am trying to write a ctypes wrapper for >> WideCharToMultiByte. > > Just for the fun of it? Yes, I am afraid so. ;-) >> http://pastebin.com/SEr4Wec9 >> The code is kinda verbose, but I hope this makes it easier to read. >> Does this makes sense at all? As for now, the program returns an >> error code (oddly, zero is an error code here). > > Use None for NULL. Aahh... I was already thinking the prototype didn't match the zeroes. > You shouldn't encode a string argument you've declared as c_wchar_p > (i.e. wintypes.LPCWSTR, i.e. type 'Z'). If you initialize to a byte > string, the setter Z_set calls PyUnicode_FromEncodedObject using the > "mbcs" encoding (this is the default on Windows, set by > set_conversion_mode("mbcs", "ignore")). This hands off to > decode_mbcs, > which produces nonsense for a UTF-16LE encoded string. Ok, yes, that was plain stupid of me. ? > GetLastError should be defined already, along with WinError, a > convenience function that returns an instance of WindowsError. 2.6.4 > source: Convenient indeed. No need to reinvent the wheel. > http://hg.python.org/cpython/file/8803c3d61da2/Lib/ctypes/__init__.py#l448 > > Here's a quick hack that you should help you along: > > ? ? import ctypes > ? ? from ctypes import wintypes As per PEP8, the only time I use from x import * is with ctypes. Don't you do this because of name clashes with wintypes? I general, the module-dot-function notation is nicer (I hate that about R, where this is almost the rule, although one could write things like reshape::melt) > ? ? _CP_UTF8 = 65001 > ? ? _CP_ACP = 0? # ANSI > ? ? _LPBOOL = ctypes.POINTER(ctypes.c_long) > > ? ? _wideCharToMultiByte = ctypes.windll.kernel32.WideCharToMultiByte > ? ? _wideCharToMultiByte.restype = ctypes.c_int > ? ? _wideCharToMultiByte.argtypes = [ > ? ? ? wintypes.UINT, wintypes.DWORD, wintypes.LPCWSTR, ctypes.c_int, > ? ? ? wintypes.LPSTR, ctypes.c_int, wintypes.LPCSTR, _LPBOOL] > > ? ? def wide2utf8(fn): > ? ? ? ? codePage = _CP_UTF8 > ? ? ? ? dwFlags = 0 > ? ? ? ? lpWideCharStr = fn > ? ? ? ? cchWideChar = len(fn) > ? ? ? ? lpMultiByteStr = None > ? ? ? ? cbMultiByte = 0? # zero requests size > ? ? ? ? lpDefaultChar = None > ? ? ? ? lpUsedDefaultChar = None > ? ? ? ? # get size > ? ? ? ? mbcssize = _wideCharToMultiByte( > ? ? ? ? ? codePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, > ? ? ? ? ? cbMultiByte, lpDefaultChar, lpUsedDefaultChar) > ? ? ? ? if mbcssize <= 0: > ? ? ? ? ? ? raise ctypes.WinError(mbcssize) > ? ? ? ? lpMultiByteStr = ctypes.create_string_buffer(mbcssize) > ? ? ? ? # convert > ? ? ? ? retcode = _wideCharToMultiByte( > ? ? ? ? ? codePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, > ? ? ? ? ? mbcssize, lpDefaultChar, lpUsedDefaultChar) > ? ? ? ? if retcode <= 0: > ? ? ? ? ? ? raise ctypes.WinError(retcode) > ? ? ? ? return lpMultiByteStr.value Awesome, thank you so much! Glad to see that my code was pretty much in the right direction, but I made some silly and some more fundamental mistakes. From dancingbush at gmail.com Thu Dec 20 21:40:12 2012 From: dancingbush at gmail.com (Ciaran Mooney) Date: Thu, 20 Dec 2012 20:40:12 +0000 Subject: [Tutor] invalid literal for int error in Game loop Message-ID: HI All, Was hoping someone could help me with a problem I am having programming a game using pygame. I am trying to return a variable with a integer value defined by user input from a function. Basically the idea is to allow the player to select the level of difficulty by selecting from a choice of keys, each key corresponding too the number of frames per second. The function code is as follows: def difficultyLevel(): FPS = '' windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),pygame.FULLSCREEN) windowSurface.fill(BACKGROUNDCOLOUR) drawText('LEVEL OF PLAY', font3, windowSurface, (WINDOWWIDTH/6), (WINDOWHEIGHT/6)) drawText('B: Your an absoulute Begineer', font3, windowSurface, (WINDOWWIDTH/6)-100, (WINDOWHEIGHT/6)+100) drawText('M: Not Too Snazy', font3, windowSurface, (WINDOWWIDTH/6)-100, (WINDOWHEIGHT/6)+150) drawText('H: Deathwish' ,font3,windowSurface, (WINDOWWIDTH/6)-100, (WINDOWHEIGHT/6)+200) pygame.display.update() for event in pygame.event.get(): if event.type == QUIT: terminate() if event.type == KEYDOWN: if event.key == ord('b'): FPS = 30 elif event.key == ord('m'): FPS = 70 elif event.key == ord('h'): FPS = 120 return FPS The function is then called and FPS converted too a integer value in the main game loop as follows: topScore = 0 while True: #set up the game and all intial key and mouse movement values as false baddies = [] #baddies as a empy list score = 0 etc FPS = int(difficultyLevel()) FPS is then used within the game loop to allow for number of frames per second in: mainClock.tick(FPS) However I keep getting the following error: ValueError: invalid literal for int() with base 10: '' Please note I am a absolute beoingeer in programming and have had no formal education to date so please excuse anything I may of left out !! Thanks Ciaran -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Dec 20 21:51:03 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 21 Dec 2012 07:51:03 +1100 Subject: [Tutor] invalid literal for int error in Game loop In-Reply-To: References: Message-ID: <50D37A37.4070805@pearwood.info> On 21/12/12 07:40, Ciaran Mooney wrote: > def difficultyLevel(): > FPS = '' Here you set the variable FPS to the empty string. > if event.type == KEYDOWN: > if event.key == ord('b'): > FPS = 30 > elif event.key == ord('m'): > FPS = 70 > elif event.key == ord('h'): > FPS = 120 These three options set FPS to an integer value. But notice that if the user does not press one of the b m or h keys, FPS never gets changed so it still has the initial value of the empty string. > return FPS > > The function is then called and FPS converted too a integer value in the main game loop as follows: > FPS = int(difficultyLevel()) > However I keep getting the following error: > > ValueError: invalid literal for int() with base 10: '' There are three possible values that difficultyLevel() may return: * if the user hits the 'h' key, it returns the integer value 120; * if the user hits the 'm' key, it returns the integer value 70; * if the user hits the 'b' key, it returns the integer value 30; * otherwise, it returns the empty string. Three of the four values are already ints and don't need to be converted; the fourth cannot be converted because it is not a numeric string. To fix this, change the line FPS = '' to a default integer value, say, FPS = 30. -- Steven From d at davea.name Thu Dec 20 22:07:40 2012 From: d at davea.name (Dave Angel) Date: Thu, 20 Dec 2012 16:07:40 -0500 Subject: [Tutor] invalid literal for int error in Game loop In-Reply-To: <50D37A37.4070805@pearwood.info> References: <50D37A37.4070805@pearwood.info> Message-ID: <50D37E1C.3070901@davea.name> On 12/20/2012 03:51 PM, Steven D'Aprano wrote: > On 21/12/12 07:40, Ciaran Mooney wrote: > >> def difficultyLevel(): >> FPS = '' > > Here you set the variable FPS to the empty string. > >> if event.type == KEYDOWN: >> if event.key == ord('b'): >> FPS = 30 >> elif event.key == ord('m'): >> FPS = 70 >> elif event.key == ord('h'): >> FPS = 120 > > These three options set FPS to an integer value. But notice that if > the user > does not press one of the b m or h keys, FPS never gets changed so it > still > has the initial value of the empty string. > >> return FPS >> >> The function is then called and FPS converted too a integer value in >> the main game loop as follows: > >> FPS = int(difficultyLevel()) > >> However I keep getting the following error: >> >> ValueError: invalid literal for int() with base 10: '' > > There are three possible values that difficultyLevel() may return: > > * if the user hits the 'h' key, it returns the integer value 120; > * if the user hits the 'm' key, it returns the integer value 70; > * if the user hits the 'b' key, it returns the integer value 30; > * otherwise, it returns the empty string. > > Three of the four values are already ints and don't need to be converted; > the fourth cannot be converted because it is not a numeric string. > > To fix this, change the line FPS = '' to a default integer value, > say, FPS = 30. > > > Or even better, remove that default value and add an else clause to the if/elif/elif section. That way it's all in one place, and it's more obvious that the return value will always be an int. And of course you can remove the int() call on the line FPS = int( difficultyLevel() ) becomes FPS = difficultyLevel() BTW, when quoting an error, please include the whole traceback, not just one line of it. Here it was obvious, but many times other parts will be very useful, or even essential. -- DaveA From ramit.prasad at jpmorgan.com Thu Dec 20 22:33:47 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 20 Dec 2012 21:33:47 +0000 Subject: [Tutor] how to control putty window In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> Ufuk Eskici wrote: > Hello, > > I run this command and opens putty: > > import os > import subprocess > command = '"c:\Program Files\Putty\putty.exe" -ssh ufukeskici at 10.10.10.10 -pw test > subprocess.Popen(command) > > But then I want to input new commands to this Putty new window. How can I do it? > Do you need to control Putty or just SSH to another computer? If all you need to SSH then I would recommend using a 3rd party module such as Fabric (which relies on Paramiko). Those modules will simply SSH significantly. They are Python 2.x but you should be able to use Paramiko in 3.x except for SFTP. This link might help to install Paramiko if you are using Python 3. https://github.com/paramiko/paramiko/issues/16 If you need to control Putty specifically then I cannot help. Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From dancingbush at gmail.com Thu Dec 20 22:34:40 2012 From: dancingbush at gmail.com (Ciaran Mooney) Date: Thu, 20 Dec 2012 21:34:40 +0000 Subject: [Tutor] invalid literal for int error in Game loop In-Reply-To: <50D37E1C.3070901@davea.name> References: <50D37A37.4070805@pearwood.info> <50D37E1C.3070901@davea.name> Message-ID: <66098356-604F-47DB-A78B-2CC859623BBE@gmail.com> Thanks for the feedback. Steve, If I set the FPS to a default of say 30, the game seems to run at this default FPS=30 regardless of the key pressed in the function. Dave, If I remove the the default value at the start of the function and add it to elif in the loop I get the following error: Traceback (most recent call last): File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 139, in FPS = difficultyLevel() File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 50, in difficultyLevel return FPS UnboundLocalError: local variable 'FPS' referenced before assignment I wondered if i could set FPS to nether a string or integer and just declare it by setting FPS=None but I get the following error: Traceback (most recent call last): File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 301, in mainClock.tick(FPS) TypeError: a float is required Cheers Ciaran On 20 Dec 2012, at 21:07, Dave Angel wrote: > On 12/20/2012 03:51 PM, Steven D'Aprano wrote: >> On 21/12/12 07:40, Ciaran Mooney wrote: >> >>> def difficultyLevel(): >>> FPS = '' >> >> Here you set the variable FPS to the empty string. >> >>> if event.type == KEYDOWN: >>> if event.key == ord('b'): >>> FPS = 30 >>> elif event.key == ord('m'): >>> FPS = 70 >>> elif event.key == ord('h'): >>> FPS = 120 >> >> These three options set FPS to an integer value. But notice that if >> the user >> does not press one of the b m or h keys, FPS never gets changed so it >> still >> has the initial value of the empty string. >> >>> return FPS >>> >>> The function is then called and FPS converted too a integer value in >>> the main game loop as follows: >> >>> FPS = int(difficultyLevel()) >> >>> However I keep getting the following error: >>> >>> ValueError: invalid literal for int() with base 10: '' >> >> There are three possible values that difficultyLevel() may return: >> >> * if the user hits the 'h' key, it returns the integer value 120; >> * if the user hits the 'm' key, it returns the integer value 70; >> * if the user hits the 'b' key, it returns the integer value 30; >> * otherwise, it returns the empty string. >> >> Three of the four values are already ints and don't need to be converted; >> the fourth cannot be converted because it is not a numeric string. >> >> To fix this, change the line FPS = '' to a default integer value, >> say, FPS = 30. >> >> >> > > Or even better, remove that default value and add an else clause to the > if/elif/elif section. That way it's all in one place, and it's more > obvious that the return value will always be an int. > > And of course you can remove the int() call on the line > FPS = int( difficultyLevel() ) > becomes > FPS = difficultyLevel() > > BTW, when quoting an error, please include the whole traceback, not just > one line of it. Here it was obvious, but many times other parts will be > very useful, or even essential. > > > > -- > > DaveA > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Dec 20 22:53:05 2012 From: d at davea.name (Dave Angel) Date: Thu, 20 Dec 2012 16:53:05 -0500 Subject: [Tutor] invalid literal for int error in Game loop In-Reply-To: <66098356-604F-47DB-A78B-2CC859623BBE@gmail.com> References: <50D37A37.4070805@pearwood.info> <50D37E1C.3070901@davea.name> <66098356-604F-47DB-A78B-2CC859623BBE@gmail.com> Message-ID: <50D388C1.2030804@davea.name> On 12/20/2012 04:34 PM, Ciaran Mooney wrote: > Thanks for the feedback. > > Steve, If I set the FPS to a default of say 30, the game seems to run at this default FPS=30 regardless of the key pressed in the function. > > Dave, If I remove the the default value at the start of the function and add it to elif in the loop I get the following error: > > > Traceback (most recent call last): > File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 139, in > FPS = difficultyLevel() > File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 50, in difficultyLevel > return FPS > UnboundLocalError: local variable 'FPS' referenced before assignment > Please don't top-post. Your message is now out of order with whatever you're quoting below. You don't show your new version of code, but clearly, your else is NOT in the right place. If it were, then there would be no way that FPS would be undefined. And this is exactly why it's a good thing to use else rather than defining FPS at the top. The error message shows you your error, or at lest makes you think more about the problem. You have a for-loop here. Is it possible that it's not executing at all? If so, what value do you want for FPS? Do you just want to wait till the user causes SOME event? or what? And what about if you go around the loop multiple times? Do you want the last value of FPS, or the first? Assuming you want to take the first matching event that's waiting, you could do this. The breaks get you out of the loop for event in pygame.event.get(): if event.type == QUIT: terminate() if event.type == KEYDOWN: if event.key == ord('b'): FPS = 30 break elif event.key == ord('m'): FPS = 70 break elif event.key == ord('h'): FPS = 120 break else: FPS = 30 #default value return FPS This doesn't address the problem of the user not being fast enough at hitting that key. Perhaps you want to wait till he hits something, or till a timeout has passed, or something? Without a clear spec in your head, the fact that we could help you get your code to not crash is not the same as getting it right. -- DaveA From ramit.prasad at jpmorgan.com Thu Dec 20 22:53:48 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Thu, 20 Dec 2012 21:53:48 +0000 Subject: [Tutor] invalid literal for int error in Game loop In-Reply-To: <66098356-604F-47DB-A78B-2CC859623BBE@gmail.com> References: <50D37A37.4070805@pearwood.info> <50D37E1C.3070901@davea.name> <66098356-604F-47DB-A78B-2CC859623BBE@gmail.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF474180A48AA@SCACMX008.exchad.jpmchase.net> Ciaran Mooney wrote: > Sent: Thursday, December 20, 2012 3:35 PM > To: d at davea.name > Cc: tutor at python.org > Subject: Re: [Tutor] invalid literal for int error in Game loop > > Thanks for the feedback. > > Steve, If I set the FPS to a default of say 30, the game seems to run at this default FPS=30 regardless of the > key pressed in the function. > > Dave, If I remove the the default value at the start of the function and add it to elif in the loop I get the > following error: > > > Traceback (most recent call last): > ? File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 139, in > > ? ? FPS = difficultyLevel() > ? File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 50, in > difficultyLevel > ? ? return FPS > UnboundLocalError: local variable 'FPS' referenced before assignment If you read Dave's comment a little closer you will see that he suggests adding an *else* block not another elif. (pseudocode) if key == b: FPS = 30 elif key == m: FPS = 60 elif key == h: FPS = 120 else: FPS = 15 # or whatever default value you want > > > I wondered if i could set FPS to nether a string or integer and just declare it by setting FPS=None but I get > the following error: > > Traceback (most recent call last): > ? File "/Users/callanmooneys/Desktop/Pythoin Programmes/Aliens Game/dodger ver 3 instructons.py", line 301, in > > ? ? mainClock.tick(FPS) > TypeError: a float is required The else clause prevents FPS from being anything other than a number. You can choose to predefine FPS or just let the if/else tree take care of defining it. > > Cheers > Ciaran This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From kbailey at howlermonkey.net Fri Dec 21 00:46:15 2012 From: kbailey at howlermonkey.net (Kirk Bailey) Date: Thu, 20 Dec 2012 18:46:15 -0500 Subject: [Tutor] playing mp3 files Message-ID: <50D3A347.4020603@howlermonkey.net> the robotradio program is coming along. It plays. it dances. it sings. IT selects random libraries and random broadcast files. and it restarts itself with consummate ease. Aka, NOW wtf?!? Jim is exchanging letters with me on this, but does not yet have a solution. Here's the code of the hour: BEWARE WORD WRAP!!! #!c:\Python25\pythonw.exe # Copyright (c) 2011 by James K. Lawless # jimbo at radiks.net http://www.mailsend-online.com # License: MIT / X11 # See: http://www.mailsend-online.com/license.php # for full license details. # from ctypes import * import os, os.path, sys, time, glob, random import cgitb; cgitb.enable() # # there are 2 directories under this one- com and id. There are also numbered folders which # contain files of old broadcasts. The system only referrs to number name directories to play programs # # # # # # # # # print "Content-Type: text/html \n" print "Robot Radio WHEN" print '' print '

Robot Radio WHEN


' # winmm = windll.winmm # def mciSend(s): i=winmm.mciSendStringA(s,0,0,0) if i<>0: print "Error %d in mciSendString %s" % ( i, s ) # def playMP3(mp3Name): mciSend("Close All") mciSend("Open \"%s\" Type MPEGVideo Alias theMP3" % mp3Name) mciSend("Play theMP3 Wait") mciSend("Close theMP3") # def RandomFile(): files=glob.glob("*.mp3") # files is a list of the CURRENT file=random.choice(files) # directories contents print "file=",file,"
" return file os.chdir("./RobotRadio_WHEN/") # starts in cgi-bin, move to application's # # home directory. print "- - - - - - - - - - - - - - - - - - - - - - - -

" while 1: # this is an endless loop. Get creative to stop it. # ok, let's do the loop. os.chdir("./ID") # go to the station identification folder playMP3(RandomFile()) # and pick one at random and play it. time.sleep(5) # a breif pause os.chdir("../com") # now back go home, then on to the commercial folder playMP3(RandomFile()) # pick one at random and play it. time.sleep(5) # another pause os.chdir("..") # this takes us back to the homne of the application #print "cwd=",os.getcwd(),"
" # test code, dummy out later dirlist=glob.glob("*") # get a listing of the directories in this directory os.chdir(str(1+int(random.random()*len(dirlist)-2))) # randomly select a directory and go to it. print "cwd=",os.getcwd() # test code, dummy out later # # now we pick a program library, then # # pick a program in it at random and play it. # mp3file=RandomFile() print "program=",mp3file,"
" # os/chdir("./1") #select a program library randomly and make it current. print "cwd=",os.getcwd(),"
" # we will log this to the display screen on the console running this program print "cwd=",os.getcwd()," play ",mp3file," AT: ", time.strftime("%a, %d %b %Y %I:%M %p", time.localtime()) playMP3(mp3file) os.chdir("..") # go back to the home directory print "- - - - - - - - - - - - - - - - - - - - - - - -

" # Above plays a file named in the variable -mp3file- time.sleep(5) # this gives us some time to do a CNTL-C to break the loop. sys.exit() # maybe. -- -Shaboom. Kirk Bailey CEO, Freehold Marketing LLC http://www.OneBuckHosting.com/ Fnord! From eryksun at gmail.com Fri Dec 21 05:37:49 2012 From: eryksun at gmail.com (eryksun) Date: Thu, 20 Dec 2012 23:37:49 -0500 Subject: [Tutor] sys.getfilesystemencoding() In-Reply-To: <1356026514.60293.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <1355836438.19097.YahooMailNeo@web163806.mail.gq1.yahoo.com> <1355913839.41344.YahooMailNeo@web163806.mail.gq1.yahoo.com> <1356026514.60293.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On Thu, Dec 20, 2012 at 1:01 PM, Albert-Jan Roskam wrote: > >> You shouldn't encode a string argument you've declared as c_wchar_p >> (i.e. wintypes.LPCWSTR, i.e. type 'Z'). > > Ok, yes, that was plain stupid of me. On Windows, CPython 3.3 presents a twist. sizeof(c_wchar) is 2, but sys.maxunicode is 1114111. In general you'd have to test for this case and add the number of surrogates (i.e. characters with ord > U+FFFF) to the length. However, since ctypes.c_wchar_p (i.e. wintypes.LPCWSTR) creates a null-terminated buffer (16-bit NUL, U+0000), you can set cchWideChar = -1. Here's what MSDN says: If this parameter is -1, the function processes the entire input string, including the terminating null character. Therefore, the resulting character string has a terminating null character, and the length returned by the function includes this character. Including the null in lpMultiByteStr is fine. The "value" descriptor of the c_char array stops at the first null byte when creating a Python string (bytes in 3.x). To get the whole string use the "raw" descriptor. >> import ctypes >> from ctypes import wintypes > > As per PEP8, the only time I use from x import * is with ctypes. Don't you > do this because of name clashes with wintypes? I see no compelling reason to repeatedly type ctypes.wintypes instead of just wintypes. As far as import * goes, wintypes doesn't override ctypes. Mostly it defines type aliases and structures. http://hg.python.org/cpython/file/8803c3d61da2/Lib/ctypes/wintypes.py From ufukeskici at gmail.com Fri Dec 21 15:44:39 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Fri, 21 Dec 2012 16:44:39 +0200 Subject: [Tutor] how to control putty window In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> Message-ID: Hello, I changed my way. This time I'm using "plink" under Putty. My python code is: os.chdir("c:\\Program Files\\Putty") cmd = "plink -ssh -l ufuk10.10.10.10 -pw password" process = subprocess.Popen(cmd) inputdata="r van" result = process.communicate(inputdata) But after the successful SSH, I cannot continue, no command runs: This is the new output after the initial SSH connection: No data input. Last login: Fri Dec 21 16:27: ufuk at home-ubuntu:~$ 2012/12/20 Prasad, Ramit > Ufuk Eskici wrote: > > Hello, > > > > I run this command and opens putty: > > > > import os > > import subprocess > > command = '"c:\Program Files\Putty\putty.exe" -ssh > ufukeskici at 10.10.10.10 -pw test > > subprocess.Popen(command) > > > > But then I want to input new commands to this Putty new window. How can > I do it? > > > > Do you need to control Putty or just SSH to another computer? > If all you need to SSH then I would recommend using a 3rd > party module such as Fabric (which relies on Paramiko). Those modules > will simply SSH significantly. They are Python 2.x but you should > be able to use Paramiko in 3.x except for SFTP. This link might help > to install Paramiko if you are using Python 3. > https://github.com/paramiko/paramiko/issues/16 > > If you need to control Putty specifically then I cannot help. > > > Ramit > > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of > securities, accuracy and completeness of information, viruses, > confidentiality, legal privilege, and legal entity disclaimers, > available at http://www.jpmorgan.com/pages/disclosures/email. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com Fri Dec 21 16:59:52 2012 From: ramit.prasad at jpmorgan.com (Prasad, Ramit) Date: Fri, 21 Dec 2012 15:59:52 +0000 Subject: [Tutor] how to control putty window In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF474180A7CDD@SCACMX008.exchad.jpmchase.net> Ufuk Eskici wrote: > Hello, > > I changed my way. This time I'm using "plink" under Putty. > > My python code is: > > os.chdir("c:\\Program Files\\Putty") > cmd = "plink -ssh -l ufuk10.10.10.10 -pw password" > process = subprocess.Popen(cmd) > inputdata="r van" > result = process.communicate(inputdata) > > But after the successful SSH, I cannot continue, no command runs: > This is the new output after the initial SSH connection: > No data input. > > Last login: Fri Dec 21 16:27: > ufuk at home-ubuntu:~$ I am not familiar with plink, so I cannot help you. I recommend using an SSH module which help a lot with all of this. That being said, maybe this post will help: http://code.activestate.com/lists/python-tutor/74807/ Also take a look at the subprocess.communicate documentation[1] as it says """ Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. **Wait for process to terminate**. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child. communicate() returns a tuple (stdoutdata, stderrdata). Note that if you want to send data to the process's stdin, you need to create the Popen object with stdin=PIPE. Similarly, to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE too. """ (emphasis added) This suggests communicate is waiting for the plink to end? Also, you should probably pass in a pipe so that you can send data more than once. Note, I am not familiar with subprocess so YMMV. [1]http://docs.python.org/2.7/library/subprocess.html#subprocess.Popen.communicate Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ufukeskici at gmail.com Fri Dec 21 18:00:31 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Fri, 21 Dec 2012 19:00:31 +0200 Subject: [Tutor] how to control putty window In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474180A7CDD@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF474180A7CDD@SCACMX008.exchad.jpmchase.net> Message-ID: I used this code: os.chdir("c:\\Program Files\\Putty") cmd = "plink -ssh -l ufuk 10.10.10.10 -pw password" process = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate () After running this code, just one black cmd screen appears, but it frezzes and doesn't continue. I close the window manually and when I write - *print (stdout)* - I can get some output *"......ufuk at home-ubuntu:~$ "* - as a long string. But I dont know why it freezes and why I cannot input anything. How should I continue? 2012/12/21 Prasad, Ramit > Ufuk Eskici wrote: > > Hello, > > > > I changed my way. This time I'm using "plink" under Putty. > > > > My python code is: > > > > os.chdir("c:\\Program Files\\Putty") > > cmd = "plink -ssh -l ufuk10.10.10.10 -pw password" > > process = subprocess.Popen(cmd) > > inputdata="r van" > > result = process.communicate(inputdata) > > > > But after the successful SSH, I cannot continue, no command runs: > > This is the new output after the initial SSH connection: > > No data input. > > > > Last login: Fri Dec 21 16:27: > > ufuk at home-ubuntu:~$ > > I am not familiar with plink, so I cannot help you. I recommend using > an SSH module which help a lot with all of this. That being said, maybe > this post will help: > http://code.activestate.com/lists/python-tutor/74807/ > > Also take a look at the subprocess.communicate documentation[1] as > it says > """ Interact with process: Send data to stdin. Read data from stdout and > stderr, until end-of-file is reached. **Wait for process to terminate**. > The optional input argument should be a string to be sent to the child > process, or None, if no data should be sent to the child. > communicate() returns a tuple (stdoutdata, stderrdata). > Note that if you want to send data to the process's stdin, you need to > create the Popen object with stdin=PIPE. Similarly, to get anything other > than None in the result tuple, you need to give stdout=PIPE and/or > stderr=PIPE too. > """ (emphasis added) > This suggests communicate is waiting for the plink to end? Also, you > should probably pass in a pipe so that you can send data more than > once. Note, I am not familiar with subprocess so YMMV. > > [1] > http://docs.python.org/2.7/library/subprocess.html#subprocess.Popen.communicate > > > > Ramit > > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of > securities, accuracy and completeness of information, viruses, > confidentiality, legal privilege, and legal entity disclaimers, > available at http://www.jpmorgan.com/pages/disclosures/email. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Fri Dec 21 18:12:03 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 21 Dec 2012 12:12:03 -0500 Subject: [Tutor] how to control putty window In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> Message-ID: On Fri, Dec 21, 2012 at 9:44 AM, Ufuk Eskici wrote: > > cmd = "plink -ssh -l ufuk10.10.10.10 -pw password" > process = subprocess.Popen(cmd) > inputdata="r van" > result = process.communicate(inputdata) > > But after the successful SSH, I cannot continue, no command runs: To use communicate(), you need to set one or more of the standard streams to a file or pipe (e.g. stdout=subprocess.PIPE). That said, if you just have a single command, it's simpler to have the ssh client execute it. Get the result using check_output (it sets up and calls communicate): user = 'ufuk' password = 'password' host = "10.10.10.10" remote_cmd = 'r van' cmd = ['plink', '-ssh', '-l', user, '-pw', password, '"%s"' % remote_cmd] result = subprocess.check_output(cmd, stdin=subprocess.PIPE) I had to add stdin=subprocess.PIPE when trying this interactively. Otherwise plink leaves the console stdin in an unusable state. This is probably the source of the lockup you're getting. If you need an interactive, stateful session, then communicate() won't help since it closes the streams. You'll have to roll your own by manually handling the stdin/stdout pipes. That means you'll need a background thread to get around readline blocking (select only works for sockets on Windows). You'll hit a brick wall with this approach if the program uses full buffering in a pipe. With Linux you can sometimes tweak the streams using stdbuf, but not if the program uses setvbuf(). To get around this in Unix you can use pexpect to fake a tty. I think that's only available on Windows via Cygwin. From steve at pearwood.info Sat Dec 22 00:57:55 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 22 Dec 2012 10:57:55 +1100 Subject: [Tutor] Your thoughts on using Python 2.4.x? In-Reply-To: References: Message-ID: <50D4F783.9020608@pearwood.info> On 19/12/12 14:54, boB Stepp wrote: > Now after the upgrades some machines now have Python 2.4.4 > and others Python 2.4.6. For the purposes of creating/manipulating > text files and running Solaris-flavored Unix commands, is there > anything I should be specially aware of? I have been working entirely > in my Python studies with version 3.x up to this point. I understand > that there are differences between 2.x versus 3.x such as print > statements versus print functions, etc. Yes; using Python 2.4 is painful compared to Python 3.x because it is missing so many cool and useful features. 2.4 is quite old now, and there have been many, many bug-fixes and new features added since then. Some of those missing features can be easily added into your own code. Because I have to support 2.4, I have a compatibility module to backport the more essential features. http://code.google.com/p/my-startup-file/source/browse/backports.py > BTW, does 2.4.x come with Tkinter standard? Depends on what you mean by standard. Tkinter is part of the standard library, but it requires external dependencies which may not be present. If your Solaris system doesn't include tcl ("tickle") then you won't have Tkinter either. At the Solaris prompt, enter "tclsh". You should get a % prompt, which you can quit by entering "exit" command. If you don't, then you do not have tcl installed and won't have Tkinter either. -- Steven From steve at pearwood.info Sat Dec 22 02:34:42 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 22 Dec 2012 12:34:42 +1100 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: Message-ID: <50D50E32.1080706@pearwood.info> On 18/12/12 01:36, Oscar Benjamin wrote: > I think it's unfortunate that Python's int() function combines two > distinct behaviours in this way. In different situations int() is used > to: > 1) Coerce an object of some type other than int into an int without > changing the value of the integer that the object represents. The second half of the sentence (starting from "without changing") is not justified. You can't safely make that assumption. All you know is that calling int() on an object is intended to convert the object to an int, in whatever way is suitable for that object. In some cases, that will be numerically exact (e.g. int("1234") will give 1234), in other cases it will not be. > 2) Round an object with a non-integer value to an integer value. int() does not perform rounding (except in the most generic sense that any conversion from real-valued number to integer is "rounding"). That is what the round() function does. int() performs truncating: it returns the integer part of a numeric value, ignoring any fraction part: py> from decimal import Decimal as D py> from fractions import Fraction as F py> int(D("-123.99999999")) -123 py> int(F(999, 100)) 9 So you shouldn't think of int(number) as "convert number to an int", since that is ambiguous. There are at least six common ways to convert arbitrary numbers to ints: * truncate, or round towards zero (drop any fraction part); * floor, or round towards -infinity (always round down); * ceiling, or round towards +infinity (always round up); * round to nearest, with ties rounding up; * round to nearest, with ties rounding down; * banker's rounding (round to nearest, with ties rounding to the nearest even number) Python provides truncation via the int and math.trunc functions, floor and ceiling via math.floor and math.ceil, and round to nearest via round. In Python 2, ties are rounded up, which is biased; in Python 3, the unbiased banker's rounding is used. Instead, you should consider int(number) to be one of a pair of functions, "return integer part", "return fraction part", where unfortunately the second function isn't provided directly. In general though, you can get the fractional part of a number with "x % 1". For floats, math.modf also works. So, in a sense int() does to double-duty as both a constructor of ints from non-numbers such as strings, and as a "get integer part" function for numbers. I'm okay with that. -- Steven From robertvstepp at gmail.com Sat Dec 22 03:00:55 2012 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 21 Dec 2012 20:00:55 -0600 Subject: [Tutor] Your thoughts on using Python 2.4.x? In-Reply-To: <50D4F783.9020608@pearwood.info> References: <50D4F783.9020608@pearwood.info> Message-ID: On Fri, Dec 21, 2012 at 5:57 PM, Steven D'Aprano wrote: > Yes; using Python 2.4 is painful compared to Python 3.x because it is > missing so many cool and useful features. 2.4 is quite old now, and > there have been many, many bug-fixes and new features added since then. > > Some of those missing features can be easily added into your own code. > Because I have to support 2.4, I have a compatibility module to backport > the more essential features. > > http://code.google.com/p/my-startup-file/source/browse/backports.py Thanks! I will look into your code in more depth once I get deeper than I currently am in Python. More importantly you've given me a useful approach that I can use for many things (potentially). This type of approach had not yet occurred to me. >> BTW, does 2.4.x come with Tkinter standard? > > > > Depends on what you mean by standard. Tkinter is part of the standard > library, but it requires external dependencies which may not be present. > If your Solaris system doesn't include tcl ("tickle") then you won't have > Tkinter either. > > At the Solaris prompt, enter "tclsh". You should get a % prompt, which you > can quit by entering "exit" command. If you don't, then you do not have > tcl installed and won't have Tkinter either. Thanks for pointing this out and how to check for tcl being installed. I will have to check for this after the holidays. boB From steve at pearwood.info Sat Dec 22 03:06:44 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 22 Dec 2012 13:06:44 +1100 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: Message-ID: <50D515B4.9050706@pearwood.info> Oh, another comment... On 18/12/12 01:36, Oscar Benjamin wrote: > I have often found myself writing awkward functions to prevent a > rounding error from occurring when coercing an object with int(). > Here's one: > > def make_int(obj): > '''Coerce str, float and int to int without rounding error > Accepts strings like '4.0' but not '4.1' > ''' > fnum = float('%s' % obj) > inum = int(fnum) > assert inum == fnum > return inum Well, that function is dangerously wrong. In no particular order, I can find four bugs and one design flaw. 1) It completely fails to work as advertised when Python runs with optimizations on: [steve at ando python]$ cat make_int.py def make_int(obj): '''Coerce str, float and int to int without rounding error Accepts strings like '4.0' but not '4.1' ''' fnum = float('%s' % obj) inum = int(fnum) assert inum == fnum return inum print make_int('4.0') print make_int('4.1') # this should raise an exception [steve at ando python]$ python -O make_int.py 4 4 2) Even when it does work, it is misleading and harmful to raise AssertionError. The problem is with the argument's *value*, hence *ValueError* is the appropriate exception, not ImportError or TypeError or KeyError ... or AssertionError. Don't use assert as a lazy way to get error checking for free. 3) Worse, it falls over when given a sufficiently large int value: py> make_int(10**500) Traceback (most recent call last): File "", line 1, in File "", line 6, in make_int OverflowError: cannot convert float infinity to integer but at least you get an exception to warn you that something has gone wrong. 4) Disturbingly, the function silently does the wrong thing even for exact integer arguments: py> n = 10**220 # an exact integer value py> make_int(n) == n False 5) It loses precision for string values: py> s = "1"*200 py> make_int(s) % 10 8L And not by a little bit: py> make_int(s) - int(s) # should be zero 13582401819835255060712844221836126458722074364073358155901190901 52694241435026881979252811708675741954774190693711429563791133046 96544199238575935688832088595759108887701431234301497L Lest you think that it is only humongous numbers where this is a problem, it is not. A mere seventeen digits is enough: py> s = "10000000000000001" py> make_int(s) - int(s) -1L And at that point I stopped looking for faults. -- Steven From oscar.j.benjamin at gmail.com Sat Dec 22 18:38:53 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 22 Dec 2012 17:38:53 +0000 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: <50D50E32.1080706@pearwood.info> References: <50D50E32.1080706@pearwood.info> Message-ID: On 22 December 2012 01:34, Steven D'Aprano wrote: > On 18/12/12 01:36, Oscar Benjamin wrote: > >> I think it's unfortunate that Python's int() function combines two >> distinct behaviours in this way. In different situations int() is used >> to: >> 1) Coerce an object of some type other than int into an int without >> changing the value of the integer that the object represents. > > The second half of the sentence (starting from "without changing") is not > justified. You can't safely make that assumption. All you know is that > calling int() on an object is intended to convert the object to an int, > in whatever way is suitable for that object. In some cases, that will > be numerically exact (e.g. int("1234") will give 1234), in other cases it > will not be. If I was to rewrite that sentence would replace the word 'integer' with 'number' but otherwise I'm happy with it. Your reference to "numerically exact" shows that you understood exactly what I meant. >> 2) Round an object with a non-integer value to an integer value. > > > int() does not perform rounding (except in the most generic sense that any > conversion from real-valued number to integer is "rounding"). That is what > the round() function does. int() performs truncating: it returns the > integer part of a numeric value, ignoring any fraction part: I was surprised by your objection to my use of the word "rounding" here. So I looked it up on Wikipedia: http://en.wikipedia.org/wiki/Rounding#Rounding_to_integer That section describes "round toward zero (or truncate..." which is essentially how I would have put it, and also how you put it below: > > * truncate, or round towards zero (drop any fraction part); So I'm not really sure what your objection is to that, though you are free to prefer the word truncate to round in this case (and I am free to disagree). > So you shouldn't think of int(number) as "convert number to an int", since > that is ambiguous. There are at least six common ways to convert arbitrary > numbers to ints: This is precisely my point. I would prefer if if int(obj) would fail on non-integers leaving me with the option of calling an appropriate rounding function. After catching RoundError (or whatever) you would know that you have a number type object that can be passed to round, ceil, floor etc. > Python provides truncation via the int and math.trunc functions, floor and > ceiling via math.floor and math.ceil, and round to nearest via round. > In Python 2, ties are rounded up, which is biased; in Python 3, the > unbiased banker's rounding is used. I wasn't aware of this change. Thanks for that. > Instead, you should consider int(number) to be one of a pair of functions, > "return integer part", "return fraction part", where unfortunately the > second function isn't provided directly. In general though, you can get > the fractional part of a number with "x % 1". For floats, math.modf also > works. Assuming that you know you have an object that supports algebraic operations in a sensible way then this works, although the complementary function for "x % 1" would be "x // 1" or "math.floor(x)" rather than "int(x)". To get the complementary function for "int(x)" you could do "math.copysign(abs(x) % 1, x)" (maybe there's a simpler way): $ python Python 2.7.3 (default, Sep 26 2012, 21:51:14) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> def reconstruct(x): ... return int(x) + x % 1 ... >>> reconstruct(1) 1 >>> reconstruct(1.5) 1.5 >>> reconstruct(-2) -2 >>> reconstruct(-2.5) -1.5 > So, in a sense int() does to double-duty as both a constructor of ints > from non-numbers such as strings, and as a "get integer part" function for > numbers. I'm okay with that. And I am not. Oscar From oscar.j.benjamin at gmail.com Sat Dec 22 18:57:06 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 22 Dec 2012 17:57:06 +0000 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: <50D515B4.9050706@pearwood.info> References: <50D515B4.9050706@pearwood.info> Message-ID: On 22 December 2012 02:06, Steven D'Aprano wrote: > On 18/12/12 01:36, Oscar Benjamin wrote: > >> I have often found myself writing awkward functions to prevent a >> rounding error from occurring when coercing an object with int(). >> Here's one: >> >> def make_int(obj): >> '''Coerce str, float and int to int without rounding error >> Accepts strings like '4.0' but not '4.1' >> ''' >> fnum = float('%s' % obj) >> inum = int(fnum) >> assert inum == fnum >> return inum > > Well, that function is dangerously wrong. In no particular order, > I can find four bugs and one design flaw. I expected someone to object to this function. I had hoped that they might also offer an improved version, though. I can't see a good way to do this without special casing the treatment of some or other type (the obvious one being str). Although you have listed 5 errors I would have written the same list as 2 errors: 1) You don't like my use of assert. 2) The function doesn't work for large numbers (bigger than around 100000000000000000). I would also add: 3) It's ridiculous to convert types several times just to convert to an integer without rounding. Whether or not assert is appropriate depends on the context (although I imagine that some people would disapprove of it always). I would say that if you are using assert then it should really be in a situation where you're not really looking to handle errors but just to abort the program and debug when something goes wrong. In that context I think that, far from being confusing, assert statements make it plainly clear what the programmer who wrote them was meaning to do. > Lest you think that it is only humongous numbers where this is a > problem, it is not. A mere seventeen digits is enough: > > py> s = "10000000000000001" > py> make_int(s) - int(s) > -1L I think that most people would consider one hundred thousand million million and one to be a fairly big number. Oscar From fomcl at yahoo.com Sat Dec 22 21:53:56 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 22 Dec 2012 12:53:56 -0800 (PST) Subject: [Tutor] regex: matching unicode Message-ID: <1356209636.65123.YahooMailNeo@web163806.mail.gq1.yahoo.com> Hi, Is the code below the only/shortest way to match unicode characters? I would like to match whatever is defined as a character in the unicode reference database. So letters in the broadest sense of the word, but not digits, underscore or whitespace. Until just now, I was convinced that the re.UNICODE flag generalized the [a-z] class to all unicode letters, and that the absence of re.U was an implicit 're.ASCII'. Apparently that mental model was *wrong*. But [^\W\s\d_]+ is kind of hard to read/write. import re s = unichr(956)? # mu sign m = re.match(ur"[^\W\s\d_]+", s, re.I | re.U) Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ?~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? From farrukhali2010 at live.com Sat Dec 22 22:02:10 2012 From: farrukhali2010 at live.com (Farrukh Ali) Date: Sat, 22 Dec 2012 21:02:10 +0000 Subject: [Tutor] =?utf-8?q?check_it_for_me?= Message-ID: Hi Tutor, I am new to python, and a novice in the world of programming, I am learning python from learnpythonthehardway, and now a little bit confused in exercise 3, where the author of the book wants us to rewrite the ex3.py by saying Rewrite ex3.py to use floating point numbers so it?s more accurate (hint: 20.0 is floating point). so I have written the source code in this manner using floating points, here it is: print "I will now count my chickens:" print "Hens",25+30.0/6 print "Roosters",100 - 25.0 * 3.0 % 4 print "Now I will count the eggs:" print 3+2+1-5.0+4.0%2-1/4.0+6 print "Is it true that 3+2<5-7?" print 3+2<5-7 print "What is 3+2?",3+2 print "What is 5-7?",5-7 print "Oh, that's why it's False." print "How about some more." print "Is it greater?", 5>-2 print "Is it greater or equal?", 5>=-2 print "Is it less or equal?", 5<=-2 print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 kindly check that whether I have done right? thanks a lot waiting for ur reply Sent from Windows Mail -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Sat Dec 22 22:54:53 2012 From: d at davea.name (Dave Angel) Date: Sat, 22 Dec 2012 16:54:53 -0500 Subject: [Tutor] check it for me In-Reply-To: References: Message-ID: <50D62C2D.5020004@davea.name> On 12/22/2012 04:02 PM, Farrukh Ali wrote: > Hi Tutor, > > I am new to python, and a novice in the world of programming, I am learning python from learnpythonthehardway, and now a little bit confused in exercise 3, where the author of the book wants us to rewrite the ex3.py by saying > Rewrite ex3.py to use floating point numbers so it?s more accurate (hint: 20.0 is floating point). > > so I have written the source code in this manner using floating points, here it is: > Welcome to the forum. I hope you enjoy learning Python, and we'll try to help as much as we can. Please send your mail as plain text. Your html mail was triple spaced, and very hard to make sense of. I've removed the extra spaces below. > > > print "I will now count my chickens:" > print "Hens",25+30.0/6 > print "Roosters",100 - 25.0 * 3.0 % 4 > print "Now I will count the eggs:" > print 3+2+1-5.0+4.0%2-1/4.0+6 > print "Is it true that 3+2<5-7?" > print 3+2<5-7 > print "What is 3+2?",3+2 > print "What is 5-7?",5-7 > print "Oh, that's why it's False." > print "How about some more." > print "Is it greater?", 5>-2 > print "Is it greater or equal?", 5>=-2 > print "Is it less or equal?", 5<=-2 > print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 > > > kindly check that whether I have done right? To be honest, I don't understand what the author meant by "to use floating point numbers so it?s more accurate" For whole numbers, ints are always at least as accurate as floats. Once you start dividing those ints, you'll probably need floats or some other equivalent non-integer. So you have a formula for eggs which gives a non-integer; I'm not sure what sense that makes. Perhaps if you also showed us the original ex3.py, it'd make more sense. Or maybe somebody else reading your message has downloaded the course. It is also usually useful to state what Python version you're using (presumably 2.7) and what version of Windows.For example, the divide operator is defined differently in python 3.x -- DaveA From farrukhali2010 at live.com Sat Dec 22 23:38:59 2012 From: farrukhali2010 at live.com (Farrukh Ali) Date: Sun, 23 Dec 2012 03:38:59 +0500 Subject: [Tutor] check it for me Message-ID: Hi, i am using ActivePython 2.7.2.5, and windows 8 professional. well the original ex3.py is: print "I will now count my chickens:" print "Hens", 25 + 30 / 6 print "Roosters", 100 - 25 * 3 % 4 print "Now I will count the eggs:" print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 print "Is it true that 3 + 2 < 5 - 7?" print 3 + 2 < 5 - 7 print "What is 3 + 2?", 3 + 2 print "What is 5 - 7?", 5 - 7 print "Oh, that's why it's False." print "How about some more." print "Is it greater?", 5 > -2 print "Is it greater or equal?", 5 >= -2 print "Is it less or equal?", 5 <= -2 And i have made ex3.py this way below print "I will now count my chickens:" print "Hens",25+30.0/6 print "Roosters",100 - 25.0 * 3.0 % 4 print "Now I will count the eggs:" print 3+2+1-5.0+4.0%2-1/4.0+6 print "Is it true that 3+2<5-7?" print 3+2<5-7 print "What is 3+2?",3+2 print "What is 5-7?",5-7 print "Oh, that's why it's False." print "How about some more." print "Is it greater?", 5>-2 print "Is it greater or equal?", 5>=-2 print "Is it less or equal?", 5<=-2 The author has given us extra credit to practice so here it is and the 5th point below, which i was asking. Extra Credit 1. Above each line, use the # to write a comment to yourself explaining what the line does. 2. Remember in Exercise 0 when you started python? Start python this way again and using the above characters and what you know, use python as a calculator. 3. Find something you need to calculate and write a new .py file that does it. 4. Notice the math seems "wrong"? There are no fractions, only whole numbers. Find out why by researching what a "floating point" number is. 5. Rewrite ex3.py to use floating point numbers so it's more accurate (hint: 20.0 is floating point). -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Sat Dec 22 23:52:06 2012 From: d at davea.name (Dave Angel) Date: Sat, 22 Dec 2012 17:52:06 -0500 Subject: [Tutor] check it for me In-Reply-To: References: Message-ID: <50D63996.9080407@davea.name> On 12/22/2012 05:38 PM, Farrukh Ali wrote: > Hi, i am using ActivePython 2.7.2.5, and windows 8 professional. > well the original ex3.py is: > print "I will now count my chickens:" > print "Hens", 25 + 30 / 6 > print "Roosters", 100 - 25 * 3 % 4 > print "Now I will count the eggs:" > print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 > print "Is it true that 3 + 2 < 5 - 7?" > print 3 + 2 < 5 - 7 > print "What is 3 + 2?", 3 + 2 > print "What is 5 - 7?", 5 - 7 > print "Oh, that's why it's False." > print "How about some more." > print "Is it greater?", 5 > -2 > print "Is it greater or equal?", 5 >= -2 > print "Is it less or equal?", 5 <= -2 > > And i have made ex3.py this way below > > print "I will now count my chickens:" > print "Hens",25+30.0/6 > print "Roosters",100 - 25.0 * 3.0 % 4 > print "Now I will count the eggs:" > print 3+2+1-5.0+4.0%2-1/4.0+6 > print "Is it true that 3+2<5-7?" > print 3+2<5-7 > print "What is 3+2?",3+2 > print "What is 5-7?",5-7 > print "Oh, that's why it's False." > print "How about some more." > print "Is it greater?", 5>-2 > print "Is it greater or equal?", 5>=-2 > print "Is it less or equal?", 5<=-2 > > > The author has given us extra credit to practice so here it is and the > 5th point below, which i was asking. > > Extra Credit > 1. Above each line, use the # to write a comment to yourself > explaining what the line does. > 2. Remember in Exercise 0 when you started python? Start python this > way again and using the above characters > and what you know, use python as a calculator. > 3. Find something you need to calculate and write a new .py file that > does it. > 4. Notice the math seems "wrong"? There are no fractions, only whole > numbers. Find out why by researching > what a "floating point" number is. > 5. Rewrite ex3.py to use floating point numbers so it's more accurate > (hint: 20.0 is floating point). OK, so his point #4 is that for eggs, 1/4 gives a zero, not a 0.25. So you lose accuracy in that sense. So point #5 is to allow fractions to propagate. Note that in Python 3.x, 1/4 will already give a float, rather than an int, and if you want the 2.x behavior, you would write it as 1//4 Otherwise, it looks right to me. -- DaveA From hugo.yoshi at gmail.com Sat Dec 22 23:54:17 2012 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sat, 22 Dec 2012 23:54:17 +0100 Subject: [Tutor] regex: matching unicode In-Reply-To: <1356209636.65123.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <1356209636.65123.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On Sat, Dec 22, 2012 at 9:53 PM, Albert-Jan Roskam wrote: > Hi, > > Is the code below the only/shortest way to match unicode characters? I > would like to match whatever is defined as a character in the unicode > reference database. So letters in the broadest sense of the word, but not > digits, underscore or whitespace. Until just now, I was convinced that the > re.UNICODE flag generalized the [a-z] class to all unicode letters, and > that the absence of re.U was an implicit 're.ASCII'. Apparently that mental > model was *wrong*. > But [^\W\s\d_]+ is kind of hard to read/write. > > import re > s = unichr(956) # mu sign > m = re.match(ur"[^\W\s\d_]+", s, re.I | re.U) > > A thought would be to rely on the general category of the character, as listed in the Unicode database. Unicodedata.category will give you what you need. Here is a list of categories in the Unicode standard: http://www.fileformat.info/info/unicode/category/index.htm So, if you wanted only letters, you could say: def is_unicode_character(c): assert len(c) == 1 return 'L' in unicodedata.category(c) if only the Letter category will get you what you need, this is pretty simple, but if you also need symbols and marks or something it will start to get more complicated. Another thought is to match against two separate regexes, one being \w for alphanumeric and the other being [^\d] to leave you only with alpha. Not exactly ideal either. The last option is to just go with the regex, make sure you write it only once, and leave a nice comment. That's not too bad. Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From kbailey at howlermonkey.net Sun Dec 23 04:33:13 2012 From: kbailey at howlermonkey.net (Kirk Bailey) Date: Sat, 22 Dec 2012 22:33:13 -0500 Subject: [Tutor] playing mp3 files Message-ID: <50D67B79.3040400@howlermonkey.net> The best I have found still has some cereal in it- lots of flakes and nuts. Anyone got some ideas on playing mp3 files in a windows machine? -- -Shaboom. Kirk Bailey CEO, Freehold Marketing LLC http://www.OneBuckHosting.com/ Fnord! From slashharsh at hotmail.com Sun Dec 23 05:11:10 2012 From: slashharsh at hotmail.com (Harsh Bhatia) Date: Sun, 23 Dec 2012 09:41:10 +0530 Subject: [Tutor] Patch bugs in windows Message-ID: The document of python representing bug patching in windows is lot confusing ... can anyone help me with an example of previous solved bug ... from its issue to patch submission. thank you !! -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Sun Dec 23 05:12:47 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 23 Dec 2012 15:12:47 +1100 Subject: [Tutor] regex: matching unicode In-Reply-To: <1356209636.65123.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <1356209636.65123.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: <50D684BF.3040801@pearwood.info> On 23/12/12 07:53, Albert-Jan Roskam wrote: > Hi, > > Is the code below the only/shortest way to match unicode characters? No. You could install a more Unicode-aware regex engine, and use it instead of Python's re module, where Unicode support is at best only partial. Try this one: http://pypi.python.org/pypi/regex and report any issues to the author. > I would like to match whatever is defined as a character in the unicode >reference database. So letters in the broadest sense of the word, Well, not really, actually letters in the sense of the Unicode reference database :-) In the above regex module, I think you could write: '\p{Alphabetic}' or '\p{L}' but don't quote me on this. >but not digits, underscore or whitespace. Until just now, I was convinced >that the re.UNICODE flag generalized the [a-z] class to all unicode letters, >and that the absence of re.U was an implicit 're.ASCII'. Apparently that >mental model was *wrong*. > But [^\W\s\d_]+ is kind of hard to read/write. Of course it is. It's a regex. > import re > s = unichr(956) # mu sign > m = re.match(ur"[^\W\s\d_]+", s, re.I | re.U) Unfortunately that matches too much: in Python 2.7, it matches 340 non-letter characters. Run this to see them: import re import unicodedata MAXNUM = 0x10000 # one more than maximum unichr in Python "narrow builds" regex = re.compile("[^\W\s\d_]+", re.I | re.U) LETTERS = 'L|Ll|Lm|Lo|Lt|Lu'.split('|') failures = [] kinds = set() for c in map(unichr, range(MAXNUM)): if bool(re.match(regex, c)) != (unicodedata.category(c) in LETTERS): failures.append(c) kinds.add(unicodedata.category(c)) print kinds, len(failures) The failures are all numbers with category Nl or No ("letterlike numeric character" and "numeric character of other type"). You can see them with: for c in failures: print c, unicodedata.category(c), unicodedata.name(c) I won't show the full output, but a same sample includes: ? No SUPERSCRIPT TWO ? No VULGAR FRACTION ONE QUARTER ? No BENGALI CURRENCY NUMERATOR ONE ? No ETHIOPIC NUMBER EIGHTY ? Nl RUNIC ARLAUG SYMBOL ? Nl ROMAN NUMERAL THREE so you will probably have to post-process your matching results to exclude these false-positives. Or just accept them. If you have a "wide build", or Python 3.3, you can extend the test to the full Unicode range of 0x110000. When I do that, I find 684 false matches, all in category Nl and No. -- Steven From steve at pearwood.info Sun Dec 23 05:15:16 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 23 Dec 2012 15:15:16 +1100 Subject: [Tutor] Patch bugs in windows In-Reply-To: References: Message-ID: <50D68554.7020309@pearwood.info> On 23/12/12 15:11, Harsh Bhatia wrote: > > The document of python representing bug patching in windows is lot confusing ... > can anyone help me with an example of previous solved bug ... from its issue to patch submission. > thank you !! What document are you talking about? Why don't you have to look at the Python bug tracker, find a Windows bug, and read the issue's history? -- Steven From steve at pearwood.info Sun Dec 23 05:17:37 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 23 Dec 2012 15:17:37 +1100 Subject: [Tutor] playing mp3 files In-Reply-To: <50D3A347.4020603@howlermonkey.net> References: <50D3A347.4020603@howlermonkey.net> Message-ID: <50D685E1.3070805@pearwood.info> On 21/12/12 10:46, Kirk Bailey wrote: > the robotradio program is coming along. It plays. it dances. it sings. > IT selects random libraries and random broadcast files. and it restarts > itself with consummate ease. Is that a good thing or a bad thing? > Aka, NOW wtf?!? Jim is exchanging letters with me on this, but does not >yet have a solution. Who is Jim, and a solution to what? -- Steven From steve at pearwood.info Sun Dec 23 05:20:34 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 23 Dec 2012 15:20:34 +1100 Subject: [Tutor] playing mp3 files In-Reply-To: <50D67B79.3040400@howlermonkey.net> References: <50D67B79.3040400@howlermonkey.net> Message-ID: <50D68692.60909@pearwood.info> On 23/12/12 14:33, Kirk Bailey wrote: > The best I have found still has some cereal in it- lots of flakes >and nuts. Anyone got some ideas on playing mp3 files in a windows >machine? Since this is at least your third post on this subject, and I don't think anyone has replied yet, I guess the answer is no. This is a mailing list for learning Python the language, not for dealing with the vagaries of Windows APIs for playing mp3 files. You may have better luck asking on the main Python list: python-list at python.org also mirrored on Usenet: comp.lang.python Otherwise, I can only suggest you use something like Pygame or Wxpython, which probably support playing mp3 files. -- Steven From mariocatch at gmail.com Sun Dec 23 08:48:42 2012 From: mariocatch at gmail.com (Mario Cacciatore) Date: Sun, 23 Dec 2012 02:48:42 -0500 Subject: [Tutor] List Comprehension Syntax Message-ID: <50d6b74d.031d650a.3497.ffffad6a@mx.google.com> Hey everyone, I am having a very hard time understanding the list comprehension syntax. I've followed the docs and could use some guidance from the fine folks here to supplement my findings. If someone wouldn't mind replying back with an example or two, with some explanation of each part I'd appreciate it. -------------- next part -------------- An HTML attachment was scrubbed... URL: From msirenef at lightbird.net Sun Dec 23 09:14:21 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Sun, 23 Dec 2012 03:14:21 -0500 Subject: [Tutor] List Comprehension Syntax In-Reply-To: <50d6b74d.031d650a.3497.ffffad6a@mx.google.com> References: <50d6b74d.031d650a.3497.ffffad6a@mx.google.com> Message-ID: <50D6BD5D.3080207@lightbird.net> On 12/23/2012 02:48 AM, Mario Cacciatore wrote: > Hey everyone, > > I am having a very hard time understanding the list comprehension syntax. I've followed the docs and could use some guidance from the fine folks here to supplement my findings. If someone wouldn't mind replying back with an example or two, with some explanation of each part I'd appreciate it. Hi Mario, here are some examples (using python3 but very similar in py2.7): >>> L = range(20) >>> [x for x in L] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] >>> [x for x in L if x<=10] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] >>> [(x,x) for x in L] [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 12), (13, 13), (14, 14), (15, 15), (16, 16), (17, 17), (18, 18), (19, 19)] >>> [x*2 for x in L] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38] >>> [(x, x*3) for x in L if x<=10] [(0, 0), (1, 3), (2, 6), (3, 9), (4, 12), (5, 15), (6, 18), (7, 21), (8, 24), (9, 27), (10, 30)] >>> def strmul(x): return str(x), x*2 ... >>> [strmul(x) for x in L if x<=10] [('0', 0), ('1', 2), ('2', 4), ('3', 6), ('4', 8), ('5', 10), ('6', 12), ('7', 14), ('8', 16), ('9', 18), ('10', 20)] Hope this helps! -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From alan.gauld at btinternet.com Sun Dec 23 10:14:16 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 23 Dec 2012 09:14:16 +0000 Subject: [Tutor] List Comprehension Syntax In-Reply-To: <50d6b74d.031d650a.3497.ffffad6a@mx.google.com> References: <50d6b74d.031d650a.3497.ffffad6a@mx.google.com> Message-ID: On 23/12/12 07:48, Mario Cacciatore wrote: > I am having a very hard time understanding the list comprehension > syntax. I've followed the docs and could use some guidance from the fine > folks here to supplement my findings. If someone wouldn't mind replying > back with an example or two, with some explanation of each part I'd > appreciate it. You could try reading the List Comp sub-section of the Functional Programming topic in my tutorial. That has several examples with explanations and equivalent code etc. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Sun Dec 23 12:53:08 2012 From: __peter__ at web.de (Peter Otten) Date: Sun, 23 Dec 2012 12:53:08 +0100 Subject: [Tutor] List Comprehension Syntax References: <50d6b74d.031d650a.3497.ffffad6a@mx.google.com> Message-ID: Mario Cacciatore wrote: > Hey everyone, > > I am having a very hard time understanding the list comprehension syntax. > I've followed the docs and could use some guidance from the fine folks > here to supplement my findings. If someone wouldn't mind replying back > with an example or two, with some explanation of each part I'd appreciate > it. Consider [c.lower() + x % 3 * c for x in range(10) if x not in [7,8] for c in "aBcDE" if c.isupper() if c != "B"] That looks complex! So let's take it apart. Here's the cheat-sheet: result = [expr(x) for x in items] is equivalent to a for-loop: result = [] for x in items: result.append(expr(x)) The expression on the left of the list-comp moves into the (innermost if there is mor than one) loop. result = [expr(x) for x in items if cond(x)] is equivalent to result = [] for x in items: if cond(x): result.append(expr(x)) You can add an arbitrary number of conditions: result = [expr(x) for x in items if cond1(x) if cond2(x) if cond3(x)] is equivalent to result = [] for x in items: if cond1(x): if cond2(x): if cond3(x): result.append(expr(x)) You can also have multiple 'for' clauses: result = [expr(x, y, z) for x in items1 for y in items2 for z in items3] is equivalent to result = [] for x in items1: for y in items2: for z in items3: result.append(expr(x, y, z)) Now back to our initial example. Let's reformat it a bit result = [ c.lower() + x % 3 * c # that's expr(x, c) for x in range(10) if x not in [7,8] for c in "aBcDE" if c.isupper() if c != "B" ] That looks quite similar to result = [] for x in range(10) : if x not in [7,8]: for c in "aBcDE": if c.isupper(): if c != "B": result.append(c.lower() + x % 3 * c) Whenever you encounter a list comprehension you don't understand immediately you can easily translate it into for-loops and if-statements, either by reformatting in an editor or in your mind. Similarly you can convert for- loops appending to a list into a list comprehension. Can you spell result_loop = [] for x in range(10): for y in range(10): if (x + y) % 2: result_loop.append((x, y)) as a list-comp? Copy the above twice and apply the reverse reformatting trick to the second copy. result_listcomp = [...] # your code assert result_loop == result_listcomp, "Something went wrong, try again" print("success") If you run the script it should print 'success'. From steve at pearwood.info Sun Dec 23 14:27:33 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 24 Dec 2012 00:27:33 +1100 Subject: [Tutor] List Comprehension Syntax In-Reply-To: <50d6b74d.031d650a.3497.ffffad6a@mx.google.com> References: <50d6b74d.031d650a.3497.ffffad6a@mx.google.com> Message-ID: <50D706C5.201@pearwood.info> On 23/12/12 18:48, Mario Cacciatore wrote: > Hey everyone, > > I am having a very hard time understanding the list comprehension syntax. >I've followed the docs and could use some guidance from the fine folks >here to supplement my findings. If someone wouldn't mind replying back >with an example or two, with some explanation of each part I'd appreciate >it. If you did mathematics in high school, you may remember set-builder notation: http://en.wikipedia.org/wiki/Set-builder_notation There are a number of variations of this notation, depending on how formal you want to be. For example: {3x+1 ? x ? {1, 2, 3}} This says: "build the set of values 3 times x plus 1, for all x values that are elements of the set {1, 2, 3}" and it would produce the values: x=1 -> 3*1 + 1 x=2 -> 3*2 + 1 x=3 -> 3*3 + 1} giving the final set {4, 7, 10} Python uses similar notation, except based on English words instead of mathematical symbols. In Python, we generally use lists or tuples, not sets, so the list comprehension would be: [3*x + 1 for x in (1, 2, 3)] We can pull this apart to see what each part does. [ ] The square brackets build a list. 3*x + 1 This is the list comprehension expression. It is evaluated each time the list comp goes through the loop. for x in (1, 2, 3) This sets up the list comprehension loop, and defines the loop variable, just like a for-loop. The tuple (1, 2, 3) is the loop sequence, x takes each value from this in turn. So this list comprehension is equivalent to this for-loop: tmp = [] for x in (1, 2, 3): tmp.append(3*x + 1) except that you don't need to define a temporary list to accumulate the results, the list comprehension does that for you. List comprehensions can be more complicated. They can also take one or more "if" clause: [2*n for n in range(10) if n%2 == 1] is equivalent to this for-loop: tmp = [] for n in range(10): if n%2 == 1: tmp.append(2*n) and so it will produce the list: [2, 6, 10, 14, 18] Naturally, you can use any sequence or iterable in the for-loop part of list comps: myname = "Quentin" [c for c in myname if c.lower() != "q" if c.upper() != "T"] will give ['u', 'e', 'i', 'n'] The sequence can even be another list comprehension: [y+1 for y in [2*x for x in (1, 2, 3)] if y < 5] gives [3, 5], and is equivalent to this pair of loops: tmp1 = [] tmp2 = [] for x in (1, 2, 3): tmp1.append(2*x) for y in tmp1: if y < 5: tmp2.append(y+1) List comps can also take multiple loops: [(a, b) for a in range(3) for b in range(3)] gives this result: [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] and is equivalent to this nested loop: tmp = [] for a in range(3): for b in range(3): tmp.append( (a, b) ) List comprehensions are powerful and compact, but because they are so compact, they can also be hard to read. Resist the temptation to write every for-loop as a list comprehension! Keep your list comps simple, and you will not regret it. Nobody has ever said, "I wish my list comprehensions were more complicated and hard to read!" -- Steven From malcolm.newsome at gmail.com Sun Dec 23 21:18:38 2012 From: malcolm.newsome at gmail.com (Malcolm Newsome) Date: Sun, 23 Dec 2012 14:18:38 -0600 Subject: [Tutor] List Comprehension Syntax In-Reply-To: References: Message-ID: > Message: 4 > Date: Sun, 23 Dec 2012 02:48:42 -0500 > From: Mario Cacciatore > To: "tutor at python.org" > Subject: [Tutor] List Comprehension Syntax > Message-ID: <50d6b74d.031d650a.3497.ffffad6a at mx.google.com> > Content-Type: text/plain; charset="windows-1252" > > Hey everyone, > > I am having a very hard time understanding the list comprehension syntax. I've followed the docs and could use some guidance from the fine folks here to supplement my findings. If someone wouldn't mind replying back with an example or two, with some explanation of each part I'd appreciate it. > -------------- next part -------------- Here's an example that I used a list comprehension for when I was trying to learn it. I'd leave it to the tutors to say whether this is an appropriate use of it. # search sorted array for integer k def finder(list, k): try: s = sorted([int(i) for i in list]) k = int(k) except ValueError: print "Args must be ints. Your list arg was: {0} and your k arg was '{1}'." .format(list, k) return None if k in s: print "Index is: {0}".format(s.index(k)) else: return -1 Hope it helps! Malcolm Newsome P.S. I hope the formatting is ok...I'm responding from my phone. -------------- next part -------------- An HTML attachment was scrubbed... URL: From farrukhali2010 at live.com Sun Dec 23 21:32:40 2012 From: farrukhali2010 at live.com (Farrukh Ali) Date: Mon, 24 Dec 2012 01:32:40 +0500 Subject: [Tutor] Tutor Digest, Vol 106, Issue 60 In-Reply-To: References: Message-ID: Thank you so so much Sir. Now i got u very well and have command on this concepty... Regards ------------------------------ Message: 2 Date: Sat, 22 Dec 2012 17:52:06 -0500 From: Dave Angel To: tutor at python.org Subject: Re: [Tutor] check it for me Message-ID: <50D63996.9080407 at davea.name> Content-Type: text/plain; charset=ISO-8859-1 On 12/22/2012 05:38 PM, Farrukh Ali wrote: >> Hi, i am using ActivePython 2.7.2.5, and windows 8 professional. >> well the original ex3.py is: >> print "I will now count my chickens:" >> print "Hens", 25 + 30 / 6 >> print "Roosters", 100 - 25 * 3 % 4 >> print "Now I will count the eggs:" >> print 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 >> print "Is it true that 3 + 2 < 5 - 7?" >> print 3 + 2 < 5 - 7 >> print "What is 3 + 2?", 3 + 2 >> print "What is 5 - 7?", 5 - 7 >> print "Oh, that's why it's False." >> print "How about some more." >> print "Is it greater?", 5 > -2 >> print "Is it greater or equal?", 5 >= -2 >> print "Is it less or equal?", 5 <= -2 >> >> And i have made ex3.py this way below >> >> print "I will now count my chickens:" >> print "Hens",25+30.0/6 >> print "Roosters",100 - 25.0 * 3.0 % 4 >> print "Now I will count the eggs:" >> print 3+2+1-5.0+4.0%2-1/4.0+6 >> print "Is it true that 3+2<5-7?" >> print 3+2<5-7 >> print "What is 3+2?",3+2 >> print "What is 5-7?",5-7 >> print "Oh, that's why it's False." >> print "How about some more." >> print "Is it greater?", 5>-2 >> print "Is it greater or equal?", 5>=-2 >> print "Is it less or equal?", 5<=-2 >> >> >> The author has given us extra credit to practice so here it is and the >> 5th point below, which i was asking. >> >> Extra Credit >> 1. Above each line, use the # to write a comment to yourself >> explaining what the line does. >> 2. Remember in Exercise 0 when you started python? Start python this >> way again and using the above characters >> and what you know, use python as a calculator. >> 3. Find something you need to calculate and write a new .py file that >> does it. >> 4. Notice the math seems "wrong"? There are no fractions, only whole >> numbers. Find out why by researching >> what a "floating point" number is. >> 5. Rewrite ex3.py to use floating point numbers so it's more accurate >> (hint: 20.0 is floating point). > OK, so his point #4 is that for eggs, 1/4 gives a zero, not a 0.25. So > you lose accuracy in that sense. So point #5 is to allow fractions to > propagate. > > Note that in Python 3.x, 1/4 will already give a float, rather than an > int, and if you want the 2.x behavior, you would write it as 1//4 > > Otherwise, it looks right to me. > From tktucker at gmail.com Mon Dec 24 02:03:44 2012 From: tktucker at gmail.com (Tom Tucker) Date: Sun, 23 Dec 2012 20:03:44 -0500 Subject: [Tutor] For Loop Question Message-ID: Python Gurus, I got a question for ya. Below I have three instance variables (self.A, self.B, etc). How can I use the below for loop for A, B, C to also call those instance variables? Example ############### ..... ..... self.A = 1 self.B = 2 self.C = 3 myDict = {'A': 1, 'B': 2, 'C': 1} for x in [A, B, C]: if myDict[x] == (self.%s % (x)): # Invalid if statement.. ;-) print "Yep, we have a match!" else: print "Sorry..No match!" -------------- next part -------------- An HTML attachment was scrubbed... URL: From msirenef at lightbird.net Mon Dec 24 02:16:33 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Sun, 23 Dec 2012 20:16:33 -0500 Subject: [Tutor] For Loop Question In-Reply-To: References: Message-ID: <50D7ACF1.4070909@lightbird.net> On 12/23/2012 08:03 PM, Tom Tucker wrote: > > > Python Gurus, > I got a question for ya. Below I have three instance variables > (self.A, self.B, etc). How can I use the below > for loop for A, B, C to also call those instance variables? > > Example > ############### > > ..... > ..... > self.A = 1 > self.B = 2 > self.C = 3 > > myDict = {'A': 1, 'B': 2, 'C': 1} > for x in [A, B, C]: > > if myDict[x] == (self.%s % (x)): # Invalid if statement.. ;-) > print "Yep, we have a match!" > else: > print "Sorry..No match!" > > If I understood you right, you can do: myDict[x] == getattr(self, x) - mitya -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From alan.gauld at btinternet.com Mon Dec 24 03:02:36 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 24 Dec 2012 02:02:36 +0000 Subject: [Tutor] For Loop Question In-Reply-To: <50D7ACF1.4070909@lightbird.net> References: <50D7ACF1.4070909@lightbird.net> Message-ID: On 24/12/12 01:16, Mitya Sirenef wrote: > On 12/23/2012 08:03 PM, Tom Tucker wrote: >> >> >> Python Gurus, >> I got a question for ya. Below I have three instance variables >> (self.A, self.B, etc). How can I use the below >> for loop for A, B, C to also call those instance variables? >> Your example is not entirely clear. You do realize that self is only defined inside a method? Therefore, by implication, all of your code should lie inside a class and most of it inside a single method? Therefore, I'd expect your code to look like: >> >> ..... >> ..... class MyClass: def someMethod(self): >> self.A = 1 >> self.B = 2 >> self.C = 3 >> def someOtherMethod(self): >> myDict = {'A': 1, 'B': 2, 'C': 1} >> for x in [A, B, C]: in which case this becomes, as Mitya suggests: for key in myDict: if myDict[key] == getattr(self, key): > print "Yep, we have a match!" > else: > print "Sorry..No match!" But, whatever you are trying to do here, there is very likely a better way to do it. We just don't know enough context to offer alternatives. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Mon Dec 24 05:16:29 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 23 Dec 2012 23:16:29 -0500 Subject: [Tutor] regex: matching unicode In-Reply-To: <50D684BF.3040801@pearwood.info> References: <1356209636.65123.YahooMailNeo@web163806.mail.gq1.yahoo.com> <50D684BF.3040801@pearwood.info> Message-ID: On Sat, Dec 22, 2012 at 11:12 PM, Steven D'Aprano wrote: > > No. You could install a more Unicode-aware regex engine, and use it instead > of Python's re module, where Unicode support is at best only partial. > > Try this one: > > http://pypi.python.org/pypi/regex Looking over the old docs, I count 4 regex implementations up to 2.0: regexp regex (0.9.5) re / pcre (1.5) re / sre (2.0) It would be nice to see Matthew Barnett's regex module added as an option in 3.4, just as sre was added to 1.6 before taking the place of pcre in 2.0. > The failures are all numbers with category Nl or No ("letterlike > numeric character" and "numeric character of other type"). The pattern basically matches any word character that's not a decimal/underscore (the \s is redundant AFAIK). Any character that's numeric but not decimal also matches. For example, the following are all numeric: \N{SUPERSCRIPT ONE}: category "No", digit, not decimal \N{ROMAN NUMERAL ONE}: category "Nl", not digit, not decimal \u4e00 (1, CJK): category "Lo", not digit, not decimal Regarding the latter, if the pattern shouldn't match numeric characters in a broad sense, then it should be OK to exclude CJK numeric ideograms in category "Lo", but it's like excluding the word "one". From d at davea.name Mon Dec 24 05:17:49 2012 From: d at davea.name (Dave Angel) Date: Sun, 23 Dec 2012 23:17:49 -0500 Subject: [Tutor] For Loop Question In-Reply-To: References: <50D7ACF1.4070909@lightbird.net> Message-ID: <50D7D76D.80604@davea.name> On 12/23/2012 09:02 PM, Alan Gauld wrote: > On 24/12/12 01:16, Mitya Sirenef wrote: >> On 12/23/2012 08:03 PM, Tom Tucker wrote: >>> >>> >>> Python Gurus, >>> I got a question for ya. Below I have three instance variables >>> (self.A, self.B, etc). How can I use the below >>> for loop for A, B, C to also call those instance variables? >>> > > Your example is not entirely clear. > > You do realize that self is only defined inside a method? Therefore, > by implication, all of your code should lie inside a class and most > of it inside a single method? Therefore, I'd expect your code > to look like: > > >>> >>> ..... >>> ..... > class MyClass: > def someMethod(self): >>> self.A = 1 >>> self.B = 2 >>> self.C = 3 >>> > def someOtherMethod(self): >>> myDict = {'A': 1, 'B': 2, 'C': 1} >>> for x in [A, B, C]: > But A, B, and C have no values yet. Perhaps the OP means for x in ["A", "B", "C"] > in which case this becomes, as Mitya suggests: > > for key in myDict: > if myDict[key] == getattr(self, key): > > > print "Yep, we have a match!" > > else: > > print "Sorry..No match!" > > But, whatever you are trying to do here, there is very likely a better > way to do it. We just don't know enough context to offer alternatives. > > > HTH -- DaveA From eryksun at gmail.com Mon Dec 24 05:42:39 2012 From: eryksun at gmail.com (eryksun) Date: Sun, 23 Dec 2012 23:42:39 -0500 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: <50D515B4.9050706@pearwood.info> Message-ID: On Sat, Dec 22, 2012 at 12:57 PM, Oscar Benjamin wrote: >>> >>> def make_int(obj): >>> '''Coerce str, float and int to int without rounding error >>> Accepts strings like '4.0' but not '4.1' >>> ''' >>> fnum = float('%s' % obj) >>> inum = int(fnum) >>> assert inum == fnum >>> return inum >> >> Well, that function is dangerously wrong. In no particular order, >> I can find four bugs and one design flaw. > > I expected someone to object to this function. I had hoped that they > might also offer an improved version, though. I can't see a good way > to do this without special casing the treatment of some or other type > (the obvious one being str). Strings don't implement __int__ or __trunc__; they aren't numbers, so why not special case them? You can parse strings with obj = Decimal(obj) (this uses a regex). Then for all inputs set inum = int(obj) and raise ValueError if inum != obj. From fomcl at yahoo.com Mon Dec 24 08:51:58 2012 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sun, 23 Dec 2012 23:51:58 -0800 (PST) Subject: [Tutor] regex: matching unicode In-Reply-To: References: <1356209636.65123.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: <1356335518.93117.YahooMailNeo@web163801.mail.gq1.yahoo.com> >>Is the code below the only/shortest way to match unicode characters? I would like to match whatever is defined as a character in the unicode reference database. So letters in the broadest sense of the word, but not digits, underscore or whitespace. Until just now, I was convinced that the re.UNICODE flag generalized the [a-z] class to all unicode letters, and that the absence of re.U was an implicit 're.ASCII'. Apparently that mental model was *wrong*. >>But [^\W\s\d_]+ is kind of hard to read/write. >> >>import re >>s = unichr(956)? # mu sign >>m = re.match(ur"[^\W\s\d_]+", s, re.I | re.U) >> >> >A thought would be to rely on the general category of the character, as listed in the Unicode database. Unicodedata.category will give you what you need. Here is a list of categories in the Unicode standard: > > >http://www.fileformat.info/info/unicode/category/index.htm > > > >So, if you wanted only letters, you could say: > > >def is_unicode_character(c): >? ? assert len(c) == 1 >? ? return 'L' in unicodedata.category(c) Hi everybody, Thanks for your replies, they have been most insightful. For now the 'unicodedata' approach works best for me. I need to validate a word and this is now a two-step approach. First, check if the first character is a (unicode) letter, second, do other checks with a regular regex (ie., no spaces, ampersands and whatnot). Using one regex would be more elegant though, but I got kinda intimidated by the hail of additional flags in the regex module. Having unicode versions of the classes \d, \w, etc (let's call them \ud, \uw) would be cool.Here another useful way to use your (Hugo's) function. The Japanese hangul sign and the degree sign almost look the same! import unicodedata hangul = unichr(4363) degree = unichr(176) def isUnicodeChar(c): ? assert len(c) == 1 ? c = c.decode("utf-8") if isinstance(c, str) else c ? return 'L' in unicodedata.category(c) >>> isUnicodeChar(hangul) True >>> isUnicodeChar(degree) False From bgailer at gmail.com Mon Dec 24 17:31:09 2012 From: bgailer at gmail.com (bob gailer) Date: Mon, 24 Dec 2012 11:31:09 -0500 Subject: [Tutor] For Loop Question In-Reply-To: References: <50D7ACF1.4070909@lightbird.net> Message-ID: <50D8834D.6070004@gmail.com> On 12/23/2012 9:02 PM, Alan Gauld wrote: > > You do realize that self is only defined inside a method? Perhaps. self is just another name - it could be "just" a variable, outside any method. -- Bob Gailer 919-636-4239 Chapel Hill NC From alan.gauld at btinternet.com Tue Dec 25 00:49:47 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 24 Dec 2012 23:49:47 +0000 Subject: [Tutor] For Loop Question In-Reply-To: <50D8834D.6070004@gmail.com> References: <50D7ACF1.4070909@lightbird.net> <50D8834D.6070004@gmail.com> Message-ID: On 24/12/12 16:31, bob gailer wrote: > On 12/23/2012 9:02 PM, Alan Gauld wrote: >> >> You do realize that self is only defined inside a method? > Perhaps. > > self is just another name - it could be "just" a variable, outside any > method. True enough, but given the usage "self.A" it looks pretty certain in this case it follows convention and signifies an instance. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wangyonguci2012 at gmail.com Tue Dec 25 02:39:46 2012 From: wangyonguci2012 at gmail.com (wang yong) Date: Mon, 24 Dec 2012 17:39:46 -0800 Subject: [Tutor] Use python to read data from mouse but not move cursor Message-ID: Hi Tutors, I am new to Python. I am trying to use win32api to read out mouse movement from an additional mouse attached to my compputer. But, I do not want this mouse to move my cursor. So, do you guys have any suggestions or codes for this task! Really appreciate it! Thanks for the help. Yong -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Tue Dec 25 04:25:14 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 24 Dec 2012 22:25:14 -0500 Subject: [Tutor] Use python to read data from mouse but not move cursor In-Reply-To: References: Message-ID: On Mon, Dec 24, 2012 at 8:39 PM, wang yong wrote: > > I am new to Python. I am trying to use win32api to read out mouse movement > from an additional mouse attached to my compputer. But, I do not want this > mouse to move my cursor. You could try using PyUSB with libusb-win32. The latter has a GUI "filter wizard" that I think *might* let you replace the default mouse driver with a libusb-win32 device driver. This *might* let you use the mouse exclusively with PyUSB. I'm sorry to have to hedge my statements here, but I haven't tried this. At least it *might* point you in the right direction. http://sourceforge.net/apps/trac/pyusb/ http://sourceforge.net/apps/trac/libusb-win32/ From eryksun at gmail.com Tue Dec 25 05:22:31 2012 From: eryksun at gmail.com (eryksun) Date: Mon, 24 Dec 2012 23:22:31 -0500 Subject: [Tutor] regex: matching unicode In-Reply-To: <1356335518.93117.YahooMailNeo@web163801.mail.gq1.yahoo.com> References: <1356209636.65123.YahooMailNeo@web163806.mail.gq1.yahoo.com> <1356335518.93117.YahooMailNeo@web163801.mail.gq1.yahoo.com> Message-ID: On Mon, Dec 24, 2012 at 2:51 AM, Albert-Jan Roskam wrote: > > First, check if the first character is a (unicode) letter You can use unicode.isalpha, with a caveat. On a narrow build isalpha fails for supplementary planes. That's about 50% of all alphabetic characters, +/- depending on the version of Unicode. But it's mostly the less common CJK characters (over 90% this), dead languages (e.g. Linear B, Cuneiform, Egyptian Hieroglyphs), and mathematical script. Instead, you could check if index 0 is category 'Cs' (surrogate). If so, check the category of the slice [:2]. > Having unicode versions of the classes \d, \w, etc (let's call them > \ud, \uw) would be cool. (?u) enables re.U in case you're looking to keep the flag setting in the pattern itself. \d and \w are defined for Unicode. It's just the available categories are insufficient. Matthew Barnett's regex module implements level 1 (and much of level 2) of UTS #18: Unicode Regular Expressions. See RL1.2 and Annex C: http://unicode.org/reports/tr18/ > def isUnicodeChar(c): > assert len(c) == 1 > c = c.decode("utf-8") if isinstance(c, str) else c > return 'L' in unicodedata.category(c) For UTF-8, len(c) == 1 only for ASCII codes; otherwise character codes have a leading byte and up to 3 continuation bytes. Also, on a narrow build len(c) is 2 for codes in the supplementary planes. Also keep in mind canonical composition/decomposition equivalence and compatibility when thinking about 'characters' in terms of comparison, sorting, dictionary keys, sets, etc. You might want to first normalize a string to canonical composed form (NFC). Python 3.x uses NFKC for identifiers. For example: >>> d = {} >>> exec("e\u0301 = 1", d) >>> d["e\u0301"] Traceback (most recent call last): File "", line 1, in KeyError: 'e?' >>> d["\xe9"] 1 >>> "\xe9" '?' http://en.wikipedia.org/wiki/Unicode_equivalence From alan.gauld at btinternet.com Wed Dec 26 00:14:16 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 25 Dec 2012 23:14:16 +0000 Subject: [Tutor] Use python to read data from mouse but not move cursor In-Reply-To: References: Message-ID: On 25/12/12 01:39, wang yong wrote: > I am new to Python. I am trying to use win32api to read out mouse > movement from an additional mouse attached to my compputer. But, I do > not want this mouse to move my cursor. So, do you guys have any > suggestions or codes for this task! Really appreciate it! Thanks for the > help. This group is really about learning the python language and standard library and that's a pretty deeply Windows specific question. I suspect you'll have more success on the win32 mailing list. This is really about how to handle Windows mouse events rather than Python per se. I suspect the answer will be the same regardless of the language you use. You can try the Python Windows mailing list or the newsgroup version on gmane: news://news.gmane.org:119/gmane.comp.python.windows Or even some of the more generic Windows programming lists/groups/fora HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From ufukeskici at gmail.com Thu Dec 27 10:36:07 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Thu, 27 Dec 2012 11:36:07 +0200 Subject: [Tutor] how to control putty window In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> Message-ID: Hello, I've installed Paramiko on my PC with Python 2.7. My code is: import paramiko import os ssh = paramiko.SSHClient() ssh.connect('10.10.10.10', username='ufuk', password='ufuk') But I'm getting this error: >>> Traceback (most recent call last): File "C:/Users/eufuesk/Desktop/paramiko.py", line 1, in import paramiko File "C:/Users/eufuesk/Desktop\paramiko.py", line 4, in ssh = paramiko.SSHClient() AttributeError: 'module' object has no attribute 'SSHClient' >>> Anybody familiar with paramiko? 2012/12/20 Prasad, Ramit > Ufuk Eskici wrote: > > Hello, > > > > I run this command and opens putty: > > > > import os > > import subprocess > > command = '"c:\Program Files\Putty\putty.exe" -ssh > ufukeskici at 10.10.10.10 -pw test > > subprocess.Popen(command) > > > > But then I want to input new commands to this Putty new window. How can > I do it? > > > > Do you need to control Putty or just SSH to another computer? > If all you need to SSH then I would recommend using a 3rd > party module such as Fabric (which relies on Paramiko). Those modules > will simply SSH significantly. They are Python 2.x but you should > be able to use Paramiko in 3.x except for SFTP. This link might help > to install Paramiko if you are using Python 3. > https://github.com/paramiko/paramiko/issues/16 > > If you need to control Putty specifically then I cannot help. > > > Ramit > > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of > securities, accuracy and completeness of information, viruses, > confidentiality, legal privilege, and legal entity disclaimers, > available at http://www.jpmorgan.com/pages/disclosures/email. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 27 10:47:35 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 27 Dec 2012 09:47:35 +0000 Subject: [Tutor] how to control putty window In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> Message-ID: On 27/12/12 09:36, Ufuk Eskici wrote: > I've installed Paramiko on my PC with Python 2.7. > > My code is: > import paramiko > import os > ssh = paramiko.SSHClient() > ssh.connect('10.10.10.10', username='ufuk', password='ufuk') > > Traceback (most recent call last): > File "C:/Users/eufuesk/Desktop/paramiko.py", line 1, in > import paramiko > File "C:/Users/eufuesk/Desktop\paramiko.py", line 4, in > ssh = paramiko.SSHClient() > AttributeError: 'module' object has no attribute 'SSHClient' > >>> Did paramiko install properly? Can you access anything in the module? What happens if you do >>> import paramiko >>> dir(paramiko) or >>> help(paramiko) If those didn't work it suggests the install didn't work properly. If they do work then I don't know what's wrong. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From ufukeskici at gmail.com Thu Dec 27 10:53:29 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Thu, 27 Dec 2012 11:53:29 +0200 Subject: [Tutor] how to control putty window In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> Message-ID: It seems it is looking for Paramiko under wrong folder. >>> import paramiko Traceback (most recent call last): File "", line 1, in import paramiko File "C:/Users/eufuesk/Desktop\paramiko.py", line 3, in ssh = paramiko.SSHClient() AttributeError: 'module' object has no attribute 'SSHClient' >>> os.chdir("c:\\Python27") Which directory should I use? 2012/12/27 Alan Gauld > On 27/12/12 09:36, Ufuk Eskici wrote: > > I've installed Paramiko on my PC with Python 2.7. >> >> My code is: >> import paramiko >> import os >> ssh = paramiko.SSHClient() >> ssh.connect('10.10.10.10', username='ufuk', password='ufuk') >> >> Traceback (most recent call last): >> File "C:/Users/eufuesk/Desktop/**paramiko.py", line 1, in >> import paramiko >> File "C:/Users/eufuesk/Desktop\**paramiko.py", line 4, in >> ssh = paramiko.SSHClient() >> AttributeError: 'module' object has no attribute 'SSHClient' >> >>> >> > > Did paramiko install properly? > Can you access anything in the module? > > What happens if you do > > >>> import paramiko > >>> dir(paramiko) > > or > > >>> help(paramiko) > > If those didn't work it suggests the install didn't work properly. > If they do work then I don't know what's wrong. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Thu Dec 27 11:25:51 2012 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Thu, 27 Dec 2012 11:25:51 +0100 Subject: [Tutor] how to control putty window In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> Message-ID: On Thu, Dec 27, 2012 at 10:53 AM, Ufuk Eskici wrote: > It seems it is looking for Paramiko under wrong folder. > > >>> import paramiko > > Traceback (most recent call last): > File "", line 1, in > import paramiko > File "C:/Users/eufuesk/Desktop\paramiko.py", line 3, in > ssh = paramiko.SSHClient() > AttributeError: 'module' object has no attribute 'SSHClient' > >>> os.chdir("c:\\Python27") > > > Which directory should I use? > > The problem is that you have a file named paramiko.py in the current directory. When python executes an import, it looks first in the list of built-in modules, and then in a set of directories listed in sys.path. The first directory in sys.path is always the current directory of the script. So when you do "import paramiko" python looks in the current directory, finds a file called paramiko.py, and imports that happily. The lesson is that you should never give your python scripts the same as a module you're using, because then python will be confused about which file to import, and you'll be confused why your imported module has none of the names it should have. Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Dec 27 11:32:46 2012 From: d at davea.name (Dave Angel) Date: Thu, 27 Dec 2012 05:32:46 -0500 Subject: [Tutor] how to control putty window In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> Message-ID: <50DC23CE.6030502@davea.name> On 12/27/2012 04:53 AM, Ufuk Eskici wrote: > It seems it is looking for Paramiko under wrong folder. > >>>> import paramiko > Traceback (most recent call last): > File "", line 1, in > import paramiko > File "C:/Users/eufuesk/Desktop\paramiko.py", line 3, in > ssh = paramiko.SSHClient() > AttributeError: 'module' object has no attribute 'SSHClient' >>>> os.chdir("c:\\Python27") > > Which directory should I use? > Looks to me like you're testing it with a script file called paramiko.py So when you import paramiko, it's finding your script instead of the installed paramiko. Even in the best of times, importing a module with the same name as your script is problematic. But in this case, you're completely masking the actual module. So rename your script and move it somewhere other than the desktop. Start a cmd box (DOS window), and run c:> cd sourcedir c:> python mytest.py Another comment: when starting a new thread, please use a fresh email to tutor at python.org. Don't use reply to an existing message, or your query can get lost in the noise. And of course pick a subject line that matches your query, like "Trouble importing paramiko" -- DaveA From ufukeskici at gmail.com Thu Dec 27 12:44:32 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Thu, 27 Dec 2012 13:44:32 +0200 Subject: [Tutor] Trouble importing Paramiko Message-ID: My code is: import paramiko ssh = paramiko.SSHClient() ssh.connect('710.10.10.10', username='ufuk', password='ufuk') and saved this file as "ufo.py" on the desktop. I'm still getting error: >>> ================================ RESTART ================================ >>> Traceback (most recent call last): File "C:\Users\eufuesk\Desktop\ufo.py", line 1, in import paramiko ImportError: No module named paramiko >>> -------------- next part -------------- An HTML attachment was scrubbed... URL: From randywhitewolf at ymail.com Thu Dec 27 13:07:05 2012 From: randywhitewolf at ymail.com (Randy WhiteWolf) Date: Thu, 27 Dec 2012 04:07:05 -0800 (PST) Subject: [Tutor] help Message-ID: <1356610025.95729.YahooMailNeo@web160705.mail.bf1.yahoo.com> I am an older newbie teaching myself Python programming. I copied the code # Emonstrates escape sequences. This exercise is on page 22 of the Phthon Programming for the Absolute Beginner by Michael Dawson.?I have copied the code verbatim below. ? ? ? # Sound the system bell print "\a" ? print "\t\t\tFancy Credits" ? print "\t\t\t \\ \\ \\ \\ \\ \\ \\" print "\t\t\t\tby print "\t\t\tMichael Dawson print "\t\t\t \\ \\ \\ \\ \\ \\ \\" print "\nSpecial thanks goes out to:" print "My hair stylist. Henry \'The Great\', who never says \"can\'t\"." ? raw_input ("\n\nPress the enter key to exit.") ? ? My problem is I hear no system bell; the enter doesn't respond by quitting the program; The problem with the program code the enter key hasn't worked in earlier programs. ? I appreciate any advice I may recieve with this coding glitch. ? Sincerely, Randy -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Dec 27 13:30:26 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 27 Dec 2012 23:30:26 +1100 Subject: [Tutor] Trouble importing Paramiko In-Reply-To: References: Message-ID: <50DC3F62.7090109@pearwood.info> On 27/12/12 22:44, Ufuk Eskici wrote: > Traceback (most recent call last): > File "C:\Users\eufuesk\Desktop\ufo.py", line 1, in > import paramiko > ImportError: No module named paramiko Have you installed paramiko? It is not a standard Python module, you have to install it first. -- Steven From ufukeskici at gmail.com Thu Dec 27 13:33:20 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Thu, 27 Dec 2012 14:33:20 +0200 Subject: [Tutor] Trouble importing Paramiko In-Reply-To: <50DC3F62.7090109@pearwood.info> References: <50DC3F62.7090109@pearwood.info> Message-ID: Yes, I've installed it. how can I check it? 2012/12/27 Steven D'Aprano > On 27/12/12 22:44, Ufuk Eskici wrote: > > Traceback (most recent call last): >> File "C:\Users\eufuesk\Desktop\ufo.**py", line 1, in >> import paramiko >> ImportError: No module named paramiko >> > > > Have you installed paramiko? It is not a standard Python module, > you have to install it first. > > > -- > Steven > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Dec 27 13:45:18 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 27 Dec 2012 23:45:18 +1100 Subject: [Tutor] help In-Reply-To: <1356610025.95729.YahooMailNeo@web160705.mail.bf1.yahoo.com> References: <1356610025.95729.YahooMailNeo@web160705.mail.bf1.yahoo.com> Message-ID: <50DC42DE.7050405@pearwood.info> On 27/12/12 23:07, Randy WhiteWolf wrote: > # Sound the system bell > print "\a" That comment is misleading. \a does not necessarily sound the system bell. Whether it does or not depends on the terminal you are using. For example, under Linux I am using the "Konsole" terminal, and I have four settings for the terminal bell: * Off * Flash the screen * System notification * Make a sound except that the "make a sound" setting doesn't appear to work. Other terminals may offer other choices, or no choice at all. So chances are high that printing "\a" will not ring the system bell. [...] > raw_input ("\n\nPress the enter key to exit.") > > > My problem is I hear no system bell; the enter doesn't respond by >quitting the program; The problem with the program code the enter >key hasn't worked in earlier programs. The enter key thing will depend on how you are running the code. If you are running the code inside an interactive environment, such as IDLE or the default Python interactive interpreter, then the above line of code does not exit. Just because it says "Press enter to exit" doesn't make it exit! If it said "Press enter to become King of England", you wouldn't expect that to happen would you? *wink* However, when you run a script non-interactively, the script will exit when it gets to end of the file. So if you add this line just before the end: raw_input("blah blah blah any message at all") when Python reaches this line, it will pause until you hit the Enter key, then it will reach the end of the script, then it will exit. -- Steven From steve at pearwood.info Thu Dec 27 13:48:09 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 27 Dec 2012 23:48:09 +1100 Subject: [Tutor] Trouble importing Paramiko In-Reply-To: References: <50DC3F62.7090109@pearwood.info> Message-ID: <50DC4389.5000604@pearwood.info> On 27/12/12 23:33, Ufuk Eskici wrote: > 2012/12/27 Steven D'Aprano > >> On 27/12/12 22:44, Ufuk Eskici wrote: >> >> Traceback (most recent call last): >>> File "C:\Users\eufuesk\Desktop\ufo.**py", line 1, in >>> import paramiko >>> ImportError: No module named paramiko >> >> Have you installed paramiko? It is not a standard Python module, >> you have to install it first. > > > Yes, I've installed it. how can I check it? You can check it by trying to import it. Since that has failed, there is a problem with the installation. How did you install it? What happens if you run the Windows "Find File" command and search for paramiko? What does it find? -- Steven From ufukeskici at gmail.com Thu Dec 27 14:16:46 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Thu, 27 Dec 2012 15:16:46 +0200 Subject: [Tutor] Trouble importing Paramiko In-Reply-To: <50DC4389.5000604@pearwood.info> References: <50DC3F62.7090109@pearwood.info> <50DC4389.5000604@pearwood.info> Message-ID: This is the output, it fails. Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import paramiko Traceback (most recent call last): File "", line 1, in import paramiko ImportError: No module named paramiko >>> screenshot of search result: http://i49.tinypic.com/21kmtfp.jpg I inslatted accoording to this link: http://vijaymurthy.blogspot.com/2011/03/installing-paramiko-for-windows.html 2012/12/27 Steven D'Aprano > On 27/12/12 23:33, Ufuk Eskici wrote: > >> 2012/12/27 Steven D'Aprano >> >> On 27/12/12 22:44, Ufuk Eskici wrote: >>> >>> Traceback (most recent call last): >>> >>>> File "C:\Users\eufuesk\Desktop\ufo.****py", line 1, in >>>> >>>> import paramiko >>>> ImportError: No module named paramiko >>>> >>> >>> Have you installed paramiko? It is not a standard Python module, >>> you have to install it first. >>> >> >> >> Yes, I've installed it. how can I check it? >> > > You can check it by trying to import it. Since that has failed, there > is a problem with the installation. > > How did you install it? > > What happens if you run the Windows "Find File" command and search > for paramiko? What does it find? > > > > > -- > Steven > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Thu Dec 27 14:38:24 2012 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Thu, 27 Dec 2012 14:38:24 +0100 Subject: [Tutor] Trouble importing Paramiko In-Reply-To: References: <50DC3F62.7090109@pearwood.info> <50DC4389.5000604@pearwood.info> Message-ID: On Thu, Dec 27, 2012 at 2:16 PM, Ufuk Eskici wrote: > This is the output, it fails. > > Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] > on win32 > Type "copyright", "credits" or "license()" for more information. > >>> import paramiko > > Traceback (most recent call last): > File "", line 1, in > import paramiko > ImportError: No module named paramiko > >>> > > screenshot of search result: http://i49.tinypic.com/21kmtfp.jpg > > I inslatted accoording to this link: > http://vijaymurthy.blogspot.com/2011/03/installing-paramiko-for-windows.html > > Looking at that link, the guide doesn't actually install paramiko, it only builds it. try issuing this command in the same folder where you issued the command of step 4: python setup.py install please report results. Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From ufukeskici at gmail.com Thu Dec 27 14:48:25 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Thu, 27 Dec 2012 15:48:25 +0200 Subject: [Tutor] Trouble importing Paramiko In-Reply-To: References: <50DC3F62.7090109@pearwood.info> <50DC4389.5000604@pearwood.info> Message-ID: I got this output with lots of errors: copying build\lib\paramiko\sftp_file.py -> build\bdist.win32\egg\paramiko copying build\lib\paramiko\sftp_handle.py -> build\bdist.win32\egg\paramiko copying build\lib\paramiko\sftp_server.py -> build\bdist.win32\egg\paramiko copying build\lib\paramiko\sftp_si.py -> build\bdist.win32\egg\paramiko copying build\lib\paramiko\ssh_exception.py -> build\bdist.win32\egg\paramiko copying build\lib\paramiko\transport.py -> build\bdist.win32\egg\paramiko copying build\lib\paramiko\util.py -> build\bdist.win32\egg\paramiko copying build\lib\paramiko\win_pageant.py -> build\bdist.win32\egg\paramiko copying build\lib\paramiko\__init__.py -> build\bdist.win32\egg\paramiko byte-compiling build\bdist.win32\egg\paramiko\agent.py to agent.cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\auth_handler.py to auth_handler.cp ython-33.pyc File "build\bdist.win32\egg\paramiko\auth_handler.py", line 311 except SSHException, e: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\ber.py to ber.cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\buffered_pipe.py to buffered_pipe. cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\channel.py to channel.cpython-33.p yc File "build\bdist.win32\egg\paramiko\channel.py", line 586 except PipeTimeout, e: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\client.py to client.cpython-33.pyc File "build\bdist.win32\egg\paramiko\client.py", line 409 except SSHException, e: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\common.py to common.cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\compress.py to compress.cpython-33 .pyc byte-compiling build\bdist.win32\egg\paramiko\config.py to config.cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\dsskey.py to dsskey.cpython-33.pyc File "build\bdist.win32\egg\paramiko\dsskey.py", line 188 except BERException, x: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\file.py to file.cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\hostkeys.py to hostkeys.cpython-33 .pyc byte-compiling build\bdist.win32\egg\paramiko\kex_gex.py to kex_gex.cpython-33.p yc byte-compiling build\bdist.win32\egg\paramiko\kex_group1.py to kex_group1.cpytho n-33.pyc File "build\bdist.win32\egg\paramiko\kex_group1.py", line 35 P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBE A63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B5 76625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6492866 51ECE65381FFFFFFFFFFFFFFFFL ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\logging22.py to logging22.cpython- 33.pyc byte-compiling build\bdist.win32\egg\paramiko\message.py to message.cpython-33.p yc File "build\bdist.win32\egg\paramiko\message.py", line 279 if i > 0xffffffffL: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\packet.py to packet.cpython-33.pyc File "build\bdist.win32\egg\paramiko\packet.py", line 92 self.__sequence_number_out = 0L ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\pipe.py to pipe.cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\pkey.py to pkey.cpython-33.pyc File "build\bdist.win32\egg\paramiko\pkey.py", line 306 except base64.binascii.Error, e: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\primes.py to primes.cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\resource.py to resource.cpython-33 .pyc byte-compiling build\bdist.win32\egg\paramiko\rng.py to rng.cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\rng_posix.py to rng_posix.cpython- 33.pyc byte-compiling build\bdist.win32\egg\paramiko\rng_win32.py to rng_win32.cpython- 33.pyc File "build\bdist.win32\egg\paramiko\rng_win32.py", line 58 except Exception, exc: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\rsakey.py to rsakey.cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\server.py to server.cpython-33.pyc File "build\bdist.win32\egg\paramiko\server.py", line 589 except Exception, e: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\sftp.py to sftp.cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\sftp_attr.py to sftp_attr.cpython- 33.pyc File "build\bdist.win32\egg\paramiko\sftp_attr.py", line 47 FLAG_EXTENDED = 0x80000000L ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\sftp_client.py to sftp_client.cpyt hon-33.pyc File "build\bdist.win32\egg\paramiko\sftp_client.py", line 87 except EOFError, x: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\sftp_file.py to sftp_file.cpython- 33.pyc File "build\bdist.win32\egg\paramiko\sftp_file.py", line 460 except Exception, x: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\sftp_handle.py to sftp_handle.cpyt hon-33.pyc File "build\bdist.win32\egg\paramiko\sftp_handle.py", line 103 except IOError, e: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\sftp_server.py to sftp_server.cpyt hon-33.pyc File "build\bdist.win32\egg\paramiko\sftp_server.py", line 95 except Exception, e: ^ SyntaxError: invalid syntax byte-compiling build\bdist.win32\egg\paramiko\sftp_si.py to sftp_si.cpython-33.p yc byte-compiling build\bdist.win32\egg\paramiko\ssh_exception.py to ssh_exception. cpython-33.pyc byte-compiling build\bdist.win32\egg\paramiko\transport.py to transport.cpython- 33.pyc File "build\bdist.win32\egg\paramiko\transport.py", line 353 out = ' build\bdist.win32\egg\EGG-INFO copying paramiko.egg-info\SOURCES.txt -> build\bdist.win32\egg\EGG-INFO copying paramiko.egg-info\dependency_links.txt -> build\bdist.win32\egg\EGG-INFO copying paramiko.egg-info\requires.txt -> build\bdist.win32\egg\EGG-INFO copying paramiko.egg-info\top_level.txt -> build\bdist.win32\egg\EGG-INFO zip_safe flag not set; analyzing archive contents... creating 'dist\paramiko-1.7.4-py3.3.egg' and adding 'build\bdist.win32\egg' to i t removing 'build\bdist.win32\egg' (and everything under it) Processing paramiko-1.7.4-py3.3.egg creating c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg Extracting paramiko-1.7.4-py3.3.egg to c:\python33\lib\site-packages File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\auth_han dler.py", line 311 except SSHException, e: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\channel. py", line 586 except PipeTimeout, e: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\client.p y", line 409 except SSHException, e: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\dsskey.p y", line 188 except BERException, x: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\kex_grou p1.py", line 35 P = 0xFFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBE A63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F14374FE1356D6D51C245E485B5 76625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6492866 51ECE65381FFFFFFFFFFFFFFFFL ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\message. py", line 279 if i > 0xffffffffL: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\packet.p y", line 92 self.__sequence_number_out = 0L ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\pkey.py" , line 306 except base64.binascii.Error, e: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\rng_win3 2.py", line 58 except Exception, exc: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\server.p y", line 589 except Exception, e: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\sftp_att r.py", line 47 FLAG_EXTENDED = 0x80000000L ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\sftp_cli ent.py", line 87 except EOFError, x: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\sftp_fil e.py", line 460 except Exception, x: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\sftp_han dle.py", line 103 except IOError, e: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\sftp_ser ver.py", line 95 except Exception, e: ^ SyntaxError: invalid syntax File "c:\python33\lib\site-packages\paramiko-1.7.4-py3.3.egg\paramiko\transpor t.py", line 353 out = '=1.9 Reading http://pypi.python.org/simple/pycrypto/ Download error on http://pypi.python.org/simple/pycrypto/: timed out -- Some pac kages may not be found! Reading http://pypi.python.org/simple/pycrypto/ 2012/12/27 Hugo Arts > On Thu, Dec 27, 2012 at 2:16 PM, Ufuk Eskici wrote: > >> This is the output, it fails. >> >> Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] >> on win32 >> Type "copyright", "credits" or "license()" for more information. >> >>> import paramiko >> >> Traceback (most recent call last): >> File "", line 1, in >> import paramiko >> ImportError: No module named paramiko >> >>> >> >> screenshot of search result: http://i49.tinypic.com/21kmtfp.jpg >> >> I inslatted accoording to this link: >> http://vijaymurthy.blogspot.com/2011/03/installing-paramiko-for-windows.html >> >> > Looking at that link, the guide doesn't actually install paramiko, it only > builds it. try issuing this command in the same folder where you issued the > command of step 4: > > python setup.py install > > please report results. > > Hugo > -------------- next part -------------- An HTML attachment was scrubbed... URL: From d at davea.name Thu Dec 27 17:16:13 2012 From: d at davea.name (Dave Angel) Date: Thu, 27 Dec 2012 11:16:13 -0500 Subject: [Tutor] Trouble importing Paramiko In-Reply-To: References: Message-ID: <50DC744D.4030902@davea.name> On 12/27/2012 06:44 AM, Ufuk Eskici wrote: First comment: please don't top-post. You've done it many times now, and it's not how this forum works. Put your comments after the parts you're quoting. (My email is mostly busted; I've been trying to send this for hours) First question: what version of Python ? And do you know where it's installed? Since you're on Windows, I won't bother to tell you to use the which command. Your latest post seems to imply you're using a 2.x version of paramiko, and trying to install it on Python 3.3 > My code is: > > import paramiko > ssh = paramiko.SSHClient() > ssh.connect('710.10.10.10', username='ufuk', password='ufuk') > > and saved this file as "ufo.py" on the desktop. > > I'm still getting error: > >>>> ================================ RESTART > ================================ What's this? Are you working in some environment other than the command line? If you are, then you should make sure it all works on the command line before trying to separately debug why the IDE (or whatever) is messing you up. > Traceback (most recent call last): > File "C:\Users\eufuesk\Desktop\ufo.py", line 1, in > import paramiko > ImportError: No module named paramiko > Now we're getting somewhere. Now the questions are where did paramiko get installed to, and where is python looking for it. For the former, hopefully you remember what it said when it installed, or you can search the drive for paramiko.* dir /s \paramiko.* For the latter, you can add the following line to ufo.py, just before the import line: import sys print "import path is ", sys.path -- DaveA From alan.gauld at btinternet.com Thu Dec 27 18:09:50 2012 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Thu, 27 Dec 2012 17:09:50 +0000 (GMT) Subject: [Tutor] how to control putty window In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> Message-ID: <1356628190.11950.YahooMailNeo@web186002.mail.ir2.yahoo.com> It looks like you have named your program paramiko.py? That is hiding the module. Try renaming your script. BTW its probavbly a bad idea to keepm Python scripts on the Desktop. Better to create a folder. ? Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ >________________________________ > From: Ufuk Eskici >To: Alan Gauld >Cc: "tutor at python.org" >Sent: Thursday, 27 December 2012, 9:53 >Subject: Re: [Tutor] how to control putty window > > >It seems it is looking for Paramiko under wrong folder. > > >>>> import paramiko > > >Traceback (most recent call last): >? File "", line 1, in >? ? import paramiko >? File "C:/Users/eufuesk/Desktop\paramiko.py", line 3, in >? ? ssh = paramiko.SSHClient() >AttributeError: 'module' object has no attribute 'SSHClient' >>>> os.chdir("c:\\Python27") > > > > >Which directory should I use? > > > >2012/12/27 Alan Gauld > >On 27/12/12 09:36, Ufuk Eskici wrote: >> >> >>I've installed Paramiko on my PC with Python 2.7. >>> >>>My code is: >>>import paramiko >>>import os >>>ssh = paramiko.SSHClient() >>>ssh.connect('10.10.10.10', username='ufuk', password='ufuk') >>> >>> >>>Traceback (most recent call last): >>>? ?File "C:/Users/eufuesk/Desktop/paramiko.py", line 1, in >>>? ? ?import paramiko >>>? ?File "C:/Users/eufuesk/Desktop\paramiko.py", line 4, in >>>? ? ?ssh = paramiko.SSHClient() >>>AttributeError: 'module' object has no attribute 'SSHClient' >>>?>>> >>> >>Did paramiko install properly? >>Can you access anything in the module? >> >>What happens if you do >> >>>>> import paramiko >>>>> dir(paramiko) >> >>or >> >>>>> help(paramiko) >> >>If those didn't work it suggests the install didn't work properly. >>If they do work then I don't know what's wrong. >> >> >>-- >>Alan G >>Author of the Learn to Program web site >>http://www.alan-g.me.uk/ >> >>_______________________________________________ >>Tutor maillist ?- ?Tutor at python.org >>To unsubscribe or change subscription options: >>http://mail.python.org/mailman/listinfo/tutor >> > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Thu Dec 27 18:13:44 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 27 Dec 2012 17:13:44 +0000 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: <50D515B4.9050706@pearwood.info> Message-ID: On 24 December 2012 04:42, eryksun wrote: > On Sat, Dec 22, 2012 at 12:57 PM, Oscar Benjamin > wrote: >>>> >>>> def make_int(obj): >>>> '''Coerce str, float and int to int without rounding error >>>> Accepts strings like '4.0' but not '4.1' >>>> ''' >>>> fnum = float('%s' % obj) >>>> inum = int(fnum) >>>> assert inum == fnum >>>> return inum >>> >>> Well, that function is dangerously wrong. In no particular order, >>> I can find four bugs and one design flaw. >> >> I expected someone to object to this function. I had hoped that they >> might also offer an improved version, though. I can't see a good way >> to do this without special casing the treatment of some or other type >> (the obvious one being str). > > Strings don't implement __int__ or __trunc__; they aren't numbers, so > why not special case them? I hadn't realised that. Does the int(obj) function use isinstance(obj, str) under the hood? > You can parse strings with obj = > Decimal(obj) (this uses a regex). Then for all inputs set inum = > int(obj) and raise ValueError if inum != obj. It had occurred to me that this would be the obvious fix for the issue with large numbers. The result is: from decimal import Decimal def int_decimal(x): if isinstance(x, str): x = Decimal(x) ix = int(x) if ix != x: raise ValueError('Not an integer: %s' % x) return ix Probably what I more often want, though, is a function that simply refuses to handle real-valued types as inputs. That way if a float sneaks in I can choose the appropriate rounding function (or bug fix) at the source of the number. I'm not sure what's the best way to detect real-valued types. At least for the stdlib using the numbers module works: from numbers import Integral def int_(x): if not isinstance(x, (Integral, str)): raise TypeError('Need Integral: use round() or trunc()') return int(x) Oscar From alan.gauld at btinternet.com Thu Dec 27 18:14:20 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 27 Dec 2012 17:14:20 +0000 Subject: [Tutor] how to control putty window In-Reply-To: <50DC23CE.6030502@davea.name> References: <5B80DD153D7D744689F57F4FB69AF474180A4810@SCACMX008.exchad.jpmchase.net> <50DC23CE.6030502@davea.name> Message-ID: On 27/12/12 10:32, Dave Angel wrote: > Another comment: when starting a new thread, please use a fresh email > to tutor at python.org. Don't use reply to an existing message, or your > query can get lost in the noise. And of course pick a subject line that > matches your query, like "Trouble importing paramiko" To be fair this is part of the putty thread in that he was suggested paramiko as an alternative to controlling putty... It's still part of the original problem solution, its just that the original subject was a tad too specific. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Dec 27 18:30:50 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 27 Dec 2012 17:30:50 +0000 Subject: [Tutor] help In-Reply-To: <1356610025.95729.YahooMailNeo@web160705.mail.bf1.yahoo.com> References: <1356610025.95729.YahooMailNeo@web160705.mail.bf1.yahoo.com> Message-ID: On 27/12/12 12:07, Randy WhiteWolf wrote: > Phthon Programming for the Absolute Beginner by Michael Dawson. I have > copied the code verbatim below. > # Sound the system bell > print "\a" > ... > raw_input ("\n\nPress the enter key to exit.") > My problem is I hear no system bell; the enter doesn't respond by > quitting the program; How are you running the code? I suspect you are using IDLE or Pythonwin or some other IDE? These generally don't obey the standard escape character conventions. And in fact not all terminals do either! If you put the code in a text file called screen.py (or whatever you prefer) and run it from the command line with C:\Windows> python screens.py You might see something more like the expected result. But a lot depends on your OS and terminal software. My Linux terminal doesn't ring a bell (or flash) for example. Lesson: don't rely on terminal escape characters unless you know exactly what terminal your users will be using. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Thu Dec 27 18:49:54 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 28 Dec 2012 04:49:54 +1100 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: <50D515B4.9050706@pearwood.info> Message-ID: <50DC8A42.5050604@pearwood.info> On 23/12/12 04:57, Oscar Benjamin wrote: > On 22 December 2012 02:06, Steven D'Aprano wrote: >> On 18/12/12 01:36, Oscar Benjamin wrote: >> >>> I have often found myself writing awkward functions to prevent a >>> rounding error from occurring when coercing an object with int(). >>> Here's one: >>> >>> def make_int(obj): >>> '''Coerce str, float and int to int without rounding error >>> Accepts strings like '4.0' but not '4.1' >>> ''' >>> fnum = float('%s' % obj) >>> inum = int(fnum) >>> assert inum == fnum >>> return inum >> >> Well, that function is dangerously wrong. In no particular order, >> I can find four bugs and one design flaw. > > I expected someone to object to this function. I had hoped that they > might also offer an improved version, though. I can't see a good way > to do this without special casing the treatment of some or other type > (the obvious one being str). Why is that a problem? I think this should do the trick. However, I am lazy and have not tested it, so this is your opportunity to catch me writing buggy code :-) def make_int(obj): try: # Fail if obj is not numeric. obj + 0 except TypeError: # For simplicity, I require that objects that convert to # ints always do so losslessly. try: return int(obj) except ValueError: obj = float(obj) # If we get here, obj is numeric. But is it an int? n = int(obj) # This may fail if obj is a NAN or INF. if n == obj: return n raise ValueError('not an integer') > Although you have listed 5 errors I would have written the same list > as 2 errors: > 1) You don't like my use of assert. That's more than a mere personal preference. See below. > 2) The function doesn't work for large numbers (bigger than around > 100000000000000000). It's not just that it "doesn't work", but it experiences distinct failure modes. If you were writing regression tests for these bugs, you would need *at least* two such tests: - large strings convert exactly; - for int n, make_int(n) always returns n If I were writing unit tests, I would ensure that I had a unit test for each of the failures I showed. > I would also add: > 3) It's ridiculous to convert types several times just to convert to > an integer without rounding. Perhaps. Even if that is the case, that's not a bug, merely a slightly less efficient implementation. > Whether or not assert is appropriate depends on the context (although > I imagine that some people would disapprove of it always). I would say > that if you are using assert then it should really be in a situation > where you're not really looking to handle errors but just to abort the > program and debug when something goes wrong. In that context I think > that, far from being confusing, assert statements make it plainly > clear what the programmer who wrote them was meaning to do. And what is that? "I only sometimes want to handle errors, sometimes I want errors to silently occur without warning"? Asserts can be disabled by the person running your code. That alone means that assert is *never* suitable for error checking, because you cannot be sure if your error checking is taking place or not. It is as simple as that. So what is assert useful for? - Asserts are really handy for testing in the interactive interpreter; assert is a lazy person's test, but when you're being quick and dirty, that's a feature, not a bug. - Asserts are also useful for test suites, although less so because you cannot run your test suite with optimizations on. - Asserts are good for checking the internal logic and/or state of your program. This is not error checking in the usual sense, since you are not checking that data is okay, but defensively checking that your code is okay. What do I mean by that last one? If you're ever written defensive code with a comment saying "This cannot ever happen", this is a good candidate for an assertion. Good defensive technique is to be very cautious about the assumptions you make: just because you think something cannot happen, doesn't mean you are correct. So you test your own logic by checking that the thing you think must be true is true, and raise an error if it turns out you are wrong. But it seems pretty wasteful and pointless to be checking something that you know is always correct. Especially if those checks are expensive, you might want to turn them off. Hence, you use assert, which can be turned off. This is a trade-off, of course: you're trading a bit of extra speed for a bit more risk of a silent failure. If you're aren't confident enough to make that trade-off, you are better off using an explicit, non-assert check. It's a subtle difference, and a matter of personal judgement where the line between "internal logic" and "error checking" lies. But here's an example of what I consider a check of internal logic, specifically that numbers must be zero, positive or negative: # you can assume that x is a numeric type like int, float, Decimal, etc. if x == 0: handle_zero() elif x > 0: handle_positive() elif x < 0: handle_negative() else: # This cannot ever happen, so we make an assertion. assert False, "x is neither less than, greater than, or equal to zero" I've deliberately given an example where the internal logic is actually *wrong*. You might think that the assertion is pointless because the logic is self-evidently correct, but without the final else clause, there is a bug waiting to happen. With the assert, instead of the program silently doing the wrong thing, it will loudly and immediately fail with an AssertionError. Bonus points to anyone who can tell me what is wrong with my logic that any numeric type must be either less than, equal, or greater than zero. > >> Lest you think that it is only humongous numbers where this is a >> problem, it is not. A mere seventeen digits is enough: >> >> py> s = "10000000000000001" >> py> make_int(s) - int(s) >> -1L > > I think that most people would consider one hundred thousand million > million and one to be a fairly big number. That's because most people are neither mathematicians, nor programmers. That's less than 2**54, a mere seven bytes. -- Steven From oscar.j.benjamin at gmail.com Thu Dec 27 20:22:16 2012 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 27 Dec 2012 19:22:16 +0000 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: <50DC8A42.5050604@pearwood.info> References: <50D515B4.9050706@pearwood.info> <50DC8A42.5050604@pearwood.info> Message-ID: On 27 December 2012 17:49, Steven D'Aprano wrote: > On 23/12/12 04:57, Oscar Benjamin wrote: >> >> On 22 December 2012 02:06, Steven D'Aprano wrote: >>> >>> On 18/12/12 01:36, Oscar Benjamin wrote: >>> >>>> I have often found myself writing awkward functions to prevent a >>>> rounding error from occurring when coercing an object with int(). >>>> Here's one: >>>> >>>> def make_int(obj): >>>> '''Coerce str, float and int to int without rounding error >>>> Accepts strings like '4.0' but not '4.1' >>>> ''' >>>> fnum = float('%s' % obj) >>>> inum = int(fnum) >>>> assert inum == fnum >>>> return inum >>> >>> >>> Well, that function is dangerously wrong. In no particular order, >>> I can find four bugs and one design flaw. >> >> >> I expected someone to object to this function. I had hoped that they >> might also offer an improved version, though. I can't see a good way >> to do this without special casing the treatment of some or other type >> (the obvious one being str). > > > Why is that a problem? > > > I think this should do the trick. However, I am lazy and have not > tested it, so this is your opportunity to catch me writing buggy > code :-) > > > def make_int(obj): > try: > # Fail if obj is not numeric. > obj + 0 > except TypeError: > # For simplicity, I require that objects that convert to > # ints always do so losslessly. > try: > return int(obj) > except ValueError: > obj = float(obj) > # If we get here, obj is numeric. But is it an int? > n = int(obj) # This may fail if obj is a NAN or INF. > if n == obj: > return n > raise ValueError('not an integer') This one has another large number related problem (also solved by using Decimal instead of float): >>> make_int('100000000000000000.1') 100000000000000000 Otherwise the function is good and it demonstrates my original point quite nicely: the function we've ended up with is pretty horrific for such a simple operation. It's also not something that a novice programmer could be expected to write or perhaps even to fully understand. In my ideal world the int() function would always raise an error for non-integers. People would have to get used to calling trunc() in place of int() but only in the (relatively few) places where they actually wanted that behaviour. The resulting code would be more explicit about when numeric values were being altered and what kind of rounding is being used, both of which are good things. At one point a similar (perhaps better) idea was discussed on python-dev: http://mail.python.org/pipermail/python-dev/2008-January/076481.html but it was rejected citing backwards compatibility concerns: http://mail.python.org/pipermail/python-dev/2008-January/076552.html > >> Whether or not assert is appropriate depends on the context (although >> I imagine that some people would disapprove of it always). I would say >> that if you are using assert then it should really be in a situation >> where you're not really looking to handle errors but just to abort the >> program and debug when something goes wrong. In that context I think >> that, far from being confusing, assert statements make it plainly >> clear what the programmer who wrote them was meaning to do. > > And what is that? "I only sometimes want to handle errors, sometimes I > want errors to silently occur without warning"? > > Asserts can be disabled by the person running your code. That alone means > that assert is *never* suitable for error checking, because you cannot be > sure if your error checking is taking place or not. It is as simple as > that. Maybe no-one else will ever run your code. This is the case for much of the code that I write. > So what is assert useful for? > > - Asserts are really handy for testing in the interactive interpreter; > assert is a lazy person's test, but when you're being quick and dirty, > that's a feature, not a bug. This would be my number one reason for using an assert (probably also the reason in that particular case). > > - Asserts are also useful for test suites, although less so because you > cannot run your test suite with optimizations on. I've seen this done a few times for example here: https://github.com/sympy/sympy/blob/master/sympy/assumptions/tests/test_matrices.py but I hadn't considered that particular problem. I think the reason for using them in sympy is that py.test has a special handler for pulling apart assert statements to show you the values in the expression that failed. > - Asserts are good for checking the internal logic and/or state of your > program. This is not error checking in the usual sense, since you are > not checking that data is okay, but defensively checking that your > code is okay. This is always the case when I use an assert. I don't want to catch the error and I also think the condition is, for some reason, always true unless my own code has a bug somewhere. > > It's a subtle difference, and a matter of personal judgement where the > line between "internal logic" and "error checking" lies. But here's an > example of what I consider a check of internal logic, specifically > that numbers must be zero, positive or negative: > > > # you can assume that x is a numeric type like int, float, Decimal, etc. > if x == 0: > handle_zero() > elif x > 0: > handle_positive() > elif x < 0: > handle_negative() > else: > # This cannot ever happen, so we make an assertion. > assert False, "x is neither less than, greater than, or equal to zero" If I'm feeling really lazy I might just use a bare raise statement for this kind of thing. Obviously that doesn't work if there is an exception to be re-raised, though. > > > I've deliberately given an example where the internal logic is actually > *wrong*. You might think that the assertion is pointless because the > logic is self-evidently correct, but without the final else clause, there > is a bug waiting to happen. With the assert, instead of the program silently > doing the wrong thing, it will loudly and immediately fail with an > AssertionError. > > Bonus points to anyone who can tell me what is wrong with my logic that any > numeric type must be either less than, equal, or greater than zero. A NaN leads there and I think it's a good example of where I would use an assert. Generally I want to abort if I get a NaN. It would be even better if I could tell Python to abort instantly when the NaN is generated but that's a separate issue. Oscar From francois.dion at gmail.com Thu Dec 27 21:16:27 2012 From: francois.dion at gmail.com (Francois Dion) Date: Thu, 27 Dec 2012 15:16:27 -0500 Subject: [Tutor] Trouble importing Paramiko In-Reply-To: References: <50DC3F62.7090109@pearwood.info> <50DC4389.5000604@pearwood.info> Message-ID: On Thu, Dec 27, 2012 at 8:48 AM, Ufuk Eskici wrote: > I got this output with lots of errors: > File "build\bdist.win32\egg\paramiko\auth_handler.py", line 311 > except SSHException, e: > ^ > SyntaxError: invalid syntax Use Python 2.x, Python 3 is in the works, kindoff: https://github.com/paramiko/paramiko/issues/16 https://github.com/paramiko/paramiko/issues/123 Fran?ois -- www.pyptug.org - raspberry-python.blogspot.com From eryksun at gmail.com Fri Dec 28 07:34:21 2012 From: eryksun at gmail.com (eryksun) Date: Fri, 28 Dec 2012 01:34:21 -0500 Subject: [Tutor] Limitation of int() in converting strings In-Reply-To: References: <50D515B4.9050706@pearwood.info> Message-ID: On Thu, Dec 27, 2012 at 12:13 PM, Oscar Benjamin wrote: > > I hadn't realised that. Does the int(obj) function use isinstance(obj, > str) under the hood? Yes. int_new and long_new use the macros PyString_Check (in 3.x PyBytes_Check) and PyUnicode_Check, which check the type's tp_flags. The C API can check for a subclass via tp_flags for the following types: #define Py_TPFLAGS_INT_SUBCLASS (1L<<23) #define Py_TPFLAGS_LONG_SUBCLASS (1L<<24) #define Py_TPFLAGS_LIST_SUBCLASS (1L<<25) #define Py_TPFLAGS_TUPLE_SUBCLASS (1L<<26) #define Py_TPFLAGS_STRING_SUBCLASS (1L<<27) #define Py_TPFLAGS_UNICODE_SUBCLASS (1L<<28) #define Py_TPFLAGS_DICT_SUBCLASS (1L<<29) #define Py_TPFLAGS_BASE_EXC_SUBCLASS (1L<<30) #define Py_TPFLAGS_TYPE_SUBCLASS (1L<<31) In 3.x bit 27 is renamed Py_TPFLAGS_BYTES_SUBCLASS. nb_int (__int__) in a types's PyNumberMethods is a unaryfunc, so __int__ as designed can't have the optional "base" argument that's used for strings. That has to be special cased. Without a specified a base, int_new (in 3.x long_new) redirects to the abstract function PyNumber_Int (in 3.x PyNumber_Long). This tries __int__ and __trunc__ (the latter returns an Integral, which is converted to int) before checking for a string or char buffer. Using the buffer interface is the reason the following works for a bytearray in 2.x: >>> int(bytearray('123')) 123 but specifying a base fails: >>> int(bytearray('123'), 10) Traceback (most recent call last): File "", line 1, in TypeError: int() can't convert non-string with explicit base long_new in 3.x adds a PyByteArray_Check: >>> int(bytearray(b'123'), 10) 123 Regarding this whole debate, I think a separate constructor for strings would have been cleaner, but I'm not Dutch. Source links: 3.3, long_new (see 4277): http://hg.python.org/cpython/file/bd8afb90ebf2/Objects/longobject.c#l4248 3.3, PyNumber_Long: http://hg.python.org/cpython/file/bd8afb90ebf2/Objects/abstract.c#l1262 2.7.3, int_new: http://hg.python.org/cpython/file/70274d53c1dd/Objects/intobject.c#l1049 2.7.3, PyNumber_int: http://hg.python.org/cpython/file/70274d53c1dd/Objects/abstract.c#l1610 From ufukeskici at gmail.com Fri Dec 28 08:53:50 2012 From: ufukeskici at gmail.com (Ufuk Eskici) Date: Fri, 28 Dec 2012 09:53:50 +0200 Subject: [Tutor] Trouble importing Paramiko In-Reply-To: References: <50DC3F62.7090109@pearwood.info> <50DC4389.5000604@pearwood.info> Message-ID: Hello, My Python version is 2.7.3 and it is installed in "C:\Python27 (on the web, it says Paramiko-1.7.4 is supported with Python 2.7) @Dave: Previous output was from IDLE which should be same with CLI. I'm getting this output now from Windows Command Line: **************************************************** C:\Python27>python ufo.py Traceback (most recent call last): File "ufo.py", line 1, in import paramiko ImportError: No module named paramiko C:\Python27> **************************************************** This is the output after addning .sys path: **************************************************** C:\Python27>python ufo.py import path is ['C:\\Python27', 'C:\\Windows\\system32\\python27.zip', 'C:\\Pyt hon27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\ \lib\\lib-tk', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] Traceback (most recent call last): File "ufo.py", line 4, in import paramiko ImportError: No module named paramiko **************************************************** 2012/12/27 Francois Dion > On Thu, Dec 27, 2012 at 8:48 AM, Ufuk Eskici wrote: > > I got this output with lots of errors: > > File "build\bdist.win32\egg\paramiko\auth_handler.py", line 311 > > except SSHException, e: > > ^ > > SyntaxError: invalid syntax > > Use Python 2.x, Python 3 is in the works, kindoff: > > https://github.com/paramiko/paramiko/issues/16 > https://github.com/paramiko/paramiko/issues/123 > > Fran?ois > > -- > www.pyptug.org - raspberry-python.blogspot.com > -------------- next part -------------- An HTML attachment was scrubbed... URL: From onyxtic at gmail.com Fri Dec 28 13:30:07 2012 From: onyxtic at gmail.com (Evans Anyokwu) Date: Fri, 28 Dec 2012 12:30:07 +0000 Subject: [Tutor] help In-Reply-To: <1356610025.95729.YahooMailNeo@web160705.mail.bf1.yahoo.com> References: <1356610025.95729.YahooMailNeo@web160705.mail.bf1.yahoo.com> Message-ID: On Thu, Dec 27, 2012 at 12:07 PM, Randy WhiteWolf wrote: > I am an older newbie teaching myself Python programming. I copied the code > # Emonstrates escape sequences. This exercise is on page 22 of the Phthon > Programming for the Absolute Beginner by Michael Dawson. I have copied the > code verbatim below. > > > > # Sound the system bell > print "\a" > > print "\t\t\tFancy Credits" > > print "\t\t\t \\ \\ \\ \\ \\ \\ \\" > print "\t\t\t\tby > print "\t\t\tMichael Dawson > print "\t\t\t \\ \\ \\ \\ \\ \\ \\" > print "\nSpecial thanks goes out to:" > print "My hair stylist. Henry \'The Great\', who never says \"can\'t\"." > > raw_input ("\n\nPress the enter key to exit.") > > > My problem is I hear no system bell; the enter doesn't respond by quitting > the program; The problem with the program code the enter key hasn't worked > in earlier programs. > > I appreciate any advice I may recieve with this coding glitch. > > Sincerely, > Randy > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > I just tried your code and it worked for me. Like Alan and Steven have pointed out already, sounding the system bell depends on how you are running the code and your platform. On my computer I have Putty installed which I use to connect remotely to my server - so running that script remotely will not produce any sound. However, it worked when saved and run locally on my Windows computer. If on windows try this - >>> print '\a' If it still does not work make sure that your sound card is functioning and the volume it not muted. Good luck. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwpolska at gmail.com Sat Dec 29 19:34:18 2012 From: kwpolska at gmail.com (Kwpolska) Date: Sat, 29 Dec 2012 19:34:18 +0100 Subject: [Tutor] help In-Reply-To: References: <1356610025.95729.YahooMailNeo@web160705.mail.bf1.yahoo.com> Message-ID: On Fri, Dec 28, 2012 at 1:30 PM, Evans Anyokwu wrote: > I just tried your code and it worked for me. Like Alan and Steven have > pointed out already, sounding the system bell depends on how you are running > the code and your platform. > > On my computer I have Putty installed which I use to connect remotely to my > server - so running that script remotely will not produce any sound. WROOOOONG! print '\a' sends ASCII 0x07 to the terminal, which then is handled by the terminal the way you (or the developer) told it to handle it. Now, you have set PuTTY to ignore bells (or, more likely, did not bother to change the setting and got the default). This is why you did not hear it. >>> print 'sp\am' sp[beep!]m [= 73 70 07 6D] >>> > However, it worked when saved and run locally on my Windows computer. Because cmd (or whatever else you used) is set to make sound with the bell. -- Kwpolska stop html mail | always bottom-post www.asciiribbon.org | www.netmeister.org/news/learn2quote.html GPG KEY: 5EAAEA16 From msg.ufo at gmail.com Sun Dec 30 13:30:21 2012 From: msg.ufo at gmail.com (Mike G) Date: Sun, 30 Dec 2012 04:30:21 -0800 Subject: [Tutor] help Message-ID: Hi Randy > I am an older newbie teaching myself Python programming. > Me too :) > My problem is I hear no system bell; the enter doesn't respond by quitting the program; > The problem with the program code the enter key hasn't worked in earlier programs. > > I appreciate any advice I may recieve with this coding glitch. > I copied the code into a blank .py file and ran it from cmd in Windows XP x86 using Python 273, it worked fine - including the beep. As well, hitting "Enter" exited the program. It sounds like (no pun intended) it may be how you're running the program. I would use a py file and run it using cmd - holler if you need help, you may if the path to Python isn't good to go. Mike From merrittb7 at gmail.com Mon Dec 31 00:59:14 2012 From: merrittb7 at gmail.com (Brandon Merritt) Date: Sun, 30 Dec 2012 15:59:14 -0800 Subject: [Tutor] another for loop question - latin square Message-ID: I am having trouble figuring out a solution after a couple hours now of playing with the code. I'm trying to make a latin square using the code below: scaleorder = int(raw_input('Please enter a number for an n*n square: ')) topleft = int(raw_input('Please enter the top left number for the square: ')) firstrow = range((topleft),scaleorder+1) count = 0 while count < 8: for i in firstrow: print i count += 1 firstrow[i+1] ----------------------------------------------------------------------- It seemed like I could make the for loop work by doing something like this: for i in firstrow: print i, i+2 but that obviously is not a solution either. Any ideas? Thanks, Brandon -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Dec 31 01:27:22 2012 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 31 Dec 2012 11:27:22 +1100 Subject: [Tutor] another for loop question - latin square In-Reply-To: References: Message-ID: <50E0DBEA.5060009@pearwood.info> On 31/12/12 10:59, Brandon Merritt wrote: > I am having trouble figuring out a solution after a couple hours now of > playing with the code. I'm trying to make a latin square using the code > below: > > scaleorder = int(raw_input('Please enter a number for an n*n square: ')) > topleft = int(raw_input('Please enter the top left number for the square: ')) > firstrow = range((topleft),scaleorder+1) > > count = 0 > while count< 8: > for i in firstrow: > print i > count += 1 > firstrow[i+1] > > > ----------------------------------------------------------------------- > > It seemed like I could make the for loop work by doing something like this: > > for i in firstrow: > print i, i+2 > > > but that obviously is not a solution either. Any ideas? Absolutely none. I don't understand your question. What's a latin square? Is it the same as a magic square? What do you mean, "make the for loop work"? The for loop already works: it iterates over the range topleft to scaleorder inclusive. You say "that obviously is not a solution", but a solution to what? What is it supposed to do? I think that you need to think a bit more carefully about what you are trying to accomplish. Probably the most valuable piece of advice I ever received was that *writing code* should be the last thing you do when trying to solve a problem. When I have a tricky problem to accomplish, I will sometimes spend hours designing my code with pencil and paper before writing my first line of code. Can YOU solve a latin square using pen and paper? If you can't, how do you expect to write a program to do it? Start by writing down the steps that you would take to make a latin square. I suggest with starting with a fixed size, say, 5x5: * pick a number for the top left corner, say, 3 * write down the first row: 3 ? ? ? ? [you need to come up with a rule for making the row] * write down the second row: ? ? ? ? ? [again, you need to come up with a rule for making the next row] ... and so on for the other three rows. Now you have an algorithm for making 5 x 5 latin squares. Now you can write some code to do that! Once that is working, you can start thinking about changing from fixed 5 x 5 squares to arbitrary N x N sizes. -- Steven From d at davea.name Mon Dec 31 01:44:27 2012 From: d at davea.name (Dave Angel) Date: Sun, 30 Dec 2012 19:44:27 -0500 Subject: [Tutor] another for loop question - latin square In-Reply-To: References: Message-ID: <50E0DFEB.5000909@davea.name> On 12/30/2012 06:59 PM, Brandon Merritt wrote: > I am having trouble Please tell us what Python version you're targeting. It looks like 2.7, but you really should specify it for us. Next, you should tell us your skill level; are you experienced at another language and learning Python, or what? Is this latin square an assignment, from a tutorial or book, or just for fun? If from a tutorial, have you been doing the earlier problems? Have you completed any other Python projects which were non-trivial? Do you realize just how tricky a latin square is to create? > figuring out a solution after a couple hours now of > playing with the code. I'm trying to make a latin square For other readers, an nxn latin square (not a magic square) has the same n symbols in each row and in each column, with no duplicates in any row, nor in any column. For a 4x4 square, one solution might be A B C D B D A C C A D B D C B A > using the code > below: > > scaleorder = int(raw_input('Please enter a number for an n*n square: ')) > > > topleft = int(raw_input('Please enter the top left number for the square: > ')) > > firstrow = range((topleft),scaleorder+1) You do realize that this won't necessarily create a list of the requested size? Specifically, if the user didn't specify a top-left of exactly 1, then the size will be wrong. > count = 0 > > while count < 8: > for i in firstrow: > print i > count += 1 > firstrow[i+1] Just what did you expect the last line to accomplish? And what was the 8 supposed to mean? > > ----------------------------------------------------------------------- > > It seemed like I could make the for loop work by doing something like this: > > for i in firstrow: > print i, i+2 > > > but that obviously is not a solution either. Any ideas? > > Thanks, > Brandon > > > Have you constructed a latin square by hand, of any size ? (try 4 for your first attempt) Do you realize that you could build 3 lines and then discover that there is no valid fourth line for those 3? Have you learned how and why to build functions in your code? I maintain this problem is too tricky to do without at least factoring each row into a function. -- DaveA From msirenef at lightbird.net Mon Dec 31 01:49:23 2012 From: msirenef at lightbird.net (Mitya Sirenef) Date: Sun, 30 Dec 2012 19:49:23 -0500 Subject: [Tutor] another for loop question - latin square In-Reply-To: References: Message-ID: <50E0E113.2020403@lightbird.net> On 12/30/2012 06:59 PM, Brandon Merritt wrote: > I am having trouble figuring out a solution after a couple hours now > of playing with the code. I'm trying to make a latin square using the > code below: > > scaleorder = int(raw_input('Please enter a number for an n*n square: ')) > > > topleft = int(raw_input('Please enter the top left number for the > square: ')) > > firstrow = range((topleft),scaleorder+1) > > count = 0 > > while count < 8: > for i in firstrow: > print i > count += 1 > firstrow[i+1] > > > ----------------------------------------------------------------------- > > It seemed like I could make the for loop work by doing something like > this: > > for i in firstrow: > print i, i+2 It's a bit hard to understand what you're trying to do, but if my guess is anywhere close to truth, you might be trying to make the first line of a latin square by incrementing from a specified number. In this case, you should make a range from topleft to scaleorder+topleft, and if you want the top row randomized, you can use random.shuffle(): from random import shuffle scaleorder = int(raw_input('Please enter a number for an n*n square: ')) topleft = int(raw_input('Please enter the top left number for the square: ')) firstrow = range(topleft, scaleorder+topleft) shuffle(firstrow) print firstrow Does this help? -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ From alan.gauld at btinternet.com Mon Dec 31 01:51:07 2012 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 31 Dec 2012 00:51:07 +0000 Subject: [Tutor] another for loop question - latin square In-Reply-To: <50E0DBEA.5060009@pearwood.info> References: <50E0DBEA.5060009@pearwood.info> Message-ID: On 31/12/12 00:27, Steven D'Aprano wrote: > On 31/12/12 10:59, Brandon Merritt wrote: >> I am having trouble figuring out a solution after a couple hours now of >> playing with the code. I'm trying to make a latin square using the code >> below: I totally agree with everything Steven said. However there is one glaring mistake in your code.... >> count = 0 >> while count< 8: >> for i in firstrow: >> print i >> count += 1 >> firstrow[i+1] What do you think that last row is doing? Whatever it is, you are almost certainly wrong. But eventually it is likely to throw an index error... >> ----------------------------------------------------------------------- >> >> It seemed like I could make the for loop work by doing something like >> this: >> >> for i in firstrow: >> print i, i+2 >> >> but that obviously is not a solution either. Any ideas? It works perfectly. It prints out i and i+2 for every member of firstrow. Whether that's what you want to do is another matter. As Steven said, try working out what you want to do first, then write the code. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From devyncjohnson at gmail.com Thu Dec 20 04:16:03 2012 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Thu, 20 Dec 2012 03:16:03 -0000 Subject: [Tutor] Python Regex References Message-ID: <50D282EA.2040609@Gmail.com> I am trying to convert the SED code below to Python3.3. How do I reference the value of the first and second wildcards so that I can put the variable values in the "replace" portion of the code? SED (this works): export DATA=$(echo "$DATA" | sed -r -e "s|^ $|\1(>=>)\2|gI") Python3.3 (attempted): DATA = re.sub('^$', '\1(>=>)\2', DATA, flags=re.I) Thank you, DevynCJohnson at Gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From devyncjohnson at gmail.com Thu Dec 20 05:13:50 2012 From: devyncjohnson at gmail.com (Devyn Collier Johnson) Date: Thu, 20 Dec 2012 04:13:50 -0000 Subject: [Tutor] Python Regex References Message-ID: <50D2821F.9010207@Gmail.com> I am trying to convert the SED code below to Python3.3. How do I reference the value of the first and second wildcards so that I can put the variable values in the "replace" portion of the code? SED (this works): export DATA=$(echo "$DATA" | sed -r -e "s|^ $|\1(>=>)\2|gI") Python3.3 (attempted): DATA = re.sub('^$', '\1(>=>)\2', DATA, flags=re.I) Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From dustinguerri at gmail.com Mon Dec 17 00:09:47 2012 From: dustinguerri at gmail.com (Dustin Guerri) Date: Sun, 16 Dec 2012 23:09:47 -0000 Subject: [Tutor] (no subject) Message-ID: Hi there, I'm completely new to Python and to programming. I have the Python Launcher (v2.7.3) app installed on my system, and I'm running Mac OS X Mountain Lion. Does this mean I have Python itself installed ? Many thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From msg.ufo at gmail.com Thu Dec 27 17:52:50 2012 From: msg.ufo at gmail.com (Mike G) Date: Thu, 27 Dec 2012 08:52:50 -0800 Subject: [Tutor] help Message-ID: Hi Randy > I am an older newbie teaching myself Python programming. > Me too :) > My problem is I hear no system bell; the enter doesn't respond by quitting the program; > The problem with the program code the enter key hasn't worked in earlier programs. > > I appreciate any advice I may recieve with this coding glitch. > I copied the code into a blank .py file and ran it from cmd in Windows XP x86 using Python 273, it worked fine - including the beep. As well, hitting "Enter" exited the program. It sounds like (no pun intended) it may be how you're running the program. I would use a py file and run it using cmd - holler if you need help, you may if the path to Python isn't good to go. Mike