From wprins at gmail.com Wed Dec 1 01:23:19 2010 From: wprins at gmail.com (Walter Prins) Date: Wed, 1 Dec 2010 00:23:19 +0000 Subject: [Tutor] Pyserial and invalid handle In-Reply-To: <4CF52D15.7030806@verizon.net> References: <4CF27B67.8000803@verizon.net> <4CF41EB6.3000802@verizon.net> <4CF52D15.7030806@verizon.net> Message-ID: Hello John, On 30 November 2010 16:57, John Smith wrote: > Hi, Walter - > > I did the above and then got this: > > >>> import serial > > > Traceback (most recent call last): > File "", line 1, in > import serial > File "E:\Python27\lib\site-packages\serial\__init__.py", line 18, in > > from serialwin32 import * > File "E:\Python27\lib\site-packages\serial\serialwin32.py", line 9, in > > import win32file # The base COM port and file IO functions. > ImportError: No module named win32file > >>> > > I guess that file was included in 2.5 but not in 2.4? > Apparently so. Well, win32file is part of the PyWin32 package, which are a set of modules that wrap many Windows API's. I'm not sure why it was't/isn't required for PySerial 2.5 or whether as you say perhaps this module is included in PySerial2.5 and isn't in 2.4. But whatever the case may be, suffice it to say I've reproduced your issue on my Win7 64bit box, and then resolved it by installing the PyWin32 modules. It's probably a good idea to install this package anyway -- if you're working on Windows the PyWin32 modules are very useful - they basically wrap and makes available a shedload of Windows specific API's to Python. (Many people working with Python on Windows almost automatically would install this, it's also why i didn't run into this issue in the first place as I already had PyWin32 installed prior to testing my suggestion. Sorry.) Anyway. To download and install PyWin32, go here: http://ur.ly/vLwv Presumably you want the AMD64 (64 bit) Py2.7 version. Install it then try your test again. Fingers crossed. ;) Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From jocjo.s at verizon.net Wed Dec 1 02:00:58 2010 From: jocjo.s at verizon.net (John Smith) Date: Tue, 30 Nov 2010 19:00:58 -0600 Subject: [Tutor] Pyserial and invalid handle In-Reply-To: References: <4CF27B67.8000803@verizon.net> <4CF41EB6.3000802@verizon.net> <4CF52D15.7030806@verizon.net> Message-ID: <4CF59E4A.6030107@verizon.net> On 11/30/2010 6:23 PM, Walter Prins wrote: > Hello John, (snip) > Apparently so. Well, win32file is part of the PyWin32 package, which > are a set of modules that wrap many Windows API's. I'm not sure why it > was't/isn't required for PySerial 2.5 or whether as you say perhaps this > module is included in PySerial2.5 and isn't in 2.4. > > But whatever the case may be, suffice it to say I've reproduced your > issue on my Win7 64bit box, and then resolved it by installing the > PyWin32 modules. It's probably a good idea to install this package > anyway -- if you're working on Windows the PyWin32 modules are very > useful - they basically wrap and makes available a shedload of Windows > specific API's to Python. (Many people working with Python on Windows > almost automatically would install this, it's also why i didn't run into > this issue in the first place as I already had PyWin32 installed prior > to testing my suggestion. Sorry.) > > Anyway. To download and install PyWin32, go here: http://ur.ly/vLwv > > Presumably you want the AMD64 (64 bit) Py2.7 version. Install it then > try your test again. > > Fingers crossed. ;) > > Walter Hi, Walter - I got pywin32-214.win32-py2.7.exe because I have the Intel i7 (I'm guessing that the AMD versions are for the AMD processor). However, all of the exe offerings have the same "Python not found in registry" problem that started this whole thing. So, since the only source module available is pywin32-214.zip, I got it and installed it. It does not work, maybe because I'm using Python 2.7 and the zip is for 3.2. I really appreciate all the time you have put into my problems, Walter. Thank you. Cheers, John From adam.jtm30 at gmail.com Wed Dec 1 02:27:22 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Wed, 01 Dec 2010 01:27:22 +0000 Subject: [Tutor] Pyserial and invalid handle In-Reply-To: <4CF59E4A.6030107@verizon.net> References: <4CF27B67.8000803@verizon.net> <4CF41EB6.3000802@verizon.net> <4CF52D15.7030806@verizon.net> <4CF59E4A.6030107@verizon.net> Message-ID: <4CF5A47A.10502@gmail.com> On 01/12/10 01:00, John Smith wrote: > > Hi, Walter - > > I got pywin32-214.win32-py2.7.exe because I have the Intel i7 (I'm > guessing that the AMD versions are for the AMD processor). However, > all of the exe offerings have the same "Python not found in registry" > problem that started this whole thing. > > So, since the only source module available is pywin32-214.zip, I got > it and installed it. It does not work, maybe because I'm using Python > 2.7 and the zip is for 3.2. > > I really appreciate all the time you have put into my problems, > Walter. Thank you. > > Cheers, > John > Actually, AMD 64 is now the standard x86-64. It was originally designed by AMD because intel were making their Itanium thing but that didn't go so well. Anyway if you're running 64 bit windows that's probably why the 32-bit python install is having a problem. Download the version Walter suggested and you should be good to go. From jocjo.s at verizon.net Wed Dec 1 02:52:40 2010 From: jocjo.s at verizon.net (John Smith) Date: Tue, 30 Nov 2010 19:52:40 -0600 Subject: [Tutor] Pyserial and invalid handle In-Reply-To: <4CF5A47A.10502@gmail.com> References: <4CF27B67.8000803@verizon.net> <4CF41EB6.3000802@verizon.net> <4CF52D15.7030806@verizon.net> <4CF59E4A.6030107@verizon.net> <4CF5A47A.10502@gmail.com> Message-ID: <4CF5AA68.9080908@verizon.net> On 11/30/2010 7:27 PM, Adam Bark wrote: > On 01/12/10 01:00, John Smith wrote: >> >> Hi, Walter - >> >> I got pywin32-214.win32-py2.7.exe because I have the Intel i7 (I'm >> guessing that the AMD versions are for the AMD processor). However, >> all of the exe offerings have the same "Python not found in registry" >> problem that started this whole thing. >> >> So, since the only source module available is pywin32-214.zip, I got >> it and installed it. It does not work, maybe because I'm using Python >> 2.7 and the zip is for 3.2. >> >> I really appreciate all the time you have put into my problems, >> Walter. Thank you. >> >> Cheers, >> John >> > > Actually, AMD 64 is now the standard x86-64. It was originally designed > by AMD because intel were making their Itanium thing but that didn't go > so well. Anyway if you're running 64 bit windows that's probably why the > 32-bit python install is having a problem. Download the version Walter > suggested and you should be good to go. > Yes! I have gone no farther than to say ser.read() knowing that nothing is attached to the port and expected a delay of 5 seconds. It now does that, so I have a clue that it is working. I had no idea that the AMD thing was now standard. Thanks for that. I also found that the file Walter recommended did install from the exe while the non-AMD file did not due to the registry thing. Wow! All I can say is thanks to everybody for the help. Now I need to start trying to get a modem to talk to me. By the way, the whole purpose of doing this is to communicate with some test instruments via GPIB/HPIB to automate some testing that is time consuming. The last time I did this (using a BASIC program), it took about 45 minutes (not due to BASIC, but due to instrument response time). I was able to start the test, go to lunch, then analyze the data when I returned. Thanks again for the help. Cheers, John From wprins at gmail.com Wed Dec 1 03:15:41 2010 From: wprins at gmail.com (Walter Prins) Date: Wed, 1 Dec 2010 02:15:41 +0000 Subject: [Tutor] Web Harvesting & AJAX/Javascript In-Reply-To: References: Message-ID: Hello Roy On 29 November 2010 19:42, Roy Hinkelman wrote: > Researching this has led me to PAMIE and Selenium. PAMIE is giving me > problems with permissions, and the discussion group appears to be fading > away. I have not tried Selenium yet. Both open a browser instance, and PAMIE > is quite slow, and I expect Selenium to be quite slow as well. > > How are you navigating around these Javascript based pages? Is there a > library or plugin that I am missing? > I'm no expert in this area, however I have done some research in this direction in the past, also to deal with sites that use Javascript (obviously Python is very adept at interacting with sites that are plain HTML by itself, e.g. there's lxml, BeautifulSoup & friends etc) and I remain interested in this. Anyway so then, here's my $0.02 for what it's worth. Firstly, Selenium may be an option, I briefly played with it quite some time ago, I don't know PAMIE. However, I suspect what you might rather want to look at is perhaps something like the PyWebkitGTK (or PyWebkitQT) which leverages the webkit browser engine. (This kind of assumes you're on Linux... are you?) Some hopefully relevant links: http://webkit.org/ http://code.google.com/p/pywebkitgtk/ http://www.gnu.org/software/pythonwebkit/ http://www.pygtk.org/pygtkmozembed/ http://directfb.org/ Best wishes, Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Dec 1 12:19:48 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 01 Dec 2010 22:19:48 +1100 Subject: [Tutor] A regular expression problem In-Reply-To: References: <4CF28DD9.3090808@pearwood.info> Message-ID: <4CF62F54.6090301@pearwood.info> Josep M. Fontana wrote: [...] > I guess this is because the character encoding was not specified but > accented characters in the languages I'm dealing with should be > treated as a-z or A-Z, shouldn't they? No. a-z means a-z. If you want the localized set of alphanumeric characters, you need \w. Likewise 0-9 means 0-9. If you want localized digits, you need \d. > I mean, how do you deal with > languages that are not English with regular expressions? I would > assume that as long as you set the right encoding, Python will be able > to determine which subset of specific sequences of bytes count as a-z > or A-Z. Encodings have nothing to do with this issue. Literal characters a, b, ..., z etc. always have ONE meaning: they represent themselves (although possibly in a case-insensitive fashion). E means E, not ?, ?, ? or ?. Localization tells the regex how to interpret special patterns like \d and \w. This has nothing to do with encodings -- by the time the regex sees the string, it is already dealing with characters. Localization is about what characters are in categories ("is 5 a digit or a letter? how about ? ?"). Encoding is used to translate between bytes on disk and characters. For example, the character ? could be stored on disk as the hex bytes: \xcb # one byte \xc3\x8b # two bytes \xff\xfe\xcb\x00 # four bytes and more, depending on the encoding used. -- Steven From steve at pearwood.info Wed Dec 1 12:37:51 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 01 Dec 2010 22:37:51 +1100 Subject: [Tutor] How to handle exceptions raised inside a function? In-Reply-To: References: <4CF56B5F.3040903@pearwood.info> Message-ID: <4CF6338F.4010508@pearwood.info> Patty wrote: > This is very interesting to me - the below excerpt is something I was > trying to do for one of my programs and gave up on it: > >> A fifth approach, common in some other languages, is to return some >> arbitrary value, and set an error flag. The caller then has to write >> code like this: >> >> result = function(arguments) >> if not last_result_error: >> # no error occurred >> print "result is", result >> >> >> If you do this, I will *personally* track you down and beat you to >> death with a rather large fish. >> >> *wink* > > I think I was trying to do something like thius at the end of a function > I wrote- > > return 2 or return my_special_integer_mvar That syntax won't work. However, the basic idea is (moderately) sound: your function has a special value that means "something funny happened". Python very occasionally uses this: >>> "hello world".find("z") # not found -1 which you then use like this: result = string.find(target) if result == -1: # special value print("not found") else: print("found at position %d" % result) In general, this idiom is mildly disparaged in Python circles, but not forbidden. Exceptions are usually considered better. However, what I'm talking about is quite different. Here's how I might write the string find method using this (horrible) implementation: # Global status flag. find_succeeded = 0 def find(string, target): global find_succeeded if target in string: find_succeeded = 1 return string.find(target) else: find_succeeded = 0 # I never know what number to return... return 42 # that'll do... (In low-level languages like C, the number returned on failure (where I choose 42) is often whatever value happens to be in some piece of memory -- essentially a random number.) result = find("hello world", "z") if find_succeeded == 1: print("found at position %d" % result) else: print("not found") This is even more inconvenient and difficult than earlier. Consider what happens if you want to do two or more searches. Because they all report their status via the one global variable, you must inspect the global after each call, before making the next call, or the status will be lost. In Python you can do this: results = [s.find(target) for s in list_of_strings] and then, later, inspect each individual result to see if it was -1 or not. But with the global status idiom, you can't do that, because the status flag is lost once you call find() again. -- Steven From patty at cruzio.com Wed Dec 1 19:46:30 2010 From: patty at cruzio.com (patty at cruzio.com) Date: Wed, 1 Dec 2010 10:46:30 -0800 (PST) Subject: [Tutor] How to handle exceptions raised inside a function? In-Reply-To: <4CF6338F.4010508@pearwood.info> References: <4CF56B5F.3040903@pearwood.info> <4CF6338F.4010508@pearwood.info> Message-ID: I think I understand, I will have to reread this a couple times! But I do consider myself a C programmer so that probably explains why I was trying to write code that way. And you are right 'you must inspect the global > after each call, before making the next call'. I *was* setting up the function to check on and muck with this mvar before the end where I 'return' my own thing. So for Python programming you are advising us to use one of the other four approaches to error handling and not try to do something like: call custom function, something is returned to a mvar while processing each statement, which is then examined for content and if one of three 'bad' or 'good' things are in the mvar, trying to avoid return values of [0 1 -1 2 -2] because off-the-top-of-my-head these are meaningful in various languages, so come up with 3 way-out-there-numbers-of-my-own, such as your '42'example. Then try and 'return' this from the function back to the calling function, possibly main(). Then examine return value integer and have ' if statements' doing something depending... So does this mean I am thinking through this like a C programmer? And I don't think I would be checking a variable at all using the other four ways, I would just let an error happen and let the return value be whatever it is and let the exception come up (or my custom exception handler) and handle it, instead I was trying to get right in the middle of it and force things. Also I was trying to use the return value for purposes other than error. I think what I was trying to do would be like a Case Statement really if that were supported in Python. return my_special_integer_mvar So the above will not work? If you were to try this, do you have to return digits? You can't return an mvar (and hope it doesn't change on you while going back to calling program)? Thanks for confirming my understanding or confusion as the case may be!! Patty > Patty wrote: >> This is very interesting to me - the below excerpt is something I was >> trying to do for one of my programs and gave up on it: >> >>> A fifth approach, common in some other languages, is to return some >>> arbitrary value, and set an error flag. The caller then has to write >>> code like this: >>> >>> result = function(arguments) >>> if not last_result_error: >>> # no error occurred >>> print "result is", result >>> >>> >>> If you do this, I will *personally* track you down and beat you to >>> death with a rather large fish. >>> >>> *wink* >> >> I think I was trying to do something like thius at the end of a function >> I wrote- >> >> return 2 or return my_special_integer_mvar > > That syntax won't work. However, the basic idea is (moderately) sound: > your function has a special value that means "something funny happened". > Python very occasionally uses this: > > >>> "hello world".find("z") # not found > -1 > > which you then use like this: > > result = string.find(target) > if result == -1: # special value > print("not found") > else: > print("found at position %d" % result) > > In general, this idiom is mildly disparaged in Python circles, but not > forbidden. Exceptions are usually considered better. > > However, what I'm talking about is quite different. Here's how I might > write the string find method using this (horrible) implementation: > > > # Global status flag. > find_succeeded = 0 > > def find(string, target): > global find_succeeded > if target in string: > find_succeeded = 1 > return string.find(target) > else: > find_succeeded = 0 > # I never know what number to return... > return 42 # that'll do... > > (In low-level languages like C, the number returned on failure (where I > choose 42) is often whatever value happens to be in some piece of memory > -- essentially a random number.) > > > result = find("hello world", "z") > if find_succeeded == 1: > print("found at position %d" % result) > else: > print("not found") > > > This is even more inconvenient and difficult than earlier. Consider what > happens if you want to do two or more searches. Because they all report > their status via the one global variable, you must inspect the global > after each call, before making the next call, or the status will be > lost. In Python you can do this: > > results = [s.find(target) for s in list_of_strings] > > and then, later, inspect each individual result to see if it was -1 or > not. But with the global status idiom, you can't do that, because the > status flag is lost once you call find() again. > > > > -- > Steven > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From rdmoores at gmail.com Wed Dec 1 21:08:17 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 1 Dec 2010 12:08:17 -0800 Subject: [Tutor] How to handle exceptions raised inside a function? In-Reply-To: <4CF56B5F.3040903@pearwood.info> References: <4CF56B5F.3040903@pearwood.info> Message-ID: On Tue, Nov 30, 2010 at 13:23, Steven D'Aprano wrote: > Richard D. Moores wrote: >> >> Please take a look at 2 functions I just wrote to calculate the >> harmonic and geometric means of lists of positive numbers: >> . >> >> Both Hlist and Glist must contain only positive numbers, so I really >> need to test for this inside each function. But is there a good way to >> do this? What should the functions return should a non-positive number >> be detected? Is there a conventional Pythonic way to do this? > > (2) If you don't trust that a sensible exception will be raised, then do > your own error checking, and raise an exception. I'll go with this one because I do want both Hlist and Glist to contain only positive real numbers. So I'll go with Jerry Hill's suggestion for both H and G. See the two revised functions at . > For what it's worth, I have a module of statistics functions (shameless > plug: http://pypi.python.org/pypi/stats and > http://code.google.com/p/pycalcstats -- feedback and bug reports welcome) An impressive collection. Thanks for sharing! > that includes the harmonic and geometric mean. My harmonic mean looks like > this: > > def harmonic_mean(data): > ? ?try: > ? ? ? ?m = mean(1.0/x for x in data) > ? ?except ZeroDivisionError: > ? ? ? ?return 0.0 > ? ?if m == 0.0: > ? ? ? ?return math.copysign(float('inf'), m) > ? ?return 1/m math.copysign! Didn't know about that one. But "mean"? It's not a built-in function.. Dick > Notice that if the data includes one or more zeroes, the harmonic mean > itself will be zero: limit as x->0 of 1/x -> infinity, and 1/infinity -> 0. > If the sum of reciprocals itself cancels to zero, I return the infinity with > the appropriate sign. The only exceptions that could occur are: > > * mean will raise ValueError if the data is empty; > * if an argument is non-numeric, TypeError will occur when I take the > reciprocal of it. From steve at pearwood.info Wed Dec 1 23:11:17 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 02 Dec 2010 09:11:17 +1100 Subject: [Tutor] How to handle exceptions raised inside a function? In-Reply-To: References: <4CF56B5F.3040903@pearwood.info> Message-ID: <4CF6C805.8090206@pearwood.info> Richard D. Moores wrote: [...] >> def harmonic_mean(data): >> try: >> m = mean(1.0/x for x in data) >> except ZeroDivisionError: >> return 0.0 >> if m == 0.0: >> return math.copysign(float('inf'), m) >> return 1/m > > math.copysign! Didn't know about that one. But "mean"? It's not a > built-in function.. No, it's not, it's a function from my stats module. The naive version is simple: def mean(data): return sum(data)/len(data) My version is a bit more complicated than that, in order to minimize round-off error and support iterators. -- Steven From mehgcap at gmail.com Wed Dec 1 23:45:57 2010 From: mehgcap at gmail.com (Alex Hall) Date: Wed, 1 Dec 2010 17:45:57 -0500 Subject: [Tutor] permutations? Message-ID: Hi all, I am wondering if there is a python package that will find permutations? For example, if I have (1, 2, 3), the possibilities I want are: 12 13 23 123 132 231 Order does not matter; 21 is the same as 12, but no numbers can repeat. If no package exists, does someone have a hint as to how to get a function to do this? The one I have right now will not find 132 or 231, nor will it find 13. TIA. -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From hugo.yoshi at gmail.com Wed Dec 1 23:55:51 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 1 Dec 2010 23:55:51 +0100 Subject: [Tutor] permutations? In-Reply-To: References: Message-ID: On Wed, Dec 1, 2010 at 11:45 PM, Alex Hall wrote: > Hi all, > I am wondering if there is a python package that will find > permutations? For example, if I have (1, 2, 3), the possibilities I > want are: > 12 > 13 > 23 > 123 > 132 > 231 > > Order does not matter; 21 is the same as 12, but no numbers can > repeat. If no package exists, does someone have a hint as to how to > get a function to do this? The one I have right now will not find 132 > or 231, nor will it find 13. TIA. > Does order matter or not? You say (2, 1) == (1, 2) but you do list (1, 2, 3) and (1, 3, 2) as separate, so order does matter there. Be consistent. You probably want to look at the itertools.permutations and itertools.combinations functions. Hugo From steve at pearwood.info Thu Dec 2 00:05:47 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 02 Dec 2010 10:05:47 +1100 Subject: [Tutor] permutations? In-Reply-To: References: Message-ID: <4CF6D4CB.7010005@pearwood.info> Alex Hall wrote: > Hi all, > I am wondering if there is a python package that will find > permutations? What version of Python are you using? Starting from Python 2.6, the itertools module contains combinations, permutations and Cartesian product: >>> list(itertools.combinations([1,2,3], 1)) [(1,), (2,), (3,)] >>> list(itertools.combinations([1,2,3], 2)) [(1, 2), (1, 3), (2, 3)] >>> list(itertools.combinations([1,2,3], 3)) [(1, 2, 3)] >>> list(itertools.permutations([1,2,3], 1)) [(1,), (2,), (3,)] >>> list(itertools.permutations([1,2,3], 2)) [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)] >>> list(itertools.permutations([1,2,3], 3)) [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] >>> list(itertools.product([1,2,3], 'ab')) [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b'), (3, 'a'), (3, 'b')] For example, if I have (1, 2, 3), the possibilities I > want are: > 12 > 13 > 23 > 123 > 132 > 231 > > Order does not matter; According to your example, it certainly does -- you have 123 and 132 and 231 listed as three distinct possibilities. -- Steven From bgailer at gmail.com Thu Dec 2 00:12:03 2010 From: bgailer at gmail.com (bob gailer) Date: Wed, 01 Dec 2010 18:12:03 -0500 Subject: [Tutor] permutations? In-Reply-To: References: Message-ID: <4CF6D643.6000501@gmail.com> On 12/1/2010 5:45 PM, Alex Hall wrote: > Hi all, > I am wondering if there is a python package that will find > permutations? For example, if I have (1, 2, 3), the possibilities I > want are: > 12 > 13 > 23 > 123 > 132 > 231 > > Order does not matter; 21 is the same as 12, but no numbers can > repeat. If no package exists, does someone have a hint as to how to > get a function to do this? The one I have right now will not find 132 > or 231, nor will it find 13. TIA. According to Wikipedia " there are six permutations of the set {1,2,3}, namely [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]." Above you show some "combinations" and a subset of the permutations. What rules did you apply to come up with your result? -- Bob Gailer 919-636-4239 Chapel Hill NC From rdmoores at gmail.com Thu Dec 2 01:27:13 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 1 Dec 2010 16:27:13 -0800 Subject: [Tutor] How to handle exceptions raised inside a function? In-Reply-To: <4CF56B5F.3040903@pearwood.info> References: <4CF56B5F.3040903@pearwood.info> Message-ID: On Tue, Nov 30, 2010 at 13:23, Steven D'Aprano wrote: > For what it's worth, I have a module of statistics functions (shameless > plug: http://pypi.python.org/pypi/stats and > http://code.google.com/p/pycalcstats -- feedback and bug reports welcome) Your readme says: Installation ------------ stats requires Python 3.1. To install, do the usual: python3 setup.py install I'm afraid I've never fully understood instructions like that. I have Python 3.1. I now have your stats-0.1.1a. where do I put it to do the above? It's now in 3.1's site-packages. Do I CD to stats-0.1.1a and run that command? Or? Could I have put stats-0.1.1a anywhere, CD-ed to that "anywhere", and then run the command? I hesitate to experiment because I don't want your files sprayed all over my hard disk. Thanks, Dick From prologic at shortcircuit.net.au Thu Dec 2 01:41:19 2010 From: prologic at shortcircuit.net.au (James Mills) Date: Thu, 2 Dec 2010 10:41:19 +1000 Subject: [Tutor] How to handle exceptions raised inside a function? In-Reply-To: References: <4CF56B5F.3040903@pearwood.info> Message-ID: On Thu, Dec 2, 2010 at 10:27 AM, Richard D. Moores wrote: > Could I have put stats-0.1.1a anywhere, CD-ed to that "anywhere", and > then run the command? Yes. python setup.py install essentially instructs distutils (or setuptools or distribute - whichever is being used) to "install" the package into your site-packages or $PYTHONPATH (if configured that way by means of configuration). NB: Copying the package's directory into site-packages hwoever has the same effect. The directory that contains the __init__.py cheers James -- -- James Mills -- -- "Problems are solved by method" From rdmoores at gmail.com Thu Dec 2 03:03:24 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Wed, 1 Dec 2010 18:03:24 -0800 Subject: [Tutor] How to handle exceptions raised inside a function? In-Reply-To: References: <4CF56B5F.3040903@pearwood.info> Message-ID: On Wed, Dec 1, 2010 at 16:41, James Mills wrote: > On Thu, Dec 2, 2010 at 10:27 AM, Richard D. Moores wrote: >> Could I have put stats-0.1.1a anywhere, CD-ed to that "anywhere", and >> then run the command? > > Yes. Thanks, James. Did that. Thought I'd publicize Steven's stuff a bit. I pasted the "help on module stats" at Dick From mehgcap at gmail.com Thu Dec 2 03:19:38 2010 From: mehgcap at gmail.com (Alex Hall) Date: Wed, 1 Dec 2010 21:19:38 -0500 Subject: [Tutor] permutations? In-Reply-To: <4CF6D643.6000501@gmail.com> References: <4CF6D643.6000501@gmail.com> Message-ID: Thanks to everyone for the itertools hint; that sounds like it will work. Sorry I was not clearer: 1. Order matters; I meant to say that direction does not. That is, 123 is not the same as 213, but 123 is the same as 321 since the second example is simply a reversal. 2. I am looking for all permutations and subpermutations (if that is a word) of 1-n where the list must have at least 2, but no more than n, unique numbers (so 1 is not valid, nor is 1231 since it repeats 1 and is too long for n=3). I hope that makes sense. However, hopefully itertools will do it; if I run into problems I will respond to this email to keep it in the same thread. Thanks again! Oh, to the person who asked, I have 2.6 and 2.7 installed, with the default being 2.6. On 12/1/10, bob gailer wrote: > On 12/1/2010 5:45 PM, Alex Hall wrote: >> Hi all, >> I am wondering if there is a python package that will find >> permutations? For example, if I have (1, 2, 3), the possibilities I >> want are: >> 12 >> 13 >> 23 >> 123 >> 132 >> 231 >> >> Order does not matter; 21 is the same as 12, but no numbers can >> repeat. If no package exists, does someone have a hint as to how to >> get a function to do this? The one I have right now will not find 132 >> or 231, nor will it find 13. TIA. > > According to Wikipedia " there are six permutations of the set {1,2,3}, > namely [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]." > > Above you show some "combinations" and a subset of the permutations. > > What rules did you apply to come up with your result? > > -- > Bob Gailer > 919-636-4239 > Chapel Hill NC > > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From dana-s at charter.net Thu Dec 2 02:51:20 2010 From: dana-s at charter.net (Dana) Date: Wed, 01 Dec 2010 20:51:20 -0500 Subject: [Tutor] data structures Message-ID: <4CF6FB98.5010304@charter.net> Hello, I'm using Python to extract words from plain text files. I have a list of words. Now I would like to convert that list to a dictionary of features where the key is the word and the value the number of occurrences in a group of files based on the filename (different files correspond to different categories). What is the best way to represent this data? When I finish I expect to have about 70 unique dictionaries with values I plan to use in frequency distributions, etc. Should I use globally defined dictionaries? Dana From mehgcap at gmail.com Thu Dec 2 04:55:10 2010 From: mehgcap at gmail.com (Alex Hall) Date: Wed, 1 Dec 2010 22:55:10 -0500 Subject: [Tutor] permutations? In-Reply-To: References: <4CF6D643.6000501@gmail.com> Message-ID: Alright, I have it working. Now the problem is that it does not throw out reversals. I tried to do this myself with a couple loops, but I get index errors. My total list of permutations is called l. for i in range(0, len(l)): r=l[i]; r.reverse() for j in range(0, len(l)): print l[j], r, i, j if r==l[j]: l.remove(r) if r==l[j]: l.remove(r) IndexError: list index out of range When it has found two repeats (oddly, they are the same repeat - 2,1) and removed them, there are ten of the twelve items left. Once j hits 10, it errors out, obviously, since it is looking for l[10] which no longer exists. However, should the loop not re-evaluate len(l) each time, so this should not be a problem? Why is it still looking for an index that is no longer in the range (0, len(l)), and how can I fix this? Also, why would it find several items twice? For example, it picks up (2,1), then picks it up again. Is there a better way of doing this that would avoid me having to write the function? On 12/1/10, Alex Hall wrote: > Thanks to everyone for the itertools hint; that sounds like it will work. > > Sorry I was not clearer: > 1. Order matters; I meant to say that direction does not. That is, 123 > is not the same as 213, but 123 is the same as 321 since the second > example is simply a reversal. > 2. I am looking for all permutations and subpermutations (if that is a > word) of 1-n where the list must have at least 2, but no more than n, > unique numbers (so 1 is not valid, nor is 1231 since it repeats 1 and > is too long for n=3). > I hope that makes sense. However, hopefully itertools will do it; if I > run into problems I will respond to this email to keep it in the same > thread. Thanks again! Oh, to the person who asked, I have 2.6 and 2.7 > installed, with the default being 2.6. > > On 12/1/10, bob gailer wrote: >> On 12/1/2010 5:45 PM, Alex Hall wrote: >>> Hi all, >>> I am wondering if there is a python package that will find >>> permutations? For example, if I have (1, 2, 3), the possibilities I >>> want are: >>> 12 >>> 13 >>> 23 >>> 123 >>> 132 >>> 231 >>> >>> Order does not matter; 21 is the same as 12, but no numbers can >>> repeat. If no package exists, does someone have a hint as to how to >>> get a function to do this? The one I have right now will not find 132 >>> or 231, nor will it find 13. TIA. >> >> According to Wikipedia " there are six permutations of the set {1,2,3}, >> namely [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]." >> >> Above you show some "combinations" and a subset of the permutations. >> >> What rules did you apply to come up with your result? >> >> -- >> Bob Gailer >> 919-636-4239 >> Chapel Hill NC >> >> > > > -- > Have a great day, > Alex (msg sent from GMail website) > mehgcap at gmail.com; http://www.facebook.com/mehgcap > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From cfuller084 at thinkingplanet.net Thu Dec 2 05:48:43 2010 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Wed, 1 Dec 2010 22:48:43 -0600 Subject: [Tutor] data structures In-Reply-To: <4CF6FB98.5010304@charter.net> References: <4CF6FB98.5010304@charter.net> Message-ID: <201012012248.49464.cfuller084@thinkingplanet.net> It sounds to me like all you need is for the values to be another dictionary, keyed on filename? Don't use globals if you can help it. You should be able to structure your program such that they aren't necessary. Something like this? features = { feature1': {'file1': 12, 'file2': 0, 'file3':6 } feature2': {'file1': 4, 'file2': 17, 'file3':0 } } Cheers On Wednesday 01 December 2010, Dana wrote: > Hello, > > I'm using Python to extract words from plain text files. I have a list > of words. Now I would like to convert that list to a dictionary of > features where the key is the word and the value the number of > occurrences in a group of files based on the filename (different files > correspond to different categories). What is the best way to represent > this data? When I finish I expect to have about 70 unique dictionaries > with values I plan to use in frequency distributions, etc. Should I use > globally defined dictionaries? > > Dana > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From knacktus at googlemail.com Thu Dec 2 07:07:04 2010 From: knacktus at googlemail.com (Knacktus) Date: Thu, 02 Dec 2010 07:07:04 +0100 Subject: [Tutor] data structures In-Reply-To: <4CF6FB98.5010304@charter.net> References: <4CF6FB98.5010304@charter.net> Message-ID: <4CF73788.1050005@googlemail.com> Am 02.12.2010 02:51, schrieb Dana: > Hello, > > I'm using Python to extract words from plain text files. I have a list > of words. Now I would like to convert that list to a dictionary of > features where the key is the word and the value the number of > occurrences in a group of files based on the filename (different files > correspond to different categories). What is the best way to represent > this data? When I finish I expect to have about 70 unique dictionaries > with values I plan to use in frequency distributions, etc. Should I use > globally defined dictionaries? Depends on what else you want to do with the group of files. If you're expecting some operations on the group's data you should create a class to be able to add some more methods to the data. I would probably go with a class. class FileGroup(object): def __init__(self, filenames): self.filenames = filenames self.word_to_occurrences = {} self._populate_word_to_occurrences() def _populate_word_to_occurrences(): for filename in filenames: with open(filename) as fi: # do the processing Now you could add other meaningful data and methods to a group of files. But also I think dictionaries can be fine. If you really only need the dicts. You could create a function to create those. def create_word_to_occurrences(filenames): word_to_occurrences = {} for filename in filenames: with open(filename) as fi # do the processing return word_to_occurrences But as I said, if in doubt I would go for the class. > > Dana > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Thu Dec 2 09:38:07 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 2 Dec 2010 08:38:07 -0000 Subject: [Tutor] permutations? References: <4CF6D643.6000501@gmail.com> Message-ID: "Alex Hall" wrote > Alright, I have it working. Now the problem is that it does not > throw > out reversals. I tried to do this myself with a couple loops, but I > get index errors. My total list of permutations is called l. > > for i in range(0, len(l)): > r=l[i]; r.reverse() You don''t need to specify the 0, its the default. But you don't need to use indexing either. for r,i in enumerate(l): r.reverse() > for j in range(0, len(l)): > print l[j], r, i, j Here you are comparing the reversed item to every item in the list, including the ones you have already reversed, including the item itself. So you will always find a match when i==j... Using the in operator and list slicing would be easier if r in l[i:]: l.remove(r) but... > if r==l[j]: l.remove(r) Its never a good idea to remove items from a list you are iterating over, its like the old cartoon of the guy cutting down the tree branch he is sitting on. If you must do it I'd use a while loop and manually manage the indexing. Or operate on a copy of the list. > longer exists. However, should the loop not re-evaluate len(l) each > time, No because the loop uses range() - a function in its own right and range evaluates len() when first called and then return a sequence to the loop based on that. The loop does not use len() You can find an example of this type of thing at the end of the Branching topic of my tutorial. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ HTH, From alan.gauld at btinternet.com Thu Dec 2 09:43:50 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 2 Dec 2010 08:43:50 -0000 Subject: [Tutor] data structures References: <4CF6FB98.5010304@charter.net> Message-ID: "Dana" wrote > I'm using Python to extract words from plain text files. I have a > list of words. Now I would like to convert that list to a > dictionary of features where the key is the word and the value the > number of occurrences in a group of files based on the filename I think I know what you mean but an illustration of the desired output would be useful. Just to be sure. > correspond to different categories). What is the best way to > represent this data? When I finish I expect to have about 70 unique > dictionaries with values I plan to use in frequency distributions, > etc. Should I use globally defined dictionaries? You will either need a class/object to store the data or a set of global dictionaries (or a database/file I suppose but that only makes sense if you have a lot of data or need persistence). You should consider keeping the individual dictionaries in another dictionary so you only have a single global level variable. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From rdmoores at gmail.com Thu Dec 2 12:06:00 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 2 Dec 2010 03:06:00 -0800 Subject: [Tutor] a print puzzle Message-ID: >>> s = [.1,.1,.1,.1,.1,.1,.1,.1,.1,.1] >>> sum(s) 0.9999999999999999 >>> print(sum(s)) 1.0 Why? >>> f = 0.9999999999999999 >>> f**100 0.9999999999999889 >>> print(f**100) 1.0 Why? >>> f**10000 0.9999999999988898 >>> print(f**10000) 0.999999999999 >>> from math import fsum >>> fsum(s) 1.0 >>> from math import fsum >>> fsum(s) 1.0 See < http://docs.python.org/py3k/library/math.html?highlight=fsum#math.fsum> The "Why?"s mark what are puzzles for me. I see nothing in the doc for print() that explains these: < http://docs.python.org/py3k/library/functions.html?highlight=print#print>, but I'm sure a Tutor can. Dick Moores -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 2 12:13:08 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 2 Dec 2010 11:13:08 -0000 Subject: [Tutor] a print puzzle References: Message-ID: "Richard D. Moores" wrote >>>> s = [.1,.1,.1,.1,.1,.1,.1,.1,.1,.1] >>>> sum(s) > 0.9999999999999999 This uses repr() to display the result >>>> print(sum(s)) > 1.0 Why? This uses str() to display the result. In many cases str() calls repr() but in other cases str() is a more user friendly format. repr() tends to be a developers eye view of things. This is one reason that in my tutorial I always use print() to display results. The output is more newbie friendly that way :-) I suspect the details will be revealed by a search for documentation on __repr__ HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Thu Dec 2 13:53:22 2010 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Dec 2010 13:53:22 +0100 Subject: [Tutor] permutations? References: <4CF6D643.6000501@gmail.com> Message-ID: Alex Hall wrote: > 1. Order matters; I meant to say that direction does not. That is, 123 > is not the same as 213, but 123 is the same as 321 since the second > example is simply a reversal. > 2. I am looking for all permutations and subpermutations (if that is a > word) of 1-n where the list must have at least 2, but no more than n, > unique numbers (so 1 is not valid, nor is 1231 since it repeats 1 and > is too long for n=3). >>> from itertools import permutations, chain >>> def hall(items): ... items = list(items) ... maxwidth = len(items) ... return chain.from_iterable( ... (p for p in permutations(items, w) if p < p[::-1]) ... for w in range(2, maxwidth+1)) ... >>> map("".join, hall("123")) ['12', '13', '23', '123', '132', '213'] >>> map("".join, hall("111")) [] Is that it? Be warned though that normally you'll get duplicates if items repeat: >>> map("".join, hall("112")) ['12', '12', '112', '112'] Peter From shantanoo at gmail.com Thu Dec 2 14:54:40 2010 From: shantanoo at gmail.com (=?UTF-8?B?4KS24KSC4KSk4KSo4KWC?=) Date: Thu, 2 Dec 2010 19:24:40 +0530 Subject: [Tutor] permutations? In-Reply-To: References: Message-ID: Following link could be useful: http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python On Thu, Dec 2, 2010 at 04:15, Alex Hall wrote: > Hi all, > I am wondering if there is a python package that will find > permutations? For example, if I have (1, 2, 3), the possibilities I > want are: > 12 > 13 > 23 > 123 > 132 > 231 > > Order does not matter; 21 is the same as 12, but no numbers can > repeat. If no package exists, does someone have a hint as to how to > get a function to do this? The one I have right now will not find 132 > or 231, nor will it find 13. TIA. > > -- > Have a great day, > Alex (msg sent from GMail website) > mehgcap at gmail.com; http://www.facebook.com/mehgcap > _______________________________________________ > 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 rabidpoobear at gmail.com Thu Dec 2 15:07:06 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Thu, 2 Dec 2010 08:07:06 -0600 Subject: [Tutor] permutations? In-Reply-To: References: Message-ID: <7473BF82-0A6B-4D6A-9235-D2096E297AB9@gmail.com> Instead of nested fors which will grow exponentially in running time, consider this for removing reversals: newlist = [] tempdict = {} for i in oldlist: try: tempdict[i] except: tempdict[i] = 1 tempdict[i.reverse()] = 1 newlist.append(i) That's the gneral idea. Pseudocode, so untested. You exploit the O(1) hash lookup of the dict to avoid the runtime cost of the nested loops, and it is easier to read as well. Would need to timeit to be sure it helps with your data sets though. They may be small enough that it doesn't matter. ----------------------------- Sent from a mobile device with a bad e-mail client. ----------------------------- On Dec 2, 2010, at 7:54 AM, ????? wrote: > Following link could be useful: > > http://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list-in-python > > On Thu, Dec 2, 2010 at 04:15, Alex Hall wrote: > Hi all, > I am wondering if there is a python package that will find > permutations? For example, if I have (1, 2, 3), the possibilities I > want are: > 12 > 13 > 23 > 123 > 132 > 231 > > Order does not matter; 21 is the same as 12, but no numbers can > repeat. If no package exists, does someone have a hint as to how to > get a function to do this? The one I have right now will not find 132 > or 231, nor will it find 13. TIA. > > -- > Have a great day, > Alex (msg sent from GMail website) > mehgcap at gmail.com; http://www.facebook.com/mehgcap > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > _______________________________________________ > 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 james.homme at highmark.com Thu Dec 2 15:35:15 2010 From: james.homme at highmark.com (Homme, James) Date: Thu, 2 Dec 2010 09:35:15 -0500 Subject: [Tutor] Trying To Debug Code That Runs Arbitrary Function Message-ID: <86F022B554AF0447A870CEE306C14DC6026E9A6E18@EXMB1.highmark.com> Hi, If you can get away with not telling me the answer, but pointing me to where to look for the answer, I'd be grateful. In my Python learning, I am just now starting to understand how to make classes and extend them, so I have a very long way to go. I wrote this code because I wanted to avoid lots of if statements and having to maintain a bunch of code like that. Eventually, my idea is to read in strings from a file, look one up, and use it to execute a function. So I created the following code. But nothing gets printed to the screen. How do I go about figuring out why this isn't happening? Here's the code. myfuncs = [ "func1", "func2" ] def func1(): print "func 1" def func2(): print "func 2" eval (myfuncs[0]) raw_input("Press enter to quit") Thanks. Jim Jim Homme, Usability Services, Phone: 412-544-1810. Skype: jim.homme Internal recipients, Read my accessibility blog. Discuss accessibility here. Accessibility Wiki: Breaking news and accessibility advice ________________________________ This e-mail and any attachments to it are confidential and are intended solely for use of the individual or entity to whom they are addressed. If you have received this e-mail in error, please notify the sender immediately and then delete it. If you are not the intended recipient, you must not keep, use, disclose, copy or distribute this e-mail without the author's prior permission. The views expressed in this e-mail message do not necessarily represent the views of Highmark Inc., its subsidiaries, or affiliates. -------------- next part -------------- An HTML attachment was scrubbed... URL: From vceder at gmail.com Thu Dec 2 16:15:35 2010 From: vceder at gmail.com (Vern Ceder) Date: Thu, 2 Dec 2010 10:15:35 -0500 Subject: [Tutor] Trying To Debug Code That Runs Arbitrary Function In-Reply-To: <86F022B554AF0447A870CEE306C14DC6026E9A6E18@EXMB1.highmark.com> References: <86F022B554AF0447A870CEE306C14DC6026E9A6E18@EXMB1.highmark.com> Message-ID: Here's your hint... to execute a Python function, it must be followed by parentheses.... otherwise you are just referring to the function object. HTH, Vern On Thu, Dec 2, 2010 at 9:35 AM, Homme, James wrote: > Hi, > > If you can get away with not telling me the answer, but pointing me to > where to look for the answer, I'd be grateful. > > > > In my Python learning, I am just now starting to understand how to make > classes and extend them, so I have a very long way to go. > > > > I wrote this code because I wanted to avoid lots of if statements and > having to maintain a bunch of code like that. Eventually, my idea is to read > in strings from a file, look one up, and use it to execute a function. So I > created the following code. But nothing gets printed to the screen. How do I > go about figuring out why this isn't happening? Here's the code. > > > > myfuncs = [ "func1", > > "func2" ] > > > > def func1(): > > print "func 1" > > > > def func2(): > > print "func 2" > > > > eval (myfuncs[0]) > > > > raw_input("Press enter to quit") > > > > Thanks. > > > > Jim > > Jim Homme, > > Usability Services, > > Phone: 412-544-1810. Skype: jim.homme > > Internal recipients, Read my accessibility blog. > Discuss accessibility here. > Accessibility Wiki: Breaking news and accessibility advice > > > > ------------------------------ > This e-mail and any attachments to it are confidential and are intended > solely for use of the individual or entity to whom they are addressed. If > you have received this e-mail in error, please notify the sender immediately > and then delete it. If you are not the intended recipient, you must not > keep, use, disclose, copy or distribute this e-mail without the author's > prior permission. The views expressed in this e-mail message do not > necessarily represent the views of Highmark Inc., its subsidiaries, or > affiliates. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Vern Ceder vceder at gmail.com, vceder at dogsinmotion.com The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 2 18:57:15 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 2 Dec 2010 17:57:15 -0000 Subject: [Tutor] Trying To Debug Code That Runs Arbitrary Function References: <86F022B554AF0447A870CEE306C14DC6026E9A6E18@EXMB1.highmark.com> Message-ID: "Homme, James" wrote > If you can get away with not telling me the answer, but pointing > me to where to look for the answer, I'd be grateful. OK, Don't use eval or exec they have secuity issues - especially if reading the strings from a file! > idea is to read in strings from a file, look one up, > and use it to execute a function. Functions are objects sop can be stored as data. Dictionariers can associate strings with arbitrary data - including functions. Is that enough of a hint? :-) If not, try reading the OOP topic in my tutorial, under the section heading "Collections of Objects" where I discuss the related scenario of managing collections of objects with arbitrary (or user assigned) names/IDs. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Thu Dec 2 18:52:23 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 2 Dec 2010 09:52:23 -0800 (PST) Subject: [Tutor] Is there any way I can use named tuples in Python 2.5? Message-ID: <21480.42957.qm@web110709.mail.gq1.yahoo.com> Hi, Is there any way I can use named tuples in Python 2.5? It's available since 2.6 and I'm not sure if from "__future__" works. I can't try it here. I cannot simply upgrade Python (alas!) http://www.python.org/doc//current/library/collections.html#collections.namedtuple Cheers!! 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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Thu Dec 2 20:09:34 2010 From: rdmoores at gmail.com (Richard D. Moores) Date: Thu, 2 Dec 2010 11:09:34 -0800 Subject: [Tutor] a print puzzle In-Reply-To: References: Message-ID: On Thu, Dec 2, 2010 at 03:13, Alan Gauld wrote: > > "Richard D. Moores" wrote > >>>>> s = [.1,.1,.1,.1,.1,.1,.1,.1,.1,.1] >>>>> sum(s) >> >> 0.9999999999999999 > > This uses repr() to display the result > >>>>> print(sum(s)) >> >> 1.0 ? ? ?Why? > > This uses str() to display the result. > > In many cases str() calls repr() but in other cases str() is a more user > friendly format. repr() tends to be a developers eye view of things. > This is one reason that in my tutorial I always use print() to display > results. The output is more newbie friendly that way :-) > > I suspect the details will be revealed by a search for documentation > on __repr__ I found nothing that helped as much as you have already. Thanks, Dick From emile at fenx.com Thu Dec 2 20:16:19 2010 From: emile at fenx.com (Emile van Sebille) Date: Thu, 02 Dec 2010 11:16:19 -0800 Subject: [Tutor] Is there any way I can use named tuples in Python 2.5? In-Reply-To: <21480.42957.qm@web110709.mail.gq1.yahoo.com> References: <21480.42957.qm@web110709.mail.gq1.yahoo.com> Message-ID: On 12/2/2010 9:52 AM Albert-Jan Roskam said... > Hi, > > Is there any way I can use named tuples in Python 2.5? It's available since 2.6 > and I'm not sure if from "__future__" works. I can't try it here. > I cannot simply upgrade Python (alas!) Maybe this'll help... http://code.activestate.com/recipes/500261/ Emile From fomcl at yahoo.com Thu Dec 2 20:56:44 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 2 Dec 2010 11:56:44 -0800 (PST) Subject: [Tutor] Is there any way I can use named tuples in Python 2.5? In-Reply-To: References: <21480.42957.qm@web110709.mail.gq1.yahoo.com> Message-ID: <6607.35969.qm@web110706.mail.gq1.yahoo.com> Hi, Thank you! Woaah, very cool code btw. There's much to learn from it, for me at least. Cheers!! 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: Emile van Sebille To: tutor at python.org Sent: Thu, December 2, 2010 8:16:19 PM Subject: Re: [Tutor] Is there any way I can use named tuples in Python 2.5? On 12/2/2010 9:52 AM Albert-Jan Roskam said... > Hi, > > Is there any way I can use named tuples in Python 2.5? It's available since 2.6 > and I'm not sure if from "__future__" works. I can't try it here. > I cannot simply upgrade Python (alas!) Maybe this'll help... http://code.activestate.com/recipes/500261/ Emile _______________________________________________ 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 bgailer at gmail.com Thu Dec 2 21:46:04 2010 From: bgailer at gmail.com (bob gailer) Date: Thu, 02 Dec 2010 15:46:04 -0500 Subject: [Tutor] a print puzzle In-Reply-To: References: Message-ID: <4CF8058C.2020509@gmail.com> On 12/2/2010 2:09 PM, Richard D. Moores wrote: [snip] > > I found nothing that helped as much as you have already. > In the reference (for 2.6.2) under print statement I find: "print evaluates each expression in turn and writes the resulting object to standard output (see below). If an object is not a string, it is first converted to a string using the rules for string conversions. This leaves the reader pondering "rules for string conversions". I wonder whether this is defined and why it was not cross-referenced here. In the reference (for 3.1.3) under print function I find: "All non-keyword arguments are converted to strings like str() does" Under str() I find: "returns its nicely printable representation." OK but still kinda vague. -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Dec 2 22:36:56 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 03 Dec 2010 08:36:56 +1100 Subject: [Tutor] a print puzzle In-Reply-To: <4CF8058C.2020509@gmail.com> References: <4CF8058C.2020509@gmail.com> Message-ID: <4CF81178.7000107@pearwood.info> bob gailer wrote: > On 12/2/2010 2:09 PM, Richard D. Moores wrote: > > [snip] >> >> I found nothing that helped as much as you have already. >> > In the reference (for 2.6.2) under print statement I find: > > "print evaluates each expression in turn and writes the resulting object > to standard output (see below). If an object is not a string, it is > first converted to a string using the rules for string conversions. > > This leaves the reader pondering "rules for string conversions". I > wonder whether this is defined and why it was not cross-referenced here. I'm not sure why there's no cross-reference, or even where it should cross-reference to, but the rules for string conversion are that: if the object has a method __str__, it is called and the result used; if not, but it has a method __repr__, it is called and the result used; if not (only relevant for old-style classes in Python 2.x), then Python has a default string format. -- Steven From ben.ganzfried at gmail.com Thu Dec 2 19:27:44 2010 From: ben.ganzfried at gmail.com (Ben Ganzfried) Date: Thu, 2 Dec 2010 13:27:44 -0500 Subject: [Tutor] Scanning a file for specific text and copying it to a new file Message-ID: I'm trying to build a program that reads in a file and copies specific sections to a new file. More specifically, every time the words "summary on" are in the original file, I want to copy the following text to the new file until I get to the words "summary off". My questions are the following: 1) Once I have read in the old file, how do I copy just the parts that I want to the new file? What command do I give the computer to know that "summary on" (whether capitalized or not) means start writing to the new file-- and "summary off" means stop? Also, I assume I'll put all of this in a while loop based on the condition that we should read until we are done reading the whole document? (although, this is somewhat inefficient because the last "summary on/off" could be way before the end, but this is preferable to missing one that is at the end...) Thank you very much for any help you can provide. Much appreciated! Ben From emile at fenx.com Fri Dec 3 00:25:23 2010 From: emile at fenx.com (Emile van Sebille) Date: Thu, 02 Dec 2010 15:25:23 -0800 Subject: [Tutor] Scanning a file for specific text and copying it to a new file In-Reply-To: References: Message-ID: On 12/2/2010 10:27 AM Ben Ganzfried said... > I'm trying to build a program that reads in a file and copies specific > sections to a new file. More specifically, every time the words > "summary on" are in the original file, I want to copy the following > text to the new file until I get to the words "summary off". > > My questions are the following: > 1) Once I have read in the old file, how do I copy just the parts that > I want to the new file? I'd consider reading the entire file in, then use the resulting string's split method to break out the parts that will need to be written, and finally write those out. If the file you're reading is very large (eg, several hundred MB), or competition is high for memory, you'll want to consider loops. Emile > What command do I give the computer to know > that "summary on" (whether capitalized or not) means start writing to > the new file-- and "summary off" means stop? Also, I assume I'll put > all of this in a while loop based on the condition that we should read > until we are done reading the whole document? (although, this is > somewhat inefficient because the last "summary on/off" could be way > before the end, but this is preferable to missing one that is at the > end...) > > Thank you very much for any help you can provide. Much appreciated! > > Ben From steve at pearwood.info Fri Dec 3 01:03:12 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 03 Dec 2010 11:03:12 +1100 Subject: [Tutor] Scanning a file for specific text and copying it to a new file In-Reply-To: References: Message-ID: <4CF833C0.8020701@pearwood.info> Ben Ganzfried wrote: > I'm trying to build a program that reads in a file and copies specific > sections to a new file. More specifically, every time the words > "summary on" are in the original file, I want to copy the following > text to the new file until I get to the words "summary off". > > My questions are the following: > 1) Once I have read in the old file, how do I copy just the parts that > I want to the new file? What command do I give the computer to know > that "summary on" (whether capitalized or not) means start writing to > the new file-- and "summary off" means stop? There is no such command -- you have to program it yourself. I'll give you an outline: def copy(inname, outname): infile = open(inname, 'r') outfile = open(outname, 'w') copying = False # Don't copy anything yet. for line in infile: if copying: outfile.write(line) infile.close() outfile.close() Note: (1) This is not how you would implement a "real" file utility. There are much faster methods if all you want is to copy the contents in full. But since the next step will be to *process* the data in the file, we need to do it the slow(ish) way. (2) This has a lack of error checking. What happens if you pass the same file name for both arguments? What if the input file doesn't exist, or the output file does? (3) This assumes that the file is a text file, and won't work on arbitrary binary files. The next step is to look for the tag. You don't say whether "summary on" has to be in a line on its own, or if it can be in the middle of a line, or even whether it could be split over two lines. That third case will be tricky, the second case is probably a bad idea, so I'll just assume "summary on" must be in a line on its own: for line in infile: # Ignore leading & trailing whitespace, don't care about case. s = line.strip().lower() if s == "summary on": copying = True # Start copying. continue # But not *this* line, start with the next. elif s == "summary off": copying = False # Stop copying. if copying: outfile.write(line) Note that this allows a single file to have more than one summary section. If this is not the case, you can replace the "Stop copying" line with the command `break` to exist the for-loop early and avoid processing the rest of the file. -- Steven From alan.gauld at btinternet.com Fri Dec 3 01:14:46 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 3 Dec 2010 00:14:46 -0000 Subject: [Tutor] Scanning a file for specific text and copying it to a newfile References: Message-ID: "Ben Ganzfried" wrote > I'm trying to build a program that reads in a file and copies > specific > sections to a new file. More specifically, every time the words > "summary on" are in the original file, I want to copy the following > text to the new file until I get to the words "summary off". This is a very common data processing technique and is often termed setting a "sentinel" value or flag. The generic logic looks like: for line in file: if sentinalFlag is True: if line is sentinel off sentinelFlag = False else process line else if sentinel is in line sentinelFlag = True > 1) Once I have read in the old file, how do I copy just the parts > that > I want to the new file? See above where "process line" is wrting the line to the output file > all of this in a while loop based on the condition that we should > read > until we are done reading the whole document? Since you don't know where the last sentinel off line is you must process the whole file so a for loop is probably more appropriate than a while loop. If the data is short you can read the whole string into memory and tokenize it instead (ie search for the first occurence of on then find the next occurence of off and copy the slice between start and end positions). But if you ever need to process large files then the line by line sentinel approach is more effective and uses a lot less memory. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Fri Dec 3 01:20:24 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 03 Dec 2010 11:20:24 +1100 Subject: [Tutor] data structures In-Reply-To: <4CF73788.1050005@googlemail.com> References: <4CF6FB98.5010304@charter.net> <4CF73788.1050005@googlemail.com> Message-ID: <4CF837C8.5080809@pearwood.info> Knacktus wrote: > Am 02.12.2010 02:51, schrieb Dana: >> Hello, >> >> I'm using Python to extract words from plain text files. I have a list >> of words. Now I would like to convert that list to a dictionary of >> features where the key is the word and the value the number of >> occurrences in a group of files based on the filename (different files >> correspond to different categories). What is the best way to represent >> this data? When I finish I expect to have about 70 unique dictionaries >> with values I plan to use in frequency distributions, etc. Should I use >> globally defined dictionaries? > Depends on what else you want to do with the group of files. If you're > expecting some operations on the group's data you should create a class > to be able to add some more methods to the data. I would probably go > with a class. Unless you're planning to have multiple "file groups" at once, or intending to re-use this code for other modules, using a class is probably overkill. This isn't Java where everything has to be a class :) > But also I think dictionaries can be fine. If you really only need the > dicts. You could create a function to create those. Agreed. One way or the other, a dict {word: count} is the natural data structure to use for a concordance. Whether you store those dicts in a class, or just operate directly on the dicts, is relatively unimportant. -- Steven From carroll at tjc.com Fri Dec 3 01:26:58 2010 From: carroll at tjc.com (Terry Carroll) Date: Thu, 2 Dec 2010 16:26:58 -0800 (PST) Subject: [Tutor] Pyserial and invalid handle In-Reply-To: References: <4CF27B67.8000803@verizon.net> <4CF41EB6.3000802@verizon.net> <4CF52D15.7030806@verizon.net> Message-ID: On Wed, 1 Dec 2010, Walter Prins wrote: > But whatever the case may be, suffice it to say I've reproduced your issue > on my Win7 64bit box, and then resolved it by installing the PyWin32 > modules. I'd like to put in a plug for Activestate Python here. Activestate has a free distribution of Python for Windows that not only includes the basic Python program and libraries, but also commonly used extras including the Win32 modules. I've always used Activestate Python from the start, and recommend it. They have distributions for both Python 2 and 3, each both in 32-bit and 64-bit. http://www.activestate.com/activepython/downloads I have no association with Activestate, I'm just a satisfied customer. Well, not even a customer. I'm a satisfied freeloader. From steve at pearwood.info Fri Dec 3 01:32:59 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 03 Dec 2010 11:32:59 +1100 Subject: [Tutor] permutations? In-Reply-To: References: <4CF6D643.6000501@gmail.com> Message-ID: <4CF83ABB.20209@pearwood.info> Alan Gauld wrote: > > "Alex Hall" wrote > >> Alright, I have it working. Now the problem is that it does not throw >> out reversals. I tried to do this myself with a couple loops, but I >> get index errors. My total list of permutations is called l. >> >> for i in range(0, len(l)): >> r=l[i]; r.reverse() > > You don''t need to specify the 0, its the default. > But you don't need to use indexing either. > > for r,i in enumerate(l): r.reverse() > >> for j in range(0, len(l)): >> print l[j], r, i, j > > Here you are comparing the reversed item to every item in the > list, including the ones you have already reversed, including the > item itself. So you will always find a match when i==j... > > Using the in operator and list slicing would be easier > > if r in l[i:]: l.remove(r) > > but... > >> if r==l[j]: l.remove(r) > > Its never a good idea to remove items from a list you are iterating > over, its like the old cartoon of the guy cutting down the tree branch > he is sitting on. You can cut off the branch you're sitting on so long as you remember to cut on the correct side :) Back in Ancient Days when dinosaurs walked the earth, and I programmed in Pascal, computers didn't have much memory, and were slow. Consequently it wasn't practical to make a copy of a list if you wanted to delete a few items. The only practical way to modify lists was to modify them in place, and if you needed to delete items, you had to work backwards. It took me a while to break myself of the habit of doing this: for i in range(len(mylist)-1, -1, -1): if mylist[i] == "something": del mylist[i] (Note that you only need to work backwards if you're *deleting* entries, not if you replace them with something else.) This is still a useful technique to have in your toolbox, but generally speaking the above is better written as: mylist = [x for x in mylist if x != "something"] If you really need to modify the list in place, and not just re-bind the name "mylist" to the new list, then one tiny change will do it: mylist[:] = [x for x in mylist if x != "something"] -- Steven From ranceh at gmail.com Fri Dec 3 05:30:24 2010 From: ranceh at gmail.com (Rance Hall) Date: Thu, 2 Dec 2010 22:30:24 -0600 Subject: [Tutor] printing in python 3.x Message-ID: I've been working on this cli based python 3.x app for a friends shop. So far, everything is working well. We are now ready to start development on a new module of my code. The shop is a repair shop, and I've already done the code for client management and employee management and all the framework and supporting stuff, but now its time to check in broken items for repair and print a reciept for the customer and a shop ticket for the item. shop tickets are easy to do *we think*, we think all we need to do is create a "template" text file, and open that text file, sub markers for real data, and create a temp file then send that to the printer with the functions of the underlying OS. In my tests on windows this worked fine. But shop owner wants to do something nicer with the customer receipts. He is talking shop logos and pdf files. I have several issues. Google keeps bringing up ReportLib and the python imaging library, but neither have a 3.x compatible version yet. Shop owner is talking about eventually being able to save customer check in tickets and check out tickets on a web site where clients can log in and check on their work. My thought is that this is perfect for PDF files, but they aren't available on 3.x yet. What other options do I have? I looked at word integration and openoffice integration and they dont look ready for 3.x yet either. From alan.gauld at btinternet.com Fri Dec 3 09:13:43 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 3 Dec 2010 08:13:43 -0000 Subject: [Tutor] printing in python 3.x References: Message-ID: "Rance Hall" wrote > > But shop owner wants to do something nicer with the customer > receipts. > He is talking shop logos and pdf files. The simplest technique is probably to generate an HTML file and then get the OS to print that for you via the default browser. > Google keeps bringing up ReportLib and the python imaging library, > but > neither have a 3.x compatible version yet. THats much harder work IMHO than using an HTML file as a template. > Shop owner is talking about eventually being able to save customer > check in tickets and check out tickets on a web site where clients > can > log in and check on their work. And HTML woyuld definitely be easier here! > My thought is that this is perfect for PDF files, but they aren't > available on 3.x yet. Well you could hand crank them, they are just text files after all. But HTML is much easier to generate by hand! > I looked at word integration and openoffice integration and they > dont > look ready for 3.x yet either. The Pywin32 and ctypes modules all work on Python 3 so you could use COM integration, or even WSH. But HTML stioll gets my vote. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Fri Dec 3 11:19:02 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 3 Dec 2010 02:19:02 -0800 (PST) Subject: [Tutor] Question on tkinter event binding Message-ID: <565024.32044.qm@web110702.mail.gq1.yahoo.com> Hi, I'm trying to make a small improvement on a data entry program and it is literally giving me a headache. I would appreciate your help or suggestions. The actual program uses Autocomplete entry widgets [1], which is a subclass of the Tkinter Entry widget. The sample code below uses a simple Entry widget, for the sake of simplicity. The autocompletions are recorded in a dictionary of the form {entry name: set()}. The problem is that entries with typos cannot be deleted, so wrong autocomplete suggestions ("Bbbilly Gates") are given until the end of time. My solution: I want to bind each entry widget to the Delete key, which makes it possible to remove the typo-entry from the set of entries. I am using an ' expanded event handler' [2] to do the event binding. The sample code below creates two entry widgets. The problem is that the entry contents is not retrieved correctly. If I fill the 2nd entry with some text, then hit 'Del' , it shows the content of the *first* entry. Also, I would like to isolate the event handler into its own function, not as a function within a function, but I'm not quite sure how. [1] http://tkinter.unpythonic.net/wiki/AutocompleteEntry [2] http://www.daniweb.com/code/snippet306072.html from Tkinter import * def createWidgets(veldnamen): root=Tk() termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]), "*Postcode": set(["2600AA", "8000NN"])} handleDels = {} for veldnaam in veldnamen: # tcl names must start with lowercase letter entryWidget=Entry(root, name=veldnaam[0].lower() + veldnaam[1:]) entryWidget.grid() def handleDel(event, widget=entryWidget, root=root, termenlijst=termenlijst): vensternaam = str(root.focus_get())[1:].capitalize() # ... and back to uppercase if vensternaam.startswith("*"): # mandatory fields start with '*' in my program. vensternaam = "*" + vensternaam[1:].capitalize() vensterinhoud = entryWidget.get() print "Naam", vensternaam # entry name print "Inhoud", vensterinhoud # entry contents try: termenlijst[vensternaam].remove(vensterinhoud) # here's where the typo is removed except KeyError: pass # user tries to delete a term that doesn't exist in the termenlijst. handleDels[entryWidget] = handleDel # do all the bindings (is this where it goes wrong??) for entryWidget, handleDel in handleDels.iteritems(): entryWidget.bind("", handleDel) print handleDels print termenlijst createWidgets(["Naam", "*Postcode"]) Thanks again for having a look at this. Cheers!! 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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -------------- next part -------------- An HTML attachment was scrubbed... URL: From eddie9139 at gmail.com Fri Dec 3 11:26:34 2010 From: eddie9139 at gmail.com (Eddie) Date: Fri, 3 Dec 2010 21:26:34 +1100 Subject: [Tutor] Finally finished my currency exchange program Message-ID: I've finally finished my currency exchange program thanks to help from this list and another list. It updates the exchange rates via an xml feed etc. Feedback on it would be appreciated please: http://www.dinksoftware.com/astute_currency_analyzer.php Dink -------------- next part -------------- An HTML attachment was scrubbed... URL: From danielle.davout at gmail.com Fri Dec 3 12:51:31 2010 From: danielle.davout at gmail.com (danielle davout) Date: Fri, 3 Dec 2010 18:51:31 +0700 Subject: [Tutor] Finally finished my currency exchange program In-Reply-To: References: Message-ID: sadly you didn't take into account Linux users On 12/3/10, Eddie wrote: > I've finally finished my currency exchange program thanks to help from this > list and another list. It updates the exchange rates via an xml feed etc. > > Feedback on it would be appreciated please: > http://www.dinksoftware.com/astute_currency_analyzer.php > > Dink > From alan.gauld at btinternet.com Fri Dec 3 14:07:34 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 3 Dec 2010 13:07:34 -0000 Subject: [Tutor] Finally finished my currency exchange program References: Message-ID: "Eddie" wrote in message news:AANLkTimXrAJnBvj9eGx4DiQBWrb1LtFa9quv2fDeqNpf at mail.gmail.com... > I've finally finished my currency exchange program thanks to help > from this > list and another list. It updates the exchange rates via an xml > feed etc. If you wrote it in Python why only Windows support? Virtually all Python GUI toolkits are portable across platforms, one of the biggest advantages of Python... And the core functions are surely just basic Python code? Seems like a missed opportunity. Alan G. From joel.goldstick at gmail.com Fri Dec 3 15:21:11 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 3 Dec 2010 09:21:11 -0500 Subject: [Tutor] Finally finished my currency exchange program In-Reply-To: References: Message-ID: On Fri, Dec 3, 2010 at 8:07 AM, Alan Gauld wrote: > > "Eddie" wrote in message > news:AANLkTimXrAJnBvj9eGx4DiQBWrb1LtFa9quv2fDeqNpf at mail.gmail.com... > > I've finally finished my currency exchange program thanks to help from >> this >> list and another list. It updates the exchange rates via an xml feed etc. >> > > If you wrote it in Python why only Windows support? > > Virtually all Python GUI toolkits are portable across platforms, one of > the biggest advantages of Python... And the core functions are surely > just basic Python code? > > Seems like a missed opportunity. > > Alan G. > > Since this is a python tutor group, I figured you might have had earlier discussions here about the coding of this project. But I didn't find anything. hmm? Why did you decide to make this a desktop application, and not a website application? I'm guessing that you may have published it for Windows only because of issues around the installation process. I haven't tried creating any sort of install packages in Windows, or Linux, so I am completely in the dark about those things. But, by putting the application on the web, install issues disappear, and you might get more hits on your site. -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From onyxtic at gmail.com Fri Dec 3 15:54:13 2010 From: onyxtic at gmail.com (Evans Anyokwu) Date: Fri, 3 Dec 2010 14:54:13 +0000 Subject: [Tutor] Finally finished my currency exchange program In-Reply-To: References: Message-ID: I didn't find any of his previous questions as well - Looks like he just registered on this list to try and promote his product. Sneaky! On Fri, Dec 3, 2010 at 2:21 PM, Joel Goldstick wrote: > > On Fri, Dec 3, 2010 at 8:07 AM, Alan Gauld wrote: > >> >> "Eddie" wrote in message >> news:AANLkTimXrAJnBvj9eGx4DiQBWrb1LtFa9quv2fDeqNpf at mail.gmail.com... >> >> I've finally finished my currency exchange program thanks to help from >>> this >>> list and another list. It updates the exchange rates via an xml feed >>> etc. >>> >> >> If you wrote it in Python why only Windows support? >> >> Virtually all Python GUI toolkits are portable across platforms, one of >> the biggest advantages of Python... And the core functions are surely >> just basic Python code? >> >> Seems like a missed opportunity. >> >> Alan G. >> >> Since this is a python tutor group, I figured you might have had earlier > discussions here about the coding of this project. But I didn't find > anything. hmm? > > Why did you decide to make this a desktop application, and not a website > application? I'm guessing that you may have published it for Windows only > because of issues around the installation process. I haven't tried creating > any sort of install packages in Windows, or Linux, so I am completely in the > dark about those things. But, by putting the application on the web, > install issues disappear, and you might get more hits on your site. > > > -- > Joel Goldstick > > > _______________________________________________ > 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 mehgcap at gmail.com Fri Dec 3 19:11:04 2010 From: mehgcap at gmail.com (Alex Hall) Date: Fri, 3 Dec 2010 13:11:04 -0500 Subject: [Tutor] permutations? In-Reply-To: <4CF83ABB.20209@pearwood.info> References: <4CF6D643.6000501@gmail.com> <4CF83ABB.20209@pearwood.info> Message-ID: Thanks, everyone! While I do not understand the function provided a few messages back, it works perfectly. The rest of the responses were very interesting. I need to try, yet again, to really understand list comprehensions, possibly the most difficult topic I have yet come across, along with the colon when used in lists. Thanks again to all! On 12/2/10, Steven D'Aprano wrote: > Alan Gauld wrote: >> >> "Alex Hall" wrote >> >>> Alright, I have it working. Now the problem is that it does not throw >>> out reversals. I tried to do this myself with a couple loops, but I >>> get index errors. My total list of permutations is called l. >>> >>> for i in range(0, len(l)): >>> r=l[i]; r.reverse() >> >> You don''t need to specify the 0, its the default. >> But you don't need to use indexing either. >> >> for r,i in enumerate(l): r.reverse() >> >>> for j in range(0, len(l)): >>> print l[j], r, i, j >> >> Here you are comparing the reversed item to every item in the >> list, including the ones you have already reversed, including the >> item itself. So you will always find a match when i==j... >> >> Using the in operator and list slicing would be easier >> >> if r in l[i:]: l.remove(r) >> >> but... >> >>> if r==l[j]: l.remove(r) >> >> Its never a good idea to remove items from a list you are iterating >> over, its like the old cartoon of the guy cutting down the tree branch >> he is sitting on. > > You can cut off the branch you're sitting on so long as you remember to > cut on the correct side :) > > Back in Ancient Days when dinosaurs walked the earth, and I programmed > in Pascal, computers didn't have much memory, and were slow. > Consequently it wasn't practical to make a copy of a list if you wanted > to delete a few items. The only practical way to modify lists was to > modify them in place, and if you needed to delete items, you had to work > backwards. It took me a while to break myself of the habit of doing this: > > for i in range(len(mylist)-1, -1, -1): > if mylist[i] == "something": > del mylist[i] > > (Note that you only need to work backwards if you're *deleting* entries, > not if you replace them with something else.) > > This is still a useful technique to have in your toolbox, but generally > speaking the above is better written as: > > mylist = [x for x in mylist if x != "something"] > > > If you really need to modify the list in place, and not just re-bind the > name "mylist" to the new list, then one tiny change will do it: > > mylist[:] = [x for x in mylist if x != "something"] > > > > -- > Steven > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From cspears2002 at yahoo.com Fri Dec 3 19:52:24 2010 From: cspears2002 at yahoo.com (Christopher Spears) Date: Fri, 3 Dec 2010 10:52:24 -0800 (PST) Subject: [Tutor] floats Message-ID: <679464.43786.qm@web51604.mail.re2.yahoo.com> I have a float variable that is very long. >>> float_a = 1.16667 However, I want to pass the value of float_a to float_b, but I want the float to be accurate to two decimal points. >>> float_a = 1.16667 >>> print "%.2f" % float_a 1.17 I tried the following: >>> float_b = "%.2f" % float_a >>> float_b '1.17' >>> type(float_b) This doesn't work because it yields a string. Any suggestions? From nitinpawar432 at gmail.com Fri Dec 3 19:56:13 2010 From: nitinpawar432 at gmail.com (Nitin Pawar) Date: Sat, 4 Dec 2010 00:26:13 +0530 Subject: [Tutor] floats In-Reply-To: <679464.43786.qm@web51604.mail.re2.yahoo.com> References: <679464.43786.qm@web51604.mail.re2.yahoo.com> Message-ID: you have two options 1) either type cast like float_b = float(etcetc) 2) use round method which available inbuilt On Sat, Dec 4, 2010 at 12:22 AM, Christopher Spears wrote: > > I have a float variable that is very long. > > >>> float_a = 1.16667 > > However, I want to pass the value of float_a to float_b, but I want the > float to be accurate to two decimal points. > > >>> float_a = 1.16667 > >>> print "%.2f" % float_a > 1.17 > > I tried the following: > > >>> float_b = "%.2f" % float_a > >>> float_b > '1.17' > >>> type(float_b) > > > This doesn't work because it yields a string. > > Any suggestions? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Nitin Pawar -------------- next part -------------- An HTML attachment was scrubbed... URL: From kb1pkl at aim.com Fri Dec 3 19:58:20 2010 From: kb1pkl at aim.com (Corey Richardson) Date: Fri, 03 Dec 2010 13:58:20 -0500 Subject: [Tutor] floats In-Reply-To: <679464.43786.qm@web51604.mail.re2.yahoo.com> References: <679464.43786.qm@web51604.mail.re2.yahoo.com> Message-ID: <4CF93DCC.9040707@aim.com> On 12/3/2010 1:52 PM, Christopher Spears wrote: > I have a float variable that is very long. > >>>> float_a = 1.16667 > However, I want to pass the value of float_a to float_b, but I want the float to be accurate to two decimal points. > >>>> float_a = 1.16667 >>>> print "%.2f" % float_a > 1.17 > > I tried the following: > >>>> float_b = "%.2f" % float_a >>>> float_b > '1.17' >>>> type(float_b) > > > This doesn't work because it yields a string. > > Any suggestions? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > float_b = float(float_b) That takes the string and makes it a float. ~Corey Richardson From emile at fenx.com Fri Dec 3 20:01:19 2010 From: emile at fenx.com (Emile van Sebille) Date: Fri, 03 Dec 2010 11:01:19 -0800 Subject: [Tutor] floats In-Reply-To: <679464.43786.qm@web51604.mail.re2.yahoo.com> References: <679464.43786.qm@web51604.mail.re2.yahoo.com> Message-ID: On 12/3/2010 10:52 AM Christopher Spears said... > > I have a float variable that is very long. > >>>> float_a = 1.16667 > > However, I want to pass the value of float_a to float_b, but I want the float to be accurate to two decimal points. > >>>> float_a = 1.16667 >>>> print "%.2f" % float_a > 1.17 > > I tried the following: > >>>> float_b = "%.2f" % float_a >>>> float_b > '1.17' >>>> type(float_b) > > > This doesn't work because it yields a string. > > Any suggestions? If you want control over the precision I'd use the decimal module. >>> x 1.1666666699999999 >>> y = decimal.Decimal("%.2f" % x) >>> y Decimal('1.17') >>> Emile From malaclypse2 at gmail.com Fri Dec 3 20:02:08 2010 From: malaclypse2 at gmail.com (Jerry Hill) Date: Fri, 3 Dec 2010 14:02:08 -0500 Subject: [Tutor] floats In-Reply-To: <679464.43786.qm@web51604.mail.re2.yahoo.com> References: <679464.43786.qm@web51604.mail.re2.yahoo.com> Message-ID: On Fri, Dec 3, 2010 at 1:52 PM, Christopher Spears wrote: >>>> float_a = 1.16667 > > However, I want to pass the value of float_a to float_b, but I want the float to be accurate to two decimal points. Use the built-in round() function, like this: >>> a = 1.16667 >>> print a 1.16667 >>> b = round(a, 2) >>> print b 1.17 -- Jerry From alan.gauld at btinternet.com Fri Dec 3 20:15:29 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 3 Dec 2010 19:15:29 -0000 Subject: [Tutor] floats References: <679464.43786.qm@web51604.mail.re2.yahoo.com> Message-ID: "Christopher Spears" wrote > I have a float variable that is very long. > >>>> float_a = 1.16667 Thats not really very long! > However, I want to pass the value of float_a to float_b, > but I want the float to be accurate to two decimal points. > >>>> float_a = 1.16667 >>>> print "%.2f" % float_a > 1.17 Rounding issues will give you all sorts of issues here. However if you really want to round it up or down to 2 digits then the round() function is what you want. But I'd be interested in why you need to lose precision like that, it's not that common a requirement. Usually controlling the presentation is sufficient (and preferred). >>>> float_b = "%.2f" % float_a >>>> float_b > '1.17' >>>> type(float_b) > > > This doesn't work because it yields a string. It works in that it returns a string representation of what you want. It doesn't produce a float because *string formatting* can only produce strings. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From joel.goldstick at gmail.com Fri Dec 3 20:18:56 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 3 Dec 2010 14:18:56 -0500 Subject: [Tutor] floats In-Reply-To: <679464.43786.qm@web51604.mail.re2.yahoo.com> References: <679464.43786.qm@web51604.mail.re2.yahoo.com> Message-ID: On Fri, Dec 3, 2010 at 1:52 PM, Christopher Spears wrote: > > I have a float variable that is very long. > > >>> float_a = 1.16667 > > However, I want to pass the value of float_a to float_b, but I want the > float to be accurate to two decimal points. > > >>> float_a = 1.16667 > >>> print "%.2f" % float_a > 1.17 > > I tried the following: > > >>> float_b = "%.2f" % float_a > >>> float_b > '1.17' > >>> type(float_b) > > > This doesn't work because it yields a string. > > Any suggestions? > This would be good to read: http://docs.python.org/tutorial/floatingpoint.html If you have: float_a = 1.66667 and do round(float_a, 2) you get: round(1.16667, 2) 1.1699999999999999 I don't think that is what you want, but since floating point arithmetic is done in binary there are rounding errors. You might want to look at the decimal module > _______________________________________________ > 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 fomcl at yahoo.com Fri Dec 3 21:18:40 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 3 Dec 2010 12:18:40 -0800 (PST) Subject: [Tutor] Question on tkinter event binding In-Reply-To: <565024.32044.qm@web110702.mail.gq1.yahoo.com> References: <565024.32044.qm@web110702.mail.gq1.yahoo.com> Message-ID: <508713.11256.qm@web110705.mail.gq1.yahoo.com> Aaahhh, got it! Peace! I did two things wrong: (1) I didn't use a tcl StringVar() to get the entry widget contents (2) I didn't consistently close the menus generated by previous attempts to run the program, which led to inconsistent results. I'll paste the working code below. It's partially in Dutch, but hey, so is Guido van Rossem. ;-) Even so, I'd be happy to hear suggestions for improvement or simplification. I'd love to chop the code up into smaller, more comprehensible bits. from Tkinter import * def createWidgets(veldnamen): root=Tk() termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]), "*Postcode": set(["2600AA", "8000BB"]), "Adres": set(["Street", "Avenue"])} handleDeletions = {} for veldnaam in veldnamen: labelWidget=Label(root, text=veldnaam, takefocus=False) labelWidget.grid() # tcl names must start with a lowercase letter tclName = veldnaam[0].lower() + veldnaam[1:] content = StringVar() entryWidget=Entry(root, name=tclName, textvariable=content) entryWidget.grid() def handleDeletion(event, widget=entryWidget, root=root, termenlijst=termenlijst,content=content): actieveVenster = root.focus_get() actieveVensternaam = str(actieveVenster)[1:].capitalize() if actieveVensternaam.startswith("*"): actieveVensternaam = "*" + actieveVensternaam[1:].capitalize() vensterinhoud = content.get().strip() print "Name: %s -- Contents: %s" % (actieveVensternaam, vensterinhoud) try: termenlijst[actieveVensternaam].remove(vensterinhoud) actieveVenster.delete(0, END) print "Deleted term '%s'" % vensterinhoud except KeyError: print "No such term '%s'" % vensterinhoud pass handleDeletions[entryWidget] = handleDeletion for entryWidget, handleDeletion in handleDeletions.iteritems(): entryWidget.bind("", handleDeletion) createWidgets(["Naam", "*Postcode", "Adres"]) Cheers!! 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: Albert-Jan Roskam To: Python Mailing List Sent: Fri, December 3, 2010 11:19:02 AM Subject: [Tutor] Question on tkinter event binding Hi, I'm trying to make a small improvement on a data entry program and it is literally giving me a headache. I would appreciate your help or suggestions. The actual program uses Autocomplete entry widgets [1], which is a subclass of the Tkinter Entry widget. The sample code below uses a simple Entry widget, for the sake of simplicity. The autocompletions are recorded in a dictionary of the form {entry name: set()}. The problem is that entries with typos cannot be deleted, so wrong autocomplete suggestions ("Bbbilly Gates") are given until the end of time. My solution: I want to bind each entry widget to the Delete key, which makes it possible to remove the typo-entry from the set of entries. I am using an ' expanded event handler' [2] to do the event binding. The sample code below creates two entry widgets. The problem is that the entry contents is not retrieved correctly. If I fill the 2nd entry with some text, then hit 'Del' , it shows the content of the *first* entry. Also, I would like to isolate the event handler into its own function, not as a function within a function, but I'm not quite sure how. [1] http://tkinter.unpythonic.net/wiki/AutocompleteEntry [2] http://www.daniweb.com/code/snippet306072.html from Tkinter import * def createWidgets(veldnamen): root=Tk() termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]), "*Postcode": set(["2600AA", "8000NN"])} handleDels = {} for veldnaam in veldnamen: # tcl names must start with lowercase letter entryWidget=Entry(root, name=veldnaam[0].lower() + veldnaam[1:]) entryWidget.grid() def handleDel(event, widget=entryWidget, root=root, termenlijst=termenlijst): vensternaam = str(root.focus_get())[1:].capitalize() # ... and back to uppercase if vensternaam.startswith("*"): # mandatory fields start with '*' in my program. vensternaam = "*" + vensternaam[1:].capitalize() vensterinhoud = entryWidget.get() print "Naam", vensternaam # entry name print "Inhoud", vensterinhoud # entry contents try: termenlijst[vensternaam].remove(vensterinhoud) # here's where the typo is removed except KeyError: pass # user tries to delete a term that doesn't exist in the termenlijst. handleDels[entryWidget] = handleDel # do all the bindings (is this where it goes wrong??) for entryWidget, handleDel in handleDels.iteritems(): entryWidget.bind("", handleDel) print handleDels print termenlijst createWidgets(["Naam", "*Postcode"]) Thanks again for having a look at this. Cheers!! 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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -------------- next part -------------- An HTML attachment was scrubbed... URL: From patty at cruzio.com Fri Dec 3 23:39:28 2010 From: patty at cruzio.com (Patty) Date: Fri, 3 Dec 2010 14:39:28 -0800 Subject: [Tutor] Question on tkinter event binding References: <565024.32044.qm@web110702.mail.gq1.yahoo.com> <508713.11256.qm@web110705.mail.gq1.yahoo.com> Message-ID: <614AAA2090C841188E44C23ED34C3081@mycomputer> Hello Albert-Jan: I am glad you made the comment below. I was fascinated with the fact that your code was partly in English/Python and also in Dutch. I am a linguist so have great interest in bilingualism. How does this work in practice? I mean as a programmer, with native language other than English, do you download or buy English language software programs and work with them as-is? Do you have translated tutorials to help you learn? If you had a Dutch language software program and created your own program so that everything is totally in Dutch, and you wanted to communicate with English language email group :) How would you do that? Or would you try and find a Dutch language resource? Besides that, I am definitely saving your code segments for the future. Thanks for sharing. Patty ----- Original Message ----- From: Albert-Jan Roskam To: Albert-Jan Roskam ; Python Mailing List Sent: Friday, December 03, 2010 12:18 PM Subject: Re: [Tutor] Question on tkinter event binding I'll paste the working code below. It's partially in Dutch, but hey, so is Guido van Rossem. ;-) -------------- next part -------------- An HTML attachment was scrubbed... URL: From carroll at tjc.com Sat Dec 4 00:53:40 2010 From: carroll at tjc.com (Terry Carroll) Date: Fri, 3 Dec 2010 15:53:40 -0800 (PST) Subject: [Tutor] Question on tkinter event binding In-Reply-To: <508713.11256.qm@web110705.mail.gq1.yahoo.com> References: <565024.32044.qm@web110702.mail.gq1.yahoo.com> <508713.11256.qm@web110705.mail.gq1.yahoo.com> Message-ID: On Fri, 3 Dec 2010, Albert-Jan Roskam wrote: > I'm trying to make a small improvement on a data entry program and it is > literally giving me a headache. Followed shortly thereafter with: On Fri, 3 Dec 2010, Albert-Jan Roskam wrote: > Aaahhh, got it! Peace! ... I'll paste the working code below. It's > partially in Dutch, but hey, so is Guido van Rossem. ;-) Once again proving the Zen of Python: >>> import this The Zen of Python, by Tim Peters . . . There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. From ilhs_hs at yahoo.com Sat Dec 4 01:27:59 2010 From: ilhs_hs at yahoo.com (Hs Hs) Date: Fri, 3 Dec 2010 16:27:59 -0800 (PST) Subject: [Tutor] print problem Message-ID: <581989.29689.qm@web111207.mail.gq1.yahoo.com> hi I have a file and it is in chunks: I want to be able to select the number that follows 'Elution: ' where the line startswith (TITLE)(say 72.958) and check if it is larger than my choice of number (say 71.4). If it is then I want to print chunk from BEGIN LINE to END LINE separated by one empty line. I wrote the following script, when executed in IDEL or python mode, it works. however I use python test_script_test.py , I get name error: could you please help me whats wrong here. thanks Command line mode: $ python test_script_test.py Traceback (most recent call last): File "test_script_test.py", line 14, in newk = dat[mystartl:myfinish] NameError: name 'myfinish' is not defined In python mode: >>> f1 = open('test','r') >>> da = f1.read().split('\n') >>> dat = da[:-1] >>> mytimep = 71.4 >>> for i in range(len(dat)): ... if dat[i].startswith('BEGIN LINE'): ... mystartl = i ... if dat[i].startswith('END LINE'): ... myfinish = i ... if dat[i].startswith('Title'): ... col = dat[i].split(',') ... x= float(col[2].split()[1]) ... if x > mytimep: ... newk = dat[mystartl:myfinish] ... for x in newk: ... print x ... BEGIN LINE MYMASS=643.973587067287 CHARGE=2+ Title=File:xyx, sample= Cyst 7 band 7 (sample number 8), Elution: 72.958 to 73.198 min, Period: 1 132.4082 0.0323 133.0418 0.2596 136.0618 0.3276 138.0482 0.546 140.4848 0.0499 140.7096 0.0333 END LINE BEGIN LINE MYMASS=643.973587067287 CHARGE=2+ Title=File:xyx, sample= Cyst 7 band 7 (sample number 8), Elution: 72.958 to 73.198 min, Period: 1 132.4082 0.0323 133.0418 0.2596 136.0618 0.3276 138.0482 0.546 140.4848 0.0499 140.7096 0.0333 my file: BEGIN LINE MYMASS=643.973587067287 CHARGE=2+ Title=File:xyx, sample= Cyst 7 band 7 (sample number 8), Elution: 72.958 to 73.198 min, Period: 1 132.4082 0.0323 133.0418 0.2596 136.0618 0.3276 138.0482 0.546 140.4848 0.0499 140.7096 0.0333 END LINE BEGIN LINE MYMASS=643.973587067287 CHARGE=2+ Title=File:xyx, sample= Cyst 7 band 7 (sample number 8), Elution: 72.958 to 73.198 min, Period: 1 132.4082 0.0323 133.0418 0.2596 136.0618 0.3276 138.0482 0.546 140.4848 0.0499 140.7096 0.0333 END LINE -------------- next part -------------- An HTML attachment was scrubbed... URL: From eddie9139 at gmail.com Sat Dec 4 01:45:44 2010 From: eddie9139 at gmail.com (Eddie) Date: Sat, 4 Dec 2010 11:45:44 +1100 Subject: [Tutor] Finally finished my currency exchange program In-Reply-To: References: Message-ID: Its Windows only as that is what I am more comfortable with and I don't use Linux (tried and not had much luck with it really). Yes, the installer is Windows only. Also, No, I didn't just register on thsi list, I've been registered on this list for years. Dink On 4 December 2010 01:54, Evans Anyokwu wrote: > I didn't find any of his previous questions as well - > Looks like he just registered on this list to try and promote his product. > Sneaky! > > > > On Fri, Dec 3, 2010 at 2:21 PM, Joel Goldstick wrote: > >> >> On Fri, Dec 3, 2010 at 8:07 AM, Alan Gauld wrote: >> >>> >>> "Eddie" wrote in message >>> news:AANLkTimXrAJnBvj9eGx4DiQBWrb1LtFa9quv2fDeqNpf at mail.gmail.com... >>> >>> I've finally finished my currency exchange program thanks to help from >>>> this >>>> list and another list. It updates the exchange rates via an xml feed >>>> etc. >>>> >>> >>> If you wrote it in Python why only Windows support? >>> >>> Virtually all Python GUI toolkits are portable across platforms, one of >>> the biggest advantages of Python... And the core functions are surely >>> just basic Python code? >>> >>> Seems like a missed opportunity. >>> >>> Alan G. >>> >>> Since this is a python tutor group, I figured you might have had earlier >> discussions here about the coding of this project. But I didn't find >> anything. hmm? >> >> Why did you decide to make this a desktop application, and not a website >> application? I'm guessing that you may have published it for Windows only >> because of issues around the installation process. I haven't tried creating >> any sort of install packages in Windows, or Linux, so I am completely in the >> dark about those things. But, by putting the application on the web, >> install issues disappear, and you might get more hits on your site. >> >> >> -- >> Joel Goldstick >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > > _______________________________________________ > 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 wprins at gmail.com Sat Dec 4 02:42:23 2010 From: wprins at gmail.com (Walter Prins) Date: Sat, 4 Dec 2010 01:42:23 +0000 Subject: [Tutor] print problem In-Reply-To: <581989.29689.qm@web111207.mail.gq1.yahoo.com> References: <581989.29689.qm@web111207.mail.gq1.yahoo.com> Message-ID: Please post the exact contents of "test_script_test.py", don't paraphrase, don't abbreviate. A good place to post code snippets are here: http://pastebin.com/ That said, I'll try to put down some of the troubleshooting thought process you should be having. Firstly, the error message is telling you that on line 14 of your module on the line saying "newk = dat[mystartl:myfinish]", that "mystartl" is not defined. So then you should be asking yourself "how can it happen that the program can get to this point and 'mystartl' not be set?". Which should lead on to "OK, let's see where mystart is in fact being set, and see if there's conditions attached to that which may explain the behaviour". Which should lead to you looking back in the code and spotting the lines that says: ... if dat[i].startswith('BEGIN LINE'): ... mystartl = i OK, so: "mystartl" is *only* set if dat[i].startswith('BEGIN LINE') is false. So then the implication would be that dat[i] doesn't start with 'BEGIN LINE' when run from the prompt, so the next question would be why that is? The implication would be that it wasn't read from the file. So then implication is that the file "test" that your script reads is not what you think it is when you run the file from the prompt. HTH, Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From georgeandsnickerdoodle at gmail.com Sat Dec 4 02:47:16 2010 From: georgeandsnickerdoodle at gmail.com (Ashley Blackwell) Date: Fri, 3 Dec 2010 20:47:16 -0500 Subject: [Tutor] Newline Message-ID: Hello everybody, I'm new to the mailing list so I'm pretty sure I'll have lots of questions:) It's a very basic question I have and everybody might look at this question and say, "Wow, she reallly doesn't get it?" But oh well. Here's my question: I'm in Chapter 2 of my Python Programming Third Editition book. I've gotten to the section of the chapter where it talks about a "newline character. Exactly what is a newline character? I keep reading the same section of the book and I'm just not really understanding. Here's the code that the book uses to explain the newline character: print("Here", end=" ") print("it is...") The book says that I can specify that a space be the final character printed instead of a newline. In the first print() statement I specify that a space be the final string printed. So the text "Here" appears but no newline character. The next print() statement prints the text "it is"..." right after the space following the final "e" in the "Here" text. You do it by passing a space to the end parameter of the print() function with the code end" ". My Operating System is Windows 7 and the Python version is 3.1.3. That's it. Thanks to anybody who responds to my question. -- *Ava Durand* -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Sat Dec 4 03:02:32 2010 From: wprins at gmail.com (Walter Prins) Date: Sat, 4 Dec 2010 02:02:32 +0000 Subject: [Tutor] Newline In-Reply-To: References: Message-ID: Hello Ava/Ashley On 4 December 2010 01:47, Ashley Blackwell wrote: > Exactly what is a newline character? Not all characters used to encode information are printable/displayable characters like the letters in the alphabet or numerals or punctuation and symbols. There's several "special" characters that have special meanings and represent other things. A "newline character" is such a character. It is character 13 in the ASCII character set (which is a well known encoding standard that define a mapping between bytes of data and characters. See here: http://www.asciitable.com/ ) It's meaning is "start a new line" instead of encoding "a" or "z" or "#" or whatever. > I keep reading the same section of the book and I'm just not really > understanding. Here's the code that the book uses to explain the newline > character: > > print("Here", end=" ") > print("it is...") > > The book says that I can specify that a space be the final character > printed instead of a newline. In the first print() statement I specify that > a space be the final string printed. So the text "Here" appears but no > newline character. The next print() statement prints the text "it is"..." > right after the space following the final "e" in the "Here" text. You do it > by passing a space to the end parameter of the print() function with the > code end" ". > So, what this is trying to explain is that by default Python appends a newline character to the end of whatever it's been told to print. This has the effect of normally puttig successive print statements on successive lines. However, you can override this behaviour and tell Python to use another character as its "end" character to append to whatever is printed, by specifying the "end" parameter. If you for example replace this with space e.g. " " then as you can see when you execute the 2 print statements they end up on one line, seperated by thace space, instead of being on 2 seperate lines. Does that help? Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From ranceh at gmail.com Sat Dec 4 03:04:45 2010 From: ranceh at gmail.com (Rance Hall) Date: Fri, 3 Dec 2010 20:04:45 -0600 Subject: [Tutor] Newline In-Reply-To: References: Message-ID: Ava: On an old typewriter when you slapped that mechanical arm two separate but related things happened. 1) the page advanced to the next line of text. 2) the carriage was returnedt to the beginning of the line of text. If you were careful about the way you slapped that mechanical arm, you could get the page to advance without moving the carriage to the beginning of the line. You would get stair stepped output like this Now in the computing world, these two separate but related things can sometimes still be dealt with individually. strictly speaking a newline character is the command to move down to the next line. and a return character is the command to go to the beginning of the line. On Windows, by convention the newline command does both. So you likely won't see the return command much. But there is one, and it is used on most non-windows systems. as to your specific question, I've never actually tried to specify a new line character like you have demonstrated. I just use the "\n" sequence to make my new lines happen where I want. On Fri, Dec 3, 2010 at 7:47 PM, Ashley Blackwell wrote: > Hello everybody, I'm new to the mailing list so I'm pretty sure I'll have > lots of questions:) > > It's a very basic question I?have and everybody might look at this > question?and say, "Wow, she reallly doesn't get it?" But oh well. Here's my > question: I'm in Chapter 2 of my Python Programming Third?Editition?book. > I've gotten to the section of the chapter where it talks about a "newline > character. Exactly?what is?a newline character? I keep reading the same > section of the book and I'm just not really understanding. Here's the code > that the book uses to explain the newline character: > > print("Here", end="? ") > print("it?is...") > > The book says that I can specify that a space be the final character printed > instead of a newline. In the first print() statement I specify that a space > be the final string printed. So the text "Here" appears but no newline > character. The next print() statement prints the text "it is"..." right > after the space following the final "e" in the "Here" text. You do it by > passing a space?to the end parameter of the print() function with the code > end"? ". > > My Operating System is Windows 7 and the Python version is 3.1.3. That's it. > Thanks to anybody who responds to my question. > > -- > Ava Durand > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From mehgcap at gmail.com Sat Dec 4 03:07:07 2010 From: mehgcap at gmail.com (Alex Hall) Date: Fri, 3 Dec 2010 21:07:07 -0500 Subject: [Tutor] Newline In-Reply-To: References: Message-ID: On 12/3/10, Ashley Blackwell wrote: > Hello everybody, I'm new to the mailing list so I'm pretty sure I'll have > lots of questions:) That is what the list is for. :) > > It's a very basic question I have and everybody might look at this > question and say, "Wow, she reallly doesn't get it?" But oh well. Here's my > question: I'm in Chapter 2 of my Python Programming Third Editition book. > I've gotten to the section of the chapter where it talks about a "newline > character. Exactly what is a newline character? I keep reading the same > section of the book and I'm just not really understanding. Here's the code > that the book uses to explain the newline character: > > print("Here", end=" ") > print("it is...") > > The book says that I can specify that a space be the final character printed > instead of a newline. In the first print() statement I specify that a space > be the final string printed. So the text "Here" appears but no newline > character. The next print() statement prints the text "it is"..." right > after the space following the final "e" in the "Here" text. You do it by > passing a space to the end parameter of the print() function with the code > end" ". > > My Operating System is Windows 7 and the Python version is 3.1.3. That's it. > Thanks to anybody who responds to my question. A newline character is just a character telling the computer to move to the next line. Understand that all characters, from basic letters (a, b, c, ...) to strange characters that you never see are represented by numbers. For example, the letter a is 97, b is 98, and so on. A space character is 32. A newline has a number as well, since it is, after all, just another character. Instead of using numbers, though, Python (like most programming languages) lets you use what I believe are called escape sequences (someone correct me here). Basically, putting a backslash (\) followed by a letter will sometimes represent a character. \t is tab, for example. In this case, try passing \n to your function (remember to put it in quotes, since it is still a string!) and see if that does it. I hope this makes some sense. > > -- > *Ava Durand* > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From steve at pearwood.info Sat Dec 4 03:09:09 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 04 Dec 2010 13:09:09 +1100 Subject: [Tutor] Newline In-Reply-To: References: Message-ID: <4CF9A2C5.5070708@pearwood.info> Ashley Blackwell wrote: > Hello everybody, I'm new to the mailing list so I'm pretty sure I'll have > lots of questions:) > > It's a very basic question I have and everybody might look at this > question and say, "Wow, she reallly doesn't get it?" But oh well. Here's my > question: I'm in Chapter 2 of my Python Programming Third Editition book. > I've gotten to the section of the chapter where it talks about a "newline > character. Exactly what is a newline character? Text (say, in a word processor, or in source code) is made up of characters. Some characters are visible, like letters and digits. Some characters are invisible "whitespace". The most obvious one is the space character, which you get by pressing the spacebar. A little less obvious is the "newline" character, which you get by pressing the Enter key. The newline is special, because it instructions your word processor or text editor to start a new line. Duh! :-) Anyway, the point is that when you're dealing with text, there is an "invisible" newline at the end of each line. That's how your program knows that it is the end of a line! The print() function automatically adds a newline by default: >>> s = "Nobody expects the Spanish Inquisition!" # no newline >>> print(s) # automatically adds a newline Nobody expects the Spanish Inquisition! >>> If the string already includes a newline, you get a blank line: >>> s = "Nobody expects the Spanish Inquisition!\n" # \n = newline >>> print(s) Nobody expects the Spanish Inquisition! >>> Notice the blank line between the printed string and the next prompt (>>>)? print() adds a newline because that's nearly always what you want: >>> for i in (1, 2, 3): ... print(i, "spam") ... 1 spam 2 spam 3 spam but sometimes you want to print on the same line: >>> for i in (1, 2, 3): ... print(i, "spam", end="***") ... 1 spam***2 spam***3 spam***>>> Notice that because there's no newline printed at all, the prompt >>> ends up on the same line as the output! There are ways around that. Here's one: >>> for i in (1, 2, 3): ... print(i, "spam", end="\n" if i==3 else "***") ... 1 spam***2 spam***3 spam >>> Hope this helps, -- Steven From hugo.yoshi at gmail.com Sat Dec 4 03:30:30 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sat, 4 Dec 2010 03:30:30 +0100 Subject: [Tutor] Newline In-Reply-To: References: Message-ID: On Sat, Dec 4, 2010 at 3:04 AM, Rance Hall wrote: > Ava: > > On an old typewriter when you slapped that mechanical arm two separate > but related things happened. > > 1) the page advanced to the next line of text. > 2) the carriage was returnedt to the beginning of the line of text. > > If you were careful about the way you slapped that mechanical arm, you > could get the page to advance without moving the carriage to the > beginning of the line. > > You would get > ? ? ? ? ? ? ? ? ? ? stair stepped > ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? output like this > > Now in the computing world, these two separate but related things can > sometimes still be dealt with individually. > > strictly speaking a newline character is the command to move down to > the next line. > > and a return character is the command to go to the beginning of the line. > On Windows, by convention the newline command does both. > So you likely won't see the return command much. > But there is one, and it is used on most non-windows systems. As a matter of fact, on windows, a newline is encoded in binary as a carriage return (CR, ASCII code 13) followed by a linefeed (LF, ASCII code 10). The carriage return is the "return to beginning of line," and the linefeed is the "move one line down" that you mention. So in effect just the ASCII "newline" character *doesn't* do both. However, python is smart, so when you tell it to print a "\n" escape character, it prints out both a linefeed and a carriage return on windows systems (most other programming languages do this as well). Most older operating systems that predate Unix used this CR+LF convention as well, because their printing was done on teletypes that functioned much like actual typewriters. However, you're very unlikely to come across these systems today, apart from Windows. Unix systems don't use the carriage return at all, so just a newline character suffices there to move to the beginning of the next line. Mac OS (up to version 9, IIRC) actually used just a carriage return, no newline. Hugo From steve at pearwood.info Sat Dec 4 04:05:47 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 04 Dec 2010 14:05:47 +1100 Subject: [Tutor] print problem In-Reply-To: <581989.29689.qm@web111207.mail.gq1.yahoo.com> References: <581989.29689.qm@web111207.mail.gq1.yahoo.com> Message-ID: <4CF9B00B.9080809@pearwood.info> Hs Hs wrote: > hi I have a file and it is in chunks: > I want to be able to select the number that follows 'Elution: ' where the line > startswith (TITLE)(say > 72.958) and check if it is larger than my choice of number (say > 71.4). If it is then I want to print chunk from BEGIN LINE to END > LINE separated by one empty line. > > I wrote the following script, when executed in IDEL or python mode, What is "python mode"? Do you mean the interactive interpreter? > it works. however I use python test_script_test.py , I get name error: > > could you please help me whats wrong here. > $ python test_script_test.py > Traceback (most recent call last): > File "test_script_test.py", line 14, in > newk = dat[mystartl:myfinish] > NameError: name 'myfinish' is not defined > > > In python mode: >>>> f1 = open('test','r') >>>> da = f1.read().split('\n') >>>> dat = da[:-1] >>>> mytimep = 71.4 >>>> for i in range(len(dat)): > ... if dat[i].startswith('BEGIN LINE'): > ... mystartl = i > ... if dat[i].startswith('END LINE'): > ... myfinish = i > ... if dat[i].startswith('Title'): > ... col = dat[i].split(',') > ... x= float(col[2].split()[1]) > ... if x > mytimep: > ... newk = dat[mystartl:myfinish] > ... for x in newk: > ... print x This can fail because myfinish doesn't get defined until you see a line "END LINE". So if you have a file like this: BEGIN LINE <= defines "mystart" Title ... <= tries to use "mystart" and "myfinish" END LINE <= defines "myfinish" the call to dat[mystartl:myfinish] fails because there is no myfinish. The reason it works in IDLE and the Python interactive interpreter is that you must have already defined a myfinish variable previously. Your code does way too much work manually instead of letting Python handle it. If you want to read lines from a file, just read them from a file. This should be close to what you want: f1 = open('test','r') mytimep = 71.4 flag = False collected = [] # Hold collected lines as you see them. for line in f1: line = line.rstrip() # strip whitespace from the end of the line if line == "BEGIN LINE": flag = True # start processing continue # but not *this* line, start at the next line if line == "END LINE": flag == False if flag: # process the line if line.startswith('Title'): col = line.split(',') x = float(col[2].split()[1]) if x > mytimep: collected.append(line) for line in collected: print line -- Steven From tgreen673 at aol.com Sat Dec 4 07:08:16 2010 From: tgreen673 at aol.com (tgreen673 at aol.com) Date: Sat, 4 Dec 2010 01:08:16 -0500 (EST) Subject: [Tutor] Need Help! Message-ID: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> Hello I am totally new to python and our last assignment is that we have to create a Gui Application game similar to Hangman. The game allows the user to guess a movie title from hints and allows the player to also guess a letter in the title. Right now I just have the GUI set up. Whatever help you guys can give me will be apperciated. I will attach my Driver and GUI file to this email. -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: project_classes.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: project_driver.py URL: From hugo.yoshi at gmail.com Sat Dec 4 07:47:51 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sat, 4 Dec 2010 07:47:51 +0100 Subject: [Tutor] Need Help! In-Reply-To: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> References: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> Message-ID: On Sat, Dec 4, 2010 at 7:08 AM, wrote: > > Hello I am totally new to python and our last assignment is that we have to > create a Gui Application game similar to Hangman. The game allows the user > to guess a movie title from hints and allows the player to also guess a > letter in the title. Right now I just have the GUI set up. Whatever help you > guys can give me will be apperciated. I will attach my Driver and GUI file > to this email. > I'm sorry, you seem to have forgotten to ask your question. If you're unsure of how to do so, this guide might be of some help: http://www.catb.org/~esr/faqs/smart-questions.html We all like to help out people who have specific, good questions. We generally don't like to do someone's homework for them. If we did, how would they learn? So: with what are you having trouble and what have you tried? Hugo From steve at pearwood.info Sat Dec 4 07:49:14 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 04 Dec 2010 17:49:14 +1100 Subject: [Tutor] Need Help! In-Reply-To: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> References: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> Message-ID: <4CF9E46A.5070603@pearwood.info> tgreen673 at aol.com wrote: > > > Hello I am totally new to python and our last assignment is that we have to create a Gui Application game similar to Hangman. The game allows the user to guess a movie title from hints and allows the player to also guess a letter in the title. Right now I just have the GUI set up. Whatever help you guys can give me will be apperciated. I will attach my Driver and GUI file to this email. Did you have a question, or would you just like us to do your assignment for you? -- Steven From joel at joelschwartz.com Sat Dec 4 08:19:27 2010 From: joel at joelschwartz.com (Joel Schwartz) Date: Fri, 3 Dec 2010 23:19:27 -0800 Subject: [Tutor] Question about the "main" Python help list In-Reply-To: <4CF9E46A.5070603@pearwood.info> References: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> <4CF9E46A.5070603@pearwood.info> Message-ID: <0d2401cb9383$9654cd90$6501a8c0@JLAPTOP> I just went to sign up for the main Python help list and discovered that it is a "closed" list, in the sense that the answers to queries are kept private between the questioner and the helpers, rather than shared with everyone on the list. My question: Is there a general Python help list that is open for all to see, in the same way that the Python Tutor list is open? Although the Tutor list is great for getting started, it would also be very helpful to be able to see answers to questions asked by Python programmers with more experience and more sophisticated projects than those discussed on the Tutor list. I've been using the R programming language for a couple of years now and the general R-help list is open for all to see and with searchable archives. It is an invaluable resource for learning advanced programming techniques and sophisticated data analysis and management methods. Is there anything similar for Python? Thanks, Joel From stefan_ml at behnel.de Sat Dec 4 08:43:00 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 04 Dec 2010 08:43:00 +0100 Subject: [Tutor] Question about the "main" Python help list In-Reply-To: <0d2401cb9383$9654cd90$6501a8c0@JLAPTOP> References: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> <4CF9E46A.5070603@pearwood.info> <0d2401cb9383$9654cd90$6501a8c0@JLAPTOP> Message-ID: Joel Schwartz, 04.12.2010 08:19: > I just went to sign up for the main Python help list and discovered that it > is a "closed" list What (and where) is the "main Python help list"? Stefan From joel at joelschwartz.com Sat Dec 4 09:07:54 2010 From: joel at joelschwartz.com (Joel Schwartz) Date: Sat, 4 Dec 2010 00:07:54 -0800 Subject: [Tutor] Question about the "main" Python help list In-Reply-To: References: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> <4CF9E46A.5070603@pearwood.info><0d2401cb9383$9654cd90$6501a8c0@JLAPTOP> Message-ID: <0d2801cb938a$5ad157f0$6501a8c0@JLAPTOP> I meant the Python-Help mailing list (http://mail.python.org/mailman/listinfo/python-help), which is described on the Python mailing list page (http://mail.python.org/mailman/listinfo) as "Expert volunteers answer Python-related questions." I guess I should have stated specifically what list I was referring to. Basically, the Python-Tutor list seems to be for people in the "beginner" or "initial learning" stage of Python, while the Python-Help list seems to be the general help list for people who are beyond the basics but who have some problem they can't solve on their own. I was hoping that there is a general Python help list that is not closed. Does anyone know if there is? Does anyone know why Python-Help Q&A are not made public? Joel > -----Original Message----- > From: tutor-bounces+joel=joelschwartz.com at python.org > [mailto:tutor-bounces+joelelschwartz.com at python.org] On > Behalf Of Stefan Behnel > Sent: Friday, December 03, 2010 11:43 PM > To: tutor at python.org > Subject: Re: [Tutor] Question about the "main" Python help list > > Joel Schwartz, 04.12.2010 08:19: > > I just went to sign up for the main Python help list and discovered > > that it is a "closed" list > > What (and where) is the "main Python help list"? > > Stefan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From stefan_ml at behnel.de Sat Dec 4 09:14:22 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 04 Dec 2010 09:14:22 +0100 Subject: [Tutor] Question about the "main" Python help list In-Reply-To: <0d2801cb938a$5ad157f0$6501a8c0@JLAPTOP> References: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> <4CF9E46A.5070603@pearwood.info><0d2401cb9383$9654cd90$6501a8c0@JLAPTOP> <0d2801cb938a$5ad157f0$6501a8c0@JLAPTOP> Message-ID: Joel Schwartz, 04.12.2010 09:07: > I meant the Python-Help mailing list > (http://mail.python.org/mailman/listinfo/python-help), which is described on > the Python mailing list page (http://mail.python.org/mailman/listinfo) as > "Expert volunteers answer Python-related questions." > > I guess I should have stated specifically what list I was referring to. > Basically, the Python-Tutor list seems to be for people in the "beginner" or > "initial learning" stage of Python, while the Python-Help list seems to be > the general help list for people who are beyond the basics but who have some > problem they can't solve on their own. > > I was hoping that there is a general Python help list that is not closed. > Does anyone know if there is? Sure, it's called "python-list" or as a newsgroup: "comp.lang.python". Stefan From davea at ieee.org Sat Dec 4 09:37:10 2010 From: davea at ieee.org (Dave Angel) Date: Sat, 04 Dec 2010 03:37:10 -0500 Subject: [Tutor] Question about the "main" Python help list In-Reply-To: <0d2801cb938a$5ad157f0$6501a8c0@JLAPTOP> References: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> <4CF9E46A.5070603@pearwood.info><0d2401cb9383$9654cd90$6501a8c0@JLAPTOP> <0d2801cb938a$5ad157f0$6501a8c0@JLAPTOP> Message-ID: <4CF9FDB6.5020604@ieee.org> On 01/-10/-28163 02:59 PM, Joel Schwartz wrote: > I meant the Python-Help mailing list > (http://mail.python.org/mailman/listinfo/python-help), which is described on > the Python mailing list page (http://mail.python.org/mailman/listinfo) as > "Expert volunteers answer Python-related questions." > > I guess I should have stated specifically what list I was referring to. > Basically, the Python-Tutor list seems to be for people in the "beginner" or > "initial learning" stage of Python, while the Python-Help list seems to be > the general help list for people who are beyond the basics but who have some > problem they can't solve on their own. > > I was hoping that there is a general Python help list that is not closed. > Does anyone know if there is? Does anyone know why Python-Help Q&A are not > made public? > > Joel > > I think you're looking for comp.lang.python, which is a normal, subscribable list. it works just like python-tutor, except the latter is for beginners. Send Python-list mailing list submissions to python-list at python.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.python.org/mailman/listinfo/python-list or, via email, send a message with subject or body 'help' to python-list-request at python.org You can reach the person managing the list at python-list-owner at python.org From fomcl at yahoo.com Sat Dec 4 12:59:24 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 4 Dec 2010 03:59:24 -0800 (PST) Subject: [Tutor] Question on tkinter event binding In-Reply-To: <614AAA2090C841188E44C23ED34C3081@mycomputer> References: <565024.32044.qm@web110702.mail.gq1.yahoo.com> <508713.11256.qm@web110705.mail.gq1.yahoo.com> <614AAA2090C841188E44C23ED34C3081@mycomputer> Message-ID: <652967.82402.qm@web110713.mail.gq1.yahoo.com> Hi Patty, As far as books are concerned, I actually prefer (programming) books in the English language. Although the Dutch don't do it as much as e.g. the French or the Germans, I hate it when technical terms are translated into Dutch in a somewhat artificial way ("Computer" is "Ordinateur" in French and "Rechner" in German [although "Computer" is also OK]; in Dutch it's simply "Computer") . It also makes it harder to find additional info on the internet. In addition, books in the English language are usually far cheaper than those in Dutch. As far as programming itself is concerned, I find it slightly more readable to use Dutch variable and function names. The risk of name clashes is also virtually absent! In my office we have a coding convention which states that Dutch names should be used. I must confess, however, I don't always consistently follow the convention. One example are getter and setter methods. It's just clearer to use 'get' and 'set' in the method name. When I download software, I use then 'as-is'. I might translate code snippets so it blends better with the rest of the code. If I send snippets to e.g. this mailing list, I usually translate the variable names + comments --only this time I was a bit lazy. ;-) Cheers!! 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: Patty To: Albert-Jan Roskam ; Python Mailing List Sent: Fri, December 3, 2010 11:39:28 PM Subject: Re: [Tutor] Question on tkinter event binding Hello Albert-Jan: I am glad you made the comment below. I was fascinated with the fact that your code was partly in English/Python and also in Dutch. I am a linguist so have great interest in bilingualism. How does this work in practice? I mean as a programmer, with native language other than English, do you download or buy English language software programs and work with them as-is? Do you have translated tutorials to help you learn? If you had a Dutch language software program and created your own program so that everything is totally in Dutch, and you wanted to communicate with English language email group :) How would you do that? Or would you try and find a Dutch language resource? Besides that, I am definitely saving your code segments for the future. Thanks for sharing. Patty ----- Original Message ----- >From: Albert-Jan Roskam >To: Albert-Jan Roskam ; Python Mailing List >Sent: Friday, December 03, 2010 12:18 PM >Subject: Re: [Tutor] Question on tkinter event binding > > > > >I'll paste the working code below. It's partially in Dutch, but hey, so is >Guido van Rossem. ;-) > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Sat Dec 4 14:10:04 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sat, 4 Dec 2010 14:10:04 +0100 Subject: [Tutor] Question on tkinter event binding In-Reply-To: <652967.82402.qm@web110713.mail.gq1.yahoo.com> References: <565024.32044.qm@web110702.mail.gq1.yahoo.com> <508713.11256.qm@web110705.mail.gq1.yahoo.com> <614AAA2090C841188E44C23ED34C3081@mycomputer> <652967.82402.qm@web110713.mail.gq1.yahoo.com> Message-ID: <83E7E28F-8121-4BAA-939E-E47D7FDFB8E7@gmail.com> > Hi Patty, > > As far as books are concerned, I actually prefer (programming) books in the English language. Although the Dutch don't do it as much as e.g. the French or the Germans, I hate it when technical terms are translated into Dutch in a somewhat artificial way ("Computer" is "Ordinateur" in French and "Rechner" in German [although "Computer" is also OK]; in Dutch it's simply "Computer") . It also makes it harder to find additional info on the internet. In addition, books in the English language are usually far cheaper than those in Dutch. > > As far as programming itself is concerned, I find it slightly more readable to use Dutch variable and function names. The risk of name clashes is also virtually absent! In my office we have a coding convention which states that Dutch names should be used. I must confess, however, I don't always consistently follow the convention. One example are getter and setter methods. It's just clearer to use 'get' and 'set' in the method name. When I download software, I use then 'as-is'. I might translate code snippets so it blends better with the rest of the code. If I send snippets to e.g. this mailing list, I usually translate the variable names + comments --only this time I was a bit lazy. ;-) I tend to follow this piece of advice from PEP 8: "Python coders from non-English speaking countries: please write your comments in English, unless you are 120% sure that the code will never be read by people who don't speak your language." It's about comments, but applies just as well to variable names etc. If output strings are eg Dutch, that's only the non-English part, and I might even make use of some "translation" library (ie, English strings, which get replaced by the appropriate strings upon a language settings. I know Django uses this system). It has such a huge advantage when, for example, asking questions on a list like this. Albert-Jan may occasionally translate his problems, but there's no guarantee that that wouldn't masquerade the actual problem. I actually find it a bit weird that a (programming?) company has a coding convention for non-English names; makes it harder if you want to hire non-Dutch speaking employees, distribute software (& code) internationally etc. Cheers, Evert > > Cheers!! > 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: Patty > To: Albert-Jan Roskam ; Python Mailing List > Sent: Fri, December 3, 2010 11:39:28 PM > Subject: Re: [Tutor] Question on tkinter event binding > > Hello Albert-Jan: > I am glad you made the comment below. I was fascinated with the fact that your code was partly in English/Python and also in Dutch. I am a linguist so have great interest in bilingualism. How does this work in practice? I mean as a programmer, with native language other than English, do you download or buy English language software programs and work with them as-is? Do you have translated tutorials to help you learn? If you had a Dutch language software program and created your own program so that everything is totally in Dutch, and you wanted to communicate with English language email group :) How would you do that? Or would you try and find a Dutch language resource? > > Besides that, I am definitely saving your code segments for the future. Thanks for sharing. > > Patty > ----- Original Message ----- > From: Albert-Jan Roskam > To: Albert-Jan Roskam ; Python Mailing List > Sent: Friday, December 03, 2010 12:18 PM > Subject: Re: [Tutor] Question on tkinter event binding > > > > I'll paste the working code below. It's partially in Dutch, but hey, so is Guido van Rossem. ;-) > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Sat Dec 4 14:45:44 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 4 Dec 2010 13:45:44 -0000 Subject: [Tutor] Newline References: <4CF9A2C5.5070708@pearwood.info> Message-ID: "Steven D'Aprano" wrote > >>> for i in (1, 2, 3): > ... print(i, "spam", end="\n" if i==3 else "***") Ooooh! A new trick. I hadn't thought of using the conditional expression there but it makes a lot of sense. Definitely more fun and flexible than the old comma at the end of a print in v2 :-) Thanks for that, Alan G. From alan.gauld at btinternet.com Sat Dec 4 14:55:08 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 4 Dec 2010 13:55:08 -0000 Subject: [Tutor] Question about the "main" Python help list References: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com><4CF9E46A.5070603@pearwood.info> <0d2401cb9383$9654cd90$6501a8c0@JLAPTOP> Message-ID: "Joel Schwartz" wrote > My question: Is there a general Python help list that is open for > all to > see, in the same way that the Python Tutor list is open? There are many Python mailing lists and usenet newsgroups. They are all available to browse on gmane. (just short of 200 of them by my estimate) http://dir.gmane.org/index.php?prefix=gmane.comp.python Usually you will be best served by picking the most specific newsgroup for your topic and asking there. Take a look at the archive list to get an idea of how busy and current they are, also what the average turn-around time is for replies. You can subscribe to most of them as mailing lists or use a newsreader (eg Outlook Express or Thunderbird or GNUs etc) to access the gmane nntp feed HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Sat Dec 4 15:20:33 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 4 Dec 2010 06:20:33 -0800 (PST) Subject: [Tutor] Question on tkinter event binding In-Reply-To: <83E7E28F-8121-4BAA-939E-E47D7FDFB8E7@gmail.com> References: <565024.32044.qm@web110702.mail.gq1.yahoo.com> <508713.11256.qm@web110705.mail.gq1.yahoo.com> <614AAA2090C841188E44C23ED34C3081@mycomputer> <652967.82402.qm@web110713.mail.gq1.yahoo.com> <83E7E28F-8121-4BAA-939E-E47D7FDFB8E7@gmail.com> Message-ID: <720105.71070.qm@web110716.mail.gq1.yahoo.com> Hi Evert, I actually wanted the var names to be in English, but apparently the people who came up with the coding convention did not agree with me on this. Then again, the Zen of Python states that (1) readability counts and that (2) practicality beats purity. Most of the time, I'm 120% sure the code doesn't leave Holland. If it inadvertently does, Ctrl-H is my friend. It's impractical to overgeneralize one's applications. Let's say I have a list of all the medical specialisms we have here in N. I really wouldn't know all the exact English equivalents. My manager wouldn't like it if I spent my time doing all the translations. Besides, doesn't it with the current political climate to have dutch-only code? ;-) [Note: We had a landslide win of a xenophobic, right-wing party during the last elections] Meanwhile, I tinkered a bit more with the code. I used exec() to isolate the event handler function. It works and it's better, but I think it could be still better. I'm not so fond of eval() and exec(). from Tkinter import * def createWidgets(veldnamen): root=Tk() termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]), "*Postcode": set(["2600AA", "8000BB"]), "Adres": set(["Street", "Avenue"])} handleDeletions = {} for veldnaam in veldnamen: labelWidget=Label(root, text=veldnaam, takefocus=False) labelWidget.grid() # tcl names must start with a lowercase letter tclName = veldnaam[0].lower() + veldnaam[1:] content = StringVar() entryWidget=Entry(root, name=tclName, textvariable=content) entryWidget.grid() exec(doHandleDeletion()) handleDeletions[entryWidget] = handleDeletion for entryWidget, handleDeletion in handleDeletions.iteritems(): entryWidget.bind("", handleDeletion) def doHandleDeletion(): func = \ """def handleDeletion(event, widget=entryWidget, root=root, termenlijst=termenlijst,content=content): actieveVenster = root.focus_get() actieveVensternaam = str(actieveVenster)[1:].capitalize() if actieveVensternaam.startswith("*"): actieveVensternaam = "*" + actieveVensternaam[1:].capitalize() vensterinhoud = content.get().strip() print "Name: %s -- Contents: %s" % (actieveVensternaam, vensterinhoud) try: termenlijst[actieveVensternaam].remove(vensterinhoud) actieveVenster.delete(0, END) print "Deleted term '%s'" % vensterinhoud except KeyError: print "No such term '%s'" % vensterinhoud pass""" return func createWidgets(["Naam", "*Postcode", "Adres"]) Cheers!! 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: Evert Rol To: Albert-Jan Roskam Cc: Patty ; Python Mailing List Sent: Sat, December 4, 2010 2:10:04 PM Subject: Re: [Tutor] Question on tkinter event binding > Hi Patty, > > As far as books are concerned, I actually prefer (programming) books in the >English language. Although the Dutch don't do it as much as e.g. the French or >the Germans, I hate it when technical terms are translated into Dutch in a >somewhat artificial way ("Computer" is "Ordinateur" in French and "Rechner" in >German [although "Computer" is also OK]; in Dutch it's simply "Computer") . It >also makes it harder to find additional info on the internet. In addition, books >in the English language are usually far cheaper than those in Dutch. > > As far as programming itself is concerned, I find it slightly more readable to >use Dutch variable and function names. The risk of name clashes is also >virtually absent! In my office we have a coding convention which states that >Dutch names should be used. I must confess, however, I don't always consistently >follow the convention. One example are getter and setter methods. It's just >clearer to use 'get' and 'set' in the method name. When I download software, I >use then 'as-is'. I might translate code snippets so it blends better with the >rest of the code. If I send snippets to e.g. this mailing list, I usually >translate the variable names + comments --only this time I was a bit lazy. ;-) I tend to follow this piece of advice from PEP 8: "Python coders from non-English speaking countries: please write your comments in English, unless you are 120% sure that the code will never be read by people who don't speak your language." It's about comments, but applies just as well to variable names etc. If output strings are eg Dutch, that's only the non-English part, and I might even make use of some "translation" library (ie, English strings, which get replaced by the appropriate strings upon a language settings. I know Django uses this system). It has such a huge advantage when, for example, asking questions on a list like this. Albert-Jan may occasionally translate his problems, but there's no guarantee that that wouldn't masquerade the actual problem. I actually find it a bit weird that a (programming?) company has a coding convention for non-English names; makes it harder if you want to hire non-Dutch speaking employees, distribute software (& code) internationally etc. Cheers, Evert > > Cheers!! > 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: Patty > To: Albert-Jan Roskam ; Python Mailing List > Sent: Fri, December 3, 2010 11:39:28 PM > Subject: Re: [Tutor] Question on tkinter event binding > > Hello Albert-Jan: > I am glad you made the comment below. I was fascinated with the fact that your >code was partly in English/Python and also in Dutch. I am a linguist so have >great interest in bilingualism. How does this work in practice? I mean as a >programmer, with native language other than English, do you download or buy >English language software programs and work with them as-is? Do you have >translated tutorials to help you learn? If you had a Dutch language software >program and created your own program so that everything is totally in Dutch, and >you wanted to communicate with English language email group :) How would you do >that? Or would you try and find a Dutch language resource? > > Besides that, I am definitely saving your code segments for the future. Thanks >for sharing. > > Patty > ----- Original Message ----- > From: Albert-Jan Roskam > To: Albert-Jan Roskam ; Python Mailing List > Sent: Friday, December 03, 2010 12:18 PM > Subject: Re: [Tutor] Question on tkinter event binding > > > > I'll paste the working code below. It's partially in Dutch, but hey, so is >Guido van Rossem. ;-) > > > > _______________________________________________ > 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 Sat Dec 4 15:36:03 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 05 Dec 2010 01:36:03 +1100 Subject: [Tutor] Question on tkinter event binding In-Reply-To: <83E7E28F-8121-4BAA-939E-E47D7FDFB8E7@gmail.com> References: <565024.32044.qm@web110702.mail.gq1.yahoo.com> <508713.11256.qm@web110705.mail.gq1.yahoo.com> <614AAA2090C841188E44C23ED34C3081@mycomputer> <652967.82402.qm@web110713.mail.gq1.yahoo.com> <83E7E28F-8121-4BAA-939E-E47D7FDFB8E7@gmail.com> Message-ID: <4CFA51D3.4080602@pearwood.info> Evert Rol wrote: > I actually find it a bit weird that a (programming?) company has a coding convention for non-English names; makes it harder if you want to hire non-Dutch speaking employees, distribute software (& code) internationally etc. I can't put my hands on the reference right now, but something like 99% of all software in the world is written purely for internal use and never gets distributed *at all*, let alone internationally. -- Steven From steve at pearwood.info Sat Dec 4 15:49:26 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 05 Dec 2010 01:49:26 +1100 Subject: [Tutor] Question on tkinter event binding In-Reply-To: <720105.71070.qm@web110716.mail.gq1.yahoo.com> References: <565024.32044.qm@web110702.mail.gq1.yahoo.com> <508713.11256.qm@web110705.mail.gq1.yahoo.com> <614AAA2090C841188E44C23ED34C3081@mycomputer> <652967.82402.qm@web110713.mail.gq1.yahoo.com> <83E7E28F-8121-4BAA-939E-E47D7FDFB8E7@gmail.com> <720105.71070.qm@web110716.mail.gq1.yahoo.com> Message-ID: <4CFA54F6.1000101@pearwood.info> Albert-Jan Roskam wrote: > Meanwhile, I tinkered a bit more with the code. I used exec() to isolate the > event handler function. It works and it's better, but I think it could be still > better. I'm not so fond of eval() and exec(). They have their uses, but yes, it's best to avoid them unless you need them. Let's see if we can avoid them :) > from Tkinter import * > > def createWidgets(veldnamen): > root=Tk() > termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]), > "*Postcode": set(["2600AA", "8000BB"]), > "Adres": set(["Street", "Avenue"])} > handleDeletions = {} > for veldnaam in veldnamen: > labelWidget=Label(root, text=veldnaam, takefocus=False) > labelWidget.grid() > # tcl names must start with a lowercase letter > tclName = veldnaam[0].lower() + veldnaam[1:] > content = StringVar() > entryWidget=Entry(root, name=tclName, textvariable=content) > entryWidget.grid() > > exec(doHandleDeletion()) > handleDeletions[entryWidget] = handleDeletion The classic solution for callbacks is to use lambda, but of course lambda is limited to a single expression and won't do the job here. So what you need is a factory function that returns a new function: handleDeletions[entryWidget] = make_deletion_handler() and the factory itself is defined something like this: def make_deletion_handler(): # Create a function. def handleDeletion(event, widget=entryWidget, root=root, termenlijst=termenlijst, content=content): actieveVenster = root.focus_get() actieveVensternaam = str(actieveVenster)[1:].capitalize() if actieveVensternaam.startswith("*"): actieveVensternaam = "*"+actieveVensternaam[1:].capitalize() vensterinhoud = content.get().strip() print "Name: %s -- Contents: %s" \ % (actieveVensternaam, vensterinhoud) try: termenlijst[actieveVensternaam].remove(vensterinhoud) actieveVenster.delete(0, END) print "Deleted term '%s'" % vensterinhoud except KeyError: print "No such term '%s'" % vensterinhoud # And return it. return handleDeletion If you move the factory outside of your createWidgets function, you will need to explicitly pass arguments entryWidget, content, etc. to the factory. Otherwise you can nest the factory inside createWidgets, at it will pick the variables up automatically. Hope this helps, -- Steven From mehgcap at gmail.com Sat Dec 4 16:50:02 2010 From: mehgcap at gmail.com (Alex Hall) Date: Sat, 4 Dec 2010 10:50:02 -0500 Subject: [Tutor] Newline In-Reply-To: References: <4CF9A2C5.5070708@pearwood.info> Message-ID: On 12/4/10, Alan Gauld wrote: > > "Steven D'Aprano" wrote > >> >>> for i in (1, 2, 3): >> ... print(i, "spam", end="\n" if i==3 else "***") > > Ooooh! A new trick. > I hadn't thought of using the conditional expression there but it > makes a lot of sense. > Definitely more fun and flexible than the old comma at the end of a > print in v2 :-) So is this a python3.x feature only? Is there an equivallent in 2.x? I had not realized if statements could be used in calling functions like that or that they could be arranged in that way, but I am sticking to python2 since most packages are still written for it. > > Thanks for that, > > Alan G. > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From rabidpoobear at gmail.com Sat Dec 4 17:37:31 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Sat, 4 Dec 2010 10:37:31 -0600 Subject: [Tutor] Question about the "main" Python help list In-Reply-To: <0d2401cb9383$9654cd90$6501a8c0@JLAPTOP> References: <8CD6190C6AABD1F-1FC4-6D91@webmail-m060.sysops.aol.com> <4CF9E46A.5070603@pearwood.info> <0d2401cb9383$9654cd90$6501a8c0@JLAPTOP> Message-ID: <5F8B8944-AB3F-419A-8442-06EFCA41A9D2@gmail.com> Next time you email tutor, start a new email msg instead of clearing out the contents of a reply to a different e-mail. When you do it like that it breaks threading, so all these emails are in the same thread as the "need help" one even though they aren't related to that e-mail. ----------------------------- Sent from a mobile device with a bad e-mail client. ----------------------------- On Dec 4, 2010, at 1:19 AM, "Joel Schwartz" wrote: > I just went to sign up for the main Python help list and discovered that it > is a "closed" list, in the sense that the answers to queries are kept > private between the questioner and the helpers, rather than shared with > everyone on the list. > > My question: Is there a general Python help list that is open for all to > see, in the same way that the Python Tutor list is open? Although the Tutor > list is great for getting started, it would also be very helpful to be able > to see answers to questions asked by Python programmers with more experience > and more sophisticated projects than those discussed on the Tutor list. > > I've been using the R programming language for a couple of years now and the > general R-help list is open for all to see and with searchable archives. It > is an invaluable resource for learning advanced programming techniques > and sophisticated data analysis and management methods. Is there anything > similar for Python? > > Thanks, > Joel > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Sat Dec 4 18:00:35 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 05 Dec 2010 04:00:35 +1100 Subject: [Tutor] Newline In-Reply-To: References: <4CF9A2C5.5070708@pearwood.info> Message-ID: <4CFA73B3.3040304@pearwood.info> Alex Hall wrote: > On 12/4/10, Alan Gauld wrote: >> "Steven D'Aprano" wrote >> >>>>>> for i in (1, 2, 3): >>> ... print(i, "spam", end="\n" if i==3 else "***") >> Ooooh! A new trick. >> I hadn't thought of using the conditional expression there but it >> makes a lot of sense. >> Definitely more fun and flexible than the old comma at the end of a >> print in v2 :-) > So is this a python3.x feature only? Is there an equivallent in 2.x? I > had not realized if statements could be used in calling functions like > that or that they could be arranged in that way, but I am sticking to > python2 since most packages are still written for it. Python has had "if" expressions (also known as the ternary operator) since Python2.5. You can write: true_value if condition else false_value anywhere that will accept an expression. E.g.: len("chicken" if today == tuesday else "fish") Since Python2.6, you can get the print function by using: from __future__ import print_function Note that there are TWO underscores at the start and end of "__future__", and that like all __future__ imports it must be the first executable line of your script or module. -- Steven From alan.gauld at btinternet.com Sat Dec 4 17:55:14 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 4 Dec 2010 16:55:14 +0000 (GMT) Subject: [Tutor] Newline In-Reply-To: References: <4CF9A2C5.5070708@pearwood.info> Message-ID: <96092.75675.qm@web86705.mail.ird.yahoo.com> > >> ... print(i, "spam", end="\n" if i==3 else "***") > > > > I hadn't thought of using the conditional expression there but it > > makes a lot of sense. > So is this a python3.x feature only? Is there an equivallent in 2.x? I The end parameter of print is a Python 3 feature - only possible because print is now a function rather than a command. In Python two the only option was to add a comma at the end of the print statement qwhich suppressed the newline. But the end parameter allows you to add anything you like, not just newlines or a null.. Of course you can use string formatting in Python 2 to achieve similar results. The if construct is nothing to do with print, it is a standard conditional expression introduced in Python v2.5 or 2.6? As is true of any *expression* it can be used anywhere that a value is used. This is because every expression produces to a value. So in Python v2 we could simulate Steven's loop with: for n in range(3): s += "%d spam%s" % (n,"\n" if n==3 else "***") print s, # comma to avoid print adding its own \n... But the print(..., end=....) version is much prettier IMHO. HTH, Alan G. From mehgcap at gmail.com Sat Dec 4 18:21:39 2010 From: mehgcap at gmail.com (Alex Hall) Date: Sat, 4 Dec 2010 12:21:39 -0500 Subject: [Tutor] Newline In-Reply-To: <4CFA73B3.3040304@pearwood.info> References: <4CF9A2C5.5070708@pearwood.info> <4CFA73B3.3040304@pearwood.info> Message-ID: On 12/4/10, Steven D'Aprano wrote: > Alex Hall wrote: >> On 12/4/10, Alan Gauld wrote: >>> "Steven D'Aprano" wrote >>> >>>>>>> for i in (1, 2, 3): >>>> ... print(i, "spam", end="\n" if i==3 else "***") >>> Ooooh! A new trick. >>> I hadn't thought of using the conditional expression there but it >>> makes a lot of sense. >>> Definitely more fun and flexible than the old comma at the end of a >>> print in v2 :-) >> So is this a python3.x feature only? Is there an equivallent in 2.x? I >> had not realized if statements could be used in calling functions like >> that or that they could be arranged in that way, but I am sticking to >> python2 since most packages are still written for it. > > > Python has had "if" expressions (also known as the ternary operator) > since Python2.5. You can write: > > true_value if condition else false_value How cool!! I never knew that. > > anywhere that will accept an expression. E.g.: > > len("chicken" if today == tuesday else "fish") > > Since Python2.6, you can get the print function by using: > > from __future__ import print_function > > Note that there are TWO underscores at the start and end of > "__future__", and that like all __future__ imports it must be the first > executable line of your script or module. > > > -- > Steven > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From kbailey at howlermonkey.net Sat Dec 4 20:27:10 2010 From: kbailey at howlermonkey.net (Kirk Bailey) Date: Sat, 04 Dec 2010 14:27:10 -0500 Subject: [Tutor] PHP Message-ID: <4CFA960E.4050108@howlermonkey.net> For reasons of my work, of recent I have started aquiring php. Straightaway, i was impressed by one powerful property; the ability to imbed php into a html webpage, and switch into and out of php as needed, and let normal ssi and html and css work the rest of the time, outside of php. all I have to do is start the php block with # end the php block and continue in html. What a BLOODY shame we can't do that in python, which is otherwise awesome and superior in every way i can thus far evaluate. -- end Very Truly yours, - Kirk Bailey, Largo Florida kniht +-----+ | BOX | +-----+ think From fomcl at yahoo.com Sat Dec 4 21:00:21 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 4 Dec 2010 12:00:21 -0800 (PST) Subject: [Tutor] Question on tkinter event binding Message-ID: <756518.87035.qm@web110708.mail.gq1.yahoo.com> Hi Steven, Awesome, I've got it working now! Here's the code: http://pastebin.com/BQhW8piD (I also pasted it below this message). I tried your approach before but I abandoned it because I made a mistake in lines 16 (pass None for event) and 22 (I didn't use the parameters of the outer function as the arguments of the inner function. Thanks a lot for putting me back on the right track!! It's really working cool now and I was able to remove some unnecessary code (and I added some bells and whistles ;-)) Cheers!! Albert-Jan import Tkinter, time, sys # To be used in conjunction with the AutocompleteEntry class: # http://tkinter.unpythonic.net/wiki/AutocompleteEntry def createWidgets(veldnamen, termenlijst): root=Tk() for veldnaam in veldnamen: labelWidget=Tkinter.Label(root, text=veldnaam, takefocus=False) labelWidget.grid() # tcl names must start with a lowercase letter tclName = veldnaam[0].lower() + veldnaam[1:] entryWidget=Tkinter.Entry(root, name=tclName, highlightcolor="yellow") entryWidget.grid() makeDeletionHandler(event=None, widget=entryWidget, root=root, termenlijst=termenlijst) def makeDeletionHandler(event, widget, root, termenlijst): def handleDeletion(event, widget=widget, root=root, termenlijst=termenlijst): vensternaam = str(widget)[1:].capitalize() if vensternaam.startswith("*"): vensternaam = "*" + vensternaam[1:].capitalize() vensterinhoud = widget.get().strip() print "Name: %s -- Contents: %s" % (vensternaam, vensterinhoud) try: termenlijst[vensternaam].remove(vensterinhoud) widget.delete(0, END) widget.configure(bg = "green") #print termenlijst print "Deleted term '%s'" % vensterinhoud except KeyError: print "No such term '%s'" % vensterinhoud pass finally: delay = 0.5 if sys.platform.lower().startswith("win"): delay = delay * 1000 time.sleep(delay) # Windows: specify in ms!) widget.configure(bg = "white") widget.bind("", handleDeletion) return handleDeletion createWidgets(veldnamen = ["Naam", "*Postcode", "Adres", "*Organization name"], termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]), "*Postcode": set(["2600AA", "8000BB"]), "Adres": set(["Street", "Avenue"]), "*Organization name": set(["CWI", "MIT"])}) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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: Steven D'Aprano To: Python Mailing List Sent: Sat, December 4, 2010 3:49:26 PM Subject: Re: [Tutor] Question on tkinter event binding Albert-Jan Roskam wrote: > Meanwhile, I tinkered a bit more with the code. I used exec() to isolate the >event handler function. It works and it's better, but I think it could be still >better. I'm not so fond of eval() and exec(). They have their uses, but yes, it's best to avoid them unless you need them. Let's see if we can avoid them :) > from Tkinter import * > > def createWidgets(veldnamen): > root=Tk() > termenlijst = {"Naam": set(["Bill Gates", "Elvis Presley"]), > "*Postcode": set(["2600AA", "8000BB"]), > "Adres": set(["Street", "Avenue"])} > handleDeletions = {} > for veldnaam in veldnamen: > labelWidget=Label(root, text=veldnaam, takefocus=False) > labelWidget.grid() > # tcl names must start with a lowercase letter > tclName = veldnaam[0].lower() + veldnaam[1:] > content = StringVar() > entryWidget=Entry(root, name=tclName, textvariable=content) > entryWidget.grid() > > exec(doHandleDeletion()) > handleDeletions[entryWidget] = handleDeletion The classic solution for callbacks is to use lambda, but of course lambda is limited to a single expression and won't do the job here. So what you need is a factory function that returns a new function: handleDeletions[entryWidget] = make_deletion_handler() and the factory itself is defined something like this: def make_deletion_handler(): # Create a function. def handleDeletion(event, widget=entryWidget, root=root, termenlijst=termenlijst, content=content): actieveVenster = root.focus_get() actieveVensternaam = str(actieveVenster)[1:].capitalize() if actieveVensternaam.startswith("*"): actieveVensternaam = "*"+actieveVensternaam[1:].capitalize() vensterinhoud = content.get().strip() print "Name: %s -- Contents: %s" \ % (actieveVensternaam, vensterinhoud) try: termenlijst[actieveVensternaam].remove(vensterinhoud) actieveVenster.delete(0, END) print "Deleted term '%s'" % vensterinhoud except KeyError: print "No such term '%s'" % vensterinhoud # And return it. return handleDeletion If you move the factory outside of your createWidgets function, you will need to explicitly pass arguments entryWidget, content, etc. to the factory. Otherwise you can nest the factory inside createWidgets, at it will pick the variables up automatically. Hope this helps, -- 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 kb1pkl at aim.com Sat Dec 4 21:29:10 2010 From: kb1pkl at aim.com (Corey Richardson) Date: Sat, 04 Dec 2010 15:29:10 -0500 Subject: [Tutor] PHP In-Reply-To: <4CFA960E.4050108@howlermonkey.net> References: <4CFA960E.4050108@howlermonkey.net> Message-ID: <4CFAA496.3060101@aim.com> On 12/4/2010 2:27 PM, Kirk Bailey wrote: > For reasons of my work, of recent I have started aquiring php. > Straightaway, i was impressed by one powerful property; the ability to > imbed php into a html webpage, and switch into and out of php as needed, > and let normal ssi and html and css work the rest of the time, outside > of php. all I have to do is start the php block with > write code; > ?> # end the php block > and continue in html. What a BLOODY shame we can't do that in python, > which is otherwise awesome and superior in every way i can thus far > evaluate. > > Actually, you CAN do that with Python. Python Server Pages (PSP) and mod_python enable you to do this on an Apache server. ~Corey Richardson From wescpy at gmail.com Sat Dec 4 21:38:00 2010 From: wescpy at gmail.com (wesley chun) Date: Sat, 4 Dec 2010 12:38:00 -0800 Subject: [Tutor] PHP In-Reply-To: <4CFA960E.4050108@howlermonkey.net> References: <4CFA960E.4050108@howlermonkey.net> Message-ID: > Straightaway, i was impressed by one powerful property; the ability to > imbed php into a html webpage, and switch into and out of php as needed, > and let normal ssi and html and css work the rest of the time, outside > of php. all I have to do is start the php block with > write code; > ?> # end the php block > and continue in html. What a BLOODY shame we can't do that in python, > which is otherwise awesome and superior in every way i can thus far evaluate. sure you can! however you have to use a web templating system, and there are many in Python (Mako, Genshi, Myghty, Kid, Cheetah, Jinja, Django templates, etc.). some examples here: Mako: http://www.makotemplates.org/docs/syntax.html#python-blocks Genshi: http://genshi.edgewall.org/wiki/Documentation/templates.html Myghty: http://www.myghty.org/docs/embedding.html it is not built-in to the language because Python is more general-purpose, so you have to import those packages to use them. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 ? ? http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From swiftone at swiftone.org Sat Dec 4 21:44:18 2010 From: swiftone at swiftone.org (Brett Ritter) Date: Sat, 4 Dec 2010 15:44:18 -0500 Subject: [Tutor] PHP In-Reply-To: <4CFA960E.4050108@howlermonkey.net> References: <4CFA960E.4050108@howlermonkey.net> Message-ID: On Sat, Dec 4, 2010 at 2:27 PM, Kirk Bailey wrote: > and continue in html. What a BLOODY shame we can't do that in python, > which is otherwise awesome and superior in every way i can thus far > evaluate. As someone mentioned, you CAN do that in Python. However, you'll find that mixing content and processing logic isn't considered a best practice. Embedding simple display logic into your presentational templates is fine (looping, if conditional then display this section, etc), but you'll want to avoid mixing complicated logic in with presentation - it makes maintenance/testing/reuse that much harder, even moreso when working on a team with different skill specialties. -- Brett Ritter / SwiftOne swiftone at swiftone.org From memilanuk at gmail.com Sat Dec 4 22:03:48 2010 From: memilanuk at gmail.com (Monte Milanuk) Date: Sat, 04 Dec 2010 13:03:48 -0800 Subject: [Tutor] printing in python 3.x In-Reply-To: References: Message-ID: Alan, Perhaps this is a silly question (and possibly not strictly python-related) but I was under the impression that controlling exactly layout via html was kind of difficult and somewhat fragile. The latter perhaps less so as one could make some fairly concrete assumptions about the paper size being used in this situation. Is it common to use HTML for formatting printed reports?!? Could you give an example of how you'd control the layout of specific fields on a printed report? Thanks, Monte From wescpy at gmail.com Sat Dec 4 23:02:46 2010 From: wescpy at gmail.com (wesley chun) Date: Sat, 4 Dec 2010 14:02:46 -0800 Subject: [Tutor] PHP In-Reply-To: References: <4CFA960E.4050108@howlermonkey.net> Message-ID: On Sat, Dec 4, 2010 at 12:44 PM, Brett Ritter wrote: > On Sat, Dec 4, 2010 at 2:27 PM, Kirk Bailey wrote: >> and continue in html. What a BLOODY shame we can't do that in python, >> which is otherwise awesome and superior in every way i can thus far >> evaluate. > > As someone mentioned, you CAN do that in Python. ?However, you'll find > that mixing content and processing logic isn't considered a best > practice. ?Embedding simple display logic into your presentational > templates is fine (looping, if conditional then display this section, > etc), but you'll want to avoid mixing complicated logic in with > presentation - it makes maintenance/testing/reuse that much harder, > even moreso when working on a team with different skill specialties. +1 on that point, which i failed to mention. :P separating the logic from the template adheres more the common best practice of using the MVC pattern. the template represents the "view," or the HTML template -- usually you want to keep this out of the hands of developers and give it to your designers (UX/UE/UI) folks. on the flip side, you want the controller (logic) to be the responsibility of the programmers. this segregation of duties enables for more agile development and creation of more accurate (and less confusing) unit-testing and faster and cleaner development overall. outside of the loops and conditionals mentioned, any real chunk of code embedded in a template i'd consider a special case or deployment issue (fewer approvals needed to push non-code, no need for server restart on static file pushes, etc.). cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Python Web Development with Django", Addison Wesley, (c) 2009 ? ? http://withdjango.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From jbiquez at icsmx.com Sat Dec 4 23:42:36 2010 From: jbiquez at icsmx.com (Jorge Biquez) Date: Sat, 04 Dec 2010 16:42:36 -0600 Subject: [Tutor] Which non SQL Database ? Message-ID: <201012042241.oB4Mfcfi079977@krusty.intranet.com.mx> Hello all. Newbie question. Sorry. As part of my process to learn python I am working on two personal applications. Both will do it fine with a simple structure of data stored in files. I now there are lot of databases around I can use but I would like to know yoor advice on what other options you would consider for the job (it is training so no pressure on performance). One application will run as a desktop one,under Windows, Linux, Macintosh, being able to update data, not much, not complex, not many records. The second application, running behind web pages, will do the same, I mean, process simple data, updating showing data. not much info, not complex. As an excersice it is more than enough I guess and will let me learn what I need for now. Talking with a friend about what he will do (he use C only) he suggest to take a look on dBase format file since it is a stable format, fast and the index structure will be fine or maybe go with BD (Berkley) database file format (I hope I understood this one correctly) . Plain files it is not an option since I would like to have option to do rapid searches. What would do you suggest to take a look? If possible available under the 3 plattforms. Thanks in advance for your comments. Jorge Biquez From swiftone at swiftone.org Sun Dec 5 00:27:59 2010 From: swiftone at swiftone.org (Brett Ritter) Date: Sat, 4 Dec 2010 18:27:59 -0500 Subject: [Tutor] Which non SQL Database ? In-Reply-To: <201012042241.oB4Mfcfi079977@krusty.intranet.com.mx> References: <201012042241.oB4Mfcfi079977@krusty.intranet.com.mx> Message-ID: On Sat, Dec 4, 2010 at 5:42 PM, Jorge Biquez wrote: > Newbie question. Sorry. If it isn't you're on the wrong list :) > training so no pressure on performance). One application will run as a > desktop one,under Windows, Linux, Macintosh, being able to update data, not > much, not complex, not many records. The important details here are: simple data, low-volume. I'm assuming this is single-user (as in, each instance of your application has it's own DB) > The second application, running behind > ?web pages, will do the same, Is this multiple users, each accessing the same DB? That really changes what you are looking for. If you are dealing with single-user, or only a few users, I'd say look into SQLite - It uses SQL syntax but doesn't run as a server and stores the database as a single file. It's great to use in small projects because the syntax is the same as larger projects, and you can replace with a full-blown multi-user SQL DB if you ever need to without having to rework everything. It's also very simple to use. I believe SQLite (sqlite3) is part of the core library in recent Python versions, or available as a package for older pythons. Berkeley DB is pretty much interchangeable with SQLite in terms of functionality. I much prefer SQLite. If your web application intends to have multiple users interacting with the same data, neither is probably a good fit. -- Brett Ritter / SwiftOne swiftone at swiftone.org From knacktus at googlemail.com Sun Dec 5 10:08:16 2010 From: knacktus at googlemail.com (Knacktus) Date: Sun, 05 Dec 2010 10:08:16 +0100 Subject: [Tutor] Which non SQL Database ? In-Reply-To: <201012042241.oB4Mfcfi079977@krusty.intranet.com.mx> References: <201012042241.oB4Mfcfi079977@krusty.intranet.com.mx> Message-ID: <4CFB5680.4000707@googlemail.com> Am 04.12.2010 23:42, schrieb Jorge Biquez: > Hello all. > > Newbie question. Sorry. > > As part of my process to learn python I am working on two personal > applications. Both will do it fine with a simple structure of data > stored in files. I now there are lot of databases around I can use but I > would like to know yoor advice on what other options you would consider > for the job (it is training so no pressure on performance). One > application will run as a desktop one,under Windows, Linux, Macintosh, > being able to update data, not much, not complex, not many records. The > second application, running behind web pages, will do the same, I mean, > process simple data, updating showing data. not much info, not complex. > As an excersice it is more than enough I guess and will let me learn > what I need for now. > Talking with a friend about what he will do (he use C only) he suggest > to take a look on dBase format file since it is a stable format, fast > and the index structure will be fine or maybe go with BD (Berkley) > database file format (I hope I understood this one correctly) . Plain > files it is not an option since I would like to have option to do rapid > searches. > > What would do you suggest to take a look? If possible available under > the 3 plattforms. As a non SQL Database I can recommend MongoDB. It's a document store, which uses binary json as format. You can create secondary indexes and also perform more sophisticated queris. The main advantage from my point of view is the ease of use. Installation was a breeze and it has an easy to use python driver. Note that you need a 64-bit OS to use it with bigger data volumes (I remember > 3 GB, but it's all on their website). > > Thanks in advance for your comments. > > Jorge Biquez > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Sun Dec 5 10:31:59 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 5 Dec 2010 09:31:59 -0000 Subject: [Tutor] printing in python 3.x References: Message-ID: "Monte Milanuk" wrote > ... I was under the impression that controlling exactly layout via > html was kind of difficult and somewhat fragile. Absolutely true, especially compared to PDF. But its much better than it was say 10-15 years ago. But if you can construct the page such that precision layout is not required that isn't too big a problem. > perhaps less so as one could make some fairly concrete assumptions > about the paper size being used in this situation. Is it common to > use HTML for formatting printed reports?!? Could you give an > example of how you'd control the layout of specific fields on a > printed report? Its not unknown. It depends on how precise it needs to be. But if you use tables extensively, with fixed widths etc you can get fairly good control. Also use CSS stylesheets to control fonts etc. HTML for printing is almost the opposite of HTML for browser display - you avoid all the flexible size settings etc and use fixed sixes and fonts. The great thing about HTML is that you can very quickly try things out manually before committing the effort of generating it by code. If you are in control of the printed output then you should be able to get precision good enough. If not then PDF may be your best bet. One option is to get the HTML working well then print to a PDF printer file. Then you can ship the PDF file with its precise layout but retain the ease of production of the HTML. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun Dec 5 10:41:25 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 5 Dec 2010 09:41:25 -0000 Subject: [Tutor] Which non SQL Database ? References: <201012042241.oB4Mfcfi079977@krusty.intranet.com.mx> Message-ID: "Jorge Biquez" wrote > Talking with a friend about what he will do (he use C only) he > suggest to take a look on dBase format file since it is a stable True enough and Dabo is worth a look since it provides not only a dbase format db engine but a good GUI builder tool too. However.... > (Berkley) database file format (I hope I understood this one > correctly) . Plain files it is not an option since I would like to > have option to do rapid searches. Why not use SQL? SQLlite comes with Python, is small, easy to use and if necessary can be used in-memory and as such fast. Once you have decided to go down the database route then not using SQL nowadays is a very odd decision. Its rather liike going out looking for a car that has solid types instead of pneumatics. Why would you want to? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From knacktus at googlemail.com Sun Dec 5 13:34:01 2010 From: knacktus at googlemail.com (Knacktus) Date: Sun, 05 Dec 2010 13:34:01 +0100 Subject: [Tutor] Which non SQL Database ? In-Reply-To: References: <201012042241.oB4Mfcfi079977@krusty.intranet.com.mx> Message-ID: <4CFB86B9.4050402@googlemail.com> Am 05.12.2010 10:41, schrieb Alan Gauld: > > "Jorge Biquez" wrote > >> Talking with a friend about what he will do (he use C only) he suggest >> to take a look on dBase format file since it is a stable > > True enough and Dabo is worth a look since it provides not only a dbase > format db engine but a good GUI builder tool too. > > However.... > >> (Berkley) database file format (I hope I understood this one >> correctly) . Plain files it is not an option since I would like to >> have option to do rapid searches. > > Why not use SQL? > SQLlite comes with Python, is small, easy to use and if necessary can be > used in-memory and as such fast. > Once you have decided to go down the database route then not using SQL > nowadays is a very odd decision. Its rather liike going out looking for > a car that has solid types instead of pneumatics. Why would you want to? The NoSQL databases seem to be en vogue. Some big internet companies have started to use them, like Google with its Big Table. That's how I stumbled across them. There're a lot of discussions going on on the internet which is better for which use case. There're some cases where one kind of the database type is the clear winner, but most use cases can be modelled with both. My conclusion and what draw me finally to MongoDB over PostgreSQL and SQLAlqemy was: 1) Really the ease of use (from setting up the database and later when it comes to administration and scaling, which is particularly easy). MongoDB is very useful for prototyping. 2) I don't need complex transactions and the ability to perform complex, not in advanced known queries. I'd say SQL-DBs are more sophisticated but more complex to use. For the sake of learning, go with SQL. It doesn't hurt having a driver license. If you need to go to the bakery around the corner the driver license doesn't prevent you to jump on your bicycle. Cheers, Jan > > HTH, > From alan.gauld at btinternet.com Sun Dec 5 17:00:52 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 5 Dec 2010 16:00:52 -0000 Subject: [Tutor] Which non SQL Database ? References: <201012042241.oB4Mfcfi079977@krusty.intranet.com.mx> <4CFB86B9.4050402@googlemail.com> Message-ID: "Knacktus" wrote >> Why not use SQL? >> SQLlite comes with Python, is small, easy to use and if necessary >> can be >> used in-memory and as such fast. > The NoSQL databases seem to be en vogue. Some big internet companies > have started to use them, like Google with its Big Table. There are cases where it makes sense but they are nearly all cases where extreme performance is required for a small fixed set of queries. In these cases the data structures can be optimised to match the queries and low level file access can provide optimum access speed. But unless you absolutely know you need that a lightweight SQL database like SQLlite or firebird will be easier to configure, easier to query and be much more scaleable and flexible. (Plus you get for free the ability to run ad-hoc MIS style reports of the back of the SQL engine.) > one kind of the database type is the clear winner, but most use > cases can be modelled with both. Yes, its like OOP v procedural, both can be used for any problem but usually OOP is is the more powerful, flexibly and easier option. But for specific, small or high performance apps procedural can be better. > My conclusion and what draw me finally to MongoDB over PostgreSQL > and SQLAlqemy was: I've no idea what MongoDB is like but PostGrres is one of the more complex (and powerful) SQL options. Certainly not my first chooice for a small, single user app. > 1) Really the ease of use (from setting up the database and later > when it comes to administration and scaling, which is particularly > easy). Most SQL databases come with GUI tools for all of that. > MongoDB is very useful for prototyping. > > 2) I don't need complex transactions and the ability to perform > complex, not in advanced known queries. Fixed in advance, simple, single dimensional queries are the area where non SQL can be a valid choice. Any kind of multi dimensional query will quickly become difficult to code and maintain, and thats where SQL wins. In my experience the majority of so-called "simple database" applications very quickly become complicated! Of course if all you need is data persistence then flat files are a perfectly valid option. But I'm assuming here that database implies off-disk data search and access. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From vinnievega007 at gmail.com Sun Dec 5 20:21:28 2010 From: vinnievega007 at gmail.com (Sean Finnegan) Date: Sun, 5 Dec 2010 14:21:28 -0500 Subject: [Tutor] Tkinter Scale Question Message-ID: Hi Everyone, I have successfully visualized 4 layers in Tkinter and am now trying to apply some sort of zoom in/out and pan function so that the layers can be seen more closely. I am using a package to visualize the map layers where i have separate py files for points, polylines, polygons, map, layers, readshapefile, feature, and features, and a main file that tells tkinter what to draw based on the bounding box settings in the map file. I am not exactly sure what path I should be exploring, either a button module or a scale module. I have successfully visualized both, but can not seem to make them take on the functionality that I need. Most recently I am trying some bind code in the main file that I have modified to try and fit the map needs, but it isn't producing the desired functionality either. Can someone please give me some direction as to which way would be best to approach this? from Tkinter import * from Map import * root = Tk() import sys if sys.version_info < (3,): import Tkinter as tk import tkSimpleDialog as tk_dialog else: import tkinter as tk from tkinter import simpledialog as tk_dialog class Viewer(object): #Base class viewer to display map def __init__(self, parent, width=800, height=600, min_x=-2.5, min_y=-1.5, max_x=1.): self.parent = parent self.canvas_width = width self.canvas_height = height # The following are drawing boundaries in the complex plane self.min_x = min_x self.min_y = min_y self.max_x = max_x self.calculate_pixel_size() self.max_y = self.min_y + self.canvas_height*self.pixel_size self.calculating = False self.nb_iterations = 20 self.normal_zoom(None) self.canvas = tk.Canvas(parent, width=width, height=height) self.canvas.pack() self.status = tk.Label(self.parent, text="", bd=1, relief=tk.SUNKEN, anchor=tk.W) self.status.pack(side=tk.BOTTOM, fill=tk.X) self.status2 = tk.Label(self.parent, text=self.info(), bd=1, relief=tk.SUNKEN, anchor=tk.W) self.status2.pack(side=tk.BOTTOM, fill=tk.X) # We change the size of the image using the keyboard. self.parent.bind("+", self.zoom_in) self.parent.bind("-", self.zoom_out) self.parent.bind("n", self.normal_zoom) self.parent.bind("b", self.bigger_zoom) # Set the maximum number of iterations via a keyboard-triggered event self.parent.bind("i", self.set_max_iter) # We move the canvas using the mouse. self.translation_line = None self.parent.bind("", self.mouse_down) self.parent.bind("", self.mouse_motion) self.parent.bind("", self.mouse_up) self.draw_map() # Needs to be implemented by subclass def info(self): #information about map location''' return "Location: (%f, %f) to (%f, %f)" %(self.min_x, self.min_y, self.max_x, self.max_y) def calculate_pixel_size(self): #Calculates the size of a (square) pixel in complex plane #coordinates based on the canvas_width. self.pixel_size = 1.*(self.max_x - self.min_x)/self.canvas_width return def mouse_down(self, event): #records the x and y positions of the mouse when the left button #is clicked. self.start_x = self.canvas.canvasx(event.x) self.start_y = self.canvas.canvasy(event.y) def mouse_motion(self, event): #keep track of the mouse motion by drawing a line from its #starting point to the current point. x = self.canvas.canvasx(event.x) y = self.canvas.canvasy(event.y) if (self.start_x != event.x) and (self.start_y != event.y) : self.canvas.delete(self.translation_line) self.translation_line = self.canvas.create_line( self.start_x, self.start_y, x, y, fill="orange") self.canvas.update_idletasks() def mouse_up(self, event): #Moves the canvas based on the mouse motion dx = (self.start_x - event.x)*self.pixel_size dy = (self.start_y - event.y)*self.pixel_size self.min_x += dx self.max_x += dx # y-coordinate in complex plane run in opposite direction from # screen coordinates self.min_y -= dy self.max_y -= dy self.canvas.delete(self.translation_line) self.status.config(text="Moving the map. Please wait.") self.status.update_idletasks() self.status2.config(text=self.info()) self.status2.update_idletasks() self.draw_map() def normal_zoom(self, event, scale=1): #Sets the zooming in/out scale to its normal value if scale==1: self.zoom_info = "[normal zoom]" else: self.zoom_info = "[faster zoom]" if event is not None: self.status.config(text=self.zoom_info) self.status.update_idletasks() self.zoom_in_scale = 0.1 self.zoom_out_scale = -0.125 def bigger_zoom(self, event): #Increases the zooming in/out scale from its normal value self.normal_zoom(event, scale=3) self.zoom_in_scale = 0.3 self.zoom_out_scale = -0.4 def zoom_in(self, event): #decreases the size of the region of the complex plane displayed if self.calculating: return self.status.config(text="Zooming in. Please wait.") self.status.update_idletasks() self.change_scale(self.zoom_in_scale) def zoom_out(self, event): #increases the size of the region of the complex plane displayed''' if self.calculating: return self.status.config(text="Zooming out. Please wait.") self.status.update_idletasks() self.change_scale(self.zoom_out_scale) def change_scale(self, scale): #changes the size of the region of the complex plane displayed and #redraws if self.calculating: return dx = (self.max_x - self.min_x)*scale dy = (self.max_y - self.min_y)*scale self.min_x += dx self.max_x -= dx self.min_y += dy self.max_y -= dy self.calculate_pixel_size() self.draw_map() def set_max_iter(self, event): #set maximum number of iterations i = tk_dialog.askinteger('title', 'prompt') if i is not None: self.nb_iterations = i self.status.config(text="Redrawing. Please wait.") self.status.update_idletasks() self.draw_map() def draw_map(self): #draws map on the canvas raise NotImplementedError map = Map(root, 800, 600) map.addLayer('States') map.addLayer('USInterstates') map.addLayer('Rivers') map.addLayer('RoadsRiversIntersect_points') map.vis() root.mainloop() #root.wait_window() Thank you, -Sean -------------- next part -------------- An HTML attachment was scrubbed... URL: From alans at risingrealty.com Mon Dec 6 04:55:19 2010 From: alans at risingrealty.com (Alan Stern) Date: Sun, 5 Dec 2010 21:55:19 -0600 Subject: [Tutor] role playing game - help needed Message-ID: Hi all. My name is Al Stern. I am an absolute beginner to programming and based on what I'd read, decided to try Python as my 1st language. I am starting with a book called Python Programming for the Absolute Beginner by Michael Dawson. The book has been pretty good and up to this point, I have grasped all the concepts it has covered. At the end of each chapter, there are a number of challenges you need to complete before moving on. Problem is, I have gotten stumped on one in Chapter 5: Lists and Dictionaries. I'm not sure exactly how this mailing list works but was hoping someone here could give me some insight as to how to proceed. Apologies if I am not posting the following in the correct format. I just copied and pasted.) Feel free to point me in the right direction if there are instructions as to how to do it. Here is the challenge: Write a Character Creator program for a role-playing game. The player should be given a pool of 30 points to spend on four attributes: Strength, Health, Wisdom, and Dexterity. The >player should be able to spend points from the pool on any attribute and should also be able to take points from an attribute and put them back into the pool. I think I did ok setting up the variables but haven't figured out where to go from there. Here is what I have (along with my latest error message). Thanks in advance. # character creator / role playing game # have 30 total points to work with # set variables attributes = ["strength", "health", "wisdom", "dexterity"] points = [0,0,0,0] MAX_POINTS = 30 available_points = MAX_POINTS - sum(points) print(""" Our hero will need strength, health, wisdom and dexterity to survive. You will have a limited amount of points to 'spend' on these attributes. Use them wisely. His life depends on it. """ ) # point allocation print ("You have", available_points, "points available to use.") for x in range(len(attributes)): print ("\t",attributes[x], points[x]) for x in range(len(attributes)): point_choice = input("\n\nHow many points do you want to assign to " + attributes[x] + "?: ") ______________________ Alan Stern Rising Realty 1820 W. Webster Ave #307 Chicago, IL 60614 (W)773-395-9999 (F)773-395-9553 (C)773-502-2556 www.risingrealty.com Statement of Confidentiality The contents of this e-mail message and its attachments are intended solely for the addressee(s) hereof. In addition, this e-mail transmission may be confidential and it may be subject to privilege protecting communications between attorneys or solicitors and their clients. If you are not the named addressee, or if this message has been addressed to you in error, you are directed not to read, disclose, reproduce, distribute, disseminate or otherwise use this transmission. Delivery of this message to any person other than the intended recipient(s) is not intended in any way to waive privilege or confidentiality. If you have received this transmission in error, please alert the sender by reply e-mail; we also request that you immediately delete this message and its attachments, if any. Rising Realty reserve the right to monitor all e-mail communications through their networks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Dec 6 09:36:47 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Dec 2010 08:36:47 -0000 Subject: [Tutor] role playing game - help needed References: Message-ID: "Alan Stern" wrote > Hi all. My name is Al Stern. Hi Al, > Chapter 5: Lists and Dictionaries. Note Lists AND Dictionaries.... > I'm not sure exactly how this mailing list works but > was hoping someone here could give me some insight You are doing just fine. The only extra info we could find helpful; are the Python version you use and the OS you run. For now, I'll assume Python v3 and Windows... > I think I did ok setting up the variables but haven't figured out > where to go from there. ... > Here is what I have (along with my latest error message). I don;t see any error messages? BTW. Please post complete error messages not just summaries. Notice the topic. You have Lists but I see no dictionaries. Think about how a dictionary works and how it could be useful here. > attributes = ["strength", "health", "wisdom", "dexterity"] > points = [0,0,0,0] You ave a list of attributes and a separate list of points. A dictionary holds a set of keys and associated values. Can you see a connection? MAX_POINTS = 30 available_points = MAX_POINTS - sum(points) print ("You have", available_points, "points available to use.") for x in range(len(attributes)): print ("\t",attributes[x], points[x]) for x in range(len(attributes)): point_choice = input("\n\nHow many points do you want to assign to " + attributes[x] + "?: ") A start would be to convert the result to an integer and then assign that to points[x]... Then modify available_points as needed. However using a dictionary will simplify things considerably. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From washakie at gmail.com Mon Dec 6 13:34:37 2010 From: washakie at gmail.com (John) Date: Mon, 6 Dec 2010 13:34:37 +0100 Subject: [Tutor] modified dictionary class Message-ID: Hello all, I have been using this class extensively in my modules / scripts. It provides the ability to .reference dictionary values. I find it handy, but am afraid it may come back to haunt me at some point. Is there anything wrong with using this? class Structure(dict): """ A 'fancy' dictionary. It provides 'MatLab' structure-like referencing. Could also just define a generic class. Caution: This may be deprecated in a future release. """ def __getattr__(self, attr): # Fake a __getstate__ method that resturns None if attr == "__getstate__": return lambda: None return self[attr] def __setattr__(self, attr, value): self[attr] = value def set_with_dict(self,D): """ set attributes with a dict """ for k in D.keys(): self.__setattr__(k,D[k]) From joel.goldstick at gmail.com Mon Dec 6 14:06:23 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 6 Dec 2010 08:06:23 -0500 Subject: [Tutor] modified dictionary class In-Reply-To: References: Message-ID: On Mon, Dec 6, 2010 at 7:34 AM, John wrote: > Hello all, > > I have been using this class extensively in my modules / scripts. It > provides the ability to .reference dictionary values. I find it handy, > but am afraid it may come back to haunt me at some point. Is there > anything wrong with using this? > > > class Structure(dict): > """ A 'fancy' dictionary. It provides 'MatLab' structure-like > referencing. Could also just define a generic class. > Caution: This may be deprecated in a future release. > """ > def __getattr__(self, attr): > # Fake a __getstate__ method that resturns None > if attr == "__getstate__": > return lambda: None > why not "return None"? > return self[attr] > def __setattr__(self, attr, value): > self[attr] = value > > def set_with_dict(self,D): > """ set attributes with a dict """ > for k in D.keys(): > self.__setattr__(k,D[k]) > _______________________________________________ > 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 Mon Dec 6 14:44:08 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Dec 2010 13:44:08 -0000 Subject: [Tutor] modified dictionary class References: Message-ID: "Joel Goldstick" wrote >> def __getattr__(self, attr): >> # Fake a __getstate__ method that resturns None >> if attr == "__getstate__": >> return lambda: None > > why not "return None"? Presumably the caller is expecting a function that he can call. The lambda is such a function, but one which always returns None. lambda: None is not the same as None -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From deshpande.jaidev at gmail.com Mon Dec 6 14:44:45 2010 From: deshpande.jaidev at gmail.com (Jaidev Deshpande) Date: Mon, 6 Dec 2010 19:14:45 +0530 Subject: [Tutor] Python vs. MATLAB Message-ID: Dear all What advantages does Python have over MATLAB as a programming language, (not the computing environment of MATLAB)? Also, wikipedia says Python is an interpreted language, what does that mean? -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Dec 6 14:47:20 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Dec 2010 13:47:20 -0000 Subject: [Tutor] modified dictionary class References: Message-ID: "Joel Goldstick" wrote >> def __getattr__(self, attr): >> # Fake a __getstate__ method that resturns None >> if attr == "__getstate__": >> return lambda: None > why not "return None"? > > >> return self[attr] Oops, too quick. I neant to add: But this line seems to return a value so the return values of this function seem to be incompatible which is not a good design pattern. If attr is getstate it returns a function object. Otherwise it returns whatever the dict holds - Unless the dict holds functions of course. Alan G. From robert.sjoblom at gmail.com Mon Dec 6 15:03:38 2010 From: robert.sjoblom at gmail.com (=?ISO-8859-1?Q?Robert_Sj=F6blom?=) Date: Mon, 6 Dec 2010 15:03:38 +0100 Subject: [Tutor] role playing game - help needed Message-ID: >I am starting with a book called Python Programming for the Absolute Beginner by Michael Dawson. ?The book has been >pretty good >and up to this point, I have grasped all the concepts it has covered. ?At the end of each chapter, there are a number of challenges you >need to complete before moving on. ?Problem is, I have gotten stumped on one in Chapter 5: Lists and Dictionaries. [snip] > attributes = ["strength", "health", "wisdom", "dexterity"] > points = [0,0,0,0] > MAX_POINTS = 30 > available_points = MAX_POINTS - sum(points) Before anyone comments that you can write a function for the points system, which was helpful to me when I asked a question regarding that very chapter: The book doesn't deal with functions until Chapter 6. As for your problem, Alan; I believe Alan already answered it -- you have two different lists, attributes and points, but a dictionary would be easier to handle. Not to say that it couldn't be done the way you're doing it though. A dictionary is built with key:value pairs. Then it's just about figuring out a way to change the value associated with each key when the attribute changes. I don't want to spoil the challenge of working it out yourself, but when I asked I was told to check out how values() and sum() worked. best regards, Robert S. From cfuller084 at thinkingplanet.net Mon Dec 6 15:22:18 2010 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Mon, 6 Dec 2010 08:22:18 -0600 Subject: [Tutor] Python vs. MATLAB In-Reply-To: References: Message-ID: <201012060822.19490.cfuller084@thinkingplanet.net> 1) Python is free! 2) Python indices start at 0! 3) Python has way better GUIs 4) Python can emulate a lot of Matlab functionality with matplotlib and numpy 5) Matlab has a rather limited number of data structures 6) Matlab doesn't have anything like the third party support for almost anything 7) Almost everything in Matlab is pass-by-value. This can get to be a bit of a pain at times. 8) Python is a general-purpose programming language, and as such, has a much wider set of features 9) There are a lot more Python developers to share code with and learn from It's free! Install it on all your computers. Take it home to use for work or play. Show it to your friends. Keep using it after you leave your place of work/study! The "interpreted" bit refers to the fact that the source code is not compiled before it is run. This is also true of Matlab. Cheers On Monday 06 December 2010, Jaidev Deshpande wrote: > Dear all > > What advantages does Python have over MATLAB as a programming language, > (not the computing environment of MATLAB)? > > Also, wikipedia says Python is an interpreted language, what does that > mean? From hugo.yoshi at gmail.com Mon Dec 6 15:37:52 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Mon, 6 Dec 2010 15:37:52 +0100 Subject: [Tutor] modified dictionary class In-Reply-To: References: Message-ID: On Mon, Dec 6, 2010 at 2:47 PM, Alan Gauld wrote: > Oops, too quick. I neant to add: > > But this line seems to return a value so the return values of this function > seem to be incompatible which is not a good design pattern. I agree it's not the clearest way to do this, but not because of incompatible return values. Attribute and Method access have the same syntax on objects, and __getattr__ is only called if the attribute isn't found "in the usual places," so it won't hide other methods. In other words: >>> a = Structure() >>> a["spam"] = 5 >>> a.__getstate__() None >>> a.spam 5 Doesn't look that strange to me, interface-wise. Attribute access sometimes returns a function and sometimes not. The comment specifically states it's faking a method, so it's pretty clear. I do have a few other gripes though: * why not just define __getstate__ in the class? why return a new lambda every time when you can just write "def __getstate__(self): return None" and be done with it? This also simplifies getattr. * it's now impossible to have keys that collide with a method name: dict, items, keys, clear, popitem, et cetera. You can set them without error, but when you try to get them you'll get the old method, which might cause hard-to-find bugs * it's not much of an advantage over regular dicts, really. You limit yourself to string keys, you make it very unclear that you're using a dict, it's not as efficient, and really just unpythonic in general. Why do this at all? To make python more Matlab-like? Hugo From albstern at gmail.com Mon Dec 6 16:05:52 2010 From: albstern at gmail.com (Al Stern) Date: Mon, 6 Dec 2010 09:05:52 -0600 Subject: [Tutor] role playing game - help needed In-Reply-To: References: Message-ID: <000001cb9557$13e15920$3ba40b60$@com> Thanks for the advice. I think I have the dictionary function set up right now although I'm still not clear why it is better than the list. attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} I think my next task is to set up a while function based on when available_points drops below 0. A little lost on how to do it though. -----Original Message----- From: tutor-bounces+alans=risingrealty.com at python.org [mailto:tutor-bounces+alans=risingrealty.com at python.org] On Behalf Of Robert Sj?blom Sent: Monday, December 06, 2010 8:04 AM To: tutor at python.org Subject: Re: [Tutor] role playing game - help needed >I am starting with a book called Python Programming for the Absolute Beginner by Michael Dawson. ?The book has been >pretty good >and up to this point, I have grasped all the concepts it has covered. ?At the end of each chapter, there are a number of challenges you >need to complete before moving on. ?Problem is, I have gotten stumped on one in Chapter 5: Lists and Dictionaries. [snip] > attributes = ["strength", "health", "wisdom", "dexterity"] > points = [0,0,0,0] > MAX_POINTS = 30 > available_points = MAX_POINTS - sum(points) Before anyone comments that you can write a function for the points system, which was helpful to me when I asked a question regarding that very chapter: The book doesn't deal with functions until Chapter 6. As for your problem, Alan; I believe Alan already answered it -- you have two different lists, attributes and points, but a dictionary would be easier to handle. Not to say that it couldn't be done the way you're doing it though. A dictionary is built with key:value pairs. Then it's just about figuring out a way to change the value associated with each key when the attribute changes. I don't want to spoil the challenge of working it out yourself, but when I asked I was told to check out how values() and sum() worked. best regards, Robert S. _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From washakie at gmail.com Mon Dec 6 16:32:20 2010 From: washakie at gmail.com (John) Date: Mon, 6 Dec 2010 16:32:20 +0100 Subject: [Tutor] modified dictionary class In-Reply-To: References: Message-ID: Hugo, Thank you for the response, it's very helpful. I posted a question earlier to this list (or possibly numpy) about how to create a 'matlab like structure'. This was early in my learning of Python, and yes, the intention was to have something that was similar to a matlab structure... now, I've been using Python much more, and I realize that this may be in fact 'un-Pythonic'. The problem is, I've used it extensively throughout my existing modules, and I have become somewhat comfortable with it... I guess an alternative would be to simply define a generic class, no? class Structure(object): pass Then, you could easily say: S = Structure() S.this = ['my slice of cheese'] and perhaps I would be being more 'pythonic'?? This I could quite easily do, as, where I have used this class, I almost never actually use the 'dictionary' like referencing... --john On Mon, Dec 6, 2010 at 3:37 PM, Hugo Arts wrote: > On Mon, Dec 6, 2010 at 2:47 PM, Alan Gauld wrote: >> Oops, too quick. I neant to add: >> >> But this line seems to return a value so the return values of this function >> seem to be incompatible which is not a good design pattern. > > I agree it's not the clearest way to do this, but not because of > incompatible return values. Attribute and Method access have the same > syntax on objects, and __getattr__ is only called if the attribute > isn't found "in the usual places," so it won't hide other methods. In > other words: > >>>> a = Structure() >>>> a["spam"] = 5 >>>> a.__getstate__() > None >>>> a.spam > 5 > > Doesn't look that strange to me, interface-wise. Attribute access > sometimes returns a function and sometimes not. The comment > specifically states it's faking a method, so it's pretty clear. > > I do have a few other gripes though: > > * why not just define __getstate__ in the class? why return a new > lambda every time when you can just write "def __getstate__(self): > return None" ?and be done with it? This also simplifies getattr. > > * it's now impossible to have keys that collide with a method name: > dict, items, keys, clear, popitem, et cetera. You can set them without > error, but when you try to get them you'll get the old method, which > might cause hard-to-find bugs > > * it's not much of an advantage over regular dicts, really. You limit > yourself to string keys, you make it very unclear that you're using a > dict, it's not as efficient, and really just unpythonic in general. > Why do this at all? To make python more Matlab-like? > > Hugo > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Configuration `````````````````````````` Plone 2.5.3-final, CMF-1.6.4, Zope (Zope 2.9.7-final, python 2.4.4, linux2), Python 2.6 PIL 1.1.6 Mailman 2.1.9 Postfix 2.4.5 Procmail v3.22 2001/09/10 Basemap: 1.0 Matplotlib: 1.0.0 From hugo.yoshi at gmail.com Mon Dec 6 16:37:19 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Mon, 6 Dec 2010 16:37:19 +0100 Subject: [Tutor] modified dictionary class In-Reply-To: References: Message-ID: On Mon, Dec 6, 2010 at 4:32 PM, John wrote: > Hugo, > > Thank you for the response, it's very helpful. I posted a question > earlier to this list (or possibly numpy) about how to create a 'matlab > like structure'. This was early in my learning of Python, and yes, the > intention was to have something that was similar to a matlab > structure... now, I've been using Python much more, and I realize that > this may be in fact 'un-Pythonic'. The problem is, I've used it > extensively throughout my existing modules, and I have become somewhat > comfortable with it... > > I guess an alternative would be to simply define a generic class, no? > > class Structure(object): > ? ?pass > > Then, you could easily say: > > S = Structure() > S.this = ['my slice of cheese'] > > and perhaps I would be being more 'pythonic'?? This I could quite > easily do, as, where I have used this class, I almost never actually > use the 'dictionary' like referencing... > This is better, and I believe it is even mentioned in the official tutorial. A problem with this is that you don't get a lot of the nice methods the dict class provides you with to iterate over its keys/values, find keys, and more such things. You *can* do it this way. But do you have a specific reason for *not* using dicts? Because that would be the idiomatic way. From joel at joelschwartz.com Mon Dec 6 18:09:27 2010 From: joel at joelschwartz.com (Joel Schwartz) Date: Mon, 6 Dec 2010 09:09:27 -0800 Subject: [Tutor] Python vs. MATLAB In-Reply-To: <201012060822.19490.cfuller084@thinkingplanet.net> References: <201012060822.19490.cfuller084@thinkingplanet.net> Message-ID: <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> Chris, Can you say more about number (7) in your list? What does "pass by value" mean and what are the alternatives? Thanks, Joel > -----Original Message----- > From: tutor-bounces+joel=joelschwartz.com at python.org > [mailto:tutor-bounces+joel=joelschwartz.com at python.org] On > Behalf Of Chris Fuller > Sent: Monday, December 06, 2010 6:22 AM > To: tutor at python.org > Subject: Re: [Tutor] Python vs. MATLAB > > 1) Python is free! > 2) Python indices start at 0! > 3) Python has way better GUIs > 4) Python can emulate a lot of Matlab functionality with > matplotlib and numpy > 5) Matlab has a rather limited number of data structures > 6) Matlab doesn't have anything like the third party support > for almost anything > 7) Almost everything in Matlab is pass-by-value. This can get > to be a bit of a > pain at times. > 8) Python is a general-purpose programming language, and as > such, has a much > wider set of features > 9) There are a lot more Python developers to share code with > and learn from > > It's free! Install it on all your computers. Take it home to > use for work or play. Show it to your friends. Keep using it > after you leave your place of work/study! > > The "interpreted" bit refers to the fact that the source code > is not compiled before it is run. This is also true of Matlab. > > > Cheers > > > On Monday 06 December 2010, Jaidev Deshpande wrote: > > Dear all > > > > What advantages does Python have over MATLAB as a > programming language, > > (not the computing environment of MATLAB)? > > > > Also, wikipedia says Python is an interpreted language, > what does that > > mean? > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From waynejwerner at gmail.com Mon Dec 6 18:53:45 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 6 Dec 2010 11:53:45 -0600 Subject: [Tutor] Python vs. MATLAB In-Reply-To: <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> References: <201012060822.19490.cfuller084@thinkingplanet.net> <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> Message-ID: On Mon, Dec 6, 2010 at 11:09 AM, Joel Schwartz wrote: > Chris, > > Can you say more about number (7) in your list? What does "pass by value" > mean and what are the alternatives? > Pass by value is exactly what it sounds like - you pass the value (a copy of everything in the memory). This is bad when you're passing a 10,000 item list to a function - because you now have *two* 10,000 item lists. It's even worse when you have many times that amount of data. Python, OTOH passes by reference - instead of copying the list, a pointer to the list is passed, so when you see something like this: def do_something(a_list): a_list[2] = 4 mylist = [1,2,3,4] do_something(mylist) now mylist is: [1,2,4,4]. This is much more efficient (although it tends to bite novice programmers!). HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Mon Dec 6 20:05:40 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Mon, 6 Dec 2010 20:05:40 +0100 Subject: [Tutor] Python vs. MATLAB In-Reply-To: <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> References: <201012060822.19490.cfuller084@thinkingplanet.net> <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> Message-ID: On Mon, Dec 6, 2010 at 6:09 PM, Joel Schwartz wrote: > Chris, > > Can you say more about number (7) in your list? What does "pass by value" > mean and what are the alternatives? > > Thanks, > Joel > Generally, pass-by-* refers to how the arguments to functions are treated. * In call-by-value, the value of the arguments are copied into the function. There is no way to modify variables outside of the function since you don't have access to them, only to copies. C uses this, among many others: int a = 5 void func(int b) { b = 6; } func(a); a == 5; /* evaluates to true, variable outside function scope remains unchanged */ The value b, inside the function, contains a copy of a. So when it is modified, the original a remains unchanged. * in call-by-reference, the function is given implicit *references* to its arguments. When modifying the variables inside of the function, the variable outside is also changed. you can simulate it in many languages by passing an expicit reference rather than an implicit one (such as C's pointers): int a = 5 void func(int * b) { *b = 6; } func(&a); a == 6; /* evaluates to true. the function was able to modify a variable outside of its scope */ * python uses something that wikipedia calls "call-by-sharing." It is not call-by-value, nor is it call-by-reference. It means, in short, that while the function has access to the callers, *values*, it does NOT have access to the callers *variables*. To demonstrate: a = [] def f(b): b.append(1) b = [2] f(a) print a # prints "[1]" As in pass-by-reference, the function f could modify it's callers values by appending 1 to the list. However, unlike *real* pass-by-reference, when trying to *re-assign* the variable into something entirely different, there was no effect (a did not become equal to [2]). Many people call python pass-by-reference, even though this is technically incorrect. The difference comes from the semantics of variables and values. In languages such as C, a variable is an area of memory that contains something. An assignment then, copies the value on the right into the variable (memory) on the left. python doesn't have variables, but names. a name is essentially itself a reference to some *object* that lives somewhere in memory. An assignment is something completely different in this context, it merely sets the reference (variable) on the left to *point to* the object on the right. So, when evaluating function arguments, names inside the function are set to point to the *objects* supplied as arguments, (not to names!). Since we don't have access to the caller's names, python is not a true pass-by-reference language. for more on why python is neither call-by-value nor call-by-reference: http://effbot.org/zone/call-by-object.htm for more on python's variable semantics and how it differs from languages like C: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables Hugo From alan.gauld at btinternet.com Mon Dec 6 20:16:06 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Dec 2010 19:16:06 -0000 Subject: [Tutor] role playing game - help needed References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: "Al Stern" wrote > Thanks for the advice. I think I have the dictionary function set > up right > now although I'm still not clear why it is better than the list. > > attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": > 0} Consider where you want to update the points for "health" Using two lists you need to loop over the keys list to find the index of "health" then access the values list to set the points. Something like this: for x in range(len(keys)): if keys[x] == "health": values[x] = newValue With a dictionary you just need to do attributes["health"] = newValue That's a lot less typing and will be faster performance too. I'd also say its a lot easier to see whats happening - its more readable. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Dec 6 20:22:22 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 6 Dec 2010 19:22:22 -0000 Subject: [Tutor] Python vs. MATLAB References: Message-ID: "Jaidev Deshpande" wrote > What advantages does Python have over MATLAB as a programming > language, Python is a general purpose programming language. Matlab is a mathematical modelling toool that happens to be programmable. You wouldn't sensibly use Matlab to create a web site, or an action game or a network analyzer, or a graphics editor, or.... You could use Python for any of these. But Matlab beats Python if you want to build standard math models. You can do those in Python, and arguably for very complex models Python is better. But for "simple" models as found in most engineering and science experiments Matlab will be easier to use. IMHO -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From cfuller084 at thinkingplanet.net Mon Dec 6 19:55:01 2010 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Mon, 6 Dec 2010 12:55:01 -0600 Subject: [Tutor] Python vs. MATLAB In-Reply-To: References: <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> Message-ID: <201012061255.04313.cfuller084@thinkingplanet.net> It also makes it hard to modify some data structure you are working with. I was experimenting with some object-oriented programming in Matlab, and you actually have to explicitly inherit from "handle" if you have any methods that modify internal state. That was a surprise! If you've got a data structure built out of arrays or cell arrays and you want to not make copies everytime you pass it into or out of a function, you pretty much have to wrap it in one of these classes. Another option is to use closures, with function handles, but that's pretty clunky also. Cheers On Monday 06 December 2010, Wayne Werner wrote: > On Mon, Dec 6, 2010 at 11:09 AM, Joel Schwartz wrote: > > Chris, > > > > Can you say more about number (7) in your list? What does "pass by value" > > mean and what are the alternatives? > > Pass by value is exactly what it sounds like - you pass the value (a copy > of everything in the memory). This is bad when you're passing a 10,000 > item list to a function - because you now have *two* 10,000 item lists. > It's even worse when you have many times that amount of data. > > Python, OTOH passes by reference - instead of copying the list, a pointer > to the list is passed, so when you see something like this: > > def do_something(a_list): > a_list[2] = 4 > > mylist = [1,2,3,4] > do_something(mylist) > > now mylist is: > > [1,2,4,4]. > > This is much more efficient (although it tends to bite novice > programmers!). > > HTH, > Wayne From susana.delgado_s at utzmg.edu.mx Mon Dec 6 21:20:15 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Mon, 6 Dec 2010 14:20:15 -0600 Subject: [Tutor] SAVE FILE IN A SPECIFIC DIRECTORY Message-ID: I'm trying to save files into a specific directory, the file will be generated from a python script. The script will look for some data stored in a directory which only allows to read files, it doesn't let the user write or modify information. But when I run the script I got the next error: Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import crawler_shp Traceback (most recent call last): File "", line 1, in File "crawler_shp.py", line 103, in a = open (filepath +t,"w+") IOError: [Errno 2] No such file or directory: 'C\\Python26\\BUFFER1000_bd.txt' >>> My script is: import os, time, socket, pylab,fnmatch from xlwt import Workbook from osgeo import ogr,gdal,osr from dbf import * gdal.AllRegister() file_list = [] folders = None for root, folders, files in os.walk( "R:\\" ): for filename in fnmatch.filter(files, '*.shp'): file_list.append(os.path.join(root, filename)) wrkbk = Workbook() wksht = wrkbk.add_sheet('shp') wksht.row(0).write(0,'ruta') wksht.row(0).write(1,'archivo') wksht.row(0).write(2,'estructura bd') for row, filepath in enumerate(file_list, start=1): wksht.row(row).write(0, filepath) (ruta, filename) = os.path.split(filepath) wksht.row(row).write(1, filename) f = os.path.splitext(filename) t = f[0]+'_bd.txt' d = n[0]+'.dbf' if os.path.lexists(d): filepath = "C\\Python26\\" a = open (filepath +t,"w+") dbf = Dbf(d,new=False) for fldName in dbf.fieldDefs: a.write(fldName.name) a.write(" || ") a.write(fldName.typeCode) a.write("\n") dbf.close() a.close() wksht.row(row).write(2, t) else: print "El archivo " +n[0]+".shp" " no tiene dbf" wksht.row(row).write(10, "Sin bd") wrkbk.save('C\\Python26\\biblio_shp.xls') -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Mon Dec 6 22:11:17 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 07 Dec 2010 08:11:17 +1100 Subject: [Tutor] Python vs. MATLAB In-Reply-To: <201012060822.19490.cfuller084@thinkingplanet.net> References: <201012060822.19490.cfuller084@thinkingplanet.net> Message-ID: <4CFD5175.8000107@pearwood.info> Chris Fuller wrote: > On Monday 06 December 2010, Jaidev Deshpande wrote: >> Also, wikipedia says Python is an interpreted language, what does that >> mean? > The "interpreted" bit refers to the fact that the source code is not compiled > before it is run. This is also true of Matlab. That's not completely correct. What do you think the compile() function in Python does, or what the "c" in .pyc files stands for? Strictly speaking, languages are neither interpreted or compiled, they just *are*. Implementations of languages are either interpreted or compiled. In Python's case, the three major implementations (CPython, Jython and IronPython) take the source code and compile it to byte-code rather than native machine code. Then the byte-code is executed by a virtual machine, rather than machine code executed directly by your CPU. This use of a virtual machine is much the same as what (e.g.) Java does. However, Java many years ago developed the ability to compile direct to native machine code, which is much faster to run. That step is considerably harder for Python. However, there is good progress in that direction: * Psyco is an old but still serviceable Just In Time compiler for 32-bit versions of CPython which can speed many (but not all) operations up greatly, by compiling them to native machine code at runtime. * Berp and HoPe are experimental implementations of Python written in Haskell instead of C. Berp translates the Python source code into Haskell, which can then be compiled to machine code. I'm not sure about HoPe. * UnPython is an experimental attempt to translate Python into C, which can then be compiled to machine code. * PyPy is an optimizing Python virtual machine written in Python itself, using JIT technology to produce faster code. PyPy now is about twice as fast as CPython, for typical programs, and the ambitious aim is to become an general-purpose optimizing engine that can produce code that runs faster than the equivalent written in C. -- Steven From __peter__ at web.de Tue Dec 7 00:10:31 2010 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Dec 2010 00:10:31 +0100 Subject: [Tutor] SAVE FILE IN A SPECIFIC DIRECTORY References: Message-ID: Susana Iraiis Delgado Rodriguez wrote: [UPPER CASE text is interpreted as shouting in emails and usenet posts. Please don't shout. Because spammers do it all the time it's more likely to make potential readers turn away from your writ rather than pay extra attention.] > I'm trying to save files into a specific directory, the file will be > generated from a python script. The script will look for some data stored > in a directory which only allows to read files, it doesn't let the user > write or modify information. But when I run the script I got the next > error: Python 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit > (Intel)] on > win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> import crawler_shp > Traceback (most recent call last): > File "", line 1, in > File "crawler_shp.py", line 103, in > a = open (filepath +t,"w+") > IOError: [Errno 2] No such file or directory: > 'C\\Python26\\BUFFER1000_bd.txt' Looks like you forgot a colon after the "C". Also note that C:\\Python26 is a bad place to store your own stuff. A general remark on your code: it looks like you spend quite some time on it, and the up-front time it takes to make it reader-friendly will save you and others a multiple in debugging cost. So please - Use 4-space indents - Choose descriptive names, not a, t, f, that even if understood in the context of an expression will be forgotten three lines below. Also 'wrksht' or 'wrkSht' have no advantage over 'worksheet'. Vowels are your friend ;) - Cut dead wood. Remove variables or imports that aren't currently used. You can always reintroduce them later on. - Bonus: split your code into functions. It may seem like extra work at first, but it makes it easy to test small chunks of simple code and then to build more complex scripts by combining these "known good" pieces. Peter From steve at pearwood.info Tue Dec 7 00:16:43 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 07 Dec 2010 10:16:43 +1100 Subject: [Tutor] Python vs. MATLAB In-Reply-To: <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> References: <201012060822.19490.cfuller084@thinkingplanet.net> <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> Message-ID: <4CFD6EDB.60202@pearwood.info> Joel Schwartz wrote: > Chris, > > Can you say more about number (7) in your list? What does "pass by value" > mean and what are the alternatives? Oh boy, is that a can of worms... and this is going to be a long post. You might want to go make yourself a coffee first :) Pass by whatever (also written as "call by ...") is one of those frustrating topics where people have added vast amounts of confusion where no confusion need exist. Take a variable, "x", and give it a value, say, 42. When you pass that variable to a function, func(x), what happens? Such a simple question, but you wouldn't believe how many angry words have been written about it. The problem is that there are a whole lot of answers to that question, used by many different programming languages, but most people are only familiar with *two*: pass by value, and pass by reference. This is particularly strange since most popular modern languages, like Ruby, Python and Java, don't use either of those! Nevertheless, people have got it in their head that there are only two calling conventions, and so they hammer the square peg of the language's actual behaviour until it will fit one or the other of the round holes in their mind. So there are huge flame wars about whether Python is pass by value or pass by reference, with some people wrongly claiming that Python is p-b-v for "simple" objects like numbers and strings and p-b-r for "complicated" objected like lists. This is nonsense. But that pales before the craziness of the Java community, that claims that Java is pass by value so long as you understand that that values being passed are references and not the value of the variable. But don't make the mistake of thinking that makes Java pass by reference! Pass by value-which-is-actually-a-reference is completely different from pass by reference. Only the Java people don't call it that, they just call it pass by value, even though Java's behaviour is different from pass by value in common languages like C, Pascal and Visual Basic. How is this helpful? Even if *technically* true, for some definition of "reference" and "value", it is gobbledygook. It's as helpful as claiming that every language ever written, without exception, is actually pass by flipping bits. No values are actually passed anywhere, it's all just flipping bits in memory. 100% true, and 100% useless. Translated into Python terms, the Java argument is this: Take a variable, call it "x". When you say x = 42, the value of x is not actually the number 42, like naive non-Java programmers might think, but some invisible reference to 42. Then when you call function(x), what gets passed to the function is not 42 itself (what Pascal or C programmers call "pass by value"), nor is it a reference to the *variable* "x" (which would be "pass by reference"), but the invisible reference to 42. Since this is the "true" value of x, Java is pass by value. (The situation is made more complicated because Java actually does pass ints like 5 by value, in the C or Pascal sense. The above description should be understood as referring to "boxed" integers, rather than unboxed. If this means nothing to you, be glad. All you need know is that in Java terms, all Python integers are boxed.) And note that in the Ruby community, they call the exact same behaviour "pass by reference". And then folks wonder why people get confused. All this because people insist on the false dichotomy that there are only two argument passing conventions, pass by value and pass by reference. But as this Wikipedia page shows, there are actually many more than that: http://en.wikipedia.org/wiki/Evaluation_strategy Let's go back to my earlier question. You have a variable x = 42, and you pass it to a function. What happens? In Pascal, or C, the compiler keeps a table mapping variable names to fixed memory addresses, like this: Variable Address ======== ======= x 10234 y 10238 z 10242 The command "x = 42" stuffs the value 42 into memory address 10234. Then, when you call func(x), the compiler looks up memory address 10234 and copies whatever it finds (in this case, 42) into another memory address (say, 27548), where func can see it. This is pass by value. What this means is the the variable x *inside* the function is not the same as the variable x *outside* the function. Inside the function, x has the address 27548, and the command "x = x + 1" will store 43 there, leaving the outside x at 10234 unchanged. This is normally a good thing. The classic test of pass by value is, does the value get copied when you pass it to a function? We can test Python to see if it copies values: >>> def func(arg): ... print(id(arg)) ... >>> x = 42 >>> print(id(x)) 135996112 >>> func(x) 135996112 The local variable arg and the global variable x have the same ID, which means they are the same object. This is conclusive proof that Python does not make a copy of x to pass to the function. So Python is not pass by value. Pass by value is nice and fast for values like 42, which are ints and therefore small. But what if x is (say) an array of a million numbers? Then the compiler has to copy all one million numbers, which is expensive, and your function will be slow. One alternative is pass by reference: instead of copying 42 to memory address 27548 (which is where func looks), the compiler can pass a reference to the *variable* x. That's as simple as passing 10234 instead. The compiler then treats that as the equivalent of "See here..." and follows that reference to get to the actual value wanted. Because addresses are small numbers, this is fast, but it means that the *local* variable and the *global* variable are, in fact, the same variable. This means that func can now operate on the variable x directly: if func uses call by reference, and func executes "x = x + 1", then the value 43 will be written into memory address 10234. Pascal and Visual Basic (and Perl, I think) have compiler support for pass by reference. In C, you have to fake it by hand by passing a pointer to the value, and then doing your own re-direction. Except for arrays, which are handled differently, to the confusion of all. The classic test of pass by reference is to write a "swap" function -- can you swap the value of two variables *without* returning them? In other words, something like this: a = 1 b = 2 swap(a, b) assert a == 2 and b == 1 In Python, you would swap two values like this: a, b = b, a but we want to do it inside a function. Doing this would be cheating: a, b = swap(a, b) because that explicitly re-assigns the variables a and b outside of the function. To be pass by reference, the swap must be done inside the function. There's no way of writing a general purpose swap function like this in Python. You can write a limited version: def swap(): global a, b a, b = b, a but that doesn't meet the conditions of the test: swap must take the variables to swap as arguments, and not hard-coded into the function. Python is not pass by value, because it doesn't make a copy of the value before passing it to the function. And it's not pass by reference, because it doesn't pass a reference to the variable itself: assignment inside the function doesn't effect the outer variable, only the inner variable (except in the limited case that you use the global statement). So Python is neither pass by value nor pass by reference. So what does Python actually do? Well, to start with Python doesn't have variables in the C or Pascal sense. There is no table of variable:address available to the compiler. Python's model is of *name binding*, not fixed memory addresses. So Python keeps a global dictionary of names and *objects*: {'x': , 'y': , 'z': , } The general name for this is "namespace". (Aside: you can access the global namespace with the globals() function. Don't mess with it unless you know what you're doing.) Functions have access to the global namespace, but they also get their own local namespace. You can access it with the locals() function. (Aside: as an optimization, CPython doesn't use a real dictionary for locals. Consequently, the dict returned by locals() is a copy, not the real thing, and you can't modify local variables by messing with locals(). Other Pythons may do differently.) So when you have this function: def func(arg): # do stuff in here... and then call func(x), Python initialises the function and creates a local namespace containing: ('arg': } No copy is made -- it is very fast to add the object to the namespace, regardless of how big or small the object is. (Implementation note: CPython does it by using pointers. Other Pythons may use different strategies, although it's hard to think of one which would be better.) So the local arg and the global x share the same value: 42. But if you do an assignment inside the function, say: arg += 1 43 will be stored in the local namespace, leaving the global x untouched. So far so good -- Python behaves like pass by value, when you assign to a local variable inside the function. But we've already seen it doesn't copy the value, so it isn't actually pass by value. This is where it gets interesting, and leads to people mistakingly thinking that Python is sometimes pass by value and sometimes pass by reference. Suppose you call func(z) instead, where z is a list. This time the local namespace will be: ('arg': } Now, instead of assigning to the name arg, suppose we modify it like so: arg.append(1) Naturally the list object [1,2,3] becomes [1,2,3,1]. But since the local arg and the global x are the same object, and not copies, both the local and the global list see the same change. Naturally, since they are one and the same object! So, for *immutable* objects that can't be modified in place, Python behaves superficially like pass by value (except it doesn't copy values) and for *mutable* objects that can be modified in place, Python behaves superficially like pass by reference (except you can't assign to the global variable, only the local). So Python's behaviour combines some behaviour of both pass by value and pass by reference, while being implemented differently from both. This strategy has been known as "pass by sharing" or "pass by object sharing" since 1974, when it was invented by Barbara Liskov. It is the same as what Ruby calls "pass by reference" and Java calls "pass by value", to the confusion of all. There is no need for this confusion except for the stubborn insistence that there are only two argument passing strategies. -- Steven From albstern at gmail.com Tue Dec 7 05:27:50 2010 From: albstern at gmail.com (Al Stern) Date: Mon, 6 Dec 2010 22:27:50 -0600 Subject: [Tutor] role playing game - help needed In-Reply-To: References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: Ok. I think I am starting to get it but still not sure how to separate the value from the key. Once I have this... attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} MAX_POINTS = 30 How do I set the variable for available_points? available_points = MAX_POINTS - (not sure what goes here) I am pretty sure I will change the values by something like this... attributes["strength"] = input("\nHow many points do you want to assign to strength?: ") Please let me know if this isn't advisable. It seems to work on the surface. On Mon, Dec 6, 2010 at 1:16 PM, Alan Gauld wrote: > > "Al Stern" wrote > > > Thanks for the advice. I think I have the dictionary function set up right >> now although I'm still not clear why it is better than the list. >> >> attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} >> > > Consider where you want to update the points for "health" > > Using two lists you need to loop over the keys list to find the index > of "health" then access the values list to set the points. Something > like this: > > for x in range(len(keys)): > if keys[x] == "health": > values[x] = newValue > > With a dictionary you just need to do > > attributes["health"] = newValue > > That's a lot less typing and will be faster performance too. > I'd also say its a lot easier to see whats happening - its more readable. > > HTH, > > -- > Alan Gauld > 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 alan.gauld at btinternet.com Tue Dec 7 10:44:57 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 7 Dec 2010 09:44:57 -0000 Subject: [Tutor] role playing game - help needed References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: "Al Stern" wrote > attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": > 0} > MAX_POINTS = 30 > > How do I set the variable for available_points? > > available_points = MAX_POINTS - (not sure what goes here) Check the mail from Robert Sjoblom, he gives you the necessary clues. You can check the archive a few weeks back(21st Nov) for his question too and get some alternative options and discussion. > attributes["strength"] = input("\nHow many points do you want to > assign to > strength?: ") > > Please let me know if this isn't advisable. It seems to work on the > surface. Close, but remember that input() returns a string. You need numbers so you need to convert strings to integers. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From robert.sjoblom at gmail.com Tue Dec 7 13:10:57 2010 From: robert.sjoblom at gmail.com (=?ISO-8859-1?Q?Robert_Sj=F6blom?=) Date: Tue, 7 Dec 2010 13:10:57 +0100 Subject: [Tutor] Python vs. MATLAB Message-ID: > Joel Schwartz wrote: >> Chris, >> >> Can you say more about number (7) in your list? What does "pass by value" >> mean and what are the alternatives? > > Oh boy, is that a can of worms... and this is going to be a long post. > You might want to go make yourself a coffee first :) [snipped wall of text] That was quite an interesting read, thanks for the lesson! best regards, Robert S. From robert.sjoblom at gmail.com Tue Dec 7 13:24:30 2010 From: robert.sjoblom at gmail.com (=?ISO-8859-1?Q?Robert_Sj=F6blom?=) Date: Tue, 7 Dec 2010 13:24:30 +0100 Subject: [Tutor] role playing game - help needed Message-ID: >> Thanks for the advice. I think I have the dictionary function set up right >>> now although I'm still not clear why it is better than the list. >>> >>> attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} >>> >> >> Consider where you want to update the points for "health" >> >> Using two lists you need to loop over the keys list to find the index >> of "health" then access the values list to set the points. Something >> like this: >> >> for x in range(len(keys)): >> if keys[x] == "health": >> values[x] = newValue >> >> With a dictionary you just need to do >> >> attributes["health"] = newValue >> >> That's a lot less typing and will be faster performance too. >> I'd also say its a lot easier to see whats happening - its more readable. [snip] > > Ok. ?I think I am starting to get it but still not sure how to separate the > value from the key. ?Once I have this... > > attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} > MAX_POINTS = 30 > > How do I set the variable for available_points? > > available_points = MAX_POINTS - (not sure what goes here) attributes.values() will give you a list of the values inside the attributes dictionary (remember, dictionaries work by key: value pairs). sum() won't work on a dictionary that contains both strings and integers, . If you only use integers in your values, the list will only contain integers, and so you can easily use sum() on it. > I am pretty sure I will change the values by something like this... > > attributes["strength"] = input("\nHow many points do you want to assign to > strength?: ") > > Please let me know if this isn't advisable. ?It seems to work on the > surface. That's how you do it. best regards, Robert S. From robert.sjoblom at gmail.com Tue Dec 7 13:37:23 2010 From: robert.sjoblom at gmail.com (=?ISO-8859-1?Q?Robert_Sj=F6blom?=) Date: Tue, 7 Dec 2010 13:37:23 +0100 Subject: [Tutor] role playing game - help needed Message-ID: >> attributes["strength"] = input("\nHow many points do you want to >> assign to >> strength?: ") >> >> Please let me know if this isn't advisable. ?It seems to work on the >> surface. > > Close, but remember that input() returns a string. You need numbers > so you need to convert strings to integers. Actually, input() only accept integers, consider the following: >>> input("input: ") input: d Traceback (most recent call last): File "", line 1, in input("input: ") File "", line 1, in NameError: name 'd' is not defined if you assign d to an integer, input() will accept it, however: >>> d = 7 >>> input("input: ") input: d 7 >>> input("input: ") input: 7 7 >>> help(input) Help on built-in function input in module __builtin__: input(...) input([prompt]) -> value Equivalent to eval(raw_input(prompt)). I've been told to use input() if I know that I'll only get integers, and raw_input() for "everything." Would you say it's better to use raw_input() for everything and convert as necessary? best regards, Robert S. From __peter__ at web.de Tue Dec 7 14:14:57 2010 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Dec 2010 14:14:57 +0100 Subject: [Tutor] role playing game - help needed References: Message-ID: Robert Sj?blom wrote: >> Close, but remember that input() returns a string. You need numbers >> so you need to convert strings to integers. > > Actually, input() only accept integers, consider the following: >>>> input("input: ") > input: d > > Traceback (most recent call last): > File "", line 1, in > input("input: ") > File "", line 1, in > NameError: name 'd' is not defined You are using Python 2.x where raw_input() was used to enter strings and input() behaved like eval(raw_input()) >From the above follows that input() in 2.x accepts arbitrary Python expressions: Python 2.6.4 (r264:75706, Dec 7 2009, 18:43:55) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d = 42 >>> input() d 42 >>> input() d**2 1764 >>> input() "*".join([str(d)]*5) '42*42*42*42*42' I think I drove the point home ;) input() in Python 3.x on the other hand is similar to 2.x's raw_input(): Python 3.1.1+ (r311:74480, Nov 2 2009, 15:45:00) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> input() d 'd' >>> input() "*".join([str(d)]*5) '"*".join([str(d)]*5)' Peter From albstern at gmail.com Tue Dec 7 16:35:40 2010 From: albstern at gmail.com (Al Stern) Date: Tue, 7 Dec 2010 09:35:40 -0600 Subject: [Tutor] role playing game - help needed In-Reply-To: References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: Apologies for all my questions. Up to this point I have been able to work out most of the challenges but I seem to have hit a wall. Can't seem to make any progress and completely frustrated. I looked at the 11/21 discussion. From the documentation, I realized I needed to set the variables to view the keys and values. Getting an error though. attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} MAX_POINTS = 30 keys = attributes.viewkeys() values = attributes.viewvalues() Traceback (most recent call last): File "C:\Users\Public\Documents\My Python programs\role_playing_game1.py", line 8, in keys = attributes.viewkeys() AttributeError: 'dict' object has no attribute 'viewkeys' On Tue, Dec 7, 2010 at 3:44 AM, Alan Gauld wrote: > > "Al Stern" wrote > > attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} >> MAX_POINTS = 30 >> >> How do I set the variable for available_points? >> >> available_points = MAX_POINTS - (not sure what goes here) >> > > Check the mail from Robert Sjoblom, he gives you the necessary clues. > You can check the archive a few weeks back(21st Nov) for his question > too and get some alternative options and discussion. > > > attributes["strength"] = input("\nHow many points do you want to assign to >> strength?: ") >> >> Please let me know if this isn't advisable. It seems to work on the >> surface. >> > > Close, but remember that input() returns a string. You need numbers > so you need to convert strings to integers. > > > HTH, > > > -- > Alan Gauld > 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 __peter__ at web.de Tue Dec 7 17:06:22 2010 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Dec 2010 17:06:22 +0100 Subject: [Tutor] role playing game - help needed References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: Al Stern wrote: > Apologies for all my questions. Up to this point I have been able to work > out most of the challenges but I seem to have hit a wall. Can't seem to > make any progress and completely frustrated. > > I looked at the 11/21 discussion. From the documentation, I realized I > needed to set the variables to view the keys and values. Getting an error > though. > > attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} > MAX_POINTS = 30 > keys = attributes.viewkeys() > values = attributes.viewvalues() > > Traceback (most recent call last): > File "C:\Users\Public\Documents\My Python > programs\role_playing_game1.py", > line 8, in > keys = attributes.viewkeys() > AttributeError: 'dict' object has no attribute 'viewkeys' The dictionary methods you are looking for are called keys() and values() not viewkeys() or viewvalues(). They do return view objects which may be causing the confusion. Have a look at the documentation at http://docs.python.org/dev/py3k/library/stdtypes.html#dictionary-view- objects which shows a simple example. By the way you, can use the interactive interpreter to find out what attributes an object has to offer: $ python3 Python 3.1.1+ (r311:74480, Nov 2 2009, 15:45:00) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d = {"a": 1, "b": 2} >>> dir(d) ['__class__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values'] Peter From joel at joelschwartz.com Tue Dec 7 17:43:08 2010 From: joel at joelschwartz.com (Joel Schwartz) Date: Tue, 7 Dec 2010 08:43:08 -0800 Subject: [Tutor] Python vs. MATLAB In-Reply-To: <4CFD6EDB.60202@pearwood.info> References: <201012060822.19490.cfuller084@thinkingplanet.net><00b101cb9568$579ebf70$6501a8c0@JLAPTOP> <4CFD6EDB.60202@pearwood.info> Message-ID: <02a401cb962d$d4160a20$6501a8c0@JLAPTOP> Steven, Thanks for taking the time to write such a detailed and illuminating response! I learned programming in Pascal in college in the early 1980s and used Fortran in grad school in the late 80s. That was pretty much the end of my contact with programming until I began learning R last year and now Python. About a paragraph into reading your response, something clunked into place in the dark recesses of my brain and suddenly "pass by value" and "pass by reference" started sounding familiar. But whatever I knew about these terms 20+ years ago was obviously quite simplistic when compared to where things stand now. Thanks again, Joel > -----Original Message----- > From: tutor-bounces+joel=joelschwartz.com at python.org > [mailto:tutor-bounces+joel=joelschwartz.com at python.org] On > Behalf Of Steven D'Aprano > Sent: Monday, December 06, 2010 3:17 PM > To: tutor at python.org > Subject: Re: [Tutor] Python vs. MATLAB > > Joel Schwartz wrote: > > Chris, > > > > Can you say more about number (7) in your list? What does > "pass by value" > > mean and what are the alternatives? > > Oh boy, is that a can of worms... and this is going to be a > long post. > You might want to go make yourself a coffee first :) > > Pass by whatever (also written as "call by ...") is one of > those frustrating topics where people have added vast amounts > of confusion where no confusion need exist. > > Take a variable, "x", and give it a value, say, 42. When you > pass that variable to a function, func(x), what happens? > > Such a simple question, but you wouldn't believe how many > angry words have been written about it. > > The problem is that there are a whole lot of answers to that > question, used by many different programming languages, but > most people are only familiar with *two*: pass by value, and > pass by reference. This is particularly strange since most > popular modern languages, like Ruby, Python and Java, don't > use either of those! Nevertheless, people have got it in > their head that there are only two calling conventions, and > so they hammer the square peg of the language's actual > behaviour until it will fit one or the other of the round > holes in their mind. > > So there are huge flame wars about whether Python is pass by > value or pass by reference, with some people wrongly claiming > that Python is p-b-v for "simple" objects like numbers and > strings and p-b-r for "complicated" objected like lists. This > is nonsense. > > But that pales before the craziness of the Java community, > that claims that Java is pass by value so long as you > understand that that values being passed are references and > not the value of the variable. But don't make the mistake of > thinking that makes Java pass by reference! Pass by > value-which-is-actually-a-reference is completely different > from pass by reference. Only the Java people don't call it > that, they just call it pass by value, even though Java's > behaviour is different from pass by value in common languages > like C, Pascal and Visual Basic. > > How is this helpful? Even if *technically* true, for some > definition of "reference" and "value", it is gobbledygook. > It's as helpful as claiming that every language ever written, > without exception, is actually pass by flipping bits. No > values are actually passed anywhere, it's all just flipping > bits in memory. > > 100% true, and 100% useless. > > Translated into Python terms, the Java argument is this: > > Take a variable, call it "x". When you say x = 42, the value > of x is not actually the number 42, like naive non-Java > programmers might think, but some invisible reference to 42. > Then when you call function(x), what gets passed to the > function is not 42 itself (what Pascal or C programmers call > "pass by value"), nor is it a reference to the > *variable* "x" (which would be "pass by reference"), but the > invisible reference to 42. Since this is the "true" value of > x, Java is pass by value. > > (The situation is made more complicated because Java actually > does pass ints like 5 by value, in the C or Pascal sense. The > above description should be understood as referring to > "boxed" integers, rather than unboxed. If this means nothing > to you, be glad. All you need know is that in Java terms, all > Python integers are boxed.) > > And note that in the Ruby community, they call the exact same > behaviour "pass by reference". And then folks wonder why > people get confused. > > All this because people insist on the false dichotomy that > there are only two argument passing conventions, pass by > value and pass by reference. But as this Wikipedia page > shows, there are actually many more than that: > > http://en.wikipedia.org/wiki/Evaluation_strategy > > > Let's go back to my earlier question. You have a variable x = > 42, and you pass it to a function. What happens? > > In Pascal, or C, the compiler keeps a table mapping variable > names to fixed memory addresses, like this: > > Variable Address > ======== ======= > x 10234 > y 10238 > z 10242 > > The command "x = 42" stuffs the value 42 into memory address 10234. > Then, when you call func(x), the compiler looks up memory > address 10234 and copies whatever it finds (in this case, 42) > into another memory address (say, 27548), where func can see > it. This is pass by value. > > What this means is the the variable x *inside* the function > is not the same as the variable x *outside* the function. > Inside the function, x has the address 27548, and the command > "x = x + 1" will store 43 there, leaving the outside x at > 10234 unchanged. This is normally a good thing. > > The classic test of pass by value is, does the value get > copied when you pass it to a function? We can test Python to > see if it copies values: > > >>> def func(arg): > ... print(id(arg)) > ... > >>> x = 42 > >>> print(id(x)) > 135996112 > >>> func(x) > 135996112 > > The local variable arg and the global variable x have the > same ID, which means they are the same object. This is > conclusive proof that Python does not make a copy of x to > pass to the function. So Python is not pass by value. > > Pass by value is nice and fast for values like 42, which are > ints and therefore small. But what if x is (say) an array of > a million numbers? > Then the compiler has to copy all one million numbers, which > is expensive, and your function will be slow. > > One alternative is pass by reference: instead of copying 42 > to memory address 27548 (which is where func looks), the > compiler can pass a reference to the *variable* x. That's as > simple as passing 10234 instead. The compiler then treats > that as the equivalent of "See here..." and follows that > reference to get to the actual value wanted. > Because addresses are small numbers, this is fast, but it > means that the > *local* variable and the *global* variable are, in fact, the > same variable. This means that func can now operate on the variable x > directly: if func uses call by reference, and func executes > "x = x + 1", then the value 43 will be written into memory > address 10234. > > Pascal and Visual Basic (and Perl, I think) have compiler > support for pass by reference. In C, you have to fake it by > hand by passing a pointer to the value, and then doing your > own re-direction. Except for arrays, which are handled > differently, to the confusion of all. > > The classic test of pass by reference is to write a "swap" > function -- can you swap the value of two variables *without* > returning them? In other words, something like this: > > a = 1 > b = 2 > swap(a, b) > assert a == 2 and b == 1 > > In Python, you would swap two values like this: > a, b = b, a > > but we want to do it inside a function. Doing this would be cheating: > > a, b = swap(a, b) > > because that explicitly re-assigns the variables a and b > outside of the function. To be pass by reference, the swap > must be done inside the function. > > There's no way of writing a general purpose swap function > like this in Python. You can write a limited version: > > def swap(): > global a, b > a, b = b, a > > but that doesn't meet the conditions of the test: swap must > take the variables to swap as arguments, and not hard-coded > into the function. > > Python is not pass by value, because it doesn't make a copy > of the value before passing it to the function. And it's not > pass by reference, because it doesn't pass a reference to the > variable itself: assignment inside the function doesn't > effect the outer variable, only the inner variable (except in > the limited case that you use the global statement). > So Python is neither pass by value nor pass by reference. > > So what does Python actually do? > > Well, to start with Python doesn't have variables in the C or > Pascal sense. There is no table of variable:address available > to the compiler. > Python's model is of *name binding*, not fixed memory > addresses. So Python keeps a global dictionary of names and *objects*: > > {'x': , > 'y': , > 'z': , > } > > The general name for this is "namespace". > > (Aside: you can access the global namespace with the > globals() function. > Don't mess with it unless you know what you're doing.) > > Functions have access to the global namespace, but they also > get their own local namespace. You can access it with the > locals() function. > > (Aside: as an optimization, CPython doesn't use a real > dictionary for locals. Consequently, the dict returned by > locals() is a copy, not the real thing, and you can't modify > local variables by messing with locals(). Other Pythons may > do differently.) > > So when you have this function: > > def func(arg): > # do stuff in here... > > and then call func(x), Python initialises the function and > creates a local namespace containing: > > ('arg': } > > No copy is made -- it is very fast to add the object to the > namespace, regardless of how big or small the object is. > (Implementation note: > CPython does it by using pointers. Other Pythons may use > different strategies, although it's hard to think of one > which would be better.) So the local arg and the global x > share the same value: 42. But if you do an assignment inside > the function, say: > > arg += 1 > > 43 will be stored in the local namespace, leaving the global > x untouched. > > So far so good -- Python behaves like pass by value, when you > assign to a local variable inside the function. But we've > already seen it doesn't copy the value, so it isn't actually > pass by value. > > This is where it gets interesting, and leads to people > mistakingly thinking that Python is sometimes pass by value > and sometimes pass by reference. Suppose you call func(z) > instead, where z is a list. This time the local namespace will be: > > ('arg': } > > Now, instead of assigning to the name arg, suppose we modify > it like so: > > arg.append(1) > > Naturally the list object [1,2,3] becomes [1,2,3,1]. But > since the local arg and the global x are the same object, and > not copies, both the local and the global list see the same > change. Naturally, since they are one and the same object! > > So, for *immutable* objects that can't be modified in place, > Python behaves superficially like pass by value (except it > doesn't copy values) and for *mutable* objects that can be > modified in place, Python behaves superficially like pass by > reference (except you can't assign to the global variable, > only the local). So Python's behaviour combines some > behaviour of both pass by value and pass by reference, while > being implemented differently from both. > > This strategy has been known as "pass by sharing" or "pass by > object sharing" since 1974, when it was invented by Barbara > Liskov. It is the same as what Ruby calls "pass by reference" > and Java calls "pass by value", to the confusion of all. > There is no need for this confusion except for the stubborn > insistence that there are only two argument passing strategies. > > > > -- > Steven > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From susana.delgado_s at utzmg.edu.mx Tue Dec 7 18:15:13 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Tue, 7 Dec 2010 11:15:13 -0600 Subject: [Tutor] Save file in a specific directory Message-ID: I make a script to redirect a txt file from an external directory, but in this directory I don't have permission to write, just to read data. So I make this module: import os, time,fnmatch from xlwt import Workbook from osgeo import ogr,gdal,osr from dbf import * gdal.AllRegister() file_list = [] folders = None for root, folders, files in os.walk( "R:\\" ): for filename in fnmatch.filter(files, '*.shp'): file_list.append(os.path.join(root, filename)) wrkbk = Workbook() wksht = wrkbk.add_sheet('shp') wksht.row(0).write(0,'ruta') wksht.row(0).write(1,'archivo') wksht.row(0).write(2,'estructura bd') for row, filepath in enumerate(file_list, start=1): wksht.row(row).write(0, filepath) (ruta, filename) = os.path.split(filepath) wksht.row(row).write(1, filename) f = os.path.splitext(filename) t = f[0]+'_bd.txt' d = n[0]+'.dbf' if os.path.lexists(d): filepath = "C:\\Python26\\" a = open (filepath +t,"w+") dbf = Dbf(d,new=False) for fldName in dbf.fieldDefs: a.write(fldName.name) a.write(" || ") a.write(fldName.typeCode) a.write("\n") dbf.close() a.close() wksht.row(row).write(2, t) else: print "El archivo " +n[0]+".shp" " no tiene dbf" wksht.row(row).write(10, "Sin bd") wrkbk.save('C\\Python26\\biblio_shp.xls') -------------- next part -------------- An HTML attachment was scrubbed... URL: From susana.delgado_s at utzmg.edu.mx Tue Dec 7 18:20:12 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Tue, 7 Dec 2010 11:20:12 -0600 Subject: [Tutor] Save file in a specific directory In-Reply-To: References: Message-ID: My other message was incomplete, it was a mistake: This is the correct one 2010/12/7 Susana Iraiis Delgado Rodriguez > I make a script to redirect a txt file from an external directory, but in > this directory I don't have permission to write, just to read data. So I > make this module: > import os, time,fnmatch > from xlwt import Workbook > from osgeo import ogr,gdal,osr > from dbf import * > gdal.AllRegister() > file_list = [] > folders = None > for root, folders, files in os.walk( "R:\\" ): > for filename in fnmatch.filter(files, '*.shp'): > file_list.append(os.path.join(root, filename)) > wrkbk = Workbook() > wksht = wrkbk.add_sheet('shp') > wksht.row(0).write(0,'ruta') > wksht.row(0).write(1,'archivo') > wksht.row(0).write(2,'estructura bd') > for row, filepath in enumerate(file_list, start=1): > wksht.row(row).write(0, filepath) > (ruta, filename) = os.path.split(filepath) > wksht.row(row).write(1, filename) > f = os.path.splitext(filename) > t = f[0]+'_bd.txt' > d = n[0]+'.dbf' > if os.path.lexists(d): > filepath = "C:\\Python26\\" > a = open (filepath +t,"w+") > dbf = Dbf(d,new=False) > for fldName in dbf.fieldDefs: > a.write(fldName.name) > a.write(" || ") > a.write(fldName.typeCode) > a.write("\n") > dbf.close() > a.close() > wksht.row(row).write(2, t) > else: > print "El archivo " +n[0]+".shp" " no tiene dbf" > wksht.row(row).write(10, "Sin bd" > wrkbk.save('C:\\Python26\\biblio_shp.xls') > When I run the script I got the next error: >>> import crawler_shp Traceback (most recent call last): File "", line 1, in File "crawler_shp.py", line 105, in dbf = Dbf(d,new=False) File "C:\Python26\lib\site-packages\dbf.py", line 125, in __init__ self.stream = file(f, ("r+b", "rb")[bool(readOnly)]) IOError: [Errno 13] Permission denied: 'R:\\Aplicaciones\\IRISv3\\mis proyectos\ \HURACAN\\BUFFER1000.dbf' The error is pointing to a library I used to make the script run: dbf.py. in thid lines: if isinstance(f, basestring): # a filename self.name = f if new: # new table (table file must be # created or opened and truncated) self.stream = file(f, "w+b") else: # tabe file must exist self.stream = file(f, ("r+b", "rb")[bool(readOnly)]) > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sander.sweers at gmail.com Tue Dec 7 18:53:01 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Tue, 7 Dec 2010 18:53:01 +0100 Subject: [Tutor] Python vs. MATLAB In-Reply-To: <4CFD6EDB.60202@pearwood.info> References: <201012060822.19490.cfuller084@thinkingplanet.net> <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> <4CFD6EDB.60202@pearwood.info> Message-ID: On 7 December 2010 00:16, Steven D'Aprano wrote: > Oh boy, is that a can of worms... and this is going to be a long post. You > might want to go make yourself a coffee first :) Great writeup and much appreciated :-). Thx Sander From joel.goldstick at gmail.com Tue Dec 7 19:47:30 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 7 Dec 2010 13:47:30 -0500 Subject: [Tutor] Save file in a specific directory In-Reply-To: References: Message-ID: On Tue, Dec 7, 2010 at 12:20 PM, Susana Iraiis Delgado Rodriguez < susana.delgado_s at utzmg.edu.mx> wrote: > My other message was incomplete, it was a mistake: This is the correct one > > 2010/12/7 Susana Iraiis Delgado Rodriguez > > I make a script to redirect a txt file from an external directory, but in >> this directory I don't have permission to write, just to read data. So I >> make this module: >> import os, time,fnmatch >> from xlwt import Workbook >> from osgeo import ogr,gdal,osr >> from dbf import * >> gdal.AllRegister() >> file_list = [] >> folders = None >> for root, folders, files in os.walk( "R:\\" ): >> for filename in fnmatch.filter(files, '*.shp'): >> file_list.append(os.path.join(root, filename)) >> wrkbk = Workbook() >> wksht = wrkbk.add_sheet('shp') >> wksht.row(0).write(0,'ruta') >> wksht.row(0).write(1,'archivo') >> wksht.row(0).write(2,'estructura bd') >> for row, filepath in enumerate(file_list, start=1): >> wksht.row(row).write(0, filepath) >> (ruta, filename) = os.path.split(filepath) >> wksht.row(row).write(1, filename) >> f = os.path.splitext(filename) >> t = f[0]+'_bd.txt' >> d = n[0]+'.dbf' >> if os.path.lexists(d): >> filepath = "C:\\Python26\\" >> a = open (filepath +t,"w+") >> dbf = Dbf(d,new=False) >> for fldName in dbf.fieldDefs: >> a.write(fldName.name) >> a.write(" || ") >> a.write(fldName.typeCode) >> a.write("\n") >> dbf.close() >> a.close() >> wksht.row(row).write(2, t) >> else: >> print "El archivo " +n[0]+".shp" " no tiene dbf" >> wksht.row(row).write(10, "Sin bd" >> wrkbk.save('C:\\Python26\\biblio_shp.xls') >> > When I run the script I got the next error: > >>> import crawler_shp > Traceback (most recent call last): > File "", line 1, in > File "crawler_shp.py", line 105, in > > dbf = Dbf(d,new=False) > File "C:\Python26\lib\site-packages\dbf.py", line 125, in __init__ > self.stream = file(f, ("r+b", "rb")[bool(readOnly)]) > IOError: [Errno 13] Permission denied: 'R:\\Aplicaciones\\IRISv3\\mis > proyectos\ > \HURACAN\\BUFFER1000.dbf' > > The error is pointing to a library I used to make the script run: dbf.py. > in thid lines: > if isinstance(f, basestring): > # a filename > self.name = f > if new: > # new table (table file must be > # created or opened and truncated) > self.stream = file(f, "w+b") > else: > # tabe file must exist > self.stream = file(f, ("r+b", "rb")[bool(readOnly)]) > > >> >> > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Do you have write privilege in the directory where you want to create the file? -- Joel Goldstick -------------- next part -------------- An HTML attachment was scrubbed... URL: From malaclypse2 at gmail.com Tue Dec 7 19:51:59 2010 From: malaclypse2 at gmail.com (Jerry Hill) Date: Tue, 7 Dec 2010 13:51:59 -0500 Subject: [Tutor] Save file in a specific directory In-Reply-To: References: Message-ID: > When I run the script I got the next error: >>>> import crawler_shp > Traceback (most recent call last): > ? File "", line 1, in > ? File "crawler_shp.py", line 105, in > ??? dbf = Dbf(d,new=False) > ? File "C:\Python26\lib\site-packages\dbf.py", line 125, in __init__ > ??? self.stream = file(f, ("r+b", "rb")[bool(readOnly)]) > IOError: [Errno 13] Permission denied: 'R:\\Aplicaciones\\IRISv3\\mis > proyectos\ > \HURACAN\\BUFFER1000.dbf' > > The error is pointing to a library I used to make the script run: dbf.py. in > thid lines: > if isinstance(f, basestring): > ??????????? # a filename > ??????????? self.name = f > ??????????? if new: > ??????????????? # new table (table file must be > ??????????????? # created or opened and truncated) > ??????????????? self.stream = file(f, "w+b") > ??????????? else: > ??????????????? # tabe file must exist > ??????????????? self.stream = file(f, ("r+b", "rb")[bool(readOnly)]) Sounds like you need to change your dbf object to something like this: dbf = Dbf(d,new=False, readOnly=True) Note that I don't know anything about the Dbf module, other than what you just posted. If you really have read permission, but not write permissions, this should do what you want though. -- Jerry From albstern at gmail.com Tue Dec 7 23:10:10 2010 From: albstern at gmail.com (Al Stern) Date: Tue, 7 Dec 2010 16:10:10 -0600 Subject: [Tutor] role playing game - help needed In-Reply-To: References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: Tried to use the documentation but still getting the errors... The 1st one has to do with the available_points # set variables attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} MAX_POINTS = 30 available_points = MAX_POINTS - attributes.values() keys = attributes.keys() values = attributes.values() this is the error i'm getting... Traceback (most recent call last): File "C:\Users\Public\Documents\My Python programs\role_playing_game1.py", line 8, in available_points = MAX_POINTS - attributes.values() TypeError: unsupported operand type(s) for -: 'int' and 'dict_values' I know using attributes.values here isn't correct but I can't figure out how to put the sum of the values into that equation. I looked up this part f the docs... http://docs.python.org/py3k/library/stdtypes.html?highlight=values#dict.values and tried to copy the format into my program. I am attempting to get the total of the values of everything in my dictionary. Not sure what is different between my 'attributes' dictionary and the 'dishes' dictionary they use. I used the following code and got the following error. attributes["strength"] = input("\nHow many points do you want to assign to strength?: ") #point allocation point_total = 0 for val in values: point_total += val print (point_total) and get this error... Traceback (most recent call last): File "C:\Users\Public\Documents\My Python programs\role_playing_game1.py", line 26, in point_total += val TypeError: unsupported operand type(s) for +=: 'int' and 'str' On Tue, Dec 7, 2010 at 10:06 AM, Peter Otten <__peter__ at web.de> wrote: > Al Stern wrote: > > > Apologies for all my questions. Up to this point I have been able to > work > > out most of the challenges but I seem to have hit a wall. Can't seem to > > make any progress and completely frustrated. > > > > I looked at the 11/21 discussion. From the documentation, I realized I > > needed to set the variables to view the keys and values. Getting an > error > > though. > > > > attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} > > MAX_POINTS = 30 > > keys = attributes.viewkeys() > > values = attributes.viewvalues() > > > > Traceback (most recent call last): > > File "C:\Users\Public\Documents\My Python > > programs\role_playing_game1.py", > > line 8, in > > keys = attributes.viewkeys() > > AttributeError: 'dict' object has no attribute 'viewkeys' > > The dictionary methods you are looking for are called keys() and values() > not viewkeys() or viewvalues(). They do return view objects which may be > causing the confusion. Have a look at the documentation at > > http://docs.python.org/dev/py3k/library/stdtypes.html#dictionary-view- > objects > > which shows a simple example. > By the way you, can use the interactive interpreter to find out what > attributes an object has to offer: > > $ python3 > Python 3.1.1+ (r311:74480, Nov 2 2009, 15:45:00) > [GCC 4.4.1] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> d = {"a": 1, "b": 2} > >>> dir(d) > ['__class__', '__contains__', '__delattr__', '__delitem__', '__doc__', > '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', > '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', > '__lt__', > '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', > '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', > 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', > 'setdefault', 'update', 'values'] > > Peter > > _______________________________________________ > 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 adam.jtm30 at gmail.com Tue Dec 7 23:19:00 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Tue, 07 Dec 2010 22:19:00 +0000 Subject: [Tutor] role playing game - help needed In-Reply-To: References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: <4CFEB2D4.9060201@gmail.com> On 07/12/10 22:10, Al Stern wrote: > Tried to use the documentation but still getting the errors... > The 1st one has to do with the available_points > # set variables > attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} > MAX_POINTS = 30 > available_points = MAX_POINTS - attributes.values() > keys = attributes.keys() > values = attributes.values() > this is the error i'm getting... > Traceback (most recent call last): > File "C:\Users\Public\Documents\My Python > programs\role_playing_game1.py", line 8, in > available_points = MAX_POINTS - attributes.values() > TypeError: unsupported operand type(s) for -: 'int' and 'dict_values' > I know using attributes.values here isn't correct but I can't figure > out how to put the sum of the values into that equation. > Using attributes.values is fine. There is a built in function for summing all the values in a sequence, it's called "sum". HTH Adam. From washakie at gmail.com Tue Dec 7 23:36:37 2010 From: washakie at gmail.com (John) Date: Tue, 7 Dec 2010 23:36:37 +0100 Subject: [Tutor] calling a method within a function Message-ID: Hello, I have a strange problem with a piece of code I've written. It's a bit overly complicated to make an example with, but the gist is below. But in the example below, it works. However, in my example, when I call the method from within the function, it returns something other than what I expect. If I call the method outside the function, it behaves properly??? class DataHolder(): def __init__(self): self.x = np.arange(100) def f(self,wl): f = np.sin(self.x) ** wl return f def function(DH,wl=20): f = DH.f(wl) plt.plot(DH.x,f) From adam.jtm30 at gmail.com Wed Dec 8 00:06:53 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Tue, 07 Dec 2010 23:06:53 +0000 Subject: [Tutor] calling a method within a function In-Reply-To: References: Message-ID: <4CFEBE0D.7050308@gmail.com> On 07/12/10 22:36, John wrote: > Hello, > > I have a strange problem with a piece of code I've written. It's a bit > overly complicated to make an example with, but the gist is below. But > in the example below, it works. However, in my example, when I call > the method from within the function, it returns something other than > what I expect. If I call the method outside the function, it behaves > properly??? > > It's hard to tell, from an example that doesn't display the same behaviour, what the problem is but it sounds like there must be some problem with the way you're calling the method from your function that isn't happening otherwise. Can you post the full source somewhere and mail a link to the mailing list? From __peter__ at web.de Wed Dec 8 00:29:40 2010 From: __peter__ at web.de (Peter Otten) Date: Wed, 08 Dec 2010 00:29:40 +0100 Subject: [Tutor] role playing game - help needed References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: Al Stern wrote: > I used the following code and got the following error. The result of input is always a string. > attributes["strength"] = input("\nHow many points do you want to assign to > strength?: ") Say you type 42 when you run your script. Then the above assignment is effectively attributes["strength"] = "42" and when you loop over the values you try to add a string to an integer which is what Python complains about: >>> 42 + "42" Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str' To get an integer you have to convert the string explicitly: >>> 42 + int("42") 84 The best place to do that is as early as possible, even before you put the value into the dictionary: attributes["strength"] = int(input(...)) (In a real application you'd guard against values that cannot be converted to integers) > #point allocation > point_total = 0 > for val in values: > point_total += val > print (point_total) > > and get this error... > > Traceback (most recent call last): > File "C:\Users\Public\Documents\My Python programs\role_playing_game1.py", > line 26, in > point_total += val > TypeError: unsupported operand type(s) for +=: 'int' and 'str' From alan.gauld at btinternet.com Wed Dec 8 02:18:54 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 8 Dec 2010 01:18:54 -0000 Subject: [Tutor] calling a method within a function References: Message-ID: "John" wrote > I have a strange problem with a piece of code I've written. It's a > bit > overly complicated to make an example with, but the gist is below. > But > in the example below, it works. However, in my example, when I call > the method from within the function, it returns something other than > what I expect. Care to give us a clue? What did you expect? What did you get? What does the real code look like? Its a bit hard to diagnose your problem based on a bit of code that works and a loose description of your dissapointment with another bit of code that may or may not work - depending on what you expected! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From washakie at gmail.com Wed Dec 8 09:38:52 2010 From: washakie at gmail.com (John) Date: Wed, 8 Dec 2010 09:38:52 +0100 Subject: [Tutor] calling a method within a function In-Reply-To: References: Message-ID: I understand... but don't blame me if it hurts your head ;) The class is here: http://python.pastebin.com/gPpep50Y The function is here: http://python.pastebin.com/faK0vZ8U The issue is with the 'reflectance' method of the FlightData class (line 76). You can see in the class definition I now just add reflectance to self, but before I was trying to return it, and in the function (line 38-40) I was calling the method to get the array. On Ipython it worked fine to call the method and get a refl array. The array worked as expected (ie. refl.max(), refl.min() returned floats). But when I passed the same FlightData object to the function and called it inside it would not work. It would still return a numpy.ma.core.MaskedArray, but the max and min methods on the array returned empty values, which caused my function to crash. On Wed, Dec 8, 2010 at 2:18 AM, Alan Gauld wrote: > > "John" wrote > >> I have a strange problem with a piece of code I've written. It's a bit >> overly complicated to make an example with, but the gist is below. But >> in the example below, it works. However, in my example, when I call >> the method from within the function, it returns something other than >> what I expect. > > Care to give us a clue? > > What did you expect? > What did you get? > What does the real code look like? > > Its a bit hard to diagnose your problem based on a bit of code that > works and a loose description of your dissapointment with another > bit of code that may or may not work - depending on what you > expected! > > > -- > Alan Gauld > 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 > -- Configuration `````````````````````````` Plone 2.5.3-final, CMF-1.6.4, Zope (Zope 2.9.7-final, python 2.4.4, linux2), Python 2.6 PIL 1.1.6 Mailman 2.1.9 Postfix 2.4.5 Procmail v3.22 2001/09/10 Basemap: 1.0 Matplotlib: 1.0.0 From alan.gauld at btinternet.com Wed Dec 8 10:11:59 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Wed, 8 Dec 2010 09:11:59 +0000 (GMT) Subject: [Tutor] calling a method within a function In-Reply-To: References: Message-ID: <228203.35948.qm@web86705.mail.ird.yahoo.com> > I understand... but don't blame me if it hurts your head ;) First a few comments. Theres an awful lot of superfluous "stuff" in there that would make it much easier to read and work with. The docstring does not appear to be accurate and there are about 30 lines of commented code. Do you use a version control, system? That would avoid the need for all of that commenting. Even so the function is very long and could be easily broken into smaller, more maintainable chunks that would help you diagnose and manage problems more effectively. In the class you assifn self to D in init? If its just to avoid some typing you could just use D instead of self in the parameter list. The use of self is just a convention. However, for the sake of 3 characters I personally think its a convention worth following... However, to your problem,... > > The class is here: > http://python.pastebin.com/gPpep50Y > > The function is here: > http://python.pastebin.com/faK0vZ8U > > The issue is with the 'reflectance' method of the FlightData class > (line 76). You can see in the class definition I now just add > reflectance to self, but before I was trying to return it, and in the > function (line 38-40) I was calling the method to get the array. The reflectance method does not return anything, it will set your value to None. HTH, Alan G. From hgfernan at gmail.com Wed Dec 8 19:52:38 2010 From: hgfernan at gmail.com (Hilton Fernandes) Date: Wed, 8 Dec 2010 16:52:38 -0200 Subject: [Tutor] Python vs. MATLAB In-Reply-To: References: <201012060822.19490.cfuller084@thinkingplanet.net> <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> Message-ID: Hi, Hugo ! Excellent explanation. Thank you. All the best, hilton On Mon, Dec 6, 2010 at 5:05 PM, Hugo Arts wrote: > On Mon, Dec 6, 2010 at 6:09 PM, Joel Schwartz > wrote: > > Chris, > > > > Can you say more about number (7) in your list? What does "pass by value" > > mean and what are the alternatives? > > > > Thanks, > > Joel > > > > Generally, pass-by-* refers to how the arguments to functions are treated. > * In call-by-value, the value of the arguments are copied into the > function. There is no way to modify variables outside of the function > since you don't have access to them, only to copies. C uses this, > among many others: > > int a = 5 > void func(int b) { b = 6; } > func(a); > a == 5; /* evaluates to true, variable outside function scope remains > unchanged */ > > The value b, inside the function, contains a copy of a. So when it is > modified, the original a remains unchanged. > > * in call-by-reference, the function is given implicit *references* to > its arguments. When modifying the variables inside of the function, > the variable outside is also changed. you can simulate it in many > languages by passing an expicit reference rather than an implicit one > (such as C's pointers): > > int a = 5 > void func(int * b) { *b = 6; } > func(&a); > a == 6; /* evaluates to true. the function was able to modify a > variable outside of its scope */ > > * python uses something that wikipedia calls "call-by-sharing." It is > not call-by-value, nor is it call-by-reference. It means, in short, > that while the function has access to the callers, *values*, it does > NOT have access to the callers *variables*. To demonstrate: > > a = [] > def f(b): > b.append(1) > b = [2] > f(a) > print a # prints "[1]" > > As in pass-by-reference, the function f could modify it's callers > values by appending 1 to the list. However, unlike *real* > pass-by-reference, when trying to *re-assign* the variable into > something entirely different, there was no effect (a did not become > equal to [2]). > > > Many people call python pass-by-reference, even though this is > technically incorrect. The difference comes from the semantics of > variables and values. In languages such as C, a variable is an area of > memory that contains something. An assignment then, copies the value > on the right into the variable (memory) on the left. > > python doesn't have variables, but names. a name is essentially itself > a reference to some *object* that lives somewhere in memory. An > assignment is something completely different in this context, it > merely sets the reference (variable) on the left to *point to* the > object on the right. So, when evaluating function arguments, names > inside the function are set to point to the *objects* supplied as > arguments, (not to names!). Since we don't have access to the caller's > names, python is not a true pass-by-reference language. > > for more on why python is neither call-by-value nor call-by-reference: > http://effbot.org/zone/call-by-object.htm > > for more on python's variable semantics and how it differs from > languages like C: > > http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables > > Hugo > _______________________________________________ > 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 Wed Dec 8 20:04:12 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 8 Dec 2010 20:04:12 +0100 Subject: [Tutor] Python vs. MATLAB In-Reply-To: References: <201012060822.19490.cfuller084@thinkingplanet.net> <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> Message-ID: On Wed, Dec 8, 2010 at 7:52 PM, Hilton Fernandes wrote: > Hi, Hugo ! > > Excellent explanation. Thank you. > > All the best, > hilton > Haha, well, at least someone noticed my explanation. I should probably refer you to Steven's dissertation of same, though, I think most would prefer it to mine. From howitzer at archlinux.us Wed Dec 8 20:11:18 2010 From: howitzer at archlinux.us (howitzer at archlinux.us) Date: Wed, 8 Dec 2010 20:11:18 +0100 Subject: [Tutor] Feedback on coding style Message-ID: <20101208191118.GA4243@sousuke> Hi. For the past week, I've been following an online Python guide named: 'Learn Python the Hard Way'. I'm very happy with it as it gives a lot of freedom to explore. However, due to this I have no idea if I'm thinking the right way. That's why I've attached a script of mine I've been working on all day. It works a 100%, but I'm afraid I've made very bad choices concerning design and coding style. (If it could've been much simpler, if there are glaring mistakes, poor methods, ..) Could anyone be so friendly as to offer a bit of feedback, to a newbie? PS: The script very simple accepts 2 arguments from the commandline. First arg being the number to which should be counted, second arg being the interval. Thank you, Adrian -------------- next part -------------- A non-text attachment was scrubbed... Name: exercise33.py Type: text/x-python Size: 2680 bytes Desc: not available URL: From hgfernan at gmail.com Wed Dec 8 21:05:45 2010 From: hgfernan at gmail.com (Hilton Fernandes) Date: Wed, 8 Dec 2010 18:05:45 -0200 Subject: [Tutor] Python vs. MATLAB In-Reply-To: References: <201012060822.19490.cfuller084@thinkingplanet.net> <00b101cb9568$579ebf70$6501a8c0@JLAPTOP> Message-ID: Hi, Hugo ! Never mind that: it is usual that people in lists pay attention to usual posters. Your explanation was clear and straightforward, however it was also short. At least for the time being, i don't need any other document to teach me Python parameter passing semantics. All the best, hilton On Wed, Dec 8, 2010 at 5:04 PM, Hugo Arts wrote: > On Wed, Dec 8, 2010 at 7:52 PM, Hilton Fernandes wrote: >> Hi, Hugo ! >> >> Excellent explanation. Thank you. >> >> All the best, >> hilton >> > > Haha, well, at least someone noticed my explanation. I should probably > refer you to Steven's dissertation of same, though, I think most would > prefer it to mine. > From hugo.yoshi at gmail.com Wed Dec 8 21:46:31 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Wed, 8 Dec 2010 21:46:31 +0100 Subject: [Tutor] Feedback on coding style In-Reply-To: <20101208191118.GA4243@sousuke> References: <20101208191118.GA4243@sousuke> Message-ID: On Wed, Dec 8, 2010 at 8:11 PM, wrote: > Hi. > > For the past week, I've been following an online Python guide named: > 'Learn Python the Hard Way'. I'm very happy with it as it gives a lot of > freedom to explore. > > However, due to this I have no idea if I'm thinking the right way. That's > why I've attached a script of mine I've been working on all day. > > It works a 100%, but I'm afraid I've made very bad choices concerning > design and coding style. (If it could've been much simpler, if there are > glaring mistakes, poor methods, ..) > > Could anyone be so friendly as to offer a bit of feedback, to a newbie? > I like that you've divided the program up into functions, this is A Good Thing(TM). It made it much easier for me to understand what is going on, so keep doing that. little nitpick (maybe not *so* little), your functions are mutually recursive. This is a difficult word that basically means that they are all calling each other in a cycle that is potentially endless. In this case, the cycle goes like this: check_OP_size -> ask_change -> choice_result -> change_vars -> check_OP_size. You see that if I keep inputting the right things, I can go through this cycle endlessly. In this case, it's not a big problem, because no one is likely to do that for long enough to cause problems, but if another part of your program is doing the inputting, it can go wrong quickly. Why? Because functions can't call each other indefinitely. Try running this example: def f(): f() f() This is a function that keeps calling itself, the simplest possible cycle. It gives you an error "maximum recursion depth exceeded." There are reasons for this, and you should learn about recursion and the call stack at some point, but that is another long essay. For now it is enough to know that recursion is something to be careful with, as there is a limit to how many functions can call each other. Second, you're using globals too much. Functions should rely as much as possible on their arguments, and nothing else. I can count the number of times I needed the global statement on one hand, and I've programmed a lot of python. Globals make your program difficult to follow, because you can't clearly see where the data is coming from and going to. Rather than having your functions communicate through global variables, you should have them communicate through function arguments and return values. See if you can write a version without the global statements. There are a few other little things. The two if statements in check_OP_size can be rewritten into an if/else statement, since the two checks are complementary (one of them will always be true). The while loop in the_loop is a little overcomplicated (you had the right idea with the commented out range call). Lastly, the program could've been written a lot more succinctly, but this will come with time and practice. Writing code is hard, and getting a feel for how to divide up your functions and how to express yourself in python most naturally will take time and practice. I'd say you're on the right track, and if you get rid of all the globals you'll be well on your way to becoming an excelling python programmer. If you need any more help, let us know. Hugo From davea at ieee.org Wed Dec 8 23:01:05 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 08 Dec 2010 17:01:05 -0500 Subject: [Tutor] Feedback on coding style In-Reply-To: <20101208191118.GA4243@sousuke> References: <20101208191118.GA4243@sousuke> Message-ID: <4D000021.9030406@ieee.org> On 01/-10/-28163 02:59 PM, howitzer at archlinux.us wrote: > Hi. > > For the past week, I've been following an online Python guide named: > 'Learn Python the Hard Way'. I'm very happy with it as it gives a lot of > freedom to explore. > > However, due to this I have no idea if I'm thinking the right way. That's > why I've attached a script of mine I've been working on all day. > > It works a 100%, but I'm afraid I've made very bad choices concerning > design and coding style. (If it could've been much simpler, if there are > glaring mistakes, poor methods, ..) > > Could anyone be so friendly as to offer a bit of feedback, to a newbie? > > PS: The script very simple accepts 2 arguments from the commandline. > First arg being the number to which should be counted, > second arg being the interval. > > Thank you, > Adrian I agree with Hugo, probably on all his points. But to help you fix it, I think I can help you a bit as well. First is I think you're misunderstanding the purpose of a function. A function should be a self-contained piece of code that gets called, and that returns when done. You're using them mostly as a way to implement what BASIC used to have as a GOTO. For example, when a loop is needed, you do it by calling another function which calls the first one. That is called recursion if it's done on purpose, and a bug if it happens by accident. Second is that you're misusing global variables. A function needs to be called with those values it needs to do its work, and it needs to return the results of that work. Very seldom should those things be globals. Start with the first function. You declare it with maxn and incr as parameters, and you call it correctly. But it also uses the globals, rather than using the ones passed in. Then the function ask_change(). You should be passing it the two arguments, and getting back modified arguments as return values. And the function shouldn't call the_loop() itself, it should just get the new values. jibjab_result() is trying to be a loop, by effectively calling itself, through choice_result(). If you need a loop, just write one. And that loop will probably be in ask_change(), without needing any other functions inside. It's good to decompose a problem into functions, but you're not using functions in the way they're designed. For small problems, this can work, but as the problems get more complex, you'll be forced to change your habits. DaveA From carroll at tjc.com Wed Dec 8 23:08:32 2010 From: carroll at tjc.com (Terry Carroll) Date: Wed, 8 Dec 2010 14:08:32 -0800 (PST) Subject: [Tutor] Which non SQL Database ? In-Reply-To: <201012042241.oB4Mfcfi079977@krusty.intranet.com.mx> References: <201012042241.oB4Mfcfi079977@krusty.intranet.com.mx> Message-ID: On Sat, 4 Dec 2010, Jorge Biquez wrote: > What would do you suggest to take a look? If possible available under the 3 > plattforms. I would second the use of SQLite. It's built into Python now, on all platforms. But you specified "non SQL", so one other thing I'd suggest is to just create the data structure you need in Python and use pickle to save it. I recently had an exercise of recovering files from a damaged hard drive. The problem is, it recovered a lot of legitimately deleted files along with the recovered "live" files. All the files had generic names, with only filetypes to guide me for content, like "028561846.avi" instead of descriptive names. I wrote a program to read every single one of these files and determine its MD5 checksum; I stored the results in a dictionary. The key to the dictionary was the checksum; and the value was a list of files that had that checksum; the list was usually, but not always, only one element. Then I pickled that dictionary. In another program, I ran os.walk against my archive CDROMs/DVDRROMs, or some other directories on my hard drive, finding the MD5 of each file; and if it corresponded to a "rescued" file, it deleted the rescued file. Ideally, I would have also updated the dictionary to drop the files I'd cleaned up, and at the end of processing, re-pickle the edited dictionary; but that wasn't an option as I usually had 2 or 3 instances of the program running simultaneously, each processing a different directory of CD/DVD. From albstern at gmail.com Wed Dec 8 23:56:44 2010 From: albstern at gmail.com (Al Stern) Date: Wed, 8 Dec 2010 16:56:44 -0600 Subject: [Tutor] role playing game - help needed In-Reply-To: References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: Peter, Thanks for the advice as to how to use numbers. I had learned this before but the example in the documentation didn't use 'int' so I thought I didn't need to when using dictionary values. Anyway, I finally finished the program. I am sure it isn't the most efficient code and I suspect I should have used the 'while' loop earlier, but the bottom line is it seems to work. I will paste what I came up with in case anyone is interested. Thanks again to everyone. I couldn't have done it wothout you. -Al # character creator / role playing game # have 30 total points to work with # set variables attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0} MAX_POINTS = int(30) keys = attributes.keys() values = attributes.values() list (values) print(""" Welcome to Quest. The goal of the game is to help our hero achieve his mission. He will need strength, health, wisdom and dexterity to survive. You will have 30 points to 'spend' on these attributes. Use them wisely. His life depends on it. """ ) attributes["strength"] = int(input("\nHow many points do you want to assign to strength?: ")) attributes["health"] = int(input("\nHow many points do you want to assign to health?: ")) attributes["wisdom"] = int(input("\nHow many points do you want to assign to wisdom?: ")) attributes["dexterity"] = int(input("\nHow many points do you want to assign to dexterity?: ")) #point allocation point_total = 0 for val in values: point_total += val print ("\nThis is how you have chosen to allocate your 30 points.") print ("\nStrength:",(attributes["strength"])) print ("Health:", (attributes["health"])) print ("Wisdom:", (attributes["wisdom"])) print ("Dexterity:", (attributes["dexterity"])) available_points = (MAX_POINTS) - (point_total) while point_total != "": if point_total > 30: print ("\nYou have gone over your alloted 30 points.") print ("Please re-enter your choices. ") attributes["strength"] = int(input("\nHow many points do you want to assign to strength?: ")) attributes["health"] = int(input("\nHow many points do you want to assign to health?: ")) attributes["wisdom"] = int(input("\nHow many points do you want to assign to wisdom?: ")) attributes["dexterity"] = int(input("\nHow many points do you want to assign to dexterity?: ")) #point allocation point_total = 0 for val in values: point_total += val print ("\nThis is how you have chosen to allocate your 30 points.") print ("\nStrength:",(attributes["strength"])) print ("Health:", (attributes["health"])) print ("Wisdom:", (attributes["wisdom"])) print ("Dexterity:", (attributes["dexterity"])) available_points = (MAX_POINTS) - (point_total) continue else: break print ("\nYou have", available_points, "points left.") print ("\nSince you have points left over, you may reallocate your points or begin your quest.") choice = int(input("\nTo reallocate, press 1. To begin, press 2: ")) while choice != "": if choice == 1: print ("Please re-enter your choices. ") attributes["strength"] = int(input("\nHow many points do you want to assign to strength?: ")) attributes["health"] = int(input("\nHow many points do you want to assign to health?: ")) attributes["wisdom"] = int(input("\nHow many points do you want to assign to wisdom?: ")) attributes["dexterity"] = int(input("\nHow many points do you want to assign to dexterity?: ")) #point allocation point_total = 0 for val in values: point_total += val print ("\nThis is how you have chosen to allocate your 30 points.") print ("\nStrength:",(attributes["strength"])) print ("Health:", (attributes["health"])) print ("Wisdom:", (attributes["wisdom"])) print ("Dexterity:", (attributes["dexterity"])) available_points = (MAX_POINTS) - (point_total) print ("\nYou have", available_points, "points left.") print ("\nSince you have points left over, you may reallocate your points or begin your quest.") choice = int(input("\nTo reallocate, press 1. To begin, press 2: ")) elif choice == 2: break else: continue print ("You are now ready to begin your quest. Good luck.") input ("\n\nPress the enter key to continue.\n") On Tue, Dec 7, 2010 at 5:29 PM, Peter Otten <__peter__ at web.de> wrote: > Al Stern wrote: > > > I used the following code and got the following error. > > The result of input is always a string. > > > attributes["strength"] = input("\nHow many points do you want to assign > to > > strength?: ") > > Say you type 42 when you run your script. Then the above assignment is > effectively > > attributes["strength"] = "42" > > and when you loop over the values you try to add a string to an integer > which is what Python complains about: > > >>> 42 + "42" > Traceback (most recent call last): > File "", line 1, in > TypeError: unsupported operand type(s) for +: 'int' and 'str' > > To get an integer you have to convert the string explicitly: > > >>> 42 + int("42") > 84 > > The best place to do that is as early as possible, even before you put the > value into the dictionary: > > attributes["strength"] = int(input(...)) > > (In a real application you'd guard against values that cannot be converted > to integers) > > > #point allocation > > point_total = 0 > > for val in values: > > point_total += val > > print (point_total) > > > > and get this error... > > > > Traceback (most recent call last): > > File "C:\Users\Public\Documents\My Python > programs\role_playing_game1.py", > > line 26, in > > point_total += val > > TypeError: unsupported operand type(s) for +=: 'int' and 'str' > > > _______________________________________________ > 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 Thu Dec 9 01:35:32 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 9 Dec 2010 00:35:32 -0000 Subject: [Tutor] role playing game - help needed References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: "Al Stern" wrote > Anyway, I finally finished the program. I am sure it isn't the most > efficient code and I suspect I should have used the 'while' loop > earlier, Some things to think about below. > attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": > 0} > MAX_POINTS = int(30) > keys = attributes.keys() > values = attributes.values() you don't need keys and values as variables because you can always fetch them from atttributes as needed. > list (values) This converts values into a list, which it already is, then throws it away. I suspect you think its doing something else?. > attributes["strength"] = int(input("\nHow many points do you want to > assign > to strength?: ")) > attributes["health"] = int(input("\nHow many points do you want to > assign to > health?: ")) > attributes["wisdom"] = int(input("\nHow many points do you want to > assign to > wisdom?: ")) > attributes["dexterity"] = int(input("\nHow many points do you want > to assign > to dexterity?: ")) One of the advantages of using a dict is that you can use a loop here and thus easily extend your data in the futire without changing the input/output code: for name in attributes.keys(): attributes[name] = int( input("How many points do you want to assign to %s " % name) ) > #point allocation > point_total = 0 > for val in values: > point_total += val point_total = sum( attributes.values() ) > print ("\nThis is how you have chosen to allocate your 30 points.") > print ("\nStrength:",(attributes["strength"])) > print ("Health:", (attributes["health"])) > print ("Wisdom:", (attributes["wisdom"])) > print ("Dexterity:", (attributes["dexterity"])) And as above this can be a loop: for name in attributes.keys(): print("\n", name, ":", attributes[name] ) > available_points = (MAX_POINTS) - (point_total) No need for any of the parentheses here > while point_total != "": You are comparing point_total to a string, but point_total is a number. You need to be careful about keeping your data types consistent. > if point_total > 30: > print ("\nYou have gone over your alloted 30 points.") > print ("Please re-enter your choices. ") .... > > #point allocation > point_total = 0 > for val in values: > point_total += val again, use sum() > print ("\nThis is how you have chosen to allocate your 30 > points.") > print ("\nStrength:",(attributes["strength"])) > print ("Health:", (attributes["health"])) > print ("Wisdom:", (attributes["wisdom"])) > print ("Dexterity:", (attributes["dexterity"])) > available_points = (MAX_POINTS) - (point_total) > continue > else: > break The logic here could be simplified into while point_total > 30: > print ("\nYou have", available_points, "points left.") > print ("\nSince you have points left over, you may reallocate your > points or > begin your quest.") > choice = int(input("\nTo reallocate, press 1. To begin, press 2: > ")) > while choice != "": Again comparing to a string but you have made choice an integer > if choice == 1: > print ("Please re-enter your choices. ") > attributes["strength"] = int(input("\nHow many points do you want > to > assign to strength?: ")) > attributes["health"] = int(input("\nHow many points do you want > to > assign to health?: ")) > attributes["wisdom"] = int(input("\nHow many points do you want > to > assign to wisdom?: ")) > attributes["dexterity"] = int(input("\nHow many points do you > want to > assign to dexterity?: ")) > > #point allocation > point_total = 0 > for val in values: > point_total += val Note, you haven't actually assigned anything to values since the user entered the new data. This is another reason it would be better to not have the variable, just get values directly from attributes on demand. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Thu Dec 9 09:44:21 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 9 Dec 2010 00:44:21 -0800 (PST) Subject: [Tutor] Feedback on coding style In-Reply-To: <4D000021.9030406@ieee.org> References: <20101208191118.GA4243@sousuke> <4D000021.9030406@ieee.org> Message-ID: <779030.67323.qm@web110715.mail.gq1.yahoo.com> Hi, Re: coding style, I can *really* recommend the book 'Code Complete' (http://cc2e.com/). It doesn't focus on Python specifically, but it's a wonderful book. You can find a pdf checklist of the book if you Google a?bit. ?Cheers!! 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: Dave Angel To: howitzer at archlinux.us Cc: tutor at python.org Sent: Wed, December 8, 2010 11:01:05 PM Subject: Re: [Tutor] Feedback on coding style On 01/-10/-28163 02:59 PM, howitzer at archlinux.us wrote: > Hi. > > For the past week, I've been following an online Python guide named: > 'Learn Python the Hard Way'. I'm very happy with it as it gives a lot of > freedom to explore. > > However, due to this I have no idea if I'm thinking the right way. That's > why I've attached a script of mine I've been working on all day. > > It works a 100%, but I'm afraid I've made very bad choices concerning > design and coding style. (If it could've been much simpler, if there are > glaring mistakes, poor methods, ..) > > Could anyone be so friendly as to offer a bit of feedback, to a newbie? > > PS: The script very simple accepts 2 arguments from the commandline. > ??? First arg being the number to which should be counted, > ??? second arg being the interval. > > Thank you, > Adrian I agree with Hugo, probably on all his points.? But to help you fix it, I think I can help you a bit as well. First is I think you're misunderstanding the purpose of a function.? A function should be a self-contained piece of code that gets called, and that returns when done.? You're using them mostly as a way to implement what BASIC used to have as a GOTO.? For example, when a loop is needed, you do it by calling another function which calls the first one.? That is called recursion if it's done on purpose, and a bug if it happens by accident. Second is that you're misusing global variables.? A function needs to be called with those values it needs to do its work, and it needs to return the results of that work.? Very seldom should those things be globals. Start with the first function.? You declare it with maxn and incr as parameters, and you call it correctly.? But it also uses the globals, rather than using the ones passed in. Then the function ask_change().? You should be passing it the two arguments, and getting back modified arguments as return values.? And the function shouldn't call the_loop() itself, it should just get the new values. jibjab_result() is trying to be a loop, by effectively calling itself, through choice_result().? If you need a loop, just write one.? And that loop will probably be in ask_change(), without needing any other functions inside. It's good to decompose a problem into functions, but you're not using functions in the way they're designed.? For small problems, this can work, but as the problems get more complex, you'll be forced to change your habits. 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 alan.gauld at btinternet.com Thu Dec 9 10:09:51 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Thu, 9 Dec 2010 09:09:51 +0000 (GMT) Subject: [Tutor] role playing game - help needed In-Reply-To: References: <000001cb9557$13e15920$3ba40b60$@com> Message-ID: <844426.52951.qm@web86702.mail.ird.yahoo.com> Can you explain this in a little more detail? > > sure. > >for name in attributes.keys(): > attributes[name] = int( input("How many points do you want to assign to %s " % >name) ) > >Where did you get 'name' from? > I made it up. The for loop takes the form for in : where the bits in <> are provided by the programmer. name was just a "meaningful" variable name that I chose. > What does the % do and how does it work inside the > quotation marks as opposed to outside? This is called string formatting. Actually in Python 3 there is a new way of doing this but the older style, like this, still works. Basically within the string we insert percent signs followed by a special letter to indicate the type of data we want to insert into the string. %s means a string, %d a decimal number, %f a floating point number etc. The % immediately after the string is a separator preceding the values to be inserted into the string Try it at the >>> prompt: >>> "%d is a number" % 6 6 is a number >>> "%d is the result of %d + %d" % (6+7,6,7) 13 is the result of 6 + 7 >>> "My name is %s" % "Alan" My name is Alan Someone else might show you the v3 way using the new string.format() operator HTH, Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 9 10:34:32 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 9 Dec 2010 09:34:32 -0000 Subject: [Tutor] Feedback on coding style References: <20101208191118.GA4243@sousuke> <4D000021.9030406@ieee.org> <779030.67323.qm@web110715.mail.gq1.yahoo.com> Message-ID: "Albert-Jan Roskam" wrote > Re: coding style, I can *really* recommend the book 'Code Complete' > (http://cc2e.com/). It doesn't focus on Python specifically, but > it's a > wonderful book. I'll second that. I haven't read the 2nd Ed but the first edition was one of the few (5 or 6?) books I've read about computing that actually changed the way I write code. It's not an absolute beginners book, you need a bit of experience in writing reasonable length programs - more than 100 lines say, to really appreciate why some things are recommended, but for the right audience it is, as Albert says, a wonderful book.. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From howitzer at archlinux.us Thu Dec 9 11:15:41 2010 From: howitzer at archlinux.us (howitzer at archlinux.us) Date: Thu, 9 Dec 2010 11:15:41 +0100 Subject: [Tutor] Feedback on coding style In-Reply-To: <20101208191118.GA4243@sousuke> References: <20101208191118.GA4243@sousuke> Message-ID: Thanks a lot for the very friendly and constructive comments. I can't wait to start overhauling the script with the comments received here. If I could repay the friendliness to anyone, let me know. Adrian -------------- next part -------------- An HTML attachment was scrubbed... URL: From susana.delgado_s at utzmg.edu.mx Thu Dec 9 17:07:21 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Thu, 9 Dec 2010 10:07:21 -0600 Subject: [Tutor] Save file in a specific directory Message-ID: Thank you Jerry! The suggestion you told me make my code worked! > > > > dbf = Dbf(d,new=False, readOnly=True) > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ranceh at gmail.com Thu Dec 9 17:46:34 2010 From: ranceh at gmail.com (Rance Hall) Date: Thu, 9 Dec 2010 10:46:34 -0600 Subject: [Tutor] updating databases with null values Message-ID: I have a set of questions that ask about a customers name, address, email, etc. some of these values are allowed to be null, and others aren't. Some are required to have specific formats when they aren't null. I'm happy with the code Ive written and its question asking routine, but I need help understanding how best to deal with null values. I'm connecting to a postgres database with the python postgres module described here: http://python.projects.postgresql.org/ I'm also using python 3.x if it matters. The sql is different if you want to allow null values update table set value = "string" where condition with null value: update table set value = NULL where condition It would seem I need several iterations of the prepared db statement for various combinations of null values or I don't allow null values at all and put empty strings in the database instead of the null value. Could someone help me think through this idea and help me decide on a good method. Right now I'm thinking that I should skip null values and just store zero length strings in the db. I don't like it, but its certainly much easier (at least to me so far) Your thoughts? From bgailer at gmail.com Thu Dec 9 18:34:15 2010 From: bgailer at gmail.com (bob gailer) Date: Thu, 09 Dec 2010 12:34:15 -0500 Subject: [Tutor] updating databases with null values In-Reply-To: References: Message-ID: <4D011317.7040303@gmail.com> On 12/9/2010 11:46 AM, Rance Hall wrote: > I have a set of questions that ask about a customers name, address, email, etc. > > some of these values are allowed to be null, and others aren't. Some > are required to have specific formats when they aren't null. > > I'm happy with the code Ive written and its question asking routine, > but I need help understanding how best to deal with null values. > > I'm connecting to a postgres database with the python postgres module > described here: http://python.projects.postgresql.org/ > > I'm also using python 3.x if it matters. > > The sql is different if you want to allow null values > > update table set value = "string" where condition > > with null value: > > update table set value = NULL where condition > > It would seem I need several iterations of the prepared db statement > for various combinations of null values or I don't allow null values > at all and put empty strings in the database instead of the null Offhand I'd say you could use a parameterized stattement, like update table set value = ? where condition Then pass either the string or NULL when executing. I don't know postgresql's exact way to do this. > value. > > Could someone help me think through this idea and help me decide on a > good method. Right now I'm thinking that I should skip null values > and just store zero length strings in the db. I don't like it, but > its certainly much easier (at least to me so far) > > Your thoughts? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer 919-636-4239 Chapel Hill NC From patty at cruzio.com Thu Dec 9 19:18:11 2010 From: patty at cruzio.com (patty at cruzio.com) Date: Thu, 9 Dec 2010 10:18:11 -0800 (PST) Subject: [Tutor] Calling Program within Program Message-ID: <0d3883baf3e181715954557c078de7bc.squirrel@cruziomail.cruzio.com> Hello: I would like to know how to call a program from within a program and what directory I should place one small program file in. I am running Python 2.6.6 and Windows 7. I have a directory called C:\Users\StarShip\PyProgs and it has the files BreakersCafe.txt and BreakersCafe.py. This is my primary program running fine. I have a subdirectory called C:\Users\StarShip\PyProg \PicturesForTesting and another subdirectory C:\Users\StarShip\PyProgs\CustomFunctions with various program files, functions defined in them, etc. which I import in my primary program. For example: def Newbanner(): print "\n Alternate Selections\n" Now I have the small program below which is fully self-contained and I want to execute it from within BreakersCafe.txt. I would like to use raw_input and if statement for simple yes/no asking if they would like to see this BeveragesMenu.txt and have it as the last 3-4 lines of the main(). The only directory of these three that has __init__ is C:\Users\StarShip\PyProgs\CustomFunctions and as you can see, the program below is not a function, it has no 'def' anything. I get 'NameError: global name 'BeveragesMenu' is not defined' when I run this in IDLE. I suppose I _could_ make this a function :} but it is likely doable to call a program as a program, right? I am also compiling to a .pyw file where applicable. Thanks for the help. Patty """ This is C:\Users\StarShip\PyProgs\BeveragesMenu.txt and BeveragesMenu.py. Program file for displaying an image using Tkinter built-in GUI functions. open root window, open file descriptor for image, open new window file descriptor to manipulate with Tkinter Label library function. Pic needs to be displayed as a banner. Uses the compound="top" argument to do this. --> take out the pack()function didn't work, pack()is required Uses import Tkinter Program using new breaker's jpg picture; bar choices are not selectable """ import Tkinter import ImageTk rootwindow = Tkinter.Tk() fhdl= ImageTk.Image.open("C:\Users\StarShip\PyProgs\PicturesForTesting\houseimage.jpg") image_var = ImageTk.PhotoImage(fhdl) whdl = Tkinter.Label(rootwindow, compound="top", image=image_var, text="\n\n!!!WELCOME TO BREAKER'S BREAKFAST BAR!!!\n\n\nBeverage Choices: Sparkling Water; Milk; Orange Juice; Apple Juice *OR*\n\n **From Our Bar** Smoothie; Breakers Sun Tea; Chai; Cafe Mocha; Latte; Mimosa") whdl.pack() rootwindow.mainloop() From lmhosie at jacks.sdstate.edu Thu Dec 9 20:39:35 2010 From: lmhosie at jacks.sdstate.edu (lmhosie at jacks.sdstate.edu) Date: Thu, 9 Dec 2010 19:39:35 +0000 Subject: [Tutor] Increment by string, Array Message-ID: Hello, I have a large code that I am using for ARC GIS I know could be much smaller but it works like it is. I have programmed in C++ but am just beginning with python I believe the answer would be to do a parallel array however I am having trouble keeping the SQL statment in the string format it should be and putting in the array. So I ended up using a string.template and just incrementing manually. Here is a sample of the code: # --------------------------------------------------------------------------- # MakeFeature.py # Created on: Tue Nov 23 2010 11:07:25 AM # # --------------------------------------------------------------------------- # Import system modules import sys, string, os, arcgisscripting # Create the Geoprocessor object gp = arcgisscripting.create() gp.overwriteoutput = 1 # Check out any necessary licenses gp.CheckOutExtension("spatial") # Load required toolboxes... gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx") gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx") # Local variables... wrs_season_Layer = "wrs_season_Layer" wrs_season_shp = "D:\\model\\wrs_season.shp" Temp_mdb = "D:\\model\\Temp.mdb" ecoregions = "D:\\model\\Eco\\wwfecoregions\ecoregions.shp" t=string.Template("D:\\model\\Temp.mdb\\T$Num") p = string.Template('"PATH" = $Path')t # Process: print "Calculating row 1" gp.MakeFeatureLayer_management(wrs_season_shp, wrs_season_Layer, p.substitute(Path ='1'), Temp_mdb,) gp.TabulateArea_sa(wrs_season_Layer,"PR", ecoregions, "BIOME", t.substitute(Num = '1'), "0.01") print "Calculating row 2" gp.MakeFeatureLayer_management(wrs_season_shp, wrs_season_Layer, p.substitute(Path ='2'), Temp_mdb,) gp.TabulateArea_sa(wrs_season_Layer,"PR", ecoregions, "BIOME", t.substitute(Num = '2'), "0.01") print "finished #This process repeats for 200 paths each requiring a calculation and a separate table creation indicating which path they are calculating# The problem I had when making $Path a variable is it won't process the SQL statement any longer because of the quotes around it. The format has to stay in (' "PATH" = X') for it to ingest correctly, another way would be "\"PATH\" = 1" however the same problem arises when you try to turn the 1 into a variable. Any help would be much appreciated I would think I could just create a parallel array incrementing by 1 and just looping through for 233 paths? Thankyou, Lynn -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Thu Dec 9 21:22:48 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 9 Dec 2010 15:22:48 -0500 Subject: [Tutor] Increment by string, Array In-Reply-To: References: Message-ID: Can you use something like this: for i in range(1,201): s = str(i) Then Path = '1' can become Path = s On Thu, Dec 9, 2010 at 2:39 PM, wrote: > Hello, > I have a large code that I am using for ARC GIS I know could be much > smaller but it works like it is. I have programmed in C++ but am just > beginning with python I believe the answer would be to do a parallel array > however I am having trouble keeping the SQL statment in the string format it > should be and putting in the array. So I ended up using a string.template > and just incrementing manually. > Here is a sample of the code: > > # > --------------------------------------------------------------------------- > # MakeFeature.py > # Created on: Tue Nov 23 2010 11:07:25 AM > # > # > --------------------------------------------------------------------------- > # Import system modules > import sys, string, os, arcgisscripting > # Create the Geoprocessor object > gp = arcgisscripting.create() > gp.overwriteoutput = 1 > # Check out any necessary licenses > gp.CheckOutExtension("spatial") > # Load required toolboxes... > gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Spatial > Analyst Tools.tbx") > gp.AddToolbox("C:/Program Files (x86)/ArcGIS/ArcToolbox/Toolboxes/Data > Management Tools.tbx") > > # Local variables... > wrs_season_Layer = "wrs_season_Layer" > wrs_season_shp = "D:\\model\\wrs_season.shp" > Temp_mdb = "D:\\model\\Temp.mdb" > ecoregions = "D:\\model\\Eco\\wwfecoregions\ecoregions.shp" > > t=string.Template("D:\\model\\Temp.mdb\\T$Num") > p = string.Template('"PATH" = $Path')t > > # Process: > > print "Calculating row 1" > gp.MakeFeatureLayer_management(wrs_season_shp, wrs_season_Layer, > p.substitute(Path ='1'), Temp_mdb,) > gp.TabulateArea_sa(wrs_season_Layer,"PR", ecoregions, "BIOME", > t.substitute(Num = '1'), "0.01") > > print "Calculating row 2" > gp.MakeFeatureLayer_management(wrs_season_shp, wrs_season_Layer, > p.substitute(Path ='2'), Temp_mdb,) > gp.TabulateArea_sa(wrs_season_Layer,"PR", ecoregions, "BIOME", > t.substitute(Num = '2'), "0.01") > print "finished > > > #This process repeats for 200 paths each requiring a calculation and a > separate table creation indicating which path they are calculating# > The problem I had when making $Path a variable is it won't process the SQL > statement any longer because of the quotes around it. The format has to stay > in (' "PATH" = X') > for it to ingest correctly, another way would be "\"PATH\" = 1" however the > same problem arises when you try to turn the 1 into a variable. > > Any help would be much appreciated I would think I could just create a > parallel array incrementing by 1 and just looping through for 233 paths? > Thankyou, > Lynn > > _______________________________________________ > 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 mehgcap at gmail.com Fri Dec 10 01:51:53 2010 From: mehgcap at gmail.com (Alex Hall) Date: Thu, 9 Dec 2010 19:51:53 -0500 Subject: [Tutor] 'or' in assignment (not if statement)? Message-ID: Hi all, I am reading the source of a project I hope to help with (http://www.qwitter-client.net). I sometimes see something like: val=val or 1 I am guessing that val is an int. If val==0, the 'or' kicks in and val=1, else the or is not needed and val=val. Am I close? Can other words or symbols be used in contexts where one would not normally think of them? Thanks. -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From adam.jtm30 at gmail.com Fri Dec 10 01:59:34 2010 From: adam.jtm30 at gmail.com (Adam Bark) Date: Fri, 10 Dec 2010 00:59:34 +0000 Subject: [Tutor] 'or' in assignment (not if statement)? In-Reply-To: References: Message-ID: <4D017B76.8010608@gmail.com> On 10/12/10 00:51, Alex Hall wrote: > Hi all, > I am reading the source of a project I hope to help with > (http://www.qwitter-client.net). I sometimes see something like: > val=val or 1 > I am guessing that val is an int. If val==0, the 'or' kicks in and > val=1, else the or is not needed and val=val. Am I close? Can other > words or symbols be used in contexts where one would not normally > think of them? > Thanks. > > Hi Alex, This is one of those times the interactive interpreter comes in handy eg: In [1]: val=5 In [2]: val=val or 1 In [3]: val Out[3]: 5 In [4]: val=0 In [5]: val=val or 1 In [6]: val Out[6]: 1 You are right by the way and I know you can't test every possibility but, as you already suspected the outcome, this just reinforces it I think. HTH, Adam. From alan.gauld at btinternet.com Fri Dec 10 02:07:02 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Dec 2010 01:07:02 -0000 Subject: [Tutor] 'or' in assignment (not if statement)? References: Message-ID: "Alex Hall" wrote > val=val or 1 > I am guessing that val is an int. If val==0, the 'or' kicks in and > val=1, else the or is not needed and val=val. Am I close? Yes this is a combination of what is known as short circuit evaluation of boolean expressions and a quirk of Python that returns the actual value of something that is being treated as a boolean. There is a section on this in the Functional Programming topic in my tutor which explains and illustrates in much more detail. This particular trick is now deprecated in favour of the new conditional expressiion, so your code would now be written as: val = val if val else 1 > Can other words or symbols be used in contexts where one > would not normally think of them? See my tutor, it shows how and can be used in similar ways... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mehgcap at gmail.com Fri Dec 10 02:23:24 2010 From: mehgcap at gmail.com (Alex Hall) Date: Thu, 9 Dec 2010 20:23:24 -0500 Subject: [Tutor] 'or' in assignment (not if statement)? In-Reply-To: References: Message-ID: Thanks to all for the quick responses. Python always surprises me with its shortcuts... On 12/9/10, Alan Gauld wrote: > > "Alex Hall" wrote > >> val=val or 1 > >> I am guessing that val is an int. If val==0, the 'or' kicks in and >> val=1, else the or is not needed and val=val. Am I close? > > Yes this is a combination of what is known as short circuit > evaluation of boolean expressions and a quirk of Python that > returns the actual value of something that is being treated as > a boolean. > > There is a section on this in the Functional Programming > topic in my tutor which explains and illustrates in much > more detail. > > This particular trick is now deprecated in favour of the new > conditional expressiion, so your code would now be written as: > > val = val if val else 1 > >> Can other words or symbols be used in contexts where one >> would not normally think of them? > > See my tutor, it shows how and can be used in similar ways... > > HTH, > > -- > Alan Gauld > 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 > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From hugo.yoshi at gmail.com Fri Dec 10 02:36:43 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 10 Dec 2010 02:36:43 +0100 Subject: [Tutor] 'or' in assignment (not if statement)? In-Reply-To: References: Message-ID: On Fri, Dec 10, 2010 at 2:07 AM, Alan Gauld wrote: > > "Alex Hall" wrote >> >> val=val or 1 > >> I am guessing that val is an int. If val==0, the 'or' kicks in and >> val=1, else the or is not needed and val=val. Am I close? > > Yes this is a combination of what is known as short circuit evaluation of > boolean expressions and a quirk of Python that returns the actual value of > something that is being treated as a boolean. > Doesn't short-circuit evaluation refer specifically to the behavior where arguments are only evaluated if they need to be? It's a very useful feature, but not technically required for the "val = val or 1" behavior to work. Also, returning on of its operands rather than a boolean is hardly a quirk, since basically all dynamic languages do it ever since perl made "val = val or 1" an idiom (at least, I think it was perl). There is more to the innocent little or. See http://docs.python.org/library/stdtypes.html#truth-value-testing It works on any type, not just numbers, and operates by truth value testing, which is more complicated than it might first seem. Hugo From steve at pearwood.info Fri Dec 10 04:50:11 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 10 Dec 2010 14:50:11 +1100 Subject: [Tutor] 'or' in assignment (not if statement)? In-Reply-To: References: Message-ID: <4D01A373.4000705@pearwood.info> Hugo Arts wrote: > Doesn't short-circuit evaluation refer specifically to the behavior > where arguments are only evaluated if they need to be? It's a very > useful feature, but not technically required for the "val = val or 1" > behavior to work. Yes, exactly. Some languages (Pascal comes to mind) doesn't have short-circuit behaviour at all. If I've understood correctly, some languages (Algol, I think, but don't quote me) have short-circuiting function parameters, so you could do this: x = 0 function(flag, x/0) and the argument x/0 would only be evaluated if the function actually tried to use it. Python has short-circuit behaviour for: x or y x and y all(iterable) any(iterable) true_value if condition else false_value > Also, returning on of its operands rather than a boolean is hardly a > quirk, since basically all dynamic languages do it ever since perl > made "val = val or 1" an idiom (at least, I think it was perl). Yes, it's certainly useful and not deprecated. At worst, it's less common since the introduction of the ternary if operator, but it's still useful. You can do things like this: extras = [] # Global list of extra names to use. def func(x, names=None): # Do something with x and an optional list of names. names = names or extras or ['my', 'internal', 'list', 'of', 'names'] do_stuff_with(x, names) This means that the names actually used will be the first of: - the function argument - the global extras - the built-in internal list which is not empty. So you can override the internal list globally by setting extras, and you can override the global list by passing a list of names to the function. -- Steven From modulok at gmail.com Fri Dec 10 07:43:42 2010 From: modulok at gmail.com (Modulok) Date: Thu, 9 Dec 2010 23:43:42 -0700 Subject: [Tutor] Calling Program within Program In-Reply-To: <0d3883baf3e181715954557c078de7bc.squirrel@cruziomail.cruzio.com> References: <0d3883baf3e181715954557c078de7bc.squirrel@cruziomail.cruzio.com> Message-ID: Patty, I didn't read through your code, but to call an external program see the 'subprocess' module in the standard library: http://docs.python.org/library/subprocess.html -Modulok- On 12/9/10, patty at cruzio.com wrote: > > Hello: > > I would like to know how to call a program from within a program and what > directory I should place one small program file in. I am running Python > 2.6.6 and Windows 7. > > I have a directory called C:\Users\StarShip\PyProgs and it has the files > BreakersCafe.txt and BreakersCafe.py. This is my primary program running > fine. > > I have a subdirectory called C:\Users\StarShip\PyProg \PicturesForTesting > and another subdirectory C:\Users\StarShip\PyProgs\CustomFunctions with > various program files, functions defined in them, etc. which I import in > my primary program. For example: > > def Newbanner(): > print "\n Alternate Selections\n" > > Now I have the small program below which is fully self-contained and I > want to execute it from within BreakersCafe.txt. I would like to use > raw_input and if statement for simple yes/no asking if they would like to > see this BeveragesMenu.txt and have it as the last 3-4 lines of the > main(). > > The only directory of these three that has __init__ is > C:\Users\StarShip\PyProgs\CustomFunctions > and as you can see, the program below is not a function, it has no 'def' > anything. I get 'NameError: global name 'BeveragesMenu' is not defined' > when I run this in IDLE. I suppose I _could_ make this a function :} but > it is likely doable to call a program as a program, right? I am also > compiling to a .pyw file where applicable. > > Thanks for the help. > > Patty > > """ > This is C:\Users\StarShip\PyProgs\BeveragesMenu.txt and BeveragesMenu.py. > Program file for displaying an image using Tkinter built-in GUI functions. > open root window, open file descriptor for image, open new window file > descriptor to manipulate with Tkinter Label library function. > > Pic needs to be displayed as a banner. Uses the compound="top" argument to > do this. > > --> take out the pack()function didn't work, pack()is required > > Uses import Tkinter > > Program using new breaker's jpg picture; bar choices are not selectable > """ > > > import Tkinter > import ImageTk > > rootwindow = Tkinter.Tk() > > fhdl= > ImageTk.Image.open("C:\Users\StarShip\PyProgs\PicturesForTesting\houseimage.jpg") > image_var = ImageTk.PhotoImage(fhdl) > > whdl = Tkinter.Label(rootwindow, compound="top", image=image_var, > text="\n\n!!!WELCOME TO BREAKER'S BREAKFAST BAR!!!\n\n\nBeverage Choices: > Sparkling Water; Milk; Orange Juice; Apple Juice *OR*\n\n **From Our > Bar** Smoothie; Breakers Sun Tea; Chai; Cafe Mocha; Latte; > Mimosa") > > whdl.pack() > rootwindow.mainloop() > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From alan.gauld at btinternet.com Fri Dec 10 09:46:25 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Fri, 10 Dec 2010 08:46:25 +0000 (GMT) Subject: [Tutor] 'or' in assignment (not if statement)? In-Reply-To: References: Message-ID: <552185.23819.qm@web86708.mail.ird.yahoo.com> > Doesn't short-circuit evaluation refer specifically to the behavior > where arguments are only evaluated if they need to be? It's a very > useful feature, but not technically required for the "val = val or 1" > behavior to work. Its essential. If Python always evaluated all parts of a boolean expression the return value would always be the last item. It's the fact that Python knows that if val is true then it doesn't need to evaluate the second term that causes it to return val rather than 1. > Also, returning on of its operands rather than a boolean is hardly a > quirk, since basically all dynamic languages do it ever since perl > made "val = val or 1" an idiom (at least, I think it was perl). Its a quirk in that it is not the intuitively expected behaviour. It's a boolean expression you would reasonably expect a true boolean result. I think you are right that Perl was the first popular language to do this, but Perl is a relatively recent arrival (abouit the same time as Python - 1988-90?) and it has become a feature of many recent dynamic languages. But most static languages still return true boolean values. Alan G. From steve at pearwood.info Fri Dec 10 14:25:56 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 11 Dec 2010 00:25:56 +1100 Subject: [Tutor] 'or' in assignment (not if statement)? In-Reply-To: <552185.23819.qm@web86708.mail.ird.yahoo.com> References: <552185.23819.qm@web86708.mail.ird.yahoo.com> Message-ID: <4D022A64.60703@pearwood.info> ALAN GAULD wrote: > >> Doesn't short-circuit evaluation refer specifically to the behavior >> where arguments are only evaluated if they need to be? It's a very >> useful feature, but not technically required for the "val = val or 1" >> behavior to work. > > Its essential. > If Python always evaluated all parts of a boolean expression > the return value would always be the last item. You can simulate non-short-circuit behaviour with a function: def or_(a, b): if a: return a return b x = 0 or_(True, 1/x) Unlike the short-circuiting `or` operator, this version evaluates the 1/x even though it doesn't end up being used. This was one of the reasons why the ternary `if` operator had to be handled by syntax, rather than having people write a function: ifte(condition, a, b): if condition: return a else: return b ifte(len(x) > 0, x[0], "nothing there") This will fail if x is an empty list, unlike this: x[0] if len(x) > 0 else "nothing there" which works because the Python interpreter knows not to evaluate x[0] unless needed. It's the fact that > Python knows that if val is true then it doesn't need to evaluate > the second term that causes it to return val rather than 1. That's what makes it short circuiting, but that's not why it returns the first argument. `or` in standard Pascal doesn't short-circuit. Take this example `or.p` file: [steve at sylar pascal]$ cat or.p program main(input, output); function f1(a:integer):boolean; begin writeln('calling f1'); f1 := True; end; function f2(a:integer):boolean; begin writeln('calling f2'); f2 := True; end; var n: integer; f: boolean; begin n := 0; f := f1(n) or f2(n); end. [steve at sylar pascal]$ [steve at sylar pascal]$ gpc --no-short-circuit or.p [steve at sylar pascal]$ ./a.out calling f1 calling f2 (gpc defaults to the sensible but non-standard short-circuit behavior, and you have to pass a compiler option to get the standard behaviour.) >> Also, returning on of its operands rather than a boolean is hardly a >> quirk, since basically all dynamic languages do it ever since perl >> made "val = val or 1" an idiom (at least, I think it was perl). > > Its a quirk in that it is not the intuitively expected behaviour. > It's a boolean expression you would reasonably expect a > true boolean result. That brought a smile to my face! That's very amusing, the idea that *boolean algebra* is intuitively expected! As an experiment, offer to buy your wife (or girlfriend, boyfriend, significant other, or if all else fails, mum) dinner, and ask if she'd prefer to go to an Italian or Chinese restaurant. If she says that she doesn't understand the question, because restaurants aren't True/False boolean values, then you might have a point :) -- Steven From ben.ganzfried at gmail.com Fri Dec 10 15:48:53 2010 From: ben.ganzfried at gmail.com (Ben Ganzfried) Date: Fri, 10 Dec 2010 09:48:53 -0500 Subject: [Tutor] Meaning of -1 in Python Message-ID: I'm currently working through the Google Python tutorial exercises and had questions about the following function: def not_bad(s): # +++your code here+++ # LAB(begin solution) n = s.find('not') b = s.find('bad') if n != -1 and b != -1 and b > n: s = s[:n] + 'good' + s[b+3:] return s It's clear that n!=-1 and b!=-1 means something like : "if in the string 's' we find the word "not" and in string 's' we find the word "bad." I'm wondering the following: On a deeper computational level, what is going on here? What exactly does Python take -1 to mean? Is it saying that since the string 's' is indexed starting from 0 to len(s), and since -1 is not part of that, that therefore something having the value of -1 will never be in the string? If so, then how exactly does using negative numbers to count a string work? I have read something about this earlier... Also, does the part: b>n mean, in this case: "bad comes after not in the string 's'"? Thank you very much. Sincerely, Ben From patty at cruzio.com Fri Dec 10 17:46:14 2010 From: patty at cruzio.com (patty at cruzio.com) Date: Fri, 10 Dec 2010 08:46:14 -0800 (PST) Subject: [Tutor] Calling Program within Program In-Reply-To: References: <0d3883baf3e181715954557c078de7bc.squirrel@cruziomail.cruzio.com> Message-ID: Hello Modulok: The code below is very simple, so I don't think you would have needed to read it. I will review the subprocess doc and look this up in my books and class notes. If I have more questions, I will email the group. I also realized that I wasn't clear in asking a secondary question which is what directory/directories to place files that are part of a program, actually an application. As if you were creating a source code hierarchy for the first time :} If I have more questions about that I will email again and change the subject header. Thanks for the reply!! Patty > Patty, > > I didn't read through your code, but to call an external program see > the 'subprocess' module in the standard library: > http://docs.python.org/library/subprocess.html > > -Modulok- > > > On 12/9/10, patty at cruzio.com wrote: >> >> Hello: >> >> I would like to know how to call a program from within a program and >> what >> directory I should place one small program file in. I am running Python >> 2.6.6 and Windows 7. >> >> I have a directory called C:\Users\StarShip\PyProgs and it has the >> files >> BreakersCafe.txt and BreakersCafe.py. This is my primary program >> running >> fine. >> >> I have a subdirectory called C:\Users\StarShip\PyProg >> \PicturesForTesting >> and another subdirectory C:\Users\StarShip\PyProgs\CustomFunctions with >> various program files, functions defined in them, etc. which I import >> in >> my primary program. For example: >> >> def Newbanner(): >> print "\n Alternate Selections\n" >> >> Now I have the small program below which is fully self-contained and I >> want to execute it from within BreakersCafe.txt. I would like to use >> raw_input and if statement for simple yes/no asking if they would like >> to >> see this BeveragesMenu.txt and have it as the last 3-4 lines of the >> main(). >> >> The only directory of these three that has __init__ is >> C:\Users\StarShip\PyProgs\CustomFunctions >> and as you can see, the program below is not a function, it has no 'def' >> anything. I get 'NameError: global name 'BeveragesMenu' is not defined' >> when I run this in IDLE. I suppose I _could_ make this a function :} >> but >> it is likely doable to call a program as a program, right? I am also >> compiling to a .pyw file where applicable. >> >> Thanks for the help. >> >> Patty >> >> """ >> This is C:\Users\StarShip\PyProgs\BeveragesMenu.txt and >> BeveragesMenu.py. >> Program file for displaying an image using Tkinter built-in GUI >> functions. >> open root window, open file descriptor for image, open new window file >> descriptor to manipulate with Tkinter Label library function. >> >> Pic needs to be displayed as a banner. Uses the compound="top" argument >> to >> do this. >> >> --> take out the pack()function didn't work, pack()is required >> >> Uses import Tkinter >> >> Program using new breaker's jpg picture; bar choices are not selectable >> """ >> >> >> import Tkinter >> import ImageTk >> >> rootwindow = Tkinter.Tk() >> >> fhdl= >> ImageTk.Image.open("C:\Users\StarShip\PyProgs\PicturesForTesting\houseimage.jpg") >> image_var = ImageTk.PhotoImage(fhdl) >> >> whdl = Tkinter.Label(rootwindow, compound="top", image=image_var, >> text="\n\n!!!WELCOME TO BREAKER'S BREAKFAST BAR!!!\n\n\nBeverage >> Choices: >> Sparkling Water; Milk; Orange Juice; Apple Juice *OR*\n\n **From >> Our >> Bar** Smoothie; Breakers Sun Tea; Chai; Cafe Mocha; Latte; >> Mimosa") >> >> whdl.pack() >> rootwindow.mainloop() >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > From alan.gauld at btinternet.com Fri Dec 10 18:12:31 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Dec 2010 17:12:31 -0000 Subject: [Tutor] 'or' in assignment (not if statement)? References: <552185.23819.qm@web86708.mail.ird.yahoo.com> <4D022A64.60703@pearwood.info> Message-ID: "Steven D'Aprano" wrote >> Python knows that if val is true then it doesn't need to evaluate >> the second term that causes it to return val rather than 1. > > That's what makes it short circuiting, but that's not why it returns > the first argument. `or` in standard Pascal doesn't short-circuit. But Pascal returns a boolean result, it doesn't return the original value of either term, it returns the true Boolean value of the expression. > Take this example `or.p` file: I don't see what point you are trying to make? Other than that Pascal doesn't do short-circuit evaluation. But that doesn't relate to the fact that its a combination of Python doing short circuit evaluation plus returning the original value that enables the "val or 1" idiom to work. >> It's a boolean expression you would reasonably expect a true >> boolean result. > > That brought a smile to my face! That's very amusing, the idea that > *boolean algebra* is intuitively expected! It is if you are used to computer programming languages :-) So for example if I do print len("foo") I don't expect "bar" - even though bar does have the same length as "foo". The way most languages nowadays treat logic is inconsistent with how they treat other operations but done for the many conveniences that such an approach has over the more purist. eg if myString rather than if myString == "" etc. > As an experiment, offer to buy your wife dinner, and ask if she'd > prefer to go to an Italian or Chinese restaurant. :-) She would either answer "Yes" (she would like to go to one of them, and if I'm lucky she might give me a clue which!) or "No" (she would prefer neither, Indian perhaps...) > If she says that she doesn't understand the question, because > restaurants aren't True/False boolean values, then you might have a > point :) Yes, I agree that only a truue mathematician would reply like that. Alan G. From alan.gauld at btinternet.com Fri Dec 10 18:21:11 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Dec 2010 17:21:11 -0000 Subject: [Tutor] Meaning of -1 in Python References: Message-ID: "Ben Ganzfried" wrote > n = s.find('not') > b = s.find('bad') > if n != -1 and b != -1 and b > n: > s = s[:n] + 'good' + s[b+3:] > return s > > It's clear that n!=-1 and b!=-1 means something like : "if in the > string 's' we find the word "not" and in string 's' we find the word > "bad." Exactly the oopsite in fact. find() returns the index where the string is fouind. -1 means the string was not found Thats why inside the if block we can use the n and b values to slice the string. They represent the positions of the words we were looking for within the string. > On a deeper computational level, what is going on here? What > exactly > does Python take -1 to mean? Is it saying that since the string 's' > is indexed starting from 0 to len(s), and since -1 is not part of > that, that therefore something having the value of -1 will never be > in > the string? Exactly so. -1 is not a "valid" index (made more confusing by the fact that in Python it is because Python supports negatiove indexes! But forget about that temporarily!) This is a common convention in programming languages and Python has adopted it. Personally I think a return of None would have made more sense, but history has dictated otherwise! > If so, then how exactly does using negative numbers to > count a string work? I have read something about this earlier... Python allows you to use negative numbers to index a string in reverse. So an index of -1 actually indicates the last character, but for find() you have to forget about that... > Also, does the part: b>n mean, in this case: "bad comes after not in > the string 's'"? Yes, if the index of the 'b' in 'bad' is higher than the index of 'n' in 'not' then 'bad' comes after 'not'. read more about how Python functions work by using the help() function at the >>> prompt >>> help(''.find) Note you only type the name not the () after find HTH, Alan G. From mehgcap at gmail.com Fri Dec 10 20:14:30 2010 From: mehgcap at gmail.com (Alex Hall) Date: Fri, 10 Dec 2010 14:14:30 -0500 Subject: [Tutor] the "**" operator? Message-ID: Hi all, I was googling a way to do something like mydict=mydict.extend(additionaldict) and someone on a forum recommends this: mydict=dict(mydict, **additionaldict) What is the ** doing here? I tried to look it up, but Google seems to ignore it since it is punctuation. The poster on the forum says it will put all elements of additionaldict in mydict that are not already in mydict, which is the behavior I am looking for, but I am not sure why this would be so. TIA. -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From joel.goldstick at gmail.com Fri Dec 10 20:40:08 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 10 Dec 2010 14:40:08 -0500 Subject: [Tutor] the "**" operator? In-Reply-To: References: Message-ID: The value you provide as the second argument must be a dictionary. Google kwargs python for lots of more in depth info On Fri, Dec 10, 2010 at 2:14 PM, Alex Hall wrote: > Hi all, > I was googling a way to do something like > mydict=mydict.extend(additionaldict) > > and someone on a forum recommends this: > mydict=dict(mydict, **additionaldict) > What is the ** doing here? I tried to look it up, but Google seems to > ignore it since it is punctuation. The poster on the forum says it > will put all elements of additionaldict in mydict that are not already > in mydict, which is the behavior I am looking for, but I am not sure > why this would be so. TIA. > > -- > Have a great day, > Alex (msg sent from GMail website) > mehgcap at gmail.com; http://www.facebook.com/mehgcap > _______________________________________________ > 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 piotr-kam at o2.pl Fri Dec 10 20:41:02 2010 From: piotr-kam at o2.pl (=?utf-8?B?UGlvdHIgS2FtacWEc2tp?=) Date: Fri, 10 Dec 2010 20:41:02 +0100 Subject: [Tutor] the "**" operator? In-Reply-To: References: Message-ID: Dnia 10-12-2010 o 20:14:30 Alex Hall napisa?(a): > Hi all, > I was googling a way to do something like > mydict=mydict.extend(additionaldict) > > and someone on a forum recommends this: > mydict=dict(mydict, **additionaldict) > What is the ** doing here? As far as I know the ** indicates that the argument passed (here named: additionaldict) is a dictionary. One * would mean that it is a tuple. I'm not sure but it seams to me that the usual way to *update* a dictionary with the contents of an other dictionary is to use the .update dictionary method. Examples: >>> animals= {'cat': 'Bobo', 'dog': 'Rex'} >>> animals2= {'cow': 'some name that I can\'t think of right now', >>> 'bird': 'Falco'} >>> animals.update(animals2) >>> print animals {'bird': 'Falco', 'dog': 'Rex', 'cow': "some name that I can't think of right now", 'cat': 'Bobo'} >>> print animals2 {'bird': 'Falco', 'cow': "some name that I can't think of right now"} >>> Hope my answers are right and it helps, Piotr From hugo.yoshi at gmail.com Fri Dec 10 20:48:56 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 10 Dec 2010 20:48:56 +0100 Subject: [Tutor] the "**" operator? In-Reply-To: References: Message-ID: On Fri, Dec 10, 2010 at 8:14 PM, Alex Hall wrote: > Hi all, > I was googling a way to do something like > mydict=mydict.extend(additionaldict) > mydict.update(additionaldict) see also: http://docs.python.org/library/stdtypes.html#dict.update > and someone on a forum recommends this: > mydict=dict(mydict, **additionaldict) > What is the ** doing here? I tried to look it up, but Google seems to > ignore it since it is punctuation. The poster on the forum says it > will put all elements of additionaldict in mydict that are not already > in mydict, which is the behavior I am looking for, but I am not sure > why this would be so. TIA. > The * and **, when calling functions, are what's called extended calling syntax. It's a bit hard to explain, so I'm going to do a sort of clarify by example here: >>> def f(a, b, c): ... print a, b, c ... >>> f(1, 2, 3) 1 2 3 >>> a = [1, 2, 3] >>> f(*a) 1 2 3 >>> b = {'a': 2, 'b': 1, 'c': 3} >>> f(*b) a c b >>> f(**b) 2 1 3 In short, if a = [1, 2, 3], f(*a) is the same as f(1, 2, 3). if b = {'a': 2, 'b': 1, 'c': 3}, then f(**b) is the same as f(a=2, b=1, c=3). The * will iterate over its argument, and supply the resulting values as arguments to the function (iterating over a dict will return its keys, which is what you see with f(*b) here). ** requires a mapping type, and supplies the keys and values to the function as keyword arguments. * and ** are very flexible, and you can use them alongside with regular arguments: >>> f(1, *[2, 3]) 1 2 3 >>> f(1, **{'b': 2, 'c': 3}) 1 2 3 * and ** also have uses in function definition, where they sort of do the reverse. They can capture any number of regular or keyword arguments in a tuple or dictionary: >>> def f(*args, **kwargs): ... print args, kwargs ... >>> f() () {} >>> f(1, 2, 3) (1, 2, 3) {} >>> f(1, 2, 3, a=4, b=5) (1, 2, 3) {'a': 4, 'b': 5} >>> f(a=4, b=5) () {'a': 4, 'b': 5} Like with function calls, you can mix up regular arguments and */** arguments, as long as the resulting function is unambiguous: >>> def f(a, *args, **kwargs): ... print a, args, kwargs ... >>> f() Traceback (most recent call last): File "", line 1, in TypeError: f() takes at least 1 argument (0 given) >>> f(1) 1 () {} >>> f(1, 2, 3) 1 (2, 3) {} >>> f(1, 2, 3, b=4, c=5) 1 (2, 3) {'c': 5, 'b': 4} From modulok at gmail.com Fri Dec 10 21:14:34 2010 From: modulok at gmail.com (Modulok) Date: Fri, 10 Dec 2010 13:14:34 -0700 Subject: [Tutor] Writing to the terminal? Message-ID: List, Forgive me if I don't describe this well, I'm new to it: Assume I'm working in a command shell on a terminal. Something like tcsh on xterm, for example. I have a program which does *something*. Let's say it counts down from 10. How do I print a value, and then erase that value, replacing it with another value? Say I had something like '10' that appears, then wait a second, then the 10 is replaced by '9'... '8'.. and so forth. The point is, I don't want to print to a new line, nor do I want the new number to appear next to the previous number... I just want to change it in place. (If that makes any sense?) Think of console based progress counters in programs like fetch or wget, or lame. How do you do this in Python? -Modulok- From waynejwerner at gmail.com Fri Dec 10 21:34:56 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Fri, 10 Dec 2010 14:34:56 -0600 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: Message-ID: If you just want a single line you can use chr(13) which is a carriage return. If you want a more complex program you'll need a curses type library hth, wayne On 12/10/10, Modulok wrote: > List, > > Forgive me if I don't describe this well, I'm new to it: > > Assume I'm working in a command shell on a terminal. Something like > tcsh on xterm, for example. I have a program which does *something*. > Let's say it counts down from 10. How do I print a value, and then > erase that value, replacing it with another value? Say I had something > like '10' that appears, then wait a second, then the 10 is replaced by > '9'... '8'.. and so forth. The point is, I don't want to print to a > new line, nor do I want the new number to appear next to the previous > number... I just want to change it in place. (If that makes any > sense?) Think of console based progress counters in programs like > fetch or wget, or lame. > > How do you do this in Python? > -Modulok- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Sent from my mobile device From kb1pkl at aim.com Fri Dec 10 21:37:43 2010 From: kb1pkl at aim.com (Corey Richardson) Date: Fri, 10 Dec 2010 15:37:43 -0500 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: Message-ID: <4D028F97.6060900@aim.com> On 12/10/2010 3:14 PM, Modulok wrote: > List, > > Forgive me if I don't describe this well, I'm new to it: > > Assume I'm working in a command shell on a terminal. Something like > tcsh on xterm, for example. I have a program which does *something*. > Let's say it counts down from 10. How do I print a value, and then > erase that value, replacing it with another value? Say I had something > like '10' that appears, then wait a second, then the 10 is replaced by > '9'... '8'.. and so forth. The point is, I don't want to print to a > new line, nor do I want the new number to appear next to the previous > number... I just want to change it in place. (If that makes any > sense?) Think of console based progress counters in programs like > fetch or wget, or lame. > > How do you do this in Python? > -Modulok- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > I've never used it before, but I think the curses module does this. -------------- next part -------------- An HTML attachment was scrubbed... URL: From kb1pkl at aim.com Fri Dec 10 21:38:49 2010 From: kb1pkl at aim.com (Corey Richardson) Date: Fri, 10 Dec 2010 15:38:49 -0500 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: Message-ID: <4D028FD9.4000609@aim.com> On 12/10/2010 3:34 PM, Wayne Werner wrote: > If you just want a single line you can use chr(13) which is a carriage > return. If you want a more complex program you'll need a curses type > library > hth, wayne > > On 12/10/10, Modulok wrote: >> List, >> >> Forgive me if I don't describe this well, I'm new to it: >> >> Assume I'm working in a command shell on a terminal. Something like >> tcsh on xterm, for example. I have a program which does *something*. >> Let's say it counts down from 10. How do I print a value, and then >> erase that value, replacing it with another value? Say I had something >> like '10' that appears, then wait a second, then the 10 is replaced by >> '9'... '8'.. and so forth. The point is, I don't want to print to a >> new line, nor do I want the new number to appear next to the previous >> number... I just want to change it in place. (If that makes any >> sense?) Think of console based progress counters in programs like >> fetch or wget, or lame. >> >> How do you do this in Python? >> -Modulok- >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> Try that in the interactive interpreter, it doesn't work. >>> print "a" + chr(13) a (Python 2.6.6) From susana.delgado_s at utzmg.edu.mx Fri Dec 10 21:45:58 2010 From: susana.delgado_s at utzmg.edu.mx (Susana Iraiis Delgado Rodriguez) Date: Fri, 10 Dec 2010 14:45:58 -0600 Subject: [Tutor] 'ascii' codec can't decode byte Message-ID: Hello members: I need your help, I'm developing a python script to make an excel file... I've been working in this for a long time. The module will write some data from a .shp file. Unfortuanely that information has some characters unrecorgnized by ascii. I tried to fix this adding an unicode sentence, but it didn't work, the script is: import os, time,fnmatch from xlwt import Workbook from osgeo import ogr,gdal,osr from dbf import * gdal.AllRegister() file_list = [] folders = None for root, folders, files in os.walk( "R:\\" ): for filename in fnmatch.filter(files, '*.shp'): file_list.append(os.path.join(root, filename) wrkbk = Workbook() wksht = wrkbk.add_sheet('shp') wksht.row(0).write(0,'ruta') wksht.row(0).write(1,'archivo' wksht.row(0).write(2,'prj') wksht.row(0).write(3,'fecha_modificacion') wksht.row(0).write(4,'maquina_host') wksht.row(0).write(5,'usuario') for row, filepath in enumerate(file_list, start=1): wksht.row(row).write(0, unicode(filepath,errors='ignore')) (ruta, filename) = os.path.split(filepath) wksht.row(row).write(1, unicode(filename,errors='ignore')) n = os.path.splitext(filepath) p = n[0]+'.prj' if os.path.exists(p): prj_text = open(p, 'r').read() unicode(prj_text,errors='ignore') wksht.row(row).write(2,prj_text) else: wksht.row(row).write(2, 'Sin prj, no se puede determinar la proyeccion') wksht.row(row).write(4, unicode(socket.gethostname(),errors='ignore')) t = time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(os.path.getmtime(filepath))) wksht.row(row).write(3, unicode(t,errors='ignore')) wksht.row(row).write(5,unicode(os.environ.get("USERNAME"),errors='ignore')) wrkbk.save('C:\\Python26\\biblio\\biblio_shp.xls') When I run it, I got the next error: Warning 1: organizePolygons() received an unexpected geometry. Either a polygon with interior rings, or a polygon with less than 4 points, or a non-Polygon geometry. Return arguments as a collection. Warning 1: Geometry of polygon of fid 0 cannot be translated to Simple Geometry. All polygons will be contained in a multipolygon. El archivo R:\Regiones\BosquePrimavera\Redefinici?n de ptos del ANP del B la P\D atos GPS\Meridian\polnuevonm.shp no tiene dbf Traceback (most recent call last): File "", line 1, in File "crawler_shp.py", line 138, in wrkbk.save('C:\\Python26\\biblio\\biblio_shp.xls') File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 634, in save doc.save(filename, self.get_biff_data()) File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 609, in get_biff_d ata shared_str_table = self.__sst_rec() File "C:\Python26\lib\site-packages\xlwt\Workbook.py", line 571, in __sst_rec return self.__sst.get_biff_record() File "C:\Python26\lib\site-packages\xlwt\BIFFRecords.py", line 53, in get_biff _record self._add_to_sst(s) File "C:\Python26\lib\site-packages\xlwt\BIFFRecords.py", line 66, in _add_to_ sst u_str = upack2(s, self.encoding) File "C:\Python26\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2 us = unicode(s, encoding) UnicodeDecodeError: 'ascii' codec can't decode byte 0xf3 in position 9: ordinal not in range(128) >>> -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Dec 10 21:51:14 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 11 Dec 2010 07:51:14 +1100 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: Message-ID: <4D0292C2.2050306@pearwood.info> Modulok wrote: > List, > > Forgive me if I don't describe this well, I'm new to it: [snip description of a progress bar] Here's one way: import time, sys f = sys.stdout for i in range(20): time.sleep(1) # do some work f.write('=') f.flush() # make the change visible immediately else: f.write('\n') You might be able to get the same effect with print, but it's probably easier using sys.stdout directly. Here's another way, which *must* use stdout and not print. for i in range(20): percentage = i/20.0 spinner = '/-\\-'[i % 4] f.write("Progress: %5.2f%% %s %s>\r" % (percentage, spinner, '='*(i+1))) f.flush() time.sleep(1) else: f.write('\n') -- Steven From hugo.yoshi at gmail.com Fri Dec 10 22:28:47 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 10 Dec 2010 22:28:47 +0100 Subject: [Tutor] Writing to the terminal? In-Reply-To: <4D028FD9.4000609@aim.com> References: <4D028FD9.4000609@aim.com> Message-ID: On Fri, Dec 10, 2010 at 9:38 PM, Corey Richardson wrote: > > Try that in the interactive interpreter, it doesn't work. >>>> print "a" + chr(13) > a You forgot to print something after the carriage return. It works for me: Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print 'a'+chr(13)+'b' b >>> the carriage return resets the cursor to the beginning of the line, so you can write over what you wrote before. Hugo From echowit at aol.com Fri Dec 10 23:11:38 2010 From: echowit at aol.com (echowit at aol.com) Date: Fri, 10 Dec 2010 17:11:38 -0500 (EST) Subject: [Tutor] Globals as variables in a tkinter widget Message-ID: <8CD66CE54C507C1-A1C-A224@webmail-m043.sysops.aol.com> This code produces a good run. from tkinter import * root = Tk() class Application(Frame): global sv, Ptype, PtypeI, sel_rate_label, label label = Label(root) Ptype = 999 PtypeI = IntVar() W = 5 Y = 4 def sel(self): global W global Y Ptype = PtypeI.get() if Ptype <= 333: print('\nResulting Values:', PtypeI.get(),Ptype) else: print('Ptype Failure:',PtypeI.get()) print('Ptype Completed',Ptype,'\n') V = 2 X = 3 Z = X * 2 W = X + 6 Y = V ** 2 U = W * Y print("V: ",V," X: ",X," Z: ",Z," W: ",W," Y: ",Y," U: ",U,'\n') return def __init__(self,master=None): Frame.__init__(self) self.pack() sel_rate_label = Label(self) sel_rate_label.config(text = '\nSelect Goats Personality Type',) sel_rate_label.pack(anchor=N) MODES = [ ("1 Below Average", 000), ("2 Average", 111), ("3 Above Average", 222), ("4 Super Star", 333), ] for text, mode in MODES: b = Radiobutton(self, text=text, variable=PtypeI, value=mode, command = self.sel) b.pack(anchor=W) label.pack() return app = Application(master=root) app.mainloop() #root.destroy() This is the IDLE output. >>> ================================ RESTART ================================ >>> Resulting Value: 333 333 Ptype Completed 333 V: 2 X: 3 Z: 6 W: 9 Y: 4 U: 36 >>> Assigning U prior to recalculating W & Y: U = W * Y V = 2 X = 3 Z = X * 2 W = X + 6 Y = V ** 2 print("V: ",V," X: ",X," Z: ",Z," W: ",W," Y: ",Y," U: ",U,'\n') Produces: >>> ================================ RESTART ================================ >>> Resulting Values: 333 333 Ptype Completed 333 Exception in Tkinter callback Traceback (most recent call last): File "C:\Python31\lib\tkinter\__init__.py", line 1399, in __call__ return self.func(*args) File "C:\Larry\FCGCF\GPDB\Dev\EX_OK.py", line 18, in sel U = W * Y TypeError: can't multiply sequence by non-int of type 'str' My goal is to I learnto use a global variable as an argument inside a widget function, My Question is: Anybody know of some good on-line documentation about using GUIs to do more than say 'Hello World'? Thanx in advance, Larry Rochester AKA echowit at aol.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Dec 11 00:21:22 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Fri, 10 Dec 2010 23:21:22 +0000 (GMT) Subject: [Tutor] role playing game - help needed In-Reply-To: References: <000001cb9557$13e15920$3ba40b60$@com> <844426.52951.qm@web86702.mail.ird.yahoo.com> Message-ID: <448643.14907.qm@web86708.mail.ird.yahoo.com> Thanks again Alan. Much clearer now. One final part I don't understand. > >>>> "%d is the result of %d + %d" % (6+7,6,7) > >I understand (I think) the 6+7 part but why the ,6,7 after that. I could >see how either '6+7' or '6,7' would be the correct format but not both. The format string has 3 percent markers - all numbers. It is therefore expecting 3 values, so I have to give it them. The first marker gets 6+7, ie 13. The second gets 6 the third 7... Look at the first example in the loops topic in my tutorial. It uses variables in the same way rather than literal values, that might make it more obvious. Maybe :-) HTH, Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Dec 11 00:42:56 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Dec 2010 23:42:56 -0000 Subject: [Tutor] Writing to the terminal? References: Message-ID: "Modulok" wrote > Assume I'm working in a command shell on a terminal. Something like > tcsh on xterm, for example. I have a program which does *something*. > Let's say it counts down from 10. How do I print a value, and then > erase that value, replacing it with another value? This is one of those things that sounds like it should be easy but is in fact deceptively difficult to get right! It all depends on your terminal, and there are dozens of different terminal types and settings. Thats why Unix has a terminfo (or in some Unices termcap - even they can't agree! :-) library to define the different terminal types and try to bring some sanity to proceedings.But you need to learn the basic terminal control codes. In this case you need the backspace code which is Ctrl-H or chr(8). Try this >>> print "spam" + "chr(8)*2+"ear" If your terminal recognises chr(8) as backspace you should see "spear". If it doesn't you'll see someting like: spam^h^hear The only foolproof way to do this kind of thing is to use a library like curses which allows you to positioon the cursor and overwrite whatever was there. But curses is non-trivial to use - not ridiculously hard, but non trivial. You can get a basic intro to curses in the event-driven topic of my tutorial. BTW there is quite a good writeup on terminfo here: http://tldp.org/HOWTO/Text-Terminal-HOWTO-16.html and, as usual, Wikipedia has a fair writeup. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sat Dec 11 01:06:19 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 11 Dec 2010 00:06:19 -0000 Subject: [Tutor] Globals as variables in a tkinter widget References: <8CD66CE54C507C1-A1C-A224@webmail-m043.sysops.aol.com> Message-ID: wrote > This code produces a good run. Not sure what you mean by that. What is "a good run"? > from tkinter import * > root = Tk() > class Application(Frame): > global sv, Ptype, PtypeI, sel_rate_label, label This is bizarre. Globals are generally considered evil and to be avoided if possible. One way to avoid globals is to use classes. So why on earth are you trying to introduce globals ionto a class? What do you think you will achieve that using class variables can't? > label = Label(root) > Ptype = 999 > PtypeI = IntVar() > W = 5 > Y = 4 And in fact most of these should probably be instance variables defined within your init() method... def sel(self): global W global Y Ptype = PtypeI.get() if Ptype <= 333: print('\nResulting Values:', PtypeI.get(),Ptype) else: print('Ptype Failure:',PtypeI.get()) print('Ptype Completed',Ptype,'\n') V = 2 X = 3 Z = X * 2 W = X + 6 Y = V ** 2 U = W * Y print("V: ",V," X: ",X," Z: ",Z," W: ",W," Y: ",Y," U: ",U,'\n') return Mixing print statements and GUIS is valid for debugging, but these don't look like debnug statements... Are you sure you want these appearing in the console rather than the GUI? def __init__(self,master=None): Frame.__init__(self) You might want to pass the master in to Frame too... self.pack() sel_rate_label = Label(self) sel_rate_label.config(text = '\nSelect Goats Personality Type',) sel_rate_label.pack(anchor=N) MODES = [ ("1 Below Average", 000), ("2 Average", 111), ("3 Above Average", 222), ("4 Super Star", 333), ] for text, mode in MODES: b = Radiobutton(self, text=text, variable=PtypeI, value=mode, command = self.sel) b.pack(anchor=W) label.pack() Why use the global label here when you use the instance level labels elsewhere? Especially as you don't appear to use it? > Resulting Values: 333 333 > Ptype Completed 333 > Exception in Tkinter callback > Traceback (most recent call last): > File "C:\Python31\lib\tkinter\__init__.py", line 1399, in __call__ > return self.func(*args) > File "C:\Larry\FCGCF\GPDB\Dev\EX_OK.py", line 18, in sel > U = W * Y > TypeError: can't multiply sequence by non-int of type 'str' It seems to think that Y is a string. Your code suggests Y is V**2 or 4. Can you insertt a print statement just before the offending line to find out what W and Y are at that exact point? > My goal is to I learnto use a global variable as an argument inside > a widget function, Why do you think that will be useful? What do you think you can do with globals that you can't do with class or instance variables? > My Question is: Anybody know of some good on-line documentation > about using GUIs to do more than say 'Hello World'? There are a lot of GUI tutorials, some even use Tkinter. Even my tutorial includes the GUI in the case study and the event driven programming topics as well as the GUI topic itself. They don;t go far beyond hello world but they do take at least one step further. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From mehgcap at gmail.com Sat Dec 11 01:26:52 2010 From: mehgcap at gmail.com (Alex Hall) Date: Fri, 10 Dec 2010 19:26:52 -0500 Subject: [Tutor] the "**" operator? In-Reply-To: References: Message-ID: Thanks all! I thought update() would add an item even if it would be a duplicate, but apparently not. I also now better understand why I am always passing around *args and **kwargs when calling super(). Very interesting... On 12/10/10, Hugo Arts wrote: > On Fri, Dec 10, 2010 at 8:14 PM, Alex Hall wrote: >> Hi all, >> I was googling a way to do something like >> mydict=mydict.extend(additionaldict) >> > > mydict.update(additionaldict) > > see also: > http://docs.python.org/library/stdtypes.html#dict.update > >> and someone on a forum recommends this: >> mydict=dict(mydict, **additionaldict) >> What is the ** doing here? I tried to look it up, but Google seems to >> ignore it since it is punctuation. The poster on the forum says it >> will put all elements of additionaldict in mydict that are not already >> in mydict, which is the behavior I am looking for, but I am not sure >> why this would be so. TIA. >> > > The * and **, when calling functions, are what's called extended > calling syntax. It's a bit hard to explain, so I'm going to do a sort > of clarify by example here: > >>>> def f(a, b, c): > ... print a, b, c > ... >>>> f(1, 2, 3) > 1 2 3 >>>> a = [1, 2, 3] >>>> f(*a) > 1 2 3 >>>> b = {'a': 2, 'b': 1, 'c': 3} >>>> f(*b) > a c b >>>> f(**b) > 2 1 3 > > In short, if a = [1, 2, 3], f(*a) is the same as f(1, 2, 3). if b = > {'a': 2, 'b': 1, 'c': 3}, then f(**b) is the same as f(a=2, b=1, c=3). > > The * will iterate over its argument, and supply the resulting values > as arguments to the function (iterating over a dict will return its > keys, which is what you see with f(*b) here). ** requires a mapping > type, and supplies the keys and values to the function as keyword > arguments. > > * and ** are very flexible, and you can use them alongside with > regular arguments: > >>>> f(1, *[2, 3]) > 1 2 3 >>>> f(1, **{'b': 2, 'c': 3}) > 1 2 3 > > * and ** also have uses in function definition, where they sort of do > the reverse. They can capture any number of regular or keyword > arguments in a tuple or dictionary: > >>>> def f(*args, **kwargs): > ... print args, kwargs > ... >>>> f() > () {} >>>> f(1, 2, 3) > (1, 2, 3) {} >>>> f(1, 2, 3, a=4, b=5) > (1, 2, 3) {'a': 4, 'b': 5} >>>> f(a=4, b=5) > () {'a': 4, 'b': 5} > > Like with function calls, you can mix up regular arguments and */** > arguments, as long as the resulting function is unambiguous: > >>>> def f(a, *args, **kwargs): > ... print a, args, kwargs > ... >>>> f() > Traceback (most recent call last): > File "", line 1, in > TypeError: f() takes at least 1 argument (0 given) >>>> f(1) > 1 () {} >>>> f(1, 2, 3) > 1 (2, 3) {} >>>> f(1, 2, 3, b=4, c=5) > 1 (2, 3) {'c': 5, 'b': 4} > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From tim at johnsons-web.com Sat Dec 11 01:24:52 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Fri, 10 Dec 2010 15:24:52 -0900 Subject: [Tutor] Code evaluation inside of string fails with __get_item In-Reply-To: <8CD66CE54C507C1-A1C-A224@webmail-m043.sysops.aol.com> References: <8CD66CE54C507C1-A1C-A224@webmail-m043.sysops.aol.com> Message-ID: <20101211002452.GB3380@johnsons-web.com> I'm using Python 2.6.5. The following problem is coming from inside of a complex code base and involves an implementation that I have used for years, and is now failing to execute in certain conditions. This problem occurs with either of the follow two classes, which are 'lifted' from 'Python Cookbook'. Class code follows: class Eval: def __init__(self, globals=None, locals=None): self.globals = globals or {} self.locals = locals or None def __getitem__(self, key): if self.locals is None: self.locals = sys._getframe(1).f_locals key = key % self return eval(key, self.globals, self.locals) ## and this one: class Evalx: def __init__(self, localvals = None, globalvals = None): if localvals is None : self.locals = sys._getframe(1).f_locals else : self.locals = locals if globalvals is None : self.globals = sys._getframe(1).f_globals else : self.globals = globals def __getitem__(self,key): return eval(key,self.globals,self.locals) ## either are used the same way: To evaluate code inside of a string ## A larger description can be found at: ## http://code.activestate.com/recipes/66018-evaluating-code-inside-strings/ The execution looks like this: self.content = formatted_string % Evalx() ## where 'self' refers to an object of another class, that uses the ## Eval class Under certain circumstances, the embedded is code *not* executed. By inserting debugging stubs, I can see that the the Eval/Evalx instantiation does occur, but the overloaded function call to __get_item does *not* occur. I have also found that a seemingly unrelated event having to do with file I/O must be causing a side effect. However, to keep things simple, I am first asking the following question: What would cause __get_item__ not to be called? I can confirm by other testing that the embedded codde is properly composed with matching keywords. This is probably the most difficult to resolve problem I've ever run into in python. My lack of understand of the underlying code in the Eval classes is probably a contributing factor. TIA -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From emile at fenx.com Sat Dec 11 01:36:13 2010 From: emile at fenx.com (Emile van Sebille) Date: Fri, 10 Dec 2010 16:36:13 -0800 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: Message-ID: On 12/10/2010 12:14 PM Modulok said... > List, > > Forgive me if I don't describe this well, I'm new to it: > > Assume I'm working in a command shell on a terminal. Something like > tcsh on xterm, for example. I have a program which does *something*. > Let's say it counts down from 10. How do I print a value, and then > erase that value, replacing it with another value? Say I had something > like '10' that appears, then wait a second, then the 10 is replaced by > '9'... '8'.. and so forth. The point is, I don't want to print to a > new line, nor do I want the new number to appear next to the previous > number... I just want to change it in place. (If that makes any > sense?) Think of console based progress counters in programs like > fetch or wget, or lame. > > How do you do this in Python? > -Modulok- > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > The trick is starting python with the -u option: emile at paj39:~$ python -u Python 2.6.4rc2 (r264rc2:75497, Oct 20 2009, 02:55:11) [GCC 4.4.1] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> for ii in range(10): ... print " %s" % (ii,), ... time.sleep(2) ... print "\r", ... >>> Emile From hugo.yoshi at gmail.com Sat Dec 11 01:55:05 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sat, 11 Dec 2010 01:55:05 +0100 Subject: [Tutor] the "**" operator? In-Reply-To: References: Message-ID: On Sat, Dec 11, 2010 at 1:26 AM, Alex Hall wrote: > Thanks all! I thought update() would add an item even if it would be a > duplicate, but apparently not. I also now better understand why I am > always passing around *args and **kwargs when calling super(). Very > interesting... > Actually, it does. If a key is already present, the value will be overwritten (since you can't have a key with more than one value, obviously). If you don't want to overwrite the duplicates, filter them some_dict.update(x for x in additional_dict.items() if x[0] not in some_dict) From carroll at tjc.com Sat Dec 11 02:37:16 2010 From: carroll at tjc.com (Terry Carroll) Date: Fri, 10 Dec 2010 17:37:16 -0800 (PST) Subject: [Tutor] Meaning of -1 in Python In-Reply-To: References: Message-ID: On Fri, 10 Dec 2010, Alan Gauld wrote: > "Ben Ganzfried" wrote > >> n = s.find('not') >> b = s.find('bad') >> if n != -1 and b != -1 and b > n: >> s = s[:n] + 'good' + s[b+3:] >> return s >> >> It's clear that n!=-1 and b!=-1 means something like : "if in the >> string 's' we find the word "not" and in string 's' we find the word >> "bad." > > Exactly the oopsite in fact. > find() returns the index where the string is fouind. > -1 means the string was not found I don't think it's opposite; I think Ben and Alan are saying the same thing, albeit in different ways. Ben: n = s.find('not') n!=-1 ... means something like : "if in the string 's' we find the word "not" Yes; "n != -1" means "not not found", that is, found. (ditto for the discussion of "b!=-1") Alan: -1 means the string was not found Exactly. Both of you are saying that -1 means "not found"; Ben is expresing that negating that is equivalent to "found." From wprins at gmail.com Sat Dec 11 03:38:15 2010 From: wprins at gmail.com (Walter Prins) Date: Sat, 11 Dec 2010 02:38:15 +0000 Subject: [Tutor] 'or' in assignment (not if statement)? In-Reply-To: <4D01A373.4000705@pearwood.info> References: <4D01A373.4000705@pearwood.info> Message-ID: Hi Steven On 10 December 2010 03:50, Steven D'Aprano wrote: > Some languages (Pascal comes to mind) doesn't have short-circuit behaviour > at all. > Don't mean to nit pick, but in my experience it really depends on the compiler implementation and which version of Pascal you're talking about. Certainly, Borland's Turbo Pascal had and later Object Pascal (today called Delphi) has to this day short-circuit evaluation as default behaviour, although you can turn this off via a compiler switch if you want. (And as an aside, according to wikipedia ISO Pascal actually also allows but does not require support of short-circuit boolean evaluation.) It really depends on what your program does -- if your program contains functions with side-effects (a bad idea, but if it does) then short-circuit evaluation will probably break your code. On the other hand, not having short-circuit boolean expression evaluation can in most programming contexts be needlessly inefficient. Anyway, your general point is of course quite correct, so feel free to ignore my ramblings... Best, Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From wallenpb at gmail.com Sat Dec 11 04:23:18 2010 From: wallenpb at gmail.com (Bill Allen) Date: Fri, 10 Dec 2010 21:23:18 -0600 Subject: [Tutor] using a networked data file Message-ID: This is somewhat non-Python specific I have an idea for a Python application that I want to write at work. The application needs to have a data file be available to multiple users for access, read and write. I know that a typical database, such as mysql, would work ok. However, I am trying to keep managed infrastructure down to a minimum so I am considering using sqlite instead. Since there is not a database service running to handle requests to the sqlite database, how can I allow for multiple people trying to use this database at the same time? Is this practical? Thanks, Bill Allen -------------- next part -------------- An HTML attachment was scrubbed... URL: From tim at johnsons-web.com Sat Dec 11 04:32:17 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Fri, 10 Dec 2010 18:32:17 -0900 Subject: [Tutor] Code evaluation inside of string fails with __get_item Message-ID: <20101211033217.GC3380@johnsons-web.com> This is a resend. I note that the original had an incorrect `reply-to' ID attached to it. (sorry) ------------------------------------------------------------------ I'm using Python 2.6.5. The following problem is coming from inside of a complex code base and involves an implementation that I have used for years, and is now failing to execute in certain conditions. This problem occurs with either of the follow two classes, which are 'lifted' from 'Python Cookbook'. Class code follows: class Eval: def __init__(self, globals=None, locals=None): self.globals = globals or {} self.locals = locals or None def __getitem__(self, key): if self.locals is None: self.locals = sys._getframe(1).f_locals key = key % self return eval(key, self.globals, self.locals) ## and this one: class Evalx: def __init__(self, localvals = None, globalvals = None): if localvals is None : self.locals = sys._getframe(1).f_locals else : self.locals = locals if globalvals is None : self.globals = sys._getframe(1).f_globals else : self.globals = globals def __getitem__(self,key): return eval(key,self.globals,self.locals) ## either are used the same way: To evaluate code inside of a string ## A larger description can be found at: ## http://code.activestate.com/recipes/66018-evaluating-code-inside-strings/ The execution looks like this: self.content = formatted_string % Evalx() ## where 'self' refers to an object of another class, that uses the ## Eval class Under certain circumstances, the embedded is code *not* executed. By inserting debugging stubs, I can see that the the Eval/Evalx instantiation does occur, but the overloaded function call to __get_item does *not* occur. I have also found that a seemingly unrelated event having to do with file I/O must be causing a side effect. However, to keep things simple, I am first asking the following question: What would cause __get_item__ not to be called? I can confirm by other testing that the embedded codde is properly composed with matching keywords. This is probably the most difficult to resolve problem I've ever run into in python. My lack of understand of the underlying code in the Eval classes is probably a contributing factor. TIA -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From echowit at aol.com Sat Dec 11 07:38:38 2010 From: echowit at aol.com (echowit at aol.com) Date: Sat, 11 Dec 2010 01:38:38 -0500 (EST) Subject: [Tutor] Tutor Digest, Vol 82, Issue 45, Topic 3, Globals In-Reply-To: References: Message-ID: <8CD67152D87C929-3A4-C1A6@Webmail-d125.sysops.aol.com> -----Original Message----- From: tutor-request To: tutor Sent: Fri, Dec 10, 2010 6:33 pm Subject: Tutor Digest, Vol 82, Issue 45 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 r, 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 han "Re: Contents of Tutor digest..." oday's Topics: 1. Re: role playing game - help needed (ALAN GAULD) 2. Re: Writing to the terminal? (Alan Gauld) 3. Re: Globals as variables in a tkinter widget (Alan Gauld) 4. Re: the "**" operator? (Alex Hall) 5. Code evaluation inside of string fails with __get_item (Tim Johnson) --------------------------------------------------------------------- Message: 1 ate: Fri, 10 Dec 2010 23:21:22 +0000 (GMT) rom: ALAN GAULD o: Al Stern c: "tutor at python.org" ubject: Re: [Tutor] role playing game - help needed essage-ID: <448643.14907.qm at web86708.mail.ird.yahoo.com> ontent-Type: text/plain; charset="utf-8" Thanks again Alan. Much clearer now. One final part I don't understand. >>> "%d is the result of %d + %d" % (6+7,6,7) I understand (I think) the 6+7 part but why the ,6,7 after that. I could see how either '6+7' or '6,7' would be the correct format but not both. The format string has 3 percent markers - all numbers. t is therefore expecting 3 values, so I have to give it them. he first marker gets 6+7, ie 13. The second gets 6 he third 7... Look at the first example in the loops topic in my tutorial. t uses variables in the same way rather than literal values, hat might make it more obvious. Maybe :-) HTH, Alan G. ------------- next part -------------- n HTML attachment was scrubbed... RL: ------------------------------ Message: 2 ate: Fri, 10 Dec 2010 23:42:56 -0000 rom: "Alan Gauld" o: tutor at python.org ubject: Re: [Tutor] Writing to the terminal? essage-ID: ontent-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original Modulok" wrote > Assume I'm working in a command shell on a terminal. Something like tcsh on xterm, for example. I have a program which does *something*. Let's say it counts down from 10. How do I print a value, and then erase that value, replacing it with another value? This is one of those things that sounds like it should be easy but is n act deceptively difficult to get right! It all depends on your terminal, and there are dozens of different erminal ypes and settings. Thats why Unix has a terminfo (or in some Unices ermcap - even they can't agree! :-) library to define the different erminal ypes and try to bring some sanity to proceedings.But you need to earn the basic terminal control codes. In this case you need the backspace code which is Ctrl-H or chr(8). Try this >>> print "spam" + "chr(8)*2+"ear" If your terminal recognises chr(8) as backspace you should ee "spear". If it doesn't you'll see someting like: spam^h^hear The only foolproof way to do this kind of thing is to use a library ike urses which allows you to positioon the cursor and overwrite whatever as there. But curses is non-trivial to use - not ridiculously hard, ut on trivial. You can get a basic intro to curses in the event-driven topic f my tutorial. BTW there is quite a good writeup on terminfo here: ttp://tldp.org/HOWTO/Text-Terminal-HOWTO-16.html nd, as usual, Wikipedia has a fair writeup. -- lan Gauld uthor of the Learn to Program web site ttp://www.alan-g.me.uk/ ----------------------------- Message: 3 ate: Sat, 11 Dec 2010 00:06:19 -0000 rom: "Alan Gauld" o: tutor at python.org ubject: Re: [Tutor] Globals as variables in a tkinter widget essage-ID: ontent-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original echowit at aol.com> wrote > This code produces a good run. Not sure what you mean by that. What is "a good run"? > from tkinter import * root = Tk() class Application(Frame): global sv, Ptype, PtypeI, sel_rate_label, label This is bizarre. lobals are generally considered evil and to be avoided if possible. ne way to avoid globals is to use classes. o why on earth are you trying to introduce globals ionto a class? hat do you think you will achieve that using class variables can't? > label = Label(root) Ptype = 999 PtypeI = IntVar() W = 5 Y = 4 And in fact most of these should probably be instance ariables defined within your init() method... def sel(self): global W global Y Ptype = PtypeI.get() if Ptype <= 333: print('\nResulting Values:', typeI.get(),Ptype) else: print('Ptype Failure:',PtypeI.get()) print('Ptype Completed',Ptype,'\n') V = 2 X = 3 Z = X * 2 W = X + 6 Y = V ** 2 U = W * Y print("V: ",V," X: ",X," Z: ",Z," W: ",W," Y: ",Y," U: ,U,'\n') return Mixing print statements and GUIS is valid for debugging, ut these don't look like debnug statements... Are you sure ou want these appearing in the console rather than the GUI? def __init__(self,master=None): Frame.__init__(self) You might want to pass the master in to Frame too... self.pack() sel_rate_label = Label(self) sel_rate_label.config(text = '\nSelect Goats Personality ype',) sel_rate_label.pack(anchor=N) MODES = [ ("1 Below Average", 000), ("2 Average", 111), ("3 Above Average", 222), ("4 Super Star", 333), ] for text, mode in MODES: b = Radiobutton(self, text=text, variable=PtypeI, value=mode, command = elf.sel) b.pack(anchor=W) label.pack() Why use the global label here when you use the instance evel labels elsewhere? Especially as you don't appear to use it? > Resulting Values: 333 333 Ptype Completed 333 Exception in Tkinter callback Traceback (most recent call last): File "C:\Python31\lib\tkinter\__init__.py", line 1399, in __call__ return self.func(*args) File "C:\Larry\FCGCF\GPDB\Dev\EX_OK.py", line 18, in sel U = W * Y TypeError: can't multiply sequence by non-int of type 'str' It seems to think that Y is a string. our code suggests Y is V**2 or 4. an you insertt a print statement just before the offending ine to find out what W and Y are at that exact point? > My goal is to I learnto use a global variable as an argument inside a widget function, Why do you think that will be useful? hat do you think you can do with globals that you can't do with lass or instance variables? > My Question is: Anybody know of some good on-line documentation about using GUIs to do more than say 'Hello World'? There are a lot of GUI tutorials, some even use Tkinter. ven my tutorial includes the GUI in the case study and the event riven rogramming topics as well as the GUI topic itself. They don;t go far eyond hello world but they do take at least one step further. HTH, -- lan Gauld uthor of the Learn to Program web site ttp://www.alan-g.me.uk/ hanks for the speedy and thorough response. I'm brand new to Python and was trying old, lazy C+ tricks, I guess. Am going to take your advice, try to do it right and see what happens. 'preciate the code examples and the link, checked it out & like its style, anyway. Thanks again, Larry ----------------------------- Message: 4 ate: Fri, 10 Dec 2010 19:26:52 -0500 rom: Alex Hall o: Hugo Arts c: tutor at python.org ubject: Re: [Tutor] the "**" operator? essage-ID: ontent-Type: text/plain; charset=ISO-8859-1 Thanks all! I thought update() would add an item even if it would be a uplicate, but apparently not. I also now better understand why I am lways passing around *args and **kwargs when calling super(). Very nteresting... On 12/10/10, Hugo Arts wrote: On Fri, Dec 10, 2010 at 8:14 PM, Alex Hall wrote: > Hi all, > I was googling a way to do something like > mydict=mydict.extend(additionaldict) > mydict.update(additionaldict) see also: http://docs.python.org/library/stdtypes.html#dict.update > and someone on a forum recommends this: > mydict=dict(mydict, **additionaldict) > What is the ** doing here? I tried to look it up, but Google seems to > ignore it since it is punctuation. The poster on the forum says it > will put all elements of additionaldict in mydict that are not already > in mydict, which is the behavior I am looking for, but I am not sure > why this would be so. TIA. > The * and **, when calling functions, are what's called extended calling syntax. It's a bit hard to explain, so I'm going to do a sort of clarify by example here: >>> def f(a, b, c): ... print a, b, c ... >>> f(1, 2, 3) 1 2 3 >>> a = [1, 2, 3] >>> f(*a) 1 2 3 >>> b = {'a': 2, 'b': 1, 'c': 3} >>> f(*b) a c b >>> f(**b) 2 1 3 In short, if a = [1, 2, 3], f(*a) is the same as f(1, 2, 3). if b = {'a': 2, 'b': 1, 'c': 3}, then f(**b) is the same as f(a=2, b=1, c=3). The * will iterate over its argument, and supply the resulting values as arguments to the function (iterating over a dict will return its keys, which is what you see with f(*b) here). ** requires a mapping type, and supplies the keys and values to the function as keyword arguments. * and ** are very flexible, and you can use them alongside with regular arguments: >>> f(1, *[2, 3]) 1 2 3 >>> f(1, **{'b': 2, 'c': 3}) 1 2 3 * and ** also have uses in function definition, where they sort of do the reverse. They can capture any number of regular or keyword arguments in a tuple or dictionary: >>> def f(*args, **kwargs): ... print args, kwargs ... >>> f() () {} >>> f(1, 2, 3) (1, 2, 3) {} >>> f(1, 2, 3, a=4, b=5) (1, 2, 3) {'a': 4, 'b': 5} >>> f(a=4, b=5) () {'a': 4, 'b': 5} Like with function calls, you can mix up regular arguments and */** arguments, as long as the resulting function is unambiguous: >>> def f(a, *args, **kwargs): ... print a, args, kwargs ... >>> f() Traceback (most recent call last): File "", line 1, in TypeError: f() takes at least 1 argument (0 given) >>> f(1) 1 () {} >>> f(1, 2, 3) 1 (2, 3) {} >>> f(1, 2, 3, b=4, c=5) 1 (2, 3) {'c': 5, 'b': 4} - ave a great day, lex (msg sent from GMail website) ehgcap at gmail.com; http://www.facebook.com/mehgcap ----------------------------- Message: 5 ate: Fri, 10 Dec 2010 15:24:52 -0900 rom: Tim Johnson o: tutor at python.org ubject: [Tutor] Code evaluation inside of string fails with __get_item essage-ID: <20101211002452.GB3380 at johnsons-web.com> ontent-Type: text/plain; charset=us-ascii I'm using Python 2.6.5. he following problem is coming from inside of a complex code base nd involves an implementation that I have used for years, and is ow failing to execute in certain conditions. his problem occurs with either of the follow two classes, which are lifted' from 'Python Cookbook'. Class code follows: lass Eval: def __init__(self, globals=None, locals=None): self.globals = globals or {} self.locals = locals or None def __getitem__(self, key): if self.locals is None: self.locals = sys._getframe(1).f_locals key = key % self return eval(key, self.globals, self.locals) # and this one: lass Evalx: def __init__(self, localvals = None, globalvals = None): if localvals is None : self.locals = sys._getframe(1).f_locals else : self.locals = locals if globalvals is None : self.globals = sys._getframe(1).f_globals else : self.globals = globals def __getitem__(self,key): return eval(key,self.globals,self.locals) # either are used the same way: To evaluate code inside of a string # A larger description can be found at: # http://code.activestate.com/recipes/66018-evaluating-code-inside-strings/ he execution looks like this: elf.content = formatted_string % Evalx() # where 'self' refers to an object of another class, that uses the # Eval class nder certain circumstances, the embedded is code *not* executed. y inserting debugging stubs, I can see that the the Eval/Evalx instantiation oes occur, but the overloaded function call to __get_item does not* occur. I have also found that a seemingly unrelated event having o do with file I/O must be causing a side effect. However, to keep hings simple, I am first asking the following question: hat would cause __get_item__ not to be called? I can confirm by ther testing that the embedded codde is properly composed with atching keywords. This is probably the most difficult to resolve problem I've ever run nto in python. My lack of understand of the underlying code in the val classes is probably a contributing factor. TIA - im im at johnsons-web.com or akwebsoft.com ttp://www.akwebsoft.com ----------------------------- _______________________________________________ utor maillist - Tutor at python.org ttp://mail.python.org/mailman/listinfo/tutor nd of Tutor Digest, Vol 82, Issue 45 ************************************ -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Sat Dec 11 13:27:20 2010 From: __peter__ at web.de (Peter Otten) Date: Sat, 11 Dec 2010 13:27:20 +0100 Subject: [Tutor] Code evaluation inside of string fails with __get_item References: <20101211033217.GC3380@johnsons-web.com> Message-ID: Tim Johnson wrote: > This is a resend. I note that the original had an incorrect > `reply-to' ID attached to it. (sorry) > ------------------------------------------------------------------ > I'm using Python 2.6.5. > The following problem is coming from inside of a complex code base > and involves an implementation that I have used for years, and is > now failing to execute in certain conditions. > This problem occurs with either of the follow two classes, which are > 'lifted' from 'Python Cookbook'. Class code follows: > class Eval: > def __getitem__(self, key): > Under certain circumstances, the embedded is code *not* executed. The first step is to concentrate on just one class, at one point in the code. Then try to control the input to the method and find an argument where the code reproducibly fails. > By inserting debugging stubs, I can see that the the Eval/Evalx > instantiation does occur, but the overloaded function call to __get_item > does *not* occur. I have also found that a seemingly unrelated event > having to do with file I/O must be causing a side effect. However, to keep > things simple, I am first asking the following question: > What would cause __get_item__ not to be called? I can confirm by Nothing, provided (1) the method is spelt __getitem__ (two leading and two trailing underscores) (2) the left side is a python string with legal "%(...)s"-style format expressions. Given a format string s = "%(s.upper())s" try to feed it a real dict print s % {"s.upper()":"OK") # should print OK to verify that that precondition is met. Peter From smokefloat at gmail.com Sat Dec 11 13:32:30 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 11 Dec 2010 07:32:30 -0500 Subject: [Tutor] using a networked data file In-Reply-To: References: Message-ID: On Fri, Dec 10, 2010 at 10:23 PM, Bill Allen wrote: > This is somewhat non-Python specific ? I have an idea for a Python > application that I want to write at work.? The application needs to have a > data file be available to multiple users for access, read and write.?? I > know that a typical database, such as mysql, would work ok.?? However, I am > trying to keep managed infrastructure down to a minimum so I am considering > using sqlite instead.?? Since there is not a database service running to > handle requests to the sqlite database, how can I allow for multiple people > trying to use this database at the same time?? Is this practical? No expert, but is it a single db file per user on a server, or a single db file that adds a table for every user? > > Thanks, > Bill Allen > > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > From knacktus at googlemail.com Sat Dec 11 15:52:21 2010 From: knacktus at googlemail.com (Knacktus) Date: Sat, 11 Dec 2010 15:52:21 +0100 Subject: [Tutor] Ressources for licensing Message-ID: <4D039025.6030402@googlemail.com> Hi everyone, can anybody recommend a lib or some other ressources about license mechanisms of desktop applications written in python. I'm thinking of a license-key that can be used to limit the time the application can be used. I also need to exploit the usage of a license server. Cheers, Jan From thor at othala.us Sat Dec 11 17:25:49 2010 From: thor at othala.us (John Russell) Date: Sat, 11 Dec 2010 11:25:49 -0500 Subject: [Tutor] Slicing Tuples Message-ID: Last night I started working through a book (Beginning Python: Using Python 2.6 and Python 3.1) I bought to learn Python, and there is an example in it that doesn't make sense to me. There is an example on slicing sequences that goes like this: slice_me=("The", "next", "time", "we","meet","the","drinks","are","on","me") sliced_tuple=slice_me[5:9] print(sliced_tuple) which, results in ('drinks', 'are', 'on', 'me') there is an example a little further below that shows the same concept, only applying it to a string, and, the result is four characters long, starting at the 6th position. Now, I understand why they start where they do, because the counting starts at 0, so 5 is the 6th element. What I don't understand is why they end where they do. By my count, 5 to 9 would be 5 elements, not 4. With the tuple, I thought the result was because there wasn't enough elements left, but when I changed 5:9 to 5:8, it returned one less result. So, my question is this, and I realize that this is *very* basic - what is going on with the last element? Why is it returning one less than I think it logically should. Am I missing something here? There is not much of an explanation in the book, but I would really like to understand what's going on here. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tim at johnsons-web.com Sat Dec 11 17:42:32 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Sat, 11 Dec 2010 07:42:32 -0900 Subject: [Tutor] Code evaluation inside of string fails with __get_item In-Reply-To: References: <20101211033217.GC3380@johnsons-web.com> Message-ID: <20101211164232.GD3380@johnsons-web.com> * Peter Otten <__peter__ at web.de> [101211 03:41]: > (1) the method is spelt __getitem__ (two leading and two trailing > underscores) > > (2) the left side is a python string with legal "%(...)s"-style format > expressions. Given a format string > > s = "%(s.upper())s" > > try to feed it a real dict > > print s % {"s.upper()":"OK") # should print OK > > to verify that that precondition is met. Should be print s % {"s.upper()":"OK"} ## closing brace I've never had the occasion to use assert() or any other python - shooting tools, any thoughts on that? I am dealing with a programmatically composed format string, that originates from a source (html) file It may be 1)read from the file external to the object and the source string passed into the object at instantiation. The composed string is then translated correctly and the embedded is evaluated. 2)read from the source file 'within' the object namespace. In this case, the embedded code within the composed format is *not* evaluated. And I can observe the legal "%(...)s"-style format expressions. 3)*But* (and here is the kicker), if the composed format string is then dumped to a pickle file and then loaded from that pickle file it is then evaluated. :) As near as I can see. And that suggests a workaround. Unfortunately, no error messages are generated either at runtime or by pychecker. Thanks Peter -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From lie.1296 at gmail.com Sat Dec 11 17:54:18 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 12 Dec 2010 03:54:18 +1100 Subject: [Tutor] role playing game - help needed In-Reply-To: References: Message-ID: On 12/07/10 23:37, Robert Sj?blom wrote: > I've been told to use input() if I know that I'll only get integers, > and raw_input() for "everything." That is a bad piece of advice. You should only use input() when you can fully trust whoever doing the input (i.e. you). input() can accept any python expressions, and this means the user can potentially execute malicious code as well. >>> import subprocess >>> input("input: ") input: subprocess.Popen(('ping', 'www.google.com')) >>> PING www.l.google.com (66.102.11.104) 56(84) bytes of data. 64 bytes from syd01s01-in-f104.1e100.net (66.102.11.104): icmp_req=1 ttl=57 time=18.5 ms From hugo.yoshi at gmail.com Sat Dec 11 17:55:54 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sat, 11 Dec 2010 17:55:54 +0100 Subject: [Tutor] Slicing Tuples In-Reply-To: References: Message-ID: On Sat, Dec 11, 2010 at 5:25 PM, John Russell wrote: > Last night I started working through a book (Beginning Python: Using Python > 2.6 and Python 3.1) ?I bought to learn Python, and there is an example in it > that doesn't make sense to me. > There is an example on slicing sequences that goes like this: > slice_me=("The", "next", "time", "we","meet","the","drinks","are","on","me") > sliced_tuple=slice_me[5:9] > print(sliced_tuple) > which, results in > ('drinks', 'are', 'on', 'me') That's not really what happens for me, see here: >>> a = ("The", "next", "time", "we","meet","the","drinks","are","on","me") >>> a[5:9] ('the', 'drinks', 'are', 'on') >>> a[6:10] ('drinks', 'are', 'on', 'me') [6:10] should provide the result you showed. Please make sure to always copy-paste when quoting code, otherwise inaccuracies like this will happen. > there is an example a little further below that shows the same concept, only > applying it to a string, and, the result is ?four characters long, starting > at the 6th position. > Now, I understand why they start where they do, because the counting starts > at 0, so 5 is the 6th element. What I don't understand is why they end where > they do. By my count, 5 to 9 would be 5 elements, not 4. With the tuple, I > thought the result was because there wasn't enough elements left, but when I > changed 5:9 to 5:8, it returned one less result. > So, my question is this, and I realize that this is *very* basic - what is > going on with the last element? Why is it returning one less than I think it > logically should. Am I missing something here? There is not much of an > explanation in the book, but I would really like to understand what's going > on here. > Very simple, a slice of a[5:9] will return all elements x where 5 <= x < 9. That's actually 4 elements: 5, 6, 7, and 8. You'll notice that while the starting number of the slice is included, the end is not. You might think this strange, but python uses it everywhere. range does the same: >>> range(5, 9) [5, 6, 7, 8] There are some good reasons to make slices and ranges work like this. One, you can subtract the numbers in the slice to get the length of the slice, i.e. len(a_list[x:y]) == y - x. Two, cutting a list up in two is now very easy and intuitive: l == l[:b] + l[b:] Hugo From wallenpb at gmail.com Sat Dec 11 19:07:17 2010 From: wallenpb at gmail.com (Bill Allen) Date: Sat, 11 Dec 2010 12:07:17 -0600 Subject: [Tutor] using a networked data file In-Reply-To: References: Message-ID: David, Thanks for the feedback. I should have been more specific on the usage of the data. The data will be some email addresses, names, department, and an indicator if the email address is internal to the business or an external contact. So, one table with these being the fields in each record should suffice. The users will be presented an interface that allows them to select one or more recipients to which a standardized email with a PDF attachment will be sent. All of this is no problem. I was concerned that with more than one user at a time potentially accessing the SQLite db at a time would be a problem, but I see from the SQLite site and from some discussions here on Tutor that this is perfectly fine. The users may add information to the db also, but since such writes will be infrequent this too should be OK. At least, that is the impression that I have gotten from what I have read here and other places. Just wanting to confirm that I have understood this correctly. Also, any other suggestions are welcome. Thanks, Bill Allen On Sat, Dec 11, 2010 at 6:32 AM, David Hutto wrote: > On Fri, Dec 10, 2010 at 10:23 PM, Bill Allen wrote: > > This is somewhat non-Python specific I have an idea for a Python > > application that I want to write at work. The application needs to have > a > > data file be available to multiple users for access, read and write. I > > know that a typical database, such as mysql, would work ok. However, I > am > > trying to keep managed infrastructure down to a minimum so I am > considering > > using sqlite instead. Since there is not a database service running to > > handle requests to the sqlite database, how can I allow for multiple > people > > trying to use this database at the same time? Is this practical? > > No expert, but is it a single db file per user on a server, or a > single db file that adds a table for every user? > > > > > > Thanks, > > Bill Allen > > > > > > _______________________________________________ > > 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 Sun Dec 12 00:39:03 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 12 Dec 2010 10:39:03 +1100 Subject: [Tutor] Slicing Tuples In-Reply-To: References: Message-ID: <4D040B97.5000208@pearwood.info> John Russell wrote: > So, my question is this, and I realize that this is *very* basic - what is > going on with the last element? Why is it returning one less than I think it > logically should. Am I missing something here? There is not much of an > explanation in the book, but I would really like to understand what's going > on here. If the book really doesn't explain this (as opposed to you just having missed it), that's a fairly serious lack! Slicing in Python uses what is called "half-open intervals". There are four obvious ways to count slices: (1) Closed interval: both the start index and the end index are included. (2) Open interval: both the start index and end index are excluded. (3) Half-open interval: the start index is included, and the end index excluded. (4) Half-open interval (reversed sense): the start index is excluded, and the end index included. Python uses #3 because it is generally the most simple and least error-prone. Essentially, you should consider a slice like sequence[a:b] to be equivalent to "take a slice of items from seq, start at index=a and stop when you reach b". Because you stop *at* b, b is excluded. Indexes should be considered as falling *between* items, not on them: 0.1.2.3.4.5.6.7.8 |a|b|c|d|e|f|g|h| Run an imaginary knife along the line marked "4", and you divide the sequence abcdefgh into two pieces: abcd and efgh. Why are half-open intervals less error-prone? Because you have to adjust by 1 less often, and you have fewer off-by-one errors. E.g. * Take n items starting from index i: Half-open: seq[i:i+n] Closed: seq[i:i+n-1] * Length of a slice [i:j]: Half-open: j-i Closed: j-i+1 * Dividing a sequence into two slices with no overlap: Half-open: s = seq[:i] t = seq[i:] Closed: s = seq[:i-1] t = seq[i:] * Splitting a string at a delimiter: s = "abcd:efgh" We want to split the string into two parts, everything before the colon and everything after the colon. Half-open: p = s.find(':') before = s[:p] after = s[p+1:] Closed: p = s.find(':') before = s[:p-1] after = s[p+1:] * Empty slice: Half-open: seq[i:i] is empty Closed: seq[i:i] has a single item So half-open (start included, end excluded) has many desirable properties. Unfortunately, it has two weaknesses: - most people intuitively expect #1; - when slicing in reverse, you get more off-by-one errors. There's not much you can do about people's intuition, but since reverse slices are much rarer than forward slices, that's a reasonable cost to pay. -- Steven From bouncingcats at gmail.com Sun Dec 12 00:47:05 2010 From: bouncingcats at gmail.com (David) Date: Sun, 12 Dec 2010 10:47:05 +1100 Subject: [Tutor] Beginning Python and other resources (was Re: Slicing Tuples) Message-ID: On 12 December 2010 03:25, John Russell wrote: > Last night I started working through a book (Beginning Python: Using Python > 2.6 and Python 3.1) I bought to learn Python, and there is an example in it > that doesn't make sense to me. I have that book too, and several others thankfully. I'm just writing to share my experience, in case another perspective from another relatively new python user is helpful to anyone. I found that book is written towards a friendly beginner level, but it omits many details and is not a comprehensive reference. It does include plenty of examples on many topics. I have read the opposite criticism of other books, so it is probably impossible for one book to fit all needs. Personally I became weary of the food/fridge/kitchen theme of the early examples as it did not engage my interest at all, and there is so much of it. However I have found that book valuable for its Part III where it covers useful applications. Although I have come to expect that its examples will often need typos corrected or other small modifications to get them to run properly on Python 2.6. So while working through Parts I and II, if your experience is anything like mine where I moved away from it fairly quickly, you will definitely need other resources. Fortunately they are abundantly available. In case you are unaware, a Tutorial and Reference are integrated with Python. On my Linux box the tutorial is file:///usr/share/doc/python-docs-x.x.x/html/tutorial/index.html and the Alphabetic Index to the Reference is file:///usr/share/doc/python-docs-x.x.x/html/genindex.html I find the Tutorial easily readable, whereas the Reference can be challenging, but it is comprehensive and likely contains the answer to most questions, somewhere. As an exercise I thought I'd try to find the answer to your question using these built-in docs. The keyword is "slice" which we know from the book. I couldn't find any coverage in the 2.6 tutorial so I looked up "slice" in the Alphabetic Index, and the first link took me to a section which includes the answer: "The slicing now selects all items with index k such that i <= k < j where i and j are the specified lower and upper bounds". In case you are not aware, the web has a vast amount of great material for learning Python, see for example: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers As Python is an evolving language, one needs be mindful of the differences between Python 2.x and Python 3.x when using this material. My favourite Python book of my small beginners collection is "Python 3 Object Oriented Programming" by Dusty Phillips. It claims: "If you're new to object-oriented programming techniques, or if you have basic Python skills, and wish to learn in depth how and when to correctly apply object-oriented programming in Python, this is the book for you". I endorse that completely. Having learned the syntax basics elsewhere, for me this is a book like no other. I find its topic coverage and all its short examples consistently useful, powerful, and illuminating. I find it easy to read, well matched to my level and interest, and it has made a real difference to the code I write. I use classes with confidence now. For example, it gave me the background and confidence to design a custom sortable abstract class I needed, my first personal experience of the power of OOP. Tthe python and tutorial mailing lists are a wonderul resource as you are obviously aware. Thanks to all the contributors from whom I continue to learn. From albstern at gmail.com Sun Dec 12 01:10:52 2010 From: albstern at gmail.com (Al Stern) Date: Sat, 11 Dec 2010 18:10:52 -0600 Subject: [Tutor] Dictionaries - Using 1 Key:Value to find another Message-ID: This was another execise in my book. Following is my code for a program that uses dictionaries to find and edit pairs of fathers and sons. The program works right up to the final step which is to find out if any given father is actually a grandfather to someone else in the dictionary. I set up multiple generations in the dictionary so there would be available matches. # father son "who's your daddy" program #create a dictionary of father son pairs. Include multiple generations. pairs = {"Joel" : "Al", "Bill" : "Joel", "Joseph" : "Bill", "Palmer" : "Bob", "Bob" : "Will", "Al" : "Royal"} fathers = pairs.keys() sons = pairs.values() choice = None while choice != "0": print(""" Welcome to Who's Your Daddy? A mini database of fathers and sons. When entering names, please capitilize the 1st letter of each name. 0 - Exit 1 - List all Father-Son Pairs 2 - List all Fathers 3 - Look Up Father-Son Pairs 4 - Add a Father-Son Pair 5 - Delete a Father-Son Pair 6 - Replace the Son of a Father-Son Pair 7 - Replace a Father-Son Pair 8 - Look up Grandfather_Father_Son Pairs """) choice = input("Choice: ") # To end the program if choice == "0": print ("Thanks for playing. Goodbye.") # List pairs elif choice == "1": print ("List of all Father and Son pairs: ") print (pairs) elif choice == "2": print ("List of all Fathers: ") for fathers in pairs: print (fathers) elif choice == "3": print ("Look up Father-Son pairs: ") father = input ("What is the name of the father in the pair you want to look up? ") if father in pairs: son = pairs[father] print ("\n", father, "is the father of",son, ".") else: print ("\nSorry. That father does not exist.") elif choice == "4": print ("Add a Father-Son pair: ") new_father = input ("What is the name of the father you wish to add? ") if new_father not in pairs: new_son = input ("What is the name ofthe son you wish to add? ") pairs[new_father] = new_son print ("\n", new_father, "has been added.") else: print ("\nThat Father-Son pair already exists. Try again.") elif choice == "5": print ("Delete a Father-Son pair: ") del_father = input ("What is the name of the father in the pair you wish to delete? ") if del_father in pairs: del pairs[del_father] print ("\nThe pair in which", del_father, "is the father has been deleted.") else: print ("\nSorry.", del_father, "is not a valid father name.") elif choice == "6": print ("Replace the son of a Father-Son Pair: ") son_old = input ("What is the name of the father whose son you wish to replace? ") if son_old in pairs: son_new = input ("What is the name of the new son in this pair? ") pairs[son_old] = son_new print (son_new, "has replaced", son_old, "in the list.") else: print ("Sorry, that son does not exist. Try again.") elif choice == "7": print ("Replace a Father-Son pair: ") father = input ("Who is the father in the Father-Son pair do you which to replace? ") if father in pairs: father = input("What is the name of the father in the new Father-Son pair? ") son = input("What is the name of the son in the new Father-Son pair? ") pairs[father] = son print ("\nThe new pair of", father, "and", son, "have been added to the list.") else: print ("Sorry, that father does not exist. Try again.") elif choice == "8": print ("Find the grandson of a person in the list.") grandfather = input("Which father do you want to look up to see if he is a grandfather? ") if grandfather in pairs: father == pairs[grandfather] for father in pairs: grandson == pairs[father] print (grandfather, "is the grandfather of", grandson, ".") else: print ("Sorry.", grandfather, "is not a grandfather. Try again.") input ("\n\nPress the enter key to exit.\n") Choice 8 gives me the following error... Traceback (most recent call last): File "C:\Users\Public\Documents\My Python programs\father_son.py", line 105, in father == pairs[grandfather] NameError: name 'father' is not defined I thought father got defined in the father == pairs[grandfather] line. I have tried it a couple different ways but always get the father is not defined error once I enter the name. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ajarncolin at gmail.com Sun Dec 12 01:34:05 2010 From: ajarncolin at gmail.com (col speed) Date: Sun, 12 Dec 2010 07:34:05 +0700 Subject: [Tutor] Dictionaries - Using 1 Key:Value to find another In-Reply-To: References: Message-ID: On 12 December 2010 07:10, Al Stern wrote: > This was another execise in my book. Following is my code for a program > that uses dictionaries to find and edit pairs of fathers and sons. The > program works right up to the final step which is to find out if any given > father is actually a grandfather to someone else in the dictionary. I set > up multiple generations in the dictionary so there would be available > matches. > > elif choice == "8": > print ("Find the grandson of a person in the list.") > grandfather = input("Which father do you want to look up to see if > he is a grandfather? ") > if grandfather in pairs: > father == pairs[grandfather] > for father in pairs: > grandson == pairs[father] > print (grandfather, "is the grandfather of", grandson, ".") > else: > print ("Sorry.", grandfather, "is not a grandfather. Try > again.") > > > > input ("\n\nPress the enter key to exit.\n") > > Choice 8 gives me the following error... > > Traceback (most recent call last): > File "C:\Users\Public\Documents\My Python programs\father_son.py", line > 105, in > father == pairs[grandfather] > NameError: name 'father' is not defined > > I thought father got defined in the > > father == pairs[grandfather] > line. I have tried it a couple different ways but always get the father is > not defined error once I enter the name. > > I'm sure somebody has beaten me to it, but: You have used "==" rather than "=" -------------- next part -------------- An HTML attachment was scrubbed... URL: From bouncingcats at gmail.com Sun Dec 12 01:34:52 2010 From: bouncingcats at gmail.com (David) Date: Sun, 12 Dec 2010 11:34:52 +1100 Subject: [Tutor] Dictionaries - Using 1 Key:Value to find another In-Reply-To: References: Message-ID: On 12 December 2010 11:10, Al Stern wrote: > > I thought father got defined in the > > father == pairs[grandfather] > line. I have tried it a couple different ways but always get the father is > not defined error once I enter the name. I only glanced at your code, but maybe you have some typos there: father == pairs[grandfather] for father in pairs: grandson == pairs[father] should probably be father = pairs[grandfather] for father in pairs: grandson = pairs[father] Try that. From albstern at gmail.com Sun Dec 12 02:13:34 2010 From: albstern at gmail.com (Al Stern) Date: Sat, 11 Dec 2010 19:13:34 -0600 Subject: [Tutor] Dictionaries - Using 1 Key:Value to find another In-Reply-To: References: Message-ID: <00b201cb9999$cd5c37d0$6814a770$@com> Ah. Turns out I was just sloppy and/or stupid. I realized I had another error once I fixed the '==' part. My new code which seems to work is... elif choice == "8": print ("Find the grandson of a person in the list.") grandfather = input("Which father do you want to look up to see if he is a grandfather? ") if grandfather in pairs: father = pairs[grandfather] grandson = pairs[father] print (grandfather, "is the grandfather of", grandson, ".") else: print ("\nSorry.", grandfather, "is not a grandfather. Try again.") Thanks all. _________________ ? www.electronsbaseball.com -----Original Message----- From: David [mailto:bouncingcats at gmail.com] Sent: Saturday, December 11, 2010 6:35 PM To: Al Stern Cc: tutor at python.org Subject: Re: [Tutor] Dictionaries - Using 1 Key:Value to find another On 12 December 2010 11:10, Al Stern wrote: > > I thought father got defined in the > > father == pairs[grandfather] > line. I have tried it a couple different ways but always get the father is > not defined error once I enter the name. I only glanced at your code, but maybe you have some typos there: father == pairs[grandfather] for father in pairs: grandson == pairs[father] should probably be father = pairs[grandfather] for father in pairs: grandson = pairs[father] Try that. From lie.1296 at gmail.com Sun Dec 12 02:52:35 2010 From: lie.1296 at gmail.com (Lie Ryan) Date: Sun, 12 Dec 2010 12:52:35 +1100 Subject: [Tutor] 'or' in assignment (not if statement)? In-Reply-To: References: <552185.23819.qm@web86708.mail.ird.yahoo.com> <4D022A64.60703@pearwood.info> Message-ID: On 12/11/10 04:12, Alan Gauld wrote: > "Steven D'Aprano" wrote >> As an experiment, offer to buy your wife dinner, and ask if she'd >> prefer to go to an Italian or Chinese restaurant. > > :-) > She would either answer "Yes" (she would like to go to one of > them, and if I'm lucky she might give me a clue which!) or "No" > (she would prefer neither, Indian perhaps...) The question "Would you like Italian or Chinese for dinner" is actually a contraction of "Would you like Italian for dinner or would you like Chinese for dinner". If we ask these two questions separately to the wife, we get either "Yes or Yes", "Yes or No", "No or Yes", or "No or No", which evaluates to either "Yes", "Yes", "Yes", and "No" (use "True" or "False", if you prefer). Or syntactically: =================== I(i) = Italian (an object) I(c) = Chinese (an object) I(W) = Indication of Want (a unary relation) ------------------- W(i) or W(c) =================== In short circuiting language, that question is translated to: "If you want Italian for dinner then answer Italian, else answer Chinese". Or syntactically: =================== I(i) = Italian (an object) I(c) = Chinese (an object) I(W) = Indication of Want (a unary relation) I(E) = Eat at (a unary relation) ------------------- if W(i) then i else c --- or --- if W(i) then Ei if not W(i) then Ec =================== Neither of the two previous translations corresponds to the intuition we had in natural language. Instead, in natural language, the best translation is probably "If you prefer Italian over Chinese for dinner then answer Italian, else if you prefer Chinese over Italian for dinner then answer Chinese (assume she cannot answer Neither or Both). Or syntactically: =================== I(i) = Italian (an object) I(c) = Chinese (an object) I(>) = Wife's Order of Preference (a strict weak order binary relation) I(E) = Eat at (a unary relation) ------------------- if i > c then Ei if c > i then Ec =================== From steve at pearwood.info Sun Dec 12 03:18:44 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 12 Dec 2010 13:18:44 +1100 Subject: [Tutor] Code evaluation inside of string fails with __get_item In-Reply-To: <20101211164232.GD3380@johnsons-web.com> References: <20101211033217.GC3380@johnsons-web.com> <20101211164232.GD3380@johnsons-web.com> Message-ID: <4D043104.3040403@pearwood.info> Tim Johnson wrote: > I've never had the occasion to use assert() or any other > python - shooting tools, any thoughts on that? Assertions are a great tool, but never ever, under pain of great pain, use assert for testing user input or function arguments. It's tempting to knock up a quick and dirty function like this: def spam(n): assert n >= 0, "amount of spam must be non-negative" do_stuff_with(n) Beware! This way leads to the Dark Side, or at least to strange and mysterious bugs that will only effect a few of your users and have you scratching your head. The problem is that if you run Python with optimizations turned on, assertions are disabled. When run with python -O, your error checking disappears: def spam(n): do_stuff_with(n) Strange and terrible things may now occur, exceptions being the *least* of your worries. Better to write explicit tests and give sensible errors: def spam(n): if n < 0: raise ValueError("amount of spam must be non-negative") do_stuff_with(n) If you've ever written a comment like "This can't happen, but if it does, raise an exception", then you've essentially made an assertion. Here's a trivial example: def spam(s): s += " spam spam spam glorious SPAM!!!" p = s.find("spam") if p < 1: # This can never happen! raise RuntimeError("Serious internal error #123456") do_something_with(p) This could be written as: def spam(s): s += " spam spam spam glorious SPAM!!!" p = s.find("spam") assert p >= 1, "Serious internal error #123456" do_something_with(p) Now if you want to speed up your program by skipping all those "can never happen" tests, you can run python -O and the asserts will be compiled out. Assertions are for testing program logic, not for testing user input. You should assume that the caller should never see your assertions: if the caller ever receives an AssertionError, you have failed. (If they receive a ValueError, or similar, that's *their* fault for passing rubbish input to your function.) Assertions are for making statements about *internal* state. Having said that, sometimes it's hard to decide what counts as an internal state. Can function arguments ever be internal state? Sometimes I do things like this: def ham(n): if n < 0: raise ValueError("amount of ham must be non-negative") x = _common(n) return "Ham is like spam but not as tasty." + x def spam(n): if n < 0: raise ValueError("amount of spam must be non-negative") x = _common(n) return "Spam, glorious SPAM!!!" + x def _common(n): # Internal function. assert n >= 0 do_stuff_here(n) return something Since _common is an internal function which the caller is not supposed to use directly, I feel it is acceptable to treat the input to _common as an internal state. If the caller wants to mess with my internal functions, they're responsible for whatever horrible things happen. Another good example of assertions is for checking pre-conditions and post-conditions, particularly post-conditions. Here's a real example: in my stats module, I calculate "r", the Pearson's Correlation Coefficient. It doesn't matter what that means, but what does matter is that the result *must* be between -1 and 1, or else my code has a bug in it. Even though my code is perfectly bug-free *cough*, I end the function with the line: assert -1.0 <= r <= 1.0 to ensure that if there is a bug in my code, it will raise an exception rather than return a garbage result. If the user wants to live dangerously, they can run with the optimization flag and skip my assertions. > I am dealing with a programmatically composed format > string, that originates from a source (html) file Since the format string is generated by you, then errors in the format string are your responsibility, and they're an internal detail. Use assertions for checking the format string. However, the HTML source is not an internal detail (at least I wouldn't expect it to be), it is *user* input, so any checks you do while processing the HTML should not use assertions. > It may be > > 1)read from the file external to the object > and the source string passed into the object at instantiation. > The composed string is then translated correctly and > the embedded is evaluated. Who makes that choice? You, or the caller? If the caller, then any errors that occur are not internal state, and you shouldn't use assert. If you, then it's an internal detail and you can use assert. -- Steven From smokefloat at gmail.com Sun Dec 12 03:21:40 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 11 Dec 2010 21:21:40 -0500 Subject: [Tutor] Slicing Tuples In-Reply-To: <4D040B97.5000208@pearwood.info> References: <4D040B97.5000208@pearwood.info> Message-ID: So, in essence, that would be redefining(in Python) basic division of grammatical structures(splices)? From smokefloat at gmail.com Sun Dec 12 03:37:58 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 11 Dec 2010 21:37:58 -0500 Subject: [Tutor] role playing game - help needed In-Reply-To: References: Message-ID: On Sat, Dec 11, 2010 at 11:54 AM, Lie Ryan wrote: > On 12/07/10 23:37, Robert Sj?blom wrote: >> I've been told to use input() if I know that I'll only get integers, >> and raw_input() for "everything." > > That is a bad piece of advice. You should only use input() when you can > fully trust whoever doing the input (i.e. you). Who uses the crap we, as noobies produce? It's pie in the sky mentality. We design it because WE want it and WE(individually) use it. input() can accept any > python expressions, and this means the user can potentially execute > malicious code as well. > >>>> import subprocess >>>> input("input: ") > input: subprocess.Popen(('ping', 'www.google.com')) > >>>> PING www.l.google.com (66.102.11.104) 56(84) bytes of data. > 64 bytes from syd01s01-in-f104.1e100.net (66.102.11.104): icmp_req=1 > ttl=57 time=18.5 ms > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From smokefloat at gmail.com Sun Dec 12 03:42:46 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 11 Dec 2010 21:42:46 -0500 Subject: [Tutor] Ressources for licensing In-Reply-To: <4D039025.6030402@googlemail.com> References: <4D039025.6030402@googlemail.com> Message-ID: On Sat, Dec 11, 2010 at 9:52 AM, Knacktus wrote: > Hi everyone, > > can anybody recommend a lib or some other ressources about license > mechanisms of desktop applications written in python. I'm thinking of a > license-key that can be used to limit the time the application can be used. > I also need to exploit the usage of a license server. You probably need: def license_this(self): print "touch mine" license_this(self) > > Cheers, > > Jan > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Sun Dec 12 03:56:33 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 12 Dec 2010 13:56:33 +1100 Subject: [Tutor] 'or' in assignment (not if statement)? In-Reply-To: References: <552185.23819.qm@web86708.mail.ird.yahoo.com> <4D022A64.60703@pearwood.info> Message-ID: <4D0439E1.1080305@pearwood.info> Lie Ryan wrote: > The question "Would you like Italian or Chinese for dinner" is actually > a contraction of "Would you like Italian for dinner or would you like > Chinese for dinner". If we ask these two questions separately to the > wife, we get either "Yes or Yes", "Yes or No", "No or Yes", or "No or > No", which evaluates to either "Yes", "Yes", "Yes", and "No" (use "True" > or "False", if you prefer). Or syntactically: In natural language (at least in English, other languages may have other conventions), "or" generally has a meaning closer to exclusive-or (xor) than to the logical disjunction (boolean "or"): "We can go out, or we can stay home." "Take the money, or the box." "You must find the defendant guilty or not guilty." "The cat is either inside the box, or outside the box." "Your money, or your life." You can't do both at the same time. Even when the two alternatives aren't strictly contradictory, it's often assumed that only one will hold: "Would you like tea or coffee?" It would be surprising if somebody wanted both. (Particularly if they were served in the same cup -- my wife once ordered a chai latte at a cafe. The waiter had no idea what that was, but must have known that "chai" means tea, and so mixed tea and coffee in the same cup and served it with milk. And yes, the result was as horrible as it sounds.) We often make inclusivity an explicit choice: "Dinner, or a movie, or both?" Quoting from Websters Dictionary [1913]: A particle that marks an alternative; as, you may read or may write, -- that is, you may do one of the things at your pleasure, but not both. It corresponds to either. You may ride either to London or to Windsor. It often connects a series of words or propositions, presenting a choice of either; as, he may study law, or medicine, or divinity, or he may enter into trade. Having said that, "or" in natural language is not precisely logical-xor either. I can't think of any natural question "would you like A or B?" where the answer "No" is appropriate if you would like both. Natural language is also far more flexible, and frequently allows choices that aren't explicitly enumerated: Waiter: "Tea or coffee?" Person A: "Nothing for me." Person B: "Hot chocolate please." Steve Martin: "I'll have a tall fair-trade organic half double-decaf half caf low-fat soy latte with a twist of lemon." Logician: "Yes." The reality is, there's no one-to-one correspondence between natural language constructs and boolean algebra. -- Steven From steve at pearwood.info Sun Dec 12 04:39:19 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 12 Dec 2010 14:39:19 +1100 Subject: [Tutor] role playing game - help needed In-Reply-To: References: Message-ID: <4D0443E7.6090109@pearwood.info> David Hutto wrote: > On Sat, Dec 11, 2010 at 11:54 AM, Lie Ryan wrote: >> On 12/07/10 23:37, Robert Sj?blom wrote: >>> I've been told to use input() if I know that I'll only get integers, >>> and raw_input() for "everything." >> That is a bad piece of advice. You should only use input() when you can >> fully trust whoever doing the input (i.e. you). > > Who uses the crap we, as noobies produce? It's pie in the sky > mentality. We design it because WE want it and WE(individually) use > it. Do you want to learn good habits or learn bad habits? I think we've seen plenty of evidence on this mailing list that you have little interest in learning good habits, but actively defend your right learn bad habits. There are plenty of people who do the same. They're harmless and even pathetically amusing as newbies, and then they get a job working as a professional programmer, and end up writing crappy, bug-addled code filled with the sort of n00b errors that we've been warning about. Bug-addled code with *real* consequences. Command injection bugs are hugely common in the real world. At least four of the 25 most common security bugs in *professional* software are in my opinion varieties of the command injection flaw, and one of those is the SECOND most common flaw: SQL injection attack #2 most common Unrestricted upload of dangerous files #8 most common OS command injection #9 most common PHP file inclusion attack #13 most common http://cwe.mitre.org/top25/ OS command injection is *exactly* the sort of thing we're warning about. Feel free to continue learning bad habits, but please stop trying to encourage others to do the same. -- Steven From knacktus at googlemail.com Sun Dec 12 09:59:45 2010 From: knacktus at googlemail.com (Knacktus) Date: Sun, 12 Dec 2010 09:59:45 +0100 Subject: [Tutor] Ressources for licensing In-Reply-To: References: <4D039025.6030402@googlemail.com> Message-ID: <4D048F01.5060508@googlemail.com> Am 12.12.2010 03:42, schrieb David Hutto: > On Sat, Dec 11, 2010 at 9:52 AM, Knacktus wrote: >> Hi everyone, >> >> can anybody recommend a lib or some other ressources about license >> mechanisms of desktop applications written in python. I'm thinking of a >> license-key that can be used to limit the time the application can be used. >> I also need to exploit the usage of a license server. > > > You probably need: > > def license_this(self): > print "touch mine" > > license_this(self) > I don't get it. Could you please explain a bit? > >> >> Cheers, >> >> Jan >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> From waynejwerner at gmail.com Sun Dec 12 14:16:21 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Sun, 12 Dec 2010 07:16:21 -0600 Subject: [Tutor] Writing to the terminal? In-Reply-To: <4D028FD9.4000609@aim.com> References: <4D028FD9.4000609@aim.com> Message-ID: On Fri, Dec 10, 2010 at 2:38 PM, Corey Richardson wrote: > > > On 12/10/2010 3:34 PM, Wayne Werner wrote: > >> If you just want a single line you can use chr(13) which is a carriage >> return. If you want a more complex program you'll need a curses type >> library >> hth, wayne >> >> On 12/10/10, Modulok wrote: >> >>> List, >>> >>> Forgive me if I don't describe this well, I'm new to it: >>> >>> Assume I'm working in a command shell on a terminal. Something like >>> tcsh on xterm, for example. I have a program which does *something*. >>> Let's say it counts down from 10. How do I print a value, and then >>> erase that value, replacing it with another value? Say I had something >>> like '10' that appears, then wait a second, then the 10 is replaced by >>> '9'... '8'.. and so forth. The point is, I don't want to print to a >>> new line, nor do I want the new number to appear next to the previous >>> number... I just want to change it in place. (If that makes any >>> sense?) Think of console based progress counters in programs like >>> fetch or wget, or lame. >>> >>> How do you do this in Python? >>> -Modulok- >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >>> > Try that in the interactive interpreter, it doesn't work. > >>> print "a" + chr(13) > a > (Python 2.6.6) > Actually, it does: >>> print 'a' + chr(13) + 'b' b The cursor moves back, you just didn't bother to overwrite the 'a'. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From waynejwerner at gmail.com Sun Dec 12 14:36:03 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Sun, 12 Dec 2010 07:36:03 -0600 Subject: [Tutor] using a networked data file In-Reply-To: References: Message-ID: On Sat, Dec 11, 2010 at 12:07 PM, Bill Allen wrote: > David, > > Thanks for the feedback. I should have been more specific on the usage of > the data. The data will be some email addresses, names, department, and an > indicator if the email address is internal to the business or an external > contact. So, one table with these being the fields in each record should > suffice. The users will be presented an interface that allows them to > select one or more recipients to which a standardized email with a PDF > attachment will be sent. All of this is no problem. I was concerned that > with more than one user at a time potentially accessing the SQLite db at a > time would be a problem, but I see from the SQLite site and from some > discussions here on Tutor that this is perfectly fine. The users may add > information to the db also, but since such writes will be infrequent this > too should be OK. At least, that is the impression that I have gotten from > what I have read here and other places. Just wanting to confirm that I > have understood this correctly. Also, any other suggestions are welcome. > > Thanks, > Bill Allen I think the larger concern is how the users are accessing the DB. Multiple clients on multiple computers accessing the same db file on an accessible network location? Or something else? There are plenty of pitfalls to be aware of depending on your use. -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From thor at othala.us Sun Dec 12 15:39:40 2010 From: thor at othala.us (John Russell) Date: Sun, 12 Dec 2010 09:39:40 -0500 Subject: [Tutor] Slicing Tuples In-Reply-To: <4D040B97.5000208@pearwood.info> References: <4D040B97.5000208@pearwood.info> Message-ID: Thanks to all for your answers, especially those that went into detail about why its done in that way. As far as whether this is actually addressed in the book, as far as I can tell by going a few pages forward, it does not. In fact, after the code there's a how it works section which only added to my confusion because all it contained was one sentence: "In each case, using the colon to specify a slice of the sequence instructs Python to create a new sequence that contains *just those elements.*" I added the bold, but obviously its a bit misleading. Thanks again for taking the time to answer and explain such a basic concept. I appreciate it! -jlr On Sat, Dec 11, 2010 at 6:39 PM, Steven D'Aprano wrote: > John Russell wrote: > > So, my question is this, and I realize that this is *very* basic - what is >> going on with the last element? Why is it returning one less than I think >> it >> logically should. Am I missing something here? There is not much of an >> explanation in the book, but I would really like to understand what's >> going >> on here. >> > > > If the book really doesn't explain this (as opposed to you just having > missed it), that's a fairly serious lack! > > Slicing in Python uses what is called "half-open intervals". There are four > obvious ways to count slices: > > (1) Closed interval: > both the start index and the end index are included. > > (2) Open interval: > both the start index and end index are excluded. > > (3) Half-open interval: > the start index is included, and the end index excluded. > > (4) Half-open interval (reversed sense): > the start index is excluded, and the end index included. > > > Python uses #3 because it is generally the most simple and least > error-prone. Essentially, you should consider a slice like sequence[a:b] to > be equivalent to "take a slice of items from seq, start at index=a and stop > when you reach b". Because you stop *at* b, b is excluded. > > Indexes should be considered as falling *between* items, not on them: > > 0.1.2.3.4.5.6.7.8 > |a|b|c|d|e|f|g|h| > > Run an imaginary knife along the line marked "4", and you divide the > sequence abcdefgh into two pieces: abcd and efgh. > > Why are half-open intervals less error-prone? Because you have to adjust by > 1 less often, and you have fewer off-by-one errors. E.g. > > * Take n items starting from index i: > > Half-open: seq[i:i+n] > Closed: seq[i:i+n-1] > > * Length of a slice [i:j]: > > Half-open: j-i > Closed: j-i+1 > > * Dividing a sequence into two slices with no overlap: > > Half-open: > s = seq[:i] > t = seq[i:] > > Closed: > s = seq[:i-1] > t = seq[i:] > > * Splitting a string at a delimiter: > s = "abcd:efgh" > > We want to split the string into two parts, everything before > the colon and everything after the colon. > > Half-open: > p = s.find(':') > before = s[:p] > after = s[p+1:] > > Closed: > p = s.find(':') > before = s[:p-1] > after = s[p+1:] > > * Empty slice: > > Half-open: seq[i:i] is empty > Closed: seq[i:i] has a single item > > > So half-open (start included, end excluded) has many desirable properties. > Unfortunately, it has two weaknesses: > > - most people intuitively expect #1; > - when slicing in reverse, you get more off-by-one errors. > > There's not much you can do about people's intuition, but since reverse > slices are much rarer than forward slices, that's a reasonable cost to pay. > > > > -- > 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 wallenpb at gmail.com Sun Dec 12 15:56:24 2010 From: wallenpb at gmail.com (Bill Allen) Date: Sun, 12 Dec 2010 08:56:24 -0600 Subject: [Tutor] using a networked data file In-Reply-To: References: Message-ID: Wayne, Yes, you have characterized it pretty well. Additionally, it will be accessed typically by maybe a dozen individuals, typically only reading information from the database and infrequently writing to it. --Bill On Sun, Dec 12, 2010 at 7:36 AM, Wayne Werner wrote: > > > I think the larger concern is how the users are accessing the DB. Multiple > clients on multiple computers accessing the same db file on an accessible > network location? Or something else? > > There are plenty of pitfalls to be aware of depending on your use. > > -Wayne > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Sun Dec 12 16:21:23 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sun, 12 Dec 2010 16:21:23 +0100 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: <4D028FD9.4000609@aim.com> Message-ID: On Sun, Dec 12, 2010 at 2:16 PM, Wayne Werner wrote: >> >> Try that in the interactive interpreter, it doesn't work. >> >>> print "a" + chr(13) >> a >> (Python 2.6.6) > > Actually, it does: > ?>>> print 'a' + chr(13) + 'b' > b > The cursor moves back, you just didn't bother to overwrite the 'a'. > HTH, > Wayne > Am I being spam-filtered or something? This e-mail I sent over two days ago, cc'd to tutor and also you specifically. Not that you're wrong, it just seems rather redundant at this point: On Fri, Dec 10, 2010 at 9:38 PM, Corey Richardson wrote: > > Try that in the interactive interpreter, it doesn't work. >>>> print "a" + chr(13) > a You forgot to print something after the carriage return. It works for me: Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print 'a'+chr(13)+'b' b >>> the carriage return resets the cursor to the beginning of the line, so you can write over what you wrote before. Hugo From cajsdy at gmail.com Sun Dec 12 17:32:21 2010 From: cajsdy at gmail.com (cajsdy) Date: Sun, 12 Dec 2010 11:32:21 -0500 Subject: [Tutor] Any recommend of UML tool and UI design tool for python? Message-ID: Either paid or free open source is fine. I'm creating automation frame work. Idealy it includes: test plan management, test manager across windows, unix, linux, solaris and other os. UML documentation for python scripts IDE tool for python on windoes and linux UI design tool for python(best is integrated with IDE) Wonder any recommend? On 12/11/10, David wrote: > On 12 December 2010 03:25, John Russell wrote: >> Last night I started working through a book (Beginning Python: Using >> Python >> 2.6 and Python 3.1) I bought to learn Python, and there is an example in >> it >> that doesn't make sense to me. > > I have that book too, and several others thankfully. I'm just writing > to share my experience, in case another perspective from another > relatively new python user is helpful to anyone. > > I found that book is written towards a friendly beginner level, but it > omits many details and is not a comprehensive reference. It does > include plenty of examples on many topics. I have read the opposite > criticism of other books, so it is probably impossible for one book to > fit all needs. > > Personally I became weary of the food/fridge/kitchen theme of the > early examples as it did not engage my interest at all, and there is > so much of it. However I have found that book valuable for its Part > III where it covers useful applications. Although I have come to > expect that its examples will often need typos corrected or other > small modifications to get them to run properly on Python 2.6. > > So while working through Parts I and II, if your experience is > anything like mine where I moved away from it fairly quickly, you will > definitely need other resources. Fortunately they are abundantly > available. In case you are unaware, a Tutorial and Reference are > integrated with Python. On my Linux box the tutorial is > file:///usr/share/doc/python-docs-x.x.x/html/tutorial/index.html and > the Alphabetic Index to the Reference is > file:///usr/share/doc/python-docs-x.x.x/html/genindex.html > > I find the Tutorial easily readable, whereas the Reference can be > challenging, but it is comprehensive and likely contains the answer to > most questions, somewhere. > > As an exercise I thought I'd try to find the answer to your question > using these built-in docs. The keyword is "slice" which we know from > the book. I couldn't find any coverage in the 2.6 tutorial so I looked > up "slice" in the Alphabetic Index, and the first link took me to a > section which includes the answer: "The slicing now selects all items > with index k such that i <= k < j where i and j are the specified > lower and upper bounds". > > In case you are not aware, the web has a vast amount of great material > for learning Python, see for example: > http://wiki.python.org/moin/BeginnersGuide/NonProgrammers > As Python is an evolving language, one needs be mindful of the > differences between Python 2.x and Python 3.x when using this > material. > > My favourite Python book of my small beginners collection is "Python 3 > Object Oriented Programming" by Dusty Phillips. It claims: > "If you're new to object-oriented programming techniques, or if you > have basic Python skills, and wish to learn in depth how and when to > correctly apply object-oriented programming in Python, this is the > book for you". > I endorse that completely. Having learned the syntax basics elsewhere, > for me this is a book like no other. I find its topic coverage and all > its short examples consistently useful, powerful, and illuminating. I > find it easy to read, well matched to my level and interest, and it > has made a real difference to the code I write. I use classes with > confidence now. For example, it gave me the background and confidence > to design a custom sortable abstract class I needed, my first personal > experience of the power of OOP. > > Tthe python and tutorial mailing lists are a wonderul resource as you > are obviously aware. Thanks to all the contributors from whom I > continue to learn. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Sent from my mobile device From alan.gauld at btinternet.com Sun Dec 12 19:01:31 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Dec 2010 18:01:31 -0000 Subject: [Tutor] role playing game - help needed References: Message-ID: "David Hutto" wrote > > That is a bad piece of advice. You should only use input() when > > you can > > fully trust whoever doing the input (i.e. you). > > Who uses the crap we, as noobies produce? Hopefully you do. And can you really be absolutely sure you won't accidentally type a dangerous command into an input prompt? I once accidentally deleted all the files on my Unix workstation by thinking I was in a subdirectory when I was at the root folder as administrator.... It took me several hours to recover the bulk of my files using the raw shell commands and a nwetwork connection to my colleage's Sun box. The point is that it's nearly as easy to use good practice as it is to use bad practice so you might as well get used to doing it the safe way. Then you are protected, even from yourself. > It's pie in the sky mentality. We design it because WE > want it and WE(individually) use it. But if it works for you it may well work for somebody else, who, when they see it, want a copy. That's how the vast majority of amateur written software starts off, then it becomes shareware or opensource and starts getting copied on. And if it has insecure code in, people get bitten. Now, you can argue its their own fault for using "opensource" code, but they won't see it that way. One of the most widely distributed programs that I've written (privately) was something I did when first learning Windows programming with Delphi - literally the second Windows program I ever wrote. 15 years lqater I still get the occasional email from somebody who has found a copy and wants to use it! It was a learning excercise for my own amusement, I lent it to a friend, who lent it to a friend who asked for some tweaks, etc... There are probably several hundred users now. And remember that Linux started out as a personal learning exercise for Linus Torvalds while a student, he didn't set out to challlenge Microsoft, it was just a bit of a fun thing to do. Software that does something useful has a habit of proliferating, even when written by noobies. Get into the habit of doing things well and that will be a good thing of which you can be proud.. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun Dec 12 19:04:35 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Dec 2010 18:04:35 -0000 Subject: [Tutor] 'or' in assignment (not if statement)? References: <552185.23819.qm@web86708.mail.ird.yahoo.com> <4D022A64.60703@pearwood.info> <4D0439E1.1080305@pearwood.info> Message-ID: "Steven D'Aprano" wrote > ordered a chai latte at a cafe. The waiter had no idea what that > was, but must have known that "chai" means tea, and so mixed tea and > coffee So now I've got to ask, what is a chai latte? I could Google it but I'm feeling lazy :-) Alan G. From alan.gauld at btinternet.com Sun Dec 12 19:16:51 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Dec 2010 18:16:51 -0000 Subject: [Tutor] Any recommend of UML tool and UI design tool for python? References: Message-ID: "cajsdy" wrote > Either paid or free open source is fine. > I'm creating automation frame work. Idealy it includes: > > test plan management, > test manager across windows, unix, linux, solaris and other os. > UML documentation for python scripts > IDE tool for python on windoes and linux > UI design tool for python(best is integrated with IDE) Eclipse would be the logical choice and there are a few free UML editor plug-ins. I've tried one (can't recall the name) and although a bit clunky compared to commercoal versions it worked fine for small class and sequence diagrams. If you don't need full CASE modelling facilities someting like a drawing tool such as Dia, Visio or Smartdraw might suffice. If you want full CASE features (model validation, simulation, code generation, reverse engineering fof diagrams from code, etc) then I think you will need to pay - and probably quite a lot! I've used both Borland Together and IBM RSA. I prefer Borland although IBM produces prettier diagrams - but I found it a lot less intuitive. to use. Both come as Eclipse plugins and work with whatever version control tools Eclipse is using. There are other standalone UML tools too but it depends how much of UML you want to use. If it's only a few basic class diagrams, sequence diagrams and state diagrams then prettty much anything will do. If you need to get into the more structural aspects of UML (deployment diagrams, components, nested states, activity charts, use-cases etc) then you might want to look at paying out some money. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From modulok at gmail.com Sun Dec 12 19:23:41 2010 From: modulok at gmail.com (Modulok) Date: Sun, 12 Dec 2010 11:23:41 -0700 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: <4D028FD9.4000609@aim.com> Message-ID: List, Thanks! I think I got it working now with the help of some suggestions :-) For more complex stuff, (think blue screens with little white boxes you press spacebar to activate. Kind of like an OS installer) I would look into the `curses` module in the standard library? Thanks! -Modulok- From knacktus at googlemail.com Sun Dec 12 19:57:57 2010 From: knacktus at googlemail.com (Knacktus) Date: Sun, 12 Dec 2010 19:57:57 +0100 Subject: [Tutor] Any recommend of UML tool and UI design tool for python? In-Reply-To: References: Message-ID: <4D051B35.9030705@googlemail.com> Am 12.12.2010 19:16, schrieb Alan Gauld: > > "cajsdy" wrote >> Either paid or free open source is fine. >> I'm creating automation frame work. Idealy it includes: >> >> test plan management, >> test manager across windows, unix, linux, solaris and other os. >> UML documentation for python scripts >> IDE tool for python on windoes and linux >> UI design tool for python(best is integrated with IDE) > > Eclipse would be the logical choice and there are a few free > UML editor plug-ins. I've tried one (can't recall the name) and > although a bit clunky compared to commercoal versions it > worked fine for small class and sequence diagrams. > > If you don't need full CASE modelling facilities someting > like a drawing tool such as Dia, Visio or Smartdraw might > suffice. > > If you want full CASE features (model validation, simulation, > code generation, reverse engineering fof diagrams from code, etc) > then I think you will need to pay - and probably quite a lot! I've > used both Borland Together and IBM RSA. I prefer Borland > although IBM produces prettier diagrams - but I found it a > lot less intuitive. to use. Both come as Eclipse plugins and > work with whatever version control tools Eclipse is using. > > There are other standalone UML tools too but it depends how > much of UML you want to use. If it's only a few basic class diagrams, > sequence diagrams and state diagrams then prettty much > anything will do. If you need to get into the more structural > aspects of UML (deployment diagrams, components, nested states, > activity charts, use-cases etc) then you might want to look at > paying out some money. > If you're willing to pay for a CASE tool then check out Enterprise Architect from http://sparxsystems.eu/ For the massive features I think it's reasonable priced (professional edition for 165 Euro + VAT). UML-Source Code integration with Python ... I don't know if it really works. You would need to limit your code to pure OO-style. Such integration might work with Java and C# (Enterprise Architect can do this: Change code -> update UML and vice versa). Nevertheless. Enterprise Architect supports Python for reverse engineering. For designing UIs: You will need another tool. It depends on your GUI toolkit. If you're planning to use PyQt as GUI toolkit (which I can only highly recommend) you're lucky. It has GUI designer, which is quite nice. From smokefloat at gmail.com Sun Dec 12 20:16:46 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 12 Dec 2010 14:16:46 -0500 Subject: [Tutor] role playing game - help needed In-Reply-To: <4D0443E7.6090109@pearwood.info> References: <4D0443E7.6090109@pearwood.info> Message-ID: On Sat, Dec 11, 2010 at 10:39 PM, Steven D'Aprano wrote: > David Hutto wrote: >> >> On Sat, Dec 11, 2010 at 11:54 AM, Lie Ryan wrote: >>> >>> On 12/07/10 23:37, Robert Sj?blom wrote: >>>> >>>> I've been told to use input() if I know that I'll only get integers, >>>> and raw_input() for "everything." >>> >>> That is a bad piece of advice. You should only use input() when you can >>> fully trust whoever doing the input (i.e. you). >> >> Who uses the crap we, as noobies produce? It's pie in the sky >> mentality. We design it because WE want it and WE(individually) use >> it. > > Do you want to learn good habits or learn bad habits? I think we've seen > plenty of evidence on this mailing list that you have little interest in > learning good habits, but actively defend your right learn bad habits. You define a good habit as making the code impossible for someone just learning to use, and you call my habits bad.. I recall you making a habit of being an asshole(pystats should ring a bell, thanks for giving me the credit for inspiration...bitch) > > There are plenty of people who do the same. They're harmless and even > pathetically amusing as newbies, and then they get a job working as a > professional programmer, and end up writing crappy, bug-addled code filled > with the sort of n00b errors that we've been warning about. Bug-addled code > with *real* consequences. Yeah, we call that YOUR mistakes being pointed out later in life due to experience. 20/20 hindsight is great ain't it poindexter? > > Command injection bugs are hugely common in the real world. At least four of > the 25 most common security bugs in *professional* software are in my > opinion varieties of the command injection flaw, and one of those is the > SECOND most common flaw: > > SQL injection attack #2 most common > Unrestricted upload of dangerous files #8 most common > OS command injection #9 most common > PHP file inclusion attack #13 most common Injection is only relevant in non-personal code. > > http://cwe.mitre.org/top25/ > > OS command injection is *exactly* the sort of thing we're warning about. > > Feel free to continue learning bad habits, but please stop trying to > encourage others to do the same. I didn't encourage a bad habit, I encouraged development of a problem defined by the client and a solution developed byu the programmer. The only bad habit around here, is your condescending nature. > > > -- > Steven > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From smokefloat at gmail.com Sun Dec 12 20:27:04 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 12 Dec 2010 14:27:04 -0500 Subject: [Tutor] Ressources for licensing In-Reply-To: <4D048F01.5060508@googlemail.com> References: <4D039025.6030402@googlemail.com> <4D048F01.5060508@googlemail.com> Message-ID: You need a file that sets the initial time used for the app(the time on the individuals computer), and a function that checks that initial start up file for the current time and the original usage time of first start up. From smokefloat at gmail.com Sun Dec 12 20:32:50 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 12 Dec 2010 14:32:50 -0500 Subject: [Tutor] Ressources for licensing In-Reply-To: References: <4D039025.6030402@googlemail.com> <4D048F01.5060508@googlemail.com> Message-ID: On Sun, Dec 12, 2010 at 2:27 PM, David Hutto wrote: > You need a file that sets the initial time used for the app(the time > on the individuals computer), and a function that checks that initial > start up file for the current time and the original usage time of > first start up. > pseudocode: def initialfile(): f = open('/initialfile','w') if f: pass if not f: time = os/sys.time f.write(time) def check original: if f found: check f.time currenttime From smokefloat at gmail.com Sun Dec 12 20:33:43 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 12 Dec 2010 14:33:43 -0500 Subject: [Tutor] Ressources for licensing In-Reply-To: References: <4D039025.6030402@googlemail.com> <4D048F01.5060508@googlemail.com> Message-ID: On Sun, Dec 12, 2010 at 2:32 PM, David Hutto wrote: > On Sun, Dec 12, 2010 at 2:27 PM, David Hutto wrote: >> You need a file that sets the initial time used for the app(the time >> on the individuals computer), and a function that checks that initial >> start up file for the current time and the original usage time of >> first start up. >> > > pseudocode: > > def initialfile(): > ? ?f = open('/initialfile','w') > ? ?if f: > ? ? ? ?pass > ? ?if not f: > ? ? ? ?time = os/sys.time > ? ? ? ?f.write(time) > > def check original: > ? ?if f found: > ? ? ? ?check f.time > ? ? ? ?currenttime if f.time > setlimit of time: sys.exit() > From smokefloat at gmail.com Sun Dec 12 20:42:06 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 12 Dec 2010 14:42:06 -0500 Subject: [Tutor] role playing game - help needed In-Reply-To: References: <4D0443E7.6090109@pearwood.info> Message-ID: On Sun, Dec 12, 2010 at 2:16 PM, David Hutto wrote: > On Sat, Dec 11, 2010 at 10:39 PM, Steven D'Aprano wrote: >> David Hutto wrote: >>> >>> On Sat, Dec 11, 2010 at 11:54 AM, Lie Ryan wrote: >>>> >>>> On 12/07/10 23:37, Robert Sj?blom wrote: >>>>> >>>>> I've been told to use input() if I know that I'll only get integers, >>>>> and raw_input() for "everything." >>>> >>>> That is a bad piece of advice. You should only use input() when you can >>>> fully trust whoever doing the input (i.e. you). >>> >>> Who uses the crap we, as noobies produce? It's pie in the sky >>> mentality. We design it because WE want it and WE(individually) use >>> it. >> >> Do you want to learn good habits or learn bad habits? I think we've seen >> plenty of evidence on this mailing list that you have little interest in >> learning good habits, but actively defend your right learn bad habits. > > You define a good habit as making the code impossible for someone just > learning to use, > and you call my habits bad.. I recall you making a habit of being an > asshole(pystats should ring a bell, thanks for giving me the credit > for inspiration...bitch) > > > >> >> There are plenty of people who do the same. They're harmless and even >> pathetically amusing as newbies, Said the pathetically amusing pro. and then they get a job working as a >> professional programmer, and end up writing crappy, bug-addled code filled As your ignorant ass did when you first started(maybe no email evidence, but just an educated guess) >> with the sort of n00b errors that we've been warning about. Bug-addled code >> with *real* consequences. > > Yeah, we call that YOUR mistakes being pointed out later in life due > to experience. > 20/20 hindsight is great ain't it poindexter? > >> >> Command injection bugs are hugely common in the real world. At least four of >> the 25 most common security bugs in *professional* software are in my >> opinion varieties of the command injection flaw, and one of those is the >> SECOND most common flaw: >> >> SQL injection attack #2 most common >> Unrestricted upload of dangerous files #8 most common >> OS command injection #9 most common >> PHP file inclusion attack #13 most common > > Injection is only relevant in non-personal code. > > >> >> http://cwe.mitre.org/top25/ >> >> OS command injection is *exactly* the sort of thing we're warning about. >> >> Feel free to continue learning bad habits, but please stop trying to >> encourage others to do the same. > > I didn't encourage a bad habit, I encouraged development of a problem > defined by the client and a solution developed byu the programmer. > > The only bad habit around here, is your condescending nature. > > >> >> >> -- >> Steven >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > And if you do look at the emails, yes i was hostile at the beginning(I've learned to ignore bitches like you), because of attitudes like yours. And if you also look, everytime I tried to help point a fellow noob in the right direction, you came in and said how ignorant I was for trying to help(thanks alot, from them and me). From tim at johnsons-web.com Sun Dec 12 20:42:49 2010 From: tim at johnsons-web.com (Tim Johnson) Date: Sun, 12 Dec 2010 10:42:49 -0900 Subject: [Tutor] Code evaluation inside of string fails with __get_item In-Reply-To: <4D043104.3040403@pearwood.info> References: <20101211033217.GC3380@johnsons-web.com> <20101211164232.GD3380@johnsons-web.com> <4D043104.3040403@pearwood.info> Message-ID: <20101212194249.GI3380@johnsons-web.com> * Steven D'Aprano [101211 17:20]: > Tim Johnson wrote: > >> I've never had the occasion to use assert() or any other >> python - shooting tools, any thoughts on that? > > > Assertions are a great tool, but never ever, under pain of great pain, > use assert for testing user input or function arguments. <.....> > > Who makes that choice? You, or the caller? If the caller, then any > errors that occur are not internal state, and you shouldn't use assert. > If you, then it's an internal detail and you can use assert. Steven: Thanks very much for taking so much time and effort to answer my question. I will file this writing for future and ongoing reference. BTW: I was able to solve the problem. Essentially a clone of Eval() was being called and probably clobbering the stack frame. It was a tedious process of tracking thru known and likely code dependencies and insert debugging stubs. I've written my own debugging macros using vimscript and from your input I would guess that assert would have just clouded the process. thanks again. -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com From wprins at gmail.com Sun Dec 12 20:44:14 2010 From: wprins at gmail.com (Walter Prins) Date: Sun, 12 Dec 2010 19:44:14 +0000 Subject: [Tutor] role playing game - help needed In-Reply-To: References: <4D0443E7.6090109@pearwood.info> Message-ID: On 12 December 2010 19:16, David Hutto wrote: > I recall you making a habit of being an > asshole(pystats should ring a bell, thanks for giving me the credit > for inspiration...bitch) > Rudeness objection. Ad-hominem objection. Come on, this is not kindergarten. We all have our foibles, and although I'd agree the tone around here occasionally leaves something to be desired, you just lower yourself to the same level and make matters worse if you resort to this type of name-calling. Let's stick to objectively (as far as possible) critiquing and considering the points raised. Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From smokefloat at gmail.com Sun Dec 12 20:45:29 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 12 Dec 2010 14:45:29 -0500 Subject: [Tutor] role playing game - help needed In-Reply-To: References: <4D0443E7.6090109@pearwood.info> Message-ID: On Sun, Dec 12, 2010 at 2:44 PM, Walter Prins wrote: > > > On 12 December 2010 19:16, David Hutto wrote: >> >> ?I recall you making a habit of being an >> asshole(pystats should ring a bell, thanks for giving me the credit >> for inspiration...bitch) > > Rudeness objection.? Ad-hominem objection. > > Come on, this is not kindergarten.? We all have our foibles, and although > I'd agree the tone around here occasionally leaves something to be desired, > you just lower yourself to the same level and make matters worse if you > resort to this type of name-calling.?? Let's stick to objectively (as far as > possible) critiquing and considering the points raised. > > Walter > He started it! From smokefloat at gmail.com Sun Dec 12 20:45:46 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 12 Dec 2010 14:45:46 -0500 Subject: [Tutor] role playing game - help needed In-Reply-To: References: <4D0443E7.6090109@pearwood.info> Message-ID: On Sun, Dec 12, 2010 at 2:45 PM, David Hutto wrote: > On Sun, Dec 12, 2010 at 2:44 PM, Walter Prins wrote: >> >> >> On 12 December 2010 19:16, David Hutto wrote: >>> >>> ?I recall you making a habit of being an >>> asshole(pystats should ring a bell, thanks for giving me the credit >>> for inspiration...bitch) >> >> Rudeness objection.? Ad-hominem objection. >> >> Come on, this is not kindergarten.? We all have our foibles, and although >> I'd agree the tone around here occasionally leaves something to be desired, >> you just lower yourself to the same level and make matters worse if you >> resort to this type of name-calling.?? Let's stick to objectively (as far as >> possible) critiquing and considering the points raised. >> >> Walter >> > ?He started it Mama! > From smokefloat at gmail.com Sun Dec 12 20:51:28 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 12 Dec 2010 14:51:28 -0500 Subject: [Tutor] Code evaluation inside of string fails with __get_item In-Reply-To: <20101212194249.GI3380@johnsons-web.com> References: <20101211033217.GC3380@johnsons-web.com> <20101211164232.GD3380@johnsons-web.com> <4D043104.3040403@pearwood.info> <20101212194249.GI3380@johnsons-web.com> Message-ID: On Sun, Dec 12, 2010 at 2:42 PM, Tim Johnson wrote: > * Steven D'Aprano [101211 17:20]: >> Tim Johnson wrote: >> >>> ? I've never had the occasion to use assert() or any other >>> ? python - shooting tools, any thoughts on that? >> >> >> Assertions are a great tool, but never ever, under pain of great pain, >> use assert for testing user input or function arguments. > <.....> >> >> Who makes that choice? You, or the caller? If the caller, then any >> errors that occur are not internal state, and you shouldn't use assert. >> If you, then it's an internal detail and you can use assert. > > ?Steven: Thanks very much for taking so much time and effort to > ?answer my question. I will file this writing for future and > ?ongoing reference. > > ?BTW: I was able to solve the problem. Essentially a clone of > ?Eval() was being called and probably clobbering the stack frame. > ?It was a tedious process of tracking thru known and likely code > ?dependencies and insert debugging stubs. I've written my own > ?debugging macros using vimscript and from your input I would guess > ?that assert would have just clouded the process. > > ?thanks again. > -- > Tim > tim at johnsons-web.com or akwebsoft.com > http://www.akwebsoft.com > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Thanks steven, you're the best. From smokefloat at gmail.com Sun Dec 12 21:09:02 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 12 Dec 2010 15:09:02 -0500 Subject: [Tutor] role playing game - help needed In-Reply-To: References: <4D0443E7.6090109@pearwood.info> Message-ID: On Sun, Dec 12, 2010 at 2:45 PM, David Hutto wrote: > On Sun, Dec 12, 2010 at 2:45 PM, David Hutto wrote: >> On Sun, Dec 12, 2010 at 2:44 PM, Walter Prins wrote: >>> >>> >>> On 12 December 2010 19:16, David Hutto wrote: >>>> >>>> ?I recall you making a habit of being an >>>> asshole(pystats should ring a bell, thanks for giving me the credit >>>> for inspiration...bitch) >>> >>> Rudeness objection.? Ad-hominem objection. >>> >>> Come on, this is not kindergarten. But steven likes to be the smartest kindergartner, show his holier than thou posts since he likes to recall others posts. Look ma what I can do. We all have our foibles, and although >>> I'd agree the tone around here occasionally leaves something to be desired, >>> you just lower yourself to the same level and make matters worse if you >>> resort to this type of name-calling.?? Let's stick to objectively (as far as >>> possible) critiquing and considering the points raised. >>> >>> Walter >>> >> ?He started it Mama! >> > From carroll at tjc.com Sun Dec 12 21:44:21 2010 From: carroll at tjc.com (Terry Carroll) Date: Sun, 12 Dec 2010 12:44:21 -0800 (PST) Subject: [Tutor] Writing to the terminal? In-Reply-To: References: Message-ID: On Fri, 10 Dec 2010, Modulok wrote: > Assume I'm working in a command shell on a terminal. Something like > tcsh on xterm, for example. I have a program which does *something*. > Let's say it counts down from 10. How do I print a value, and then > erase that value, replacing it with another value? Say I had something > like '10' that appears, then wait a second, then the 10 is replaced by > '9'... '8'.. and so forth. import time for t in range(10,0, -1): print "%s \x0D" %t, time.sleep(1) print # get to next line print "Done!" The magic is \0x0D, which resets to the front of the line; and the trailing comma, which suppresses starting the next print on a new line. From steve at pearwood.info Sun Dec 12 22:20:25 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 13 Dec 2010 08:20:25 +1100 Subject: [Tutor] 'or' in assignment (not if statement)? In-Reply-To: References: <552185.23819.qm@web86708.mail.ird.yahoo.com> <4D022A64.60703@pearwood.info> <4D0439E1.1080305@pearwood.info> Message-ID: <4D053C99.6020206@pearwood.info> Alan Gauld wrote: > > "Steven D'Aprano" wrote > >> ordered a chai latte at a cafe. The waiter had no idea what that was, >> but must have known that "chai" means tea, and so mixed tea and coffee > > So now I've got to ask, what is a chai latte? > I could Google it but I'm feeling lazy :-) Spiced tea with milk. Well, technically, it just means "tea with milk", but in English chai is used exclusively for spiced tea ("masala chai" in Indian) rather than black or green tea. Oh, except for Nestles, who sell something here in Australia which they call chai but is actually flavoured coffee. I think it's flavoured with rat droppings and pimple-squeezings, no matter what the packet says, because it truly is disgusting. "Latte" is short for the Italian "caff? latte", or literally "coffee with milk". The latte part means "with milk", not coffee. -- Steven From steve at pearwood.info Sun Dec 12 22:33:57 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 13 Dec 2010 08:33:57 +1100 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: Message-ID: <4D053FC5.1070601@pearwood.info> Terry Carroll wrote: > import time > for t in range(10,0, -1): > print "%s \x0D" %t, > time.sleep(1) > print # get to next line > print "Done!" Which operating system and terminal did you use? In my experience, using print is not satisfactory, because the print command buffers the output and doesn't actually print anything until either a newline or you have a certain number of characters. So the above will queue up the following string: "10 \r9 \r8 \r7 \r6 \r5 \r4 \r3 \r2 \r1 \r\n" before anything becomes visible, and of course that just looks like "1". -- Steven From carroll at tjc.com Mon Dec 13 00:25:26 2010 From: carroll at tjc.com (Terry Carroll) Date: Sun, 12 Dec 2010 15:25:26 -0800 (PST) Subject: [Tutor] Writing to the terminal? In-Reply-To: <4D053FC5.1070601@pearwood.info> References: <4D053FC5.1070601@pearwood.info> Message-ID: On Mon, 13 Dec 2010, Steven D'Aprano wrote: > Which operating system and terminal did you use? > > In my experience, using print is not satisfactory... You're right; it worked under Windows, but not under Linux. Given the other details of the question, my suggestion is not an adequate solution. From alan.gauld at btinternet.com Mon Dec 13 01:42:08 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Dec 2010 00:42:08 -0000 Subject: [Tutor] 'or' in assignment (not if statement)? References: <552185.23819.qm@web86708.mail.ird.yahoo.com> <4D022A64.60703@pearwood.info> <4D0439E1.1080305@pearwood.info> <4D053C99.6020206@pearwood.info> Message-ID: "Steven D'Aprano" wrote >Spiced tea with milk. Well, technically, it just means "tea with >milk", but in English chai is used exclusively for spiced tea Never heard of it I confess. I've heard the, presumably related, term char, meaning a cup of black tea (as in tea without milk, not black leaves!). And when I've been in India I've heard tea called chai, but again it wasn't spiced, just plain old tea without milk. But I've never heard of chai being used in the UK, certainly not in Scotland!. > "Latte" is short for the Italian "caff? latte", or literally "coffee > with milk". The latte part means "with milk", not coffee. And I'm familiar with coffee latte, but like your waiter I'd never heard of chai and latte being used together. So I too might have brought you coffee and tea mixed! :-) PS. I tasted the Nestle's chai when I was in Australia and your description accords with my findings! :-( From alan.gauld at btinternet.com Mon Dec 13 01:49:58 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Dec 2010 00:49:58 -0000 Subject: [Tutor] Writing to the terminal? References: <4D028FD9.4000609@aim.com> Message-ID: "Modulok" wrote > For more complex stuff, (think blue screens with little white boxes > you press spacebar to activate. Kind of like an OS installer) I > would > look into the `curses` module in the standard library? curses on Unix but its not in the std library for windows. I think there is a version you can download, and there are also libraries specifically for the PC terminal, one that I've used successfully being Wconio, based on the Borland Turbo-C console I/O package conio.h. Conio is on Sourceforge. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From marupalli.charan at gmail.com Mon Dec 13 05:43:31 2010 From: marupalli.charan at gmail.com (marupalli charan) Date: Mon, 13 Dec 2010 10:13:31 +0530 Subject: [Tutor] Tutor Digest, Vol 82, Issue 54 In-Reply-To: References: Message-ID: dont send me mails again. i want to unsubscript On 12/13/10, tutor-request at python.org wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > 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: role playing game - help needed (David Hutto) > 2. Re: Code evaluation inside of string fails with __get_item > (Tim Johnson) > 3. Re: role playing game - help needed (Walter Prins) > 4. Re: role playing game - help needed (David Hutto) > 5. Re: role playing game - help needed (David Hutto) > 6. Re: Code evaluation inside of string fails with __get_item > (David Hutto) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sun, 12 Dec 2010 14:42:06 -0500 > From: David Hutto > To: "Steven D'Aprano" > Cc: tutor at python.org > Subject: Re: [Tutor] role playing game - help needed > Message-ID: > > Content-Type: text/plain; charset=ISO-8859-1 > > On Sun, Dec 12, 2010 at 2:16 PM, David Hutto wrote: >> On Sat, Dec 11, 2010 at 10:39 PM, Steven D'Aprano >> wrote: >>> David Hutto wrote: >>>> >>>> On Sat, Dec 11, 2010 at 11:54 AM, Lie Ryan wrote: >>>>> >>>>> On 12/07/10 23:37, Robert Sj?blom wrote: >>>>>> >>>>>> I've been told to use input() if I know that I'll only get integers, >>>>>> and raw_input() for "everything." >>>>> >>>>> That is a bad piece of advice. You should only use input() when you can >>>>> fully trust whoever doing the input (i.e. you). >>>> >>>> Who uses the crap we, as noobies produce? It's pie in the sky >>>> mentality. We design it because WE want it and WE(individually) use >>>> it. >>> >>> Do you want to learn good habits or learn bad habits? I think we've seen >>> plenty of evidence on this mailing list that you have little interest in >>> learning good habits, but actively defend your right learn bad habits. >> >> You define a good habit as making the code impossible for someone just >> learning to use, >> and you call my habits bad.. I recall you making a habit of being an >> asshole(pystats should ring a bell, thanks for giving me the credit >> for inspiration...bitch) >> >> >> >>> >>> There are plenty of people who do the same. They're harmless and even >>> pathetically amusing as newbies, > > Said the pathetically amusing pro. > > and then they get a job working as a >>> professional programmer, and end up writing crappy, bug-addled code >>> filled > > As your ignorant ass did when you first started(maybe no email > evidence, but just an educated guess) > >>> with the sort of n00b errors that we've been warning about. Bug-addled >>> code >>> with *real* consequences. >> >> Yeah, we call that YOUR mistakes being pointed out later in life due >> to experience. >> 20/20 hindsight is great ain't it poindexter? >> >>> >>> Command injection bugs are hugely common in the real world. At least four >>> of >>> the 25 most common security bugs in *professional* software are in my >>> opinion varieties of the command injection flaw, and one of those is the >>> SECOND most common flaw: >>> >>> SQL injection attack #2 most common >>> Unrestricted upload of dangerous files #8 most common >>> OS command injection #9 most common >>> PHP file inclusion attack #13 most common >> >> Injection is only relevant in non-personal code. >> >> >>> >>> http://cwe.mitre.org/top25/ >>> >>> OS command injection is *exactly* the sort of thing we're warning about. >>> >>> Feel free to continue learning bad habits, but please stop trying to >>> encourage others to do the same. >> >> I didn't encourage a bad habit, I encouraged development of a problem >> defined by the client and a solution developed byu the programmer. >> >> The only bad habit around here, is your condescending nature. >> >> >>> >>> >>> -- >>> Steven >>> _______________________________________________ >>> Tutor maillist ?- ?Tutor at python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >>> >> > > > And if you do look at the emails, yes i was hostile at the > beginning(I've learned to ignore bitches like you), because of > attitudes like yours. And if you also look, everytime I tried to help > point a fellow noob in the right direction, you came in and said how > ignorant I was for trying to help(thanks alot, from them and me). > > > ------------------------------ > > Message: 2 > Date: Sun, 12 Dec 2010 10:42:49 -0900 > From: Tim Johnson > To: tutor at python.org > Subject: Re: [Tutor] Code evaluation inside of string fails with > __get_item > Message-ID: <20101212194249.GI3380 at johnsons-web.com> > Content-Type: text/plain; charset=us-ascii > > * Steven D'Aprano [101211 17:20]: >> Tim Johnson wrote: >> >>> I've never had the occasion to use assert() or any other >>> python - shooting tools, any thoughts on that? >> >> >> Assertions are a great tool, but never ever, under pain of great pain, >> use assert for testing user input or function arguments. > <.....> >> >> Who makes that choice? You, or the caller? If the caller, then any >> errors that occur are not internal state, and you shouldn't use assert. >> If you, then it's an internal detail and you can use assert. > > Steven: Thanks very much for taking so much time and effort to > answer my question. I will file this writing for future and > ongoing reference. > > BTW: I was able to solve the problem. Essentially a clone of > Eval() was being called and probably clobbering the stack frame. > It was a tedious process of tracking thru known and likely code > dependencies and insert debugging stubs. I've written my own > debugging macros using vimscript and from your input I would guess > that assert would have just clouded the process. > > thanks again. > -- > Tim > tim at johnsons-web.com or akwebsoft.com > http://www.akwebsoft.com > > > ------------------------------ > > Message: 3 > Date: Sun, 12 Dec 2010 19:44:14 +0000 > From: Walter Prins > To: David Hutto > Cc: tutor at python.org > Subject: Re: [Tutor] role playing game - help needed > Message-ID: > > Content-Type: text/plain; charset="iso-8859-1" > > On 12 December 2010 19:16, David Hutto wrote: > >> I recall you making a habit of being an >> asshole(pystats should ring a bell, thanks for giving me the credit >> for inspiration...bitch) >> > > Rudeness objection. Ad-hominem objection. > > Come on, this is not kindergarten. We all have our foibles, and although > I'd agree the tone around here occasionally leaves something to be desired, > you just lower yourself to the same level and make matters worse if you > resort to this type of name-calling. Let's stick to objectively (as far as > possible) critiquing and considering the points raised. > > Walter > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > > > ------------------------------ > > Message: 4 > Date: Sun, 12 Dec 2010 14:45:29 -0500 > From: David Hutto > To: Walter Prins > Cc: tutor at python.org > Subject: Re: [Tutor] role playing game - help needed > Message-ID: > > Content-Type: text/plain; charset=ISO-8859-1 > > On Sun, Dec 12, 2010 at 2:44 PM, Walter Prins wrote: >> >> >> On 12 December 2010 19:16, David Hutto wrote: >>> >>> ?I recall you making a habit of being an >>> asshole(pystats should ring a bell, thanks for giving me the credit >>> for inspiration...bitch) >> >> Rudeness objection.? Ad-hominem objection. >> >> Come on, this is not kindergarten.? We all have our foibles, and although >> I'd agree the tone around here occasionally leaves something to be >> desired, >> you just lower yourself to the same level and make matters worse if you >> resort to this type of name-calling.?? Let's stick to objectively (as far >> as >> possible) critiquing and considering the points raised. >> >> Walter >> > He started it! > > > ------------------------------ > > Message: 5 > Date: Sun, 12 Dec 2010 14:45:46 -0500 > From: David Hutto > To: Walter Prins > Cc: tutor at python.org > Subject: Re: [Tutor] role playing game - help needed > Message-ID: > > Content-Type: text/plain; charset=ISO-8859-1 > > On Sun, Dec 12, 2010 at 2:45 PM, David Hutto wrote: >> On Sun, Dec 12, 2010 at 2:44 PM, Walter Prins wrote: >>> >>> >>> On 12 December 2010 19:16, David Hutto wrote: >>>> >>>> ?I recall you making a habit of being an >>>> asshole(pystats should ring a bell, thanks for giving me the credit >>>> for inspiration...bitch) >>> >>> Rudeness objection.? Ad-hominem objection. >>> >>> Come on, this is not kindergarten.? We all have our foibles, and although >>> I'd agree the tone around here occasionally leaves something to be >>> desired, >>> you just lower yourself to the same level and make matters worse if you >>> resort to this type of name-calling.?? Let's stick to objectively (as far >>> as >>> possible) critiquing and considering the points raised. >>> >>> Walter >>> >> ?He started it Mama! >> > > > ------------------------------ > > Message: 6 > Date: Sun, 12 Dec 2010 14:51:28 -0500 > From: David Hutto > To: tutor at python.org > Subject: Re: [Tutor] Code evaluation inside of string fails with > __get_item > Message-ID: > > Content-Type: text/plain; charset=ISO-8859-1 > > On Sun, Dec 12, 2010 at 2:42 PM, Tim Johnson wrote: >> * Steven D'Aprano [101211 17:20]: >>> Tim Johnson wrote: >>> >>>> ? I've never had the occasion to use assert() or any other >>>> ? python - shooting tools, any thoughts on that? >>> >>> >>> Assertions are a great tool, but never ever, under pain of great pain, >>> use assert for testing user input or function arguments. >> <.....> >>> >>> Who makes that choice? You, or the caller? If the caller, then any >>> errors that occur are not internal state, and you shouldn't use assert. >>> If you, then it's an internal detail and you can use assert. >> >> ?Steven: Thanks very much for taking so much time and effort to >> ?answer my question. I will file this writing for future and >> ?ongoing reference. >> >> ?BTW: I was able to solve the problem. Essentially a clone of >> ?Eval() was being called and probably clobbering the stack frame. >> ?It was a tedious process of tracking thru known and likely code >> ?dependencies and insert debugging stubs. I've written my own >> ?debugging macros using vimscript and from your input I would guess >> ?that assert would have just clouded the process. >> >> ?thanks again. >> -- >> Tim >> tim at johnsons-web.com or akwebsoft.com >> http://www.akwebsoft.com >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > Thanks steven, you're the best. > > > ------------------------------ > > _______________________________________________ > Tutor maillist - Tutor at python.org > http://mail.python.org/mailman/listinfo/tutor > > > End of Tutor Digest, Vol 82, Issue 54 > ************************************* > From kb1pkl at aim.com Mon Dec 13 05:54:10 2010 From: kb1pkl at aim.com (Corey Richardson) Date: Sun, 12 Dec 2010 23:54:10 -0500 Subject: [Tutor] Tutor Digest, Vol 82, Issue 54 In-Reply-To: References: Message-ID: <4D05A6F2.5030704@aim.com> On 12/12/2010 11:43 PM, marupalli charan wrote: > dont send me mails again. i want to unsubscript At the bottom of every single message from the list there are the following lines: To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ~Corey Richardson From smokefloat at gmail.com Mon Dec 13 05:55:30 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 12 Dec 2010 23:55:30 -0500 Subject: [Tutor] Tutor Digest, Vol 82, Issue 54 In-Reply-To: References: Message-ID: On Sun, Dec 12, 2010 at 11:43 PM, marupalli charan wrote: > dont send me mails again. i want to unsubscript _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From hugo.yoshi at gmail.com Mon Dec 13 05:58:28 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Mon, 13 Dec 2010 05:58:28 +0100 Subject: [Tutor] Tutor Digest, Vol 82, Issue 54 In-Reply-To: References: Message-ID: On Mon, Dec 13, 2010 at 5:43 AM, marupalli charan wrote: > dont send me mails again. i want to unsubscript > unfortunately, the unsubscribe option is only available to those smart enough to take the time to read the first few paragraphs of the digest, or the last few of any message at all. > On 12/13/10, tutor-request at python.org wrote: >> Send Tutor mailing list submissions to >> ? ? ? tutor at python.org >> >> To subscribe or unsubscribe via the World Wide Web, visit >> ? ? ? http://mail.python.org/mailman/listinfo/tutor >> or, via email, send a message with subject or body 'help' to >> ? ? ? tutor-request at python.org >> Up until around here, really. From jojo.mwebaze at gmail.com Mon Dec 13 09:32:22 2010 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Mon, 13 Dec 2010 09:32:22 +0100 Subject: [Tutor] making onthefly attributes persistent Message-ID: Hey Tutor Assuming i have a class bank as below . class bank(object): def __init__(self, bal=0): self.bal = bal def deposit(self, amount): self.bal+=amount print self.bal I define a method debit - which i add to the class onthefly def debit(self, amt): self.bal-=amt print self.bal bank.debit = debit myacct = bank() myacct.deposit(1000) # prints 1000 myacct.debit(99) # print 901 #I can also add an attribute owner myaccount.owner = 'jojo' dir(myacct) # prints [ ....'owner', 'bal', 'debit', 'deposit'] My problem is how to make the added attributes, 'owner' and 'debit' persistent automatically Saving the object using pickle for example does save 'owner' and 'debit' -------------- next part -------------- An HTML attachment was scrubbed... URL: From karim.liateni at free.fr Mon Dec 13 17:00:57 2010 From: karim.liateni at free.fr (Karim) Date: Mon, 13 Dec 2010 17:00:57 +0100 Subject: [Tutor] The Template Pattern Message-ID: <4D064339.6050507@free.fr> Hello all, I am seeking for information about the template pattern applied to python. Could you explain some implementation or anything else? it would be helpful. Regards Thanks a lot Karim From backslash42 at gmail.com Mon Dec 13 17:27:49 2010 From: backslash42 at gmail.com (Sachin Kamboj) Date: Mon, 13 Dec 2010 11:27:49 -0500 Subject: [Tutor] Help with using the ctypes module Message-ID: <57D166A0F1654CDBBE0DEEB23683A368@gmail.com> Hi All, I was trying to use the ctypes module for a project. I was creating a dynamically allocated array of "max_entries" pairs and once the array was exhausted, I was creating a new array of size (1.5 * max_entries) and copying the contents from the old array to the new array. Unfortunately, when I try to access the contents of this new_array, I get a "NULL pointer access" exception. The corresponding C code seems to work perfectly. (See code below.) I was wondering if I was missing something about the way the ctypes module works. Any help would be greatly appreciated. (Not sure if this is the appropriate mailing list for my question.) /Thanks! --- #!/usr/bin/env python from ctypes import * import math import random class PAIR(Structure): _fields_ = [("a", c_long), ("b", c_long)] class MY_ARR(Structure): _fields_ = [("no_entries", c_longlong), ("max_entries", c_longlong), ("entries", POINTER(POINTER(PAIR))) ] def extendArray(x): print "Extending Array" print "Before: %d/%d" % (x.no_entries, x.max_entries) old_arr = x.entries # Create a new array new_max_entries = int(math.ceil(1.5 * x.max_entries)) x.entries = (POINTER(PAIR) * new_max_entries)() # Copy the entries from the old array to the new array for i in range(x.no_entries): x.entries[i] = old_arr[i] x.max_entries = new_max_entries print "After: %d/%d" % (x.no_entries, x.max_entries) return x def printPair(x): print x.contents.a, x.contents.b def printArray(x): print "Printing %d/%d Entries" % (x.no_entries, x.max_entries) for i in range(x.no_entries): printPair(x.entries[i]) if __name__ == "__main__": x = MY_ARR(0, 10, (POINTER(PAIR) * 10)()) for i in range(100): if x.no_entries == x.max_entries: print "\n\nPrinting Before Extension" printArray(x) extendArray(x) print "\n\nPrinting After Extension" printArray(x) my_pair = PAIR(i, random.randint(0, 100)) x.entries[x.no_entries] = pointer(my_pair) x.no_entries += 1 printPair(x.entries[i]) printArray(x) --- Now unfortunately, when I try to run this code, I am getting a "NULL pointer access" exception: $ python TestExtension.py 0 40 1 40 2 11 3 36 4 82 5 73 6 93 7 100 8 75 9 80 Printing Before Extension Printing 10/10 Entries 0 40 1 40 2 11 3 36 4 82 5 73 6 93 7 100 8 75 9 80 Extending Array Before: 10/10 After: 10/15 Printing After Extension Printing 10/15 Entries Traceback (most recent call last): File "TestExtension.py", line 55, in printArray(x) File "TestExtension.py", line 42, in printArray printPair(x.entries[i]) File "TestExtension.py", line 37, in printPair print x.contents.a, x.contents.b ValueError: NULL pointer access --- The corresponding C code works perfectly: #include #include #include typedef struct { long a; long b; } pair; typedef struct { long long no_entries; long long max_entries; pair **entries; } my_arr; my_arr *extend_array(my_arr *x) { int i; pair **old_entries = x->entries; long long new_max_entries = ceil(1.5 * x->max_entries); printf("Extending Array\n"); printf("Before: %lld/%lld\n", x->no_entries, x->max_entries); x->entries = malloc(sizeof(pair *) * new_max_entries); for (i = 0; i < 100; ++i) { x->entries[i] = old_entries[i]; } x->max_entries = new_max_entries; free(old_entries); printf("After: %lld/%lld\n", x->no_entries, x->max_entries); return x; } void print_pair(pair *p) { printf("%ld\t%ld\n", p->a, p->b); } void print_array(my_arr *x) { int i; printf("Printing %lld/%lld entries\n", x->no_entries, x->max_entries); for (i = 0; i < x->no_entries; ++i) { print_pair(x->entries[i]); } } int main(int argc, char *argv[]) { int i; my_arr x = { 0, 10, malloc(sizeof(pair *) * 10) }; for (i = 0; i < 100; ++i) { if (x.no_entries == x.max_entries) { extend_array(&x); } pair *my_pair = malloc(sizeof(pair)); my_pair->a = i; my_pair->b = rand() % 100; x.entries[x.no_entries++] = my_pair; print_pair(x.entries[i]); } print_array(&x); return 0; } -------------- next part -------------- An HTML attachment was scrubbed... URL: From scarolan at gmail.com Mon Dec 13 18:08:16 2010 From: scarolan at gmail.com (Sean Carolan) Date: Mon, 13 Dec 2010 11:08:16 -0600 Subject: [Tutor] Problem with "yaml load" Message-ID: Hi folks: I'm trying to define a short function that will import yaml data into a python dictionary. I am able to do this by dumping my data into a temporary file and then importing it with yaml.load. I would like to see if I can eliminate the temporary file and import the data directly. This works fine: import yaml import os def grabfacts(): ''' This function grabs facter data and sucks it into a dictionary called dataMap ''' os.system('facter --yaml > /tmp/sysdata.yaml') f = open('/tmp/sysdata.yaml') dataMap = yaml.load(f) f.close() Is there an easy way to do this without writing to a temporary file? From waynejwerner at gmail.com Mon Dec 13 18:43:31 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 13 Dec 2010 11:43:31 -0600 Subject: [Tutor] Problem with "yaml load" In-Reply-To: References: Message-ID: On Mon, Dec 13, 2010 at 11:08 AM, Sean Carolan wrote: > Hi folks: > > I'm trying to define a short function that will import yaml data into > a python dictionary. I am able to do this by dumping my data into a > temporary file and then importing it with yaml.load. I would like to > see if I can eliminate the temporary file and import the data > directly. > > This works fine: > > import yaml > import os > > def grabfacts(): > ''' This function grabs facter data and sucks it into a dictionary > called dataMap ''' > os.system('facter --yaml > /tmp/sysdata.yaml') > f = open('/tmp/sysdata.yaml') > dataMap = yaml.load(f) > f.close() > > Is there an easy way to do this without writing to a temporary file? Presumably you could use something like subprocess and simply pipe stdout to your program. I presume facter --yaml will produce output to stdout? It's fairly trivial to read stdout using subprocess. http://docs.python.org/library/subprocess.html that should get you started. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From ranceh at gmail.com Mon Dec 13 18:51:46 2010 From: ranceh at gmail.com (Rance Hall) Date: Mon, 13 Dec 2010 11:51:46 -0600 Subject: [Tutor] gui coding Message-ID: When I learned FORTRAN years ago they didn't teach us OOP or what I like to call Class based programming. since then I've sort of always fallen back to be a procedural programmer with lots of functions. Python and the tkinter (Tkinter on Versions < 3) seem like a great way to write cross platform GUI apps for small light duty apps. I'm intrigued and want to learn but all the samples seem to be OOP or class based. I've tried to wrap my head around class based programming before and it didn't take. But it appears I'm going to have to try again as I can not find any tkinter samples that use a procedural approach. I'm finding it very difficult to understand what I need to get from tkinter because the classes are getting in the way and I'm not seeing what I need to see. Is there something about class based programming that GUI apps prefer to work better in? Does anyone have or know of a good tutorial or explanation of class based coding that I could have a run at? Thanks Rance From rschroev_nospam_ml at fastmail.fm Mon Dec 13 19:11:40 2010 From: rschroev_nospam_ml at fastmail.fm (Roel Schroeven) Date: Mon, 13 Dec 2010 19:11:40 +0100 Subject: [Tutor] Writing to the terminal? In-Reply-To: <4D053FC5.1070601@pearwood.info> References: <4D053FC5.1070601@pearwood.info> Message-ID: Op 2010-12-12 22:33, Steven D'Aprano schreef: > Terry Carroll wrote: > >> import time >> for t in range(10,0, -1): >> print "%s \x0D" %t, >> time.sleep(1) >> print # get to next line >> print "Done!" > > Which operating system and terminal did you use? > > In my experience, using print is not satisfactory, because the print > command buffers the output and doesn't actually print anything until > either a newline or you have a certain number of characters. So the > above will queue up the following string: > > "10 \r9 \r8 \r7 \r6 \r5 \r4 \r3 \r2 \r1 \r\n" > > before anything becomes visible, and of course that just looks like "1". Instead of print, use sys.stdout.write(): import sys import time for t in range(10, 0, -1): sys.stdout.write('\r%s ' % t) time.sleep(1) sys.stdout.write('\n') sys.stdout.write('Done!') -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven From waynejwerner at gmail.com Mon Dec 13 19:30:01 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 13 Dec 2010 12:30:01 -0600 Subject: [Tutor] gui coding In-Reply-To: References: Message-ID: On Mon, Dec 13, 2010 at 11:51 AM, Rance Hall wrote: > When I learned FORTRAN years ago they didn't teach us OOP or what I > like to call Class based programming. > That must have been a few years ago, then ;) > since then I've sort of always fallen back to be a procedural > programmer with lots of functions. > There's nothing really wrong with a lot of functions, and that also highlights one of the great advantages of Python - you can still program procedurally or using any other type of paradigm, more or less. > > Python and the tkinter (Tkinter on Versions < 3) seem like a great way > to write cross platform GUI apps for small light duty apps. > Perfect, really. You can quickly throw a nice little GUI together with few problems. And there are other people doing some rather nice things with Tkinter. > I've tried to wrap my head around class based programming before and > it didn't take. But it appears I'm going to have to try again as I > can not find any tkinter samples that use a procedural approach. I'm > finding it very difficult to understand what I need to get from > tkinter because the classes are getting in the way and I'm not seeing > what I need to see. > > Is there something about class based programming that GUI apps prefer > to work better in? > Does anyone have or know of a good tutorial or explanation of class > based coding that I could have a run at? I'm not aware of any specific tutorials, but I'll try to answer your question and give a bit of an explanation. First, if you're used to writing plenty of functions, you're about halfway to OOP. The only real difference between what you do already (using functions) and using classes is on a conceptual level. Classes can be defined as a collection of data and functions that operate on that data. Think back to a program that you've written with several related functions. Perhaps you wrote something with several records that you may have stored in a list or array or some such. So you may write a function called get_new_record() that gets data for a record, either from the user or somewhere else. Then you might have an update_record() function that modifies a given record. And perhaps even a delete_record(). You could think for a few minutes and come up with the rudimentary functions, I'm sure. Now, instead of thinking about them as either lists inside a list or a series of data elements stored in a list, imagine those records really are "things" - objects that can stand alone. And you can tell that record that you want to modify it, or you want it displayed some certain way, or any number of things. The hardest part about OOP is deciding how much responsibility an object really should have. Anyhow, that's all OOP is - just taking the related functions that you would normally write and sticking them inside a class. So instead of: ['somename', 'somedata', 'anothername', 'more data', 'no name', ''] you could have [record1, record2, record3] which you could modify the __repr__/__string__ methods to print out however you want. If you haven't made the link yet, GUIs tend to be objects because it's just easier to think of them that way. If you have a (real life) bottle, you can open the lid, close the lid, put stuff in, take it out, or throw it in the garbage. It's a lot easier to think of a text entry box as something you can put text in, or get text out of, or . It's a different way of thinking about programming that begins feeling entirely natural because we, as humans, are used to talking about things that can do stuff and we can do stuff with. You might have hedge clippers that have certain attributes - number and sharpness of blades, for instance. They also have a function that you can .open() them and .close() them. But if you close them with something inside they will .cut() the object you place inside the clippers. Alan Gauld (frequent contributor of this list) has a tutorial on OOP at http://alan-g.me.uk/ that has some pretty solid examples about class-based programming. Of course, this discussion wouldn't be complete without telling you that it is quite possible (using Tkinter especially) to actually adhere to procedural programming. You could do something like this: import Tkinter as tk def buttonclicked(): print "Yay, you clicked me!" root = tk.Tk() button = tk.Button(root, text='Click me!', command=buttonclicked) button.pack() root.mainloop() If you're more comfortable with procedural programming you can certainly do it that way, but you'll probably find it a lot easier to spend some time getting used to the concept of OOP and writing your GUI programs in a class-based fashion. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From wallenpb at gmail.com Mon Dec 13 19:53:58 2010 From: wallenpb at gmail.com (Bill Allen) Date: Mon, 13 Dec 2010 12:53:58 -0600 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: <4D028FD9.4000609@aim.com> Message-ID: Alan, Oh wow! I was not aware of the WConio module. That is exactly what I have been needing! Thanks, Bill Allen On Sun, Dec 12, 2010 at 6:49 PM, Alan Gauld wrote: > > "Modulok" wrote > > For more complex stuff, (think blue screens with little white boxes >> you press spacebar to activate. Kind of like an OS installer) I would >> look into the `curses` module in the standard library? >> > > curses on Unix but its not in the std library for windows. > > I think there is a version you can download, and there are also > libraries specifically for the PC terminal, one that I've used > successfully being Wconio, based on the Borland Turbo-C > console I/O package conio.h. > > Conio is on Sourceforge. > > HTH, > > > > -- > Alan Gauld > 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 ben.ganzfried at gmail.com Mon Dec 13 19:55:21 2010 From: ben.ganzfried at gmail.com (Ben Ganzfried) Date: Mon, 13 Dec 2010 13:55:21 -0500 Subject: [Tutor] Tab delimited question Message-ID: I'm searching line by line for certain tags and then printing the tag followed by the word immediately following the tag. So for example, suppose I had the following line of text in a file: "this is a key test123 noise noise noise noise noise" In this example, I would want to print "key test123" to a new file. The rest of the words I would not want. Here is my code so far: def test(infile, outfile): for line in infile: tagIndex = line.find("key") start = tagIndex + 4 stop = line[start:].find("\t") -1 if tagIndex != -1: print("start is: ", start) print("stop is: ", stop) print("spliced word is ", line[start: stop]) My question is the following: What is wrong w/ the variable 'stop'? The index it gives me when I print out 'stop' is not even close to the right number. Furthermore, when I try to print out just the word following the tag w/ the form: line[start: stop], it prints nothing (it seems b/c my stop variable is incorrect). I would greatly appreciate any help you have. This is a much simplified example from the script I'm actually writing, but I need to figure out a way to eliminate the noise after the key and the word immediately following it are found. Thank you very much for any help you can provide. Ben From alan.gauld at btinternet.com Mon Dec 13 20:44:37 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Dec 2010 19:44:37 -0000 Subject: [Tutor] making onthefly attributes persistent References: Message-ID: "Jojo Mwebaze" wrote > Assuming i have a class bank as below . > > class bank(object): > def __init__(self, bal=0): > self.bal = bal > def deposit(self, amount): > self.bal+=amount > print self.bal > > I define a method debit - which i add to the class onthefly > > bank.debit = debit > > #I can also add an attribute owner > > myaccount.owner = 'jojo' > My problem is how to make the added attributes, 'owner' and 'debit' > persistent automatically If that's your only problem with this approach congratulations! How does your orther code know when/if these dynamic operations/data exist so as to use them? If they just assume they exist then why not just add them in the definition. Even as nulls? While Python allows you to dynamically add features to classes/objects its not something I would recommend unless you have a really good reason - not least because you bring upon yourself all sorts of problems! If you are determined to do so you can make the objects persistent using the approach I outline on my tutorial but adding a loop to cycle over the contents of dir(). But you may find that recovering the objects - especially if they have a mixed set of attribnutes - presents even more problems... IMHO This is a feature of python that should be considered unorthodox and only to be used when no other approach will work! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From scarolan at gmail.com Mon Dec 13 21:04:36 2010 From: scarolan at gmail.com (Sean Carolan) Date: Mon, 13 Dec 2010 14:04:36 -0600 Subject: [Tutor] Problem with "yaml load" In-Reply-To: References: Message-ID: Yes, I tried using os.subprocess() but there was some sort of parsing error. I'll just stick with the temporary file for now; the documentation seems to indicate that is how yaml.load should be used anyway... On Mon, Dec 13, 2010 at 11:43 AM, Wayne Werner wrote: > On Mon, Dec 13, 2010 at 11:08 AM, Sean Carolan wrote: >> >> Hi folks: >> >> I'm trying to define a short function that will import yaml data into >> a python dictionary. ?I am able to do this by dumping my data into a >> temporary file and then importing it with yaml.load. ?I would like to >> see if I can eliminate the temporary file and import the data >> directly. >> >> This works fine: >> >> import yaml >> import os >> >> def grabfacts(): >> ? ?''' This function grabs facter data and sucks it into a dictionary >> ? ?called dataMap ''' >> ? ?os.system('facter --yaml > /tmp/sysdata.yaml') >> ? ?f = open('/tmp/sysdata.yaml') >> ? ?dataMap = yaml.load(f) >> ? ?f.close() >> >> Is there an easy way to do this without writing to a temporary file? > > Presumably you could use something like subprocess and simply pipe stdout to > your program. I presume facter --yaml will produce output to stdout? It's > fairly trivial to read stdout using subprocess. > http://docs.python.org/library/subprocess.html > that should get you started. > HTH, > Wayne From alan.gauld at btinternet.com Mon Dec 13 21:08:47 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Dec 2010 20:08:47 -0000 Subject: [Tutor] gui coding References: Message-ID: "Rance Hall" wrote > When I learned FORTRAN years ago they didn't teach us OOP or what I > like to call Class based programming. One of the requirements of OOP is to be able to group data together and from memory FORTRAN didn't have any such construct being based primarily on arrays. I beliebve Fortran 95 had some kind of limited OOP facilities but prior to that it was impossible. And my experience on Fortran was in the 80's! > I'm intrigued and want to learn but all the samples seem to be OOP > or > class based. Try not to think of it as class based. Languages like Java tend to emphasise the class but really you need to think about the actual objects as the basis of the program. The class is just a template from which to generate objects. Focussing on the classes as the core item will lead to bad habits. > can not find any tkinter samples that use a procedural approach. The GUI topic in my tutor does not use OOP till near the end. It is extremely basic but it does show the basics needed to front a CLI app with a GUI. > finding it very difficult to understand what I need to get from > tkinter because the classes are getting in the way and I'm not > seeing > what I need to see. Tkinter is, like most GUI toolkits heabvily based on objects itself. But then, so is Python. Strings, files, lists etc are all objects in Python. If you can use those you can use Tkinter. > Is there something about class based programming that GUI apps > prefer > to work better in? GUIs are conceptially made up of objects - buttons, menus, windows, scroll bars etc. There is a very natural mapping from the visual objects (widgets/controls) that you see on-screen and the code objects of the toolkit. The whole ethos of OOP is that each object is like an independent mini-program responding to events/messages from other objects. GUI windows reflect that model - a dialog box acts like a mini app within the parent app and sends data back to it... > Does anyone have or know of a good tutorial or explanation of class > based coding that I could have a run at? Try my tutor. It has topics on both OOP and GUIs. And before doing the GUI one also read the event-driven topic because GUIs are, without exception, event-driven frameworks. Most folks find the shift to thinking in terms of events harder than the shift to objects. But thinking in events makes objects more natural, so they are connected. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Dec 13 21:11:41 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Dec 2010 20:11:41 -0000 Subject: [Tutor] Problem with "yaml load" References: Message-ID: "Sean Carolan" wrote > a python dictionary. I am able to do this by dumping my data into a > temporary file and then importing it with yaml.load. I would like > to > see if I can eliminate the temporary file and import the data > directly. You could use a stringIO buffer. It acts like an in-memory file. Of course your data needs to fit in memory to use that! HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Dec 13 21:17:17 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Dec 2010 20:17:17 -0000 Subject: [Tutor] Tab delimited question References: Message-ID: "Ben Ganzfried" wrote > def test(infile, outfile): > for line in infile: > tagIndex = line.find("key") > start = tagIndex + 4 > stop = line[start:].find("\t") -1 > if tagIndex != -1: > print("start is: ", start) > print("stop is: ", stop) > print("spliced word is ", line[start: stop]) > > My question is the following: What is wrong w/ the variable 'stop'? > The index it gives me when I print out 'stop' is not even close to > the > right number. Some clues would be good - like the programs output? And its hard to tell where the \t characters are in an email, so knowing what find is looking at would help too. > Furthermore, when I try to print out just the word > following the tag w/ the form: line[start: stop], it prints nothing > (it seems b/c my stop variable is incorrect). If stop is wrong that seems likely. Is it possible there is a tab in there that you didn't expect? Have you tried printing all the character codes of all whitespace? Or replacing all tabs with 'TAB' say? HTH, From joel.goldstick at gmail.com Mon Dec 13 21:23:57 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 13 Dec 2010 15:23:57 -0500 Subject: [Tutor] Tab delimited question In-Reply-To: References: Message-ID: On Mon, Dec 13, 2010 at 1:55 PM, Ben Ganzfried wrote: > I'm searching line by line for certain tags and then printing the tag > followed by the word immediately following the tag. > > So for example, suppose I had the following line of text in a file: > > mystring = "this is a key test123 noise noise noise > noise noise" > you can get the words in a list with mylist = mystring.split() You can get the index of 'key' with mylist.index('key') So the next value is the one you want Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> s = "this is a key test123 noise noise noise noise noise" >>> a = s.split() >>> a ['this', 'is', 'a', 'key', 'test123', 'noise', 'noise', 'noise', 'noise', 'noise'] >>> a.index('key') 3 >>> mystuff = a[a.index('key') + 1] >>> mystuff 'test123' >>> > > In this example, I would want to print "key test123" to a new file. > The rest of the words I would not want. > > Here is my code so far: > > def test(infile, outfile): > for line in infile: > tagIndex = line.find("key") > start = tagIndex + 4 > stop = line[start:].find("\t") -1 > if tagIndex != -1: > print("start is: ", start) > print("stop is: ", stop) > print("spliced word is ", line[start: stop]) > > My question is the following: What is wrong w/ the variable 'stop'? > The index it gives me when I print out 'stop' is not even close to the > right number. Furthermore, when I try to print out just the word > following the tag w/ the form: line[start: stop], it prints nothing > (it seems b/c my stop variable is incorrect). > > I would greatly appreciate any help you have. This is a much > simplified example from the script I'm actually writing, but I need to > figure out a way to eliminate the noise after the key and the word > immediately following it are found. > > Thank you very much for any help you can provide. > > Ben > _______________________________________________ > 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 waynejwerner at gmail.com Mon Dec 13 21:46:42 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Mon, 13 Dec 2010 14:46:42 -0600 Subject: [Tutor] gui coding In-Reply-To: References: Message-ID: On Mon, Dec 13, 2010 at 2:08 PM, Alan Gauld wrote: > > >> Does anyone have or know of a good tutorial or explanation of class >> based coding that I could have a run at? >> > > Try my tutor. It has topics on both OOP and GUIs. > And before doing the GUI one also read the event-driven topic because > GUIs are, without exception, event-driven frameworks. Most folks find > the shift to thinking in terms of events harder than the shift to objects. > But thinking in events makes objects more natural, so they are connected. I think the thing that helped me most was in my assembly class where I actually created an event-driven painting program using non-blocking calls to check the keyboard, etc. That may be part of why GUI programming was so easy for me to pick up (and as an extension, threading). -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From martin at linux-ip.net Mon Dec 13 21:43:52 2010 From: martin at linux-ip.net (Martin A. Brown) Date: Mon, 13 Dec 2010 21:43:52 +0100 Subject: [Tutor] Tab delimited question In-Reply-To: References: Message-ID: Greetings Ben, : I'm searching line by line for certain tags and then printing the : tag followed by the word immediately following the tag. What you are describing is an awful lot like 'grep'. But, of course, many different sorts of file searching resemble grep. : So for example, suppose I had the following line of text in a file: : "this is a key test123 noise noise noise noise noise" : : In this example, I would want to print "key test123" to a new : file. The rest of the words I would not want. : : Here is my code so far: : : def test(infile, outfile): : for line in infile: : tagIndex = line.find("key") : start = tagIndex + 4 : stop = line[start:].find("\t") -1 : if tagIndex != -1: : print("start is: ", start) : print("stop is: ", stop) : print("spliced word is ", line[start: stop]) Your problem is that you are calculating the value for 'stop' from a subset of the 'line string (and then subtracting 1), though you want to be adding the value of 'start'. Replace your above line which performs assignment on the stop variable with the following. stop = line[start:].find("\t") + start : My question is the following: What is wrong w/ the variable : 'stop'? The index it gives me when I print out 'stop' is not even : close to the right number. Furthermore, when I try to print out : just the word following the tag w/ the form: line[start: stop], : it prints nothing (it seems b/c my stop variable is incorrect). Now, think about why this is happening.... You are calculating 'stop' based on a the substring of 'line'. You use the 'start' offset to create a substring, in which you then search for a tab. Then, you subtract 1 and try to use that to mean something in the original string 'line. Finally, you are slicing incorrectly (well, that's just the issue of subtracting 1 when you shouldn't be), a not uncommon slicing problem (see this post for more detail [0]). Finally, I have to wonder why are you doing so much of the work yourself, when .... : I would greatly appreciate any help you have. This is a much : simplified example from the script I'm actually writing, but I : need to figure out a way to eliminate the noise after the key and : the word immediately following it are found. I realize that your question was not like the above, but in your example, it seems that you don't know about the 'csv' module. It's convenient, simple, easy to use and quite robust. This should help you. I don't know much about your data format, nor why you are searching, but let's assume that you are searching where you wish to match 'key' as the contents of an entire field. If that's the case, then: import sys import csv def test(infile,outfile,sought): tsv = csv.reader(infile, delimiter='\t') for row in tsv: if sought in row: outfile.write( '\t'.join( row ) + '\n' ) Now, how would you call this function? if __name__ == '__main__': test(sys.stdin, sys.stdout, sys.argv[1]) And, suppose you were at a command line, how would you call that? python tabbed-reader.py < "$MYFILE" 'key' OK, so the above function called 'test' is probably not quite what you had wanted, but you should be able to adapt it pretty readily. Good luck, -Martin [0] http://mail.python.org/pipermail/tutor/2010-December/080592.html -- Martin A. Brown http://linux-ip.net/ From rabidpoobear at gmail.com Mon Dec 13 21:54:05 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Mon, 13 Dec 2010 14:54:05 -0600 Subject: [Tutor] Help with using the ctypes module In-Reply-To: <57D166A0F1654CDBBE0DEEB23683A368@gmail.com> References: <57D166A0F1654CDBBE0DEEB23683A368@gmail.com> Message-ID: <8EA3629E-4D85-4DA6-8324-9746D2E256C8@gmail.com> I confess I don't know a lot about C so I may be off base here... But it looks like your c func extendarray returns a pointer to the new extended array, but you are not capturing this pointer in your python when you call the c func. So the python code is pointing at the old deallocated array. Does that make sense and/or point you in the right direction? ----------------------------- Sent from a mobile device with a bad e-mail client. ----------------------------- On Dec 13, 2010, at 10:27 AM, Sachin Kamboj wrote: > Hi All, > > I was trying to use the ctypes module for a project. I was creating a dynamically allocated array of "max_entries" pairs and once the array was exhausted, I was creating a new array of size (1.5 * max_entries) and copying the contents from the old array to the new array. > > Unfortunately, when I try to access the contents of this new_array, I get a "NULL pointer access" exception. The corresponding C code seems to work perfectly. (See code below.) > > I was wondering if I was missing something about the way the ctypes module works. Any help would be greatly appreciated. (Not sure if this is the appropriate mailing list for my question.) > > /Thanks! > > --- > > #!/usr/bin/env python > > from ctypes import * > import math > import random > > > class PAIR(Structure): > _fields_ = [("a", c_long), > ("b", c_long)] > > > class MY_ARR(Structure): > _fields_ = [("no_entries", c_longlong), > ("max_entries", c_longlong), > ("entries", POINTER(POINTER(PAIR))) > ] > > def extendArray(x): > print "Extending Array" > print "Before: %d/%d" % (x.no_entries, x.max_entries) > old_arr = x.entries > > # Create a new array > new_max_entries = int(math.ceil(1.5 * x.max_entries)) > x.entries = (POINTER(PAIR) * new_max_entries)() > > # Copy the entries from the old array to the new array > for i in range(x.no_entries): > x.entries[i] = old_arr[i] > > x.max_entries = new_max_entries > print "After: %d/%d" % (x.no_entries, x.max_entries) > return x > > def printPair(x): > print x.contents.a, x.contents.b > > def printArray(x): > print "Printing %d/%d Entries" % (x.no_entries, x.max_entries) > for i in range(x.no_entries): > printPair(x.entries[i]) > > > if __name__ == "__main__": > x = MY_ARR(0, 10, (POINTER(PAIR) * 10)()) > for i in range(100): > if x.no_entries == x.max_entries: > print "\n\nPrinting Before Extension" > printArray(x) > > extendArray(x) > > print "\n\nPrinting After Extension" > printArray(x) > > my_pair = PAIR(i, random.randint(0, 100)) > x.entries[x.no_entries] = pointer(my_pair) > x.no_entries += 1 > > printPair(x.entries[i]) > > printArray(x) > > --- > Now unfortunately, when I try to run this code, I am getting a "NULL pointer access" exception: > > $ python TestExtension.py > 0 40 > 1 40 > 2 11 > 3 36 > 4 82 > 5 73 > 6 93 > 7 100 > 8 75 > 9 80 > > > Printing Before Extension > Printing 10/10 Entries > 0 40 > 1 40 > 2 11 > 3 36 > 4 82 > 5 73 > 6 93 > 7 100 > 8 75 > 9 80 > Extending Array > Before: 10/10 > After: 10/15 > > > Printing After Extension > Printing 10/15 Entries > Traceback (most recent call last): > File "TestExtension.py", line 55, in > printArray(x) > File "TestExtension.py", line 42, in printArray > printPair(x.entries[i]) > File "TestExtension.py", line 37, in printPair > print x.contents.a, x.contents.b > ValueError: NULL pointer access > > --- > > The corresponding C code works perfectly: > > #include > #include > #include > > typedef struct { > long a; > long b; > } pair; > > typedef struct { > long long no_entries; > long long max_entries; > pair **entries; > } my_arr; > > my_arr *extend_array(my_arr *x) { > int i; > pair **old_entries = x->entries; > long long new_max_entries = ceil(1.5 * x->max_entries); > > printf("Extending Array\n"); > printf("Before: %lld/%lld\n", x->no_entries, x->max_entries); > > x->entries = malloc(sizeof(pair *) * new_max_entries); > for (i = 0; i < 100; ++i) { > x->entries[i] = old_entries[i]; > } > x->max_entries = new_max_entries; > free(old_entries); > > printf("After: %lld/%lld\n", x->no_entries, x->max_entries); > return x; > } > > void print_pair(pair *p) { > printf("%ld\t%ld\n", p->a, p->b); > } > > void print_array(my_arr *x) { > int i; > printf("Printing %lld/%lld entries\n", x->no_entries, x->max_entries); > for (i = 0; i < x->no_entries; ++i) { > print_pair(x->entries[i]); > } > } > > int main(int argc, char *argv[]) > { > int i; > my_arr x = { > 0, > 10, > malloc(sizeof(pair *) * 10) > }; > > for (i = 0; i < 100; ++i) { > if (x.no_entries == x.max_entries) { > extend_array(&x); > } > pair *my_pair = malloc(sizeof(pair)); > my_pair->a = i; > my_pair->b = rand() % 100; > > x.entries[x.no_entries++] = my_pair; > print_pair(x.entries[i]); > } > print_array(&x); > return 0; > } > > > _______________________________________________ > 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 13 23:47:27 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 14 Dec 2010 09:47:27 +1100 Subject: [Tutor] The Template Pattern In-Reply-To: <4D064339.6050507@free.fr> References: <4D064339.6050507@free.fr> Message-ID: <4D06A27F.3070803@pearwood.info> Karim wrote: > > Hello all, > > I am seeking for information about the template pattern applied to python. > Could you explain some implementation or anything else? it would be > helpful. Design patterns are means to an end, not an end in themselves. You shouldn't say "I want to use the template pattern", you should say "I want to solve this problem, what solution is appropriate?" and then only use the template pattern if it is appropriate. In any case, the template design pattern is one of the simpler design patterns. Here's an example, adapted from Wikipedia's article on the Template Method Pattern: class AbstractGame: """An abstract class that is common to several games in which players play against the others, but only one is playing at a given time. """ def __init__(self, *args, **kwargs): if self.__class__ is AbstractGame: raise TypeError('abstract class cannot be instantiated') def playOneGame(self, playersCount): self.playersCount = playersCount self.initializeGame() j = 0 while not self.endOfGame(): self.makePlay(j) j = (j + 1) % self.playersCount self.printWinner() def initializeGame(self): raise TypeError('abstract method must be overridden') def endOfGame(self): raise TypeError('abstract method must be overridden') def makePlay(self, player_num): raise TypeError('abstract method must be overridden') def printWinner(self): raise TypeError('abstract method must be overridden') Now to create concrete (non-abstract) games, you subclass AbstractGame and override the abstract methods. class Chess(AbstractGame): def initializeGame(self): # Put the pieces on the board. ... def makePlay(player): # Process a turn for the player ... etc. -- Steven From jojo.mwebaze at gmail.com Mon Dec 13 23:50:49 2010 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Mon, 13 Dec 2010 23:50:49 +0100 Subject: [Tutor] making onthefly attributes persistent In-Reply-To: References: Message-ID: On Mon, Dec 13, 2010 at 8:44 PM, Alan Gauld wrote: > > "Jojo Mwebaze" wrote > > Assuming i have a class bank as below . >> >> class bank(object): >> def __init__(self, bal=0): >> self.bal = bal >> def deposit(self, amount): >> self.bal+=amount >> print self.bal >> >> I define a method debit - which i add to the class onthefly >> >> bank.debit = debit >> >> >> #I can also add an attribute owner >> >> myaccount.owner = 'jojo' >> > > My problem is how to make the added attributes, 'owner' and 'debit' >> persistent automatically >> > > If that's your only problem with this approach congratulations! > How does your orther code know when/if these dynamic > operations/data exist so as to use them? If they just assume > they exist then why not just add them in the definition. Even as nulls? > > While Python allows you to dynamically add features to classes/objects > its not something I would recommend unless you have a really good > reason - not least because you bring upon yourself all sorts of problems! > > If you are determined to do so you can make the objects persistent > using the approach I outline on my tutorial but adding a loop to cycle > over the contents of dir(). But you may find that recovering the > objects - especially if they have a mixed set of attribnutes - presents > even more problems... > > IMHO This is a feature of python that should be considered unorthodox > and only to be used when no other approach will work! > > HTH, > > > Thanks Allan for the feedback, the idea is to write a method like store() on the object, that probably looks up all these changes and commits them into the database. Please let me know where to find approach you propose in your tutorial. I read your tutorial when i was just initiated to python, a reference would be helpful to help me find the soln without hustle. Cheers > -- > Alan Gauld > 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 karim.liateni at free.fr Tue Dec 14 00:19:31 2010 From: karim.liateni at free.fr (Karim) Date: Tue, 14 Dec 2010 00:19:31 +0100 Subject: [Tutor] The Template Pattern In-Reply-To: <4D06A27F.3070803@pearwood.info> References: <4D064339.6050507@free.fr> <4D06A27F.3070803@pearwood.info> Message-ID: <4D06AA03.804@free.fr> On 12/13/2010 11:47 PM, Steven D'Aprano wrote: > Karim wrote: >> >> Hello all, >> >> I am seeking for information about the template pattern applied to >> python. >> Could you explain some implementation or anything else? it would be >> helpful. > > Design patterns are means to an end, not an end in themselves. You > shouldn't say "I want to use the template pattern", you should say "I > want to solve this problem, what solution is appropriate?" and then > only use the template pattern if it is appropriate. > Steven, thank you for answering ;o) I don't want to use this pattern absolutely. But I discussed this topic with a C++ programmer. He wanted to translate part of C++ code in python that implements this pattern in C++. I already have implemented a Strategy pattern in python to read different file format into my database and generate different output format file like the code below: class Input(object): """The basic Input class, leaving out the details of the algorithm.""" def __init__( self, strategy ): self.strategy = strategy self.lastRead = None def read(self, filePath): """Load into database memory an input file. @arg: filePath - string - The input file path. @return: The created database object. """ self.lastRead = self.strategy.read() return self.lastRead class InputStrategy( object ): """This InputStrategy class is an abstract interface to various read strategy objects. """ def read(self, filePath): """Abstract method to load into database memory an input file. @arg: filePath - string - The input file path. @return: The created database object. """ raise NotImplementedError By the way, I use NotImplementedError in my abstract method of the abstract class. You are using TypeError exception. Is there a general rule for that? NotImplementedError is ok? I knew from Java experience that template method pattern is only a kind of particular case of Stategy where you delegate parts of algorithm by abstract methods overriding. Indeed the base class implements some common features and let derived classes implement particular parts by polymorphism. Now with your example, I don't understand why he had problems to implement from C++ ? Or perhaps is he mixing it with C++ feature template ?! > In any case, the template design pattern is one of the simpler design > patterns. Here's an example, adapted from Wikipedia's article on the > Template Method Pattern: > > > class AbstractGame: > """An abstract class that is common to several games in which > players play against the others, but only one is playing at a > given time. > """ > def __init__(self, *args, **kwargs): > if self.__class__ is AbstractGame: > raise TypeError('abstract class cannot be instantiated') > > def playOneGame(self, playersCount): > self.playersCount = playersCount > self.initializeGame() > j = 0 > while not self.endOfGame(): > self.makePlay(j) > j = (j + 1) % self.playersCount > self.printWinner() > > def initializeGame(self): > raise TypeError('abstract method must be overridden') > > def endOfGame(self): > raise TypeError('abstract method must be overridden') > > def makePlay(self, player_num): > raise TypeError('abstract method must be overridden') > > def printWinner(self): > raise TypeError('abstract method must be overridden') > > > Now to create concrete (non-abstract) games, you subclass AbstractGame > and override the abstract methods. > > class Chess(AbstractGame): > def initializeGame(self): > # Put the pieces on the board. > ... > > def makePlay(player): > # Process a turn for the player > ... > > etc. One more Thanks for you example Steven! Regards Karim From alan.gauld at btinternet.com Tue Dec 14 01:11:46 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Tue, 14 Dec 2010 00:11:46 +0000 (GMT) Subject: [Tutor] making onthefly attributes persistent In-Reply-To: References: Message-ID: <152924.85372.qm@web86702.mail.ird.yahoo.com> The OOP topic in my tutor has a section on persisting objects by writing them to a text file. The basic principle being that each subclass only persists the attributes that it adds and relies on the superclass to persist itself. In this case you would have to do someting like get the save() method to return the list of attributes persisted. Then the bottom subclass could see what all the superclasses had saved and only save any new attributes not in that list. The trick is unwinding that when you come to restore your objects. You will need to store the data as name/value pairs so that you can read the dynamic attributes, then use setattr() or similar to create them dynamically again. The problem is how do you know that nobody has created a subclass below the bottom level?!" So that leaves you trying to read the entire list of attributes in at once at the top level and restore them which breaks the concept of objects only knowing about their own data... Its all a bit messy, it is so much easier if you don't use dynamic attributes. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ > >From: Jojo Mwebaze >To: Alan Gauld >Cc: tutor at python.org >Sent: Monday, 13 December, 2010 22:50:49 >Subject: Re: [Tutor] making onthefly attributes persistent > > > > >On Mon, Dec 13, 2010 at 8:44 PM, Alan Gauld wrote: > > >>"Jojo Mwebaze" wrote >> >> >>Assuming i have a class bank as below . >>> >>>class bank(object): >>> def __init__(self, bal=0): >>> self.bal = bal >>> def deposit(self, amount): >>> self.bal+=amount >>> print self.bal >>> >>>I define a method debit - which i add to the class onthefly >>> >>> bank.debit = debit >>> >>> >>>#I can also add an attribute owner >>> >>>myaccount.owner = 'jojo' >>> > >My problem is how to make the added attributes, 'owner' and 'debit' >>persistent automatically >> If that's your only problem with this approach congratulations! >How does your orther code know when/if these dynamic >operations/data exist so as to use them? If they just assume >they exist then why not just add them in the definition. Even as nulls? > >While Python allows you to dynamically add features to classes/objects >its not something I would recommend unless you have a really good >reason - not least because you bring upon yourself all sorts of problems! > >If you are determined to do so you can make the objects persistent >using the approach I outline on my tutorial but adding a loop to cycle >over the contents of dir(). But you may find that recovering the >objects - especially if they have a mixed set of attribnutes - presents >even more problems... > >IMHO This is a feature of python that should be considered unorthodox >and only to be used when no other approach will work! > >HTH, > > > Thanks Allan for the feedback, the idea is to write a method like store() on the object, that probably looks up all these changes and commits them into the database. Please let me know where to find approach you propose in your tutorial. I read your tutorial when i was just initiated to python, a reference would be helpful to help me find the soln without hustle. Cheers -- >Alan Gauld >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 wallenpb at gmail.com Tue Dec 14 02:08:05 2010 From: wallenpb at gmail.com (Bill Allen) Date: Mon, 13 Dec 2010 19:08:05 -0600 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: <4D028FD9.4000609@aim.com> Message-ID: Anyone know how to get WConio.putch() to properly put out a box drawing character to the screen in the while at a cmd prompt? The code page is 437, but it when I tell it to put out 188, for example, it get a 1/4 character instead of the box drawing character. I am using WConio.putch(188) I have it WConio through the whole 0 thru 256 range import WConio for i in range(0,257): WConio.putch(i) and get: ?????????????????? !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~??????????????????? ?????????????? ???????"c???-r_???3'???,1????_?AAAA????E?EEIIIID?OOOO?xOUUU?Y_????a????????????d????o??o????y_?A However if at the cmd prompt, out side of Python, I give it an Alt-188 I get the correct ? Where am I going wrong? Thanks, Bill Allen On Mon, Dec 13, 2010 at 12:53 PM, Bill Allen wrote: > Alan, > > Oh wow! I was not aware of the WConio module. That is exactly what I > have been needing! > > > > Thanks, > Bill Allen > > > > > On Sun, Dec 12, 2010 at 6:49 PM, Alan Gauld wrote: > >> >> "Modulok" wrote >> >> For more complex stuff, (think blue screens with little white boxes >>> you press spacebar to activate. Kind of like an OS installer) I would >>> look into the `curses` module in the standard library? >>> >> >> curses on Unix but its not in the std library for windows. >> >> I think there is a version you can download, and there are also >> libraries specifically for the PC terminal, one that I've used >> successfully being Wconio, based on the Borland Turbo-C >> console I/O package conio.h. >> >> Conio is on Sourceforge. >> >> HTH, >> >> >> >> -- >> Alan Gauld >> 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 alan.gauld at btinternet.com Tue Dec 14 02:48:43 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Tue, 14 Dec 2010 01:48:43 +0000 (GMT) Subject: [Tutor] Writing to the terminal? In-Reply-To: References: <4D028FD9.4000609@aim.com> Message-ID: <977202.30803.qm@web86704.mail.ird.yahoo.com> I'm no expert, but you probably need to tell Python which character set to use. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ > >From: Bill Allen >To: Alan Gauld >Cc: *tutor python >Sent: Tuesday, 14 December, 2010 1:08:05 >Subject: Re: [Tutor] Writing to the terminal? > >Anyone know how to get WConio.putch() to properly put out a box drawing >character to the screen in the while at a cmd prompt? The code page is 437, >but it when I tell it to put out 188, for example, it get a 1/4 character >instead of the box drawing character. > > >I am using WConio.putch(188) > >I have it WConio through the whole 0 thru 256 range >import WConio >for i in range(0,257): > WConio.putch(i) > >and get: > >?????????????????? >!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~??????????????????? > >?????????????? ???????"c???-r_???3'???,1????_?AAAA????E?EEIIIID?OOOO?xOUUU?Y_????a????????????d????o??o????y_?A > > >However if at the cmd prompt, out side of Python, I give it an Alt-188 I get the >correct ? > >Where am I going wrong? > >Thanks, >Bill Allen > > > > > >On Mon, Dec 13, 2010 at 12:53 PM, Bill Allen wrote: > >Alan, >> >>Oh wow! I was not aware of the WConio module. That is exactly what I have >>been needing! >> >> >> >>Thanks, >>Bill Allen >> >> >> >> >> >>On Sun, Dec 12, 2010 at 6:49 PM, Alan Gauld wrote: >> >> >>>"Modulok" wrote >>> >>> >>>For more complex stuff, (think blue screens with little white boxes >>>>you press spacebar to activate. Kind of like an OS installer) I would >>>>look into the `curses` module in the standard library? >>>> >> curses on Unix but its not in the std library for windows. >>> >>>I think there is a version you can download, and there are also >>>libraries specifically for the PC terminal, one that I've used >>>successfully being Wconio, based on the Borland Turbo-C >>>console I/O package conio.h. >>> >>>Conio is on Sourceforge. >>> >>>HTH, >>> >>> >>> >>>-- >>>Alan Gauld >>>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 knacktus at googlemail.com Tue Dec 14 09:26:44 2010 From: knacktus at googlemail.com (Knacktus) Date: Tue, 14 Dec 2010 09:26:44 +0100 Subject: [Tutor] making onthefly attributes persistent In-Reply-To: References: Message-ID: <4D072A44.2090601@googlemail.com> Am 13.12.2010 23:50, schrieb Jojo Mwebaze: > > > On Mon, Dec 13, 2010 at 8:44 PM, Alan Gauld > wrote: > > > "Jojo Mwebaze" > wrote > > Assuming i have a class bank as below . > > class bank(object): > def __init__(self, bal=0): > self.bal = bal > def deposit(self, amount): > self.bal+=amount > print self.bal > > I define a method debit - which i add to the class onthefly > > bank.debit = debit > > > #I can also add an attribute owner > > myaccount.owner = 'jojo' > > > My problem is how to make the added attributes, 'owner' and 'debit' > persistent automatically > > > If that's your only problem with this approach congratulations! > How does your orther code know when/if these dynamic > operations/data exist so as to use them? If they just assume > they exist then why not just add them in the definition. Even as nulls? > > While Python allows you to dynamically add features to classes/objects > its not something I would recommend unless you have a really good > reason - not least because you bring upon yourself all sorts of > problems! > > If you are determined to do so you can make the objects persistent > using the approach I outline on my tutorial but adding a loop to cycle > over the contents of dir(). But you may find that recovering the > objects - especially if they have a mixed set of attribnutes - presents > even more problems... > > IMHO This is a feature of python that should be considered unorthodox > and only to be used when no other approach will work! > > HTH, > > > > > Thanks Allan for the feedback, the idea is to write a method like > store() on the object, that probably looks up all these changes and > commits them into the database. Assuming you're planning to use a database with a scheme (e.g. a relational DB) you need to define your tables in advance anyway. Even if you're using a schemeless DB, where do you track which data is in it? How do you build your queries dynamically? This can become very messy. Overall, I think Alan's recommendation to avoid dynamic attribute creation is the one to follow. > > Please let me know where to find approach you propose in your tutorial. > I read your tutorial when i was just initiated to python, a reference > would be helpful to help me find the soln without hustle. > > Cheers > > > > -- > Alan Gauld > 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 > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From c.t.matsumoto at gmail.com Tue Dec 14 09:48:27 2010 From: c.t.matsumoto at gmail.com (C.T. Matsumoto) Date: Tue, 14 Dec 2010 09:48:27 +0100 Subject: [Tutor] Generating a python file Message-ID: <4D072F5B.7040506@gmail.com> Hello, Is it possible to create files containing python code in the same sort of way that you can generate text files. A simple example perhaps could be a persistent dictionary. Keys and values are written to the dictionary in a file, that can be imported later. Thanks, T -- C.T. Matsumoto Claes de Vrieselaan 60a III 3021 JR Rotterdam The Netherlands tel.: +31 (0)6 41 45 08 54 From shantanoo at gmail.com Tue Dec 14 09:54:00 2010 From: shantanoo at gmail.com (=?UTF-8?B?4KS24KSC4KSk4KSo4KWC?=) Date: Tue, 14 Dec 2010 14:24:00 +0530 Subject: [Tutor] Generating a python file In-Reply-To: <4D072F5B.7040506@gmail.com> References: <4D072F5B.7040506@gmail.com> Message-ID: On Tue, Dec 14, 2010 at 14:18, C.T. Matsumoto wrote: > Hello, > > Is it possible to create files containing python code in the same sort of > way that you can generate text files. > > A simple example perhaps could be a persistent dictionary. Keys and values > are written to the dictionary in a file, that can be imported later. > http://docs.python.org/library/pickle.html is your friend. HTH. -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Tue Dec 14 10:09:39 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 14 Dec 2010 01:09:39 -0800 (PST) Subject: [Tutor] Generating a python file In-Reply-To: References: <4D072F5B.7040506@gmail.com> Message-ID: <339499.47194.qm@web110715.mail.gq1.yahoo.com> Hello, If it's specifically about a dictionary, try also the following: import shelve help(shelve) A shelve is a persistent dictionary. ?Cheers!! 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: ????? To: C.T. Matsumoto Cc: python-tutor Sent: Tue, December 14, 2010 9:54:00 AM Subject: Re: [Tutor] Generating a python file On Tue, Dec 14, 2010 at 14:18, C.T. Matsumoto wrote: Hello, > >Is it possible to create files containing python code in the same sort of way >that you can generate text files. > >A simple example perhaps could be a persistent dictionary. Keys and values are >written to the dictionary in a file, that can be imported later. > http://docs.python.org/library/pickle.html is your friend. HTH. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Dec 14 11:07:58 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 14 Dec 2010 10:07:58 -0000 Subject: [Tutor] Generating a python file References: <4D072F5B.7040506@gmail.com> Message-ID: "C.T. Matsumoto" wrote > Is it possible to create files containing python code in the same > sort of way that you can generate text files. Yes, a python file is just a text file. There is nothing special about it other than the fact that the contents happen to be Python code. > A simple example perhaps could be a persistent dictionary. Keys and > values are written to the dictionary in a file, that can be imported > later. You can do that and in dfact a well known email application does something very similar. However for most situations a proper data storage solution is better (certainly more secure). And for simple cases pickle and shelve(for dictionaries) should suffice. For more complex cases a config file, XML or a relational database would be better. Alan G. From steve at pearwood.info Tue Dec 14 12:23:25 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 14 Dec 2010 22:23:25 +1100 Subject: [Tutor] Writing to the terminal? In-Reply-To: References: <4D028FD9.4000609@aim.com> Message-ID: <4D0753AD.4070109@pearwood.info> Bill Allen wrote: > Anyone know how to get WConio.putch() to properly put out a box drawing > character to the screen in the while at a cmd prompt? The code page is > 437, but it when I tell it to put out 188, for example, it get a 1/4 > character instead of the box drawing character. My guess is that there is a mis-match between the code page you think you have, the code page you actually have, and the encoding that your terminal is set to. I suspect though that you might need to take this to a specialist WConio mailing list, if there is one, or at least the main Python list: python at python.org or comp.lang.python. Good luck, and if you do find the solution, don't forget to post it here for future reference. -- Steven From __peter__ at web.de Tue Dec 14 13:06:04 2010 From: __peter__ at web.de (Peter Otten) Date: Tue, 14 Dec 2010 13:06:04 +0100 Subject: [Tutor] Generating a python file References: <4D072F5B.7040506@gmail.com> Message-ID: C.T. Matsumoto wrote: > Is it possible to create files containing python code in the same sort > of way that you can generate text files. > > A simple example perhaps could be a persistent dictionary. Keys and > values are written to the dictionary in a file, that can be imported > later. For simple human-readable dictionaries the established format is json, see http://docs.python.org/library/json.htm json files look similar to python dictionaries, but there are libraries to read or write them for other programming languages, too. Peter From onyxtic at gmail.com Tue Dec 14 13:13:48 2010 From: onyxtic at gmail.com (Evans Anyokwu) Date: Tue, 14 Dec 2010 12:13:48 +0000 Subject: [Tutor] Generating a python file In-Reply-To: References: <4D072F5B.7040506@gmail.com> Message-ID: On Tue, Dec 14, 2010 at 12:06 PM, Peter Otten <__peter__ at web.de> wrote: > C.T. Matsumoto wrote: > > > Is it possible to create files containing python code in the same sort > > of way that you can generate text files. > > > > A simple example perhaps could be a persistent dictionary. Keys and > > values are written to the dictionary in a file, that can be imported > > later. > > For simple human-readable dictionaries the established format is json, see > > http://docs.python.org/library/json.htm > > json files look similar to python dictionaries, but there are libraries to > read or write them for other programming languages, too. > > Peter > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > Peter, The page you linked to above was not found. Could you check the link again - -- Evans -------------- next part -------------- An HTML attachment was scrubbed... URL: From wallenpb at gmail.com Tue Dec 14 13:16:07 2010 From: wallenpb at gmail.com (Bill Allen) Date: Tue, 14 Dec 2010 06:16:07 -0600 Subject: [Tutor] Writing to the terminal? In-Reply-To: <4D0753AD.4070109@pearwood.info> References: <4D028FD9.4000609@aim.com> <4D0753AD.4070109@pearwood.info> Message-ID: Still looking on this one, but I will be sure to post back to the list if I find anything. Thanks, Bill Allen On Tue, Dec 14, 2010 at 5:23 AM, Steven D'Aprano wrote: > Bill Allen wrote: > >> Anyone know how to get WConio.putch() to properly put out a box drawing >> character to the screen in the while at a cmd prompt? The code page is >> 437, but it when I tell it to put out 188, for example, it get a 1/4 >> character instead of the box drawing character. >> > > My guess is that there is a mis-match between the code page you think you > have, the code page you actually have, and the encoding that your terminal > is set to. > > I suspect though that you might need to take this to a specialist WConio > mailing list, if there is one, or at least the main Python list: > python at python.org or comp.lang.python. > > > Good luck, and if you do find the solution, don't forget to post it here > for future reference. > > > -- > 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 __peter__ at web.de Tue Dec 14 13:38:48 2010 From: __peter__ at web.de (Peter Otten) Date: Tue, 14 Dec 2010 13:38:48 +0100 Subject: [Tutor] Generating a python file References: <4D072F5B.7040506@gmail.com> Message-ID: Evans Anyokwu wrote: > The page you linked to above was not found. > Could you check the link again - Sorry, I accidentally stripped off the trailing 'l' during cut-and-paste. The correct link is http://docs.python.org/library/json.html Peter From fal at libero.it Tue Dec 14 13:39:46 2010 From: fal at libero.it (Francesco Loffredo) Date: Tue, 14 Dec 2010 13:39:46 +0100 Subject: [Tutor] permutations? In-Reply-To: <4CF83ABB.20209@pearwood.info> References: <4CF6D643.6000501@gmail.com> <4CF83ABB.20209@pearwood.info> Message-ID: <4D076592.8020007@libero.it> On 03/12/2010 1.32, Steven D'Aprano wrote: > Back in Ancient Days when dinosaurs walked the earth, and I programmed in Pascal, computers didn't have much memory, and were slow. > Consequently it wasn't practical to make a copy of a list if you wanted to delete a few items. The only practical way to modify > lists was to modify them in place, and if you needed to delete items, you had to work backwards. It took me a while to break myself > of the habit of doing this: > > for i in range(len(mylist)-1, -1, -1): > if mylist[i] == "something": > del mylist[i] > > (Note that you only need to work backwards if you're *deleting* entries, not if you replace them with something else.) > > This is still a useful technique to have in your toolbox, but generally speaking the above is better written as: > > mylist = [x for x in mylist if x != "something"] Up to this point, I share experiences and solution. But the next point did thrill me: > > If you really need to modify the list in place, and not just re-bind the name "mylist" to the new list, then one tiny change will do > it: > > mylist[:] = [x for x in mylist if x != "something"] > I thought mylist[:] was a copy of mylist, and the two lines above would generate exactly the same code! Is this a special optimization by the Python interpreter, or is it just a mistake in my understanding of the slice operator? ----- Nessun virus nel messaggio. Controllato da AVG - www.avg.com Versione: 10.0.1170 / Database dei virus: 426/3313 - Data di rilascio: 13/12/2010 From davea at ieee.org Tue Dec 14 14:56:05 2010 From: davea at ieee.org (Dave Angel) Date: Tue, 14 Dec 2010 08:56:05 -0500 Subject: [Tutor] permutations? In-Reply-To: <4D076592.8020007@libero.it> References: <4CF6D643.6000501@gmail.com> <4CF83ABB.20209@pearwood.info> <4D076592.8020007@libero.it> Message-ID: <4D077775.7010202@ieee.org> On 01/-10/-28163 02:59 PM, Francesco Loffredo wrote: > On 03/12/2010 1.32, Steven D'Aprano wrote: >> >> mylist = [x for x in mylist if x != "something"] > Up to this point, I share experiences and solution. But the next point > did thrill me: >> >> If you really need to modify the list in place, and not just re-bind >> the name "mylist" to the new list, then one tiny change will do >> it: >> >> mylist[:] = [x for x in mylist if x != "something"] >> > I thought mylist[:] was a copy of mylist, and the two lines above would > generate exactly the same code! > > Is this a special optimization by the Python interpreter, or is it just > a mistake in my understanding of the slice operator? Not an optimization, but a fundamental part of the language. On the left side of an assignment, a slice operator specifies which part of an existing object gets replaced by the right side. So whenever you have a list that's referred to by more than one symbol, you may want to use a syntax like this. The slice operator does a very different thing inside an expression (eg. on the right side of an assignment). DaveA From fal at libero.it Wed Dec 15 10:43:16 2010 From: fal at libero.it (Francesco Loffredo) Date: Wed, 15 Dec 2010 10:43:16 +0100 Subject: [Tutor] permutations? In-Reply-To: <4D077775.7010202@ieee.org> References: <4CF6D643.6000501@gmail.com> <4CF83ABB.20209@pearwood.info> <4D076592.8020007@libero.it> <4D077775.7010202@ieee.org> Message-ID: <4D088DB4.7090008@libero.it> On 14/12/2010, Brett Ritter and Dave Angel wrote: > Francesco Loffredo wrote: >>> ... >>> mylist[:] = [x for x in mylist if x != "something"] >>> vs. >>> mylist = [x for x in mylist if x != "something"] >>> ... Brett: > mylist[:] is a slice of mylist (you know that). If you ASSIGN it to > something, you'll get a copy of that slice, but if you modify it > you're modifying the list in place. > ... Dave: > ... > On the left side of an assignment, a slice operator specifies which part of an existing object gets replaced by the right side. So > whenever you have a list that's referred to by more than one symbol, you may want to use a syntax like this. > > The slice operator does a very different thing inside an expression (eg. on the right side of an assignment). *THANK YOU BOTH!* This is something I overlooked, and I'm very happy you made me understand it. ----- Nessun virus nel messaggio. Controllato da AVG - www.avg.com Versione: 10.0.1170 / Database dei virus: 426/3315 - Data di rilascio: 14/12/2010 From fomcl at yahoo.com Wed Dec 15 11:37:06 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 15 Dec 2010 02:37:06 -0800 (PST) Subject: [Tutor] pyodbc/date values in MS Access Message-ID: <515399.28236.qm@web110708.mail.gq1.yahoo.com> Hi, I'm using pyodbc (Python 2.5) to insert records in an MS Access database. For security reasons, question marks should be used for string replacement [*]. The standard %s would make the code vulnerable to sql code injection. Problem is, string replacement in the Good Way somehow doesn't work when the values are dates. Below, snippet #1 does not work (Access says the inserted value is not consistent with the defined datatype), but #2 does. I tried various other ways (ie. DateValue, CDate, etc.) but none of them works. Is there a solution for this? [*] see http://code.google.com/p/pyodbc/wiki/GettingStarted --> under 'Parameters' ### 1 sql = "INSERT INTO tblSomeTable (myDate) VALUES (?);" cursor.execute(sql, "#01/01/2010#") ### 2 sql = "INSERT INTO tblSomeTable (myDate) VALUES (%s);" cursor.execute(sql % "#01/01/2010#") Cheers!! 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? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -------------- next part -------------- An HTML attachment was scrubbed... URL: From cajsdy at gmail.com Wed Dec 15 12:43:03 2010 From: cajsdy at gmail.com (cajsdy) Date: Wed, 15 Dec 2010 06:43:03 -0500 Subject: [Tutor] Any recommend of UML tool and UI design tool for python? In-Reply-To: <4D051B35.9030705@googlemail.com> References: <4D051B35.9030705@googlemail.com> Message-ID: Thank you very much and thank you Alan. Definitely I will take a look. I would prefer the paid version and I will need the support. On 12/12/10, Knacktus wrote: > Am 12.12.2010 19:16, schrieb Alan Gauld: >> >> "cajsdy" wrote >>> Either paid or free open source is fine. >>> I'm creating automation frame work. Idealy it includes: >>> >>> test plan management, >>> test manager across windows, unix, linux, solaris and other os. >>> UML documentation for python scripts >>> IDE tool for python on windoes and linux >>> UI design tool for python(best is integrated with IDE) >> >> Eclipse would be the logical choice and there are a few free >> UML editor plug-ins. I've tried one (can't recall the name) and >> although a bit clunky compared to commercoal versions it >> worked fine for small class and sequence diagrams. >> >> If you don't need full CASE modelling facilities someting >> like a drawing tool such as Dia, Visio or Smartdraw might >> suffice. >> >> If you want full CASE features (model validation, simulation, >> code generation, reverse engineering fof diagrams from code, etc) >> then I think you will need to pay - and probably quite a lot! I've >> used both Borland Together and IBM RSA. I prefer Borland >> although IBM produces prettier diagrams - but I found it a >> lot less intuitive. to use. Both come as Eclipse plugins and >> work with whatever version control tools Eclipse is using. >> >> There are other standalone UML tools too but it depends how >> much of UML you want to use. If it's only a few basic class diagrams, >> sequence diagrams and state diagrams then prettty much >> anything will do. If you need to get into the more structural >> aspects of UML (deployment diagrams, components, nested states, >> activity charts, use-cases etc) then you might want to look at >> paying out some money. >> > > If you're willing to pay for a CASE tool then check out Enterprise > Architect from http://sparxsystems.eu/ > For the massive features I think it's reasonable priced (professional > edition for 165 Euro + VAT). > UML-Source Code integration with Python ... I don't know if it really > works. You would need to limit your code to pure OO-style. Such > integration might work with Java and C# (Enterprise Architect can do > this: Change code -> update UML and vice versa). Nevertheless. > Enterprise Architect supports Python for reverse engineering. > > For designing UIs: You will need another tool. It depends on your GUI > toolkit. If you're planning to use PyQt as GUI toolkit (which I can > only highly recommend) you're lucky. It has GUI designer, which is quite > nice. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Sent from my mobile device From alan.gauld at btinternet.com Wed Dec 15 12:50:43 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 15 Dec 2010 11:50:43 -0000 Subject: [Tutor] pyodbc/date values in MS Access References: <515399.28236.qm@web110708.mail.gq1.yahoo.com> Message-ID: "Albert-Jan Roskam" wrote > dates. Below, snippet #1 does not work (Access says the inserted > value is not > consistent with the defined datatype), but #2 does. #2 is just normal string formatting so should always work. But the cursor method is presumabnly doing some kind of data type checking. What kind of data type does Access normally expect for a date? Is it just a formatted string? Or is there perhaps a numeric or datetime format that the execute() might be expecting? > ### 1 > sql = "INSERT INTO tblSomeTable (myDate) VALUES (?);" > cursor.execute(sql, "#01/01/2010#") > > ### 2 > sql = "INSERT INTO tblSomeTable (myDate) VALUES (%s);" > cursor.execute(sql % "#01/01/2010#") HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From cajsdy at gmail.com Wed Dec 15 12:54:24 2010 From: cajsdy at gmail.com (cajsdy) Date: Wed, 15 Dec 2010 06:54:24 -0500 Subject: [Tutor] Test Automation framework recommendation.... Message-ID: Thanks all here. I'm asked UML and UI tools for python in the other thread. I'm looking for any suggestions to build test automation system, from the test plan management, test execution, python scripting management. Testplan: Testlink is a good example and I used it for the small project, but it lacks of prompt support. Editing function is not that good for word, ppt or xls. Silk havn't tried yet. Test manager: managing the testing execution across the platform, check out test plan and TCs from Testplan manager, control the test process for distributed UUT, collect stats, upload results. Script managenet: suvversion control, Thanks -- Sent from my mobile device From __peter__ at web.de Wed Dec 15 15:06:19 2010 From: __peter__ at web.de (Peter Otten) Date: Wed, 15 Dec 2010 15:06:19 +0100 Subject: [Tutor] pyodbc/date values in MS Access References: <515399.28236.qm@web110708.mail.gq1.yahoo.com> Message-ID: Albert-Jan Roskam wrote: > Hi, > > I'm using pyodbc (Python 2.5) to insert records in an MS Access database. > For security reasons, question marks should be used for string replacement > [*]. The standard %s would make the code vulnerable to sql code injection. > Problem is, string replacement in the Good Way somehow doesn't work when > the values are dates. Below, snippet #1 does not work (Access says the > inserted value is not consistent with the defined datatype), but #2 does. > I tried various other ways (ie. DateValue, CDate, etc.) but none of them > works. Is there a solution for this? > > [*] see http://code.google.com/p/pyodbc/wiki/GettingStarted --> under > 'Parameters' > > ### 1 > sql = "INSERT INTO tblSomeTable (myDate) VALUES (?);" > cursor.execute(sql, "#01/01/2010#") (1) Try providing the date in ISO format "yyyy-mm-dd" "2010-01-01" or (even better if supported) as a date value from datetime import date date(2010, 1, 1) (2) Wrap the value into a tuple which I think is required by the Python DBAPI. cursor.execute(sql, ("2010-01-01",)) cursor.execute(sql, (date(2010, 1, 1),)) Peter From KAkram at efinancialCareers.com Wed Dec 15 13:27:23 2010 From: KAkram at efinancialCareers.com (Khalid Akram) Date: Wed, 15 Dec 2010 06:27:23 -0600 Subject: [Tutor] Can't even get hello to work???!! Message-ID: <2815303B39A4D74C805DCD2049E615B50509E0CC8A@sdmcexch1.dice.ad> Hello, I'm using Python 3.1.3 on Windows XP (using Python Shell). This is what I am trying: print 'hello' and I get: SyntaxError: invalid syntax What could possibly be wrong? I've tried it on the command line and even in hello.py - always get "SyntaxError". Very confused. Thanks Khalid -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Dec 15 18:56:35 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 15 Dec 2010 17:56:35 -0000 Subject: [Tutor] Can't even get hello to work???!! References: <2815303B39A4D74C805DCD2049E615B50509E0CC8A@sdmcexch1.dice.ad> Message-ID: "Khalid Akram" wrote > I'm using Python 3.1.3 on Windows XP (using Python Shell). > This is what I am trying: > > print 'hello' >... > SyntaxError: invalid syntax > What could possibly be wrong? You are using a Python v3 interpreter but a Python v2 tutorial. Python v3 has several new features which are not backward compatible with v2. Try print( 'hello' ) You can either downgrade Python to v2 and follow your tutorial or find a v3 tutorial. (If you can already program just use the official one on the Python web site or if you are a new programmer you might like to try mine :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From emile at fenx.com Wed Dec 15 22:26:57 2010 From: emile at fenx.com (Emile van Sebille) Date: Wed, 15 Dec 2010 13:26:57 -0800 Subject: [Tutor] Can't even get hello to work???!! In-Reply-To: References: <2815303B39A4D74C805DCD2049E615B50509E0CC8A@sdmcexch1.dice.ad> Message-ID: On 12/15/2010 9:56 AM Alan Gauld said... > > "Khalid Akram" wrote > >> I'm using Python 3.1.3 on Windows XP (using Python Shell). >> This is what I am trying: >> >> print 'hello' >> ... >> SyntaxError: invalid syntax > >> What could possibly be wrong? > > You are using a Python v3 interpreter but a Python v2 tutorial. > Python v3 has several new features which are not backward compatible > with v2. Actually, "the print statement is deprecated" ought not be out of reach. Invalidating every existing python book's first chapter without bias is unfortunate. Emile > > Try > > print( 'hello' ) > > You can either downgrade Python to v2 and follow your tutorial > or find a v3 tutorial. (If you can already program just use the official > one on the Python web site or if you are a new programmer you > might like to try mine :-) > From goodpotatoes at yahoo.com Wed Dec 15 23:58:02 2010 From: goodpotatoes at yahoo.com (Shea Grove) Date: Wed, 15 Dec 2010 14:58:02 -0800 Subject: [Tutor] Fwd: Parameterized Queries failing on MySQL References: <6FB650C8B0580345A635730F45C37CEE4C9E99034B@MSGCMSV21011.ent.wfb.bank.corp> Message-ID: > > I?m using pyodbc to interact with MS SQL Server and I?m starting to support mysql. My issue is that when I use a parameterized query, it works for SQL Server, but crashes when I point to MySQL. Is there a different syntax that I should be using? or module? > > EG > Import pyodbc > > def openCursor(DSN): > cnxn=pyodbc.connect(('DSN=%s')%DSN) > cursor=cnxn.cursor() > return cnxn,cursor > > lParams=[] > query='insert into RestaurantTable values (?, ?, ?, ?, ?, ?, ?, ?, ?)' > > #SQL Server Works > scnxn,scursor = openCursor(SQLServer) > scursor.execute(query,lParams) > scnxn.commit() > scnxn.close() > > #This Query bombs out and crashes my IDLE shell. No transactions are commited. > mcnxn,mcursor = openCursor(MySQL) > mcursor.execute(query,params) > mcnxn.commit() > mcnxn.close() > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From paulmg2010 at gmail.com Wed Dec 15 23:26:38 2010 From: paulmg2010 at gmail.com (Paul Griffiths) Date: Wed, 15 Dec 2010 22:26:38 +0000 Subject: [Tutor] vim as a python editor Message-ID: Hi - I'm a beginner at programming and Python. I have been looking for an editor to replace Idle and tried out a few. I liked Geany but decided on vim because I am also learning Linux and the vim skills might be useful. I run Python 2.6.5 on Ubuntu 10.04. How have those of you who use vim configured it? I have looked on the web but got a bit confused by the advice and options. I am currently following some of the advice in the on-line tutorial at http://openbookproject.net/thinkcs/python/english2e/app_c.html and - installed the full version of vim (sudo apt-get install vim-gnome) - created a .vimrc file in my home directory and entered syntax enable filetype indent on set et set sw=4 set smarttab map :w\|!python % - added the following to .bashrc: EDITOR=vim export EDITOR I open up a terminal in the folder where I keep my python files and create two additional tabs in this terminal. I use the first tab to run python, the second one to run vim and the third one for running linux commands such as ls to list the folder contents. Is this is a reasonable approach? Thank you for any feed back. PaulG -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Dec 16 01:30:45 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Dec 2010 00:30:45 -0000 Subject: [Tutor] vim as a python editor References: Message-ID: "Paul Griffiths" wrote > How have those of you who use vim configured it? I have looked > on the web but got a bit confused by the advice and options. I use both IDLE (for simple short scipts) and vim (for bigger multi-file projects). I use vim pretty much out of the box, I deliberately don't configure it because I want it to work the same on any Linux box I log into. I use the standad Python syntax highlighting scheme. There is an IDLE-style settings file available somewhere that will make vim superficially like IDLE, but personally I didn't find it helped much and I prefer to keep to standard vim. > - added the following to .bashrc: > EDITOR=vim > export EDITOR You should do the same for VISUAL. Some programs distinguish between EDITOR and VISUAL and the default for both is ed... > I open up a terminal in the folder where I keep my python files and > create two additional tabs in this terminal. I use the first tab to > run python, the second one to run vim and the third one for running > linux commands such as ls to list the folder contents. I don't use a tabbed console but prefer three windows(*). One running graphical vim (vim -g), one running the python interpreter and one a standard shell prompt. My mode of working is try something in the interpreter, when it works translate it into vim. Save the file and then run it in the shell window (up arrow is my friend). (*) I usually have a browser open at the Python documentation too. And I have Firefox shortcuts/tags set up so I go to my favourite pages - like the module A-Z listing - in one short code (ie a-z). I also use split screen view in vim so that within vim I often have two or three buffers open at once all displayed in a split screen. One addition I'd like in vim 8 is the ability to have tabbed editor buffers in addition to split screens. Switching buffers isn't hard but tabs make it much easier. (Scite has tabs but no split screen and that's worse, but I'd like both!) > Is this is a reasonable approach? Thank you for any feed back. Works for me, and a lot of other folks too. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From knacktus at googlemail.com Thu Dec 16 08:43:58 2010 From: knacktus at googlemail.com (Knacktus) Date: Thu, 16 Dec 2010 08:43:58 +0100 Subject: [Tutor] vim as a python editor In-Reply-To: References: Message-ID: <4D09C33E.1060204@googlemail.com> Am 15.12.2010 23:26, schrieb Paul Griffiths: > Hi - I'm a beginner at programming and Python. > > I have been looking for an editor to replace Idle and tried out a few. > I liked Geany but decided on vim because I am also learning Linux > and the vim skills might be useful. I run Python 2.6.5 on Ubuntu 10.04. > > How have those of you who use vim configured it? I have looked > on the web but got a bit confused by the advice and options. > > I am currently following some of the advice in the on-line tutorial at > http://openbookproject.net/thinkcs/python/english2e/app_c.html and > > - installed the full version of vim (sudo apt-get install vim-gnome) > - created a .vimrc file in my home directory and entered > syntax enable > filetype indent on > set et > set sw=4 > set smarttab > map :w\|!python % > > - added the following to .bashrc: > EDITOR=vim > export EDITOR > > I open up a terminal in the folder where I keep my python files and > create two additional tabs in this terminal. I use the first tab to > run python, the second one to run vim and the third one for running > linux commands such as ls to list the folder contents. > > Is this is a reasonable approach? Thank you for any feed back. Sure. I'm more an IDE-guy for Python now, but in my "before-days" with Fortran I used pretty much this approach. Even though I'm using an IDE (on Windows) myself, I would recommend learning vi/vim. Regs, Jan > > > PaulG > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From knacktus at googlemail.com Thu Dec 16 08:51:59 2010 From: knacktus at googlemail.com (Knacktus) Date: Thu, 16 Dec 2010 08:51:59 +0100 Subject: [Tutor] vim as a python editor In-Reply-To: References: Message-ID: <4D09C51F.6060600@googlemail.com> Am 15.12.2010 23:26, schrieb Paul Griffiths: > Hi - I'm a beginner at programming and Python. > > I have been looking for an editor to replace Idle and tried out a few. > I liked Geany but decided on vim because I am also learning Linux > and the vim skills might be useful. I run Python 2.6.5 on Ubuntu 10.04. > > How have those of you who use vim configured it? I have looked > on the web but got a bit confused by the advice and options. > > I am currently following some of the advice in the on-line tutorial at > http://openbookproject.net/thinkcs/python/english2e/app_c.html and > > - installed the full version of vim (sudo apt-get install vim-gnome) > - created a .vimrc file in my home directory and entered > syntax enable > filetype indent on > set et > set sw=4 > set smarttab > map :w\|!python % > > - added the following to .bashrc: > EDITOR=vim > export EDITOR I forgot: Here some more information about vim and Python: http://wiki.python.org/moin/Vim Some guy tuned vim to the maximum with code completion and everything. But start simple to learn the basics. You can extend anytime later. Jan > > I open up a terminal in the folder where I keep my python files and > create two additional tabs in this terminal. I use the first tab to > run python, the second one to run vim and the third one for running > linux commands such as ls to list the folder contents. > > Is this is a reasonable approach? Thank you for any feed back. > > > PaulG > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Thu Dec 16 14:23:06 2010 From: __peter__ at web.de (Peter Otten) Date: Thu, 16 Dec 2010 14:23:06 +0100 Subject: [Tutor] Parameterized Queries failing on MySQL References: <6FB650C8B0580345A635730F45C37CEE4C9E99034B@MSGCMSV21011.ent.wfb.bank.corp> Message-ID: Shea Grove wrote: >> I'm using pyodbc to interact with MS SQL Server and I'm starting to >> support mysql. My issue is that when I use a parameterized query, it >> works for SQL Server, but crashes when I point to MySQL. I assume it raise an Exception. >> Is there a different syntax that I should be using? or module? MySQLdb uses a different paramstyle "format" (see http://www.python.org/dev/peps/pep-0249/ ) >> query='insert into RestaurantTable values (?, ?, ?, ?, ?, ?, ?, ?, ?)' Make this query = 'insert into RestaurantTable values (%s, %s, %s, %s, %s, %s, %s, %s, %s)' >> mcursor.execute(query, params) but *never* change the above to mcursor.execute(query % params) #WRONG which unfortunately will work most of the time with that paramstyle while making your app vulnerable to sql injection as demonstrated here: http://xkcd.com/327/ Peter From alan.gauld at btinternet.com Thu Dec 16 15:30:27 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 16 Dec 2010 14:30:27 -0000 Subject: [Tutor] Can't even get hello to work???!! References: <2815303B39A4D74C805DCD2049E615B50509E0CC8A@sdmcexch1.dice.ad> Message-ID: "Emile van Sebille" wrote >>> print 'hello' >>> ... >>> SyntaxError: invalid syntax > Actually, "the print statement is deprecated" ought not be out of > reach. Its very unfortunate that Python 3 didn't include a special error or warning message for the print statement. This kind of error for a complete beginner is very off putting and print "hello world" is probably the first thing many newbies try and many v2 tutorials start with. And even for non-newbies print syntax errors are by far the most common issue when migrating to Python 3 from 2 - I've been bitten several times myself before realizing its the missing parens on print... It is the kind of basic problem that will turn many would be programmers away from Python. You can argue about the purity of such a special method being inconsistent etc, but in practical terms if we wanted people to use Python we have just introduced a big, and largely unnecessary, barrier. A missed opportunity I fear. Alan G. From bgailer at gmail.com Thu Dec 16 19:13:58 2010 From: bgailer at gmail.com (bob gailer) Date: Thu, 16 Dec 2010 13:13:58 -0500 Subject: [Tutor] Can't even get hello to work???!! In-Reply-To: References: <2815303B39A4D74C805DCD2049E615B50509E0CC8A@sdmcexch1.dice.ad> Message-ID: <4D0A56E6.6040803@gmail.com> I agree. What do we do to fix the "problem"? -- Bob Gailer 919-636-4239 Chapel Hill NC From wescpy at gmail.com Thu Dec 16 19:43:37 2010 From: wescpy at gmail.com (wesley chun) Date: Thu, 16 Dec 2010 10:43:37 -0800 Subject: [Tutor] Can't even get hello to work???!! In-Reply-To: <4D0A56E6.6040803@gmail.com> References: <2815303B39A4D74C805DCD2049E615B50509E0CC8A@sdmcexch1.dice.ad> <4D0A56E6.6040803@gmail.com> Message-ID: On Thu, Dec 16, 2010 at 10:13 AM, bob gailer wrote: > I agree. What do we do to fix the "problem"? something like "quit" (or friends) might be nice, as they are already special cases in the interpreter: $ python Python 2.5.1 (r251:54863, Feb 9 2009, 18:49:36) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> quit Use quit() or Ctrl-D (i.e. EOF) to exit >>> help Type help() for interactive help, or help(object) for help about object. >>> copyright Copyright (c) 2001-2007 Python Software Foundation. All Rights Reserved. Copyright (c) 2000 BeOpen.com. All Rights Reserved. Copyright (c) 1995-2001 Corporation for National Research Initiatives. All Rights Reserved. Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam. All Rights Reserved. >>> license Type license() to see the full license text just my $0.02, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 ? ? http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From joel at joelschwartz.com Thu Dec 16 19:58:06 2010 From: joel at joelschwartz.com (Joel Schwartz) Date: Thu, 16 Dec 2010 10:58:06 -0800 Subject: [Tutor] vim as a python editor; FOLLOW-UP QUESTION In-Reply-To: <4D09C51F.6060600@googlemail.com> References: <4D09C51F.6060600@googlemail.com> Message-ID: I'm new to Python and have been using IDLE for Python programming. My only other significant text editor experience is with Notepad++ and Tinn-R, which I've used for R programming. Vim seems more efficient, but I wanted to ask you experts whether it makes sense (or is even possible) to use vim in Windows (I use Windows XP-SP3). One more question: IDLE does not appear to have a way to review your command history, e.g., by hitting the up arrow. I find the up-arrow to be an incredible convenience when I'm working on the R command line, and the lack of it in IDLE is often a source of frustration for me. Is there any way to set up IDLE to do this? Alternatively, is there another basic Python GUI that has the up-arrow command history feature and is also good for Python coding in general? Thanks, Joel -----Original Message----- From: tutor-bounces+joel=joelschwartz.com at python.org [mailto:tutor-bounces+joel=joelschwartz.com at python.org] On Behalf Of Knacktus Sent: Wednesday, December 15, 2010 11:52 PM To: tutor at python.org Subject: Re: [Tutor] vim as a python editor Am 15.12.2010 23:26, schrieb Paul Griffiths: > Hi - I'm a beginner at programming and Python. > > I have been looking for an editor to replace Idle and tried out a few. > I liked Geany but decided on vim because I am also learning Linux and > the vim skills might be useful. I run Python 2.6.5 on Ubuntu 10.04. > > How have those of you who use vim configured it? I have looked on the > web but got a bit confused by the advice and options. > > I am currently following some of the advice in the on-line tutorial at > http://openbookproject.net/thinkcs/python/english2e/app_c.html and > > - installed the full version of vim (sudo apt-get install vim-gnome) > - created a .vimrc file in my home directory and entered > syntax enable > filetype indent on > set et > set sw=4 > set smarttab > map :w\|!python % > > - added the following to .bashrc: > EDITOR=vim > export EDITOR I forgot: Here some more information about vim and Python: http://wiki.python.org/moin/Vim Some guy tuned vim to the maximum with code completion and everything. But start simple to learn the basics. You can extend anytime later. Jan > > I open up a terminal in the folder where I keep my python files and > create two additional tabs in this terminal. I use the first tab to > run python, the second one to run vim and the third one for running > linux commands such as ls to list the folder contents. > > Is this is a reasonable approach? Thank you for any feed back. > > > PaulG > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor From bgailer at gmail.com Thu Dec 16 20:38:55 2010 From: bgailer at gmail.com (bob gailer) Date: Thu, 16 Dec 2010 14:38:55 -0500 Subject: [Tutor] vim as a python editor; FOLLOW-UP QUESTION In-Reply-To: References: <4D09C51F.6060600@googlemail.com> Message-ID: <4D0A6ACF.7050705@gmail.com> On 12/16/2010 1:58 PM, Joel Schwartz wrote: > I'm new to Python and have been using IDLE for Python programming. My only > other significant text editor experience is with Notepad++ and Tinn-R, which > I've used for R programming. Vim seems more efficient, but I wanted to ask > you experts whether it makes sense (or is even possible) to use vim in > Windows (I use Windows XP-SP3). FWIW I started 10 years ago using Python For Windows as my development environment. I have never regretted that choice. -- Bob Gailer 919-636-4239 Chapel Hill NC From steve at alchemy.com Thu Dec 16 20:24:54 2010 From: steve at alchemy.com (Steve Willoughby) Date: Thu, 16 Dec 2010 11:24:54 -0800 Subject: [Tutor] vim as a python editor; FOLLOW-UP QUESTION In-Reply-To: References: <4D09C51F.6060600@googlemail.com> Message-ID: <4D0A6786.5060400@alchemy.com> On 16-Dec-10 10:58, Joel Schwartz wrote: > I'm new to Python and have been using IDLE for Python programming. My only > other significant text editor experience is with Notepad++ and Tinn-R, which > I've used for R programming. Vim seems more efficient, but I wanted to ask > you experts whether it makes sense (or is even possible) to use vim in > Windows (I use Windows XP-SP3). Vim works wonderfully in Windows, actually, which IMHO really needs an advanced text editor like vi or emacs or something. > One more question: IDLE does not appear to have a way to review your command > history, e.g., by hitting the up arrow. I find the up-arrow to be an > incredible convenience when I'm working on the R command line, and the lack > of it in IDLE is often a source of frustration for me. Is there any way to > set up IDLE to do this? Alternatively, is there another basic Python GUI > that has the up-arrow command history feature and is also good for Python > coding in general? If you use the up-arrow to move your cursor to previous lines in the IDLE window and hit ENTER, you recall that line where you can edit and re-enter it. -- Steve Willoughby / steve at alchemy.com "A ship in harbor is safe, but that is not what ships are built for." PGP Fingerprint 48A3 2621 E72C 31D9 2928 2E8F 6506 DB29 54F7 0F53 From python at bdurham.com Thu Dec 16 20:47:42 2010 From: python at bdurham.com (python at bdurham.com) Date: Thu, 16 Dec 2010 14:47:42 -0500 Subject: [Tutor] vim as a python editor; FOLLOW-UP QUESTION In-Reply-To: <4D0A6786.5060400@alchemy.com> References: <4D09C51F.6060600@googlemail.com> <4D0A6786.5060400@alchemy.com> Message-ID: <1292528862.25532.1410717205@webmail.messagingengine.com> Joel, > One more question: IDLE does not appear to have a way to review your command > history, e.g., by hitting the up arrow. To move through your command history, use Alt+P (previous) and Alt+N (next). Malcolm From scarolan at gmail.com Thu Dec 16 21:28:26 2010 From: scarolan at gmail.com (Sean Carolan) Date: Thu, 16 Dec 2010 14:28:26 -0600 Subject: [Tutor] How to import python dictionary into MySQL table? Message-ID: Maybe one of you can help with this. I've got some data that I grabbed from a cobbler *.json file using simplejson.load(). Heres what the data looks like: In [120]: print mydata.keys() ['comment', 'kickstart', 'name_servers_search', 'ks_meta', 'kernel_options_post', 'image', 'redhat_management_key', 'power_type', 'power_user', 'kernel_options', 'virt_file_size', 'mtime', 'template_files', 'gateway', 'uid', 'virt_cpus', 'hostname', 'virt_type', 'mgmt_classes', 'power_pass', 'netboot_enabled', 'profile', 'virt_bridge', 'parent', 'virt_path', 'interfaces', 'power_address', 'name_servers', 'name', 'owners', 'ctime', 'virt_ram', 'power_id', 'random_id', 'server', 'redhat_management_server', 'depth'] In [121]: print mydata.values() ['Virtual Machine', '<>', ['subdomain.company.com', 'company.com'], {'puppet_environment': 'development'}, {}, '', '~', 'apc_snmp', '', {'dns': '10.10.132.12'}, 160, 1286408237.2311871, {}, '10.10.32.3', 'asIjeKJIDOOUuUIDSwOTY', 16, 'webservice-app16.subdomain.company.com', '<>', [], '', True, 'rhel5-server-64-domU', '<>', '', '<>', {'eth0': {'dhcp_tag': '', 'subnet': '255.255.255.0', 'virt_bridge': 'xenbr0', 'static_routes': ['10.10.0.0/255.255.0.0:10.10.32.3', '10.10.0.0/255.255.0.0:10.10.32.3'], 'dns_name': 'webservice-app16.subdomain.company.com', 'bonding': '', 'static': True, 'bonding_opts': '', 'mac_address': '00:16:3e:aa:aa:aa', 'bonding_master': '', 'ip_address': '10.10.32.219'}}, '', ['10.10.132.12', '10.10.132.13'], 'webservice-app16.subdomain.company.com', ['scarolan'], 1277755073.144805, 11749, '', '7oWY', '<>', '~', 2] I have a database with a table called "systems" that contains an auto-increment id field, as well as fields for each of the keys in mydata.keys(). But I can't seem to get the syntax to import mydata.values() into the table. I think the problem may be that some of the items in my list are dictionaries or lists... What would be the quickest and most straightforward way to do this? My table is below if you need a reference, sorry for the ugly formatting: mysql> show columns in systems; +--------------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------------------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | comment | varchar(255) | YES | | NULL | | | kickstart | varchar(255) | YES | | NULL | | | name_servers_search | varchar(255) | YES | | NULL | | | ks_meta | varchar(255) | YES | | NULL | | | kernel_options_post | varchar(255) | YES | | NULL | | | image | varchar(255) | YES | | NULL | | | redhat_management_key | varchar(255) | YES | | NULL | | | power_type | varchar(255) | YES | | NULL | | | power_user | varchar(255) | YES | | NULL | | | kernel_options | varchar(255) | YES | | NULL | | | virt_file_size | int(11) | YES | | NULL | | | mtime | int(11) | YES | | NULL | | | template_files | varchar(255) | YES | | NULL | | | gateway | varchar(255) | YES | | NULL | | | uid | varchar(255) | YES | | NULL | | | virt_cpus | smallint(64) | YES | | NULL | | | hostname | varchar(255) | YES | | NULL | | | virt_type | varchar(255) | YES | | NULL | | | mgmt_classes | varchar(255) | YES | | NULL | | | power_pass | varchar(255) | YES | | NULL | | | netboot_enabled | varchar(255) | YES | | NULL | | | profile | varchar(255) | YES | | NULL | | | virt_bridge | varchar(255) | YES | | NULL | | | parent | varchar(255) | YES | | NULL | | | virt_path | varchar(255) | YES | | NULL | | | interfaces | text | YES | | NULL | | | power_address | varchar(255) | YES | | NULL | | | name_servers | varchar(255) | YES | | NULL | | | name | varchar(255) | YES | | NULL | | | owners | varchar(255) | YES | | NULL | | | ctime | int(11) | YES | | NULL | | | virt_ram | int(11) | YES | | NULL | | | power_id | varchar(255) | YES | | NULL | | | random_id | varchar(255) | YES | | NULL | | | server | varchar(255) | YES | | NULL | | | redhat_management_server | varchar(255) | YES | | NULL | | | depth | tinyint(4) | YES | | NULL | | +--------------------------+--------------+------+-----+---------+----------------+ From waynejwerner at gmail.com Thu Dec 16 23:27:22 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Thu, 16 Dec 2010 16:27:22 -0600 Subject: [Tutor] vim as a python editor; FOLLOW-UP QUESTION In-Reply-To: References: <4D09C51F.6060600@googlemail.com> Message-ID: On Thu, Dec 16, 2010 at 12:58 PM, Joel Schwartz wrote: > whether it makes sense (or is even possible) to use vim in > Windows (I use Windows XP-SP3). > Alternatively, is there another basic Python GUI > that has the up-arrow command history feature and is also good for Python > coding in general? > On Windows and Linux both I use vim and IPython. They make a rather powerful combination. Then again so do a lot of other configurations, so whatever you feel comfortable with. The vim keybindings are extremely powerful, though, and regardless of what editor you choose, I recommend picking one that has good keybindings either natively or through a plugin. HTH, Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Dec 17 01:27:11 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 17 Dec 2010 00:27:11 -0000 Subject: [Tutor] vim as a python editor; FOLLOW-UP QUESTION References: <4D09C51F.6060600@googlemail.com> Message-ID: "Joel Schwartz" wrote > you experts whether it makes sense (or is even possible) to use vim > in > Windows (I use Windows XP-SP3). Yes, vim is great on Windows. When i used unix primarily I was an emacs fan, but emacs just doesn't feel right on windows to me so I moved to vim which works hust as well in both OS. > One more question: IDLE does not appear to have a way to > review your command history, e.g., by hitting the up arrow. IDLE borrows from emacs and uses ALT-P and ALT-N. (FWIW Pythonwin uses Ctrl-UP/Ctrl-Down for the same purpose) > set up IDLE to do this? Alternatively, is there another basic Python > GUI > that has the up-arrow command history feature and is also good for > Python > coding in general? Pythonwin is better than IDLE for Windows users. It has integration with COM for object browsing and also with the Windows help system. And the debugger is much better than the IDLE one too. And it users the same editor control as Scite. Speaking of Scite, it is a pretty useful editor in its own right and adds tabbed editing which Pythonwin doesn't have, but OTOH it loses all the Python specific tools. my ideal Windows text editor would be a combination of Scite and vim... Finally, if you have a PC big enough to run it well, Eclipse and Pydev are a good combination. Especially if you also work in Java or C++ etc And there are a bunch of others that folks like. IDEs and text editors are very personal choices. Wingz, SPE, PyCrust/PyAlaMode, and more. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From scarolan at gmail.com Fri Dec 17 04:39:57 2010 From: scarolan at gmail.com (Sean Carolan) Date: Thu, 16 Dec 2010 21:39:57 -0600 Subject: [Tutor] How to import python dictionary into MySQL table? In-Reply-To: References: Message-ID: > I have a database with a table called "systems" that contains an > auto-increment id field, as well as fields for each of the keys in > mydata.keys(). ?But I can't seem to get the syntax to import > mydata.values() into the table. ?I think the problem may be that some > of the items in my list are dictionaries or lists... > > What would be the quickest and most straightforward way to do this? I got this working in case anyone else comes across the same problem. This function will pull cobbler *.json data into a MySQL database table. The function assumes that you already have a database set up, and that you are dumping the data into the "systems" table: def importJSON(targetfile): ''' Imports JSON data from targetfile into MySQL database table. ''' value_list = [] rawdata = json.load(open(targetfile)) for key in rawdata.keys(): strvalue = str(rawdata[key]).replace("'",'"') value_list.append(strvalue) valtuple = tuple(value_list) sql = "INSERT INTO systems (comment, kickstart, name_servers_search, ks_meta, kernel_options_post, image, redhat_management_key, power_type, power_user, kernel_options, vi rt_file_size, mtime, template_files, gateway, uid, virt_cpus, hostname, virt_type, mgmt_classes, power_pass, netboot_enabled, profile, virt_bridge, parent, virt_path, interfaces, power_address, name_servers, name, owners, ctime, virt_ram, power_id, random_id, server, redhat_management_server, depth) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);" cursor.execute(sql, valtuple) From __peter__ at web.de Fri Dec 17 11:34:57 2010 From: __peter__ at web.de (Peter Otten) Date: Fri, 17 Dec 2010 11:34:57 +0100 Subject: [Tutor] How to import python dictionary into MySQL table? References: Message-ID: Sean Carolan wrote: >> I have a database with a table called "systems" that contains an >> auto-increment id field, as well as fields for each of the keys in >> mydata.keys(). But I can't seem to get the syntax to import >> mydata.values() into the table. I think the problem may be that some >> of the items in my list are dictionaries or lists... >> >> What would be the quickest and most straightforward way to do this? > > I got this working in case anyone else comes across the same problem. > This function will pull cobbler *.json data into a MySQL database > table. The function assumes that you already have a database set up, > and that you are dumping the data into the "systems" table: > > def importJSON(targetfile): > ''' > Imports JSON data from targetfile into MySQL database table. > ''' > value_list = [] > rawdata = json.load(open(targetfile)) > for key in rawdata.keys(): > strvalue = str(rawdata[key]).replace("'",'"') Is the above line really needed? > value_list.append(strvalue) > valtuple = tuple(value_list) > sql = "INSERT INTO systems (comment, kickstart, > name_servers_search, ks_meta, kernel_options_post, image, > redhat_management_key, power_type, power_user, kernel_options, vi > rt_file_size, mtime, template_files, gateway, uid, virt_cpus, > hostname, virt_type, mgmt_classes, power_pass, netboot_enabled, > profile, virt_bridge, parent, virt_path, interfaces, power_address, > name_servers, name, owners, ctime, virt_ram, power_id, random_id, > server, redhat_management_server, depth) VALUES (%s, %s, %s, %s, %s, > %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, > %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);" > cursor.execute(sql, valtuple) The order of key/value pairs in a dictionary is an implementation detail. I think it's dangerous to assume they will always be in sync with the column names as provided in the string constant. Here's what I came up with when I tried to make a generalized version of the above: def add_row(cursor, tablename, rowdict): # XXX tablename not sanitized # XXX test for allowed keys is case-sensitive # filter out keys that are not column names cursor.execute("describe %s" % tablename) allowed_keys = set(row[0] for row in cursor.fetchall()) keys = allowed_keys.intersection(rowdict) if len(rowdict) > len(keys): unknown_keys = set(rowdict) - allowed_keys print >> sys.stderr, "skipping keys:", ", ".join(unknown_keys) columns = ", ".join(keys) values_template = ", ".join(["%s"] * len(keys)) sql = "insert into %s (%s) values (%s)" % ( tablename, columns, values_template) values = tuple(rowdict[key] for key in keys) cursor.execute(sql, values) filename = ... tablename = ... db = MySQLdb.connect(...) cursor = db.cursor() with open(filename) as instream: row = json.load(instream) add_row(cursor, tablename, row) Peter From zradu1100 at gmail.com Fri Dec 17 12:46:20 2010 From: zradu1100 at gmail.com (zicu radu) Date: Fri, 17 Dec 2010 11:46:20 +0000 (UTC) Subject: [Tutor] Looking for a tutor to review my code and provide constructive feedback. Message-ID: Hello, How to prevent shadows a built-in in code. Please if you are kind enougth to take the time to provide feedback I would appreciate that it is constructive. The script is here: http://bpaste.net/show/12364/ From steve at pearwood.info Fri Dec 17 15:46:52 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 18 Dec 2010 01:46:52 +1100 Subject: [Tutor] Looking for a tutor to review my code and provide constructive feedback. In-Reply-To: References: Message-ID: <4D0B77DC.6000605@pearwood.info> zicu radu wrote: > Hello, > > How to prevent shadows a built-in in code. > Please if you are kind enougth to take the time to provide feedback I would > appreciate that it is constructive. > > The script is here: http://bpaste.net/show/12364/ Are you sure you have posted the right link? This is a function called "fixXML". It doesn't seem to have anything to do with preventing the shadowing of built-ins. -- Steven From joel at joelschwartz.com Fri Dec 17 18:52:19 2010 From: joel at joelschwartz.com (Joel Schwartz) Date: Fri, 17 Dec 2010 09:52:19 -0800 Subject: [Tutor] vim as a python editor; FOLLOW-UP QUESTION In-Reply-To: References: <4D09C51F.6060600@googlemail.com> Message-ID: <00cd01cb9e13$27195550$6501a8c0@JLAPTOP> Thanks for all the good advice guys! Joel > -----Original Message----- > From: tutor-bounces+joel=joelschwartz.com at python.org > [mailto:tutor-bounces+joel=joelschwartz.com at python.org] On > Behalf Of Alan Gauld > Sent: Thursday, December 16, 2010 4:27 PM > To: tutor at python.org > Subject: Re: [Tutor] vim as a python editor; FOLLOW-UP QUESTION > > "Joel Schwartz" wrote > > you experts whether it makes sense (or is even possible) to > use vim in > > Windows (I use Windows XP-SP3). > > Yes, vim is great on Windows. > When i used unix primarily I was an emacs fan, but emacs just > doesn't feel right on windows to me so I moved to vim which > works hust as well in both OS. > > > One more question: IDLE does not appear to have a way to > review your > > command history, e.g., by hitting the up arrow. > > IDLE borrows from emacs and uses ALT-P and ALT-N. > (FWIW Pythonwin uses Ctrl-UP/Ctrl-Down for the same purpose) > > > set up IDLE to do this? Alternatively, is there another > basic Python > > GUI that has the up-arrow command history feature and is > also good for > > Python coding in general? > > Pythonwin is better than IDLE for Windows users. It has > integration with COM for object browsing and also with the > Windows help system. > And the debugger is much better than the IDLE one too. > > And it users the same editor control as Scite. > Speaking of Scite, it is a pretty useful editor in its own > right and adds tabbed editing which Pythonwin doesn't have, > but OTOH it loses all the Python specific tools. my ideal > Windows text editor would be a combination of Scite and vim... > > Finally, if you have a PC big enough to run it well, Eclipse > and Pydev are a good combination. Especially if you also work > in Java or C++ etc > > And there are a bunch of others that folks like. IDEs and > text editors are very personal choices. Wingz, SPE, > PyCrust/PyAlaMode, and more. > > HTH, > > -- > Alan Gauld > 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 > From jacksmoo111 at gmail.com Sat Dec 18 01:07:31 2010 From: jacksmoo111 at gmail.com (jtl999) Date: Fri, 17 Dec 2010 16:07:31 -0800 Subject: [Tutor] os.system Message-ID: <1292630851.2476.7.camel@nerdism> I want to pass a variable to os.system which is dir >>> dir=raw_input ("Dir Please ") Dir Please / ok dir = / >>> os.system ("ls" + dir) sh: ls/: No such file or directory 32512 any advice -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 836 bytes Desc: This is a digitally signed message part URL: From emile at fenx.com Sat Dec 18 01:17:44 2010 From: emile at fenx.com (Emile van Sebille) Date: Fri, 17 Dec 2010 16:17:44 -0800 Subject: [Tutor] os.system In-Reply-To: <1292630851.2476.7.camel@nerdism> References: <1292630851.2476.7.camel@nerdism> Message-ID: On 12/17/2010 4:07 PM jtl999 said... > I want to pass a variable to os.system which is dir >>>> dir=raw_input ("Dir Please ") > Dir Please / > ok dir = / >>>> os.system ("ls" + dir) > sh: ls/: No such file or directory this has concatenated "ls" and dir try with "ls " Emile > 32512 > > any advice > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From matt.gregory at oregonstate.edu Sat Dec 18 01:48:30 2010 From: matt.gregory at oregonstate.edu (Gregory, Matthew) Date: Fri, 17 Dec 2010 16:48:30 -0800 Subject: [Tutor] calling setters of superclasses Message-ID: <1D673F86DDA00841A1216F04D1CE70D642680008E8@EXCH2.nws.oregonstate.edu> Hi all, Consider the following classes where PositiveX should constrain the attribute _x to be positive and SmallX should further constrain the attribute _x to be less than 10. class PositiveX(object): def __init__(self): self._x = 1 @property def x(self): return self._x @x.setter def x(self, val): if val < 0: raise ValueError('Negatives not allowed') self._x = val class SmallX(PositiveX): @property def x(self): return self._x @x.setter def x(self, val): # How do I call the superclass' @x.setter super(SmallX, self).__setattr__('x', val) if val > 10: raise ValueError('Big values not allowed') self._x = val I thought I could call the superclass setter first to ensure the value was positive, but I'm getting an infinite recursion. I also tried: super(SmallX, self).x = val but I get this: AttributeError: 'super' object has no attribute 'x' I'm fully confused and, therefore, likely doing something stupid. thanks, matt From wescpy at gmail.com Sat Dec 18 02:01:50 2010 From: wescpy at gmail.com (wesley chun) Date: Fri, 17 Dec 2010 17:01:50 -0800 Subject: [Tutor] os.system In-Reply-To: References: <1292630851.2476.7.camel@nerdism> Message-ID: On Fri, Dec 17, 2010 at 4:17 PM, Emile van Sebille wrote: > On 12/17/2010 4:07 PM jtl999 said... >> >> I want to pass a variable to os.system which is dir >>>>> dir=raw_input ("Dir Please ") >>>>> os.system ("ls" ?+ dir) >> sh: ls/: No such file or directory > > this has concatenated "ls" and dir > try with "ls " in addition to emile's obvious suggestion, i have a few minor ones: 1) don't use a built-in function as a variable name (dir) 2) os.system() only sends off the request. if the cmd has output, you get it too. you can't capture it like you can with os.popen*() 3) you should consider using subprocess... it obsoletes all the os.*() functions cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 ? ? http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From steve at pearwood.info Sat Dec 18 02:02:50 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 18 Dec 2010 12:02:50 +1100 Subject: [Tutor] os.system In-Reply-To: <1292630851.2476.7.camel@nerdism> References: <1292630851.2476.7.camel@nerdism> Message-ID: <4D0C083A.1040503@pearwood.info> jtl999 wrote: > I want to pass a variable to os.system which is dir >>>> dir=raw_input ("Dir Please ") > Dir Please / > ok dir = / >>>> os.system ("ls" + dir) > sh: ls/: No such file or directory > 32512 > > any advice Yes -- learn to read the error messages you get. The error you get is a shell error, not a Python error. It is *exactly* the same error you would get if you do this by hand: sh-3.2$ ls/ sh: ls/: No such file or directory This tells you that the shell can't find a file called "ls/". No surprises there -- you want "ls /". So you need to include a space in the shell command that you pass to os.system. -- Steven From modulok at gmail.com Sat Dec 18 02:18:08 2010 From: modulok at gmail.com (Modulok) Date: Fri, 17 Dec 2010 18:18:08 -0700 Subject: [Tutor] unit testing - Separate methods or group tests together? Message-ID: List, When you create unit tests, do you group tests for a given function together into one unit test method, or do you prefer to have separate test methods for every assert statement? Thanks! -Modulok- From alan.gauld at btinternet.com Sat Dec 18 02:25:29 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 18 Dec 2010 01:25:29 -0000 Subject: [Tutor] calling setters of superclasses References: <1D673F86DDA00841A1216F04D1CE70D642680008E8@EXCH2.nws.oregonstate.edu> Message-ID: "Gregory, Matthew" wrote > class PositiveX(object): > def __init__(self): > @property > def x(self): > @x.setter > def x(self, val): I don't use properties in Python very often (hardly ever in fact) and I've never used @setter so there may be naming requirements I'm not aware of. But in general I'd avoid having two methods with the same name. Try renaming the setter to setX() or somesuch and see if you get the same error. Just a thought. Alan G. From steve at pearwood.info Sat Dec 18 06:39:35 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 18 Dec 2010 16:39:35 +1100 Subject: [Tutor] unit testing - Separate methods or group tests together? In-Reply-To: References: Message-ID: <4D0C4917.5070402@pearwood.info> Modulok wrote: > List, > > When you create unit tests, do you group tests for a given function > together into one unit test method, or do you prefer to have separate > test methods for every assert statement? Each test method should test one thing. That doesn't necessarily mean one assert, but one conceptual thing. Here's an example from one of my test suites: class QuartileSkewnessTest(unittest.TestCase): def testFailure(self): # Test that function correctly raises an exception if the # arguments are out of order. self.assertRaises(ValueError, stats.quartile_skewness, 2, 3, 1) self.assertRaises(ValueError, stats.quartile_skewness, 9, 8, 7) def testNan(self): # Test that the degenerate case where all three arguments are # equal returns NAN. self.assertTrue(math.isnan(stats.quartile_skewness(1, 1, 1))) self.assertTrue(math.isnan(stats.quartile_skewness(5, 5, 5))) def testSkew(self): # Test skew calculations. self.assertEqual(stats.quartile_skewness(3, 5, 7), 0.0) self.assertEqual(stats.quartile_skewness(0, 1, 10), 0.8) self.assertEqual(stats.quartile_skewness(0, 9, 10), -0.8) In this particular case, I happen to have one test class for this function, but that's not always the case. Each test class is for a set of related tests (e.g. "test everything about this function", or "test that this same property holds for all eight of these functions", or similar). Within the test class, each test method is for one test, not necessarily one assert. -- Steven From karim.liateni at free.fr Sat Dec 18 07:41:22 2010 From: karim.liateni at free.fr (Karim) Date: Sat, 18 Dec 2010 07:41:22 +0100 Subject: [Tutor] The Template Pattern In-Reply-To: <4D06AA03.804@free.fr> References: <4D064339.6050507@free.fr> <4D06A27F.3070803@pearwood.info> <4D06AA03.804@free.fr> Message-ID: <4D0C5792.20103@free.fr> Hello Steven, Could take some time when possible to answer to my follow-up question? Thank you! Cheers Karim On 12/14/2010 12:19 AM, Karim wrote: > On 12/13/2010 11:47 PM, Steven D'Aprano wrote: >> Karim wrote: >>> >>> Hello all, >>> >>> I am seeking for information about the template pattern applied to >>> python. >>> Could you explain some implementation or anything else? it would be >>> helpful. >> >> Design patterns are means to an end, not an end in themselves. You >> shouldn't say "I want to use the template pattern", you should say "I >> want to solve this problem, what solution is appropriate?" and then >> only use the template pattern if it is appropriate. >> > > Steven, thank you for answering ;o) > > I don't want to use this pattern absolutely. But I discussed this > topic with a C++ programmer. > He wanted to translate part of C++ code in python that implements this > pattern in C++. > I already have implemented a Strategy pattern in python to read > different file format into > my database and generate different output format file like the code > below: > > class Input(object): > """The basic Input class, leaving out the details of the > algorithm.""" > def __init__( self, strategy ): > self.strategy = strategy > self.lastRead = None > > def read(self, filePath): > """Load into database memory an input file. > @arg: filePath - string - The input file path. > @return: The created database object. > """ > self.lastRead = self.strategy.read() > return self.lastRead > > > class InputStrategy( object ): > """This InputStrategy class is an abstract interface to various > read strategy objects. > """ > def read(self, filePath): > """Abstract method to load into database memory an input file. > @arg: filePath - string - The input file path. > @return: The created database object. > """ > raise NotImplementedError > > By the way, I use NotImplementedError in my abstract method of the > abstract class. You are using TypeError exception. > Is there a general rule for that? NotImplementedError is ok? > > I knew from Java experience that template method pattern is only a > kind of particular case of Stategy where you delegate > parts of algorithm by abstract methods overriding. Indeed the base > class implements some common features and let > derived classes implement particular parts by polymorphism. > Now with your example, I don't understand why he had problems to > implement from C++ ? > Or perhaps is he mixing it with C++ feature template ?! > >> In any case, the template design pattern is one of the simpler design >> patterns. Here's an example, adapted from Wikipedia's article on the >> Template Method Pattern: >> >> >> class AbstractGame: >> """An abstract class that is common to several games in which >> players play against the others, but only one is playing at a >> given time. >> """ >> def __init__(self, *args, **kwargs): >> if self.__class__ is AbstractGame: >> raise TypeError('abstract class cannot be instantiated') >> >> def playOneGame(self, playersCount): >> self.playersCount = playersCount >> self.initializeGame() >> j = 0 >> while not self.endOfGame(): >> self.makePlay(j) >> j = (j + 1) % self.playersCount >> self.printWinner() >> >> def initializeGame(self): >> raise TypeError('abstract method must be overridden') >> >> def endOfGame(self): >> raise TypeError('abstract method must be overridden') >> >> def makePlay(self, player_num): >> raise TypeError('abstract method must be overridden') >> >> def printWinner(self): >> raise TypeError('abstract method must be overridden') >> >> >> Now to create concrete (non-abstract) games, you subclass >> AbstractGame and override the abstract methods. >> >> class Chess(AbstractGame): >> def initializeGame(self): >> # Put the pieces on the board. >> ... >> >> def makePlay(player): >> # Process a turn for the player >> ... >> >> etc. > > > One more Thanks for you example Steven! > Regards > Karim > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From karim.liateni at free.fr Sat Dec 18 08:05:20 2010 From: karim.liateni at free.fr (Karim) Date: Sat, 18 Dec 2010 08:05:20 +0100 Subject: [Tutor] Writing to the terminal? In-Reply-To: <4D0292C2.2050306@pearwood.info> References: <4D0292C2.2050306@pearwood.info> Message-ID: <4D0C5D30.7090006@free.fr> Hello Steven, I added the pipe char '|' to have a complete spinner! This would be set as a function for my wait routine installer. Thanks to share! Karim On 12/10/2010 09:51 PM, Steven D'Aprano wrote: > Modulok wrote: >> List, >> >> Forgive me if I don't describe this well, I'm new to it: > > [snip description of a progress bar] > > Here's one way: > > import time, sys > f = sys.stdout > for i in range(20): > time.sleep(1) # do some work > f.write('=') > f.flush() # make the change visible immediately > else: > f.write('\n') > > > You might be able to get the same effect with print, but it's probably > easier using sys.stdout directly. > > Here's another way, which *must* use stdout and not print. > > for i in range(20): > percentage = i/20.0 > spinner = '/-\\-'[i % 4] > f.write("Progress: %5.2f%% %s %s>\r" % > (percentage, spinner, '='*(i+1))) > f.flush() > time.sleep(1) > else: > f.write('\n') > > > > > From karim.liateni at free.fr Sat Dec 18 08:25:53 2010 From: karim.liateni at free.fr (Karim) Date: Sat, 18 Dec 2010 08:25:53 +0100 Subject: [Tutor] Writing to the terminal? In-Reply-To: <4D0C5D30.7090006@free.fr> References: <4D0292C2.2050306@pearwood.info> <4D0C5D30.7090006@free.fr> Message-ID: <4D0C6201.7010300@free.fr> I suppose we can do something like that (kind of pseudo code) to fully configure it: def doSomeStuff(*args): ... def progressBar( func, *args) import time, sys f = sys.stdout for i in range(20): func.__call__(args) f.write('=') f.flush() # make the change visible immediately else: f.write('\n') progressBar( doSomeStuff ) Cheers Karim On 12/18/2010 08:05 AM, Karim wrote: > > Hello Steven, > > I added the pipe char '|' to have a complete spinner! > This would be set as a function for my wait routine installer. > > Thanks to share! > Karim > > > On 12/10/2010 09:51 PM, Steven D'Aprano wrote: >> Modulok wrote: >>> List, >>> >>> Forgive me if I don't describe this well, I'm new to it: >> >> [snip description of a progress bar] >> >> Here's one way: >> >> import time, sys >> f = sys.stdout >> for i in range(20): >> time.sleep(1) # do some work >> f.write('=') >> f.flush() # make the change visible immediately >> else: >> f.write('\n') >> >> >> You might be able to get the same effect with print, but it's >> probably easier using sys.stdout directly. >> >> Here's another way, which *must* use stdout and not print. >> >> for i in range(20): >> percentage = i/20.0 >> spinner = '/-\\-'[i % 4] >> f.write("Progress: %5.2f%% %s %s>\r" % >> (percentage, spinner, '='*(i+1))) >> f.flush() >> time.sleep(1) >> else: >> f.write('\n') >> >> >> >> >> > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Sat Dec 18 11:06:04 2010 From: __peter__ at web.de (Peter Otten) Date: Sat, 18 Dec 2010 11:06:04 +0100 Subject: [Tutor] calling setters of superclasses References: <1D673F86DDA00841A1216F04D1CE70D642680008E8@EXCH2.nws.oregonstate.edu> Message-ID: Gregory, Matthew wrote: > Hi all, > > Consider the following classes where PositiveX should constrain the > attribute _x to be positive and SmallX should further constrain the > attribute _x to be less than 10. > > class PositiveX(object): > def __init__(self): > self._x = 1 > @property > def x(self): > return self._x > @x.setter > def x(self, val): > if val < 0: > raise ValueError('Negatives not allowed') > self._x = val > > class SmallX(PositiveX): > @property > def x(self): > return self._x > @x.setter > def x(self, val): > # How do I call the superclass' @x.setter > super(SmallX, self).__setattr__('x', val) > if val > 10: > raise ValueError('Big values not allowed') > self._x = val > > I thought I could call the superclass setter first to ensure the value was > positive, but I'm getting an infinite recursion. I also tried: > > super(SmallX, self).x = val > > but I get this: > > AttributeError: 'super' object has no attribute 'x' > > I'm fully confused and, therefore, likely doing something stupid. I don't think /how/ you are trying it is stupid though I'm not so sure about /what/ . I didn't get it to work with super() either, so here's Plan B till someone is going to enlighten both of us: class SmallX(PositiveX): @property def x(self): return self._x @x.setter def x(self, val): if val > 10: raise ValueError('Big values not allowed') PositiveX.x.__set__(self, val) Personally, I would more or less follow Alan's advice and do something like class PositiveX(object): def __init__(self): self._x = 1 def check_x(self, val): if val < 0: raise ValueError('Negatives not allowed') @property def x(self): return self._x @x.setter def x(self, val): self.check_x(val) self._x = val class SmallX(PositiveX): def check_x(self, val): super(SmallX, self).check_x(val) if val > 10: raise ValueError('Big values not allowed') Peter From yanglei.fage at gmail.com Sat Dec 18 17:23:37 2010 From: yanglei.fage at gmail.com (lei yang) Date: Sun, 19 Dec 2010 00:23:37 +0800 Subject: [Tutor] why "ifconfig" is alway running? Message-ID: my script is #!/usr/bin/env python import datetime import subprocess import sys import os import signal from time import sleep def runForAWhile(cmd, secs=10): print("running %s" % cmd) timeout = datetime.timedelta(seconds=secs) print timeout proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) status = proc.poll() start = datetime.datetime.now() while (status is None and (datetime.datetime.now() - start) < timeout): #not timed out #print proc.stdout.readline() #TODO timestamp? print status #print datetime.datetime.now() - start if 0 == status: print("'%s' is program exited" %cmd) else: try: os.kill(proc.pid, signal.SIGINT) print "Process timeout: '%s'" % cmd except OSError: pass cmd="ifconfig" runForAWhile(cmd,10) why it print many "None" in 10 second. which means "ifconfig" is running in 10sec, why, what's wrong withi my script, I just want to let my programe running, if it's timeout(10sec), kill it Lei From hugo.yoshi at gmail.com Sat Dec 18 17:59:41 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sat, 18 Dec 2010 17:59:41 +0100 Subject: [Tutor] calling setters of superclasses In-Reply-To: References: <1D673F86DDA00841A1216F04D1CE70D642680008E8@EXCH2.nws.oregonstate.edu> Message-ID: On Sat, Dec 18, 2010 at 11:06 AM, Peter Otten <__peter__ at web.de> wrote: > > I don't think /how/ you are trying it is stupid though I'm not so sure about > /what/ . > > I didn't get it to work with super() either, so here's Plan B till someone > is going to enlighten both of us: > By no means a solution, but I've spent some time with this in the python debugger and have some ideas. The problem is not super, as it does its job at calling __setattr__ of the base object class. __setattr__, however, resolves to the x object that is bound to self, which is SmallX.x, and not PositiveX.x, and this is what causes the recursion. So what you actually need is something like super(SmallX, self).x.__set__, but that unfortunately doesn't work either since super will use __getattr__ to resolve x, which will just result in the property getter being called. So in general, properties don't jive well with inheritance of their containing objects. I'm not sure if this behavior can be considered a bug. My idea would be to create separate descriptor classes and contain the constraints within them. The functionality has nothing to do with the containing class anyway, so it might actually simplify the design. Here's the basic idea: class UnsignedProp(object): def __init__(self, val): self.x = val def __get__(self, instance, owner): return self.x def __set__(self, instance, val): if val < 0: raise ValueError("Negatives not allowed") self.x = val class SmallProp(UnsignedProp): def __set__(self, instance, val): if val > 10: raise ValueError("value too large") super(SmallProp, self).__set__(instance, val) class X(object): x = SmallProp(1) y = UnsignedProp(1) a = X() a.x = 5 a.x = 25 # will raise ValueError From alan.gauld at btinternet.com Sat Dec 18 18:15:26 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 18 Dec 2010 17:15:26 -0000 Subject: [Tutor] why "ifconfig" is alway running? References: Message-ID: "lei yang" wrote > def runForAWhile(cmd, secs=10): > proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, > > stderr=subprocess.STDOUT, shell=True) > status = proc.poll() > start = datetime.datetime.now() > while (status is None and (datetime.datetime.now() - start) < timeout): #not timed out > print status > if 0 == status: > print("'%s' is program exited" %cmd) > else: > try: > os.kill(proc.pid, signal.SIGINT) > why it print many "None" in 10 second. which means "ifconfig" is > running in 10sec, why, what's wrong withi my script, You only check the status once *before* entering the while loop. You need another status check inside the loop. > I just want to let my programe running, if it's timeout(10sec), kill > it Its not clear from your mail if you have checked whether the process really is running - using the OS commands 'top' or 'ps' for example or whjether its just the None result you are concerned about. But try updating status first... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From LConrad at Go2France.com Sun Dec 19 01:45:18 2010 From: LConrad at Go2France.com (Len Conrad) Date: Sat, 18 Dec 2010 18:45:18 -0600 Subject: [Tutor] looking for python or django SQL data editor Message-ID: <20101219014537.SM07308@W500.Go2France.com> phpmyadmin and other such mysql db design/maintain tools do too much, too complicated. I'm looking for a python or django web app that allows non-tech users to add/delete/search records. thanks Len From smokefloat at gmail.com Sun Dec 19 02:33:08 2010 From: smokefloat at gmail.com (David Hutto) Date: Sat, 18 Dec 2010 20:33:08 -0500 Subject: [Tutor] looking for python or django SQL data editor In-Reply-To: <20101219014537.SM07308@W500.Go2France.com> References: <20101219014537.SM07308@W500.Go2France.com> Message-ID: On Sat, Dec 18, 2010 at 7:45 PM, Len Conrad wrote: > phpmyadmin and other such mysql db design/maintain tools do too much, too complicated. > > I'm looking for a python or django web app that allows non-tech users to add/delete/search records. You'll probably find the same here. A bunch of functions and utilities that are useless to the project at hand within frameworks. Although I'm sure it would be more helpful if you start rolling your own and asking from there. -- All comments shoukd be taken with a grain of salt, and thorough review of aforementioned material should be reviewed by the student...understood...self learner? From steve at pearwood.info Sun Dec 19 04:33:03 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 19 Dec 2010 14:33:03 +1100 Subject: [Tutor] The Template Pattern In-Reply-To: <4D0C5792.20103@free.fr> References: <4D064339.6050507@free.fr> <4D06A27F.3070803@pearwood.info> <4D06AA03.804@free.fr> <4D0C5792.20103@free.fr> Message-ID: <4D0D7CEF.8090709@pearwood.info> Karim wrote: >> class InputStrategy( object ): >> """This InputStrategy class is an abstract interface to various >> read strategy objects. >> """ >> def read(self, filePath): >> """Abstract method to load into database memory an input file. >> @arg: filePath - string - The input file path. >> @return: The created database object. >> """ >> raise NotImplementedError >> >> By the way, I use NotImplementedError in my abstract method of the >> abstract class. You are using TypeError exception. >> Is there a general rule for that? NotImplementedError is ok? For abstract methods that should be overridden by a subclass, I would use NotImplementedError, as you do. That is a clear convention for saying "this method must be overridden". But __init__ is a special case. The __init__ method isn't merely not implemented. It *is* implemented, and it does two jobs: it performs any common initialization for the class and all subclasses, and it also prevents the AbstractCLass from being instantiated. class AbstractClass: def __init__(self, *args): if type(self) is AbstractClass: raise TypeError # do common initialization here Trying to instantiate an AbstractClass is always an error: instance = AbstractClass(args) and the error is a *type* error -- you are trying to instantiate a type that can't be. Python already does that, with the singletons None, NotImplemented (different from NotImplementedError!), and Ellipsis: >>> type(None)() Traceback (most recent call last): File "", line 1, in TypeError: cannot create 'NoneType' instances TypeError means you have an error in your code. NotImplementedError means the code is incomplete. >> I knew from Java experience that template method pattern is only a >> kind of particular case of Stategy where you delegate >> parts of algorithm by abstract methods overriding. Indeed the base >> class implements some common features and let >> derived classes implement particular parts by polymorphism. >> Now with your example, I don't understand why he had problems to >> implement from C++ ? >> Or perhaps is he mixing it with C++ feature template ?! You'd have to ask him. Maybe he's just not a very good programmer, or was having a bad day. -- Steven From lea-parker at bigpond.com Sun Dec 19 04:42:08 2010 From: lea-parker at bigpond.com (Lea Parker) Date: Sun, 19 Dec 2010 14:42:08 +1100 Subject: [Tutor] Python using version 3.1 Message-ID: <000001cb9f2e$b6569270$2303b750$@com> Hello I am working through a purchased text book Python programming for beginners. Chapter two shows how to create a basic program using triple-quoted strings. I am having problems working out what I have done incorrectly. The game over block writing should stay on the same line but the bottom half of the word over comes up next to the top half of the word over. Hope that makes sense. My code is as follows: # Game Over - Version 2 # Demonstrates the use of quotes in strings print("Program 'Game Over' 2.0") print("Same", "message", "as before") print("Just", "a bit", "bigger") print("Here", end=" ") print("it is...") print( """ _____ ____ __ __ _____ / ____| / | / |/ | | ___| | | / / | | / /| / | | |__ | | _ / ____ | / / |__/| | | __| | |_| | / / | | / / | | | |___ \_____/ /_/ |_| /_/ |_| |_____| ____ _ _ _____ _____ / __ \ | | / / | ___| | _ \ || || | | / / | |__ | |_| | || || | | / / | __| | _ / ||__|| | |/ / | |___ | | \ \ \____/ |___/ |_____| |_| \_\ """ ) input("\n\nPress the enter key to exit.") From hugo.yoshi at gmail.com Sun Dec 19 05:13:29 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sun, 19 Dec 2010 05:13:29 +0100 Subject: [Tutor] Python using version 3.1 In-Reply-To: <000001cb9f2e$b6569270$2303b750$@com> References: <000001cb9f2e$b6569270$2303b750$@com> Message-ID: On Sun, Dec 19, 2010 at 4:42 AM, Lea Parker wrote: > Hello > > I am working through a purchased text book Python programming for beginners. > Chapter two shows how to create a basic program using triple-quoted strings. > > I am having problems working out what I have done incorrectly. The game over > block writing should stay on the same line but the bottom half of the word > over comes up next to the top half of the word over. Hope that makes sense. > It's the backslashes. A backslash at the end of a line means to disregard the newline. You need to "escape" the backslash with another backslash. See here: >>> print """this string \ ... won't have a newline, or a slash in it""" this string won't have a newline, or a slash in it >>> print """this string \\ ... will""" this string \ will The R ends in backslashes, so you need to double those up. Hugo From yanglei.fage at gmail.com Sun Dec 19 06:46:29 2010 From: yanglei.fage at gmail.com (lei yang) Date: Sun, 19 Dec 2010 13:46:29 +0800 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: References: Message-ID: #!/usr/bin/env python import datetime import subprocess import sys import os import signal from time import sleep def runForAWhile(cmd, secs=10): print("running %s" % cmd) timeout = datetime.timedelta(seconds=secs) print timeout proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) status = proc.poll() start = datetime.datetime.now() while (status is None and (datetime.datetime.now() - start) < timeout): #not timed out print proc.stdout.readline() #TODO timestamp? #print status #print datetime.datetime.now() - start if 0 == status: print("'%s' is program exited" %cmd) else: try: os.kill(proc.pid, signal.SIGINT) print "Process timeout: '%s'" % cmd except OSError: pass cmd="ping 128.224.165.20 -c 4" runForAWhile(cmd,30) I see that "status" always "!=0? why program is NOT exited Lei On Sun, Dec 19, 2010 at 1:15 AM, Alan Gauld wrote: > > "lei yang" wrote > >> def runForAWhile(cmd, secs=10): >> ? proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >> >> stderr=subprocess.STDOUT, shell=True) >> ? status = proc.poll() >> ? start = datetime.datetime.now() >> ? while (status is None and > > ? ? ? ? ? ? ? (datetime.datetime.now() - start) < ? timeout): #not timed out >> >> ? ? ? ? ? ? print status >> ? if 0 == status: >> ? ? print("'%s' is program exited" %cmd) >> ?else: >> ? ? try: >> ? ? ? ? os.kill(proc.pid, signal.SIGINT) > >> why it print many "None" in 10 second. which means "ifconfig" is >> running in 10sec, why, what's wrong withi my script, > > You only check the status once *before* entering the while loop. > You need another status check inside the loop. > >> I just want to let my programe running, if it's timeout(10sec), kill it > > Its not clear from your mail if you have checked whether the > process really is running - using the OS commands 'top' or 'ps' > for example or whjether its just the None result you are concerned > about. But try updating status first... > > HTH, > > > -- > Alan Gauld > 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 > From wescpy at gmail.com Sun Dec 19 06:56:52 2010 From: wescpy at gmail.com (wesley chun) Date: Sat, 18 Dec 2010 21:56:52 -0800 Subject: [Tutor] Python using version 3.1 In-Reply-To: References: <000001cb9f2e$b6569270$2303b750$@com> Message-ID: On Sat, Dec 18, 2010 at 8:13 PM, Hugo Arts wrote: > On Sun, Dec 19, 2010 at 4:42 AM, Lea Parker wrote: >> I am having problems working out what I have done incorrectly. The game over >> block writing should stay on the same line but the bottom half of the word >> over comes up next to the top half of the word over. Hope that makes sense. > > It's the backslashes. > > A backslash at the end of a line means to disregard the newline. You > need to "escape" the backslash with another backslash. See here: > >>>> print """this string \ > ... won't have a newline, or a slash in it""" > this string won't have a newline, or a slash in it >>>> print """this string \\ > ... will""" > this string \ > will > > The R ends in backslashes, so you need to double those up. correct me if i'm wrong, but speaking of R, i think putting an 'r' or 'R' in front of the opening triple quotes to turn it into a raw string will make it so that you *wouldn't* have to double everything up. by the way... welcome to Python!! :-) cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 ? ? http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com From alan.gauld at btinternet.com Sun Dec 19 09:57:43 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 19 Dec 2010 08:57:43 -0000 Subject: [Tutor] why "ifconfig" is alway running? References: Message-ID: "lei yang" wrote def runForAWhile(cmd, secs=10): print("running %s" % cmd) timeout = datetime.timedelta(seconds=secs) print timeout proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) status = proc.poll() You are still only checking status once outside the while loop. start = datetime.datetime.now() while (status is None and (datetime.datetime.now() - start) < timeout): #not timed out print proc.stdout.readline() #TODO timestamp? #print status #print datetime.datetime.now() - start > I see that "status" always "!=0? why program is NOT exited -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From yanglei.fage at gmail.com Sun Dec 19 12:09:52 2010 From: yanglei.fage at gmail.com (lei yang) Date: Sun, 19 Dec 2010 19:09:52 +0800 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: References: Message-ID: On Sun, Dec 19, 2010 at 4:57 PM, Alan Gauld wrote: > > "lei yang" wrote > > > def runForAWhile(cmd, secs=10): > ? print("running %s" % cmd) > ? timeout = datetime.timedelta(seconds=secs) > ? print timeout > ? proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, > stderr=subprocess.STDOUT, shell=True) > ? status = proc.poll() > > You are still only checking status once outside the while loop. > Thanks, I make a stupid mistaking Lei > ? start = datetime.datetime.now() > ? while (status is None and (datetime.datetime.now() - start) < > timeout): #not timed out > ? ? ? print proc.stdout.readline() #TODO timestamp? > ? ? ? #print status > ? ? ? #print datetime.datetime.now() - start > > >> I see that "status" always "!=0? ?why ?program ?is NOT exited > > > -- > Alan Gauld > 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 > From yanglei.fage at gmail.com Sun Dec 19 12:47:56 2010 From: yanglei.fage at gmail.com (lei yang) Date: Sun, 19 Dec 2010 19:47:56 +0800 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: References: Message-ID: #!/usr/bin/env python import datetime import subprocess import sys import os import signal from time import sleep def host_run(cmd, secs=10): print("running %s" % cmd) timeout = datetime.timedelta(seconds=secs) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) start = datetime.datetime.now() while ( proc.poll() is None and (datetime.datetime.now() - start) < timeout): #not timed out print proc.stdout.readline() #TODO timestamp? print "hello,i'm here" sleep(1) if 0 == proc.poll(): print("'%s' is program exited" %cmd) else: try: os.kill(proc.pid, signal.SIGINT) print "Process timeout: '%s'" % cmd except OSError: pass #cmd="ping 128.114.122.2" cmd="ssh root at 10.0.0.1" host_run(cmd,10) if cmd="ssh root at 10.0.0.1" it never print "hello i'm here" , how can i handle this issue, and I find my script cant process the "timeout" to kill it. if cmd="ping 128.114.122.2", no this issue. Lei On Sun, Dec 19, 2010 at 4:57 PM, Alan Gauld wrote: > > "lei yang" wrote > > > def runForAWhile(cmd, secs=10): > ? print("running %s" % cmd) > ? timeout = datetime.timedelta(seconds=secs) > ? print timeout > ? proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, > stderr=subprocess.STDOUT, shell=True) > ? status = proc.poll() > > You are still only checking status once outside the while loop. > > ? start = datetime.datetime.now() > ? while (status is None and (datetime.datetime.now() - start) < > timeout): #not timed out > ? ? ? print proc.stdout.readline() #TODO timestamp? > ? ? ? #print status > ? ? ? #print datetime.datetime.now() - start > > >> I see that "status" always "!=0? ?why ?program ?is NOT exited > > > -- > Alan Gauld > 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 > From evert.rol at gmail.com Sun Dec 19 12:56:14 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sun, 19 Dec 2010 12:56:14 +0100 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: References: Message-ID: <3508A5D1-5199-4B68-A60A-9BAB603D0A1D@gmail.com> > #!/usr/bin/env python > import datetime > import subprocess > import sys > import os > import signal > from time import sleep > > def host_run(cmd, secs=10): > print("running %s" % cmd) > timeout = datetime.timedelta(seconds=secs) > proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, > stderr=subprocess.STDOUT, shell=True) > start = datetime.datetime.now() > while ( proc.poll() is None and (datetime.datetime.now() - start) > < timeout): #not timed out > print proc.stdout.readline() #TODO timestamp? > print "hello,i'm here" > sleep(1) > if 0 == proc.poll(): > print("'%s' is program exited" %cmd) > else: > try: > os.kill(proc.pid, signal.SIGINT) > print "Process timeout: '%s'" % cmd > except OSError: > pass > #cmd="ping 128.114.122.2" > cmd="ssh root at 10.0.0.1" > host_run(cmd,10) > > if cmd="ssh root at 10.0.0.1" > it never print "hello i'm here" , how can i handle this issue, and I > find my script cant process the "timeout" to kill it. > if cmd="ping 128.114.122.2", no this issue. Have you tried running these two commands from the command line? Spot the difference: ssh is waiting for input (a password. And if passwordless, it may still not produce output to stdout), while ping just continues to produce output. Then look at the line print proc.stdout.readline() Will that ever read a full line for the ssh command? Probably not, while for ping it will. So my guess is, your script gets stuck at that line. But if you ctrl-C to stop the script, you should see where your program gets stuck. You didn't say where it got stuck, that would have helped. Good luck, Evert > On Sun, Dec 19, 2010 at 4:57 PM, Alan Gauld wrote: >> >> "lei yang" wrote >> >> >> def runForAWhile(cmd, secs=10): >> print("running %s" % cmd) >> timeout = datetime.timedelta(seconds=secs) >> print timeout >> proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >> stderr=subprocess.STDOUT, shell=True) >> status = proc.poll() >> >> You are still only checking status once outside the while loop. >> >> start = datetime.datetime.now() >> while (status is None and (datetime.datetime.now() - start) < >> timeout): #not timed out >> print proc.stdout.readline() #TODO timestamp? >> #print status >> #print datetime.datetime.now() - start >> >> >>> I see that "status" always "!=0? why program is NOT exited >> >> >> -- >> Alan Gauld >> 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 >> > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From yanglei.fage at gmail.com Sun Dec 19 13:02:51 2010 From: yanglei.fage at gmail.com (lei yang) Date: Sun, 19 Dec 2010 20:02:51 +0800 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: <3508A5D1-5199-4B68-A60A-9BAB603D0A1D@gmail.com> References: <3508A5D1-5199-4B68-A60A-9BAB603D0A1D@gmail.com> Message-ID: On Sun, Dec 19, 2010 at 7:56 PM, Evert Rol wrote: >> #!/usr/bin/env python >> import datetime >> import subprocess >> import sys >> import os >> import signal >> from time import sleep >> >> def host_run(cmd, secs=10): >> ? ?print("running %s" % cmd) >> ? ?timeout = datetime.timedelta(seconds=secs) >> ? ?proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >> stderr=subprocess.STDOUT, shell=True) >> ? ?start = datetime.datetime.now() >> ? ?while ( proc.poll() is None and (datetime.datetime.now() - start) >> < timeout): #not timed out >> ? ? ? ?print proc.stdout.readline() #TODO timestamp? >> ? ? ? ?print "hello,i'm here" >> ? ? ? ?sleep(1) >> ? ?if 0 == proc.poll(): >> ? ? ? ?print("'%s' is program exited" %cmd) >> ? ?else: >> ? ? ? ?try: >> ? ? ? ? ? ?os.kill(proc.pid, signal.SIGINT) >> ? ? ? ? ? print "Process timeout: '%s'" % cmd >> ? ? ? ?except OSError: >> ? ? ? ? ? ?pass >> #cmd="ping 128.114.122.2" >> cmd="ssh root at 10.0.0.1" >> host_run(cmd,10) >> >> if cmd="ssh root at 10.0.0.1" >> it never print "hello i'm here" , how can i handle this issue, and I >> find my script cant process the "timeout" to kill it. >> if cmd="ping 128.114.122.2", no this issue. > > Have you tried running these two commands from the command line? > Spot the difference: ssh is waiting for input (a password. And if passwordless, it may still not produce output to stdout), while ping just continues to produce output. > Then look at the line > ? ?print proc.stdout.readline() > Will that ever read a full line for the ssh command? Probably not, while for ping it will. > it's a test, actually, I can't access 10.0.0.1, i just want to kill it if *no responding" in 10 second, but it doesn't kill it. I don't know why. my script is to run a cmd, if more than 10 sec, kill it can you show me where is wrong, thanks in advance Lei > So my guess is, your script gets stuck at that line. > But if you ctrl-C to stop the script, you should see where your program gets stuck. You didn't say where it got stuck, that would have helped. > > Good luck, > > ?Evert > > >> On Sun, Dec 19, 2010 at 4:57 PM, Alan Gauld wrote: >>> >>> "lei yang" wrote >>> >>> >>> def runForAWhile(cmd, secs=10): >>> ? print("running %s" % cmd) >>> ? timeout = datetime.timedelta(seconds=secs) >>> ? print timeout >>> ? proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >>> stderr=subprocess.STDOUT, shell=True) >>> ? status = proc.poll() >>> >>> You are still only checking status once outside the while loop. >>> >>> ? start = datetime.datetime.now() >>> ? while (status is None and (datetime.datetime.now() - start) < >>> timeout): #not timed out >>> ? ? ? print proc.stdout.readline() #TODO timestamp? >>> ? ? ? #print status >>> ? ? ? #print datetime.datetime.now() - start >>> >>> >>>> I see that "status" always "!=0? ?why ?program ?is NOT exited >>> >>> >>> -- >>> Alan Gauld >>> 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 >>> >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > From evert.rol at gmail.com Sun Dec 19 13:09:42 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sun, 19 Dec 2010 13:09:42 +0100 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: References: <3508A5D1-5199-4B68-A60A-9BAB603D0A1D@gmail.com> Message-ID: <001BBD87-2BE1-4CF9-887E-9E00576C3EFB@gmail.com> >>> #!/usr/bin/env python >>> import datetime >>> import subprocess >>> import sys >>> import os >>> import signal >>> from time import sleep >>> >>> def host_run(cmd, secs=10): >>> print("running %s" % cmd) >>> timeout = datetime.timedelta(seconds=secs) >>> proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >>> stderr=subprocess.STDOUT, shell=True) >>> start = datetime.datetime.now() >>> while ( proc.poll() is None and (datetime.datetime.now() - start) >>> < timeout): #not timed out >>> print proc.stdout.readline() #TODO timestamp? >>> print "hello,i'm here" >>> sleep(1) >>> if 0 == proc.poll(): >>> print("'%s' is program exited" %cmd) >>> else: >>> try: >>> os.kill(proc.pid, signal.SIGINT) >>> print "Process timeout: '%s'" % cmd >>> except OSError: >>> pass >>> #cmd="ping 128.114.122.2" >>> cmd="ssh root at 10.0.0.1" >>> host_run(cmd,10) >>> >>> if cmd="ssh root at 10.0.0.1" >>> it never print "hello i'm here" , how can i handle this issue, and I >>> find my script cant process the "timeout" to kill it. >>> if cmd="ping 128.114.122.2", no this issue. >> >> Have you tried running these two commands from the command line? >> Spot the difference: ssh is waiting for input (a password. And if passwordless, it may still not produce output to stdout), while ping just continues to produce output. >> Then look at the line >> print proc.stdout.readline() >> Will that ever read a full line for the ssh command? Probably not, while for ping it will. >> > > it's a test, actually, I can't access 10.0.0.1, i just want to kill it > if *no responding" in 10 second, but it doesn't kill it. I don't know > why. > my script is to run a cmd, if more than 10 sec, kill it > can you show me where is wrong, thanks in advance But you never said where the script gets stuck. That would be a very important first step for debugging. And if I run this script, I get the following: " $> python bla.py running ssh root at 10.0.0.1 ssh: connect to host 10.0.0.1 port 22: Connection refused hello,i'm here " So seems to work for me. thus, what is the result of $> ssh root at 10.0.0.1 for you? Evert >> So my guess is, your script gets stuck at that line. >> But if you ctrl-C to stop the script, you should see where your program gets stuck. You didn't say where it got stuck, that would have helped. >> >> Good luck, >> >> Evert >> >> >>> On Sun, Dec 19, 2010 at 4:57 PM, Alan Gauld wrote: >>>> >>>> "lei yang" wrote >>>> >>>> >>>> def runForAWhile(cmd, secs=10): >>>> print("running %s" % cmd) >>>> timeout = datetime.timedelta(seconds=secs) >>>> print timeout >>>> proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >>>> stderr=subprocess.STDOUT, shell=True) >>>> status = proc.poll() >>>> >>>> You are still only checking status once outside the while loop. >>>> >>>> start = datetime.datetime.now() >>>> while (status is None and (datetime.datetime.now() - start) < >>>> timeout): #not timed out >>>> print proc.stdout.readline() #TODO timestamp? >>>> #print status >>>> #print datetime.datetime.now() - start >>>> >>>> >>>>> I see that "status" always "!=0? why program is NOT exited >>>> >>>> >>>> -- >>>> Alan Gauld >>>> 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 >>>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> http://mail.python.org/mailman/listinfo/tutor >> >> From yanglei.fage at gmail.com Sun Dec 19 13:22:04 2010 From: yanglei.fage at gmail.com (lei yang) Date: Sun, 19 Dec 2010 20:22:04 +0800 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: <001BBD87-2BE1-4CF9-887E-9E00576C3EFB@gmail.com> References: <3508A5D1-5199-4B68-A60A-9BAB603D0A1D@gmail.com> <001BBD87-2BE1-4CF9-887E-9E00576C3EFB@gmail.com> Message-ID: On Sun, Dec 19, 2010 at 8:09 PM, Evert Rol wrote: >>>> #!/usr/bin/env python >>>> import datetime >>>> import subprocess >>>> import sys >>>> import os >>>> import signal >>>> from time import sleep >>>> >>>> def host_run(cmd, secs=10): >>>> ? ?print("running %s" % cmd) >>>> ? ?timeout = datetime.timedelta(seconds=secs) >>>> ? ?proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >>>> stderr=subprocess.STDOUT, shell=True) >>>> ? ?start = datetime.datetime.now() >>>> ? ?while ( proc.poll() is None and (datetime.datetime.now() - start) >>>> < timeout): #not timed out >>>> ? ? ? ?print proc.stdout.readline() #TODO timestamp? >>>> ? ? ? ?print "hello,i'm here" >>>> ? ? ? ?sleep(1) >>>> ? ?if 0 == proc.poll(): >>>> ? ? ? ?print("'%s' is program exited" %cmd) >>>> ? ?else: >>>> ? ? ? ?try: >>>> ? ? ? ? ? ?os.kill(proc.pid, signal.SIGINT) >>>> ? ? ? ? ? print "Process timeout: '%s'" % cmd >>>> ? ? ? ?except OSError: >>>> ? ? ? ? ? ?pass >>>> #cmd="ping 128.114.122.2" >>>> cmd="ssh root at 10.0.0.1" >>>> host_run(cmd,10) >>>> >>>> if cmd="ssh root at 10.0.0.1" >>>> it never print "hello i'm here" , how can i handle this issue, and I >>>> find my script cant process the "timeout" to kill it. >>>> if cmd="ping 128.114.122.2", no this issue. >>> >>> Have you tried running these two commands from the command line? >>> Spot the difference: ssh is waiting for input (a password. And if passwordless, it may still not produce output to stdout), while ping just continues to produce output. >>> Then look at the line >>> ? ?print proc.stdout.readline() >>> Will that ever read a full line for the ssh command? Probably not, while for ping it will. >>> >> >> it's a test, actually, I can't access 10.0.0.1, i just want to kill it >> if *no responding" in 10 second, but it doesn't kill it. I don't know >> why. >> my script is to run a cmd, if more than 10 sec, kill it >> can you show me where is wrong, thanks in advance > > But you never said where the script gets stuck. That would be a very important first step for debugging. > > And if I run this script, I get the following: > " > $> python bla.py > running ssh root at 10.0.0.1 > ssh: connect to host 10.0.0.1 port 22: Connection refused > > hello,i'm here > " > So seems to work for me. > thus, what is the result of > $> ssh root at 10.0.0.1 > for you? > [lyang0 at pek-lpgbuild13 py]$ ssh root at 10.0.0.1 ssh: connect to host 10.0.0.1 port 22: Connection timed out but it will wait for above 30 seconds then print "ssh: connect to host 10.0.0.1 port 22: Connection timed out", but my script is designed to kill it if cmd running more than 10 secs. Lei > ?Evert > > >>> So my guess is, your script gets stuck at that line. >>> But if you ctrl-C to stop the script, you should see where your program gets stuck. You didn't say where it got stuck, that would have helped. >>> >>> Good luck, >>> >>> ?Evert >>> >>> >>>> On Sun, Dec 19, 2010 at 4:57 PM, Alan Gauld wrote: >>>>> >>>>> "lei yang" wrote >>>>> >>>>> >>>>> def runForAWhile(cmd, secs=10): >>>>> ? print("running %s" % cmd) >>>>> ? timeout = datetime.timedelta(seconds=secs) >>>>> ? print timeout >>>>> ? proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >>>>> stderr=subprocess.STDOUT, shell=True) >>>>> ? status = proc.poll() >>>>> >>>>> You are still only checking status once outside the while loop. >>>>> >>>>> ? start = datetime.datetime.now() >>>>> ? while (status is None and (datetime.datetime.now() - start) < >>>>> timeout): #not timed out >>>>> ? ? ? print proc.stdout.readline() #TODO timestamp? >>>>> ? ? ? #print status >>>>> ? ? ? #print datetime.datetime.now() - start >>>>> >>>>> >>>>>> I see that "status" always "!=0? ?why ?program ?is NOT exited >>>>> >>>>> >>>>> -- >>>>> Alan Gauld >>>>> 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 >>>>> >>>> _______________________________________________ >>>> Tutor maillist ?- ?Tutor at python.org >>>> To unsubscribe or change subscription options: >>>> http://mail.python.org/mailman/listinfo/tutor >>> >>> > > From evert.rol at gmail.com Sun Dec 19 13:29:39 2010 From: evert.rol at gmail.com (Evert Rol) Date: Sun, 19 Dec 2010 13:29:39 +0100 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: References: <3508A5D1-5199-4B68-A60A-9BAB603D0A1D@gmail.com> <001BBD87-2BE1-4CF9-887E-9E00576C3EFB@gmail.com> Message-ID: >>>>> #!/usr/bin/env python >>>>> import datetime >>>>> import subprocess >>>>> import sys >>>>> import os >>>>> import signal >>>>> from time import sleep >>>>> >>>>> def host_run(cmd, secs=10): >>>>> print("running %s" % cmd) >>>>> timeout = datetime.timedelta(seconds=secs) >>>>> proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >>>>> stderr=subprocess.STDOUT, shell=True) >>>>> start = datetime.datetime.now() >>>>> while ( proc.poll() is None and (datetime.datetime.now() - start) >>>>> < timeout): #not timed out >>>>> print proc.stdout.readline() #TODO timestamp? >>>>> print "hello,i'm here" >>>>> sleep(1) >>>>> if 0 == proc.poll(): >>>>> print("'%s' is program exited" %cmd) >>>>> else: >>>>> try: >>>>> os.kill(proc.pid, signal.SIGINT) >>>>> print "Process timeout: '%s'" % cmd >>>>> except OSError: >>>>> pass >>>>> #cmd="ping 128.114.122.2" >>>>> cmd="ssh root at 10.0.0.1" >>>>> host_run(cmd,10) >>>>> >>>>> if cmd="ssh root at 10.0.0.1" >>>>> it never print "hello i'm here" , how can i handle this issue, and I >>>>> find my script cant process the "timeout" to kill it. >>>>> if cmd="ping 128.114.122.2", no this issue. >>>> >>>> Have you tried running these two commands from the command line? >>>> Spot the difference: ssh is waiting for input (a password. And if passwordless, it may still not produce output to stdout), while ping just continues to produce output. >>>> Then look at the line >>>> print proc.stdout.readline() >>>> Will that ever read a full line for the ssh command? Probably not, while for ping it will. >>>> >>> >>> it's a test, actually, I can't access 10.0.0.1, i just want to kill it >>> if *no responding" in 10 second, but it doesn't kill it. I don't know >>> why. >>> my script is to run a cmd, if more than 10 sec, kill it >>> can you show me where is wrong, thanks in advance >> >> But you never said where the script gets stuck. That would be a very important first step for debugging. >> >> And if I run this script, I get the following: >> " >> $> python bla.py >> running ssh root at 10.0.0.1 >> ssh: connect to host 10.0.0.1 port 22: Connection refused >> >> hello,i'm here >> " >> So seems to work for me. >> thus, what is the result of >> $> ssh root at 10.0.0.1 >> for you? >> > > [lyang0 at pek-lpgbuild13 py]$ ssh root at 10.0.0.1 > ssh: connect to host 10.0.0.1 port 22: Connection timed out > > but it will wait for above 30 seconds then print "ssh: connect to host > 10.0.0.1 port 22: Connection timed out", but my script is designed to > kill it if cmd running more than 10 secs. So it doesn't produce output for at least 30 secs. Then my guess that it gets stuck at the readline() line still stands. Unfortunately, you still haven't told us where the script gets stuck. Also, have you tried to let the script run for, say, 30 seconds at least? Ie, the time it takes for the ssh command to time out by itself. Evert > > > Lei > >> Evert >> >> >>>> So my guess is, your script gets stuck at that line. >>>> But if you ctrl-C to stop the script, you should see where your program gets stuck. You didn't say where it got stuck, that would have helped. >>>> >>>> Good luck, >>>> >>>> Evert >>>> >>>> >>>>> On Sun, Dec 19, 2010 at 4:57 PM, Alan Gauld wrote: >>>>>> >>>>>> "lei yang" wrote >>>>>> >>>>>> >>>>>> def runForAWhile(cmd, secs=10): >>>>>> print("running %s" % cmd) >>>>>> timeout = datetime.timedelta(seconds=secs) >>>>>> print timeout >>>>>> proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >>>>>> stderr=subprocess.STDOUT, shell=True) >>>>>> status = proc.poll() >>>>>> >>>>>> You are still only checking status once outside the while loop. >>>>>> >>>>>> start = datetime.datetime.now() >>>>>> while (status is None and (datetime.datetime.now() - start) < >>>>>> timeout): #not timed out >>>>>> print proc.stdout.readline() #TODO timestamp? >>>>>> #print status >>>>>> #print datetime.datetime.now() - start >>>>>> >>>>>> >>>>>>> I see that "status" always "!=0? why program is NOT exited >>>>>> >>>>>> >>>>>> -- >>>>>> Alan Gauld >>>>>> 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 >>>>>> >>>>> _______________________________________________ >>>>> Tutor maillist - Tutor at python.org >>>>> To unsubscribe or change subscription options: >>>>> http://mail.python.org/mailman/listinfo/tutor >>>> >>>> >> >> From yanglei.fage at gmail.com Sun Dec 19 13:43:42 2010 From: yanglei.fage at gmail.com (lei yang) Date: Sun, 19 Dec 2010 20:43:42 +0800 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: References: <3508A5D1-5199-4B68-A60A-9BAB603D0A1D@gmail.com> <001BBD87-2BE1-4CF9-887E-9E00576C3EFB@gmail.com> Message-ID: On Sun, Dec 19, 2010 at 8:29 PM, Evert Rol wrote: >>>>>> #!/usr/bin/env python >>>>>> import datetime >>>>>> import subprocess >>>>>> import sys >>>>>> import os >>>>>> import signal >>>>>> from time import sleep >>>>>> >>>>>> def host_run(cmd, secs=10): >>>>>> ? ?print("running %s" % cmd) >>>>>> ? ?timeout = datetime.timedelta(seconds=secs) >>>>>> ? ?proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >>>>>> stderr=subprocess.STDOUT, shell=True) >>>>>> ? ?start = datetime.datetime.now() >>>>>> ? ?while ( proc.poll() is None and (datetime.datetime.now() - start) >>>>>> < timeout): #not timed out >>>>>> ? ? ? ?print proc.stdout.readline() #TODO timestamp? >>>>>> ? ? ? ?print "hello,i'm here" >>>>>> ? ? ? ?sleep(1) >>>>>> ? ?if 0 == proc.poll(): >>>>>> ? ? ? ?print("'%s' is program exited" %cmd) >>>>>> ? ?else: >>>>>> ? ? ? ?try: >>>>>> ? ? ? ? ? ?os.kill(proc.pid, signal.SIGINT) >>>>>> ? ? ? ? ? print "Process timeout: '%s'" % cmd >>>>>> ? ? ? ?except OSError: >>>>>> ? ? ? ? ? ?pass >>>>>> #cmd="ping 128.114.122.2" >>>>>> cmd="ssh root at 10.0.0.1" >>>>>> host_run(cmd,10) >>>>>> >>>>>> if cmd="ssh root at 10.0.0.1" >>>>>> it never print "hello i'm here" , how can i handle this issue, and I >>>>>> find my script cant process the "timeout" to kill it. >>>>>> if cmd="ping 128.114.122.2", no this issue. >>>>> >>>>> Have you tried running these two commands from the command line? >>>>> Spot the difference: ssh is waiting for input (a password. And if passwordless, it may still not produce output to stdout), while ping just continues to produce output. >>>>> Then look at the line >>>>> ? ?print proc.stdout.readline() >>>>> Will that ever read a full line for the ssh command? Probably not, while for ping it will. >>>>> >>>> >>>> it's a test, actually, I can't access 10.0.0.1, i just want to kill it >>>> if *no responding" in 10 second, but it doesn't kill it. I don't know >>>> why. >>>> my script is to run a cmd, if more than 10 sec, kill it >>>> can you show me where is wrong, thanks in advance >>> >>> But you never said where the script gets stuck. That would be a very important first step for debugging. >>> >>> And if I run this script, I get the following: >>> " >>> $> python bla.py >>> running ssh root at 10.0.0.1 >>> ssh: connect to host 10.0.0.1 port 22: Connection refused >>> >>> hello,i'm here >>> " >>> So seems to work for me. >>> thus, what is the result of >>> $> ssh root at 10.0.0.1 >>> for you? >>> >> >> [lyang0 at pek-lpgbuild13 py]$ ssh root at 10.0.0.1 >> ssh: connect to host 10.0.0.1 port 22: Connection timed out >> >> but it will wait for above 30 seconds then print "ssh: connect to host >> 10.0.0.1 port 22: Connection timed out", but my script is designed to >> kill it if cmd running more than 10 secs. > > So it doesn't produce output for at least 30 secs. Then my guess that it gets stuck at the readline() line still stands. Right, it gets stuck at the readline(), is there a function not get stuck to instead of readline(). Lei > Unfortunately, you still haven't told us where the script gets stuck. > Also, have you tried to let the script run for, say, 30 seconds at least? Ie, the time it takes for the ssh command to time out by itself. > > ?Evert > > >> >> >> Lei >> >>> ?Evert >>> >>> >>>>> So my guess is, your script gets stuck at that line. >>>>> But if you ctrl-C to stop the script, you should see where your program gets stuck. You didn't say where it got stuck, that would have helped. >>>>> >>>>> Good luck, >>>>> >>>>> ?Evert >>>>> >>>>> >>>>>> On Sun, Dec 19, 2010 at 4:57 PM, Alan Gauld wrote: >>>>>>> >>>>>>> "lei yang" wrote >>>>>>> >>>>>>> >>>>>>> def runForAWhile(cmd, secs=10): >>>>>>> ? print("running %s" % cmd) >>>>>>> ? timeout = datetime.timedelta(seconds=secs) >>>>>>> ? print timeout >>>>>>> ? proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >>>>>>> stderr=subprocess.STDOUT, shell=True) >>>>>>> ? status = proc.poll() >>>>>>> >>>>>>> You are still only checking status once outside the while loop. >>>>>>> >>>>>>> ? start = datetime.datetime.now() >>>>>>> ? while (status is None and (datetime.datetime.now() - start) < >>>>>>> timeout): #not timed out >>>>>>> ? ? ? print proc.stdout.readline() #TODO timestamp? >>>>>>> ? ? ? #print status >>>>>>> ? ? ? #print datetime.datetime.now() - start >>>>>>> >>>>>>> >>>>>>>> I see that "status" always "!=0? ?why ?program ?is NOT exited >>>>>>> >>>>>>> >>>>>>> -- >>>>>>> Alan Gauld >>>>>>> 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 >>>>>>> >>>>>> _______________________________________________ >>>>>> Tutor maillist ?- ?Tutor at python.org >>>>>> To unsubscribe or change subscription options: >>>>>> http://mail.python.org/mailman/listinfo/tutor >>>>> >>>>> >>> >>> > > From sander.sweers at gmail.com Sun Dec 19 14:05:51 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 19 Dec 2010 14:05:51 +0100 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: References: <3508A5D1-5199-4B68-A60A-9BAB603D0A1D@gmail.com> <001BBD87-2BE1-4CF9-887E-9E00576C3EFB@gmail.com> Message-ID: On 19 December 2010 13:43, lei yang wrote: > Right, it gets stuck at the readline(), is there a function not get > stuck to instead of readline(). readline() will keep reading stdout until it received a newline character. So if there is nothing to read it will wait forever. The solution is to wait with reading until there is actually something to read. And it is recommended [1] to use proc.communicate() instead of reading directly from stdout. Also you should use proc.terminate() or proc.kill() to stop the command. Below is a modified version of your function to show what I mean. [1] http://docs.python.org/library/subprocess.html#subprocess.Popen.stdin Greets Sander def host_run(cmd, secs=10): print("running %s" % cmd) timeout = datetime.timedelta(seconds=secs) proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) start = datetime.datetime.now() while ( proc.poll() is None and (datetime.datetime.now() - start) < timeout): #not timed out print "hello,i'm here" sleep(1) if 0 == proc.poll(): stdout, stderr = proc.communicate() #get stdout and stderr print("'%s' is program exited" %cmd) print stdout print stderr else: proc.terminate() #Can use proc.kill() as well stdout, stderr = proc.communicate() #get stdout and stderr print 'Timeout: Process terminated' print stdout print stderr From yanglei.fage at gmail.com Sun Dec 19 14:54:30 2010 From: yanglei.fage at gmail.com (lei yang) Date: Sun, 19 Dec 2010 21:54:30 +0800 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: References: <3508A5D1-5199-4B68-A60A-9BAB603D0A1D@gmail.com> <001BBD87-2BE1-4CF9-887E-9E00576C3EFB@gmail.com> Message-ID: On Sun, Dec 19, 2010 at 9:05 PM, Sander Sweers wrote: > On 19 December 2010 13:43, lei yang wrote: >> Right, it gets stuck at the readline(), is there a function not get >> stuck to instead of readline(). > > readline() will keep reading stdout until it received a newline > character. So if there is nothing to read it will wait forever. The > solution is to wait with reading until there is actually something to > read. And it is recommended [1] to use proc.communicate() instead of > reading directly from stdout. > > Also you should use proc.terminate() or proc.kill() to stop the command. > > Below is a modified version of your function to show what I mean. > > [1] http://docs.python.org/library/subprocess.html#subprocess.Popen.stdin > Thanks very much, that's really what I want Lei > Greets > Sander > > def host_run(cmd, secs=10): > ? ?print("running %s" % cmd) > ? ?timeout = datetime.timedelta(seconds=secs) > ? ?proc = subprocess.Popen( > ? ? ? ?cmd, > ? ? ? ?stdout=subprocess.PIPE, > ? ? ? ?stderr=subprocess.STDOUT, > ? ? ? ?shell=True) > ? ?start = datetime.datetime.now() > ? ?while ( proc.poll() is None and (datetime.datetime.now() - start) > < timeout): #not timed out > ? ? ? ?print "hello,i'm here" > ? ? ? ?sleep(1) > ? ?if 0 == proc.poll(): > ? ? ? ?stdout, stderr = proc.communicate() #get stdout and stderr > ? ? ? ?print("'%s' is program exited" %cmd) > ? ? ? ?print stdout > ? ? ? ?print stderr > ? ?else: > ? ? ? ?proc.terminate() #Can use proc.kill() as well > ? ? ? ?stdout, stderr = proc.communicate() #get stdout and stderr > ? ? ? ?print 'Timeout: Process terminated' > ? ? ? ?print stdout > ? ? ? ?print stderr > From karim.liateni at free.fr Sun Dec 19 20:27:28 2010 From: karim.liateni at free.fr (Karim) Date: Sun, 19 Dec 2010 20:27:28 +0100 Subject: [Tutor] The Template Pattern In-Reply-To: <4D0D7CEF.8090709@pearwood.info> References: <4D064339.6050507@free.fr> <4D06A27F.3070803@pearwood.info> <4D06AA03.804@free.fr> <4D0C5792.20103@free.fr> <4D0D7CEF.8090709@pearwood.info> Message-ID: <4D0E5CA0.7050008@free.fr> Thanks a lot to point this out! Karim On 12/19/2010 04:33 AM, Steven D'Aprano wrote: > Karim wrote: > >>> class InputStrategy( object ): >>> """This InputStrategy class is an abstract interface to various >>> read strategy objects. >>> """ >>> def read(self, filePath): >>> """Abstract method to load into database memory an input file. >>> @arg: filePath - string - The input file path. >>> @return: The created database object. >>> """ >>> raise NotImplementedError >>> >>> By the way, I use NotImplementedError in my abstract method of the >>> abstract class. You are using TypeError exception. >>> Is there a general rule for that? NotImplementedError is ok? > > > For abstract methods that should be overridden by a subclass, I would > use NotImplementedError, as you do. That is a clear convention for > saying "this method must be overridden". > > But __init__ is a special case. The __init__ method isn't merely not > implemented. It *is* implemented, and it does two jobs: it performs > any common initialization for the class and all subclasses, and it > also prevents the AbstractCLass from being instantiated. > > class AbstractClass: > def __init__(self, *args): > if type(self) is AbstractClass: > raise TypeError > # do common initialization here > > Trying to instantiate an AbstractClass is always an error: > > instance = AbstractClass(args) > > and the error is a *type* error -- you are trying to instantiate a > type that can't be. Python already does that, with the singletons > None, NotImplemented (different from NotImplementedError!), and Ellipsis: > > >>> type(None)() > Traceback (most recent call last): > File "", line 1, in > TypeError: cannot create 'NoneType' instances > > > TypeError means you have an error in your code. NotImplementedError > means the code is incomplete. > > > >>> I knew from Java experience that template method pattern is only a >>> kind of particular case of Stategy where you delegate >>> parts of algorithm by abstract methods overriding. Indeed the base >>> class implements some common features and let >>> derived classes implement particular parts by polymorphism. >>> Now with your example, I don't understand why he had problems to >>> implement from C++ ? >>> Or perhaps is he mixing it with C++ feature template ?! > > You'd have to ask him. Maybe he's just not a very good programmer, or > was having a bad day. > > > > From lang at tharin.com Sun Dec 19 20:59:19 2010 From: lang at tharin.com (Lang Hurst) Date: Sun, 19 Dec 2010 11:59:19 -0800 Subject: [Tutor] os.startfile Message-ID: <4D0E6417.6080502@tharin.com> I have the following in my program: try: os.startfile('current_credit.txt') except: os.system('/usr/bin/xdg-open current_credit.txt') Basically, open a file in notepad if I'm on windows, vim if on my home linux computer. It works fine in linux and in Windows through virtualbox. The problem is when I take the program to work, it doesn't open the file. The computers at work are pretty locked down, so I'm thinking it has something to do with the os.startfile command. Are there any alternative commands I could try? -- There are no stupid questions, just stupid people. From jacksmoo111 at gmail.com Sun Dec 19 21:54:56 2010 From: jacksmoo111 at gmail.com (jtl999) Date: Sun, 19 Dec 2010 12:54:56 -0800 Subject: [Tutor] Problem with print Message-ID: <1292792096.27430.0.camel@nerdism> File "GettingStarted.py", line 91 print ("Lesson Two") ^ SyntaxError: invalid syntax Python 2.6.5 -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 836 bytes Desc: This is a digitally signed message part URL: From mehgcap at gmail.com Sun Dec 19 22:05:42 2010 From: mehgcap at gmail.com (Alex Hall) Date: Sun, 19 Dec 2010 16:05:42 -0500 Subject: [Tutor] Problem with print In-Reply-To: <1292792096.27430.0.camel@nerdism> References: <1292792096.27430.0.camel@nerdism> Message-ID: Sorry to top-post (gMail mobile). This looks like you missed a quote, colon, or something on a line elsewhere in the file (likely above it). Find that and this should be fixed. On 12/19/10, jtl999 wrote: > File "GettingStarted.py", line 91 > print ("Lesson Two") > ^ > SyntaxError: invalid syntax > > Python 2.6.5 > -- Have a great day, Alex (msg sent from GMail website) mehgcap at gmail.com; http://www.facebook.com/mehgcap From steve at pearwood.info Sun Dec 19 22:16:34 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 20 Dec 2010 08:16:34 +1100 Subject: [Tutor] os.startfile In-Reply-To: <4D0E6417.6080502@tharin.com> References: <4D0E6417.6080502@tharin.com> Message-ID: <4D0E7632.1050505@pearwood.info> Lang Hurst wrote: > I have the following in my program: > > > try: > os.startfile('current_credit.txt') > except: > os.system('/usr/bin/xdg-open current_credit.txt') > > > Basically, open a file in notepad if I'm on windows, vim if on my home > linux computer. It works fine in linux and in Windows through > virtualbox. The problem is when I take the program to work, it doesn't > open the file. The computers at work are pretty locked down, so I'm > thinking it has something to do with the os.startfile command. Are > there any alternative commands I could try? You don't give us much information to go on, such as the version of Python you use, or the operating system on your work desktops, or the error that you see when you try this, or even if you can open the file by double-clicking it, but that's okay, because I love guessing games! I guess that the problem is that your work desktops are, in fact, Apple Macintoshes running OS-X. Am I close? Other than that, you shouldn't just blindly ignore the exception raised by startfile. Not all exceptions mean "You're not running Windows and there is no startfile command", and you shouldn't catch bare excepts. You would be better off doing this: try: os.startfile('current_credit.txt') except AttributeError: # No startfile command, so we're not on Windows. # Try a Linux command instead. # (Tested on Fedora, may not work on all distros.) os.system('/usr/bin/xdg-open current_credit.txt') That way, when you get a different error, like "file not found" or "permission denied", you will see what it is, and perhaps get a hint as to what the problem is. Python doesn't have super powers. If you can't open a file because the desktop has been locked down, then Python won't be able to magically open it. It has no more permissions to do things than you do. There's no magic command "open files even if I'm not allowed to open them". -- Steven From smokefloat at gmail.com Sun Dec 19 22:17:16 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 19 Dec 2010 16:17:16 -0500 Subject: [Tutor] Problem with print In-Reply-To: References: <1292792096.27430.0.camel@nerdism> Message-ID: On Sun, Dec 19, 2010 at 4:05 PM, Alex Hall wrote: > Sorry to top-post (gMail mobile). > This looks like you missed a quote, colon, or something on a line > elsewhere in the file (likely above it). Find that and this should be > fixed. > > On 12/19/10, jtl999 wrote: >> ?File "GettingStarted.py", line 91 >> ? ? print ("Lesson Two") It looks like you're following a tutorial for python 3 where print('stuff here') is used, and in 2 you use print 'whatever', no parentheses. >> ? ? ? ? ^ >> SyntaxError: invalid syntax >> >> Python 2.6.5 >> > > > -- > Have a great day, > Alex (msg sent from GMail website) > mehgcap at gmail.com; http://www.facebook.com/mehgcap > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From sander.sweers at gmail.com Sun Dec 19 22:11:30 2010 From: sander.sweers at gmail.com (Sander Sweers) Date: Sun, 19 Dec 2010 22:11:30 +0100 Subject: [Tutor] Problem with print In-Reply-To: <1292792096.27430.0.camel@nerdism> References: <1292792096.27430.0.camel@nerdism> Message-ID: On 19 December 2010 21:54, jtl999 wrote: > ?File "GettingStarted.py", line 91 > ? ?print ("Lesson Two") > ? ? ? ?^ > SyntaxError: invalid syntax > > Python 2.6.5 You are using a howto for python version 3.X but you are using python 2.X. In python 3 the print statement was changed to a function. Python 2.X print "Lesson Two" Python 3.X print("Lesson Two") Either find a version of the howto for 2 or install python 3. greets Sander From hugo.yoshi at gmail.com Sun Dec 19 22:33:47 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Sun, 19 Dec 2010 22:33:47 +0100 Subject: [Tutor] Problem with print In-Reply-To: References: <1292792096.27430.0.camel@nerdism> Message-ID: On Sun, Dec 19, 2010 at 10:11 PM, Sander Sweers wrote: > On 19 December 2010 21:54, jtl999 wrote: >> ?File "GettingStarted.py", line 91 >> ? ?print ("Lesson Two") >> ? ? ? ?^ >> SyntaxError: invalid syntax >> >> Python 2.6.5 > > You are using a howto for python version 3.X but you are using python > 2.X. In python 3 the print statement was changed to a function. > > Python 2.X > print "Lesson Two" > > Python 3.X > print("Lesson Two") > > Either find a version of the howto for 2 or install python 3. > Alex is right, David and Sander judged too quickly: Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print("Lesson Two") Lesson Two >>> print("a", "b") ('a', 'b') Note the python version? It works. The parentheses are just parentheses, not a function call. But it still works. The actual error is likely on the line before the print statement. Hugo From smokefloat at gmail.com Sun Dec 19 22:41:32 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 19 Dec 2010 16:41:32 -0500 Subject: [Tutor] Problem with print In-Reply-To: References: <1292792096.27430.0.camel@nerdism> Message-ID: On Sun, Dec 19, 2010 at 4:33 PM, Hugo Arts wrote: > On Sun, Dec 19, 2010 at 10:11 PM, Sander Sweers wrote: >> On 19 December 2010 21:54, jtl999 wrote: >>> ?File "GettingStarted.py", line 91 >>> ? ?print ("Lesson Two") >>> ? ? ? ?^ >>> SyntaxError: invalid syntax >>> >>> Python 2.6.5 >> >> You are using a howto for python version 3.X but you are using python >> 2.X. In python 3 the print statement was changed to a function. >> >> Python 2.X >> print "Lesson Two" >> >> Python 3.X >> print("Lesson Two") >> >> Either find a version of the howto for 2 or install python 3. >> > > Alex is right, David and Sander judged too quickly: > > Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) > [GCC 4.4.5] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> print("Lesson Two") > Lesson Two >>>> print("a", "b") > ('a', 'b') > > Note the python version? It works. The parentheses are just > parentheses, not a function call. But it still works. The actual error > is likely on the line before the print statement. > Apparently so, but I'd like to see the full code to know what caused the error to point to print. http://www.google.com/search?client=ubuntu&channel=fs&q=python+print+syntax+error&ie=utf-8&oe=utf-8 > Hugo > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From lang at tharin.com Mon Dec 20 00:50:50 2010 From: lang at tharin.com (Lang Hurst) Date: Sun, 19 Dec 2010 15:50:50 -0800 Subject: [Tutor] os.startfile In-Reply-To: <4D0E7632.1050505@pearwood.info> References: <4D0E6417.6080502@tharin.com> <4D0E7632.1050505@pearwood.info> Message-ID: <4D0E9A5A.5010906@tharin.com> On 12/19/2010 01:16 PM, Steven D'Aprano wrote: > Lang Hurst wrote: >> I have the following in my program: >> >> >> try: >> os.startfile('current_credit.txt') >> except: >> os.system('/usr/bin/xdg-open current_credit.txt') >> >> >> Basically, open a file in notepad if I'm on windows, vim if on my >> home linux computer. It works fine in linux and in Windows through >> virtualbox. The problem is when I take the program to work, it >> doesn't open the file. The computers at work are pretty locked down, >> so I'm thinking it has something to do with the os.startfile >> command. Are there any alternative commands I could try? > > You don't give us much information to go on, such as the version of > Python you use, or the operating system on your work desktops, or the > error that you see when you try this, or even if you can open the file > by double-clicking it, but that's okay, because I love guessing games! > > I guess that the problem is that your work desktops are, in fact, > Apple Macintoshes running OS-X. Am I close? > > Other than that, you shouldn't just blindly ignore the exception > raised by startfile. Not all exceptions mean "You're not running > Windows and there is no startfile command", and you shouldn't catch > bare excepts. You would be better off doing this: > > try: > os.startfile('current_credit.txt') > except AttributeError: > # No startfile command, so we're not on Windows. > # Try a Linux command instead. > # (Tested on Fedora, may not work on all distros.) > os.system('/usr/bin/xdg-open current_credit.txt') > > > That way, when you get a different error, like "file not found" or > "permission denied", you will see what it is, and perhaps get a hint > as to what the problem is. > > Python doesn't have super powers. If you can't open a file because the > desktop has been locked down, then Python won't be able to magically > open it. It has no more permissions to do things than you do. There's > no magic command "open files even if I'm not allowed to open them". > > Sorry for the lack of information. I'm using Python 2.6.6, glade, gtk, vim. Once the program does what I want, I boot up the virtualbox image (XP) and try it in there. Usually it doesn't have a problem. If all works well, I wrap it all up into an executable using pyinstaller. Then I try to run the exe on XP. That works, so I pull it back into linux (Debian Sid, for what that's worth) and run the executable via wine. Everything checks out. I can't install anything at work (XP computer), hence the stand alone file. Then when I run it, everything works fine, except when I get to the point where I want notepad to open the file. I can browse to the file and manually open it with notepad and it's fine. It just won't open with notepad from the script. I just don't really know Windows that well. I was just wondering if my work computer being locked down like it is would stop the os.startfile command, and if so, do I have any alternative ways to do what I'm trying to do (open a text file). -- There are no stupid questions, just stupid people. From alan.gauld at btinternet.com Mon Dec 20 01:22:28 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Dec 2010 00:22:28 -0000 Subject: [Tutor] os.startfile References: <4D0E6417.6080502@tharin.com> <4D0E7632.1050505@pearwood.info> <4D0E9A5A.5010906@tharin.com> Message-ID: "Lang Hurst" wrote > I can't install anything at work (XP computer), hence the stand > alone file. Then when I run it, everything works fine, except when > I get to the point where I want notepad to open the file. I can > browse to the file and manually open it with notepad and it's fine. > It just won't open with notepad from the script. You have missed the most critical bit of information. Can you open the text file without going through Notepad? ie If you double click on the text file in Windows explorer does Notepad open? os.startfile() relies on the same file associations that explorer uses. If Explorer can start Notepad then your program should also be able to. But from your description it sounds like you are starting Notepad then opening the file? Given you are only dealing with Notepad and a text file you could also just start Notepad directly from your program. In fact you could even check the OS and call whichever helper editor was associated with that OS. If you put that in a config file the user could then change the editor if they didn't like the OS default... Just some thoughts, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Mon Dec 20 11:15:50 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 20 Dec 2010 21:15:50 +1100 Subject: [Tutor] Problem with print In-Reply-To: References: <1292792096.27430.0.camel@nerdism> Message-ID: <4D0F2CD6.9060002@pearwood.info> David Hutto wrote: > On Sun, Dec 19, 2010 at 4:33 PM, Hugo Arts wrote: >> On Sun, Dec 19, 2010 at 10:11 PM, Sander Sweers wrote: >>> On 19 December 2010 21:54, jtl999 wrote: >>>> File "GettingStarted.py", line 91 >>>> print ("Lesson Two") >>>> ^ >>>> SyntaxError: invalid syntax [...] > Apparently so, but I'd like to see the full code to know what caused > the error to point to print. It is very common for an error in parentheses or brackets to lead to a syntax error on the following line. >>> compile(""" ... x = 1 ... y = (x+1/2 ... z = 3 ... """, "", "exec") Traceback (most recent call last): File "", line 5, in File "", line 4 z = 3 ^ SyntaxError: invalid syntax -- Steven From steve at pearwood.info Mon Dec 20 11:56:15 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 20 Dec 2010 21:56:15 +1100 Subject: [Tutor] calling setters of superclasses In-Reply-To: References: <1D673F86DDA00841A1216F04D1CE70D642680008E8@EXCH2.nws.oregonstate.edu> Message-ID: <4D0F364F.9030102@pearwood.info> Alan Gauld wrote: > > "Gregory, Matthew" wrote >> class PositiveX(object): >> def __init__(self): >> @property >> def x(self): >> @x.setter >> def x(self, val): > > I don't use properties in Python very often (hardly ever in fact) and > I've never used @setter so there may be naming requirements I'm not > aware of. But in general I'd avoid having two methods with the same name. That's generally good advice, since one will over-write the other, but in this specific case, the following is completely bad: > Try renaming the setter to setX() or somesuch and see if you get the > same error. When using the x.setter and x.deleter decorators of a property, you *must* use the same name. The example given by help(property) for Python2.6 says this: | Decorators make defining new properties or modifying existing ones easy: | class C(object): | @property | def x(self): return self._x | @x.setter | def x(self, value): self._x = value | @x.deleter | def x(self): del self._x and the docs are even more explicit: http://docs.python.org/library/functions.html#property If you don't use the same name, chaos reigns: >>> class Broken(object): ... def __init__(self): ... self._x = 42 ... @property ... def x(self): ... return self._x ... @x.setter ... def set_x(self, value): ... self._x = value + 1 ... >>> >>> obj = Broken() >>> obj.x 42 >>> obj.x = 20 Traceback (most recent call last): File "", line 1, in AttributeError: can't set attribute -- Steven From cbeg at gmx.de Mon Dec 20 11:58:49 2010 From: cbeg at gmx.de (Chris Begert) Date: Mon, 20 Dec 2010 11:58:49 +0100 Subject: [Tutor] doing maths on lists Message-ID: <20101220105849.126940@gmx.net> Bonjour I have three lists with 65 float items and would like to do the following sum: L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))]) So it just should do a sum across all the items in the list: L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+... + L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number However, I always get this error: TypeError: can't multiply sequence by non-int of type 'float' I looked it up and there seems to be some solution using either the "for-in" command or the "map(int,...)" command but I just can't get it to work.... Any help will be very much appreciated :) Greetings from Sydney Chris -- GRATIS! Movie-FLAT mit ?ber 300 Videos. Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome From nitinpawar432 at gmail.com Mon Dec 20 12:03:39 2010 From: nitinpawar432 at gmail.com (Nitin Pawar) Date: Mon, 20 Dec 2010 16:33:39 +0530 Subject: [Tutor] doing maths on lists In-Reply-To: <20101220105849.126940@gmx.net> References: <20101220105849.126940@gmx.net> Message-ID: you may want to do type casting whenever you have a float answer cause integer to float causes data loss On Mon, Dec 20, 2010 at 4:28 PM, Chris Begert wrote: > Bonjour > > I have three lists with 65 float items and would like to do the following > sum: > > L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))]) > > So it just should do a sum across all the items in the list: > > L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+... > + L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number > > > However, I always get this error: > > TypeError: can't multiply sequence by non-int of type 'float' > > I looked it up and there seems to be some solution using either the > "for-in" command or the "map(int,...)" command but I just can't get it to > work.... > > > Any help will be very much appreciated :) > > Greetings from Sydney > Chris > -- > GRATIS! Movie-FLAT mit ?ber 300 Videos. > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Nitin Pawar -------------- next part -------------- An HTML attachment was scrubbed... URL: From cbeg at gmx.de Mon Dec 20 12:28:42 2010 From: cbeg at gmx.de (Chris Begert) Date: Mon, 20 Dec 2010 12:28:42 +0100 Subject: [Tutor] doing maths on lists In-Reply-To: References: <20101220105849.126940@gmx.net> Message-ID: <20101220112842.160860@gmx.net> Thanks I tried to put a float around my lists as well as around the sum but it didn't work. Do I have to do some type casting around all items of the list? Cheers -------- Original-Nachricht -------- > Datum: Mon, 20 Dec 2010 16:33:39 +0530 > Von: Nitin Pawar > An: Chris Begert > CC: tutor at python.org > Betreff: Re: [Tutor] doing maths on lists > you may want to do type casting whenever you have a float answer cause > integer to float causes data loss > > On Mon, Dec 20, 2010 at 4:28 PM, Chris Begert wrote: > > > Bonjour > > > > I have three lists with 65 float items and would like to do the > following > > sum: > > > > L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))]) > > > > So it just should do a sum across all the items in the list: > > > > L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+... > > + L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number > > > > > > However, I always get this error: > > > > TypeError: can't multiply sequence by non-int of type 'float' > > > > I looked it up and there seems to be some solution using either the > > "for-in" command or the "map(int,...)" command but I just can't get it > to > > work.... > > > > > > Any help will be very much appreciated :) > > > > Greetings from Sydney > > Chris > > -- > > GRATIS! Movie-FLAT mit ?ber 300 Videos. > > Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > > > > -- > Nitin Pawar -- GRATIS! Movie-FLAT mit ?ber 300 Videos. Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome From cwitts at compuscan.co.za Mon Dec 20 12:41:04 2010 From: cwitts at compuscan.co.za (Christian Witts) Date: Mon, 20 Dec 2010 13:41:04 +0200 Subject: [Tutor] doing maths on lists In-Reply-To: <20101220105849.126940@gmx.net> References: <20101220105849.126940@gmx.net> Message-ID: <4D0F40D0.4020600@compuscan.co.za> On 20/12/2010 12:58, Chris Begert wrote: > Bonjour > > I have three lists with 65 float items and would like to do the following sum: > > L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))]) > > So it just should do a sum across all the items in the list: > > L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+... > + L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number > > > However, I always get this error: > > TypeError: can't multiply sequence by non-int of type 'float' > > I looked it up and there seems to be some solution using either the "for-in" command or the "map(int,...)" command but I just can't get it to work.... > > > Any help will be very much appreciated :) > > Greetings from Sydney > Chris > Surely it should be L0 = sum([(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0, 64)]) as you want to generate the answers and then sum it up. -- Kind Regards, Christian Witts From steve at pearwood.info Mon Dec 20 13:06:21 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 20 Dec 2010 23:06:21 +1100 Subject: [Tutor] doing maths on lists In-Reply-To: <20101220105849.126940@gmx.net> References: <20101220105849.126940@gmx.net> Message-ID: <4D0F46BD.2070908@pearwood.info> Chris Begert wrote: > Bonjour > > I have three lists with 65 float items and would like to do the following sum: > > L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))]) [...] > However, I always get this error: > > TypeError: can't multiply sequence by non-int of type 'float' Works for me: >>> from math import cos >>> L0A = [i for i in range(64)] # make up some data >>> L0B = [2*i+3 for i in range(64)] >>> L0C = [3*i-2 for i in range(64)] >>> JME = 2 >>> L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))]) >>> L0 [39.022477151098606] Please compare what you typed in your email with the code you are trying to run. I think you will find that you have something like this by mistake: ([sum(L0A*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))]) Notice that it has L0A*cos(...) instead of L0A[i]*cos(...). Some other comments: Why is the sum stored in a one-item list? I don't think you need either of the two outer-most brackets, ([...]). That is: L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1)) You can simplify the call to range: range(0, 64, 1) is just a long way of writing range(64): L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(64)) Rather than explicitly specify the indexes used, a better solution is to use zip() like this: L0 = sum(a*cos(b+c*JME) for a,b,c in zip(L0A, L0B, L0C)) -- Steven From davea at ieee.org Mon Dec 20 13:32:45 2010 From: davea at ieee.org (Dave Angel) Date: Mon, 20 Dec 2010 07:32:45 -0500 Subject: [Tutor] doing maths on lists In-Reply-To: <20101220105849.126940@gmx.net> References: <20101220105849.126940@gmx.net> Message-ID: <4D0F4CED.2050600@ieee.org> On 01/-10/-28163 02:59 PM, Chris Begert wrote: > Bonjour > > I have three lists with 65 float items and would like to do the following sum: > > L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))]) > > So it just should do a sum across all the items in the list: > > L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+... > + L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number > > > However, I always get this error: > > TypeError: can't multiply sequence by non-int of type 'float' > > I looked it up and there seems to be some solution using either the "for-in" command or the "map(int,...)" command but I just can't get it to work.... > > > Any help will be very much appreciated :) > > Greetings from Sydney > Chris Look up range(), and notice that the first and 3rd arguments default to 0 and 1 respectively. So you're just doing range(64), which gives you the first 64 items in the list. But you said there were 65, so why didn't you use 65 ? Where did you get that python expression? If you just want a sum, there's no need for the outer pair of square braces or parentheses. As it is, it'll correctly build a list of 1 item, which is the sum of all but one term of the desired expression. Anyway, the expression doesn't give an error, if you've really got those three arrays of floats. My guess is that you either copied the expression wrong (use copy/paste), or you've got something else in those lists. Make a simple test case, and show the whole thing, including imports: from math import cos JME = 0.4 L0A = [2.3, 4.65] L0B = [1.8, 2.2] L0C = [12.1, 4] limit = len(L0A) L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(limit)) print L0 runs fine with Python 2.6 on Linux, producing output of: -1.52286725666 DaveA From cbeg at gmx.de Mon Dec 20 14:19:13 2010 From: cbeg at gmx.de (Chris Begert) Date: Mon, 20 Dec 2010 14:19:13 +0100 Subject: [Tutor] doing maths on lists In-Reply-To: <4D0F4CED.2050600@ieee.org> References: <20101220105849.126940@gmx.net> <4D0F4CED.2050600@ieee.org> Message-ID: <20101220131913.160840@gmx.net> Dave and Christian Thanks a lot for the help, it looks like it works! Chris -------- Original-Nachricht -------- > Datum: Mon, 20 Dec 2010 07:32:45 -0500 > Von: Dave Angel > An: Chris Begert > CC: tutor at python.org > Betreff: Re: [Tutor] doing maths on lists > On 01/-10/-28163 02:59 PM, Chris Begert wrote: > > Bonjour > > > > I have three lists with 65 float items and would like to do the > following sum: > > > > L0 = ([sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(0,64,1))]) > > > > So it just should do a sum across all the items in the list: > > > > L0A[0]*cos(L0B[0]+L0C[0]*JME)+ L0A[1]*cos(L0B[1]+L0C[1]*JME)+... > > + L0A[64]*cos(L0B[64]+L0C[64]*JME)= some float number > > > > > > However, I always get this error: > > > > TypeError: can't multiply sequence by non-int of type 'float' > > > > I looked it up and there seems to be some solution using either the > "for-in" command or the "map(int,...)" command but I just can't get it to > work.... > > > > > > Any help will be very much appreciated :) > > > > Greetings from Sydney > > Chris > > Look up range(), and notice that the first and 3rd arguments default to > 0 and 1 respectively. So you're just doing range(64), which gives you > the first 64 items in the list. But you said there were 65, so why > didn't you use 65 ? > > Where did you get that python expression? If you just want a sum, > there's no need for the outer pair of square braces or parentheses. As > it is, it'll correctly build a list of 1 item, which is the sum of all > but one term of the desired expression. > > Anyway, the expression doesn't give an error, if you've really got those > three arrays of floats. My guess is that you either copied the > expression wrong (use copy/paste), or you've got something else in those > lists. Make a simple test case, and show the whole thing, including > imports: > > > from math import cos > > JME = 0.4 > L0A = [2.3, 4.65] > L0B = [1.8, 2.2] > L0C = [12.1, 4] > limit = len(L0A) > > L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(limit)) > > > print L0 > > runs fine with Python 2.6 on Linux, producing output of: > > -1.52286725666 > > > DaveA -- Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief! Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail From yanglei.fage at gmail.com Mon Dec 20 14:38:30 2010 From: yanglei.fage at gmail.com (lei yang) Date: Mon, 20 Dec 2010 21:38:30 +0800 Subject: [Tutor] why "ifconfig" is alway running? In-Reply-To: References: <3508A5D1-5199-4B68-A60A-9BAB603D0A1D@gmail.com> <001BBD87-2BE1-4CF9-887E-9E00576C3EFB@gmail.com> Message-ID: On Sun, Dec 19, 2010 at 9:05 PM, Sander Sweers wrote: > On 19 December 2010 13:43, lei yang wrote: >> Right, it gets stuck at the readline(), is there a function not get >> stuck to instead of readline(). > > readline() will keep reading stdout until it received a newline > character. So if there is nothing to read it will wait forever. The > solution is to wait with reading until there is actually something to > read. And it is recommended [1] to use proc.communicate() instead of > reading directly from stdout. > > Also you should use proc.terminate() or proc.kill() to stop the command. > > Below is a modified version of your function to show what I mean. > > [1] http://docs.python.org/library/subprocess.html#subprocess.Popen.stdin > > Greets > Sander > > def host_run(cmd, secs=10): > ? ?print("running %s" % cmd) > ? ?timeout = datetime.timedelta(seconds=secs) > ? ?proc = subprocess.Popen( > ? ? ? ?cmd, > ? ? ? ?stdout=subprocess.PIPE, > ? ? ? ?stderr=subprocess.STDOUT, > ? ? ? ?shell=True) > ? ?start = datetime.datetime.now() > ? ?while ( proc.poll() is None and (datetime.datetime.now() - start) > < timeout): #not timed out > ? ? ? ?print "hello,i'm here" > ? ? ? ?sleep(1) > ? ?if 0 == proc.poll(): > ? ? ? ?stdout, stderr = proc.communicate() #get stdout and stderr > ? ? ? ?print("'%s' is program exited" %cmd) > ? ? ? ?print stdout > ? ? ? ?print stderr > ? ?else: > ? ? ? ?proc.terminate() #Can use proc.kill() as well my python verison is 2.5, so has no kill or terminate attribute, so I still use os.kill(proc.pid, signal.SIGINT) instead > ? ? ? ?stdout, stderr = proc.communicate() #get stdout and stderr and I find it will get stuck here > ? ? ? ?print 'Timeout: Process terminated' > ? ? ? ?print stdout > ? ? ? ?print stderr > From alan.gauld at btinternet.com Mon Dec 20 17:23:27 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Dec 2010 16:23:27 -0000 Subject: [Tutor] calling setters of superclasses References: <1D673F86DDA00841A1216F04D1CE70D642680008E8@EXCH2.nws.oregonstate.edu> <4D0F364F.9030102@pearwood.info> Message-ID: "Steven D'Aprano" wrote >> I don't use properties in Python very often (hardly ever in fact) >> and I've never used @setter so there may be naming requirements I'm >> not aware of. But in general I'd avoid having two methods with the >> same name. > > That's generally good advice, since one will over-write the other, > but in this specific case, the following is completely bad: Yep, that's what I suspected might be the case. > If you don't use the same name, chaos reigns: > > >>> class Broken(object): > ... def __init__(self): > ... @property > ... def x(self): > ... @x.setter > ... def set_x(self, value): > ... > >>> obj = Broken() > >>> obj.x = 20 > Traceback (most recent call last): > File "", line 1, in > AttributeError: can't set attribute All part of the reason I don't particularly like properties in Python. It feels like it goes against the principle of explicit is better than implicit... That and the fact that properties encourage a data-centric view of objects. Alan G. From __peter__ at web.de Mon Dec 20 17:47:49 2010 From: __peter__ at web.de (Peter Otten) Date: Mon, 20 Dec 2010 17:47:49 +0100 Subject: [Tutor] doing maths on lists References: <20101220105849.126940@gmx.net> <4D0F4CED.2050600@ieee.org> Message-ID: Dave Angel wrote: > from math import cos > > JME = 0.4 > L0A = [2.3, 4.65] > L0B = [1.8, 2.2] > L0C = [12.1, 4] > limit = len(L0A) > > L0 = sum(L0A[i]*cos(L0B[i]+L0C[i]*JME) for i in range(limit)) OP, if you are planning to do this "maths on lists" a lot you should have a look at numpy. With numpy Dave's snippet can be written as from numpy import array, cos, sum j = 0.4 a = array([2.3, 4.65]) b = array([1.8, 2.2]) c = array([12.1, 4]) print sum(a*cos(b+c*j)) Peter From ashish.makani at gmail.com Mon Dec 20 21:08:09 2010 From: ashish.makani at gmail.com (ashish makani) Date: Mon, 20 Dec 2010 12:08:09 -0800 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python Message-ID: Hi Python Tutor folks This is a rather long post, but i wanted to include all the details & everything i have tried so far myself, so please bear with me & read the entire boringly long post. Goal : I am trying to parse a ginormous ( ~ 1gb) xml file. I am looking for a specific element..there are several 10s/100s occurrences of that element in the 1gb file. I need to detect them & then for each 1, i need to copy all the content b/w the element's start & end tags & create a smaller xml 0. I am a python & xml n00b, s& have been relying on the excellent beginner book DIP(Dive_Into_Python3 by MP(Mark Pilgrim).... Mark , if u are readng this, you are AWESOME & so is your witty & humorous writing style) My hardware setup : I have a win7 pro box with 8gb of RAM & intel core2 quad cpuq9400. On this i am running sun virtualbox(3.2.12), with ubuntu 10.10(maverick) as guest os, with 23gb disk space & 2gb(2048mb) ram, assigned to the guest ubuntu os. 1. Almost all exmaples pf parsing xml in python, i have seen, start off with these 4 lines of code. import xml.etree.ElementTree as etree tree = etree.parse('*path_to_ginormous_xml*') root = tree.getroot() #my huge xml has 1 root at the top level print root 2. In the 2nd line of code above, as Mark explains in DIP, the parse function builds & returns a tree object, in-memory(RAM), which represents the entire document. I tried this code, which works fine for a small ( ~ 1MB), but when i run this simple 4 line py code in a terminal for my HUGE target file (1GB), nothing happens. In a separate terminal, i run the top command, & i can see a python process, with memory (the VIRT column) increasing from 100MB , all the way upto 2100MB. I am guessing, as this happens (over the course of 20-30 mins), the tree representing is being slowly built in memory, but even after 30-40 mins, nothing happens. I dont get an error, seg fault or out_of_memory exception. 3. I also tried using lxml, but an lxml tree is much more expensive, as it retains more info about a node's context, including references to it's parent. [http://www.ibm.com/developerworks/xml/library/x-hiperfparse/] When i ran the same 4line code above, but with lxml's elementree ( using the import below in line1of the code above) import lxml.etree as lxml_etree i can see the memory consumption of the python process(which is running the code) shoot upto ~ 2700mb & then, python(or the os ?) kills the process as it nears the total system memory(2gb) I ran the code from 1 terminal window (screenshot : http://imgur.com/ozLkB.png) & ran top from another terminal (http://imgur.com/HAoHA.png) 4. I then investigated some streaming libraries, but am confused - there is SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , the iterparse interface[http://effbot.org/zone/element-iterparse.htm], & several otehr options ( minidom) Which one is the best for my situation ? Should i instead just open the file, & use reg ex to look for the element i need ? Any & all code_snippets/wisdom/thoughts/ideas/suggestions/feedback/comments/ of the Python tutor community would be greatly appreciated. Plz feel free to email me directly too. thanks a ton cheers ashish email : ashish.makani domain:gmail.com p.s. Other useful links on xml parsing in python 0. http://diveintopython3.org/xml.html 1. http://stackoverflow.com/questions/1513592/python-is-there-an-xml-parser-implemented-as-a-generator 2. http://codespeak.net/lxml/tutorial.html 3. https://groups.google.com/forum/?hl=en&lnk=gst&q=parsing+a+huge+xml#!topic/comp.lang.python/CMgToEnjZBk 4. http://www.ibm.com/developerworks/xml/library/x-hiperfparse/ 5.http://effbot.org/zone/element-index.htm http://effbot.org/zone/element-iterparse.htm 6. SAX : http://en.wikipedia.org/wiki/Simple_API_for_XML -------------- next part -------------- An HTML attachment was scrubbed... URL: From rabidpoobear at gmail.com Mon Dec 20 21:42:33 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Mon, 20 Dec 2010 14:42:33 -0600 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: Message-ID: <52F70DA7-0F5C-41E9-BE09-2D18161F4212@gmail.com> If you can assume a well formatted file I would just parse it linearly, should be much faster. Read the file in as lines if the XML is already in human readable form, or just read in blocks and append to a list and do a join() when you have a whole match. ----------------------------- Sent from a mobile device with a bad e-mail client. ----------------------------- On Dec 20, 2010, at 2:08 PM, ashish makani wrote: > > Hi Python Tutor folks > This is a rather long post, but i wanted to include all the details & everything i have tried so far myself, so please bear with me & read the entire boringly long post. > > Goal : I am trying to parse a ginormous ( ~ 1gb) xml file. > > I am looking for a specific element..there are several 10s/100s occurrences of that element in the 1gb file. > > I need to detect them & then for each 1, i need to copy all the content b/w the element's start & end tags & create a smaller xml > > > 0. I am a python & xml n00b, s& have been relying on the excellent beginner book DIP(Dive_Into_Python3 by MP(Mark Pilgrim).... Mark , if u are readng this, you are AWESOME & so is your witty & humorous writing style) > > My hardware setup : I have a win7 pro box with 8gb of RAM & intel core2 quad cpuq9400. > On this i am running sun virtualbox(3.2.12), with ubuntu 10.10(maverick) as guest os, with 23gb disk space & 2gb(2048mb) ram, assigned to the guest ubuntu os. > > > 1. Almost all exmaples pf parsing xml in python, i have seen, start off with these 4 lines of code. > > import xml.etree.ElementTree as etree > tree = etree.parse('*path_to_ginormous_xml*') > root = tree.getroot() #my huge xml has 1 root at the top level > print root > > 2. In the 2nd line of code above, as Mark explains in DIP, the parse function builds & returns a tree object, in-memory(RAM), which represents the entire document. > I tried this code, which works fine for a small ( ~ 1MB), but when i run this simple 4 line py code in a terminal for my HUGE target file (1GB), nothing happens. > In a separate terminal, i run the top command, & i can see a python process, with memory (the VIRT column) increasing from 100MB , all the way upto 2100MB. > > I am guessing, as this happens (over the course of 20-30 mins), the tree representing is being slowly built in memory, but even after 30-40 mins, nothing happens. > I dont get an error, seg fault or out_of_memory exception. > > 3. I also tried using lxml, but an lxml tree is much more expensive, as it retains more info about a node's context, including references to it's parent. > > [http://www.ibm.com/developerworks/xml/library/x-hiperfparse/] > > When i ran the same 4line code above, but with lxml's elementree ( using the import below in line1of the code above) > import lxml.etree as lxml_etree > > i can see the memory consumption of the python process(which is running the code) shoot upto ~ 2700mb & then, python(or the os ?) kills the process as it nears the total system memory(2gb) > > I ran the code from 1 terminal window (screenshot :http://imgur.com/ozLkB.png) > & ran top from another terminal (http://imgur.com/HAoHA.png) > > 4. I then investigated some streaming libraries, but am confused - there is SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , the iterparse interface[http://effbot.org/zone/element-iterparse.htm], & several otehr options ( minidom) > > Which one is the best for my situation ? > > Should i instead just open the file, & use reg ex to look for the element i need ? > > > > Any & all code_snippets/wisdom/thoughts/ideas/suggestions/feedback/comments/ of the Python tutor community would be greatly appreciated. > Plz feel free to email me directly too. > > thanks a ton > > cheers > ashish > > email : > ashish.makani > domain:gmail.com > > p.s. > Other useful links on xml parsing in python > 0. http://diveintopython3.org/xml.html > 1. http://stackoverflow.com/questions/1513592/python-is-there-an-xml-parser-implemented-as-a-generator > 2. http://codespeak.net/lxml/tutorial.html > 3. https://groups.google.com/forum/?hl=en&lnk=gst&q=parsing+a+huge+xml#!topic/comp.lang.python/CMgToEnjZBk > 4. http://www.ibm.com/developerworks/xml/library/x-hiperfparse/ > 5.http://effbot.org/zone/element-index.htm > http://effbot.org/zone/element-iterparse.htm > 6. SAX : http://en.wikipedia.org/wiki/Simple_API_for_XML > > _______________________________________________ > 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 20 22:19:51 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 21 Dec 2010 08:19:51 +1100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: Message-ID: <4D0FC877.9010705@pearwood.info> ashish makani wrote: > Goal : I am trying to parse a ginormous ( ~ 1gb) xml file. I sympathize with you. I wonder who thought that building a 1GB XML file was a good thing. Forget about using any XML parser that reads the entire file into memory. By the time that 1GB of text is read and parsed, you will probably have something about 6-8GB (estimated) in size. [...] > My hardware setup : I have a win7 pro box with 8gb of RAM & intel core2 quad > cpuq9400. In order to access 8GB of RAM, you'll be running a 64-bit OS, correct? In this case, you should expect double the memory usage of the XML object to (estimated) 12-16GB. > I am guessing, as this happens (over the course of 20-30 mins), the tree > representing is being slowly built in memory, but even after 30-40 mins, > nothing happens. It's probably not finished. Leave it another hour or so and you'll get an out of memory error. > 4. I then investigated some streaming libraries, but am confused - there is > SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , the iterparse > interface[http://effbot.org/zone/element-iterparse.htm], & several otehr > options ( minidom) > > Which one is the best for my situation ? You absolutely need to use a streaming library. element-iterparse still builds the tree, so that's no use to you. I believe you should use SAX or minidom, but that's about my limit of knowledge of streaming XML parsers. > Should i instead just open the file, & use reg ex to look for the element i > need ? That's likely to need less memory than building a parse tree, but still a huge amount of memory. And you don't know how complex the XML is, in general you *can't* correctly parse arbitrary XML with regular expressions (although you can for simple examples). Stick with the right tool for the job, the streaming XML library. -- Steven From swiftone at swiftone.org Mon Dec 20 22:40:54 2010 From: swiftone at swiftone.org (Brett Ritter) Date: Mon, 20 Dec 2010 16:40:54 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: <4D0FC877.9010705@pearwood.info> References: <4D0FC877.9010705@pearwood.info> Message-ID: On Mon, Dec 20, 2010 at 4:19 PM, Steven D'Aprano wrote: >> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file. > > I sympathize with you. I wonder who thought that building a 1GB XML file was > a good thing. XML is like violence: if it isn't working, try more. -- Brett Ritter / SwiftOne swiftone at swiftone.org From zebra05 at gmail.com Mon Dec 20 22:50:50 2010 From: zebra05 at gmail.com (Sithembewena Lloyd Dube) Date: Mon, 20 Dec 2010 23:50:50 +0200 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: [?] Brett, that was very mischievous. I wish I could help - am watching this thread with great curiosity, I could learn something from it myself. On Mon, Dec 20, 2010 at 11:40 PM, Brett Ritter wrote: > On Mon, Dec 20, 2010 at 4:19 PM, Steven D'Aprano > wrote: > >> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file. > > > > I sympathize with you. I wonder who thought that building a 1GB XML file > was > > a good thing. > > XML is like violence: if it isn't working, try more. > > -- > Brett Ritter / SwiftOne > swiftone at swiftone.org > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Regards, Sithembewena Lloyd Dube -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: 338.gif Type: image/gif Size: 541 bytes Desc: not available URL: From steve at pearwood.info Mon Dec 20 23:32:35 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 21 Dec 2010 09:32:35 +1100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: <4D0FD983.5090304@pearwood.info> Brett Ritter wrote: > On Mon, Dec 20, 2010 at 4:19 PM, Steven D'Aprano wrote: >>> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file. >> I sympathize with you. I wonder who thought that building a 1GB XML file was >> a good thing. > > XML is like violence: if it isn't working, try more. > I love it -- may I quote you? -- Steven From swiftone at swiftone.org Tue Dec 21 01:37:34 2010 From: swiftone at swiftone.org (Brett Ritter) Date: Mon, 20 Dec 2010 19:37:34 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: <4D0FD983.5090304@pearwood.info> References: <4D0FC877.9010705@pearwood.info> <4D0FD983.5090304@pearwood.info> Message-ID: On Mon, Dec 20, 2010 at 5:32 PM, Steven D'Aprano wrote: >> XML is like violence: if it isn't working, try more. > > I love it -- may I quote you? I can't claim credit for it, I saw originally saw it on some sigs on Slashdot a few years ago. It certainly matches the majority of XML usage I've encountered. As to the original post: Yes, as others have suggested you're going to want an event-based parser along the lines of SAX. Sadly (for you) this means a mental shift in how you address your code, but it's not terrible - just different. -- Brett Ritter / SwiftOne swiftone at swiftone.org From alan.gauld at btinternet.com Tue Dec 21 02:13:08 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 01:13:08 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: Message-ID: "ashish makani" wrote > I am looking for a specific element..there are several 10s/100s > occurrences > of that element in the 1gb file. > > I need to detect them & then for each 1, i need to copy all the > content b/w > the element's start & end tags & create a smaller xml This is exactly what sax and its kin are for. If you wanted to manipulate the xml data and recreate the original file tree based is better but for this kind of one shot processing SAX will be much much faster. The concept is simple enough if you have ever used awk to process text files. (or the Python HTMLParser) You define a function that gets triggered when the parser detects a matching tag. > My hardware setup : I have a win7 pro box with 8gb of RAM & intel > core2 quad > cpuq9400. > On this i am running sun virtualbox(3.2.12), with ubuntu > 10.10(maverick) as > guest os, with 23gb disk space & 2gb(2048mb) ram, assigned to the > guest > ubuntu os. Obviously running the code in the virtuial machjine is limiting your ability to deal with the data but in this case you would be pushing hard to build the entire tree in RAM anyway so it probably doesn't matter. > 4. I then investigated some streaming libraries, but am confused - > there is > SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , > Which one is the best for my situation ? I've only used sax - I tried minidom once but couldn't get it to work as I wanted so went back to sax... There are lots of examples of xml parsing using sax, both in Python and Java - just google. > Should i instead just open the file, & use reg ex to look for the > element i > need ? Unless the xml is very simple you would probably find yourself creating a bigger problem. regex's are not good at handling the kinds of recursive data structures as can be found in SGML based languages. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From ashish.makani at gmail.com Tue Dec 21 03:37:02 2010 From: ashish.makani at gmail.com (ashish makani) Date: Mon, 20 Dec 2010 18:37:02 -0800 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: Message-ID: Thanks Luke, Steve, Brett, Lloyd & Alan for your prompt responses & sharing your wisdom. I <3 the python community... You(We ?) folks are AWESOME I cross-posted this query on comp.lang.python I bet most of you hang @ c.l.p too, but just in case, here is the link to the discussion at c.l.p https://groups.google.com/d/topic/comp.lang.python/i816mDMSoXM/discussion Thanks again for the amazing help & advice cheers ashish On Mon, Dec 20, 2010 at 5:13 PM, Alan Gauld wrote: > "ashish makani" wrote > > I am looking for a specific element..there are several 10s/100s >> occurrences >> of that element in the 1gb file. >> >> I need to detect them & then for each 1, i need to copy all the content >> b/w >> the element's start & end tags & create a smaller xml >> > > This is exactly what sax and its kin are for. If you wanted to manipulate > the xml data and recreate the original file tree based is better but for > this > kind of one shot processing SAX will be much much faster. > > The concept is simple enough if you have ever used awk to process > text files. (or the Python HTMLParser) You define a function that gets > triggered when the parser detects a matching tag. > > > My hardware setup : I have a win7 pro box with 8gb of RAM & intel core2 >> quad >> cpuq9400. >> On this i am running sun virtualbox(3.2.12), with ubuntu 10.10(maverick) >> as >> guest os, with 23gb disk space & 2gb(2048mb) ram, assigned to the guest >> ubuntu os. >> > > Obviously running the code in the virtuial machjine is limiting your > ability to deal with the data but in this case you would be pushing > hard to build the entire tree in RAM anyway so it probably doesn't > matter. > > > 4. I then investigated some streaming libraries, but am confused - there >> is >> SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , >> > > Which one is the best for my situation ? >> > > I've only used sax - I tried minidom once but couldn't get it to work > as I wanted so went back to sax... There are lots of examples of > xml parsing using sax, both in Python and Java - just google. > > > Should i instead just open the file, & use reg ex to look for the element >> i >> need ? >> > > Unless the xml is very simple you would probably find yourself > creating a bigger problem. regex's are not good at handling the > kinds of recursive data structures as can be found in SGML > based languages. > > HTH, > > > -- > Alan Gauld > 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 > * "We act as though comfort and luxury were the chief requirements of life, when all that we need to make us happy is something to be enthusiastic about." -- Albert Einstein* -------------- next part -------------- An HTML attachment was scrubbed... URL: From cfuller084 at thinkingplanet.net Tue Dec 21 03:27:44 2010 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Mon, 20 Dec 2010 20:27:44 -0600 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: Message-ID: <201012202027.44992.cfuller084@thinkingplanet.net> This isn't XML, it's an abomination of XML. Best to not treat it as XML. Good thing you're only after one class of tags. Here's what I'd do. I'll give a general solution, but there are two parameters / four cases that could make the code simpler, I'll just point them out at the end. Iterate over the file descriptor, reading in line-by-line. This will be slow on a huge file, but probably not so bad if you're only doing it once. It makes the rest easier. Knuth has some sage advice on this point (*) :) Some feedback on progress to the user can be helpful here, if it is slow. Keep track of your offset into the file. There are two ways: use the tell() method of the file descriptor (but you will have to subtract the length of the current line), or just add up the line lengths as you process them. Scan each line for the open tag. Add the offset to the tag to the offset within the file of the current line, and push that to a stack. Scan for the end tag, when you find one, pop an address from the stack, and put the two (start/end) addresses a list for later. Keep doing this until you run out of file. Now, take that list, and pull off the address-pairs; seek() and read() them directly. Lather, rinse, repeat. Some off-the-cuff untested code: stk = [] yummydataaddrs = [] fileoff = 0 fd = open('ginormous.xml', 'r') for line in fd: lineoff = line.index(start_tag) if fileoff != -1: stk.append(fileoff+lineoff) lineoff = line.index(end_tag) if lineoff != -1: yummydataaddr.append( (stk.pop(-1), fileoff+lineoff) ) fileoff += len(line) for start,end in yummydataaddrs: fd.seek(start) print "here's your stupid data:", fd.read(end-start+1) You can simplify a bit if the tags are one a line by themselves, since you don't have to keep track of the offset with the line of the tag. The other simplification is if they aren't nested. You don't need to mess around with a stack in this case. (*) "Premature optimization is the root of all evil." Cheers From ashish.makani at gmail.com Tue Dec 21 05:11:03 2010 From: ashish.makani at gmail.com (ashish makani) Date: Mon, 20 Dec 2010 23:11:03 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: <201012202027.44992.cfuller084@thinkingplanet.net> References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: Chris This block of code made my day - especially yummydataaddrs & "here's your stupid data" > for start,end in yummydataaddrs: > fd.seek(start) > print "here's your stupid data:", fd.read(end-start+1) Nothing is more impressive than solid code, with a good sense of humor. Thanks for the code & especially since i am in a time crunch, this approach, might get me what i need more quickly. Thanks also for Knuth's awesome quote & reminded me of my stanford friend who told me that Prof. Knuth, still holds a christmas tree lecture every year...unfortunately inspite of being in the bay area this year, i missed it :( http://stanford-online.stanford.edu/seminars/knuth/101206-knuth-500.asx Thanks a ton cheers ashish p.s. To everybody OT(off_topic): I moved to the bay area recently & am passionate about technology in general & linux, python, c, embedded, mobile, wireless stuff,..... I was wondering if any of you guys, are part of some bay area python( or other tech) meetup ( as in do you guys meetup, in person) for like a tech talk / discussion / brainstorming/ hack nights ? If yes, i would love to know more & be a part of it On Mon, Dec 20, 2010 at 9:27 PM, Chris Fuller wrote: > > This isn't XML, it's an abomination of XML. Best to not treat it as XML. > Good thing you're only after one class of tags. Here's what I'd do. I'll > give a general solution, but there are two parameters / four cases that > could > make the code simpler, I'll just point them out at the end. > > Iterate over the file descriptor, reading in line-by-line. This will be > slow > on a huge file, but probably not so bad if you're only doing it once. It > makes > the rest easier. Knuth has some sage advice on this point (*) :) Some > feedback on progress to the user can be helpful here, if it is slow. > > Keep track of your offset into the file. There are two ways: use the > tell() > method of the file descriptor (but you will have to subtract the length of > the > current line), or just add up the line lengths as you process them. > > Scan each line for the open tag. Add the offset to the tag to the offset > within > the file of the current line, and push that to a stack. Scan for the end > tag, > when you find one, pop an address from the stack, and put the two > (start/end) > addresses a list for later. Keep doing this until you run out of file. > > Now, take that list, and pull off the address-pairs; seek() and read() them > directly. Lather, rinse, repeat. > > Some off-the-cuff untested code: > > stk = [] > yummydataaddrs = [] > > fileoff = 0 > > fd = open('ginormous.xml', 'r') > for line in fd: > lineoff = line.index(start_tag) > if fileoff != -1: > stk.append(fileoff+lineoff) > > lineoff = line.index(end_tag) > if lineoff != -1: > yummydataaddr.append( (stk.pop(-1), fileoff+lineoff) ) > > fileoff += len(line) > > for start,end in yummydataaddrs: > fd.seek(start) > print "here's your stupid data:", fd.read(end-start+1) > > > You can simplify a bit if the tags are one a line by themselves, since you > don't have to keep track of the offset with the line of the tag. The other > simplification is if they aren't nested. You don't need to mess around > with a > stack in this case. > > > (*) "Premature optimization is the root of all evil." > > > Cheers > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > *"We act as though comfort and luxury were the chief requirements of life, when all that we need to make us happy is something to be enthusiastic about." -- Albert Einstein* -------------- next part -------------- An HTML attachment was scrubbed... URL: From jacksmoo111 at gmail.com Tue Dec 21 05:26:23 2010 From: jacksmoo111 at gmail.com (jtl999) Date: Mon, 20 Dec 2010 20:26:23 -0800 Subject: [Tutor] decimal input problem Message-ID: <1292905583.10115.3.camel@nerdism> when I try to multiply with a decimal number in python with the input this is what i get MathCheats Times-Ed by jtl999 Not for decimal numbers due to a bug in the code. Enter first number: 1.2 Traceback (most recent call last): File "Timesed.py", line 18, in numberx1 = (int)(raw_input('Enter first number: ')) ValueError: invalid literal for int() with base 10: '1.2' here is the code #!/usr/bin/python # MathCheats Times-Ed by jtl999 # (C) 2010 jtl999 # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see . # The GPL is in the file COPYING print ("MathCheats Times-Ed by jtl999") print ("Not for decimal numbers due to a bug in the code.") numberx1 = (int)(raw_input('Enter first number: ')) numberx2 = (int)(raw_input('Enter second number: ')) print ("The answer was") print numberx1*numberx2 From smokefloat at gmail.com Tue Dec 21 05:35:41 2010 From: smokefloat at gmail.com (David Hutto) Date: Mon, 20 Dec 2010 23:35:41 -0500 Subject: [Tutor] Problem with print In-Reply-To: <4D0F2CD6.9060002@pearwood.info> References: <1292792096.27430.0.camel@nerdism> <4D0F2CD6.9060002@pearwood.info> Message-ID: On Mon, Dec 20, 2010 at 5:15 AM, Steven D'Aprano wrote: > David Hutto wrote: >> >> On Sun, Dec 19, 2010 at 4:33 PM, Hugo Arts wrote: >>> >>> On Sun, Dec 19, 2010 at 10:11 PM, Sander Sweers >>> wrote: >>>> >>>> On 19 December 2010 21:54, jtl999 wrote: >>>>> >>>>> ?File "GettingStarted.py", line 91 >>>>> ? print ("Lesson Two") >>>>> ? ? ? ^ >>>>> SyntaxError: invalid syntax > > [...] > >> Apparently so, but I'd like to see the full code to know what caused >> the error to point to print. > > It is very common for an error in parentheses or brackets to lead to a > syntax error on the following line. > >>>> compile(""" > ... x = 1 > ... y = (x+1/2 > ... z = 3 > ... """, "", "exec") > Traceback (most recent call last): > ?File "", line 5, in > ?File "", line 4 > ? ?z = 3 > ? ?^ > SyntaxError: invalid syntax I was used to the tutorials from 3 showing the correct print(), and seeing the errors for print 'whatever', when i accidentally used them in 3, that I used them exclusively after seeing the 3 tutorials, and that it could be used as both a function and a statement(correct me if the term is wrong) in 2. It's important to note that had the op provided some code to go along with the error, that it might have been spotted more sooner by a less experienced eye. > > > -- > Steven > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From hugo.yoshi at gmail.com Tue Dec 21 06:00:15 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Tue, 21 Dec 2010 06:00:15 +0100 Subject: [Tutor] Problem with print In-Reply-To: References: <1292792096.27430.0.camel@nerdism> <4D0F2CD6.9060002@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 5:35 AM, David Hutto wrote: > > I was used to the tutorials from 3 showing the correct print(), and > seeing the errors for print 'whatever', when i accidentally used them > in 3, that I used them exclusively after seeing the 3 tutorials, and > that it could be used as both a function and a statement(correct me if > the term is wrong) in 2. > Note that print can actually only be used as a statement in python 2. It just so happens that you can include parentheses in the statement. Though that makes it look similar to a function call, it certainly is not. You'll see that when you try to supply multiple arguments to the "function." This makes python suddenly interpret the parentheses as a tuple, making it obviously not a function call. Hugo From smokefloat at gmail.com Tue Dec 21 06:08:12 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 00:08:12 -0500 Subject: [Tutor] Problem with print In-Reply-To: References: <1292792096.27430.0.camel@nerdism> <4D0F2CD6.9060002@pearwood.info> Message-ID: > > Note that print can actually only be used as a statement in python 2. > It just so happens that you can include parentheses in the statement. > Though that makes it look similar to a function call, it certainly is > not. You'll see that when you try to supply multiple arguments to the > "function." This makes python suddenly interpret the parentheses as a > tuple, making it obviously not a function call. However, it's declared as a NoneType in Python 3.1.2: >>> x = print('test','test1') test test1 >>> x >>> type(x) >>> I haven't gone further than that to test it's 'tupleness', or that might not have been what you meant by it being 'interpreted as a tuple'. > > Hugo From oberoc at gmail.com Tue Dec 21 06:10:18 2010 From: oberoc at gmail.com (Tino Dai) Date: Tue, 21 Dec 2010 00:10:18 -0500 Subject: [Tutor] Was: Trying to parse a HUGE(1gb) xml file in python Now: OT, Hackerspaces Message-ID: Hi Ashish, Check out Noisebridge ( https://www.*noisebridge*.net/) in SF. I think you will find there are like minded tech people there. It also has Mitch Altman ( http://en.wikipedia.org/wiki/*Mitch*_*Altman* ) is one of the founders/original members. -Tino -------------- next part -------------- An HTML attachment was scrubbed... URL: From hugo.yoshi at gmail.com Tue Dec 21 08:24:20 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Tue, 21 Dec 2010 08:24:20 +0100 Subject: [Tutor] Problem with print In-Reply-To: References: <1292792096.27430.0.camel@nerdism> <4D0F2CD6.9060002@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 6:08 AM, David Hutto wrote: >> >> Note that print can actually only be used as a statement in python 2. >> It just so happens that you can include parentheses in the statement. >> Though that makes it look similar to a function call, it certainly is >> not. You'll see that when you try to supply multiple arguments to the >> "function." This makes python suddenly interpret the parentheses as a >> tuple, making it obviously not a function call. > > However, it's declared as a NoneType in Python 3.1.2: > >>>> x = print('test','test1') > test test1 >>>> x >>>> type(x) > >>>> > > I haven't gone further than that to test it's 'tupleness', or that > might not have been what you meant by it being 'interpreted as a > tuple'. > Sorry, I haven't made myself clear. In python3, print is a function, and it return None. I'm not talking about python3 now, though. Everything below will refer to python 2.x In python2, print is a statement. The print statement in python2 can sometimes look like a function call, but it never is: Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) [GCC 4.4.5] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> print("hello") hello See? it looks like a function call, but it actually isn't. for proof, try to assign its return value: >>> a = print("hello") File "", line 1 a = print("hello") ^ SyntaxError: invalid syntax The parentheses that make this thing look like a function call so much are just regular old parentheses. This becomes apparent when we compare the output from these two statements: >>> print('a', 'b') ('a', 'b') >>> print 'a', 'b' a b >>> The parentheses are part of what's being printed now! That's because ('a', 'b') is interpreted as a tuple in this case. In python3, it would have been interpreted as a call to the print function, with arguments 'a' and 'b'. But in python2 you can't even have a function named print, because print is a keyword used in the print statement. Hugo From stefan_ml at behnel.de Tue Dec 21 09:44:10 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 09:44:10 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: <4D0FC877.9010705@pearwood.info> References: <4D0FC877.9010705@pearwood.info> Message-ID: [note that this has also been posted to comp.lang.python and discussed separately over there] Steven D'Aprano, 20.12.2010 22:19: > ashish makani wrote: > >> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file. > > I sympathize with you. I wonder who thought that building a 1GB XML file > was a good thing. > > Forget about using any XML parser that reads the entire file into memory. > By the time that 1GB of text is read and parsed, you will probably have > something about 6-8GB (estimated) in size. The in-memory size is highly dependent on the data, specifically the text-to-structure ratio. If it's a lot of text content, the difference to the serialised tree will be small. If it's a lot of structure with tiny bits of text content, the in-memory size of the tree will be a lot larger. >> I am guessing, as this happens (over the course of 20-30 mins), the tree >> representing is being slowly built in memory, but even after 30-40 mins, >> nothing happens. > > It's probably not finished. Leave it another hour or so and you'll get an > out of memory error. Right, if it gets into wild swapping, it can slow down almost to a halt, even though the XML parsing itself tends to have pretty good memory locality (but the ever growing in-memory tree obviously doesn't). >> 4. I then investigated some streaming libraries, but am confused - there is >> SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , the iterparse >> interface[http://effbot.org/zone/element-iterparse.htm], & several otehr >> options ( minidom) >> >> Which one is the best for my situation ? > > You absolutely need to use a streaming library. element-iterparse still > builds the tree, so that's no use to you. Wrong. iterparse() allows you to cut branches in the tree while it's growing, that's exactly what it's there for. > I believe you should use SAX or > minidom, but that's about my limit of knowledge of streaming XML parsers. With "minidom" being an advice that's even worse than SAX - SAX would at least solve the problem, whereas minidom wouldn't because of its intolerable memory requirements. Stefan From smokefloat at gmail.com Tue Dec 21 09:49:04 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 03:49:04 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 3:44 AM, Stefan Behnel wrote: > [note that this has also been posted to comp.lang.python and discussed > separately over there] > > Steven D'Aprano, 20.12.2010 22:19: >> >> ashish makani wrote: >> >>> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file. >> >> I sympathize with you. I wonder who thought that building a 1GB XML file >> was a good thing. David Mertz, Ph.D. Comparator, Gnosis Software, Inc. June 2003 http://gnosis.cx/publish/programming/xml_matters_29.html that was just the first listing: http://www.google.com/search?client=ubuntu&channel=fs&q=parsing+gigabyte+xml+python&ie=utf-8&oe=utf-8 >> >> Forget about using any XML parser that reads the entire file into memory. >> By the time that 1GB of text is read and parsed, you will probably have >> something about 6-8GB (estimated) in size. > > The in-memory size is highly dependent on the data, specifically the > text-to-structure ratio. If it's a lot of text content, the difference to > the serialised tree will be small. If it's a lot of structure with tiny bits > of text content, the in-memory size of the tree will be a lot larger. > > >>> I am guessing, as this happens (over the course of 20-30 mins), the tree >>> representing is being slowly built in memory, but even after 30-40 mins, >>> nothing happens. >> >> It's probably not finished. Leave it another hour or so and you'll get an >> out of memory error. > > Right, if it gets into wild swapping, it can slow down almost to a halt, > even though the XML parsing itself tends to have pretty good memory locality > (but the ever growing in-memory tree obviously doesn't). > > >>> 4. I then investigated some streaming libraries, but am confused - there >>> is >>> SAX[http://en.wikipedia.org/wiki/Simple_API_for_XML] , the iterparse >>> interface[http://effbot.org/zone/element-iterparse.htm], & several otehr >>> options ( minidom) >>> >>> Which one is the best for my situation ? >> >> You absolutely need to use a streaming library. element-iterparse still >> builds the tree, so that's no use to you. > > Wrong. iterparse() allows you to cut branches in the tree while it's > growing, that's exactly what it's there for. > > >> I believe you should use SAX or >> minidom, but that's about my limit of knowledge of streaming XML parsers. > > With "minidom" being an advice that's even worse than SAX - SAX would at > least solve the problem, whereas minidom wouldn't because of its intolerable > memory requirements. > > Stefan > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From stefan_ml at behnel.de Tue Dec 21 09:52:22 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 09:52:22 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: <201012202027.44992.cfuller084@thinkingplanet.net> References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: Chris Fuller, 21.12.2010 03:27: > This isn't XML, it's an abomination of XML. Best to not treat it as XML. > Good thing you're only after one class of tags. Here's what I'd do. I'll > give a general solution, but there are two parameters / four cases that could > make the code simpler, I'll just point them out at the end. > > Iterate over the file descriptor, reading in line-by-line. This will be slow > on a huge file, but probably not so bad if you're only doing it once. Note that it's not unlikely that this is actually *slower* than using a real XML parser: http://effbot.org/zone/celementtree.htm#benchmarks Stefan From smokefloat at gmail.com Tue Dec 21 09:53:51 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 03:53:51 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: But then again, maybe it's too much of an optimization for someone not optimizing for others or a specific application for the hardware, or it's not part of the standard python library, and therefore, expendable. From smokefloat at gmail.com Tue Dec 21 09:55:45 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 03:55:45 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 3:52 AM, Stefan Behnel wrote: > Chris Fuller, 21.12.2010 03:27: >> >> This isn't XML, it's an abomination of XML. ?Best to not treat it as XML. >> Good thing you're only after one class of tags. ?Here's what I'd do. ?I'll >> give a general solution, but there are two parameters / four cases that >> could >> make the code simpler, I'll just point them out at the end. >> >> Iterate over the file descriptor, reading in line-by-line. ?This will be >> slow >> on a huge file, but probably not so bad if you're only doing it once. > > Note that it's not unlikely that this is actually *slower* than using a real > XML parser: > Or a 'real' language like C or C++ maybe to increase, or in Python's case, bypass, the interpreter? > http://effbot.org/zone/celementtree.htm#benchmarks > From smokefloat at gmail.com Tue Dec 21 09:56:16 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 03:56:16 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 3:55 AM, David Hutto wrote: > On Tue, Dec 21, 2010 at 3:52 AM, Stefan Behnel wrote: >> Chris Fuller, 21.12.2010 03:27: >>> >>> This isn't XML, it's an abomination of XML. ?Best to not treat it as XML. >>> Good thing you're only after one class of tags. ?Here's what I'd do. ?I'll >>> give a general solution, but there are two parameters / four cases that >>> could >>> make the code simpler, I'll just point them out at the end. >>> >>> Iterate over the file descriptor, reading in line-by-line. ?This will be >>> slow >>> on a huge file, but probably not so bad if you're only doing it once. >> >> Note that it's not unlikely that this is actually *slower* than using a real >> XML parser: >> > > Or a 'real' language like C or C++ maybe to increase, or in Python's > case, bypass, the interpreter? Which is *faster*. > > >> http://effbot.org/zone/celementtree.htm#benchmarks >> > -- They're installing the breathalyzer on my email account next week. From smokefloat at gmail.com Tue Dec 21 09:59:47 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 03:59:47 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: And from what I recall XML is intended for data transfer in respect to HTML(from a recent brushup, nothing more), so not having used it, it sure has been displayed as a data transfer mechanism, I remember this from using Joomla's framework, and the xml files for menus I think. From stefan_ml at behnel.de Tue Dec 21 09:57:03 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 09:57:03 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: David Hutto, 21.12.2010 09:49: >> Steven D'Aprano, 20.12.2010 22:19: >>> >>> ashish makani wrote: >>> >>>> Goal : I am trying to parse a ginormous ( ~ 1gb) xml file. >>> >>> I sympathize with you. I wonder who thought that building a 1GB XML file >>> was a good thing. > > http://gnosis.cx/publish/programming/xml_matters_29.html Fredrik Lundh's cElementTree page has a benchmark for that, too. It's actually slower than cElementTree for the case he tested (which was basically "parsing" :) http://effbot.org/zone/celementtree.htm#benchmarks Stefan From smokefloat at gmail.com Tue Dec 21 10:06:46 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 04:06:46 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 3:59 AM, David Hutto wrote: > And from what I recall XML is intended for data transfer in respect to > HTML(from a recent brushup, nothing more), Apologies that is browser based transfer, (not sure what more, although I think it means any data tranfer) so not having used it, it > sure has been displayed as a data transfer mechanism, I remember this > from using Joomla's framework, and the xml files for menus I think. > From stefan_ml at behnel.de Tue Dec 21 10:10:52 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 10:10:52 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: David Hutto, 21.12.2010 09:55: > On Tue, Dec 21, 2010 at 3:52 AM, Stefan Behnel wrote: >> Chris Fuller, 21.12.2010 03:27: >>> >>> This isn't XML, it's an abomination of XML. Best to not treat it as XML. >>> Good thing you're only after one class of tags. Here's what I'd do. I'll >>> give a general solution, but there are two parameters / four cases that >>> could >>> make the code simpler, I'll just point them out at the end. >>> >>> Iterate over the file descriptor, reading in line-by-line. This will be >>> slow >>> on a huge file, but probably not so bad if you're only doing it once. >> >> Note that it's not unlikely that this is actually *slower* than using a real >> XML parser: > > Or a 'real' language like C or C++ maybe to increase, or in Python's > case, bypass, the interpreter? While this may be a little faster than Python code (although I suspect that benchmarking is needed to prove either way), I doubt that it's worth the overhead in code writing. If I can write a couple of lines of Python code that are easy to validate and almost as fast as C code, why would I want to write and debug hundreds of lines of code in C or C++, just to see that I need to tune my benchmark to notice the difference? But then, people even write XML handling code in Java, where neither performance nor code size is a suitable argument. Stefan From smokefloat at gmail.com Tue Dec 21 10:12:55 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 04:12:55 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: . >>>> >>>> I sympathize with you. I wonder who thought that building a 1GB XML file >>>> was a good thing. If it is: XML stands for eXtensible Markup Language. XML is designed to transport and store data. Then what other file medium would you suggest as the tagging means. You have a file with tags, you can't parse and store the data in any file anymore than the next, right? So the tags and how they are marked by any module or file extension searcher shouldn't matter, right? >> >> http://gnosis.cx/publish/programming/xml_matters_29.html > > Fredrik Lundh's cElementTree page has a benchmark for that, too. It's > actually slower than cElementTree for the case he tested (which was > basically "parsing" :) > > http://effbot.org/zone/celementtree.htm#benchmarks > > Stefan > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From smokefloat at gmail.com Tue Dec 21 10:17:31 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 04:17:31 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 4:10 AM, Stefan Behnel wrote: > David Hutto, 21.12.2010 09:55: >> >> On Tue, Dec 21, 2010 at 3:52 AM, Stefan Behnel wrote: >>> >>> Chris Fuller, 21.12.2010 03:27: >>>> >>>> This isn't XML, it's an abomination of XML. ?Best to not treat it as >>>> XML. >>>> Good thing you're only after one class of tags. ?Here's what I'd do. >>>> ?I'll >>>> give a general solution, but there are two parameters / four cases that >>>> could >>>> make the code simpler, I'll just point them out at the end. >>>> >>>> Iterate over the file descriptor, reading in line-by-line. ?This will be >>>> slow >>>> on a huge file, but probably not so bad if you're only doing it once. >>> >>> Note that it's not unlikely that this is actually *slower* than using a >>> real >>> XML parser: >> >> Or a 'real' language like C or C++ maybe to increase, or in Python's >> case, bypass, the interpreter? > > While this may be a little faster than Python code (although I suspect that > benchmarking is needed to prove either way), I doubt that it's worth the > overhead in code writing. If I can write a couple of lines of Python code > that are easy to validate and almost as fast as C code, why would I want to > write and debug hundreds of lines of code in C or C++, just to see that I > need to tune my benchmark to notice the difference? Don't get me wrong, I love the simplicity too, but if you know you really do need it along the way, then you should start thinking ahead of the easy, and toward the harder code for your project. Just as every language has it's place, so does Python. > > But then, people even write XML handling code in Java, where neither > performance nor code size is a suitable argument. > > Stefan > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From smokefloat at gmail.com Tue Dec 21 10:19:26 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 04:19:26 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 4:17 AM, David Hutto wrote: > On Tue, Dec 21, 2010 at 4:10 AM, Stefan Behnel wrote: >> David Hutto, 21.12.2010 09:55: >>> >>> On Tue, Dec 21, 2010 at 3:52 AM, Stefan Behnel wrote: >>>> >>>> Chris Fuller, 21.12.2010 03:27: >>>>> >>>>> This isn't XML, it's an abomination of XML. ?Best to not treat it as >>>>> XML. >>>>> Good thing you're only after one class of tags. ?Here's what I'd do. >>>>> ?I'll >>>>> give a general solution, but there are two parameters / four cases that >>>>> could >>>>> make the code simpler, I'll just point them out at the end. >>>>> >>>>> Iterate over the file descriptor, reading in line-by-line. ?This will be >>>>> slow >>>>> on a huge file, but probably not so bad if you're only doing it once. >>>> >>>> Note that it's not unlikely that this is actually *slower* than using a >>>> real >>>> XML parser: >>> >>> Or a 'real' language like C or C++ maybe to increase, or in Python's >>> case, bypass, the interpreter? >> >> While this may be a little faster than Python code (although I suspect that >> benchmarking is needed to prove either way), I doubt that it's worth the >> overhead in code writing. If I can write a couple of lines of Python code >> that are easy to validate and almost as fast as C code, why would I want to >> write and debug hundreds of lines of code in C or C++, just to see that I >> need to tune my benchmark to notice the difference? > > Don't get me wrong, I love the simplicity too, but if you know you > really do need it along the way, then you should start thinking ahead > of the easy, and toward the harder code for your project. Just as > every language has it's place, so does Python. If I want to write a programming language, It might not be the best idea to have a labguage needed for speed based on Python, I should maybe use wha it's based on, or refine my own optimizations, just to be a little clearer about my perspective. > > >> >> But then, people even write XML handling code in Java, where neither >> performance nor code size is a suitable argument. >> >> Stefan >> >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > > -- > They're installing the breathalyzer on my email account next week. > -- They're installing the breathalyzer on my email account next week. From yasinyaqoobi at gmail.com Tue Dec 21 10:06:52 2010 From: yasinyaqoobi at gmail.com (Yasin Yaqoobi) Date: Tue, 21 Dec 2010 02:06:52 -0700 Subject: [Tutor] Hangman game.....problem putting strings in a list..... Message-ID: <4D106E2B.2030408@gmail.com> global line global index; guessed = ["-"]; count = 0; wrong = 0; def guess(letter): global guessed if (letter in line): index = line.index(letter); print guessed; # This is the line that gives me the error don't know why? guessed[index] = " " + (letter); ,TypeError: 'str' object does not support item assignment guessed[index] = (letter); print ' '.join(guessed) else: global wrong; wrong += 1; def draw(number): if (number == 1): print " O "; elif(number == 2): print " O "; print " | "; elif (number == 3): print " O "; print " \| "; elif (number == 4): print " O "; print " \|/ "; elif (number == 5): print " O "; print " \|/ "; print " | "; elif (number == 6): print " O "; print " \|/ "; print " | "; print " / "; elif (number == 7): print " O "; print " \|/ "; print " | "; print " / \ "; print "Sorry you Lost! " def doit(): global count while(wrong != 7): a_letter = raw_input("Pick a letter --> ") print guess(a_letter); draw(wrong); print count += 1 def initArray(): global guessed print line guessed = guessed[0] * (len(line)-1) print "this is new list " + guessed; while 1: line = file.readline(); if (len(line) >= 5): initArray() doit(); break if not line: break file.close() From stefan_ml at behnel.de Tue Dec 21 10:28:38 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 10:28:38 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: Hi, I wonder why you reply to my e-mail without replying to what I wrote in it. David Hutto, 21.12.2010 10:12: > . >>>>> I sympathize with you. I wonder who thought that building a 1GB XML file >>>>> was a good thing. This was written by Steven D'Aprano. > If it is: > > XML stands for eXtensible Markup Language. > > XML is designed to transport and store data. > > > Then what other file medium would you suggest as the tagging means. There are different file formats for structured and semi-structured data. XML certainly isn't the only one, and people have been defining specific formats for their specific use cases for ages, for better or worse each time. Personally, I don't think GB-sized XML files are bad per-se. It depends on the use case, and it depends on what's considered a suitable solution in a given environment. Also note that XML tends to compress pretty well, and that it's sometimes faster to parse gzipped XML than uncompressed XML. So the serialised file size by itself isn't an argument, either. > You have a file with tags, you can't parse and store the data in any > file anymore than the next, right? > > So the tags and how they are marked by any module or file extension > searcher shouldn't matter, right? I don't think I can extract the intended meaning from the assembled words you use here. Stefan From smokefloat at gmail.com Tue Dec 21 10:29:06 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 04:29:06 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: File = string going through string code finding pieces of the string and marking the territory. I don't see 'real' optimization other than rolling your own. From stefan_ml at behnel.de Tue Dec 21 10:34:23 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 10:34:23 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: David Hutto, 21.12.2010 10:19: > On Tue, Dec 21, 2010 at 4:17 AM, David Hutto wrote: >> On Tue, Dec 21, 2010 at 4:10 AM, Stefan Behnel wrote: >>>>> Note that it's not unlikely that this is actually *slower* than using a >>>>> real XML parser: >>>> >>>> Or a 'real' language like C or C++ maybe to increase, or in Python's >>>> case, bypass, the interpreter? >>> >>> While this may be a little faster than Python code (although I suspect that >>> benchmarking is needed to prove either way), I doubt that it's worth the >>> overhead in code writing. If I can write a couple of lines of Python code >>> that are easy to validate and almost as fast as C code, why would I want to >>> write and debug hundreds of lines of code in C or C++, just to see that I >>> need to tune my benchmark to notice the difference? >> >> Don't get me wrong, I love the simplicity too, but if you know you >> really do need it along the way, then you should start thinking ahead >> of the easy, and toward the harder code for your project. Just as >> every language has it's place, so does Python. Premature optimisation is the root of all evil. That totally applies when choosing a programming language. > If I want to write a programming language, It might not be the best > idea to have a labguage needed for speed based on Python, I should > maybe use wha it's based on, or refine my own optimizations, just to > be a little clearer about my perspective. Being clearer would certainly help in understanding your postings. Stefan From smokefloat at gmail.com Tue Dec 21 10:37:49 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 04:37:49 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 4:28 AM, Stefan Behnel wrote: > Hi, > > I wonder why you reply to my e-mail without replying to what I wrote in it. > > > David Hutto, 21.12.2010 10:12: >> >> . >>>>>> >>>>>> I sympathize with you. I wonder who thought that building a 1GB XML >>>>>> file >>>>>> was a good thing. > > This was written by Steven D'Aprano. > My bad, human parsing has errors too. > >> If it is: >> >> XML stands for eXtensible Markup Language. >> >> XML is designed to transport and store data. >> >> >> Then what other file medium would you suggest as the tagging means. > > There are different file formats for structured and semi-structured data. > XML certainly isn't the only one, and people have been defining specific > formats for their specific use cases for ages, for better or worse each > time. But it's all a string of coded text with only the formats that define the markups within though. String format + text in file(type of coding for lang) > > Personally, I don't think GB-sized XML files are bad per-se. It depends on > the use case, and it depends on what's considered a suitable solution in a > given environment. Also note that XML tends to compress pretty well, and > that it's sometimes faster to parse gzipped XML than uncompressed XML. So > the serialised file size by itself isn't an argument, either. So the zipped file in compressed doesn't contain compressed tags, or data, then why is it compressed? > > >> You have a file with tags, you can't parse and store the data in any >> file anymore than the next, right? >> >> So the tags and how they are marked by any module or file extension >> searcher shouldn't matter, right? > The phrase: in a php file in a xml file in an html file. if read in any file it's the same, as How does the file extension make it any longer? This is know matter how it's interpreted by any other mechanism than just reading the text within, right? > I don't think I can extract the intended meaning from the assembled words > you use here. > > Stefan > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From stefan_ml at behnel.de Tue Dec 21 10:43:35 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 10:43:35 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: David Hutto, 21.12.2010 10:29: > File = string > > going through string code > > finding pieces of the string and marking the territory. > > > I don't see 'real' optimization other than rolling your own. Reads like a Haiku. Doesn't quite fit the verse, though. From your behaviour, I get the impression that you are just trolling. Stefan From smokefloat at gmail.com Tue Dec 21 10:46:13 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 04:46:13 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 4:34 AM, Stefan Behnel wrote: > David Hutto, 21.12.2010 10:19: >> >> On Tue, Dec 21, 2010 at 4:17 AM, David Hutto wrote: >>> >>> On Tue, Dec 21, 2010 at 4:10 AM, Stefan Behnel wrote: >>>>>> >>>>>> Note that it's not unlikely that this is actually *slower* than using >>>>>> a >>>>>> real XML parser: >>>>> >>>>> Or a 'real' language like C or C++ maybe to increase, or in Python's >>>>> case, bypass, the interpreter? >>>> >>>> While this may be a little faster than Python code (although I suspect >>>> that >>>> benchmarking is needed to prove either way), I doubt that it's worth the >>>> overhead in code writing. If I can write a couple of lines of Python >>>> code >>>> that are easy to validate and almost as fast as C code, why would I want >>>> to >>>> write and debug hundreds of lines of code in C or C++, just to see that >>>> I >>>> need to tune my benchmark to notice the difference? >>> >>> Don't get me wrong, I love the simplicity too, but if you know you >>> really do need it along the way, then you should start thinking ahead >>> of the easy, and toward the harder code for your project. Just as >>> every language has it's place, so does Python. > > Premature optimisation is the root of all evil. That totally applies when > choosing a programming language. Not premature design, but being pre mature when selecting. Do you utilize python for aviation? you could, but modeling would be better. However, you'll just have to learn another language in order to optimize the end means. I know python has it's own optimizations, but it's still interpreting to the command line. A dog fight between two f-15's would certainly notice the response rate when pulling out of a intersect course. > > >> If I want to write a programming language, It might not be the best >> idea to have a labguage needed for speed based on Python, I should >> maybe use wha it's based on, or refine my own optimizations, just to >> be a little clearer about my perspective. > > Being clearer would certainly help in understanding your postings. What's the difference between a language based on C++ Python, and C? If I used any of the above to begin writing my own language, which of the above would be faster(other languages aside) to begin with? From alan.gauld at btinternet.com Tue Dec 21 10:46:15 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 09:46:15 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: "David Hutto" wrote >> And from what I recall XML is intended for data transfer in respect >> to >> HTML(from a recent brushup, nothing more), > > Apologies that is browser based transfer, I'm not sure what that last bit means. XML is a self-describing data format. It is usually used for files but can be used in data streams or in-memory strings. It's natural competitors are TLV (Tag,Lenth,Value) and CSV(Comma Seperated Value) files but neither is as rich in structure. Alternative options include ASN.1, Edifact and IDL but these are not self-describing(*) (although they are all more compact and faster to parse, but only IDL is free.) >> sure has been displayed as a data transfer mechanism, You don't have to use it for data transfer - eg MS's use as a document storage format in Office - but frankly if you use XML to store large volumes of data you are mad, a database is a much more sensible option being far more space efficient and faster to work with. (*)ASN.1, IDL etc all rely on a shared definition, and often shared code library, at both sender and receiver. The library is a compiled version of the data definition which enables complex data structures to be read from the file in a single chunk very efficiently. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Dec 21 10:49:42 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 09:49:42 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: "David Hutto" wrote > > Note that it's not unlikely that this is actually *slower* than > > using a real > > XML parser: > > Or a 'real' language like C or C++ maybe to increase, or in Python's > case, bypass, the interpreter? Most of the Python xml parsers are written in C - many use the industry standard expat parser - so converting to C would bring minimal speed advantage and a lot of extra work. Alan G. From smokefloat at gmail.com Tue Dec 21 10:50:07 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 04:50:07 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 4:43 AM, Stefan Behnel wrote: > David Hutto, 21.12.2010 10:29: >> >> File = string A file is a string of character encoded in it's format >> >> going through string code Code that goes through the file format and the encoding >> >> finding pieces of the string and marking the territory. What sequence of characters qualifies for a target select >> >> >> I don't see 'real' optimization other than rolling your own. encoding + characters in file + what's where in the file > > Reads like a Haiku. Doesn't quite fit the verse, though. > > From your behaviour, I get the impression that you are just trolling. The water always flows freely down here, Plus the traffics light. > > Stefan > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From alan.gauld at btinternet.com Tue Dec 21 10:58:34 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 09:58:34 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: <4D0FC877.9010705@pearwood.info> Message-ID: "David Hutto" wrote >> I sympathize with you. I wonder who thought that building a 1GB XML >> file >> was a good thing. > that was just the first listing: > > http://www.google.com/search?client=ubuntu&channel=fs&q=parsing+gigabyte+xml+python&ie=utf-8&oe=utf-8 Eeek! One of the listings says: > 22 Jan 2009 ... Stripping Illegal Characters from XML in Python >> ... I'd be asking Python to process 6.4 gigabytes of CSV into 6.5 gigabytes of XML 1. ..... In fact, what happened was that the parsing didn't work and the whole db was ... And I thought a 1G file was extreme... Do these people stop to think that with XML as much as 80% of their "data" is just description (ie the tags). Alan G. From stefan_ml at behnel.de Tue Dec 21 11:00:03 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 11:00:03 +0100 Subject: [Tutor] Writing a programming language in Python (was: Trying to parse a HUGE(1gb) xml file in python) In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: David Hutto, 21.12.2010 10:46: > On Tue, Dec 21, 2010 at 4:34 AM, Stefan Behnel wrote: >> David Hutto, 21.12.2010 10:19: >>> If I want to write a programming language, It might not be the best >>> idea to have a labguage needed for speed based on Python, I should >>> maybe use wha it's based on, or refine my own optimizations, just to >>> be a little clearer about my perspective. >> >> Being clearer would certainly help in understanding your postings. > > What's the difference between a language based on C++ Python, and C? > > If I used any of the above to begin writing my own language, which of > the above would be faster(other languages aside) to begin with? Certainly Python. The Cython compiler is written in Python, for example. It translates Python to C. It would have been a lot less fun to write it in C, and it would totally have made the project progress a lot slower. PyPy and ShedSkin are other famous examples to name here, even though both are written in restricted versions of Python (not sure about ShedSkin, but if it compiles itself, it must be). Seriously, if you want to write a programming language, the best advice I can give is to write it in Python. Stefan From smokefloat at gmail.com Tue Dec 21 11:00:57 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 05:00:57 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 4:46 AM, Alan Gauld wrote: > "David Hutto" wrote > >>> And from what I recall XML is intended for data transfer in respect to >>> HTML(from a recent brushup, nothing more), >> >> Apologies that is browser based transfer, > > I'm not sure what that last bit means. > XML is a self-describing data format. It is usually used for files > but can be used in data streams or in-memory strings. I know it's self tagged, meaning you create the tags within, and that it's used elsewhere as a form of data transfer, my previous usage with the particular file format was browser based in usage, but I know it's used in many other places, which is why I didn't see the meaning of the discussion saying it was horrible to use, I just asked for any alternative suggestions for files, since everyone 'seemed' to have a bad view of the usage, since it seems to be the standard for user defined tags for data transfer. > > It's natural competitors are TLV (Tag,Lenth,Value) and > CSV(Comma Seperated Value) files but neither is as rich > in structure. That was kind of my point, I've seen all but TLV in use, but XML is the web standard it seems. Alternative options include ASN.1, Edifact and > IDL but these are not self-describing(*) (although they are all > more compact and faster to parse, but only IDL is free Haven't heard of these, but formula of file, it seems to me, is encoding + extension + text, how much can these really differ. On average it seems that the self defined tags of xml, would have a bigger impact on the average usage(someone has larger tag sizes, and more tags) than a defined file with averaged tags. > >>> sure has been displayed as a data transfer mechanism, > > You don't have to use it for data transfer - eg MS's use > as a document storage format in Office - but frankly if > you use XML to store large volumes of data you are mad, > a database is a much more sensible option being far more > space efficient and faster to work with. If truly optimizing, I would time both, and maybe move to a different language, or pattern if it truly mattered. > > (*)ASN.1, IDL etc all rely on a shared definition, and > often shared code library, at both sender and receiver. > The library is a compiled version of the data definition > which enables complex data structures to be read from > the file in a single chunk very efficiently. This I might have to work on, but I rely on experience to quasi-trust experience. From smokefloat at gmail.com Tue Dec 21 11:04:49 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 05:04:49 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 4:49 AM, Alan Gauld wrote: > > "David Hutto" wrote > >> > Note that it's not unlikely that this is actually *slower* than > using >> > a real >> > XML parser: >> >> Or a 'real' language like C or C++ maybe to increase, or in Python's >> case, bypass, the interpreter? > > Most of the Python xml parsers are written in C - many use the > industry standard expat parser - so converting to C would bring > minimal speed advantage and a lot of extra work. Somewhat of the fact that python uses C encourages me of that, but I have still been looking into c++ to optimize, because I've used it before, and the more languages I learn the more they feel 'similar', but the same, if you can understand that! From alan.gauld at btinternet.com Tue Dec 21 11:03:19 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 10:03:19 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: <4D0FC877.9010705@pearwood.info> Message-ID: "David Hutto" wrote > XML stands for eXtensible Markup Language. > XML is designed to transport and store data. > > Then what other file medium would you suggest as the tagging means. See my other post but there are many alternatives that are orders of magnitude more efficient. XML is one of the most inefficient data transport mechanisms ever invented and its main redeeming feature is its human readability. > You have a file with tags, you can't parse and store the data in any > file anymore than the next, right? Wrong, even CSV files are more efficient than parsing XML. (But are very limited in their data structure) But binary based formats like IDL and ASN.1 can be parsed very efficiently and, because they are binary based, store (and therefore transmit) their data much more efficiently too. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From smokefloat at gmail.com Tue Dec 21 11:09:18 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 05:09:18 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 4:58 AM, Alan Gauld wrote: > > "David Hutto" wrote > >>> I sympathize with you. I wonder who thought that building a 1GB XML file >>> was a good thing. > >> that was just the first listing: >> >> >> http://www.google.com/search?client=ubuntu&channel=fs&q=parsing+gigabyte+xml+python&ie=utf-8&oe=utf-8 > > Eeek! One of the listings says: > >> 22 Jan 2009 ... Stripping Illegal Characters from XML in Python >> > > ... I'd be asking Python to process 6.4 gigabytes of CSV into > 6.5 gigabytes of XML 1. ..... In fact, what happened was that > the parsing didn't work and the whole db was ... > > And I thought a 1G file was extreme... Do these people stop to think that > with XML as much as 80% of their "data" is just description (ie the tags). That';s what I saying above that xml seems to be the hog in terms of it's user defined tags. Is that somewhat a confirmation of my hunch, that it's the length of the users predefined tags that add to the above mess, and that maybe a lessened tag system in accordance with xml might be better, or a simple tag tag in the xml(other files) with an index to point to a and b would be better. From alan.gauld at btinternet.com Tue Dec 21 11:05:29 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 10:05:29 -0000 Subject: [Tutor] decimal input problem References: <1292905583.10115.3.camel@nerdism> Message-ID: "jtl999" wrote > when I try to multiply with a decimal number in python with the > input > this is what i get > > Enter first number: 1.2 > Traceback (most recent call last): > File "Timesed.py", line 18, in > numberx1 = (int)(raw_input('Enter first number: ')) > ValueError: invalid literal for int() with base 10: '1.2' You are inputting a floating point number - 1.2 and trying to convert it to an integer. You need to convert raw_input to a float() rather than an int() HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From smokefloat at gmail.com Tue Dec 21 11:16:48 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 05:16:48 -0500 Subject: [Tutor] Writing a programming language in Python (was: Trying to parse a HUGE(1gb) xml file in python) In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 5:00 AM, Stefan Behnel wrote: > David Hutto, 21.12.2010 10:46: >> >> On Tue, Dec 21, 2010 at 4:34 AM, Stefan Behnel wrote: >>> >>> David Hutto, 21.12.2010 10:19: >>>> >>>> If I want to write a programming language, It might not be the best >>>> idea to have a labguage needed for speed based on Python, I should >>>> maybe use wha it's based on, or refine my own optimizations, just to >>>> be a little clearer about my perspective. >>> >>> Being clearer would certainly help in understanding your postings. >> >> What's the difference between a language based on C++ Python, and C? >> >> If I used any of the above to begin writing my own language, which of >> the above would be faster(other languages aside) to begin with? > > Certainly Python. The Cython compiler is written in Python, for example. It > translates Python to C. It would have been a lot less fun to write it in C, > and it would totally have made the project progress a lot slower. > > PyPy and ShedSkin are other famous examples to name here, even though both > are written in restricted versions of Python (not sure about ShedSkin, but > if it compiles itself, it must be). > > Seriously, if you want to write a programming language, the best advice I > can give is to write it in Python. Simpler yeah, Several implementations of python ring a bell with that, and I understand it's .pyc so it's a compiled file and ready for usage as 'anyother'(I might be wrong on this, but sure it's the same as converting the original py file straight back to c). Every language has it's benefits, and it's devout worshippers, I love python, I even give credit to Guido for inspiring my own language deviation in C++. I like cross language diversity, so I do things everywhere, and I see noone complaining all the time about any one language, nor it's advantages to them, especially if they know it and use it. From stefan_ml at behnel.de Tue Dec 21 11:19:09 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 11:19:09 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: Alan Gauld, 21.12.2010 10:58: > "David Hutto" wrote >> http://www.google.com/search?client=ubuntu&channel=fs&q=parsing+gigabyte+xml+python&ie=utf-8&oe=utf-8 > > Eeek! One of the listings says: > >> 22 Jan 2009 ... Stripping Illegal Characters from XML in Python >> > ... I'd be asking Python to process 6.4 gigabytes of CSV into > 6.5 gigabytes of XML 1. ..... In fact, what happened was that > the parsing didn't work and the whole db was ... > > And I thought a 1G file was extreme... Do these people stop to think that > with XML as much as 80% of their "data" is just description (ie the tags). As I already said, it compresses well. In run-length compressed XML files, the tags can easily take up a negligible amount of space compared to the more widely varying data content (although that also commonly tends to compress rather well). And depending on how fast your underlying storage is, decompressing and parsing the file may still be faster than parsing a huge uncompressed file directly. So, again, the shear uncompressed file size is *not* a very interesting argument. Stefan From alan.gauld at btinternet.com Tue Dec 21 11:21:14 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 10:21:14 -0000 Subject: [Tutor] Hangman game.....problem putting strings in a list..... References: <4D106E2B.2030408@gmail.com> Message-ID: "Yasin Yaqoobi" wrote I'm confused. The error message you describe doesn't appear to match any line in your code. Please provide the full error printout not just a single line. Meanwhile some comments... > global line > global index; global is not doing anything here, it is only effective inside a function. Try not to use global variables unless you have to. Specifically only for data that's shared between functions, and even then it's usually better practice to pass the values into the functions as arghuments. > guessed = ["-"]; > count = 0; > wrong = 0; > > def guess(letter): > global guessed > if (letter in line): You don't need the parens, they don't do any harm, but they aren't needed. > index = line.index(letter); > print guessed; > # This is the line that gives me the error don't know why? > guessed[index] = " " + (letter); ,TypeError: 'str' object does not > support item assignment > guessed[index] = (letter); Again, you don't need the parens... And I suspect you really want to use append() here rather than assigning to guessed[index]. > print ' '.join(guessed) > else: > global wrong; > wrong += 1; > > > def draw(number):... > def doit(): > global count > while(wrong != 7): > a_letter = raw_input("Pick a letter --> ") > print > guess(a_letter); > draw(wrong); > print > count += 1 > > def initArray(): > global guessed > print line > guessed = guessed[0] * (len(line)-1) > print "this is new list " + guessed; If you use the append() method you don't need this. > while 1: > line = file.readline(); > if (len(line) >= 5): > initArray() > doit(); > break > if not line: break > > file.close() HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From smokefloat at gmail.com Tue Dec 21 11:22:36 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 05:22:36 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: Give me a little time to review this when it's not 5:30 in the morning and I've been up since 9 am yesterday, and 'relearning' c++:) But it still seems that you have have coding + filetype + charactersinfileinformat., one long string that has to be parsed by the C functions. From stefan_ml at behnel.de Tue Dec 21 11:22:36 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 11:22:36 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: Alan Gauld, 21.12.2010 10:46: > You don't have to use it for data transfer - eg MS's use > as a document storage format in Office - but frankly if > you use XML to store large volumes of data you are mad, > a database is a much more sensible option being far more > space efficient and faster to work with. Even "storing large volumes of data" in XML can be perfectly ok. It depends on the use case. Database storage formats are not generally portable, for example, but they provide fast online access. Totally different use cases. Nothing's inherently wrong with storing large amounts of data in (compressed) XML for medium to long term storage or data exchange, and loading them back into a database to make the data quickly accessible for heavy processing. Stefan From smokefloat at gmail.com Tue Dec 21 11:29:07 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 05:29:07 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 5:19 AM, Stefan Behnel wrote: > Alan Gauld, 21.12.2010 10:58: >> >> "David Hutto" wrote >>> >>> >>> http://www.google.com/search?client=ubuntu&channel=fs&q=parsing+gigabyte+xml+python&ie=utf-8&oe=utf-8 >> >> Eeek! One of the listings says: >> >>> 22 Jan 2009 ... Stripping Illegal Characters from XML in Python >> >> >> ... I'd be asking Python to process 6.4 gigabytes of CSV into >> 6.5 gigabytes of XML 1. ..... In fact, what happened was that >> the parsing didn't work and the whole db was ... >> >> And I thought a 1G file was extreme... Do these people stop to think that >> with XML as much as 80% of their "data" is just description (ie the tags). > > As I already said, it compresses well. In run-length compressed XML files, > the tags can easily take up a negligible amount of space compared to the > more widely varying data content (although that also commonly tends to > compress rather well). And depending on how fast your underlying storage is, > decompressing and parsing the file may still be faster than parsing a huge > uncompressed file directly. So, again, the shear uncompressed file size is > *not* a very interesting argument. > However, could they (as mentioned elsewhere, and by other in another form)mitigate the damage by using smaller tags exclusively? And also compressed is formatted, even for the tags, correct? From stefan_ml at behnel.de Tue Dec 21 11:25:32 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 11:25:32 +0100 Subject: [Tutor] Writing a programming language in Python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: David Hutto, 21.12.2010 11:16: > I understand it's .pyc so it's a compiled file and ready for usage > as 'anyother'(I might be wrong on this, but sure it's the same as > converting the original py file straight back to c). ".pyc" files have nothing to do with C. They are just compiled byte code, and not even portable to other Python runtimes (and sometimes not even between CPython versions). Stefan From alan.gauld at btinternet.com Tue Dec 21 11:30:21 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 10:30:21 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: "David Hutto" wrote >> (*)ASN.1, IDL etc all rely on a shared definition, and >> often shared code library, at both sender and receiver. > > This I might have to work on, but I rely on experience to > quasi-trust > experience. These are all data transport formats agreed and standardised long before XML appeared. IDL is the format used in COM calls for example and RPC calls between processes on an OS or across a network. It is an OpenGroup standard I believe. ASN.1 is a binary form and used in eCommerce and telecomms networks for many years. It is standardised by the ITU Edifact is the data standard of EDI and is set by the UN. It has been used for commercial trading between large corporates for many years. All of these standards developed when network bandwidth was very expensive so they all major on efficiency. XML was developed by non networks-oriented people for the ease of writing software for the web. Bandwidth was not a primary concern to them. There are other formats too, because the problem of transporting data portably between computers has been with us since the dawn of networking. XML just happens to be the most popular format today. But popularity doesn't necessarily mean its good. :-) -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Tue Dec 21 11:35:00 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 10:35:00 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: "David Hutto" wrote > Somewhat of the fact that python uses C encourages me of that, but I > have still been looking into c++ to optimize, because I've used it > before, and the more languages I learn the more they feel 'similar', > but the same, if you can understand that! Absolutely! That's why I use 3 languages in my tutor. To try and dispel the myth that programming languages are hard to learn. Once you can program in one language learning another normally takes a few days or even hours. (Becoming fluent is another matter! :-) There are probably only about 5 or 6 basic structures to programming languages: Algol based (Pascal, C, Java, Python etc) Lisp based (Lisp, Scheme, clojure etc) Prolog based Functional( Haskell, ML etc) SQL based others?... -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From smokefloat at gmail.com Tue Dec 21 11:44:18 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 05:44:18 -0500 Subject: [Tutor] Writing a programming language in Python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 5:25 AM, Stefan Behnel wrote: > David Hutto, 21.12.2010 11:16: >> >> I understand it's .pyc so it's a compiled file and ready for usage >> as 'anyother'(I might be wrong on this, but sure it's the same as >> converting the original py file straight back to c). > > ".pyc" files have nothing to do with C. They are just compiled byte code, > and not even portable to other Python runtimes (and sometimes not even > between CPython versions). I meant immediately available upon module calls on your own pc. Like when you get a module that has them already compiled for your architecture. From alan.gauld at btinternet.com Tue Dec 21 11:45:22 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 10:45:22 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: <4D0FC877.9010705@pearwood.info> Message-ID: "David Hutto" wrote > That';s what I saying above that xml seems to be the hog in terms of > it's user defined tags. Is that somewhat a confirmation of my hunch, > that it's the length of the users predefined tags that add to the > above mess, and that maybe a lessened tag system in accordance with > xml might be better, or a simple tag tag in the xml(other > files) with an index to point to a and b would be better. Shorter tags reduce the data volume by a bit (and it can be a big bit if the names are all 20 characters long!) but the inherent tag structure, even with single char names will still often surpass the data content. 5 8 bytes to describe an int which could be represented in a single byte in binary (or even in CSV). Even if the int were a 64bit binary value (8 bytes) the minimal tag structure still consumes the same data width. Of course if the data content is a long string then simple tags become cost effective (think

in XHTML)... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From smokefloat at gmail.com Tue Dec 21 11:47:52 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 05:47:52 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: On Tue, Dec 21, 2010 at 5:35 AM, Alan Gauld wrote: > > "David Hutto" wrote > >> Somewhat of the fact that python uses C encourages me of that, but I >> have still been looking into c++ to optimize, because I've used it >> before, and the more languages I learn the more they feel 'similar', >> but the same, if you can understand that! > > Absolutely! That's why I use 3 languages in my tutor. And I saw them all right their first!...for the most part To try and > dispel the myth that programming languages are hard to learn. > Once you can program in one language learning another > normally takes a few days or even hours. (Becoming fluent > is another matter! :-) > > There are probably only about 5 or 6 basic structures to > programming languages: > > Algol based (Pascal, C, Java, Python etc) > Lisp based (Lisp, Scheme, clojure etc) > Prolog based > Functional( Haskell, ML etc) > SQL based > others?... Could probably go all day about the ones I've either looked at or loved. > > -- > Alan Gauld > 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 > From stefan_ml at behnel.de Tue Dec 21 11:49:29 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 11:49:29 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: David Hutto, 21.12.2010 11:29: > On Tue, Dec 21, 2010 at 5:19 AM, Stefan Behnel wrote: >> Alan Gauld, 21.12.2010 10:58: >>>> 22 Jan 2009 ... Stripping Illegal Characters from XML in Python>> >>> >>> ... I'd be asking Python to process 6.4 gigabytes of CSV into >>> 6.5 gigabytes of XML 1. ..... In fact, what happened was that >>> the parsing didn't work and the whole db was ... >>> >>> And I thought a 1G file was extreme... Do these people stop to think that >>> with XML as much as 80% of their "data" is just description (ie the tags). >> >> As I already said, it compresses well. In run-length compressed XML files, >> the tags can easily take up a negligible amount of space compared to the >> more widely varying data content (although that also commonly tends to >> compress rather well). And depending on how fast your underlying storage is, >> decompressing and parsing the file may still be faster than parsing a huge >> uncompressed file directly. So, again, the shear uncompressed file size is >> *not* a very interesting argument. > > However, could they (as mentioned elsewhere, and by other in another > form)mitigate the damage by using smaller tags exclusively? Why should that have a (noticeable) impact on the compressed file? It's the inherent nature of compression to reduce redundancy, which in XML files usually includes the redundancy of repeated tag names (even if the compression is not specifically XML aware). It's a very bad idea to use short and obfuscated tag names to reduce the storage size. That's like coding in assembler to reduce the size of the source code. Just use compression for storage, or buy a larger hard disk for your NAS. > And also compressed is formatted, even for the tags, correct? The (lossless) compression doesn't change the content. Stefan From __peter__ at web.de Tue Dec 21 11:50:29 2010 From: __peter__ at web.de (Peter Otten) Date: Tue, 21 Dec 2010 11:50:29 +0100 Subject: [Tutor] Hangman game.....problem putting strings in a list..... References: <4D106E2B.2030408@gmail.com> Message-ID: Yasin Yaqoobi wrote: > # This is the line that gives me the error don't know why? > guessed[index] = " " + (letter); ,TypeError: 'str' object does not > support item assignment I don't get this far because I run into Traceback (most recent call last): File "hangman.py", line 69, in line = file.readline(); TypeError: descriptor 'readline' of 'file' object needs an argument In the future please copy and paste the code you are actually running. However: > def initArray(): > global guessed > print line print guessed > guessed = guessed[0] * (len(line)-1) print guessed > print "this is new list " + guessed; If you add these two print statements to your code you might find out yourself what's going wrong. Peter From eike.welk at gmx.net Tue Dec 21 11:51:34 2010 From: eike.welk at gmx.net (Eike Welk) Date: Tue, 21 Dec 2010 11:51:34 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: Message-ID: <201012211152.52117.eike.welk@gmx.net> On Tuesday 21.12.2010 10:12:55 David Hutto wrote: > Then what other file medium would you suggest as the tagging means. One of those formats, that are specially designed for large amounts of data, is HDF5. It is intended for numerical data, but you can store text as well. There are multiple Python libraries for it, the most feature rich is IMHO PyTables. http://www.pytables.org/moin Eike. From smokefloat at gmail.com Tue Dec 21 12:02:23 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 06:02:23 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 5:45 AM, Alan Gauld wrote: > > "David Hutto" wrote > >> That';s what I saying above that xml seems to be the hog in terms of >> it's user defined tags. Is that somewhat a confirmation of my hunch, >> that it's the length of the users predefined tags that add to the >> above mess, and that maybe a lessened tag system in accordance with >> xml might be better, or a simple tag tag in the xml(other >> files) with an index ?to point to a and b would be better. > > Shorter tags reduce the data volume by a bit (and it can be a > big bit if the names are all 20 characters long!) but the inherent tag > structure, even with single char names will still often surpass the > data content. > > > 5 > > > 8 bytes to describe an int which could be represented in > a single byte in binary (or even in CSV). But that byte can't describe the tag(google hold my hand). I'll get this eventually, but my iostream is long on content and hard on parsing. So many languages, and technology, yet so little time. Even if the int were > a 64bit binary value (8 bytes) the minimal tag structure still > consumes the same data width. Of course if the data > content is a long string then simple tags become cost > effective (think

in XHTML)... > > HTH, > > > -- > Alan Gauld > 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 > -- They're installing the breathalyzer on my email account next week. From smokefloat at gmail.com Tue Dec 21 12:08:43 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 06:08:43 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 5:49 AM, Stefan Behnel wrote: > David Hutto, 21.12.2010 11:29: >> >> On Tue, Dec 21, 2010 at 5:19 AM, Stefan Behnel wrote: >>> >>> Alan Gauld, 21.12.2010 10:58: >>>>> >>>>> 22 Jan 2009 ... Stripping Illegal Characters from XML in Python>> >>>> >>>> ... I'd be asking Python to process 6.4 gigabytes of CSV into >>>> 6.5 gigabytes of XML 1. ..... In fact, what happened was that >>>> the parsing didn't work and the whole db was ... >>>> >>>> And I thought a 1G file was extreme... Do these people stop to think >>>> that >>>> with XML as much as 80% of their "data" is just description (ie the >>>> tags). >>> >>> As I already said, it compresses well. In run-length compressed XML >>> files, >>> the tags can easily take up a negligible amount of space compared to the >>> more widely varying data content (although that also commonly tends to >>> compress rather well). And depending on how fast your underlying storage >>> is, >>> decompressing and parsing the file may still be faster than parsing a >>> huge >>> uncompressed file directly. So, again, the shear uncompressed file size >>> is >>> *not* a very interesting argument. >> >> However, could they (as mentioned elsewhere, and by other in another >> form)mitigate the damage by using smaller tags exclusively? > > Why should that have a (noticeable) impact on the compressed file? It's the > inherent nature of compression to reduce redundancy, which in XML files > usually includes the redundancy of repeated tag names (even if the > compression is not specifically XML aware). > > It's a very bad idea to use short and obfuscated tag names to reduce the > storage size. Maybe my style is a form of bad coder example, in some areas(present company accepted). For example, I have a dictionary that has codes within a text file, that point to other lines for verbs, adj, nouns, etc. So doesn't have to mean a it could mean = , but would that help in making the initial usage of in the xml file faster, or slower, by parsing for then relating to ? That's like coding in assembler to reduce the size of the > source code. Haven't gotten to assembler yet, almost there. Just use compression for storage, or buy a larger hard disk for > your NAS. > > >> And also compressed is formatted, even for the tags, correct? > > The (lossless) compression doesn't change the content. google search later, I promise. > > Stefan > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From stefan_ml at behnel.de Tue Dec 21 12:19:00 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 12:19:00 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: David Hutto, 21.12.2010 12:02: > On Tue, Dec 21, 2010 at 5:45 AM, Alan Gauld wrote: >> 8 bytes to describe an int which could be represented in >> a single byte in binary (or even in CSV). Well, "CSV" indicates that there's at least one separator character involved, so make that an asymptotic 2 bytes on average. But obviously, compression applies to CSV and other 'readable' formats as well. > But that byte can't describe the tag Yep, that's an argument that Alan already presented. Stefan From smokefloat at gmail.com Tue Dec 21 12:41:10 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 06:41:10 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 6:19 AM, Stefan Behnel wrote: > David Hutto, 21.12.2010 12:02: >> >> On Tue, Dec 21, 2010 at 5:45 AM, Alan Gauld wrote: >>> >>> 8 bytes to describe an int which could be represented in >>> a single byte in binary (or even in CSV). > > Well, "CSV" indicates that there's at least one separator character > involved, so make that an asymptotic 2 bytes on average. But obviously, > compression applies to CSV and other 'readable' formats as well. > > >> But that byte can't describe the tag > > Yep, that's an argument that Alan already presented. Didn't see that, but that would make the minimal format for parsing a comma, or any other single character marker, and the minimal would still be a specific marker in a file, but does not answer my question about the assignment to another file's variable. If file a.xml has simple tagged xml like , and file b.config has tags that represent the a.xml(i.e. = ) as greater tags, does this pattern optimize the process by limiting the size of the tags to be parsed in the xml, then converting those simpler tags that are found to the b.config values for the simple simple format? From smokefloat at gmail.com Tue Dec 21 12:45:30 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 06:45:30 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 6:41 AM, David Hutto wrote: > On Tue, Dec 21, 2010 at 6:19 AM, Stefan Behnel wrote: >> David Hutto, 21.12.2010 12:02: >>> >>> On Tue, Dec 21, 2010 at 5:45 AM, Alan Gauld wrote: >>>> >>>> 8 bytes to describe an int which could be represented in >>>> a single byte in binary (or even in CSV). >> >> Well, "CSV" indicates that there's at least one separator character >> involved, so make that an asymptotic 2 bytes on average. But obviously, >> compression applies to CSV and other 'readable' formats as well. >> >> >>> But that byte can't describe the tag >> >> Yep, that's an argument that Alan already presented. > > Didn't see that, but that would make the minimal format for parsing a > comma, or any other single character marker, and the minimal would > still be a specific marker in a file, but does not answer my question > about the assignment to another file's variable. > > If file a.xml has simple tagged xml like , and file b.config has > tags that represent the a.xml(i.e. = ) as greater tags, > does this pattern optimize the process by limiting the size of the > tags to be parsed in the xml, then converting those simpler tags that > are found to the b.config values for the simple simple format? > In other words I'm lazy and asking for the experiment to be performed for me(or, more importantly, if it has been), but since I'm not new to this, if no one has a specific case, I'll timeit when I get to it. From stefan_ml at behnel.de Tue Dec 21 12:59:58 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 12:59:58 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: David Hutto, 21.12.2010 12:45: >> If file a.xml has simple tagged xml like, and file b.config has >> tags that represent the a.xml(i.e. =) as greater tags, >> does this pattern optimize the process by limiting the size of the >> tags to be parsed in the xml, then converting those simpler tags that >> are found to the b.config values for the simple simple format? > > In other words I'm lazy and asking for the experiment to be performed > for me(or, more importantly, if it has been), but since I'm not new to > this, if no one has a specific case, I'll timeit when I get to it. I'm still not sure I understand what you are trying to describe here, but I think you want to look into the Wikipedia articles on indexing, hashing and compression. http://en.wikipedia.org/wiki/Index_%28database%29 http://en.wikipedia.org/wiki/Index_%28information_technology%29 http://en.wikipedia.org/wiki/Hash_function http://en.wikipedia.org/wiki/Data_compression Terms like "indirection" and "mapping" also come to my mind when I try to make sense out of your hints. Stefan From smokefloat at gmail.com Tue Dec 21 13:09:59 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 07:09:59 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 6:59 AM, Stefan Behnel wrote: > David Hutto, 21.12.2010 12:45: >>> >>> If file a.xml has simple tagged xml like, and file b.config has >>> tags that represent the a.xml(i.e. ?=) as greater tags, >>> does this pattern optimize the process by limiting the size of the >>> tags to be parsed in the xml, then converting those simpler tags that >>> are found to the b.config values for the simple ?simple format? >> I forget to insert my tags... >> In other words I'm lazy and asking for the experiment to be performed >> for me(or, more importantly, if it has been), but since I'm not new to >> this, if no one has a specific case, I'll timeit when I get to it. > > I'm still not sure I understand what you are trying to describe here, but I > think you want to look into the Wikipedia articles on indexing, hashing and > compression. a.xml has tags with simplistic forms, like was argued above, with , or . b.config has variables for the simple tags in a.xml so that = in b.config. So when parsing a.xml, you parse it, then use more complex tags to define with b.config.. I'll review the url's a little later. > > http://en.wikipedia.org/wiki/Index_%28database%29 > http://en.wikipedia.org/wiki/Index_%28information_technology%29 > http://en.wikipedia.org/wiki/Hash_function > http://en.wikipedia.org/wiki/Data_compression > > Terms like "indirection" and "mapping" also come to my mind when I try to > make sense out of your hints. Terms like tags, and xml also come to mind. Or parsing, or regular expressions, or re, or find, or alot of things come to mind. My experience is limited, but not by much, and certainly not in respect to the scope of other languages. But thank you for the references, I'm not so good, that, I can't afford to look through a bunch of coal to find a diamond. > > Stefan > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From stefan_ml at behnel.de Tue Dec 21 13:26:56 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 13:26:56 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: David Hutto, 21.12.2010 13:09: > On Tue, Dec 21, 2010 at 6:59 AM, Stefan Behnel wrote: >> David Hutto, 21.12.2010 12:45: >>>> >>>> If file a.xml has simple tagged xml like, and file b.config has >>>> tags that represent the a.xml(i.e. =) as greater tags, >>>> does this pattern optimize the process by limiting the size of the >>>> tags to be parsed in the xml, then converting those simpler tags that >>>> are found to the b.config values for the simple simple format? >>> >>> In other words I'm lazy and asking for the experiment to be performed >>> for me(or, more importantly, if it has been), but since I'm not new to >>> this, if no one has a specific case, I'll timeit when I get to it. >> >> I'm still not sure I understand what you are trying to describe here > > a.xml has tags with simplistic forms, like was argued above, with, > or. b.config has variables for the simple tags in a.xml so that > = in b.config. > > So when parsing a.xml, you parse it, then use more complex tags to > define with b.config.. I'll review the url's a little later. Ok, I'd call that simple renaming, that's what I meant with "indirection" and "mapping" (basically the two concepts that computer science is all about ;). Sure, run your own benchmarks, but don't expect anyone to be interested in the results. If your interest is to obfuscate the tag names, why not just use a binary (or less readable) format? That gives you much better obfuscation in the first place. Stefan From steve at pearwood.info Tue Dec 21 14:54:51 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 22 Dec 2010 00:54:51 +1100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: <4D10B1AB.5000900@pearwood.info> Alan Gauld wrote: > XML is a self-describing data format. It is usually used for files > but can be used in data streams or in-memory strings. > > It's natural competitors are TLV (Tag,Lenth,Value) and > CSV(Comma Seperated Value) files but neither is as rich > in structure. Alternative options include ASN.1, Edifact and > IDL but these are not self-describing(*) (although they are all > more compact and faster to parse, but only IDL is free.) I would have thought that both JSON and YAML are competitors to XML, although of course it depends on exactly what you are using XML for. For example, Gnome uses XML files extensively for their poor-man's Registry, which is a shame as (in my opinion) simple Windows-style INI files or Unix/Linux style config files would be a far better and more natural choice. Basically, people shouldn't make the mistake of thinking that because XML is text-based it is meant as a human-readable (let alone human-editable) format. It's not. It's a machine format that happens to be *just barely* human-readable and -editable in simple cases due to using ASCII text -- Steven From steve at pearwood.info Tue Dec 21 15:02:58 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 22 Dec 2010 01:02:58 +1100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> Message-ID: <4D10B392.5090609@pearwood.info> Stefan Behnel wrote: > David Hutto, 21.12.2010 10:29: >> File = string >> >> going through string code >> >> finding pieces of the string and marking the territory. >> >> >> I don't see 'real' optimization other than rolling your own. > > Reads like a Haiku. Doesn't quite fit the verse, though. > > From your behaviour, I get the impression that you are just trolling. Consider David's email address and mail sig: smokefloat at gmail.com "They're installing the breathalyzer on my email account next week." I don't think he's trolling so much as floating around the ceiling :) -- Steven From alan.gauld at btinternet.com Tue Dec 21 15:04:39 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 14:04:39 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B1AB.5000900@pearwood.info> Message-ID: "Steven D'Aprano" wrote >> It's natural competitors are TLV (Tag,Lenth,Value) and >> CSV(Comma Seperated Value) files but neither is as rich > > I would have thought that both JSON and YAML are competitors to XML, Totally agree but I excluded those on the basis that they weren't around when XML was invented but CSV and TLV etc were. (I guess my reasoning is that while XML was a competitor to JSON/YAML when they were developed - because it preceded them - they were not competitors to XML because they post dated it. But it depe4nds on how you define the competition - is it as a candidate for use now or alternatives at the time of creation...) But if you were looking for a choice of format today then YAML and JSON would have to be included. Alan g From alan.gauld at btinternet.com Tue Dec 21 15:11:24 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 14:11:24 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: <4D0FC877.9010705@pearwood.info> Message-ID: "Stefan Behnel" wrote >> And I thought a 1G file was extreme... Do these people stop to >> think that >> with XML as much as 80% of their "data" is just description (ie the >> tags). > > As I already said, it compresses well. In run-length compressed XML > files, the tags can easily take up a negligible amount of space > compared to the more widely varying data content I understand how compression helps with the data transmission aspect. > compress rather well). And depending on how fast your underlying > storage is, decompressing and parsing the file may still be faster > than parsing a huge uncompressed file directly. But I don't understand how uncompressing a file before parsing it can be faster than parsing the original uncompressed file? There are ways of processing xml to reduce the tag space (a bit like tinyurl does with long urls) but then the parsing code has to know about the tag translations too - and usually the savings are small. Curious, Alan G. From smokefloat at gmail.com Tue Dec 21 15:28:23 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 09:28:23 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: <4D10B392.5090609@pearwood.info> References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> Message-ID: Establish that with fact that initiatially I didn't have a reason to be hostile, and that your comment of my kubit kaba here, and your comment on comp.python.lang about your pystats, aftger our conversation, and your reference to it not being "set in stone", wasn't a reference tyo our statrs argument here. Who sets examples steven. Mediocre idols, or egotistical programmers(whom I've met plenty of, hence my unecesary hostility ) From smokefloat at gmail.com Tue Dec 21 15:32:08 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 09:32:08 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> Message-ID: Take a look at the flame wars individuals see, comments by programmers who are sarcastic, and think of the response you might have had to the initial questions you had , and maybe even a few paranoid delusions you got hacked. It's not a rewarding experience not being a college educated individual vs someone who is a self learner who has to put up with lots of ego because they got the degree, and I have to earn it through damn near torture and fire. From smokefloat at gmail.com Tue Dec 21 15:35:07 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 09:35:07 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 9:32 AM, David Hutto wrote: > Take a look at the flame wars individuals see, comments by programmers > who are sarcastic, and think of the response you might have had to the > initial questions you had , and maybe even a few paranoid delusions > you got hacked. > > It's not a rewarding experience not being a college educated > individual vs someone who is a self learner who has to put up with > lots of ego because they got the degree, and I have to earn it through > damn near torture and fire. > I don't mind hazing, but I do mind you underestimating my potential, or my ability From steve at pearwood.info Tue Dec 21 15:36:59 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 22 Dec 2010 01:36:59 +1100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> Message-ID: <4D10BB8B.5080702@pearwood.info> David Hutto wrote: > Establish that with fact that initiatially I didn't have a reason to > be hostile, and that your comment of my kubit kaba here, and your > comment on comp.python.lang about your pystats, aftger our > conversation, and your reference to it not being "set in stone", > wasn't a reference tyo our statrs argument here. I have no idea what you are talking about. I can't decipher the above paragraph, what's a "kubit kaba"? I don't know why you have taken offense, or even what you have taken offense over. -- Steven From smokefloat at gmail.com Tue Dec 21 15:38:39 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 09:38:39 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> Message-ID: And furthermore, I'm not the first, nor the last to get angry and frustrated on the internet. I'm not the first to get drunk, and type. And I dare any employer to deny me the right to MY personal time. From smokefloat at gmail.com Tue Dec 21 15:40:01 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 09:40:01 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: <4D10BB8B.5080702@pearwood.info> References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> <4D10BB8B.5080702@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 9:36 AM, Steven D'Aprano wrote: > David Hutto wrote: >> >> Establish that with fact that initiatially I didn't have a reason to >> be hostile, and that your comment of my kubit kaba here, and your >> comment on comp.python.lang about your pystats, aftger our >> conversation, and your reference to it not being "set in stone", >> wasn't a reference tyo our statrs argument here. > > I have no idea what you are talking about. I can't decipher the above > paragraph, what's a "kubit kaba"? Check the 'archives' steven, dcite your own reerences to deny the claim. I'm not known for being a liar.. > > I don't know why you have taken offense, or even what you have taken offense > over. > > > -- > Steven > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From smokefloat at gmail.com Tue Dec 21 15:40:16 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 09:40:16 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> <4D10BB8B.5080702@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 9:40 AM, David Hutto wrote: > On Tue, Dec 21, 2010 at 9:36 AM, Steven D'Aprano wrote: >> David Hutto wrote: >>> >>> Establish that with fact that initiatially I didn't have a reason to >>> be hostile, and that your comment of my kubit kaba here, and your >>> comment on comp.python.lang about your pystats, aftger our >>> conversation, and your reference to it not being "set in stone", >>> wasn't a reference tyo our statrs argument here. >> >> I have no idea what you are talking about. I can't decipher the above >> paragraph, what's a "kubit kaba"? > > Check the 'archives' steven, dcite your own refrences to deny the > claim. I'm not known for being a liar.. > > >> >> I don't know why you have taken offense, or even what you have taken offense >> over. >> >> >> -- >> Steven >> >> _______________________________________________ >> Tutor maillist ?- ?Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > > -- > They're installing the breathalyzer on my email account next week. > -- They're installing the breathalyzer on my email account next week. From smokefloat at gmail.com Tue Dec 21 15:45:28 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 09:45:28 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> <4D10BB8B.5080702@pearwood.info> Message-ID: Me and you, apparently know exactly what i'm talking about... http://code.activestate.com/lists/python-tutor/79293/ From smokefloat at gmail.com Tue Dec 21 15:45:28 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 09:45:28 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> <4D10BB8B.5080702@pearwood.info> Message-ID: Me and you, apparently know exactly what i'm talking about... http://code.activestate.com/lists/python-tutor/79293/ From smokefloat at gmail.com Tue Dec 21 15:48:18 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 09:48:18 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> <4D10BB8B.5080702@pearwood.info> Message-ID: you got nothing of real value. From smokefloat at gmail.com Tue Dec 21 15:49:01 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 09:49:01 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> <4D10BB8B.5080702@pearwood.info> Message-ID: And a lesson of what you really are to anyone listening. From stefan_ml at behnel.de Tue Dec 21 16:03:09 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 16:03:09 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: Alan Gauld, 21.12.2010 15:11: > "Stefan Behnel" wrote >>> And I thought a 1G file was extreme... Do these people stop to think that >>> with XML as much as 80% of their "data" is just description (ie the tags). >> >> As I already said, it compresses well. In run-length compressed XML >> files, the tags can easily take up a negligible amount of space compared >> to the more widely varying data content > > I understand how compression helps with the data transmission aspect. > >> compress rather well). And depending on how fast your underlying storage >> is, decompressing and parsing the file may still be faster than parsing a >> huge uncompressed file directly. > > But I don't understand how uncompressing a file before parsing it can > be faster than parsing the original uncompressed file? I didn't say "uncompressing a file *before* parsing it". I meant uncompressing the data *while* parsing it. Just like you have to decode it for parsing, it's just an additional step to decompress it before decoding. Depending on the performance relation between I/O speed and decompression speed, it can be faster to load the compressed data and decompress it into the parser on the fly. lxml.etree (or rather libxml2) internally does that for you, for example, if it detects compressed input when parsing from a file. Note that these performance differences are tricky to prove in benchmarks, as repeating the benchmark usually means that the file is already cached in memory after the first run, so the decompression overhead will dominate in the second run. That's not what you will see in a clean run or for huge files, though. Stefan From delegbede at dudupay.com Tue Dec 21 16:10:19 2010 From: delegbede at dudupay.com (delegbede at dudupay.com) Date: Tue, 21 Dec 2010 15:10:19 +0000 Subject: [Tutor] REGEX EXPLANATION Message-ID: <575593835-1292944190-cardhu_decombobulator_blackberry.rim.net-684656434-@b3.c12.bise7.blackberry> I have been reading regex in order to work around an assignment. Could anyone explain to me in plain english what the following regex expression translate to. (r'PSC(?P\d{6})(DC|VR)(\d{2})(RC|GA)(\d{3})(!?([A-Z\d]{1,})@?(.*))?', re.I) (r'PSC(?P\d{6})VR(?P\d{2})RC(?P\d{3})(?P[ABCDEFGHJKMNPQRSTUVWXYZ\d]{2,})?', re.I) (r'PSC(?P\d{6})VR(?P\d{2})(?P(RC|GA))(?P\d{3})!(?P[ABCDEFGHJKMNPQ]{1,})@?(?P.*)', re.I) (r'PSC(?P\d{6})DC(?P\d{2})RC(?P\d{3})(?P[ABCDEFGHJKMNPQRSTUVWX\d]{2,})?', re.I) (r'PSC(?P\d{6})DC(?P\d{2})(?P(RC|GA))(?P\d{3})!(?P[ABCDEFGHJK]{1,})@?(?P.*)', re.I) Like reading it out in plain english. This should further help me understand how these signs work and then I can get along. Thanks. Sent from my BlackBerry wireless device from MTN From smokefloat at gmail.com Tue Dec 21 16:11:14 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 10:11:14 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 10:03 AM, Stefan Behnel wrote: > Alan Gauld, 21.12.2010 15:11: >> >> "Stefan Behnel" wrote >>>> >>>> And I thought a 1G file was extreme... Do these people stop to think >>>> that >>>> with XML as much as 80% of their "data" is just description (ie the >>>> tags). >>> >>> As I already said, it compresses well. In run-length compressed XML >>> files, the tags can easily take up a negligible amount of space compared >>> to the more widely varying data content >> >> I understand how compression helps with the data transmission aspect. >> >>> compress rather well). And depending on how fast your underlying storage >>> is, decompressing and parsing the file may still be faster than parsing a >>> huge uncompressed file directly. >> >> But I don't understand how uncompressing a file before parsing it can >> be faster than parsing the original uncompressed file? > > I didn't say "uncompressing a file *before* parsing it". He didn't say utilizing code below Python either, but others will argue the microseconds matter, and if that's YOUR standard, then keep it for client and self. I meant > uncompressing the data *while* parsing it. Just like you have to decode it > for parsing, it's just an additional step to decompress it before decoding. > Depending on the performance relation between I/O speed and decompression > speed, it can be faster to load the compressed data and decompress it into > the parser on the fly. lxml.etree (or rather libxml2) internally does that > for you, for example, if it detects compressed input when parsing from a > file. > > Note that these performance differences are tricky to prove in benchmarks, Tricky and proven, then tell me what real time, and this is in reference to a recent c++ discussion, is python used in ,andhow could it be utilized in....say an aviation system to avoid a collision when milliseconds are on the line? > as repeating the benchmark usually means that the file is already cached in > memory after the first run, so the decompression overhead will dominate in > the second run. That's not what you will see in a clean run or for huge > files, though. > > Stefan > > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- They're installing the breathalyzer on my email account next week. From stefan_ml at behnel.de Tue Dec 21 16:41:14 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 21 Dec 2010 16:41:14 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: David Hutto, 21.12.2010 16:11: > On Tue, Dec 21, 2010 at 10:03 AM, Stefan Behnel wrote: >> I meant >> uncompressing the data *while* parsing it. Just like you have to decode it >> for parsing, it's just an additional step to decompress it before decoding. >> Depending on the performance relation between I/O speed and decompression >> speed, it can be faster to load the compressed data and decompress it into >> the parser on the fly. lxml.etree (or rather libxml2) internally does that >> for you, for example, if it detects compressed input when parsing from a >> file. >> >> Note that these performance differences are tricky to prove in benchmarks, > > Tricky and proven, then tell me what real time, and this is in > reference to a recent c++ discussion, is python used in ,andhow could > it be utilized in....say an aviation system to avoid a collision when > milliseconds are on the line? I doubt that there are many aviation systems that send around gigabytes of compressed XML data milliseconds before a collision. I even doubt that air plane collision detection is time critical anywhere in the milliseconds range. After all, there's a pilot who has to react to the collision warning, and he or she will certainly need more than a couple of milliseconds to react, not to mention the time that it takes for the air plane to adapt its flight direction. If you plan the system in a way that makes milliseconds count, you can just as well replace it by a jack-in-the-box. Oh, and that might even speed up the reaction of the pilot. ;) So, no, if these systems ever come close to a somewhat recent state of technology, I wouldn't mind if they were written in Python. The CPython runtime is pretty predictable in its performance characteristics, after all. Stefan From ben.ganzfried at gmail.com Tue Dec 21 17:07:54 2010 From: ben.ganzfried at gmail.com (Ben Ganzfried) Date: Tue, 21 Dec 2010 11:07:54 -0500 Subject: [Tutor] AttributeError: 'list' object has no attribute 'find' Message-ID: Hey, I keep getting the error above and unfortunately browsing through google and finding similar responses has not been fruitful for me. My code is below and I have marked off the location of the problem in my code. I'm wondering the following: 1) Doesn't the read() file object method return the specified characters from the file as a string? 2) If #1 is correct, then why is my variable "source" being viewed as a list as opposed to a string? 3) How can I change my variable "source" so that it can use the 'find' method? Or alternatively, is there another method besides the 'find' method that would do the same thing that 'find' does that would work on my variable 'source' as it currently is? Thanks so much, Ben #recursively find each instance of the tag throughout the whole document def findOneTag (tag, source, output): print("tag is ", tag) #print("source is now ", source) print("output is ", output) #base case if source == "": return #recursive case tagIndex = source.find(tag) #(*********THIS IS THE LOCATION OF THE PROBLEM**********) print("tagIndex is ", tagIndex) start = source[tagIndex:].find("\t") + 1 print("start is ", start) stop = source[tagIndex + start:].find("\t") print("stop is ", stop) if tagIndex !=-1: output.write(tag + "\t" + line[start: stop]) print("spliced text is: ", line[start:stop]) #recursively call findOneTag(tag, String source[stop:], output) findOneTag(tag, source[stop + 1:], output) def main(): tag = "species" inname = "skeletontext.txt" outname = "skeletontext1234567.txt" inname1 = open(inname, "r") output = open(outname, "w") source = inname1.readlines() print("source is: ", source) findOneTag(tag, source, output) From alan.gauld at btinternet.com Tue Dec 21 18:57:22 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 17:57:22 -0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python References: <4D0FC877.9010705@pearwood.info> Message-ID: "Stefan Behnel" wrote >> But I don't understand how uncompressing a file before parsing it >> can >> be faster than parsing the original uncompressed file? > > I didn't say "uncompressing a file *before* parsing it". I meant > uncompressing the data *while* parsing it. Ah, ok that can work, although it does add a layer of processing to identify compressed v uncompressed data, but if I/O is the bottleneck then it could give an advantage. Alan g. From alan.gauld at btinternet.com Tue Dec 21 19:02:34 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Dec 2010 18:02:34 -0000 Subject: [Tutor] AttributeError: 'list' object has no attribute 'find' References: Message-ID: "Ben Ganzfried" wrote > 1) Doesn't the read() file object method return the specified > characters from the file as a string? Yes > 2) If #1 is correct, then why is my variable "source" being viewed > as > a list as opposed to a string? You are not using read(), you are using readlines() which returns all of the lines in a list. > 3) How can I change my variable "source" so that it can use the > 'find' > method? Use read() HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From rabidpoobear at gmail.com Tue Dec 21 19:04:10 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Tue, 21 Dec 2010 12:04:10 -0600 Subject: [Tutor] AttributeError: 'list' object has no attribute 'find' In-Reply-To: References: Message-ID: Read() does return a string. I guess the better question would be... Are you using read? 'cause in the example you sent you used readlines() which returns a list of lines. ----------------------------- Sent from a mobile device with a bad e-mail client. ----------------------------- On Dec 21, 2010, at 10:07 AM, Ben Ganzfried wrote: > Hey, > > I keep getting the error above and unfortunately browsing through > google and finding similar responses has not been fruitful for me. My > code is below and I have marked off the location of the problem in my > code. I'm wondering the following: > > 1) Doesn't the read() file object method return the specified > characters from the file as a string? > 2) If #1 is correct, then why is my variable "source" being viewed as > a list as opposed to a string? > 3) How can I change my variable "source" so that it can use the 'find' > method? Or alternatively, is there another method besides the 'find' > method that would do the same thing that 'find' does that would work > on my variable 'source' as it currently is? > > Thanks so much, > > Ben > > #recursively find each instance of the tag throughout the whole document > def findOneTag (tag, source, output): > print("tag is ", tag) > #print("source is now ", source) > print("output is ", output) > #base case > if source == "": > return > #recursive case > tagIndex = source.find(tag) #(*********THIS IS THE LOCATION OF THE > PROBLEM**********) > print("tagIndex is ", tagIndex) > start = source[tagIndex:].find("\t") + 1 > print("start is ", start) > stop = source[tagIndex + start:].find("\t") > print("stop is ", stop) > if tagIndex !=-1: > output.write(tag + "\t" + line[start: stop]) > print("spliced text is: ", line[start:stop]) > #recursively call findOneTag(tag, String source[stop:], output) > findOneTag(tag, source[stop + 1:], output) > > def main(): > tag = "species" > inname = "skeletontext.txt" > outname = "skeletontext1234567.txt" > inname1 = open(inname, "r") > output = open(outname, "w") > source = inname1.readlines() > print("source is: ", source) > findOneTag(tag, source, output) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From rabidpoobear at gmail.com Tue Dec 21 19:23:01 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Tue, 21 Dec 2010 12:23:01 -0600 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> <4D10BB8B.5080702@pearwood.info> Message-ID: You're not going to win any friends here Dave. Steven is well known on this list. He is sometimes abrasive but it's rarely if ever malicious. Anytime he's ever been rude to me it was deserved. Like how I top post from my phone. Or giving bad advice to newbies. People are getting irritated because YOU are not making the effort to be understood, and then getting angry when we don't understand you. Have you read Eric raymond's smart questions article? It may help you understand the mentality here. And no we are not hostile to people because they lack education. But may be perceived as hostile to those who do not WANT education. Whatever bandying about and posturing you want to do is fine with me, but when it comes down to it, if you ask a question and I can tell you didn't make sufficient effort to solve it yourself (due to laziness, not ignorance or inability) I have little to no incentive to help you. Also, I have a masters in C.S. My stepfather has been a programmer for 30 years and has no programming degree. I do not think my degree is a replacement for that, and learn new things from him every day. It goes both ways. Have respect for yourself and your ability to learn what you need to, and we'll have the respect to help pick you up if you stumble. My 2 cents. ----------------------------- Sent from a mobile device with a bad e-mail client. ----------------------------- On Dec 21, 2010, at 8:49 AM, David Hutto wrote: > And a lesson of what you really are to anyone listening. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From ashish.makani at gmail.com Tue Dec 21 20:04:38 2010 From: ashish.makani at gmail.com (ashish makani) Date: Tue, 21 Dec 2010 11:04:38 -0800 Subject: [Tutor] [OT] Bay area meetups for python &/ {linux, embedded, mobile, wireless ?} Message-ID: > > OT(off_topic): I moved to the bay area recently & am passionate about > technology in general & linux, python, c, embedded, mobile, wireless > stuff,..... > I was wondering if any of you guys, are part of some bay area python( or > other tech) meetup ( as in do you guys meetup, in person) for like a tech > talk / discussion / brainstorming/ hack nights ? > > If yes, i would love to know more & be a part of it > Thanks Tino for the info Any other inputs from other folks ? OT(off_topic): I moved to the bay area recently & am passionate about technology in general & linux, python, c, embedded, mobile, wireless stuff,..... I was wondering if any of you guys, are part of some bay area python( or other tech) meetup ( as in do you guys meetup, in person) for like a tech talk / discussion / brainstorming/ hack nights ? If yes, i would love to know more & be a part of it I am aware of BayPiggies http://www.baypiggies.net/ http://mail.python.org/mailman/listinfo/baypiggies On Mon, Dec 20, 2010 at 9:10 PM, Tino Dai wrote: > Hi Ashish, > > Check out Noisebridge ( https://www.*noisebridge*.net/) in > SF. I think you will find there are like minded tech people there. It also > has Mitch Altman ( http://en.wikipedia.org/wiki/*Mitch*_*Altman* > ) is one of the founders/original members. > > -Tino > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > *"We act as though comfort and luxury were the chief requirements of life, when all that we need to make us happy is something to be enthusiastic about." -- Albert Einstein* -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Tue Dec 21 22:06:34 2010 From: wprins at gmail.com (Walter Prins) Date: Tue, 21 Dec 2010 21:06:34 +0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On 21 December 2010 14:11, Alan Gauld wrote: > But I don't understand how uncompressing a file before parsing it can > be faster than parsing the original uncompressed file? > Because of IO overhead/benefits. It's not so much that the parsing aspect of it is faster of course (it is what it is), it's that the total time taken to (read+decompress+parse) is faster than just (read+parse), because the time to actually read the compressed data is less than the time it takes to decompress that data into RAM. Generally speaking, compared to your CPU and memory, with respect to IO your disk is always going to be the culprit, though of course it does depend on exactly how much data we're talking about, how fast your CPU is, etc. In general computing this is less of an issue nowadays than perhaps a few years ago, and the gains can be as you say small, or sometimes not so small, depending exactly how much data you've got, how highly compressed it's become, how fast/efficient the decompresser is, how slow your I/O channel is etc, but the point nevertheless stands. Case in point, it's perhaps interesting to note that this technique is used regularly on the web in general -- most web servers actually stream their HTML content as LZ compressed data streams, since (as above) it's quicker to compress, stream, decompress and parse than it is to just stream the data direct. (And, of course, thanks to zlib + urllib one can even use this feature from Python should you wish to do so.) Anyway, just my $0.02! Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Tue Dec 21 22:13:16 2010 From: wprins at gmail.com (Walter Prins) Date: Tue, 21 Dec 2010 21:13:16 +0000 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: On 21 December 2010 17:57, Alan Gauld wrote: > > "Stefan Behnel" wrote > > But I don't understand how uncompressing a file before parsing it can >>> be faster than parsing the original uncompressed file? >>> >> >> I didn't say "uncompressing a file *before* parsing it". I meant >> uncompressing the data *while* parsing it. >> > > Ah, ok that can work, although it does add a layer of processing > to identify compressed v uncompressed data, but if I/O is the > bottleneck then it could give an advantage. > OK my apologies, I see my previous response was already circumscribed by later emails (which I had not read yet.) Feel free to ignore it. :) Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From smokefloat at gmail.com Tue Dec 21 23:17:32 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 21 Dec 2010 17:17:32 -0500 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <201012202027.44992.cfuller084@thinkingplanet.net> <4D10B392.5090609@pearwood.info> <4D10BB8B.5080702@pearwood.info> Message-ID: On Tue, Dec 21, 2010 at 1:23 PM, Luke Paireepinart wrote: > You're not going to win any friends here Dave. Wasn't trying to. Steven is well known on this list. And that means something to you only. He is sometimes abrasive but it's rarely if ever malicious. Anytime he's ever been rude to me it was deserved,and the only times I remember being rude to him, it was settled, and then we go right back to steven being an ass(not that I ever said I wasn't at some points, and we've had off list emails as well . Like how I top post from my phone. Or giving bad advice to newbies. Guilty of the same, but I wasn't trying to malicious either, just as were'nt trying to be. > > People are getting irritated because YOU are not making the effort to be understood I pete and repeat when asked, and barely try to ask, when most of the time google and the docs solve it , and then getting angry when we don't understand you. More like frustrated than angry, and I believe everyone here has had the same feeling. Have you read Eric raymond's smart questions article? Not his but others. It may help you understand the mentality here. > > And no we are not hostile to people because they lack education. And apparently you speak for all of the programmers out there. But may be perceived as hostile to those who do not WANT education. Whatever bandying about and posturing you want to do is fine with me, but when it comes down to it, if you ask a question and I can tell you didn't make sufficient effort to solve it yourself (due to laziness, not ignorance or inability) I have little to no incentive to help you. > > Also, I have a masters in C.S. My stepfather has been a programmer for 30 years and has no programming degree. I do not think my degree is a replacement for that, and learn new things from him every day. It goes both ways. Have respect for yourself and your ability to learn what you need to, and we'll have the respect to help pick you up if you stumble I like to think that in terms of being a python self learner, I have persevered on my own very well. Maybe i was feeling inadequate and frustrated, and lashed ouy, but so has he, and I've tried to let it go, and will again this time. . > > My 2 cents. Pocketed. From alan.gauld at btinternet.com Wed Dec 22 02:37:11 2010 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Wed, 22 Dec 2010 01:37:11 +0000 (GMT) Subject: [Tutor] Hangman game.....problem putting strings in a list..... In-Reply-To: References: Message-ID: <652688.68310.qm@web86704.mail.ird.yahoo.com> Forwarding to tutor list, please use Reply All whjen replying to the group. Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ ----- Original Message ---- > full error printout. I tried debugging it and I still don't know why > guessed[index] = (letter) gives me an error. I don't want to use the append > method because it will put the new letter at the end of the list which is > very different then playing the hangman game.. OK, On reading it again I see why you are using the index approach. > guessed = guessed[0] * (len(line)-1) That should be OK although personally I'd use a list comprehension to initialise the list: guessed = [None for c in line] # use the characters in line to get corresponding Nones > Traceback (most recent call last): > File "/Python_Hello_World/src/HangMan/__init__.py", line 72, in > doit(); > File "/Python_Hello_World/src/HangMan/__init__.py", line 56, in doit > guess(a_letter); > File "/Python_Hello_World/src/HangMan/__init__.py", line 15, in guess > guessed[index] = (letter); > TypeError: 'str' object does not support item assignment I don't understand how you are getting this either, I don't get that error in similar code. Can you insert a print statement just before that line like: print 'index: ',index, '\tletter: ',letter,\nguessed: ', guessed So we can see the values just before the line is executed. Also drop the semi colon and parens. They shouldn't do any harm but it removes two more variables from the equation... > I am switching from java so my python style might be a bit unorthodox. I do > however access my global variables in different methods... Don't know why it > is encouraged in java and discouraged in python ? I've never seen global variables being encouraged in Java. I think you may be thinking of class (or instance) variables which are also ok in Python albeit more clearly identified by prefixing with self (ie 'this' in Java). > >> guessed = ["-"]; > >> count = 0; > >> wrong = 0; > >> > >> def guess(letter): > >> global guessed > > > >> if (letter in line): > > > > You don't need the parens, they don't do any harm, > > but they aren't needed. > > > >> index = line.index(letter); > >> print guessed; > > > >> # This is the line that gives me the error don't know why? > >> guessed[index] = " " + (letter); ,TypeError: 'str' object does not > >> support item assignment > > > >> guessed[index] = (letter); > > > > Again, you don't need the parens... > > And I suspect you really want to use append() here rather > > than assigning to guessed[index]. > > > >> print ' '.join(guessed) > >> else: > >> global wrong; > >> wrong += 1; > >> > >> > >> def draw(number):... > > > >> def doit(): > >> global count > >> while(wrong != 7): > >> a_letter = raw_input("Pick a letter --> ") > >> print > >> guess(a_letter); > >> draw(wrong); > >> print > >> count += 1 > >> > >> def initArray(): > >> global guessed > >> print line > >> guessed = guessed[0] * (len(line)-1) > >> print "this is new list " + guessed; > > > > If you use the append() method you don't need this. > > > >> while 1: > >> line = file.readline(); > >> if (len(line) >= 5): > >> initArray() > >> doit(); > >> break > >> if not line: break > >> > >> file.close() > > > > HTH, > > > > > From rabidpoobear at gmail.com Wed Dec 22 04:38:34 2010 From: rabidpoobear at gmail.com (Luke Paireepinart) Date: Tue, 21 Dec 2010 21:38:34 -0600 Subject: [Tutor] REGEX EXPLANATION In-Reply-To: <575593835-1292944190-cardhu_decombobulator_blackberry.rim.net-684656434-@b3.c12.bise7.blackberry> References: <575593835-1292944190-cardhu_decombobulator_blackberry.rim.net-684656434-@b3.c12.bise7.blackberry> Message-ID: Which part of the regexes do you not understand? Have you tried figuring themselves out yourself first? We don't typically give out answers here. This is a tutor list, not a solve your problems for you list. We're here to teach you how to fish, not cook for you. So show us where you're stuck and we'll help out. Did you read the Python regex documentation online? I find it's quite good. Should give you a push in the right direction. On Tue, Dec 21, 2010 at 9:10 AM, wrote: > I have been reading regex in order to work around an assignment. > > Could anyone explain to me in plain english what the following regex expression translate to. > > (r'PSC(?P\d{6})(DC|VR)(\d{2})(RC|GA)(\d{3})(!?([A-Z\d]{1,})@?(.*))?', re.I) > > (r'PSC(?P\d{6})VR(?P\d{2})RC(?P\d{3})(?P[ABCDEFGHJKMNPQRSTUVWXYZ\d]{2,})?', re.I) > > (r'PSC(?P\d{6})VR(?P\d{2})(?P(RC|GA))(?P\d{3})!(?P[ABCDEFGHJKMNPQ]{1,})@?(?P.*)', re.I) > > (r'PSC(?P\d{6})DC(?P\d{2})RC(?P\d{3})(?P[ABCDEFGHJKMNPQRSTUVWX\d]{2,})?', re.I) > > (r'PSC(?P\d{6})DC(?P\d{2})(?P(RC|GA))(?P\d{3})!(?P[ABCDEFGHJK]{1,})@?(?P.*)', re.I) > > Like reading it out in plain english. This should further help me understand how these signs work and then I can get along. > > Thanks. > Sent from my BlackBerry wireless device from MTN > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From stefan_ml at behnel.de Wed Dec 22 09:19:18 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 22 Dec 2010 09:19:18 +0100 Subject: [Tutor] Trying to parse a HUGE(1gb) xml file in python In-Reply-To: References: <4D0FC877.9010705@pearwood.info> Message-ID: Walter Prins, 21.12.2010 22:13: > On 21 December 2010 17:57, Alan Gauld wrote: >> "Stefan Behnel" wrote >> >>>> But I don't understand how uncompressing a file before parsing it can >>>> be faster than parsing the original uncompressed file? >>>> >>> >>> I didn't say "uncompressing a file *before* parsing it". I meant >>> uncompressing the data *while* parsing it. >>> >> >> Ah, ok that can work, although it does add a layer of processing >> to identify compressed v uncompressed data, but if I/O is the >> bottleneck then it could give an advantage. >> > > OK my apologies, I see my previous response was already circumscribed by > later emails (which I had not read yet.) Feel free to ignore it. :) Not much of a reason to apologize. Especially on a newbee list like python-tutor, a few words more or a different way of describing things may help in widening the set of readers who understand and manage to follow other people's arguments. Stefan From fomcl at yahoo.com Wed Dec 22 10:30:27 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 22 Dec 2010 01:30:27 -0800 (PST) Subject: [Tutor] pyodbc/date values in MS Access In-Reply-To: References: <515399.28236.qm@web110708.mail.gq1.yahoo.com> Message-ID: <357339.8061.qm@web110712.mail.gq1.yahoo.com> Hi, Sorry for the late reply, but thanks a lot for helping me. It's solved now. Peter, the link you posted in another thread (or should I say 'query') was also relevant AND funny (http://xkcd.com/327/) Merry Christmas and Happy Coding! *) Cheers!! Albert-Jan *) Including those who have to parse a huge xml file *winks* ;-) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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 Otten <__peter__ at web.de> To: tutor at python.org Sent: Wed, December 15, 2010 3:06:19 PM Subject: Re: [Tutor] pyodbc/date values in MS Access Albert-Jan Roskam wrote: > Hi, > > I'm using pyodbc (Python 2.5) to insert records in an MS Access database. > For security reasons, question marks should be used for string replacement > [*]. The standard %s would make the code vulnerable to sql code injection. > Problem is, string replacement in the Good Way somehow doesn't work when > the values are dates. Below, snippet #1 does not work (Access says the > inserted value is not consistent with the defined datatype), but #2 does. > I tried various other ways (ie. DateValue, CDate, etc.) but none of them > works. Is there a solution for this? > > [*] see http://code.google.com/p/pyodbc/wiki/GettingStarted --> under > 'Parameters' > > ### 1 > sql = "INSERT INTO tblSomeTable (myDate) VALUES (?);" > cursor.execute(sql, "#01/01/2010#") (1) Try providing the date in ISO format "yyyy-mm-dd" "2010-01-01" or (even better if supported) as a date value from datetime import date date(2010, 1, 1) (2) Wrap the value into a tuple which I think is required by the Python DBAPI. cursor.execute(sql, ("2010-01-01",)) cursor.execute(sql, (date(2010, 1, 1),)) Peter _______________________________________________ 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 garry.bettle at gmail.com Wed Dec 22 13:10:01 2010 From: garry.bettle at gmail.com (Garry Bettle) Date: Wed, 22 Dec 2010 13:10:01 +0100 Subject: [Tutor] Dictionary Question Message-ID: Howdy all, Hope this message finds everyone well. I have dictionary of keys and a string of values. i.e. 8 Fixtures: Swin 1828 1844 1901 1916 1932 1948 2004 2019 2036 2052 2107 2122 HGrn 1148 1204 1218 1232 1247 1304 1319 1333 1351 Newc 1142 1157 1212 1227 1242 1258 1312 1327 1344 1403 Yarm 1833 1849 1906 1922 1937 1953 2009 2024 2041 2057 2112 BVue 1418 1437 1457 1517 1538 1558 1618 1637 1657 1717 1733 1747 1804 181 Hove 1408 1427 1447 1507 1528 1548 1608 1627 1647 1707 1722 1738 1756 181 Romfd 1930 1946 2003 2019 2035 2053 2109 2125 2141 2157 2213 2230 Sund 1839 1856 1911 1927 1943 1958 2014 2031 2047 2102 2117 I print that with the following: f = open(SummaryFile, 'a') header = "%d Fixtures, %d Races:\n" % len(FixtureDict.keys()) print header f.write(header) f.write("\n") for fixture, racetimes in FixtureDict.iteritems(): line = "%s\t%s" % (fixture, " ".join(racetimes)) print line f.write(line + "\n") f.write("\n") f.close() What I'd like to is add the number of values to the Header line. So how would I get i.e. 8 Fixtures, 93 Races I tried header = "%d Fixtures, %d Races:\n" % (len(FixtureDict.keys()), len(FixtureDict.values())) But I get print header >> 8 Fixture, 8 Races Any ideas? Cheers, Garry From steve at pearwood.info Wed Dec 22 13:31:39 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 22 Dec 2010 23:31:39 +1100 Subject: [Tutor] Dictionary Question In-Reply-To: References: Message-ID: <4D11EFAB.8030405@pearwood.info> Garry Bettle wrote: > Howdy all, > > Hope this message finds everyone well. > > I have dictionary of keys and a string of values. > > i.e. > > 8 Fixtures: I assume each fixture is a key, e.g. Swin, HGrn, etc. > Swin 1828 1844 1901 1916 1932 1948 2004 2019 2036 2052 2107 2122 > HGrn 1148 1204 1218 1232 1247 1304 1319 1333 1351 > Newc 1142 1157 1212 1227 1242 1258 1312 1327 1344 1403 > Yarm 1833 1849 1906 1922 1937 1953 2009 2024 2041 2057 2112 > BVue 1418 1437 1457 1517 1538 1558 1618 1637 1657 1717 1733 1747 1804 181 > Hove 1408 1427 1447 1507 1528 1548 1608 1627 1647 1707 1722 1738 1756 181 > Romfd 1930 1946 2003 2019 2035 2053 2109 2125 2141 2157 2213 2230 > Sund 1839 1856 1911 1927 1943 1958 2014 2031 2047 2102 2117 > > I print that with the following: > > f = open(SummaryFile, 'a') > header = "%d Fixtures, %d Races:\n" % len(FixtureDict.keys()) > print header > > f.write(header) > f.write("\n") > for fixture, racetimes in FixtureDict.iteritems(): > line = "%s\t%s" % (fixture, " ".join(racetimes)) According to your description, racetimes is already a single string, so using join on it would be the wrong thing to do: >>> racetimes = "1839 1856 1911" >>> " ".join(racetimes) '1 8 3 9 1 8 5 6 1 9 1 1' So what is racetimes? Is it a string, or is it a list of strings? ['1839', '1856', '1911'] I'm going to assume the latter. That's the right way to do it. > print line > f.write(line + "\n") > f.write("\n") > f.close() > > What I'd like to is add the number of values to the Header line. > > So how would I get i.e. > > 8 Fixtures, 93 Races > > I tried > > header = "%d Fixtures, %d Races:\n" % (len(FixtureDict.keys()), > len(FixtureDict.values())) > > But I get > > print header >>> 8 Fixture, 8 Races > > Any ideas? You need len(racetimes) rather than len(FixtureDict.values()). Every dict has exactly one value for every key, always, without exception. That is, len(dict.keys()) == len(dict.values()). In this case, the values are lists of multiple start times, but each list counts as one value. You need to count the number of items inside each value, not the number of values. In this case, you need to sum the number of races for all the fixtures: num_races = sum(len(racetimes) for racetimes in FixtureDict.values()) -- Steven From bgailer at gmail.com Wed Dec 22 16:11:25 2010 From: bgailer at gmail.com (bob gailer) Date: Wed, 22 Dec 2010 10:11:25 -0500 Subject: [Tutor] Dictionary Question In-Reply-To: <4D11EFAB.8030405@pearwood.info> References: <4D11EFAB.8030405@pearwood.info> Message-ID: <4D12151D.7070605@gmail.com> On 12/22/2010 7:31 AM, Steven D'Aprano wrote: Also note: len(dict.keys()) == len(dict.values()) == len(dict) -- Bob Gailer 919-636-4239 Chapel Hill NC From swiftone at swiftone.org Wed Dec 22 16:36:14 2010 From: swiftone at swiftone.org (Brett Ritter) Date: Wed, 22 Dec 2010 10:36:14 -0500 Subject: [Tutor] vim as a python editor In-Reply-To: References: Message-ID: On Wed, Dec 15, 2010 at 7:30 PM, Alan Gauld wrote: > I also use split screen view in vim so that within vim I often have > two or three buffers open at once all displayed in a split screen. I know you prefer "default" settings, but one mapping I tend to stick in my .vimrcs whereever I go: nmap w_ Which moves to the next screen and maximizes it. = will return to the equally distributed split screens. When coding an AJAX app, for example, I'll often have the HTML, the JS, the CSS, and the Python service, and the tests all up at once. I also use "screen" a lot because I'm often working on remote machines without X involved, plus it lets me bounce between shell (docs, git, etc) and editing easily with the added benefit of not having to worry about communication disruption (local power outage, etc) killing my work in progress. Often once I get an environment perfectly tailored (yay for virtualenv) I'll have a screen session manage the entire thing until the project is done, just reconnecting to it when I'm working on it. I highly recommend both screen and virtualenv to anyone that is unfamiliar with them. -- Brett Ritter / SwiftOne swiftone at swiftone.org From karim.liateni at free.fr Wed Dec 22 19:07:31 2010 From: karim.liateni at free.fr (Karim) Date: Wed, 22 Dec 2010 19:07:31 +0100 Subject: [Tutor] xml.etree.ElementTree.parse() against a XMLShema file Message-ID: <4D123E63.4040401@free.fr> Hello all, Is somebody has an example of the way to parse an xml file against a "grammary" file.xsd. The default parser is checking closing tags and attributes but I would like to validate a XSD file. I use the module ElementTree. Regards Karim From cheengshuchin at gmail.com Wed Dec 22 18:02:49 2010 From: cheengshuchin at gmail.com (Cheeng Shu Chin) Date: Thu, 23 Dec 2010 01:02:49 +0800 Subject: [Tutor] Pyserial and invalid handle Message-ID: Hi all, Last few days ago, I facing the same issue with my windows 7 64bit... it work well with windows XP 32-bit... FYI, I?m using python 2.6 amd64 bit as my core programming tools currently, I?m start study/learn some arduino robotic and rewrite Python-Arduino API I just do a quick anatomy to the Pyserial code and diff. of the (Ctypes) Handle range... found both are diff range with same Kernel32 dll function call, for 64-bit, ctypes.wintypes.HANDLE(-1) ==> c_void_p(18446744073709551615L) in 32 bit, give c_void_p(4294967295L) so what I guest it cause by Platform and environments issues. below is what I did and modifies on 64-bit server: a.. win32.py (C:\Python26\Lib\site-packages\serial) a.. use HRESULT to replace HANDLE for kernel32 function handle return b.. serialwin32.py (C:\Python26\Lib\site-packages\serial) a.. use win32file, pywintypes, win32event to rewrite readfile and writefile function it seem work now for my 64-bit server, somehow I didn?t put some effort on self.timeout == 0 for serial.... as I don?t have enough time for it.... that I will leave it back to Pyserial Owner to take the honor for that.... hope this will help to all open source member... (that why I?m like python most) Regards, Cheeng Shu Chin -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: serialwin32.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: win32.py URL: From karim.liateni at free.fr Wed Dec 22 19:28:52 2010 From: karim.liateni at free.fr (Karim) Date: Wed, 22 Dec 2010 19:28:52 +0100 Subject: [Tutor] xml.etree.ElementTree.parse() against a XMLShema file In-Reply-To: <4D123E63.4040401@free.fr> References: <4D123E63.4040401@free.fr> Message-ID: <4D124364.7090105@free.fr> On 12/22/2010 07:07 PM, Karim wrote: > > Is somebody has an example of the way to parse an xml file against a > "grammary" file.xsd. I found this: http://www.velocityreviews.com/forums/t695106-re-xml-parsing-with-python.html Stefan is it still true the limitation of etree in python 2.7.1 ? Regards Karim From stefan_ml at behnel.de Wed Dec 22 21:56:49 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 22 Dec 2010 21:56:49 +0100 Subject: [Tutor] xml.etree.ElementTree.parse() against a XMLShema file In-Reply-To: <4D124364.7090105@free.fr> References: <4D123E63.4040401@free.fr> <4D124364.7090105@free.fr> Message-ID: Karim, 22.12.2010 19:28: > On 12/22/2010 07:07 PM, Karim wrote: >> >> Is somebody has an example of the way to parse an xml file against a >> "grammary" file.xsd. > > I found this: > > http://www.velocityreviews.com/forums/t695106-re-xml-parsing-with-python.html > > Stefan is it still true the limitation of etree in python 2.7.1 ? Yes, ElementTree (which is in Python's stdlib) and lxml.etree are separate implementations. If you want validation, use the lxml package. Stefan From karim.liateni at free.fr Wed Dec 22 22:09:30 2010 From: karim.liateni at free.fr (Karim) Date: Wed, 22 Dec 2010 22:09:30 +0100 Subject: [Tutor] xml.etree.ElementTree.parse() against a XMLShema file In-Reply-To: References: <4D123E63.4040401@free.fr> <4D124364.7090105@free.fr> Message-ID: <4D12690A.6070306@free.fr> Thanks Stefan for answering. That's what I come up with. Using lxml (except for the different import) will be fully compliant with the ET code. Do I have to adapt it? I saw your fantastic benchmarks! Why the hell lxml is not integrated into the stdlib. I thought they put in it things which works at best for python interest ? Regards Karim On 12/22/2010 09:56 PM, Stefan Behnel wrote: > Karim, 22.12.2010 19:28: >> On 12/22/2010 07:07 PM, Karim wrote: >>> >>> Is somebody has an example of the way to parse an xml file against a >>> "grammary" file.xsd. >> >> I found this: >> >> http://www.velocityreviews.com/forums/t695106-re-xml-parsing-with-python.html >> >> >> Stefan is it still true the limitation of etree in python 2.7.1 ? > > Yes, ElementTree (which is in Python's stdlib) and lxml.etree are > separate implementations. If you want validation, use the lxml package. > > Stefan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From stefan_ml at behnel.de Wed Dec 22 22:32:13 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 22 Dec 2010 22:32:13 +0100 Subject: [Tutor] xml.etree.ElementTree.parse() against a XMLShema file In-Reply-To: <4D12690A.6070306@free.fr> References: <4D123E63.4040401@free.fr> <4D124364.7090105@free.fr> <4D12690A.6070306@free.fr> Message-ID: Karim, 22.12.2010 22:09: > Using lxml (except for the different import) will be fully compliant with > the ET code. > Do I have to adapt it? There are certain differences. http://codespeak.net/lxml/compatibility.html This page hasn't been changed for ages, but it should still be mostly accurate. > I saw your fantastic benchmarks! Why the hell lxml is not integrated into > the stdlib. > I thought they put in it things which works at best for python interest ? I proposed it but it was rejected with the argument that it's a huge dependency and brings in two large C libraries that will be hard to control for future long-term maintenance. I think that's a reasonable objection. Stefan From karim.liateni at free.fr Wed Dec 22 23:13:00 2010 From: karim.liateni at free.fr (Karim) Date: Wed, 22 Dec 2010 23:13:00 +0100 Subject: [Tutor] xml.etree.ElementTree.parse() against a XMLShema file In-Reply-To: References: <4D123E63.4040401@free.fr> <4D124364.7090105@free.fr> <4D12690A.6070306@free.fr> Message-ID: <4D1277EC.9030109@free.fr> On 12/22/2010 10:32 PM, Stefan Behnel wrote: > Karim, 22.12.2010 22:09: >> Using lxml (except for the different import) will be fully compliant >> with >> the ET code. >> Do I have to adapt it? > > There are certain differences. > > http://codespeak.net/lxml/compatibility.html > > This page hasn't been changed for ages, but it should still be mostly > accurate. > I will have a look. Anyway, I must delivered my current version. I got 300 lines of codes which should be easily translated for the improved future version. >> I saw your fantastic benchmarks! Why the hell lxml is not integrated >> into >> the stdlib. >> I thought they put in it things which works at best for python >> interest ? > > I proposed it but it was rejected with the argument that it's a huge > dependency and brings in two large C libraries that will be hard to > control for future long-term maintenance. I think that's a reasonable > objection. > One can never says never... They will reconsider it I think. Thanks for your support! Regards Karim From garry.bettle at gmail.com Wed Dec 22 23:26:46 2010 From: garry.bettle at gmail.com (Garry Bettle) Date: Wed, 22 Dec 2010 23:26:46 +0100 Subject: [Tutor] Dictionary Question Message-ID: On Wed, 22 Dec 2010 23:31:39 +1100, Steven D'Aprano wrote: > In this case, you need to sum the number of races for all the fixtures: > > num_races = sum(len(racetimes) for racetimes in FixtureDict.values()) Many thanks Steven for your explanation and final golden nugget of code. On Wed, 22 Dec 2010 10:11:25 -0500, bob gailer wrote: > > Also note: len(dict.keys()) == len(dict.values()) == len(dict) Yup, thanks Bob. Cheers, G. From matt.gregory at oregonstate.edu Thu Dec 23 01:34:38 2010 From: matt.gregory at oregonstate.edu (Matt Gregory) Date: Wed, 22 Dec 2010 16:34:38 -0800 Subject: [Tutor] calling setters of superclasses In-Reply-To: References: <1D673F86DDA00841A1216F04D1CE70D642680008E8@EXCH2.nws.oregonstate.edu> Message-ID: On 12/18/2010 2:06 AM, Peter Otten wrote: > I don't think /how/ you are trying it is stupid though I'm not so sure about > /what/ . Thank you all for very helpful suggestions. It took me a while to chew on this before I could respond. I learned a lot about descriptors and their interactions with properties that I hadn't fully understood before. Peter and Alan's advice to create a check method that is overridden in subclasses makes sense in order to avoid the naming conflicts. And I also like Hugo's idea of applying separate descriptor classes to handle the constraints introduced. That seems to be a flexible way of doing things. As far as the /what/, my example given was obviously contrived. I'm really trying to create classes for 2D envelopes that describe the bounding extent of spatial data. I have both Envelope and RasterEnvelope classes - the former being just a bounding box around any spatial data, the latter additionally specifying a raster cell size and being able to discern rows, columns, etc. I had been using the setter to do bounds checking on the Envelope class (e.g. make sure x_min isn't bigger than x_max, etc. and rolling back changes if so). For the RasterEnvelope class, I first wanted to call the Envelope bounds checking and then to adjust rows/columns if a bigger extent was requested. But you've successfully scared me away from using properties (in a hierarchical way at least) and I've been able to get what I need by just defining __setattr__ in both classes. Whether I did that correctly is a story for another thread ... matt From matt.gregory at oregonstate.edu Thu Dec 23 02:31:09 2010 From: matt.gregory at oregonstate.edu (Matt Gregory) Date: Wed, 22 Dec 2010 17:31:09 -0800 Subject: [Tutor] classes for setting 2D envelopes Message-ID: Hi all, I've been working on creating 2D bounding box (envelope) classes to describe spatial data. Variations of these are available in other spatial libraries (e.g. Shapely), although I haven't found envelopes specific to raster data that also specifies cell size. Could be I just haven't found them yet. I have two classes (Envelope and RasterEnvelope). Envelope specifies a bounding box and does error checking (through __setattr__) when a coordinate is changed. RasterEnvelope additionally specifies a cell size (along with n_rows and n_cols), does the bounds checking on a coordinate change and adjusts the spatial envelope accordingly (again through __setattr__). I've posted the code (and some unit tests) here: http://pastebin.com/Twf3RjWa So far things work, but I have a nagging feeling that there's too much work devoted to changing coordinates. In another thread I posted a while back (http://mail.python.org/pipermail/tutor/2010-August/077940.html), Steven D'Aprano recommended using immutable types for point coordinate data and I'm guessing the advice might be applicable here as well? If so, I'm a slow learner ;) There are also (at least) a couple of design flaws right now that could be remedied if I put more work into it: - A RasterEnvelope is 'pinned' by its upper-left coordinate and only changes to x_min or y_max will cause changes in this coordinate (by design but probably a limitation) - Changing cell size currently doesn't change the corresponding window because there is no specification as to whether n_cols/n_rows should change or x_max/y_min should change. Both these probably suggest creating new RasterEnvelope instances any time a coordinate changes? Any feedback would be welcome, so that I don't devote too much more time down a bad route. thanks, matt From modulok at gmail.com Thu Dec 23 08:49:04 2010 From: modulok at gmail.com (Modulok) Date: Thu, 23 Dec 2010 00:49:04 -0700 Subject: [Tutor] Weighted Random Choice - Anyone have an efficient algorithm? Message-ID: Does anyone know of an efficient way of doing a weighted random choice? (I don't even know what algorithms like this would be called.) Preferably, something that doesn't grow exponentially with the number of elements in the list, or the size of their respective values. For example: Assume I have a list of integer elements. I want to pick an index from that list, using the value of said elements to control the probability of selecting a given index: w = [5, 20, 75] wrand(w) Where wrand() will return '2', (the index of 75), about 75% of the time. It would return '1', (the index of 20) about 20% of the time, and 0, about 5% of the time. It could return the actual value, but returning the index seems more flexible. The implementation doesn't have to work exactly like this, even if the weight values don't add up to 100, or are arbitrary, that's fine. Hopefully you get the idea. Here's what I tried (it works, but is slow): ### Begin Code Example ### import random random.seed(2) #<-- So we can reproduce the sequence for testing. def wrandom(weights): ''' Return a randomly selected index of the 'weights' list, using the values of that list to affect probability of said index being returned. ''' debug = False flattened = [] for i, w in enumerate(weights): if debug: print "i, w: ", i, w for k in range(w): flattened.append(i) if debug: print "flattened: ", flattened rnd = random.randint(0, (len(flattened) - 1)) return flattened[rnd] # Not test it: print wrandom([5, 20, 75]) print wrandom([5, 20, 75]) print wrandom([5, 20, 75]) ### End Code Example ### It works and is easy enough to understand, but when the number of list items gets large, or the weights get heavy, things get ugly. -Modulok- From rasjidw at gmail.com Thu Dec 23 09:14:21 2010 From: rasjidw at gmail.com (Rasjid Wilcox) Date: Thu, 23 Dec 2010 19:14:21 +1100 Subject: [Tutor] Dynamically generated classes Message-ID: Hi all, I've been playing with dynamically generated classes. In particular: class ClassMaker(object): def __init__(maker_self, name): maker_self.name = name # class Foo(object): def __init__(self): self.parent = maker_self def whoami(self): print 'I am a Foo instance made by %s' % self.parent.name maker_self.Foo = Foo # class Bar(object): def __init__(self): self.parent = maker_self def whoami(self): print 'I am a Bar instance made by %s' % self.parent.name maker_self.Bar = Bar >>> a = ClassMaker('Alice') >>> b = ClassMaker('Bob') >>> af = a.Foo() >>> ab = a.Bar() >>> bf = b.Foo() >>> bb = b.Bar() >>> >>> af.whoami() I am a Foo instance made by Alice >>> ab.whoami() I am a Bar instance made by Alice >>> bf.whoami() I am a Foo instance made by Bob >>> bb.whoami() I am a Bar instance made by Bob >>> isinstance(bb, b.Bar) True >>> a.Foo is b.Foo False The actual use case is a system where there are multiple databases of essentially the same form, where a is database A, and b is database B, and Foo and Bar represent tables in both the databases. af would be the Foo table in database A, and bf would be the Foo table in database B. My question is: is there a better way? Based on my playing with the above, it all seems to do what I want. My only concern is that I've not seen this idiom before, and perhaps there is a simpler or more standard way? Cheers, Rasjid. From alan.gauld at btinternet.com Thu Dec 23 10:13:45 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 23 Dec 2010 09:13:45 -0000 Subject: [Tutor] Dynamically generated classes References: Message-ID: "Rasjid Wilcox" wrote > I've been playing with dynamically generated classes. In > particular: > > class ClassMaker(object): > def __init__(maker_self, name): > maker_self.name = name > # > class Foo(object): > def __init__(self): > self.parent = maker_self > def whoami(self): > print 'I am a Foo instance made by %s' % > self.parent.name > maker_self.Foo = Foo I'd probably just define the classes in a module rather than in the init method itself. Then pass the required class to the parent init as an argument. But the example code doesn't give enough clue as to how these classes will be used to be sure that will suit your need. >>>> a = ClassMaker('Alice') >>>> af = a.Foo() >>>> ab = a.Bar() >>>> af.whoami() > I am a Foo instance made by Alice >>>> ab.whoami() > I am a Bar instance made by Alice >>>> a.Foo is b.Foo > False > The actual use case is a system where there are multiple databases > of > essentially the same form, where a is database A, and b is database > B, > and Foo and Bar represent tables in both the databases. Unless you are actually building a database it would be unusual to have a class represent a table, a table normally represents a class - ie its not a two way relation - or the class uses a table to populate attributes. But again we have no clues about what the classes actually do - its normally the methods of the class that define its usage... > the Foo table in database A, and bf would be the Foo table in > database > B. That sounds like you may be trying to create a facade pattern? > My question is: is there a better way? Based on my playing with the > above, it all seems to do what I want. My only concern is that I've > not seen this idiom before, and perhaps there is a simpler or more > standard way? I think it will do what you want but I wouldn't bother putting the class definitions inside the init - unless there is a good reason. At the very least I would create two factory methods which are called by init. That would allow you to reset the classes if needed too. class A: def __init__(self, name): self.foo = makeFoo() self.bar = makeBar() def makeFoo(self): class Foo: pass return Foo def makeBar(self): class Bar: pass reurn Bar But I still think its much more flexible to define the classes in a separate module: import foo, bar class A: def __init__(self,foo,bar): self.foo = foo self.bar = bar a = A(foo.Foo(A),bar.Bar(A)) # pass A so Foo/Bar know their parent You can then extend the use of A without changing A by defining new derivatives of Foo and Bar (as you use new databases for example) HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From josep.m.fontana at gmail.com Thu Dec 23 10:25:01 2010 From: josep.m.fontana at gmail.com (Josep M. Fontana) Date: Thu, 23 Dec 2010 10:25:01 +0100 Subject: [Tutor] Problems processing accented characters in ISO-8859-1 encoded texts Message-ID: I am working with texts that are encoded as ISO-8859-1. I have included the following two lines at the beginning of my python script: !/usr/bin/env python # -*- coding: iso-8859-1 -*- If I'm not mistaken, this should tell Python that accented characters such as '?', '?', '?' or '?' should be considered as alpha-numeric characters and therefore matched with a regular expression of the form [a-zA-Z]. However, when I process my texts, all of the accented characters are matched as non alpha-numeric symbols. What am I doing wrong? I'm not including the whole script because I think the rest of the code is irrelevant. All that's relevant (I think) is that I'm using the regular expression '[^a-zA-Z\t\n\r\f\v]+' to match any string that includes non alpha-numeric characters and that returns '?', '?', '?' or '?' as well as other real non alpha-numeric characters. Has anybody else experienced this problem when working with texts encoded as ISO-8859-1 or UTF-8? Is there any additional flag or parameter that I should add to make the processing of these characters as regular word characters possible? Thanks in advance for your help. Josep M. From karim.liateni at free.fr Thu Dec 23 10:52:48 2010 From: karim.liateni at free.fr (Karim) Date: Thu, 23 Dec 2010 10:52:48 +0100 Subject: [Tutor] Dynamically generated classes In-Reply-To: References: Message-ID: <4D131BF0.9060206@free.fr> Hello Alan, You mentioned the facade pattern. Could you please provide a basic simple implementation of this pattern in python? Regards Karim On 12/23/2010 10:13 AM, Alan Gauld wrote: > > "Rasjid Wilcox" wrote > >> I've been playing with dynamically generated classes. In particular: >> >> class ClassMaker(object): >> def __init__(maker_self, name): >> maker_self.name = name >> # >> class Foo(object): >> def __init__(self): >> self.parent = maker_self >> def whoami(self): >> print 'I am a Foo instance made by %s' % self.parent.name >> maker_self.Foo = Foo > > I'd probably just define the classes in a module rather than in the > init method itself. Then pass the required class to the parent init > as an argument. But the example code doesn't give enough clue > as to how these classes will be used to be sure that will suit > your need. > >>>>> a = ClassMaker('Alice') >>>>> af = a.Foo() >>>>> ab = a.Bar() >>>>> af.whoami() >> I am a Foo instance made by Alice >>>>> ab.whoami() >> I am a Bar instance made by Alice >>>>> a.Foo is b.Foo >> False > >> The actual use case is a system where there are multiple databases of >> essentially the same form, where a is database A, and b is database B, >> and Foo and Bar represent tables in both the databases. > > Unless you are actually building a database it would be unusual to have > a class represent a table, a table normally represents a class - ie > its not > a two way relation - or the class uses a table to populate attributes. > But again we have no clues about what the classes actually do - its > normally the methods of the class that define its usage... > >> the Foo table in database A, and bf would be the Foo table in database >> B. > > That sounds like you may be trying to create a facade pattern? > >> My question is: is there a better way? Based on my playing with the >> above, it all seems to do what I want. My only concern is that I've >> not seen this idiom before, and perhaps there is a simpler or more >> standard way? > > I think it will do what you want but I wouldn't bother putting the class > definitions inside the init - unless there is a good reason. At the very > least I would create two factory methods which are called by init. > That would allow you to reset the classes if needed too. > > class A: > def __init__(self, name): > self.foo = makeFoo() > self.bar = makeBar() > def makeFoo(self): > class Foo: pass > return Foo > def makeBar(self): > class Bar: pass > reurn Bar > > But I still think its much more flexible to define the classes in a > separate module: > > import foo, bar > > class A: > def __init__(self,foo,bar): > self.foo = foo > self.bar = bar > > a = A(foo.Foo(A),bar.Bar(A)) # pass A so Foo/Bar know their parent > > You can then extend the use of A without changing A by defining > new derivatives of Foo and Bar (as you use new databases for > example) > > HTH, > From josep.m.fontana at gmail.com Thu Dec 23 10:54:02 2010 From: josep.m.fontana at gmail.com (Josep M. Fontana) Date: Thu, 23 Dec 2010 10:54:02 +0100 Subject: [Tutor] Problems processing accented characters in ISO-8859-1 encoded texts In-Reply-To: References: Message-ID: Sorry! Sorry! Sorry! I just found out this question had already been answered by Steven D'Aprano in another thread! The trick was to add '\w' besides [a-zA-Z]. Please, accept my apologies. I devote time to this project whenever I have some free time. I got very busy with other things at some point and I stopped working on that. When I started again today, I had not noticed that there was already an answer to the question I had posted a while ago that actually solved my problem. Thanks again Steven. You can consider the problem solved and this thread closed. Josep M. On Thu, Dec 23, 2010 at 10:25 AM, Josep M. Fontana wrote: > I am working with texts that are encoded as ISO-8859-1. I have > included the following two lines at the beginning of my python script: > > !/usr/bin/env python > # -*- coding: iso-8859-1 -*- > > If I'm not mistaken, this should tell Python that accented characters > such as '?', '?', '?' or '?' should be considered as alpha-numeric > characters and therefore matched with a regular expression of the form > [a-zA-Z]. However, when I process my texts, all of the accented > characters are matched as non alpha-numeric symbols. What am I doing > wrong? > > I'm not including the whole script because I think the rest of the > code is irrelevant. All that's relevant (I think) is that I'm using > the regular expression '[^a-zA-Z\t\n\r\f\v]+' to match any string that > includes non alpha-numeric characters and that returns '?', '?', '?' > or '?' as well as other real non alpha-numeric characters. > > Has anybody else experienced this problem when working with texts > encoded as ISO-8859-1 or UTF-8? Is there any additional flag or > parameter that I should add to make the processing of these characters > as regular word characters possible? > > Thanks in advance for your help. > > Josep M. > From steve at pearwood.info Thu Dec 23 11:51:42 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 23 Dec 2010 21:51:42 +1100 Subject: [Tutor] Problems processing accented characters in ISO-8859-1 encoded texts In-Reply-To: References: Message-ID: <4D1329BE.3010902@pearwood.info> Josep M. Fontana wrote: > I am working with texts that are encoded as ISO-8859-1. I have > included the following two lines at the beginning of my python script: > > !/usr/bin/env python > # -*- coding: iso-8859-1 -*- > > If I'm not mistaken, this should tell Python that accented characters > such as '?', '?', '?' or '?' should be considered as alpha-numeric > characters and therefore matched with a regular expression of the form > [a-zA-Z]. You are mistaken. a-zA-Z always means the ASCII A to Z, and nothing else. You are conflating three unrelated problems: (1) What encoding is used to convert the bytes on disk of the source code literals into characters? (2) What encoding is used for the data fed to the regex engine? (3) What characters does the regex engine consider to be alphanumeric? The encoding line only tells Python what encoding to use to read the source code. It has no effect on text read from files, or byte strings, or anything else. It is only to allow literals and identifiers to be decoded correctly, and has nothing to do with regular expressions. To match accented characters, you can do two things: (1) explicitly include the accented characters you care about in the regular expression; or (2) i. set the current LOCALE to a locale that includes the characters you care about; ii. search for the \w regex special sequence; and iii. include the ?L flag in the regex. In both cases, don't forget to use Unicode strings, not byte strings. For example: >>> text = u"...ab?yz..." >>> re.search(r'[a-zA-Z]+', text).group(0) u'ab' Setting the locale on its own isn't enough: >>> locale.setlocale(locale.LC_ALL, 'de_DE') 'de_DE' >>> re.search(r'[a-zA-Z]+', text).group(0) u'ab' Nor is using the locale-aware alphanumeric sequence, since the regex engine is still using the default C locale: >>> re.search(r'\w+', text).group(0) u'ab' But if you instruct the engine to use the current locale instead, then it works: >>> re.search(r'(?L)\w+', text).group(0) u'ab\xf6yz' (Don't be put off by the ugly printing representation of the unicode string. \xf6 is just the repr() of the character ?.) Oh, and just to prove my point that a-z is always ASCII, even with the locale set: >>> re.search(r'(?L)[a-zA-Z]+', text).group(0) u'ab' Note also that \w means alphanumeric, not just alpha, so it will also match digits. -- Steven From davea at ieee.org Thu Dec 23 12:00:08 2010 From: davea at ieee.org (Dave Angel) Date: Thu, 23 Dec 2010 06:00:08 -0500 Subject: [Tutor] Weighted Random Choice - Anyone have an efficient algorithm? In-Reply-To: References: Message-ID: <4D132BB8.2040603@ieee.org> On 01/-10/-28163 02:59 PM, Modulok wrote: > Does anyone know of an efficient way of doing a weighted random > choice? (I don't even know what algorithms like this would be called.) > Preferably, something that doesn't grow exponentially with the number > of elements in the list, or the size of their respective values. > > For example: Assume I have a list of integer elements. I want to pick > an index from that list, using the value of said elements to control > the probability of selecting a given index: > > w = [5, 20, 75] > wrand(w) > > Where wrand() will return '2', (the index of 75), about 75% of the time. > It would return '1', (the index of 20) about 20% of the time, and 0, > about 5% of the time. It could return the actual value, but returning > the index seems more flexible. > > The implementation doesn't have to work exactly like this, even if the > weight values don't add up to 100, or are arbitrary, that's fine. > Hopefully you get the idea. Here's what I tried (it works, but is > slow): > > ### Begin Code Example ### > import random > random.seed(2) #<-- So we can reproduce the sequence for testing. > > def wrandom(weights): > ''' > Return a randomly selected index of the 'weights' list, using the > values of that list to affect probability of said index being returned. > ''' > debug = False > flattened = [] > for i, w in enumerate(weights): > if debug: print "i, w: ", i, w > for k in range(w): > flattened.append(i) > if debug: print "flattened: ", flattened > rnd = random.randint(0, (len(flattened) - 1)) > return flattened[rnd] > > # Not test it: > print wrandom([5, 20, 75]) > print wrandom([5, 20, 75]) > print wrandom([5, 20, 75]) > > ### End Code Example ### > > It works and is easy enough to understand, but when the number of list > items gets large, or the weights get heavy, things get ugly. > -Modulok- > You're building a temporary list, then discarding it when the function returns. Your speed efficiency is likely to improve if you keep that "flattened list" and only do the conversion once. But the separate problem of that flattened list possibly being much larger than necessary is more interesting. What you probably want to build instead is a list of accumulated probabilities. It would be the same size as the input list, and in this case, it would contain 5, 25, and 100, respectively. The list is always monotonic, since none of your probabilities can legally be negative. Anyway, once you have that new list, you pick a random number in the range from 0 to newlist[-1], and do a binary search of the newlist for the first value > your random value. And if the list is small, you can just scan through it linearly, which is much less code. DaveA From josep.m.fontana at gmail.com Thu Dec 23 12:24:30 2010 From: josep.m.fontana at gmail.com (Josep M. Fontana) Date: Thu, 23 Dec 2010 12:24:30 +0100 Subject: [Tutor] Problems processing accented characters in ISO-8859-1 encoded texts In-Reply-To: <4D1329BE.3010902@pearwood.info> References: <4D1329BE.3010902@pearwood.info> Message-ID: Hi Steven, On Thu, Dec 23, 2010 at 11:51 AM, Steven D'Aprano wrote: > Note also that \w means alphanumeric, not just alpha, so it will also match > digits. I'm sorry you didn't get to read my next message because in there I said that you yourself had already solved my problem a few weeks ago. Then again, I'm not sorry you didn't read it in time because this way I have benefited again from your pearls of wisdom. In this message you clarified for me some conceptual problems I had and that is even more beneficial than just solving the problem at hand (how true that thing about "Don't give a man a fish. Teach him how to fish." Thanks! Just one more question. You say that \w means alphanumeric, not just alpha. Is there any expression that would mean "just alpha" and (given the appropriate LOCALE setting) would match 'a' and '?' but not '9'? Josep M. From steve at pearwood.info Thu Dec 23 12:46:32 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 23 Dec 2010 22:46:32 +1100 Subject: [Tutor] Problems processing accented characters in ISO-8859-1 encoded texts In-Reply-To: References: Message-ID: <4D133698.7070601@pearwood.info> Josep M. Fontana wrote: > Sorry! Sorry! Sorry! I just found out this question had already been > answered by Steven D'Aprano in another thread! The trick was to add > '\w' besides [a-zA-Z]. Hah ha! I didn't see this before I answered... I thought the question sounded familiar :) -- Steven From alan.gauld at btinternet.com Thu Dec 23 13:25:47 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 23 Dec 2010 12:25:47 -0000 Subject: [Tutor] Dynamically generated classes References: <4D131BF0.9060206@free.fr> Message-ID: "Karim" wrote > You mentioned the facade pattern. Could you please provide a basic > simple implementation of this pattern in python? Not without spending more time than I have available. But you can read about it on wikipedia, including Java example code. However I think I actually should have said an Adaptor pattern rather than a Facade... http://en.wikipedia.org/wiki/Adapter_pattern HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Thu Dec 23 13:30:50 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 23 Dec 2010 23:30:50 +1100 Subject: [Tutor] Weighted Random Choice - Anyone have an efficient algorithm? In-Reply-To: References: Message-ID: <4D1340FA.5040109@pearwood.info> Modulok wrote: > Does anyone know of an efficient way of doing a weighted random > choice? (I don't even know what algorithms like this would be called.) If you google for "python weighted random choice" you will find a number of hits. > Preferably, something that doesn't grow exponentially with the number > of elements in the list, or the size of their respective values. Here's one method that is linear on the number of elements: def weighted_choice(weights): total = sum(weights) p = random.uniform(0, total) # random float between 0 and total. assert 0.0 <= p <= total # Do a linear search for the right index value. If you have a # huge number of weights, a binary search may be faster. running_total = 0 for i, weight in enumerate(weights): running_total += weight if p <= running_total: return i And tested: >>> import random >>> weights = [5, 20, 75] >>> counts = {0:0, 1:0, 2:0} >>> for i in xrange(1000000): ... i = weighted_choice(weights) ... counts[i] += 1 ... >>> counts {0: 50252, 1: 199997, 2: 749751} >>> [n*1e6/100 for n in weights] # expected values [50000.0, 200000.0, 750000.0] As you can see, the results are very close to what should be expected, and of course the difference can be chalked up to random chance. -- Steven From karim.liateni at free.fr Thu Dec 23 13:54:57 2010 From: karim.liateni at free.fr (Karim) Date: Thu, 23 Dec 2010 13:54:57 +0100 Subject: [Tutor] Dynamically generated classes In-Reply-To: References: <4D131BF0.9060206@free.fr> Message-ID: <4D1346A1.5020502@free.fr> Thanks for the link. I use Adapter already for java to align to different interface. I was curious about the facade implementation. Regards Karim On 12/23/2010 01:25 PM, Alan Gauld wrote: > > "Karim" wrote >> You mentioned the facade pattern. Could you please provide a basic >> simple implementation of this pattern in python? > > Not without spending more time than I have available. > > But you can read about it on wikipedia, including Java example code. > > However I think I actually should have said an Adaptor pattern rather > than a Facade... > > http://en.wikipedia.org/wiki/Adapter_pattern > > HTH, > From rasjidw at gmail.com Thu Dec 23 15:33:18 2010 From: rasjidw at gmail.com (Rasjid Wilcox) Date: Fri, 24 Dec 2010 01:33:18 +1100 Subject: [Tutor] Dynamically generated classes In-Reply-To: References: Message-ID: On 23 December 2010 20:13, Alan Gauld wrote: > I think it will do what you want but I wouldn't bother putting the class > definitions inside the init - unless there is a good reason. At the very > least I would create two factory methods which are called by init. > That would allow you to reset the classes if needed too. Wonderful! It turns out a single factory method which can be called once for each table per database is all that I need. It has simplified the code significantly. Thank you very much. Rasjid. From karim.liateni at free.fr Thu Dec 23 16:08:00 2010 From: karim.liateni at free.fr (Karim) Date: Thu, 23 Dec 2010 16:08:00 +0100 Subject: [Tutor] Dynamically generated classes In-Reply-To: References: Message-ID: <4D1365D0.4030502@free.fr> On 12/23/2010 10:13 AM, Alan Gauld wrote: > > "Rasjid Wilcox" wrote > >> I've been playing with dynamically generated classes. In particular: >> >> class ClassMaker(object): >> def __init__(maker_self, name): >> maker_self.name = name >> # >> class Foo(object): >> def __init__(self): >> self.parent = maker_self >> def whoami(self): >> print 'I am a Foo instance made by %s' % self.parent.name >> maker_self.Foo = Foo > > I'd probably just define the classes in a module rather than in the > init method itself. Then pass the required class to the parent init > as an argument. But the example code doesn't give enough clue > as to how these classes will be used to be sure that will suit > your need. > >>>>> a = ClassMaker('Alice') >>>>> af = a.Foo() >>>>> ab = a.Bar() >>>>> af.whoami() >> I am a Foo instance made by Alice >>>>> ab.whoami() >> I am a Bar instance made by Alice >>>>> a.Foo is b.Foo >> False > >> The actual use case is a system where there are multiple databases of >> essentially the same form, where a is database A, and b is database B, >> and Foo and Bar represent tables in both the databases. > > Unless you are actually building a database it would be unusual to have > a class represent a table, a table normally represents a class - ie > its not > a two way relation - or the class uses a table to populate attributes. > But again we have no clues about what the classes actually do - its > normally the methods of the class that define its usage... > >> the Foo table in database A, and bf would be the Foo table in database >> B. > > That sounds like you may be trying to create a facade pattern? > >> My question is: is there a better way? Based on my playing with the >> above, it all seems to do what I want. My only concern is that I've >> not seen this idiom before, and perhaps there is a simpler or more >> standard way? > > I think it will do what you want but I wouldn't bother putting the class > definitions inside the init - unless there is a good reason. At the very > least I would create two factory methods which are called by init. > That would allow you to reset the classes if needed too. > > class A: > def __init__(self, name): > self.foo = makeFoo() > self.bar = makeBar() > def makeFoo(self): > class Foo: pass > return Foo > def makeBar(self): > class Bar: pass > reurn Bar > > But I still think its much more flexible to define the classes in a > separate module: > > import foo, bar > > class A: > def __init__(self,foo,bar): > self.foo = foo > self.bar = bar > > a = A(foo.Foo(A),bar.Bar(A)) # pass A so Foo/Bar know their parent > > You can then extend the use of A without changing A by defining > new derivatives of Foo and Bar (as you use new databases for > example) > > HTH, > By the way Alan your solution is simple and beautiful! I like this kind of flexible design. Regards Karim From ad.mat.cryer at gmail.com Thu Dec 23 16:22:05 2010 From: ad.mat.cryer at gmail.com (Adam Cryer) Date: Thu, 23 Dec 2010 15:22:05 +0000 Subject: [Tutor] paramiko error with put() Message-ID: Hi I am using paramiko 1.7.6 "fanny" on microsoft windows xp v2002 service pack3 with python 2.4.2 I have the follwing script: *import paramiko hostname='blah' port=22 username='blah' password='blah' fullpath='\\\\root\\path\\file.xls' remotepath='/inbox/file.xls' self.client= paramiko.SSHClient() self.client.load_system_host_keys() self.client.connect(hostname,port,username,password) sftp = self.client.open_sftp() sftp.put(fullpath,remotepath)* and i get the following error: *sftp.put(fullpath,remotepath)) File "build\bdist.win32\egg\paramiko\sftp_client.py", line 577, in put File "build\bdist.win32\egg\paramiko\sftp_client.py", line 337, in stat File "build\bdist.win32\egg\paramiko\sftp_client.py", line 628, in _request File "build\bdist.win32\egg\paramiko\sftp_client.py", line 675, in _read_response File "build\bdist.win32\egg\paramiko\sftp_client.py", line 701, in _convert_status IOError: [Errno 2] /inbox/file.xls is not a valid file path** * but the path definitely exists (I can move into it using sftp.chdir('inbox')) I have also tried moving into the folder and using put() but I get the exact same error (did take out inbox prefix) Has anyone had this issue? I would really appreciate you help Thanks Ad -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Fri Dec 24 01:27:02 2010 From: evert.rol at gmail.com (Evert Rol) Date: Fri, 24 Dec 2010 01:27:02 +0100 Subject: [Tutor] paramiko error with put() In-Reply-To: References: Message-ID: I don't really know the answer, but more a question/note (below): > Hi I am using paramiko 1.7.6 "fanny" on microsoft windows xp v2002 service pack3 with python 2.4.2 > > I have the follwing script: > import paramiko > > hostname='blah' > port=22 > username='blah' > password='blah' > fullpath='\\\\root\\path\\file.xls' > remotepath='/inbox/file.xls' > and i get the following error: > IOError: [Errno 2] /inbox/file.xls is not a valid file path > > but the path definitely exists (I can move into it using sftp.chdir('inbox')) I have also tried moving into the folder and using put() but I get the exact same error (did take out inbox prefix) chdir('inbox') seems like a relative path, while '/inbox/file.xls' is absolute. Could that be an issue, or does chdir() always translate to absolute path? Because, depending on your sftp server, you may (likely) not land in the root dir when connecting. (I've also never seen /inbox in the root dir, so './inbox/file.xls' does seem the more logical choice to me) As said, no idea really, but this is the only thing that I notice. Cheers, Evert > > Has anyone had this issue? > > > I would really appreciate you help > > Thanks > Ad > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From logan at loganmcgrath.com Fri Dec 24 03:14:18 2010 From: logan at loganmcgrath.com (Logan McGrath) Date: Thu, 23 Dec 2010 20:14:18 -0600 Subject: [Tutor] Python C API - Defining New Classes with Multiple Inheritance Message-ID: <4D1401FA.8010407@loganmcgrath.com> Hi, I've just started using the Python C API for version 2.7.1, and I've got a question! How do you define a new type which inherits from multiple types? I've been browsing the source code for Python 2.7.1 but I'm having a tough time finding examples. I see that MySQLdb defines low-level classes in the module "_mysql" using the C API, then extends from them using Python, but I want to keep as much of this in C as I can. Any help would be much appreciated! Thanks, Logan From fomcl at yahoo.com Fri Dec 24 10:40:06 2010 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 24 Dec 2010 01:40:06 -0800 (PST) Subject: [Tutor] Weighted Random Choice - Anyone have an efficient algorithm? In-Reply-To: <4D1340FA.5040109@pearwood.info> References: <4D1340FA.5040109@pearwood.info> Message-ID: <958523.6920.qm@web110709.mail.gq1.yahoo.com> Hi Steven, Doesn't this qualify as 'monkeying with the loop index'? [*] >>> import random >>> weights = [5, 20, 75] >>> counts = {0:0, 1:0, 2:0} >>> for i in xrange(1000000): ... i = weighted_choice(weights) # <--- monkeying right here (?) ... counts[i] += 1 [*] http://stackoverflow.com/questions/457036/dont-monkey-with-the-loop-index Cheers!! 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: Steven D'Aprano To: tutor at python.org Sent: Thu, December 23, 2010 1:30:50 PM Subject: Re: [Tutor] Weighted Random Choice - Anyone have an efficient algorithm? Modulok wrote: > Does anyone know of an efficient way of doing a weighted random > choice? (I don't even know what algorithms like this would be called.) If you google for "python weighted random choice" you will find a number of hits. > Preferably, something that doesn't grow exponentially with the number > of elements in the list, or the size of their respective values. Here's one method that is linear on the number of elements: def weighted_choice(weights): total = sum(weights) p = random.uniform(0, total) # random float between 0 and total. assert 0.0 <= p <= total # Do a linear search for the right index value. If you have a # huge number of weights, a binary search may be faster. running_total = 0 for i, weight in enumerate(weights): running_total += weight if p <= running_total: return i And tested: >>> import random >>> weights = [5, 20, 75] >>> counts = {0:0, 1:0, 2:0} >>> for i in xrange(1000000): ... i = weighted_choice(weights) ... counts[i] += 1 ... >>> counts {0: 50252, 1: 199997, 2: 749751} >>> [n*1e6/100 for n in weights] # expected values [50000.0, 200000.0, 750000.0] As you can see, the results are very close to what should be expected, and of course the difference can be chalked up to random chance. -- 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 davea at ieee.org Fri Dec 24 13:55:46 2010 From: davea at ieee.org (Dave Angel) Date: Fri, 24 Dec 2010 07:55:46 -0500 Subject: [Tutor] Weighted Random Choice - Anyone have an efficient algorithm? In-Reply-To: <958523.6920.qm@web110709.mail.gq1.yahoo.com> References: <4D1340FA.5040109@pearwood.info> <958523.6920.qm@web110709.mail.gq1.yahoo.com> Message-ID: <4D149852.5010505@ieee.org> On 01/-10/-28163 02:59 PM, Albert-Jan Roskam wrote: > Hi Steven, > > Doesn't this qualify as 'monkeying with the loop index'? [*] > >>>> import random >>>> weights = [5, 20, 75] >>>> counts = {0:0, 1:0, 2:0} >>>> for i in xrange(1000000): > ... i = weighted_choice(weights) #<--- monkeying right here (?) > ... counts[i] += 1 > > [*] http://stackoverflow.com/questions/457036/dont-monkey-with-the-loop-index > (please don't top-post in these forums) Your citation is for C#, where the loop index does indeed get changed. In Python a loop protects itself against such monkeying, so the example still executes a million times. It still would be better to use a different temp variable for clarity, but it's not as important as in other languages. DaveA From steve at pearwood.info Fri Dec 24 14:13:58 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 25 Dec 2010 00:13:58 +1100 Subject: [Tutor] Problems processing accented characters in ISO-8859-1 encoded texts In-Reply-To: References: <4D1329BE.3010902@pearwood.info> Message-ID: <4D149C96.6050403@pearwood.info> Josep M. Fontana wrote: > Just one more question. You say that \w means alphanumeric, not just > alpha. Is there any expression that would mean "just alpha" and (given > the appropriate LOCALE setting) would match 'a' and '?' but not '9'? Unfortunately, I don't think there is a standard code for just alpha. Apart from listing all the characters individually, I haven't been able to find a way to get the result you want, but I'll admit I haven't tried that hard. Perhaps somebody with more regex skills can answer? -- Steven From alan.gauld at btinternet.com Fri Dec 24 18:57:33 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 24 Dec 2010 17:57:33 -0000 Subject: [Tutor] Weighted Random Choice - Anyone have an efficientalgorithm? References: <4D1340FA.5040109@pearwood.info> <958523.6920.qm@web110709.mail.gq1.yahoo.com> Message-ID: "Albert-Jan Roskam" wrote > Doesn't this qualify as 'monkeying with the loop index'? [*] > >>>> import random >>>> weights = [5, 20, 75] >>>> counts = {0:0, 1:0, 2:0} >>>> for i in xrange(1000000): > ... i = weighted_choice(weights) # <--- monkeying right here (?) > ... counts[i] += 1 Not really because the for loop value is not being used as an index. It could just as well have been written: >>>> for n in xrange(1000000): > ... i = weighted_choice(weights) # <--- monkeying right here (?) > ... counts[i] += 1 The n is not used except to ensure there are a million iterations. So reusing the name inside the loop body doesn't have any bad effects. But keeping to a separate name might have been slightly more readable. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From bermanrl at cfl.rr.com Fri Dec 24 20:09:57 2010 From: bermanrl at cfl.rr.com (Robert Berman) Date: Fri, 24 Dec 2010 14:09:57 -0500 Subject: [Tutor] A class list Message-ID: <000101cba39e$27abc580$77035080$@rr.com> I am working on the second part of the 'Bingo' problem defined at the Bingo Praxis web page, http://programmingpraxis.com/2009/02/19/bingo/. My notes as how to best define and build the problem: 'In a large game with five hundred cards in play, what is the average number of calls required before any card achieves bingo?' In this version of the simulation we generate 500 unique cards which, by definition, implies 500 players. Initially, the rules insist there is only one pass through the cage. Since there are 500 players, we have to define a methodology such that the order of play is random. We first create 500 cards and assign them to a list so that the first card is referenced by cardlist[0] and the last card is represented by cardlist[499]. We then generate a random non-replaceable list of these same values (0-499). This becomes the ordering queue of which card will be played with the current value pulled from the cage. list.pop() determines the card in action. Since we only care about the game until it is won, as soon as one card has bingo, the entire game is finished and the value of all other cards is not considered in the simulation. The call counter is incremented every time the cage is accessed. I am building the program in incremental steps with each step to accomplish a set functionality. Each step I hope is designed to be tested with relative simplicity and rather obvious points of success and failure. The first function is simply to create an array(list) of classes. Rather than testing for 500 cards in the list, I am currently using 5 cards. Both the class and the list building function can be seen at: http://pastebin.com/QNPVec8L . The problem that is confounding me is that the list is returned where any member of bingocard.mycard is identical to all the other members of bingocard.mycard in the list. Through a number of tests I have learned that the last card of the list seems to permeate upward to the first. I have researched building a list of classes and I think I have done it correctly, but obviously at this point I am not going to say it should work. What I am asking is 1) Why is the function doing what it is doing rather than what I thought I programmed it to do, and 2) How can I code it to do what I want it to do: Produce N number of non duplicated cards. OS: Windows 7. Python version 2.6.4 IDE: Pycharm 1.1 EAP Thank you for any and all assistance. Robert -- I am using the free version of SPAMfighter. We are a community of 7 million users fighting spam. SPAMfighter has removed 174 of my spam emails to date. Get the free SPAMfighter here: http://www.spamfighter.com/len The Professional version does not have this message From bermanrl at cfl.rr.com Fri Dec 24 22:04:44 2010 From: bermanrl at cfl.rr.com (Robert Berman) Date: Fri, 24 Dec 2010 16:04:44 -0500 Subject: [Tutor] A class list In-Reply-To: References: <000101cba39e$27abc580$77035080$@rr.com> Message-ID: <001101cba3ae$310e0330$932a0990$@rr.com> From: Noah Hall [mailto:enalicho at gmail.com] Sent: Friday, December 24, 2010 2:36 PM To: Robert Berman Subject: Re: [Tutor] A class list Alright, I'll begin by saying that you shouldn't use a list for cards , and a separate one for players. Instead, you should use dictionaries. For example - players = { 1: "John", 2:"Phil",..,n:"Ed"} Or, using your method - for x in random.sample(xrange(low,high),n): players[x] = random.sample(list_of_names, len(list_of_names)) When I get back from holiday, I'll go through your code more thoroughly and give you a pointer in the right direction. >> Really big snip. Noah, Thanks for your prompt reply which generates its own unique question. You are saying I should have some concern for the players. But I don't care about the people playing. I care about the cards in play and the card that reaches Bingo. I care about the winner only to the extent I want to know how many numbers have been pulled from the cage for him/her to achieve Bingo. Other than that, I don't care so please elaborate why I care about the players; Besides I really cannot see why I should generate names(n) when card(n) will do just as nicely; I think. Thanks again, Robert _____ I am using the Free version of SPAMfighter . SPAMfighter has removed 174 of my spam emails to date. Do you have a slow PC? Try free scan! -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Fri Dec 24 22:12:18 2010 From: emile at fenx.com (Emile van Sebille) Date: Fri, 24 Dec 2010 13:12:18 -0800 Subject: [Tutor] A class list In-Reply-To: <000101cba39e$27abc580$77035080$@rr.com> References: <000101cba39e$27abc580$77035080$@rr.com> Message-ID: On 12/24/2010 11:09 AM Robert Berman said... > 1) Why is the function doing > what it is doing rather than what I thought I programmed it to do, you've only got one mycard at the class level that's shared between the instances. > and 2) How can I code it to do what I want it to do: Produce N > number of non duplicated cards. I swapped colranges (which can be shared) and mycard (which needs to be an instance variable.) HTH, Emile class bingocard: colranges = [ [1,15], [16,30], [31,45], [46,60], [61,75] ] nbrcalls = 0 nbrhits = 1 def __init__(self): self.mycard = [ [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0] ] col = 0 while col < 5: row = 0 low = bingocard.colranges[col][0] high = bingocard.colranges[col][1] vallst = random.sample(range(low,high),5) for x in vallst: self.mycard[row][col] = x row += 1 col += 1 self.mycard[2][2] += 100 From enalicho at gmail.com Fri Dec 24 22:26:12 2010 From: enalicho at gmail.com (Noah Hall) Date: Fri, 24 Dec 2010 21:26:12 +0000 Subject: [Tutor] A class list In-Reply-To: <001101cba3ae$310e0330$932a0990$@rr.com> References: <000101cba39e$27abc580$77035080$@rr.com> <001101cba3ae$310e0330$932a0990$@rr.com> Message-ID: Based on what you initally stated, that you would have a list containing 500 numbers, and for each number, there would be a player, using a dict would be ideal. For example, once you've got the winning number, how do you then intend on handling what happens to each player as they win? Do you intend to forget them? The player index need not be a name - it could be a number, simply referring to the player, and then storing each number that player stores in a list, for example players = {1:[0,0,0,0,0]....n:[0,0,0,0,0]} This is just some food for thought. -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at ieee.org Fri Dec 24 23:23:05 2010 From: davea at ieee.org (Dave Angel) Date: Fri, 24 Dec 2010 17:23:05 -0500 Subject: [Tutor] A class list In-Reply-To: <000101cba39e$27abc580$77035080$@rr.com> References: <000101cba39e$27abc580$77035080$@rr.com> Message-ID: <4D151D49.9080101@ieee.org> On 01/-10/-28163 02:59 PM, Robert Berman wrote: > I am working on the second part of the 'Bingo' problem defined at the Bingo > Praxis web page, http://programmingpraxis.com/2009/02/19/bingo/. > > My notes as how to best define and build the problem: > > 'In a large game with five hundred cards in play, what is the average number > of calls required before any card achieves bingo?' > > In this version of the simulation we generate 500 unique cards which, by > definition, implies 500 players. Initially, the rules insist there is only one > pass through the cage. Since there are 500 players, we have to define a > methodology such that the order of play is random. We first create 500 cards > and assign them to a list so that the first card is referenced by cardlist[0] > and the last card is represented by cardlist[499]. > We then generate a random non-replaceable list of these same values (0-499). > This becomes the ordering queue of which card will be played with the current > value pulled from the cage. list.pop() determines the card in action. > Since we only care about the game until it is won, as soon as one card has > bingo, the entire game is finished and the value of all other cards is not > considered in the simulation. > The call counter is incremented every time the cage is accessed. > > I am building the program in incremental steps with each step to accomplish a > set functionality. Each step I hope is designed to be tested with relative > simplicity and rather obvious points of success and failure. The first > function is simply to create an array(list) of classes. Rather than testing > for 500 cards in the list, I am currently using 5 cards. > > Both the class and the list building function can be seen at: > http://pastebin.com/QNPVec8L . > > The problem that is confounding me is that the list is returned where any > member of bingocard.mycard is identical to all the other members of > bingocard.mycard in the list. > Through a number of tests I have learned that the last card of the list seems > to permeate upward to the first. I have researched building a list of classes > and I think I have done it correctly, but obviously at this point I am not > going to say it should work. What I am asking is 1) Why is the function doing > what it is doing rather than what I thought I programmed it to do, and 2) How > can I code it to do what I want it to do: Produce N number of non duplicated > cards. > > OS: Windows 7. > Python version 2.6.4 > IDE: Pycharm 1.1 EAP > > Thank you for any and all assistance. > > Robert > > I didn't study the whole code listing, but there's an obvious problem you need to address first. You only have one instance of the mycard list. You made it a class attribute, while you meant to make it an instance attribute. So your first change should be to move that code inside the __init__ method, and of course precede it with "self". DaveA From stefan_ml at behnel.de Sat Dec 25 11:49:27 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 25 Dec 2010 11:49:27 +0100 Subject: [Tutor] Python C API - Defining New Classes with Multiple Inheritance In-Reply-To: <4D1401FA.8010407@loganmcgrath.com> References: <4D1401FA.8010407@loganmcgrath.com> Message-ID: Logan McGrath, 24.12.2010 03:14: > Hi, I've just started using the Python C API for version 2.7.1, and I've > got a question! > > How do you define a new type which inherits from multiple types? You can do this for Python classes, but not for C implemented types (which are single inheritance by design). Note that you can create both from C code, though, so you can just use a Python class if you need multiple inheritance and C types for everything else (e.g. for the base classes). > I've been > browsing the source code for Python 2.7.1 but I'm having a tough time > finding examples. I see that MySQLdb defines low-level classes in the > module "_mysql" using the C API, then extends from them using Python, but I > want to keep as much of this in C as I can. > > Any help would be much appreciated! The best advice I can give is to use Cython. It's basically a Python compiler that generates fast C code for extension modules. So you can just write your classes in Python and let Cython compile them for you. That way, you get about the same speed as with hand written C code (often faster, sometimes slower), but with substantially less coding. Stefan From timomlists at gmail.com Sun Dec 26 16:09:05 2010 From: timomlists at gmail.com (Timo) Date: Sun, 26 Dec 2010 16:09:05 +0100 Subject: [Tutor] Opening and closing SQLite db Message-ID: Hello, I use SQLite in my application as database, but I think I'm doing something wrong. Currently, I open a connection and cursor object every time I do an operation (insert, select, update, ...) and close it afterwards. Now I was wondering if it wouldn't be better to open the connection if my application starts, and commit and close when the user quits. Is this save? Can data be selected if I haven't commit anything? Cheers, Timo -------------- next part -------------- An HTML attachment was scrubbed... URL: From cfuller084 at thinkingplanet.net Sun Dec 26 16:21:20 2010 From: cfuller084 at thinkingplanet.net (Chris Fuller) Date: Sun, 26 Dec 2010 09:21:20 -0600 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: References: Message-ID: <201012260921.23733.cfuller084@thinkingplanet.net> You can call the commit() method of the connection object to ensure that any changes are saved to disk, but it's strictly optional. Closely/reopening is not necessary. Cheers On Sunday 26 December 2010, Timo wrote: > Hello, I use SQLite in my application as database, but I think I'm doing > something wrong. > > Currently, I open a connection and cursor object every time I do an > operation (insert, select, update, ...) and close it afterwards. Now I was > wondering if it wouldn't be better to open the connection if my application > starts, and commit and close when the user quits. > Is this save? Can data be selected if I haven't commit anything? > > Cheers, > Timo From smokefloat at gmail.com Sun Dec 26 17:04:36 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 26 Dec 2010 11:04:36 -0500 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: <201012260921.23733.cfuller084@thinkingplanet.net> References: <201012260921.23733.cfuller084@thinkingplanet.net> Message-ID: Just for future reference, when it's part of the python standard library, always go to the docs for the full functions and methods available. http://docs.python.org/library/sqlite3.html It can make the usage a lot easier. From enalicho at gmail.com Sun Dec 26 17:16:41 2010 From: enalicho at gmail.com (Noah Hall) Date: Sun, 26 Dec 2010 16:16:41 +0000 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: References: Message-ID: As a rule, I tend to open and close a connection based on what I'm doing with the database. If I were to directly run a series of queries one after another, I would keep it open until a change in interface (referring to changes in objects, both masters and slaves). Also, you can perform any query that does not change any records safely without a commit, for example, searches. For everything else, you need to call commit - but you don't need to close the connection. -------------- next part -------------- An HTML attachment was scrubbed... URL: From smokefloat at gmail.com Sun Dec 26 17:27:12 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 26 Dec 2010 11:27:12 -0500 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: References: Message-ID: On Sun, Dec 26, 2010 at 11:16 AM, Noah Hall wrote: > As a rule, I tend to open and close a connection based on what I'm doing > with the database. I +1 that. Usually if I open the db in a function, I commit and close. If it's in an app, for me personally, that should be the only place it takes place. So insert into, or call from would seem more likely functions to retrieve in a certain situation, like opening a project file would open the project db file, load the info, and then commit/close. Then saving would open commit/close the required info. If I were to directly run a series of queries one after > another, I would keep it open until a change in interface (referring?to > changes in objects, both masters and slaves). > Also, you can perform any query that does not change any records safely > without a commit, for example, searches. For everything else, you need to > call commit - but you don't need to close the connection. Nor open one, if it's not necessary. Load the info on file open, do what the user does, then save through open close. Unless intermittent saves are necessary within the app loop. > _______________________________________________ > Tutor maillist ?- ?Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- They're installing the breathalyzer on my email account next week. From smokefloat at gmail.com Sun Dec 26 17:45:22 2010 From: smokefloat at gmail.com (David Hutto) Date: Sun, 26 Dec 2010 11:45:22 -0500 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: References: Message-ID: Unless intermittent > saves are necessary within the app loop. Which is suggestible just in case the app or program crashes during use. From enalicho at gmail.com Sun Dec 26 19:31:17 2010 From: enalicho at gmail.com (Noah Hall) Date: Sun, 26 Dec 2010 18:31:17 +0000 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: References: Message-ID: > > >Which is suggestible just in case the app or program crashes during use. > (To O.P) Indeed, though in such cases you must rely on your programmers instinct to make the right decision - what applies for certain instances of an application doesn't always conform with what applies for other instances. For example, committing every event is hardly ideal for something such as a media player, but may be correct for something handling vital data to the system. There's no "one fits all feet" rule for such things - as you program and develop more and more applications and programs, listen to your users, and your bug tests. It'll come almost naturally after a while. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ben.ganzfried at gmail.com Sun Dec 26 22:15:39 2010 From: ben.ganzfried at gmail.com (Ben Ganzfried) Date: Sun, 26 Dec 2010 16:15:39 -0500 Subject: [Tutor] Java Virtual Machine Launcher Question Message-ID: Happy holidays everyone! My current question relates to a broader python programming question I have and so I thought it was worth posting here. I'm trying to read a single MAGE-TAB file (http://www.mged.org/mage-tab/) and acquire metadata. I just downloaded WinRAR to open zip files, but when trying to open the .jar file for my converter, I get the following message: "Java Virtual Machine Launcher: Failed to load Main-Class manifest attribute from C:\Users\Ben\AppData\Local\Temp\Rar$DI01.718\isatools_deps.jar" If anyone could help me better understand how to fix this, what is happening and/or point me in the right direction, I would be very much obliged. Thank you very much! Ben From steve at pearwood.info Mon Dec 27 05:14:39 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 27 Dec 2010 15:14:39 +1100 Subject: [Tutor] Java Virtual Machine Launcher Question In-Reply-To: References: Message-ID: <4D1812AF.5050808@pearwood.info> Apologies if you get (half of) this message twice, I'm having trouble with Thunderbird. Ben Ganzfried wrote: > Happy holidays everyone! > > My current question relates to a broader python programming question I > have and so I thought it was worth posting here. I'm trying to read a > single MAGE-TAB file (http://www.mged.org/mage-tab/) and acquire > metadata. This doesn't sound like it has anything to do with Python at all. > I just downloaded WinRAR to open zip files, but when trying to open > the .jar file for my converter, I get the following message: What is your converter? How are you trying to open the file? Double-clicking (that's my bet), choosing Open from the WinRAR menu, right-click in Windows Explorer, or something else? > "Java Virtual Machine Launcher: > > Failed to load Main-Class manifest attribute from > C:\Users\Ben\AppData\Local\Temp\Rar$DI01.718\isatools_deps.jar" This sounds like Windows is trying to run the Java file rather than open it in WinRAR. If you google on the error message: "failed to load main-class manifest attribute from" you'll find lots of questions that basically say "Whenever I try to run my .jar file...". So I'm pretty sure that you may have *installed* WinRAR, but you're not *using* WinRAR. > If anyone could help me better understand how to fix this, what is > happening and/or point me in the right direction, I would be very much > obliged. Lesson one: Google on the error message. Lesson two: you need to be more specific in what you're doing. It's not enough to say "I'm trying to open the file..." -- you may be *trying*, but are you sure you're succeeding? Is there any sign that WinRAR is actually involved? Good luck! -- Steven From info.chrystal at gmail.com Mon Dec 27 14:44:19 2010 From: info.chrystal at gmail.com (Chrystal) Date: Mon, 27 Dec 2010 13:44:19 +0000 Subject: [Tutor] test In-Reply-To: References: Message-ID: Hi guys I'll be happy if someone can help evaluate the result of this statement: for n in range (3, 20): > for x in range (2, n): > print (n) > I tried but couldn't figure out why the loop returned such a result Merry Christmas Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From paolino.gianrossi at gmail.com Mon Dec 27 14:52:06 2010 From: paolino.gianrossi at gmail.com (Paolino Gianrossi) Date: Mon, 27 Dec 2010 14:52:06 +0100 Subject: [Tutor] test In-Reply-To: References: Message-ID: <4D189A06.6000507@gmail.com> Il 27/12/2010 14.44, Chrystal ha scritto: > Hi guys > > I'll be happy if someone can help evaluate the result of this statement: > > for n in range (3, 20): > for x in range (2, n): > print (n) > > > I tried but couldn't figure out why the loop returned such a result > > Merry Christmas > > Thanks > > What result is so strange for you? I get a perfectly understandable 3 4 4 5 5 5 6 6 6 6 7 7 7 7 7 ... And so on up to 19 repeated 17 times... cheers paolino From steve at pearwood.info Mon Dec 27 14:58:41 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 28 Dec 2010 00:58:41 +1100 Subject: [Tutor] test In-Reply-To: References: Message-ID: <4D189B91.3010109@pearwood.info> Chrystal wrote: > Hi guys > > I'll be happy if someone can help evaluate the result of this statement: > > for n in range (3, 20): >> for x in range (2, n): >> print (n) >> > > I tried but couldn't figure out why the loop returned such a result It would help if you told us what result you get, what result you expected, and what you don't understand about it. My guess is that you expected it to print: 2 2 3 2 3 4 2 3 4 5 ... but instead it prints: 2 3 3 4 4 4 5 5 5 5 ... Hint: you are print n each time, not x. n doesn't vary inside the inner loop, only in the outer loop. To understand what is going on better, it might help if you run this instead: for n in range(3, 20): for x in range(2, n): print('n = %d, x = %d' % (n, x)) -- Steven From frankchang91 at gmail.com Tue Dec 28 02:25:36 2010 From: frankchang91 at gmail.com (Frank Chang) Date: Mon, 27 Dec 2010 17:25:36 -0800 Subject: [Tutor] Python 2.7.1 interpreter complains about NameError: global name 'levenshtein_automata' is not defined Message-ID: Good morning, I am using Python 2.7.1 on Windows XP Service Pack 3. Here is the program where the Python interpreter complains about NameError: global name 'levenshtein_automata' is not defined. The python 2,7.1 error message is: Traceback (most recent call last): File "automata_test.py", line 174, in length = len(list(testdfa.find_all_matches('food', 1, m))) File "automata_test.py", line 145, in find_all_matches lev = levenshtein_automata(word, k).to_dfa() NameError: global name 'levenshtein_automata' is not defined Here is the program. I have marked lines 174, 145 and 125. import bisect import random class NFA(object): EPSILON = object() ANY = object() def __init__(self, start_state): self.transitions = {} self.final_states = set() self._start_state = start_state @property def start_state(self): return frozenset(self._expand(set([self._start_state]))) def add_transition(self, src, input, dest): self.transitions.setdefault(src, {}).setdefault(input, set()).add(dest) def add_final_state(self, state): self.final_states.add(state) def is_final(self, states): return self.final_states.intersection(states) def _expand(self, states): frontier = set(states) while frontier: state = frontier.pop() new_states = self.transitions.get(state, {}).get(NFA.EPSILON, set()).difference(states) frontier.update(new_states) states.update(new_states) return states def next_state(self, states, input): dest_states = set() for state in states: state_transitions = self.transitions.get(state, {}) dest_states.update(state_transitions.get(input, [])) dest_states.update(state_transitions.get(NFA.ANY, [])) return frozenset(self._expand(dest_states)) def get_inputs(self, states): inputs = set() for state in states: inputs.update(self.transitions.get(state, {}).keys()) return inputs def to_dfa(self): dfa = DFA(self.start_state) frontier = [self.start_state] seen = set() while frontier: current = frontier.pop() inputs = self.get_inputs(current) for input in inputs: if input == NFA.EPSILON: continue new_state = self.next_state(current, input) if new_state not in seen: frontier.append(new_state) seen.add(new_state) if self.is_final(new_state): dfa.add_final_state(new_state) if input == NFA.ANY: dfa.set_default_transition(current, new_state) else: dfa.add_transition(current, input, new_state) return dfa class DFA(object): def __init__(self, start_state): self.start_state = start_state self.transitions = {} self.defaults = {} self.final_states = set() def add_transition(self, src, input, dest): self.transitions.setdefault(src, {})[input] = dest def set_default_transition(self, src, dest): self.defaults[src] = dest def add_final_state(self, state): self.final_states.add(state) def is_final(self, state): return state in self.final_states def next_state(self, src, input): state_transitions = self.transitions.get(src, {}) return state_transitions.get(input, self.defaults.get(src, None)) def next_valid_string(self, input): state = self.start_state stack = [] # Evaluate the DFA as far as possible print state for i, x in enumerate(input): print "%s" % input print 'e' stack.append((input[:i], state, x)) state = self.next_state(state, x) if not state: break else: stack.append((input[:i+1], state, None)) if self.is_final(state): # Input word is already valid return input # Perform a 'wall following' search for the lexicographically smallest # accepting state. while stack: path, state, x = stack.pop() x = self.find_next_edge(state, x) #print 'x' if x: path += x state = self.next_state(state, x) if self.is_final(state): print 'p' return path stack.append((path, state, None)) print 'v' return None def find_next_edge(self, s, x): if x is None: x = '\0' # u'\0' else: x = chr(ord(x) + 1) state_transitions = self.transitions.get(s, {}) if x in state_transitions or s in self.defaults: return x labels = sorted(state_transitions.keys()) pos = bisect.bisect_left(labels, x) if pos < len(labels): print 'n' return labels[pos] return None def levenshtein_automata(self, term, k): ##### line 125 ########' nfa = NFA((0, 0)) for i, c in enumerate(term): for e in range(k + 1): # Correct character nfa.add_transition((i, e), c, (i + 1, e)) if e < k: # Deletion nfa.add_transition((i, e), NFA.ANY, (i, e + 1)) # Insertion nfa.add_transition((i, e), NFA.EPSILON, (i + 1, e + 1)) # Substitution nfa.add_transition((i, e), NFA.ANY, (i + 1, e + 1)) for e in range(k + 1): if e < k: nfa.add_transition((len(term), e), NFA.ANY, (len(term), e + 1)) nfa.add_final_state((len(term), e)) return nfa def find_all_matches(self, word, k, lookup_func): lev = levenshtein_automata(word, k).to_dfa() ######### line 145 ########## match = lev.next_valid_string('\0') while match: next = lookup_func(match) if not next: return if match == next: yield match next1 = next1 + '\0' match = lev.next_valid_string(next1) class Matcher(object): def __init__(self, l): self.l = l self.probes = 0 def __call__(self, w): self.probes += 1 pos = bisect.bisect_left(self.l, w) if pos < len(self.l): return self.l[pos] else: return None words = [x.strip().lower() for x in open('F:/shedskin/database.txt')] words.sort() m = Matcher(words) testdfa = DFA(1) length = len(list(testdfa.find_all_matches('food', 1, m))) ######## line 174 ########## I cannot understand why the Python 2.7.1 interpreter complains about NameError: global name 'levenshtein_automata' is not defined. On line 125 , I try to define levenshtein_automata(self,term,k) as a method of the class DFA. I am new to Python 2.7.1. Could someone help me fix this NameError error message. Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From zstumgoren at gmail.com Tue Dec 28 02:38:19 2010 From: zstumgoren at gmail.com (Serdar Tumgoren) Date: Mon, 27 Dec 2010 20:38:19 -0500 Subject: [Tutor] Python 2.7.1 interpreter complains about NameError: global name 'levenshtein_automata' is not defined In-Reply-To: References: Message-ID: It appears that you've defined "levenshtein_automata" as a method on your DFA class, but you did not reference the class instance in your call by prefixing "self". Instead, you're trying to call a globally defined function named "levenshtein_automata" -- which because it is not defined, is throwing a NameError. Try changing line 145 to the below and see if that resolves the issue: lev = self.levenshtein_automata(word, k).to_dfa() ######### line 145 ########## -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Tue Dec 28 02:48:06 2010 From: bgailer at gmail.com (bob gailer) Date: Mon, 27 Dec 2010 20:48:06 -0500 Subject: [Tutor] Python 2.7.1 interpreter complains about NameError: global name 'levenshtein_automata' is not defined In-Reply-To: References: Message-ID: <4D1941D6.7050701@gmail.com> See my comment following line 145: On 12/27/2010 8:25 PM, Frank Chang wrote: > Good morning, I am using Python 2.7.1 on Windows XP Service Pack 3. > Here is the program where the Python interpreter complains about > NameError: global name 'levenshtein_automata' is not defined. > The python 2,7.1 error message is: > Traceback (most recent call last): > File "automata_test.py", line 174, in > length = len(list(testdfa.find_all_matches('food', 1, m))) > File "automata_test.py", line 145, in find_all_matches > lev = levenshtein_automata(word, k).to_dfa() > NameError: global name 'levenshtein_automata' is not defined > Here is the program. I have marked lines 174, 145 and 125. > import bisect > import random > class NFA(object): > EPSILON = object() > ANY = object() > def __init__(self, start_state): > self.transitions = {} > self.final_states = set() > self._start_state = start_state > @property > def start_state(self): > return frozenset(self._expand(set([self._start_state]))) > def add_transition(self, src, input, dest): > self.transitions.setdefault(src, {}).setdefault(input, > set()).add(dest) > def add_final_state(self, state): > self.final_states.add(state) > def is_final(self, states): > return self.final_states.intersection(states) > def _expand(self, states): > frontier = set(states) > while frontier: > state = frontier.pop() > new_states = self.transitions.get(state, {}).get(NFA.EPSILON, > set()).difference(states) > frontier.update(new_states) > states.update(new_states) > return states > def next_state(self, states, input): > dest_states = set() > for state in states: > state_transitions = self.transitions.get(state, {}) > dest_states.update(state_transitions.get(input, [])) > dest_states.update(state_transitions.get(NFA.ANY, [])) > return frozenset(self._expand(dest_states)) > def get_inputs(self, states): > inputs = set() > for state in states: > inputs.update(self.transitions.get(state, {}).keys()) > return inputs > def to_dfa(self): > dfa = DFA(self.start_state) > frontier = [self.start_state] > seen = set() > while frontier: > current = frontier.pop() > inputs = self.get_inputs(current) > for input in inputs: > if input == NFA.EPSILON: continue > new_state = self.next_state(current, input) > if new_state not in seen: > frontier.append(new_state) > seen.add(new_state) > if self.is_final(new_state): > dfa.add_final_state(new_state) > if input == NFA.ANY: > dfa.set_default_transition(current, new_state) > else: > dfa.add_transition(current, input, new_state) > return dfa > > > class DFA(object): > def __init__(self, start_state): > self.start_state = start_state > self.transitions = {} > self.defaults = {} > self.final_states = set() > def add_transition(self, src, input, dest): > self.transitions.setdefault(src, {})[input] = dest > def set_default_transition(self, src, dest): > self.defaults[src] = dest > def add_final_state(self, state): > self.final_states.add(state) > def is_final(self, state): > return state in self.final_states > def next_state(self, src, input): > state_transitions = self.transitions.get(src, {}) > return state_transitions.get(input, self.defaults.get(src, None)) > def next_valid_string(self, input): > state = self.start_state > stack = [] > # Evaluate the DFA as far as possible > print state > for i, x in enumerate(input): > print "%s" % input > print 'e' > stack.append((input[:i], state, x)) > state = self.next_state(state, x) > if not state: break > else: > stack.append((input[:i+1], state, None)) > > if self.is_final(state): > # Input word is already valid > return input > # Perform a 'wall following' search for the > lexicographically smallest > # accepting state. > while stack: > path, state, x = stack.pop() > x = self.find_next_edge(state, x) > #print 'x' > if x: > path += x > state = self.next_state(state, x) > if self.is_final(state): > print 'p' > return path > stack.append((path, state, None)) > print 'v' > return None > def find_next_edge(self, s, x): > if x is None: > x = '\0' # u'\0' > else: > x = chr(ord(x) + 1) > state_transitions = self.transitions.get(s, {}) > if x in state_transitions or s in self.defaults: > return x > labels = sorted(state_transitions.keys()) > pos = bisect.bisect_left(labels, x) > if pos < len(labels): > print 'n' > return labels[pos] > return None > def levenshtein_automata(self, term, k): ##### line 125 ########' > nfa = NFA((0, 0)) > for i, c in enumerate(term): > for e in range(k + 1): > # Correct character > nfa.add_transition((i, e), c, (i + 1, e)) > if e < k: > # Deletion > nfa.add_transition((i, e), NFA.ANY, (i, e + 1)) > # Insertion > nfa.add_transition((i, e), NFA.EPSILON, (i + 1, e + 1)) > # Substitution > nfa.add_transition((i, e), NFA.ANY, (i + 1, e + 1)) > for e in range(k + 1): > if e < k: > nfa.add_transition((len(term), e), NFA.ANY, (len(term), e + 1)) > nfa.add_final_state((len(term), e)) > return nfa > def find_all_matches(self, word, k, lookup_func): > lev = levenshtein_automata(word, k).to_dfa() ######### line 145 > ########## Try: lev = self.levenshtein_automata(word, k).to_dfa() > match = lev.next_valid_string('\0') > while match: > next = lookup_func(match) > if not next: > return > if match == next: > yield match > next1 = next1 + '\0' > match = lev.next_valid_string(next1) > > class Matcher(object): > def __init__(self, l): > self.l = l > self.probes = 0 > def __call__(self, w): > self.probes += 1 > pos = bisect.bisect_left(self.l, w) > if pos < len(self.l): > return self.l[pos] > else: > return None > words = [x.strip().lower() for x in open('F:/shedskin/database.txt')] > words.sort() > m = Matcher(words) > testdfa = DFA(1) > length = len(list(testdfa.find_all_matches('food', 1, m))) ######## > line 174 ########## > I cannot understand why the Python 2.7.1 interpreter complains > about NameError: global name 'levenshtein_automata' is not defined. On > line 125 , I try to define levenshtein_automata(self,term,k) as a > method of the class DFA. I am new to Python 2.7.1. Could someone help > me fix this NameError error message. Thank you. -- Bob Gailer 919-636-4239 Chapel Hill NC From alan.gauld at btinternet.com Tue Dec 28 08:42:42 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Dec 2010 07:42:42 -0000 Subject: [Tutor] Python 2.7.1 interpreter complains about NameError: global name 'levenshtein_automata' is not defined References: Message-ID: "Frank Chang" wrote > Good morning, I am using Python 2.7.1 on Windows XP Service Pack 3. > Here > is the program where the Python interpreter complains about > NameError: > global name 'levenshtein_automata' is not defined. The others have answered the specific question, however I think you have another problem lurking: > def find_all_matches(self, word, k, lookup_func): > lev = levenshtein_automata(word, k).to_dfa() > match = lev.next_valid_string('\0') > while match: > next = lookup_func(match) > if not next: > return Here you return a None value > if match == next: > yield match > next1 = next1 + '\0' > match = lev.next_valid_string(next1) > > length = len(list(testdfa.find_all_matches('food', 1, m))) ######## > line 174 And here you try to convert the return value to a list. But list(None) will fail with a TypeError. You should probably returm an empty string or raise an exception and catch it in your main block. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From timomlists at gmail.com Tue Dec 28 11:20:06 2010 From: timomlists at gmail.com (Timo) Date: Tue, 28 Dec 2010 11:20:06 +0100 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: References: Message-ID: <4D19B9D6.1010803@gmail.com> On 26-12-10 19:31, Noah Hall wrote: > > >Which is suggestible just in case the app or program crashes > during use. > > (To O.P) > Indeed, though in such cases you must rely on your programmers > instinct to make the right decision - what applies for certain > instances of an application doesn't always conform with what applies > for other instances. For example, committing every event is hardly > ideal for something such as a media player, but may be correct for > something handling vital data to the system. The user gets some dialogs where he can click a save-button, so that looks like a perfect place to commit changes. But to be sure, it is perfectly safe and valid to open the database on program startup, commit changes during the process and close it on exit (or unhandled exception)? Cheers, Timo > There's no "one fits all feet" rule for such things - as you program > and develop more and more applications and programs, listen to your > users, and your bug tests. It'll come almost naturally after a while. From enalicho at gmail.com Tue Dec 28 13:23:46 2010 From: enalicho at gmail.com (Noah Hall) Date: Tue, 28 Dec 2010 12:23:46 +0000 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: <4D19B9D6.1010803@gmail.com> References: <4D19B9D6.1010803@gmail.com> Message-ID: > > > But to be sure, it is perfectly safe and valid to open the database on > program startup, commit changes during the process and close it > on exit (or unhandled exception)? As long as it makes sense to do so, yes. There's no point having an open connection to a database if there doesn't need to be. -------------- next part -------------- An HTML attachment was scrubbed... URL: From abdulhakim.haliru at leproghrammeen.com Tue Dec 28 13:38:36 2010 From: abdulhakim.haliru at leproghrammeen.com (Abdulhakim Haliru) Date: Tue, 28 Dec 2010 13:38:36 +0100 Subject: [Tutor] Choice of Python Message-ID: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Hi Guys, I am pretty new to python as this is just my 5th day reading through God knows how many books. I am really not new to programming, I come from a Cakephp, zend framework angle cutting through ASP.net,VB and C# at an intermediate level. However, for some reason, I am thinking python but reading through lots of the input on forums like quora , keeps me confuse as to whether to continue, Unlearn my php ways (as that must certainly happen J) and learn python and web development woth python, Jquery etal ? I just need a quick opinion, I know geeks would always favour technologies they are used to in forums but then what do you guys advise ? Is python really worth the pain or should I just skip it ? Thank you so much. Abdulhakim Haliru -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Tue Dec 28 14:36:54 2010 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 28 Dec 2010 08:36:54 -0500 Subject: [Tutor] Choice of Python In-Reply-To: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: Python, to me, is much more self consistent. It is a language that was designed with a plan. PHP to me feels like its more cobbled together. Python is easier to read, you get much more done in less code. The build in data structures are intrinsically connected to coding patterns. This may seem silly, but its not to me. Barrier to entry in PHP is fairly low. You need to be interested in math, cs, agorithms to enjoy and exploit python. This to me is a more interesting group of developers. I've been writing software since the late 70s. the dawn of the pc era. Back then, there were very few people who did it. Some CS majors, some EE types (me) some physicists, some poets even. Since then, because of demand, and hype there are all sorts of people with widely varying interests in programming, and I find the people I meet who use python are more similar to those I knew earlier my career. On Tue, Dec 28, 2010 at 7:38 AM, Abdulhakim Haliru < abdulhakim.haliru at leproghrammeen.com> wrote: > Hi Guys, > > > > I am pretty new to python as this is just my 5th day reading through God > knows how many books. I am really not new to programming, > > I come from a Cakephp, zend framework angle cutting through ASP.net,VB and > C# at an intermediate level. > > > > However, for some reason, I am thinking python but reading through lots of > the input on forums like quora , keeps me confuse as to whether to > continue, > > Unlearn my php ways (as that must certainly happen J) and learn python > and web development woth python, Jquery etal ? > > > > I just need a quick opinion, I know geeks would always favour technologies > they are used to in forums but then what do you guys advise ? > > > > Is python really worth the pain or should I just skip it ? > > > > Thank you so much. > > > > > > *Abdulhakim Haliru* > > * * > > > > _______________________________________________ > 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 stefan_ml at behnel.de Tue Dec 28 14:41:10 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Tue, 28 Dec 2010 14:41:10 +0100 Subject: [Tutor] Choice of Python In-Reply-To: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: Abdulhakim Haliru, 28.12.2010 13:38: > I come from a Cakephp, zend framework angle cutting through ASP.net,VB and > C# at an intermediate level. >[...] > Is python really worth the pain or should I just skip it ? Given that you already invested your time into learning all of the above (which basically cover about 1 1/2 of several main corners of programming), I think you should really take some time off to unlearn some of the bad habits that these particular languages tend to teach you. Python is a truly good way to do that. My advice: don't spend too much time reading books. Pick a task that sounds like fun to implement and give it a try with Python. Some would propose exercises from project Euler for this or maybe pygame, but you'll likely have your own idea about what's fun and what isn't. Stefan From orasnita at gmail.com Tue Dec 28 14:57:13 2010 From: orasnita at gmail.com (Octavian Rasnita) Date: Tue, 28 Dec 2010 15:57:13 +0200 Subject: [Tutor] Choice of Python References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: From: "Abdulhakim Haliru" > Hi Guys, > > I am pretty new to python as this is just my 5th day reading through God > knows how many books. I am really not new to programming, > > I come from a Cakephp, zend framework angle cutting through ASP.net,VB and > C# at an intermediate level. > > However, for some reason, I am thinking python but reading through lots of > the input on forums like quora , keeps me confuse as to whether to > continue, > > Unlearn my php ways (as that must certainly happen J) and learn python and > web development woth python, Jquery etal ? > > I just need a quick opinion, I know geeks would always favour technologies > they are used to in forums but then what do you guys advise ? > > Is python really worth the pain or should I just skip it ? > > Thank you so much. > > Abdulhakim Haliru It depends on what you want to do. If you want to create just simple low level apps that use mainly the functions/methods provided by the language distribution without installing other libraries, PHP is the best. But you said something about CakePHP so you might be interested in more complex apps that use a framework, maybe an ORM and other things... In this case, Python is much better than PHP. Its syntax is much different than the syntax of other languages because it is based on indentation like in the old days of Cobol, but the language is much more sane than PHP. Python is a more general language than PHP and it is not specialized for the web so you can do much more things with it than just create web apps. For creating web apps Perl and Ruby are better than Python, but Python offers a much better support for Windows apps and for desktop apps in general than Perl and Ruby. Of course, the comparison is not made only among the features provided by the core language, but it takes into account all the modules, libraries, frameworks, ORMS, form processors, templating systems that can be used. The advantages also depend on your preferences. If you like to have a framework that forces you to use just a single ORM or a single templating system, you might prefer something, and if you like that framework to allow you to use any templating system, any ORM, any form processor you can choose... you may like another framework. For example, I have tested more PHP frameworks and I didn't like their very limited URL dispatching possibilities. Some of them even require to define a separate file with URL maps which is ugly and hard to maintain, there is no a very powerful ORM for PHP yet, some of them use their own templating system which is very limited and other things like these. So yes, if you need to use higher level code, there are better possibilities than those offered by PHP, even they are harder to learn. Octavian From emile at fenx.com Tue Dec 28 16:25:49 2010 From: emile at fenx.com (Emile van Sebille) Date: Tue, 28 Dec 2010 07:25:49 -0800 Subject: [Tutor] Choice of Python In-Reply-To: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: On 12/28/2010 4:38 AM Abdulhakim Haliru said... > Is python really worth the pain or should I just skip it ? I think exactly the same thing about PHP each time I run into a PHP app that I need to tweak. Mostly I just skip it... For me, there wasn't any pain in learning python. Of course, I started 12 or so years ago after a very painful two weeks with Java. I had the python version running two days later. Regards, Emile From swiftone at swiftone.org Tue Dec 28 17:05:30 2010 From: swiftone at swiftone.org (Brett Ritter) Date: Tue, 28 Dec 2010 11:05:30 -0500 Subject: [Tutor] Choice of Python In-Reply-To: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: On Tue, Dec 28, 2010 at 7:38 AM, Abdulhakim Haliru wrote: > Unlearn my php ways (as that must certainly happen J)? and learn python and > web development woth python, Jquery etal ? ... > Is python really worth the pain or should I just skip it ? If you accept that you'll continue to learn new syntaxes throughout your career, then the "pain" of python isn't notably different than any other language (though I have to constantly reteach myself not to use semicolons :) ). You aren't "unlearning" so much as "meta-learning" That covers the "pain" half of your equation. As for the Python half --- Python is pretty widely used in a variety of ways. It's probably the most common plugin language for outside utilities (not that anything is particularly standard in that landscape). I've noticed that new language development often compares against Python. Scientific fields work more and more with Python. Python won't be your last stop, but it's a valuable one. As to HOW valuable, that depends on a bunch of life details we don't know. If you want one language to focus on and use exclusively for several years, Python may or may not be the best choice depending on your field. If you want a useful tool that will improve your understanding of other tools in addition to being useful in its own right, Python is absolutely a good choice. All that said, I doubt you'll find many Python Nay-sayers on the Python Tutors mailing list :) -- Brett Ritter / SwiftOne swiftone at swiftone.org From waynejwerner at gmail.com Tue Dec 28 17:44:57 2010 From: waynejwerner at gmail.com (Wayne Werner) Date: Tue, 28 Dec 2010 10:44:57 -0600 Subject: [Tutor] Choice of Python In-Reply-To: References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: On Tue, Dec 28, 2010 at 10:05 AM, Brett Ritter wrote: > (though I have to constantly reteach myself not to > use semicolons :) ). Technically speaking, you *can* use semicolons in Python: if 3 == int('3'): print('Cool'); works the same sans semicolon. -Wayne -------------- next part -------------- An HTML attachment was scrubbed... URL: From shantanoo at gmail.com Tue Dec 28 18:12:46 2010 From: shantanoo at gmail.com (=?utf-8?B?4KS24KSC4KSk4KSo4KWC?=) Date: Tue, 28 Dec 2010 22:42:46 +0530 Subject: [Tutor] Choice of Python In-Reply-To: References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: <55A89930-2743-4E8E-B233-4858B8D75943@gmail.com> On 28-Dec-2010, at 10:14 PM, Wayne Werner wrote: > On Tue, Dec 28, 2010 at 10:05 AM, Brett Ritter wrote: > (though I have to constantly reteach myself not to > use semicolons :) ). > > Technically speaking, you *can* use semicolons in Python: > > if 3 == int('3'): > print('Cool'); > > works the same sans semicolon. And so does following... >>> if 3 == int('3'): ... print 'hello';print 'world' ... hello world From knacktus at googlemail.com Tue Dec 28 18:28:17 2010 From: knacktus at googlemail.com (Knacktus) Date: Tue, 28 Dec 2010 18:28:17 +0100 Subject: [Tutor] Choice of Python In-Reply-To: References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: <4D1A1E31.30903@googlemail.com> Am 28.12.2010 14:41, schrieb Stefan Behnel: > Abdulhakim Haliru, 28.12.2010 13:38: >> I come from a Cakephp, zend framework angle cutting through ASP.net,VB >> and >> C# at an intermediate level. >> [...] >> Is python really worth the pain or should I just skip it ? > > Given that you already invested your time into learning all of the above > (which basically cover about 1 1/2 of several main corners of > programming), I think you should really take some time off to unlearn > some of the bad habits that these particular languages tend to teach > you. Python is a truly good way to do that. > > My advice: don't spend too much time reading books. Pick a task that > sounds like fun to implement and give it a try with Python. Some would > propose exercises from project Euler for this or maybe pygame, but > you'll likely have your own idea about what's fun and what isn't. +1 for jumping into coding. You seem to have enough experience in programming generally. With Python the fun comes with the experienced productivity. At least, that was the case with me. Learning the syntax is not the deal, but how to design your app. You can use the best of OO, functional and procedural programming. Also, Python is to me the best general purpose language. You can create little helper scripts, web apps and rich client apps with PyQt or WxPython. > > Stefan > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From marc.tompkins at gmail.com Tue Dec 28 18:46:36 2010 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 28 Dec 2010 09:46:36 -0800 Subject: [Tutor] Choice of Python In-Reply-To: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: I love, love, love me some Python - it fits the way I think better than any other language I've used - but there is one consideration that occurs to me: Python is nearly ubiquitous on Linux/Mac, and easy to download and install on Windows - but most bargain-basement Web hosts don't support it (I'm looking at YOU, GoDaddy.) If you're using a premium hosting company ("premium" doesn't necessarily mean "extremely expensive", but you do need to compare hosting plans), or if you plan on hosting your site yourself, then I would absolutely recommend Python (with or without Django or what-have-you) for Web development... but if you plan on using GoDaddy, stick with PHP. -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Tue Dec 28 18:56:58 2010 From: emile at fenx.com (Emile van Sebille) Date: Tue, 28 Dec 2010 09:56:58 -0800 Subject: [Tutor] Choice of Python In-Reply-To: References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: On 12/28/2010 9:46 AM Marc Tompkins said... > I love, love, love me some Python - it fits the way I think better than any > other language I've used - but there is one consideration that occurs to me: > Python is nearly ubiquitous on Linux/Mac, and easy to download and install > on Windows - but most bargain-basement Web hosts don't support it (I'm > looking at YOU, GoDaddy.) > > If you're using a premium hosting company ("premium" doesn't necessarily > mean "extremely expensive", but you do need to compare hosting plans), or if > you plan on hosting your site yourself, then I would absolutely recommend > Python (with or without Django or what-have-you) for Web development... but > if you plan on using GoDaddy, stick with PHP. > http://help.godaddy.com/article/809 would seem to indicate differently... Emile From marc.tompkins at gmail.com Tue Dec 28 18:57:43 2010 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 28 Dec 2010 09:57:43 -0800 Subject: [Tutor] Choice of Python In-Reply-To: References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: On Tue, Dec 28, 2010 at 9:46 AM, Marc Tompkins wrote: > I love, love, love me some Python - it fits the way I think better than any > other language I've used - but there is one consideration that occurs to me: > Python is nearly ubiquitous on Linux/Mac, and easy to download and install > on Windows - but most bargain-basement Web hosts don't support it (I'm > looking at YOU, GoDaddy.) > > If you're using a premium hosting company ("premium" doesn't necessarily > mean "extremely expensive", but you do need to compare hosting plans), or if > you plan on hosting your site yourself, then I would absolutely recommend > Python (with or without Django or what-have-you) for Web development... but > if you plan on using GoDaddy, stick with PHP. > I sent that too fast: I should clarify. From your mention of CakePHP, Zend, and ASP.Net I assume you're mainly interested in Web development. For desktop development, embedded scripting, or almost any situation where you can install your own software - I recommend Python wholeheartedly. I jumped into it a few years ago when I had a legacy flat-file database I needed to write reports against, and no native tools to do it with. I had my first report written within four or five hours of downloading Python, and I've been hooked ever since. -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Tue Dec 28 19:01:46 2010 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 28 Dec 2010 10:01:46 -0800 Subject: [Tutor] Choice of Python In-Reply-To: References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: On Tue, Dec 28, 2010 at 9:56 AM, Emile van Sebille wrote: > On 12/28/2010 9:46 AM Marc Tompkins said... > > I love, love, love me some Python - it fits the way I think better than >> any >> other language I've used - but there is one consideration that occurs to >> me: >> Python is nearly ubiquitous on Linux/Mac, and easy to download and install >> on Windows - but most bargain-basement Web hosts don't support it (I'm >> looking at YOU, GoDaddy.) >> >> If you're using a premium hosting company ("premium" doesn't necessarily >> mean "extremely expensive", but you do need to compare hosting plans), or >> if >> you plan on hosting your site yourself, then I would absolutely recommend >> Python (with or without Django or what-have-you) for Web development... >> but >> if you plan on using GoDaddy, stick with PHP. >> >> > http://help.godaddy.com/article/809 would seem to indicate differently... > > Emile > > Hmmmm! That's new since I last looked into it. Color me intrigued... Has anybody tried GoDaddy's Python support? -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From python at bdurham.com Tue Dec 28 19:32:43 2010 From: python at bdurham.com (python at bdurham.com) Date: Tue, 28 Dec 2010 13:32:43 -0500 Subject: [Tutor] Choice of Python In-Reply-To: References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: <1293561163.11962.1412548187@webmail.messagingengine.com> Marc/Emile, If you're looking for a good hosting service that supports Python, I strongly recommend webfaction.com. I've worked with a lot of hosting companies and webfaction gets my highest endorsement: Great support, helpful user community, very flexible support for hosting Python applications from vanilla CGI to WSGI to Python web frameworks with long running processes such as Django, CherryPy, web2py, etc, and the latest versions of Python. Most of the hosting companies I've investigated support older versions of Python and only support CGI access. Malcolm -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeff at dcsoftware.com Tue Dec 28 19:52:21 2010 From: jeff at dcsoftware.com (Jeff Johnson) Date: Tue, 28 Dec 2010 11:52:21 -0700 Subject: [Tutor] Choice of Python In-Reply-To: References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> Message-ID: <4D1A31E5.3070501@dcsoftware.com> On 12/28/2010 10:46 AM, Marc Tompkins wrote: > I love, love, love me some Python - it fits the way I think better > than any other language I've used - but there is one consideration > that occurs to me: Python is nearly ubiquitous on Linux/Mac, and easy > to download and install on Windows - but most bargain-basement Web > hosts don't support it (I'm looking at YOU, GoDaddy.) > > If you're using a premium hosting company ("premium" doesn't > necessarily mean "extremely expensive", but you do need to compare > hosting plans), or if you plan on hosting your site yourself, then I > would absolutely recommend Python (with or without Django or > what-have-you) for Web development... but if you plan on using > GoDaddy, stick with PHP. > > I have been a software developer since the 70's. I have used most of the major languages. I used FoxPro for the last 20 years and have recently moved to Python. I absolutely love working with Python! Everything works, deployment is easy, and with all of the libraries available; there isn't much you can't do. I now develop on Ubuntu even though my customers (and deployment) are Windows. Check out Webfaction for a hosting company. They are probably the largest Django host, but using their control panel to do things is very easy! I have been using them for over two years. Jeff ------------------- Jeff Johnson jeff at dcsoftware.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From egilead at gmail.com Tue Dec 28 14:54:57 2010 From: egilead at gmail.com (Enih Gilead) Date: Tue, 28 Dec 2010 11:54:57 -0200 Subject: [Tutor] blank space after a number Message-ID: <4D19EC31.9040705@gmail.com> Hi, Abrahamsen! Would you mind tell me a way to eliminate the blank space in front of "a" [int number] ? Just as an example, the 'a' that is made = to '0', when printed, it comes with a blank space after... a, b = 0, 1 while b < 10: print a, b, a, b = a, b + 1 ... do you think is there any reasonable way to transform the '0' number into string and then back to numeral - after the printing action? Thanks beforehand ve'Shalom be'Shem 'Adonay, Enih Gil'ead From hugo.yoshi at gmail.com Tue Dec 28 20:31:28 2010 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Tue, 28 Dec 2010 20:31:28 +0100 Subject: [Tutor] blank space after a number In-Reply-To: <4D19EC31.9040705@gmail.com> References: <4D19EC31.9040705@gmail.com> Message-ID: On Tue, Dec 28, 2010 at 2:54 PM, Enih Gilead wrote: > Hi, Abrahamsen! > > Would you mind tell me a way to eliminate the blank space in front of "a" > [int number] ? > Just as an example, the 'a' that is made = to '0', when printed, it comes > with a blank space after... > > a, b = 0, 1 > while b < 10: > ? ?print a, b, > ? ?a, b = a, b + 1 > > ... do you think is there any reasonable way to transform the '0' number > into string and then back to numeral - after the printing action? > If you put commas between things in a print statement, python will put a space in between. Either use string formatting or convert to str() and add together: print "{0}{1}".format(a, b), print str(a) + str(b), Hugo From bgailer at gmail.com Tue Dec 28 20:34:47 2010 From: bgailer at gmail.com (bob gailer) Date: Tue, 28 Dec 2010 14:34:47 -0500 Subject: [Tutor] blank space after a number In-Reply-To: <4D19EC31.9040705@gmail.com> References: <4D19EC31.9040705@gmail.com> Message-ID: <4D1A3BD7.5080307@gmail.com> On 12/28/2010 8:54 AM, Enih Gilead wrote: > Hi, Abrahamsen! > > Would you mind tell me a way to eliminate the blank space in front of > "a" [int number] ? > Just as an example, the 'a' that is made = to '0', when printed, it > comes with a blank space after... > > a, b = 0, 1 > while b < 10: > print a, b, > a, b = a, b + 1 > > ... do you think is there any reasonable way to transform the '0' > number into string and then back to numeral - after the printing action? Use formatting: print '%i%i' % (a,b) -- Bob Gailer 919-636-4239 Chapel Hill NC From alan.gauld at btinternet.com Tue Dec 28 20:36:48 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Dec 2010 19:36:48 -0000 Subject: [Tutor] blank space after a number References: <4D19EC31.9040705@gmail.com> Message-ID: "Enih Gilead" wrote > Just as an example, the 'a' that is made = to '0', when printed, it > comes with a blank space after... > > a, b = 0, 1 > while b < 10: > print a, b, > a, b = a, b + 1 > > ... do you think is there any reasonable way to transform the '0' > number into string and then back to numeral - after the printing > action? You can use string formatting to create the string any way tou want: print "%d%d" % a,b will print 01 Or you can use string conversion and catenastion: print str(a)+str(b) to get a similar result. Incidentally your loop would be clearer if you omited the tuple assignment: while b < 10: print a, b, b += 1 And if you only use 'a' to zero pad the number you can do that with string formatting too: print "%02d" % b Personally I'd go for string formatting because of its much more powerful and flexible options. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From marc.tompkins at gmail.com Tue Dec 28 20:48:07 2010 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 28 Dec 2010 11:48:07 -0800 Subject: [Tutor] Choice of Python In-Reply-To: <1293561163.11962.1412548187@webmail.messagingengine.com> References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> <1293561163.11962.1412548187@webmail.messagingengine.com> Message-ID: On Tue, Dec 28, 2010 at 10:32 AM, wrote: > > Most of the hosting companies I've investigated support older versions of > Python and only support CGI access. > > Ah yes - that's what it was. To use Django (or most other frameworks) you need some processes to be running more or less constantly, as opposed to in a CGI context. Your typical shared Webhosting service is sharing a single machine or VM with lots of other customers; they can't allow long-running processes or the whole thing would grind to a halt. I have no idea how Webfaction manages it, especially with a starting price of $5.50/month - it's very tempting... For the time being I'm not looking to move any sites over - but if the need arises again, Webfaction will be the first place I check out. Actually, my own website is a few years overdue for a facelift - maybe I'll dump Joomla for Django. Perhaps then I'd actually be interested enough to maintain the damn thing. -- www.fsrtechnologies.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeff at dcsoftware.com Tue Dec 28 21:06:54 2010 From: jeff at dcsoftware.com (Jeff Johnson) Date: Tue, 28 Dec 2010 13:06:54 -0700 Subject: [Tutor] Choice of Python In-Reply-To: References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> <1293561163.11962.1412548187@webmail.messagingengine.com> Message-ID: <4D1A435E.20308@dcsoftware.com> On 12/28/2010 12:48 PM, Marc Tompkins wrote: > On Tue, Dec 28, 2010 at 10:32 AM, > wrote: > > Most of the hosting companies I've investigated support older > versions of Python and only support CGI access. > > Ah yes - that's what it was. To use Django (or most other frameworks) > you need some processes to be running more or less constantly, as > opposed to in a CGI context. Your typical shared Webhosting service > is sharing a single machine or VM with lots of other customers; they > can't allow long-running processes or the whole thing would grind to a > halt. I have no idea how Webfaction manages it, especially with a > starting price of $5.50/month - it's very tempting... > > For the time being I'm not looking to move any sites over - but if the > need arises again, Webfaction will be the first place I check out. > > Actually, my own website is a few years overdue for a facelift - maybe > I'll dump Joomla for Django. Perhaps then I'd actually be interested > enough to maintain the damn thing. > > -- > www.fsrtechnologies.com > > Webfaction supports long processes and that is why they are the largest Django hosting site. They support a ton of software, too. SVN, Trac are two I use. I've been with them at least 3 years and I find their cost amazing for what I get! Their documentation, support and forums are about the best I've seen. Jeff ------------------- Jeff Johnson jeff at dcsoftware.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From swiftone at swiftone.org Tue Dec 28 21:35:18 2010 From: swiftone at swiftone.org (Brett Ritter) Date: Tue, 28 Dec 2010 15:35:18 -0500 Subject: [Tutor] Choice of Python In-Reply-To: <4D1A435E.20308@dcsoftware.com> References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> <1293561163.11962.1412548187@webmail.messagingengine.com> <4D1A435E.20308@dcsoftware.com> Message-ID: On Tue, Dec 28, 2010 at 3:06 PM, Jeff Johnson wrote: > Webfaction supports long processes and that is why they are the largest > Django hosting site.? They support a ton of software, too.? SVN, Trac are > two I use. I didn't see git hosting among their software. Is it available without hoop-jumping? -- Brett Ritter / SwiftOne swiftone at swiftone.org From jeff at dcsoftware.com Tue Dec 28 21:56:28 2010 From: jeff at dcsoftware.com (Jeff Johnson) Date: Tue, 28 Dec 2010 13:56:28 -0700 Subject: [Tutor] Choice of Python In-Reply-To: References: <000301cba68c$2bdac160$83904420$@leproghrammeen.com> <1293561163.11962.1412548187@webmail.messagingengine.com> <4D1A435E.20308@dcsoftware.com> Message-ID: <4D1A4EFC.5000407@dcsoftware.com> On 12/28/2010 01:35 PM, Brett Ritter wrote: > On Tue, Dec 28, 2010 at 3:06 PM, Jeff Johnson wrote: >> Webfaction supports long processes and that is why they are the largest >> Django hosting site. They support a ton of software, too. SVN, Trac are >> two I use. > I didn't see git hosting among their software. Is it available > without hoop-jumping? I counted 20 what they call "applications" of which git is one. So, yes it is there. Jeff ------------------- Jeff Johnson jeff at dcsoftware.com From bgailer at gmail.com Tue Dec 28 22:32:36 2010 From: bgailer at gmail.com (bob gailer) Date: Tue, 28 Dec 2010 16:32:36 -0500 Subject: [Tutor] blank space after a number In-Reply-To: References: <4D19EC31.9040705@gmail.com> Message-ID: <4D1A5774.7030406@gmail.com> On 12/28/2010 2:36 PM, Alan Gauld wrote: > > "Enih Gilead" wrote > >> Just as an example, the 'a' that is made = to '0', when printed, it >> comes with a blank space after... >> >> a, b = 0, 1 >> while b < 10: >> print a, b, >> a, b = a, b + 1 >> >> ... do you think is there any reasonable way to transform the '0' >> number into string and then back to numeral - after the printing action? > > You can use string formatting to create the string any way tou want: > > print "%d%d" % a,b Erm... print "%d%d" % (a,b) > > will print 01 > > Or you can use string conversion and catenastion: > > print str(a)+str(b) > > to get a similar result. > > Incidentally your loop would be clearer if you omited the tuple > assignment: > > while b < 10: > print a, b, > b += 1 > > And if you only use 'a' to zero pad the number you can do that with > string formatting too: > > print "%02d" % b > > > Personally I'd go for string formatting because of its much more > powerful and flexible options. > > -- Bob Gailer 919-636-4239 Chapel Hill NC From frankchang91 at gmail.com Tue Dec 28 22:35:18 2010 From: frankchang91 at gmail.com (Frank Chang) Date: Tue, 28 Dec 2010 13:35:18 -0800 Subject: [Tutor] Python 2.7.1 interpreter passing function pointer as function argument and Shedskin 0.7 Message-ID: Good afternoon. I want to thank everyone who helped me fix the global name 'levinshtein_automata' is not defined error. When I run the Shedskin 0.7 Python to C+++ compiler on the same python program, I receive the error message * Error * automata_test.py:148 : unbound identifier 'lookup_func'. lookup_func is a python function pointer passed as an argument to a python function. I have marked lines 148,161,172 and 174 in the python program automata_test.py shown below. The shedskin tutorial says the latest version of the Shedskin 0.7 Python to C++ compiler does not support overloading __iter__ and __call__. I was wondering if anyone could suggest a python workaround to this error message as I am new to python. Thank you. # automata_test.py import bisect import random class NFA(object): EPSILON = object() ANY = object() def __init__(self, start_state): self.transitions = {} self.final_states = set() self._start_state = start_state @property def start_state(self): return frozenset(self._expand(set([self._start_state]))) def add_transition(self, src, input, dest): self.transitions.setdefault(src, {}).setdefault(input, set()).add(dest) def add_final_state(self, state): self.final_states.add(state) def is_final(self, states): return self.final_states.intersection(states) def _expand(self, states): frontier = set(states) while frontier: state = frontier.pop() new_states = self.transitions.get(state, {}).get(NFA.EPSILON, set()).difference(states) frontier.update(new_states) states.update(new_states) return states def next_state(self, states, input): dest_states = set() for state in states: state_transitions = self.transitions.get(state, {}) dest_states.update(state_transitions.get(input, [])) dest_states.update(state_transitions.get(NFA.ANY, [])) return frozenset(self._expand(dest_states)) def get_inputs(self, states): inputs = set() for state in states: inputs.update(self.transitions.get(state, {}).keys()) return inputs def to_dfa(self): dfa = DFA(self.start_state) frontier = [self.start_state] seen = set() while frontier: current = frontier.pop() inputs = self.get_inputs(current) for input in inputs: if input == NFA.EPSILON: continue new_state = self.next_state(current, input) if new_state not in seen: frontier.append(new_state) seen.add(new_state) if self.is_final(new_state): dfa.add_final_state(new_state) if input == NFA.ANY: dfa.set_default_transition(current, new_state) else: dfa.add_transition(current, input, new_state) return dfa class DFA(object): def __init__(self, start_state): self.start_state = start_state self.transitions = {} self.defaults = {} self.final_states = set() def add_transition(self, src, input, dest): self.transitions.setdefault(src, {})[input] = dest def set_default_transition(self, src, dest): self.defaults[src] = dest def add_final_state(self, state): self.final_states.add(state) def is_final(self, state): return state in self.final_states def next_state(self, src, input): state_transitions = self.transitions.get(src, {}) return state_transitions.get(input, self.defaults.get(src, None)) def next_valid_string(self, input): state = self.start_state stack = [] # Evaluate the DFA as far as possible print state for i, x in enumerate(input): print "%s" % input print 'e' stack.append((input[:i], state, x)) state = self.next_state(state, x) if not state: break else: stack.append((input[:i+1], state, None)) if self.is_final(state): # Input word is already valid return input # Perform a 'wall following' search for the lexicographically smallest # accepting state. while stack: path, state, x = stack.pop() x = self.find_next_edge(state, x) #print 'x' if x: path += x state = self.next_state(state, x) if self.is_final(state): print 'p' return path stack.append((path, state, None)) print 'v' return None def find_next_edge(self, s, x): if x is None: x = '\0' else: x = chr(ord(x) + 1) state_transitions = self.transitions.get(s, {}) if x in state_transitions or s in self.defaults: return x labels = sorted(state_transitions.keys()) pos = bisect.bisect_left(labels, x) if pos < len(labels): print 'n' return labels[pos] return None def levenshtein_automata(self, term, k): print 's' nfa = NFA((0, 0)) for i, c in enumerate(term): for e in range(k + 1): # Correct character nfa.add_transition((i, e), c, (i + 1, e)) if e < k: # Deletion nfa.add_transition((i, e), NFA.ANY, (i, e + 1)) # Insertion nfa.add_transition((i, e), NFA.EPSILON, (i + 1, e + 1)) # Substitution nfa.add_transition((i, e), NFA.ANY, (i + 1, e + 1)) for e in range(k + 1): if e < k: nfa.add_transition((len(term), e), NFA.ANY, (len(term), e + 1)) nfa.add_final_state((len(term), e)) return nfa def find_all_matches(self, word, k, lookup_func): lev = self.levenshtein_automata(word, k).to_dfa() match = lev.next_valid_string('\0') while match: next = lookup_func(match) ***** line 148 ***** if not next: return if match == next: yield match next = next + '\0' match = lev.next_valid_string(next) class Matcher(object): def __init__(self, l): self.l = l self.probes = 0 def __call__(self, w): **** line 161 ****** self.probes += 1 pos = bisect.bisect_left(self.l, w) if pos < len(self.l): return self.l[pos] else: return None words = [x.strip().lower() for x in open('F:/shedskin/database.txt')] words.sort() m = Matcher(words) **** line 172 ***** testdfa = DFA(1) length = len(list(testdfa.find_all_matches('food', 1, m))) **** line 174 *** -------------- next part -------------- An HTML attachment was scrubbed... URL: From egilead at gmail.com Tue Dec 28 22:10:01 2010 From: egilead at gmail.com (Enih Gilead) Date: Tue, 28 Dec 2010 19:10:01 -0200 Subject: [Tutor] blank space after a number In-Reply-To: References: <4D19EC31.9040705@gmail.com> Message-ID: <4D1A5229.1040301@gmail.com> Thanks a lot, Hugo Yoshi! As a beginner in Python, I do my best to satisfy my curiosity about some features in the language; and, this one was the most difficult among my "projects"! :c) Best regards, Enih Gil'ead # Formatting numbers from '01,' to '100' # Many thanks to Bob Gailer and to Hugo Yoshi a, b = 0, 1 while b < 10: print '%i%i' % (a,b) + ',', b = b+1 # what results in: ''' 01, 02, 03, 04, 05, 06, 07, 08, 09, ''' while b < 100: print '%i' % (b) + ',', b = b+1 # what results in: ''' 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 ''' print 100 On 12/28/2010 05:31 PM, Hugo Arts wrote: > On Tue, Dec 28, 2010 at 2:54 PM, Enih Gilead wrote: >> Hi, Abrahamsen! >> >> Would you mind tell me a way to eliminate the blank space in front of "a" >> [int number] ? >> Just as an example, the 'a' that is made = to '0', when printed, it comes >> with a blank space after... >> >> a, b = 0, 1 >> while b< 10: >> print a, b, >> a, b = a, b + 1 >> >> ... do you think is there any reasonable way to transform the '0' number >> into string and then back to numeral - after the printing action? >> > If you put commas between things in a print statement, python will put > a space in between. Either use string formatting or convert to str() > and add together: > > print "{0}{1}".format(a, b), > print str(a) + str(b), > > Hugo -- Shalom be'Shem 'Adonay, Enih Gil'ead From enalicho at gmail.com Wed Dec 29 01:09:43 2010 From: enalicho at gmail.com (Noah Hall) Date: Wed, 29 Dec 2010 00:09:43 +0000 Subject: [Tutor] blank space after a number In-Reply-To: <4D1A5229.1040301@gmail.com> References: <4D19EC31.9040705@gmail.com> <4D1A5229.1040301@gmail.com> Message-ID: I'll just add there's a better way to do both of the examples you've done there - > > a, b = 0, 1 > > while b < 10: > > print '%i%i' % (a,b) + ',', > > b = b+1 An easier way of doing this is to instead do the following, including the a (although not needed, as simply using 0 would work) - a, b = 0, 1 while b <10: print '%i%i,' % (a,b), b += 1 > while b < 100: > > print '%i' % (b) + ',', > > b = b+1 > while b < 100: print '%i,' % (b), b += 1 Or, using generators and a *for *loop (which is more suited to the task than a *while *loop): def stringify(x): if x < 10: return '0' + str(x) else: return str(x) print ','.join(stringify(x) for x in xrange(1,101)) -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Wed Dec 29 02:09:27 2010 From: bgailer at gmail.com (bob gailer) Date: Tue, 28 Dec 2010 20:09:27 -0500 Subject: [Tutor] blank space after a number In-Reply-To: <4D1A5229.1040301@gmail.com> References: <4D19EC31.9040705@gmail.com> <4D1A5229.1040301@gmail.com> Message-ID: <4D1A8A47.40906@gmail.com> Now that I understand what you want - for b in range(120): print '%0*i' % (max(2,int(math.log10(b))), b) -- Bob Gailer 919-636-4239 Chapel Hill NC From alan.gauld at btinternet.com Wed Dec 29 02:11:54 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Dec 2010 01:11:54 -0000 Subject: [Tutor] blank space after a number References: <4D19EC31.9040705@gmail.com> <4D1A5229.1040301@gmail.com> Message-ID: "Enih Gilead" wrote > a, b = 0, 1 > while b < 10: > print '%i%i' % (a,b) + ',', > b = b+1 If you are using string formatting it's best to get the format string to do as much of the work as possible. In this case forget about 'a' and just insert the zero into the string, and similarly don't add a comma and space, just put it in the string And for a fixed number of iterations a for loop is usually preferable: for b in range(1,10): print "%02d, " % b Does the same job. > ''' > 01, 02, 03, 04, 05, 06, 07, 08, 09, ''' > while b < 100: > print '%i' % (b) + ',', > b = b+1 And exactly the same code with the limit increased for b in range(1,101): #NB 101 to get the 100 - your while loop goes to 99... print "%02d, " % b > ''' > 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, > 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, > 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, > 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, > 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, > 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100 ''' The %02d says print the number as two characters, padding with zeros as needed. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Wed Dec 29 02:16:30 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Dec 2010 01:16:30 -0000 Subject: [Tutor] Python 2.7.1 interpreter passing function pointer as function argument and Shedskin 0.7 References: Message-ID: "Frank Chang" wrote > When I run the Shedskin 0.7 Python to C+++ compiler on the > same python program, I receive the error message * Error * > automata_test.py:148 : unbound identifier 'lookup_func'. lookup_func > is a > python function pointer passed as an argument to a python function. > I have marked lines 148,161,172 and 174 in the python program > automata_test.py shown below. The shedskin tutorial says the latest > version > of the Shedskin 0.7 Python to C++ compiler does not support > overloading > __iter__ and __call__. These sound like issues with shedskin rather than Python, you will probably get better support on their forum/newsggroup/list. You may also find other compilers deal with your particular needs better - cython for example. But finally, remember that Python is not C++. If you really need C++ use C++. The compilers are clever and can save a lot of effort, but Python is primarily an dynamic interpreted language and a lot of its more advanced idioms do not translate easily to statically compiled code. HTH, From bgailer at gmail.com Wed Dec 29 02:23:44 2010 From: bgailer at gmail.com (bob gailer) Date: Tue, 28 Dec 2010 20:23:44 -0500 Subject: [Tutor] blank space after a number In-Reply-To: <4D1A8A47.40906@gmail.com> References: <4D19EC31.9040705@gmail.com> <4D1A5229.1040301@gmail.com> <4D1A8A47.40906@gmail.com> Message-ID: <4D1A8DA0.508@gmail.com> On 12/28/2010 8:09 PM, bob gailer wrote: > Now that I understand what you want - > > for b in range(120): > print '%0*i' % (max(2,int(math.log10(b))), b) > Sorry forgot to add import math -- Bob Gailer 919-636-4239 Chapel Hill NC From smokefloat at gmail.com Wed Dec 29 03:18:17 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 28 Dec 2010 21:18:17 -0500 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: References: <4D19B9D6.1010803@gmail.com> Message-ID: But in the case you need the db constantly open(such as tracking something, where you update the db through some other offsite db), then just committing the current would be suggestible(in my opinion), and keeping the update live to check periodically for changes in the timestamps of data from the updatable source. From smokefloat at gmail.com Wed Dec 29 04:17:53 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 28 Dec 2010 22:17:53 -0500 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: References: <4D19B9D6.1010803@gmail.com> Message-ID: On Tue, Dec 28, 2010 at 9:18 PM, David Hutto wrote: > But in the case you need the db constantly open(such as tracking > something, where you update the db This assumes you don't connect directly to, but update from, for data analysis, not real time tracking. through some other offsite db), > then just committing the current would be suggestible(in my opinion), > and keeping the update live to check periodically for changes in the > timestamps of data from the updatable source. > From smokefloat at gmail.com Wed Dec 29 04:27:47 2010 From: smokefloat at gmail.com (David Hutto) Date: Tue, 28 Dec 2010 22:27:47 -0500 Subject: [Tutor] Opening and closing SQLite db In-Reply-To: References: <4D19B9D6.1010803@gmail.com> Message-ID: So in the end it boils down to: What you want the db to hold? When do you need the db to hold it? And... When and where is it necessary to access it by the user? Ahhhhh! Not an algorithm. Many paths, same destination...grasshoppa. From wprins at gmail.com Wed Dec 29 04:47:42 2010 From: wprins at gmail.com (Walter Prins) Date: Wed, 29 Dec 2010 03:47:42 +0000 Subject: [Tutor] Java Virtual Machine Launcher Question In-Reply-To: <4D1812AF.5050808@pearwood.info> References: <4D1812AF.5050808@pearwood.info> Message-ID: I'm guessing this question has something to do with this: http://isatab.sourceforge.net/validator.html Which appears to be some sort of Java application that validates "isatab" files, which appear to be related to mentioned magetab files. I'm further guessing that you thought a .jar file (A Java program archive) would be openable by WinRAR, since it can open .rar files? If so, they are utterly different things. If not, then apologies and please excuse my incorrect inferrence. It's unclear to me how this question relates to Python though? Are you trying to implement reading/writing/validation of isatab files in Python or something? Many thanks, Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From bgailer at gmail.com Wed Dec 29 06:34:10 2010 From: bgailer at gmail.com (bob gailer) Date: Wed, 29 Dec 2010 00:34:10 -0500 Subject: [Tutor] Python 2.7.1 interpreter passing function pointer as function argument and Shedskin 0.7 In-Reply-To: References: Message-ID: <4D1AC852.60300@gmail.com> On 12/28/2010 4:35 PM, Frank Chang wrote: > Good afternoon. I want to thank everyone who helped me fix the > global name 'levinshtein_automata' is not defined error. > When I run the Shedskin 0.7 Python to C+++ compiler on the > same python program, I receive the error message * Error * > automata_test.py:148 : unbound identifier 'lookup_func'. lookup_func > is a python function pointer passed as an argument to a python function. I'd like to help Please post the entire traceback. Fix the indentation. I have to make too many assumptions with the indentation messed up. Let's refine our terminology. Python does not pass pointers, it passes objects. m is not a function. It is a callable class instance. > I have marked lines 148,161,172 and 174 in the python program > automata_test.py shown below. The shedskin tutorial says the latest > version of the Shedskin 0.7 Python to C++ compiler does not support > overloading __iter__ and __call__. I was wondering if anyone could > suggest a python workaround to this error message as I am new to > python. Thank you. > # automata_test.py > import bisect > import random > class NFA(object): > EPSILON = object() > ANY = object() > def __init__(self, start_state): > self.transitions = {} > self.final_states = set() > self._start_state = start_state > @property > def start_state(self): > return frozenset(self._expand(set([self._start_state]))) > def add_transition(self, src, input, dest): > self.transitions.setdefault(src, {}).setdefault(input, > set()).add(dest) > def add_final_state(self, state): > self.final_states.add(state) > def is_final(self, states): > return self.final_states.intersection(states) > def _expand(self, states): > frontier = set(states) > while frontier: > state = frontier.pop() > new_states = self.transitions.get(state, {}).get(NFA.EPSILON, > set()).difference(states) > frontier.update(new_states) > states.update(new_states) > return states > def next_state(self, states, input): > dest_states = set() > for state in states: > state_transitions = self.transitions.get(state, {}) > dest_states.update(state_transitions.get(input, [])) > dest_states.update(state_transitions.get(NFA.ANY, [])) > return frozenset(self._expand(dest_states)) > def get_inputs(self, states): > inputs = set() > for state in states: > inputs.update(self.transitions.get(state, {}).keys()) > return inputs > def to_dfa(self): > dfa = DFA(self.start_state) > frontier = [self.start_state] > seen = set() > while frontier: > current = frontier.pop() > inputs = self.get_inputs(current) > for input in inputs: > if input == NFA.EPSILON: continue > new_state = self.next_state(current, input) > if new_state not in seen: > frontier.append(new_state) > seen.add(new_state) > if self.is_final(new_state): > dfa.add_final_state(new_state) > if input == NFA.ANY: > dfa.set_default_transition(current, new_state) > else: > dfa.add_transition(current, input, new_state) > return dfa > > > class DFA(object): > def __init__(self, start_state): > self.start_state = start_state > self.transitions = {} > self.defaults = {} > self.final_states = set() > def add_transition(self, src, input, dest): > self.transitions.setdefault(src, {})[input] = dest > def set_default_transition(self, src, dest): > self.defaults[src] = dest > def add_final_state(self, state): > self.final_states.add(state) > def is_final(self, state): > return state in self.final_states > def next_state(self, src, input): > state_transitions = self.transitions.get(src, {}) > return state_transitions.get(input, self.defaults.get(src, None)) > def next_valid_string(self, input): > state = self.start_state > stack = [] > # Evaluate the DFA as far as possible > print state > for i, x in enumerate(input): > print "%s" % input > print 'e' > stack.append((input[:i], state, x)) > state = self.next_state(state, x) > if not state: break > else: > stack.append((input[:i+1], state, None)) > > if self.is_final(state): > # Input word is already valid > return input > # Perform a 'wall following' search for the > lexicographically smallest > # accepting state. > while stack: > path, state, x = stack.pop() > x = self.find_next_edge(state, x) > #print 'x' > if x: > path += x > state = self.next_state(state, x) > if self.is_final(state): > print 'p' > return path > stack.append((path, state, None)) > print 'v' > return None > def find_next_edge(self, s, x): > if x is None: > x = '\0' > else: > x = chr(ord(x) + 1) > state_transitions = self.transitions.get(s, {}) > if x in state_transitions or s in self.defaults: > return x > labels = sorted(state_transitions.keys()) > pos = bisect.bisect_left(labels, x) > if pos < len(labels): > print 'n' > return labels[pos] > return None > def levenshtein_automata(self, term, k): > print 's' > nfa = NFA((0, 0)) > for i, c in enumerate(term): > for e in range(k + 1): > # Correct character > nfa.add_transition((i, e), c, (i + 1, e)) > if e < k: > # Deletion > nfa.add_transition((i, e), NFA.ANY, (i, e + 1)) > # Insertion > nfa.add_transition((i, e), NFA.EPSILON, (i + 1, e + 1)) > # Substitution > nfa.add_transition((i, e), NFA.ANY, (i + 1, e + 1)) > for e in range(k + 1): > if e < k: > nfa.add_transition((len(term), e), NFA.ANY, (len(term), e + 1)) > nfa.add_final_state((len(term), e)) > return nfa > def find_all_matches(self, word, k, lookup_func): > lev = self.levenshtein_automata(word, k).to_dfa() > match = lev.next_valid_string('\0') > while match: > next = lookup_func(match) ***** line 148 ***** > if not next: > return > if match == next: > yield match > next = next + '\0' > match = lev.next_valid_string(next) > class Matcher(object): > def __init__(self, l): > self.l = l > self.probes = 0 > def __call__(self, w): **** line 161 ****** > self.probes += 1 > pos = bisect.bisect_left(self.l, w) > if pos < len(self.l): > return self.l[pos] > else: > return None > > words = [x.strip().lower() for x in open('F:/shedskin/database.txt')] > words.sort() > m = Matcher(words) **** line 172 ***** > testdfa = DFA(1) > length = len(list(testdfa.find_all_matches('food', 1, m))) **** > line 174 *** > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From stefan_ml at behnel.de Wed Dec 29 07:10:08 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 29 Dec 2010 07:10:08 +0100 Subject: [Tutor] Python 2.7.1 interpreter passing function pointer as function argument and Shedskin 0.7 In-Reply-To: References: Message-ID: Frank Chang, 28.12.2010 22:35: > Good afternoon. I want to thank everyone who helped me fix the global > name 'levinshtein_automata' is not defined error. > When I run the Shedskin 0.7 Python to C+++ compiler on the > same python program, I receive the error message * Error * > automata_test.py:148 : unbound identifier 'lookup_func'. ShedSkin is very restrictive compared to CPython. It requires static types for variables and it doesn't support all Python features. Is there a reason you want to use ShedSkin for your program? Stefan From frankchang91 at gmail.com Wed Dec 29 10:25:50 2010 From: frankchang91 at gmail.com (Frank Chang) Date: Wed, 29 Dec 2010 01:25:50 -0800 Subject: [Tutor] Python 2.7.1 interpreter passing function pointer as function argument and Shedskin 0.7 Message-ID: I asked the Shedskin developers about this issue and they are currently adding support for __call__ . They recommend renaming the class Matcher __call__ method ,for example as next, and then explicitly call it on line 148 as lookup_func.next(match). I followed their suggestion and the Shedskin 0.7 Python to C++ compiler does not complain about the unbound identifier 'lookup_func' anymore. I apologize for the cut and paste mangling. Is there a better method than copy-pasting for including 20 or more lines of python source code in the tutor posts? Thank you. def find_all_matches(self, word, k, lookup_func): lev = self.levenshtein_automata(word, k).to_dfa() match = lev.next_valid_string('\0') while match: follow = lookup_func.test(match) ### line 148 ### if not follow: return if match == follow: yield match follow = follow + '\0' match = lev.next_valid_string(follow) class Matcher(object): def __init__(self, l): self.l = l self.probes = 0 def test(self, w): self.probes += 1 pos = bisect.bisect_left(self.l, w) if pos < len(self.l): return self.l[pos] else: return None -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Dec 29 10:41:15 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Dec 2010 09:41:15 -0000 Subject: [Tutor] Python 2.7.1 interpreter passing function pointer as function argument and Shedskin 0.7 References: Message-ID: "Frank Chang" wrote > I apologize for the cut and paste mangling. Is there a better > method > than copy-pasting for including 20 or more lines of python source > code in > the tutor posts? Thank you. Long listings are usually better in pastebin - where the indentation is clear and we get syntax colouring too. It all makes them more readable :-) Then just post the url to the pastebin on your mail. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ From tommy.kaas at kaasogmulvad.dk Wed Dec 29 10:54:02 2010 From: tommy.kaas at kaasogmulvad.dk (Tommy Kaas) Date: Wed, 29 Dec 2010 10:54:02 +0100 Subject: [Tutor] scraping and saving in file Message-ID: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> Hi, I?m trying to learn basic web scraping and starting from scratch. I?m using Activepython 2.6.6 I have uploaded a simple table on my web page and try to scrape it and will save the result in a text file. I will separate the columns in the file with #. It works fine but besides # I also get spaces between the columns in the text file. How do I avoid that? This is the script: import urllib2 from BeautifulSoup import BeautifulSoup f = open('tabeltest.txt', 'w') soup = BeautifulSoup(urllib2.urlopen('http://www.kaasogmulvad.dk/unv/python/tabelte st.htm').read()) rows = soup.findAll('tr') for tr in rows: cols = tr.findAll('td') print >> f, cols[0].string,'#',cols[1].string,'#',cols[2].string,'#',cols[3].string f.close() And the text file looks like this: Kommunenr # Kommune # Region # Regionsnr 101 # K?benhavn # Hovedstaden # 1084 147 # Frederiksberg # Hovedstaden # 1084 151 # Ballerup # Hovedstaden # 1084 153 # Br?ndby # Hovedstaden # 1084 155 # Drag?r # Hovedstaden # 1084 Thanks in advance Tommy Kaas Kaas & Mulvad Lykkesholms Alle 2A, 3. 1902 Frederiksberg C Mobil: 27268818 Mail: tommy.kaas at kaasogmulvad.dk Web: www.kaasogmulvad.dk -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Dec 29 11:28:07 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 29 Dec 2010 21:28:07 +1100 Subject: [Tutor] Python 2.7.1 interpreter passing function pointer as function argument and Shedskin 0.7 In-Reply-To: References: Message-ID: <4D1B0D37.8020206@pearwood.info> Alan Gauld wrote: > > "Frank Chang" wrote > >> I apologize for the cut and paste mangling. Is there a better method >> than copy-pasting for including 20 or more lines of python source code in >> the tutor posts? Thank you. > > Long listings are usually better in pastebin - where the indentation is > clear and we get syntax colouring too. It all makes them more readable :-) Even better is to spend the time to work out the smallest amount of code that exhibits the problem. The code you post must be runnable, it must actually demonstrate the problem -- it is surprising how many people post "broken" code that actually works, or worse, code that fails in some completely different way -- and lastly, and most importantly, it should be as small as possible. Doing so shows respect to those you are asking to look at the code, AND maximizes the number of people willing to spend the time answering your question: instead of looking at 200 lines and seven functions, they only need to look at one function and sixteen lines of code. (Or whatever it takes to reproduce the problem.) Even more important, while trying to reproduce the problem in the minimum code possible, you may very well solve the problem yourself. I can't tell you how many times I've gone to write up a request for help, but while refactoring my code I've discovered the mistake and didn't need to post my question. -- Steven From steve at pearwood.info Wed Dec 29 11:38:49 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 29 Dec 2010 21:38:49 +1100 Subject: [Tutor] scraping and saving in file In-Reply-To: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> References: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> Message-ID: <4D1B0FB9.6030608@pearwood.info> Tommy Kaas wrote: > I have uploaded a simple table on my web page and try to scrape it and will > save the result in a text file. I will separate the columns in the file with > #. > > It works fine but besides # I also get spaces between the columns in the > text file. How do I avoid that? The print command puts spaces between the each output object: >>> print 1, 2, 3 # Three objects being printed. 1 2 3 To prevent this, use a single output object. There are many ways to do this, here are three: >>> print "%d%d%d" % (1, 2, 3) 123 >>> print str(1) + str(2) + str(3) 123 >>> print ''.join('%s' % n for n in (1, 2, 3)) 123 But in your case, the best way is not to use print at all. You are writing to a file -- write to the file directly, don't mess about with print. Untested: f = open('tabeltest.txt', 'w') url = 'http://www.kaasogmulvad.dk/unv/python/tabeltest.htm' soup = BeautifulSoup(urllib2.urlopen(url).read()) rows = soup.findAll('tr') for tr in rows: cols = tr.findAll('td') output = "#".join(cols[i].string for i in (0, 1, 2, 3)) f.write(output + '\n') # don't forget the newline after each row f.close() -- Steven From knacktus at googlemail.com Wed Dec 29 11:41:24 2010 From: knacktus at googlemail.com (Knacktus) Date: Wed, 29 Dec 2010 11:41:24 +0100 Subject: [Tutor] scraping and saving in file In-Reply-To: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> References: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> Message-ID: <4D1B1054.8030503@googlemail.com> Am 29.12.2010 10:54, schrieb Tommy Kaas: > Hi, > > I?m trying to learn basic web scraping and starting from scratch. I?m > using Activepython 2.6.6 > > I have uploaded a simple table on my web page and try to scrape it and > will save the result in a text file. I will separate the columns in the > file with #. > > It works fine but besides # I also get spaces between the columns in the > text file. How do I avoid that? > > This is the script: > > import urllib2 > > from BeautifulSoup import BeautifulSoup > > f = open('tabeltest.txt', 'w') > > soup = > BeautifulSoup(urllib2.urlopen('http://www.kaasogmulvad.dk/unv/python/tabeltest.htm').read()) > > rows = soup.findAll('tr') > > for tr in rows: > > cols = tr.findAll('td') > > print >> f, > cols[0].string,'#',cols[1].string,'#',cols[2].string,'#',cols[3].string You can strip the whitespaces from the strings. I assume the "string"-attribute returns a string (I don't now the API of Beautiful Soup) E.g.: cols[0].string.strip() Also, you can use join() to create the complete string: resulting_string = "#".join([col.string.strip() for col in cols]) The long version without list comprehension (just for illustration, better use list comprehension): resulting_string = "#".join([cols[0].string.strip(), cols[1].string.strip(), cols[2].string.strip(), cols[3].string.strip(), cols[4].string.strip()]) HTH, Jan > > f.close() > > And the text file looks like this: > > Kommunenr # Kommune # Region # Regionsnr > > 101 # K?benhavn # Hovedstaden # 1084 > > 147 # Frederiksberg # Hovedstaden # 1084 > > 151 # Ballerup # Hovedstaden # 1084 > > 153 # Br?ndby # Hovedstaden # 1084 > > 155 # Drag?r # Hovedstaden # 1084 > > Thanks in advance > > Tommy Kaas > > Kaas & Mulvad > > Lykkesholms Alle 2A, 3. > > 1902 Frederiksberg C > > Mobil: 27268818 > > Mail: tommy.kaas at kaasogmulvad.dk > > Web: www.kaasogmulvad.dk > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Wed Dec 29 11:46:20 2010 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Dec 2010 11:46:20 +0100 Subject: [Tutor] scraping and saving in file References: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> Message-ID: Tommy Kaas wrote: > I?m trying to learn basic web scraping and starting from scratch. I?m > using Activepython 2.6.6 > I have uploaded a simple table on my web page and try to scrape it and > will save the result in a text file. I will separate the columns in the > file with > #. > It works fine but besides # I also get spaces between the columns in the > text file. How do I avoid that? > This is the script: > import urllib2 > from BeautifulSoup import BeautifulSoup > f = open('tabeltest.txt', 'w') > soup = BeautifulSoup(urllib2.urlopen('http://www.kaasogmulvad.dk/unv/python/tabelte > st.htm').read()) > rows = soup.findAll('tr') > for tr in rows: > cols = tr.findAll('td') > print >> f, > cols[0].string,'#',cols[1].string,'#',cols[2].string,'#',cols[3].string > > f.close() > And the text file looks like this: > Kommunenr # Kommune # Region # Regionsnr > 101 # K?benhavn # Hovedstaden # 1084 > 147 # Frederiksberg # Hovedstaden # 1084 > 151 # Ballerup # Hovedstaden # 1084 > 153 # Br?ndby # Hovedstaden # 1084 The print statement automatically inserts spaces, so you can either resort to the write method for i in range(4): if i: f.write("#") f.write(cols[i].string) which is a bit clumsy, or you build the complete line and then print it as a whole: print >> f, "#".join(col.string for col in cols) Note that you have non-ascii characters in your data -- I'm surprised that writing to a file works for you. I would expect that import codecs f = codecs.open("tmp.txt", "w", encoding="utf-8") is needed to successfully write your data to a file Peter From tommy.kaas at kaasogmulvad.dk Wed Dec 29 11:55:52 2010 From: tommy.kaas at kaasogmulvad.dk (Tommy Kaas) Date: Wed, 29 Dec 2010 11:55:52 +0100 Subject: [Tutor] scraping and saving in file In-Reply-To: <4D1B0FB9.6030608@pearwood.info> References: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> <4D1B0FB9.6030608@pearwood.info> Message-ID: <013701cba746$f6690c60$e33b2520$@kaasogmulvad.dk> Steven D'Aprano wrote: > But in your case, the best way is not to use print at all. You are writing to a > file -- write to the file directly, don't mess about with print. Untested: > > > f = open('tabeltest.txt', 'w') > url = 'http://www.kaasogmulvad.dk/unv/python/tabeltest.htm' > soup = BeautifulSoup(urllib2.urlopen(url).read()) > rows = soup.findAll('tr') > for tr in rows: > cols = tr.findAll('td') > output = "#".join(cols[i].string for i in (0, 1, 2, 3)) > f.write(output + '\n') # don't forget the newline after each row > f.close() Steven, thanks for the advice. I see the point. But now I have problems with the Danish characters. I get this: Traceback (most recent call last): File "C:/pythonlib/kursus/kommuner-regioner_ny.py", line 36, in f.write(output + '\n') # don't forget the newline after each row UnicodeEncodeError: 'ascii' codec can't encode character u'\xf8' in position 5: ordinal not in range(128) I have tried to add # -*- coding: utf-8 -*- to the top of the script, but It doesn't help? Tommy From __peter__ at web.de Wed Dec 29 12:26:24 2010 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Dec 2010 12:26:24 +0100 Subject: [Tutor] scraping and saving in file References: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> <4D1B0FB9.6030608@pearwood.info> <013701cba746$f6690c60$e33b2520$@kaasogmulvad.dk> Message-ID: Tommy Kaas wrote: > Steven D'Aprano wrote: >> But in your case, the best way is not to use print at all. You are >> writing > to a >> file -- write to the file directly, don't mess about with print. >> Untested: >> >> >> f = open('tabeltest.txt', 'w') >> url = 'http://www.kaasogmulvad.dk/unv/python/tabeltest.htm' >> soup = BeautifulSoup(urllib2.urlopen(url).read()) >> rows = soup.findAll('tr') >> for tr in rows: >> cols = tr.findAll('td') >> output = "#".join(cols[i].string for i in (0, 1, 2, 3)) >> f.write(output + '\n') # don't forget the newline after each row >> f.close() > > Steven, thanks for the advice. > I see the point. But now I have problems with the Danish characters. I get > this: > > Traceback (most recent call last): > File "C:/pythonlib/kursus/kommuner-regioner_ny.py", line 36, in > f.write(output + '\n') # don't forget the newline after each row > UnicodeEncodeError: 'ascii' codec can't encode character u'\xf8' in > position 5: ordinal not in range(128) > > I have tried to add # -*- coding: utf-8 -*- to the top of the script, but > It doesn't help? The coding cookie only affects unicode string constants in the source code, it doesn't change how the unicode data coming from BeautifulSoup is handled. As I suspected in my other post you have to convert your data to a specific encoding (I use UTF-8 below) before you can write it to a file: import urllib2 import codecs from BeautifulSoup import BeautifulSoup html = urllib2.urlopen( 'http://www.kaasogmulvad.dk/unv/python/tabeltest.htm').read() soup = BeautifulSoup(html) with codecs.open('tabeltest.txt', "w", encoding="utf-8") as f: rows = soup.findAll('tr') for tr in rows: cols = tr.findAll('td') print >> f, "#".join(col.string for col in cols) The with statement implicitly closes the file, so you can avoid f.close() at the end of the script. Peter From tommy.kaas at kaasogmulvad.dk Wed Dec 29 12:25:19 2010 From: tommy.kaas at kaasogmulvad.dk (Tommy Kaas) Date: Wed, 29 Dec 2010 12:25:19 +0100 Subject: [Tutor] scraping and saving in file SOLVED Message-ID: <013801cba74b$13052080$390f6180$@kaasogmulvad.dk> With Stevens help about writing and Peters help about import codecs - and when I used \r\n instead of \r to give me new lines everything worked. I just thought that \n would be necessary? Thanks. Tommy > -----Oprindelig meddelelse----- > Fra: tutor-bounces+tommy.kaas=kaasogmulvad.dk at python.org > [mailto:tutor-bounces+tommy.kaas=kaasogmulvad.dk at python.org] P? > vegne af Peter Otten > Sendt: 29. december 2010 11:46 > Til: tutor at python.org > Emne: Re: [Tutor] scraping and saving in file > > Tommy Kaas wrote: > > > I?m trying to learn basic web scraping and starting from scratch. I?m > > using Activepython 2.6.6 > > > I have uploaded a simple table on my web page and try to scrape it and > > will save the result in a text file. I will separate the columns in > > the file with #. > > > It works fine but besides # I also get spaces between the columns in > > the text file. How do I avoid that? > > > This is the script: > > > import urllib2 > > from BeautifulSoup import BeautifulSoup f = open('tabeltest.txt', 'w') > > soup = > BeautifulSoup(urllib2.urlopen('http://www.kaasogmulvad.dk/unv/python/ta > belte > > st.htm').read()) > > > rows = soup.findAll('tr') > > > for tr in rows: > > cols = tr.findAll('td') > > print >> f, > > cols[0].string,'#',cols[1].string,'#',cols[2].string,'#',cols[3].strin > > g > > > > f.close() > > > And the text file looks like this: > > > Kommunenr # Kommune # Region # Regionsnr > > 101 # K?benhavn # Hovedstaden # 1084 > > 147 # Frederiksberg # Hovedstaden # 1084 > > 151 # Ballerup # Hovedstaden # 1084 > > 153 # Br?ndby # Hovedstaden # 1084 > > The print statement automatically inserts spaces, so you can either resort to > the write method > > for i in range(4): > if i: > f.write("#") > f.write(cols[i].string) > > which is a bit clumsy, or you build the complete line and then print it as a > whole: > > print >> f, "#".join(col.string for col in cols) > > Note that you have non-ascii characters in your data -- I'm surprised that > writing to a file works for you. I would expect that > > import codecs > f = codecs.open("tmp.txt", "w", encoding="utf-8") > > is needed to successfully write your data to a file > > Peter > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From davea at ieee.org Wed Dec 29 13:25:31 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Dec 2010 07:25:31 -0500 Subject: [Tutor] scraping and saving in file In-Reply-To: <013701cba746$f6690c60$e33b2520$@kaasogmulvad.dk> References: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> <4D1B0FB9.6030608@pearwood.info> <013701cba746$f6690c60$e33b2520$@kaasogmulvad.dk> Message-ID: <4D1B28BB.5000805@ieee.org> On 01/-10/-28163 02:59 PM, Tommy Kaas wrote: > Steven D'Aprano wrote: >> But in your case, the best way is not to use print at all. You are writing > to a >> file -- write to the file directly, don't mess about with print. Untested: >> >> >> f = open('tabeltest.txt', 'w') >> url = 'http://www.kaasogmulvad.dk/unv/python/tabeltest.htm' >> soup = BeautifulSoup(urllib2.urlopen(url).read()) >> rows = soup.findAll('tr') >> for tr in rows: >> cols = tr.findAll('td') >> output = "#".join(cols[i].string for i in (0, 1, 2, 3)) >> f.write(output + '\n') # don't forget the newline after each row >> f.close() > > Steven, thanks for the advice. > I see the point. But now I have problems with the Danish characters. I get > this: > > Traceback (most recent call last): > File "C:/pythonlib/kursus/kommuner-regioner_ny.py", line 36, in > f.write(output + '\n') # don't forget the newline after each row > UnicodeEncodeError: 'ascii' codec can't encode character u'\xf8' in position > 5: ordinal not in range(128) > > I have tried to add # -*- coding: utf-8 -*- to the top of the script, but It > doesn't help? > > Tommy > The coding line only affects how characters in the source module are interpreted. For each file you input or output, you need to also decide the encoding to use. As Peter said, you probably need codecs.open(filename, "w", encoding="utf-8") DaveA From evosweet at hotmail.com Wed Dec 29 11:14:33 2010 From: evosweet at hotmail.com (Rayon) Date: Wed, 29 Dec 2010 06:14:33 -0400 Subject: [Tutor] python telnet slow down. Message-ID: Hi I need help with a some telnet automation I am trying build. I need to login to a Nortel switch and send a table dump command capture that data and send it to I file. I have the code it, works and does all that I need my problem is that when I try to dump a table later than 4 mega the it can take hours but when I use cross talk and do it the manual way it takes 20 min. I need to know why and if there are any changes I can make to my code to fix this. #!/usr/bin/env python import telnetlib #import telnet_hack class Telnet_lib: host = {'server':'','user':'','password':''} commands = [] read_till = '' capture_file ='' def fileopen(self,capture_file,data): try: out_file = open(capture_file,"w") out_file.write(data) except IOError,error: print error def gtt_telnet(self): tn = telnetlib.Telnet(self.host['server']) tn.set_debuglevel(100) tn.read_until('Enter User Name', 3) tn.write(self.host['user'] + "\r") tn.read_until('Password: ', 3) tn.write(self.host['password'] + "\r") tn.read_until('HGUO0190.PPC4 V:103', 3) for com in self.commands: tn.write(com + "\r") print 'dump starts' tn.read_some() #data = tn.read_until(self.read_till) #self.fileopen(self.capture_file,data) From __peter__ at web.de Wed Dec 29 13:45:57 2010 From: __peter__ at web.de (Peter Otten) Date: Wed, 29 Dec 2010 13:45:57 +0100 Subject: [Tutor] scraping and saving in file SOLVED References: <013801cba74b$13052080$390f6180$@kaasogmulvad.dk> Message-ID: Tommy Kaas wrote: > With Stevens help about writing and Peters help about import codecs - and > when I used \r\n instead of \r to give me new lines everything worked. I > just thought that \n would be necessary? Thanks. > Tommy Newline handling varies across operating systems. If you are on Windows and open a file in text mode your program sees plain "\n", but the data stored on disk is "\r\n". Most other OSes don't mess with newlines. If you always want "\r\n" you can rely on the csv module to write your data, but the drawback is that you have to encode the strings manually: import csv import urllib2 from BeautifulSoup import BeautifulSoup html = urllib2.urlopen( 'http://www.kaasogmulvad.dk/unv/python/tabeltest.htm').read() soup = BeautifulSoup(html) with open('tabeltest.txt', "wb") as f: writer = csv.writer(f, delimiter="#") rows = soup.findAll('tr') for tr in rows: cols = tr.findAll('td') writer.writerow([unicode(col.string).encode("utf-8") for col in cols]) PS: It took me some time to figure out how deal with beautifulsoup's flavour of unicode: >>> import BeautifulSoup as bs >>> s = bs.NavigableString(u"?lpha") >>> s u'\xe4lpha' >>> s.encode("utf-8") Traceback (most recent call last): File "", line 1, in File "/usr/lib/pymodules/python2.6/BeautifulSoup.py", line 430, in encode return self.decode().encode(encoding) UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 0: ordinal not in range(128) >>> unicode(s).encode("utf-8") # heureka '\xc3\xa4lpha' From delegbede at dudupay.com Wed Dec 29 13:49:15 2010 From: delegbede at dudupay.com (delegbede at dudupay.com) Date: Wed, 29 Dec 2010 12:49:15 +0000 Subject: [Tutor] Print Help Message-ID: <1536460536-1293626923-cardhu_decombobulator_blackberry.rim.net-1838606602-@b3.c12.bise7.blackberry> I want to loop over a list and then print out a one statement that carries all the items in the list e.g def checking(responses): ''' Converts strings that look like A2C3D89AA90 into {'A':'2', 'C':'3', 'D':'89', 'AA':'90'}''' responses=dict(re.findall(r'([A-Z]{1,2})([0-9]+)', responses.upper())) if responses else {} for key in responses.keys(): count = [] if key in ['A'] and int(responses[key]) not in range(1,3): count += key elif key in ['B', 'G', 'T', 'U', 'V', 'W', 'X'] and int(responses[key]) not in range(1, 3): count += key elif key in ['H', 'J', 'K', 'M', 'N', 'P', 'Q','R','S'] and int(responses[key]) not in range(1, 6): count += key elif key in ['D', 'E'] and int(responses[key]) == 4: print 'accepted' for err in count: count = ", ".join(count) print "Invalid Message: You specified out of range values for %s" % (", ".join(count)) What I am expecting is: Invalid Message: You specified out of range values for a,s,d,r,t,u Assuming those were the values that are incorrect. Ple help. Sent from my BlackBerry wireless device from MTN From stefan_ml at behnel.de Wed Dec 29 13:58:51 2010 From: stefan_ml at behnel.de (Stefan Behnel) Date: Wed, 29 Dec 2010 13:58:51 +0100 Subject: [Tutor] scraping and saving in file SOLVED In-Reply-To: References: <013801cba74b$13052080$390f6180$@kaasogmulvad.dk> Message-ID: Peter Otten, 29.12.2010 13:45: > File "/usr/lib/pymodules/python2.6/BeautifulSoup.py", line 430, in encode > return self.decode().encode(encoding) Wow, that's evil. Stefan From davea at ieee.org Wed Dec 29 14:27:05 2010 From: davea at ieee.org (Dave Angel) Date: Wed, 29 Dec 2010 08:27:05 -0500 Subject: [Tutor] Print Help In-Reply-To: <1536460536-1293626923-cardhu_decombobulator_blackberry.rim.net-1838606602-@b3.c12.bise7.blackberry> References: <1536460536-1293626923-cardhu_decombobulator_blackberry.rim.net-1838606602-@b3.c12.bise7.blackberry> Message-ID: <4D1B3729.5040506@ieee.org> On 01/-10/-28163 02:59 PM, delegbede at dudupay.com wrote: > #???%??h????+-jwm????)???Z?w???^?????q?????[az+^???????^???r??)???)?{ ?*'?????k?x,????$?) `??@?"?? Not sure why the quoting of this particular message didn't work. Since I have no problem with anyone else's message, I suggest it's one of your settings. Recopying your original: >I want to loop over a list and then print out a one statement that >carries all the items in the list e.g > >def checking(responses): > > ''' Converts strings that look like A2C3D89AA90 into > {'A':'2', 'C':'3', 'D':'89', 'AA':'90'}''' > > responses=dict(re.findall(r'([A-Z]{1,2})([0-9]+)', >responses.upper())) if responses else {} > for key in responses.keys(): > count = [] > if key in ['A'] and int(responses[key]) not in >range(1,3): count += key > elif key in ['B', 'G', 'T', 'U', 'V', 'W', 'X'] and >int(responses[key]) not in range(1, 3): count += key > elif key in ['H', 'J', 'K', 'M', 'N', 'P', >'Q','R','S'] and int(responses[key]) not in range(1, 6): count += key > elif key in ['D', 'E'] and int(responses[key]) == 4: >print 'accepted' > for err in count: > count = ", ".join(count) > > print "Invalid Message: You specified out of range values >for %s" % (", ".join(count)) > >What I am expecting is: >Invalid Message: You specified out of range values for a,s,d,r,t,u > >Assuming those were the values that are incorrect. Hi Delegbede, First comment is that your indentation is wrong, and the code won't compile. Did you want your print to line up with "for key" or with "elif" ? Second is that you keep defining count to have different types. Choose what type it's supposed to have, and fix all the places that would make it different. Your comments imply you want it to be a list, which is what you initialize it to. But then you concatenate a string to it, which will give an error. You probably wanted to append to it, using counts.append(key) Then you have a nonsensical twoline loop: for err in count, but inside the loop you change count. Delete both those lines, as you're going to use join inside your print statement instead. Finally, you reassign the counts list inside the loop, so it will only show the last values found. Move the counts=[] outside the outermost loop. Fix the code up so it doesn't get any compile or runtime errors, and if you can't, post a complete traceback for the error you can't fix. And if you can run it without any errors, but the output is wrong, show us what it actually prints, not just what you'd like it to. DaveA From frankchang91 at gmail.com Wed Dec 29 20:45:25 2010 From: frankchang91 at gmail.com (Frank Chang) Date: Wed, 29 Dec 2010 11:45:25 -0800 Subject: [Tutor] Python 2.7.1 interpreter question Message-ID: I separated my test program into two python files. The first one is automata.py. The pastebin url for automata.py is: The second file is automata_test2.py. It imports automata.py. The pastebin url for automata_test2.py is: . When I use the Python 2.7.1 interpreter I get the following traceback : F:\shedskin\shedskin-0.7>python automata_test2.py Traceback (most recent call last): File "automata_test2.py", line 23, in list(automata.find_all_matches('nice', 1, m)) AttributeError: 'module' object has no attribute 'find_all_matches' Could anyone help me fix this error? I am new to python. Thank you. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Dec 29 21:33:26 2010 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Dec 2010 20:33:26 -0000 Subject: [Tutor] Python 2.7.1 interpreter question References: Message-ID: "Frank Chang" wrote > automata.py. The pastebin url for automata.py is: > > list(automata.find_all_matches('nice', 1, m)) > AttributeError: 'module' object has no attribute 'find_all_matches' find_all_matches() is a method of a class. Therefore you need to call it via an instance of that class. You need to instantiate the class using the module name but after that the object will be local: myObj = module.myclass() myObj.myMethod() HTH, From steve at pearwood.info Wed Dec 29 21:34:06 2010 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 30 Dec 2010 07:34:06 +1100 Subject: [Tutor] Python 2.7.1 interpreter question In-Reply-To: References: Message-ID: <4D1B9B3E.9030409@pearwood.info> Frank Chang wrote: > When I use the Python 2.7.1 interpreter I get the following traceback > : > > F:\shedskin\shedskin-0.7>python automata_test2.py > Traceback (most recent call last): > File "automata_test2.py", line 23, in > list(automata.find_all_matches('nice', 1, m)) > AttributeError: 'module' object has no attribute 'find_all_matches' > > Could anyone help me fix this error? I am new to python. Thank you. This error tells you that the module "automata.py" does not have a global-level object called "find_all_matches". If you look at the module, you will see it does not have a global-level object called "find_all_matches", exactly as Python tells you. To fix this, you need to either: (1) Give the automata module a function "find_all_matches" that does what you expect it to do; or (2) Change automata_test2 to create a DFA instance, then call the find_all_matches method in that instance. Something like: instance = automata.DFA(start_state) instance.find_all_matches("word", k, lookup_func) -- Steven From patrick.just4fun at gmail.com Thu Dec 30 10:41:06 2010 From: patrick.just4fun at gmail.com (Patrick Sabin) Date: Thu, 30 Dec 2010 10:41:06 +0100 Subject: [Tutor] scraping and saving in file In-Reply-To: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> References: <011301cba73e$53f88d00$fbe9a700$@kaasogmulvad.dk> Message-ID: <4D1C53B2.403@gmail.com> On 2010-12-29 10:54, Tommy Kaas wrote: > It works fine but besides # I also get spaces between the columns in the > text file. How do I avoid that? You could use the new print-function and the sep keyword argument, e.g.: from __future__ import print_function f = open("filename", "w") print("1", "2", "3", file=f, sep="") From danci_emanuel at yahoo.com Thu Dec 30 13:07:35 2010 From: danci_emanuel at yahoo.com (Danci Emanuel) Date: Thu, 30 Dec 2010 04:07:35 -0800 (PST) Subject: [Tutor] Problem installing pyaudiere Message-ID: <527116.18401.qm@web110412.mail.gq1.yahoo.com> Hello! I am trying to install the pyaudiere package on linux and I get the following error: "?AudioDevice.h:6: fatal error: audiere.h: No such file or directorycompilation terminated. " I am interested if anyone managed to solve this problem or if someone could suggest a package that could replace pyaudiere. Thanks,Emanuel DANCI -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Dec 30 13:40:43 2010 From: __peter__ at web.de (Peter Otten) Date: Thu, 30 Dec 2010 13:40:43 +0100 Subject: [Tutor] Problem installing pyaudiere References: <527116.18401.qm@web110412.mail.gq1.yahoo.com> Message-ID: Danci Emanuel wrote: > I am trying to install the pyaudiere package on linux and I get the > following error: " AudioDevice.h:6: fatal error: audiere.h: No such file > or directorycompilation terminated. " I am interested if anyone managed to > solve this problem or if someone could suggest a package that could > replace pyaudiere. [Just gueessing] If you want to install pyaudiere from source you probably need the development package for the underlying library. You may be able to install that with sudo apt-get install libaudiere-dev or the equivalent command and package for your distribution. Peter From serdar at pardus.org.tr Thu Dec 30 13:36:50 2010 From: serdar at pardus.org.tr (Serdar Dalgic) Date: Thu, 30 Dec 2010 14:36:50 +0200 Subject: [Tutor] Problem installing pyaudiere In-Reply-To: <527116.18401.qm@web110412.mail.gq1.yahoo.com> References: <527116.18401.qm@web110412.mail.gq1.yahoo.com> Message-ID: <20101230143650.4cc4ce56.serdar@pardus.org.tr> On Thu, 30 Dec 2010 04:07:35 -0800 (PST) Danci Emanuel wrote: > > Hello! Hi; > I am trying to install the pyaudiere package on linux and I get the following error: "?AudioDevice.h:6: fatal error: audiere.h: No such file or directorycompilation terminated. " > I am interested if anyone managed to solve this problem or if someone could suggest a package that could replace pyaudiere. > Thanks,Emanuel DANCI > from the homepage of PyAudiere: "PyAudiere depends on the Audiere Sound Library" http://pyaudiere.org/ So you should install audiere too. -- - Serdar Dalgic TUBITAK/UEKAE - Pardus GNU/Linux http://www.pardus.org.tr/eng From frankchang91 at gmail.com Thu Dec 30 17:27:08 2010 From: frankchang91 at gmail.com (Frank Chang) Date: Thu, 30 Dec 2010 08:27:08 -0800 Subject: [Tutor] Python 2.7.1 interpreter Message-ID: I wanted to thank Steven D'Aprano and Allan Gauld for solving my Python 2.7.1 interpreter question in Tutor Digest Vol. 82 Issue 121. Thank you for yor help. -------------- next part -------------- An HTML attachment was scrubbed... URL: