Re: [Tutor] SSL Error
On Wed, 1 Aug 2018, Marc Tompkins wrote: On 01/08/18 05:07, Saket Mehrotra wrote: Hi I am also not using any Open SSL package. I have just added " import requests" in py file. And when I run the module I get the SSL package error ,not sure why. Give us the _whole_ error message, even the parts that look like they don't make any sense. For one thing, the traceback tells us exactly which line of code triggered the exception - and which file that line of code came from. From your description, it sounds like the error is being thrown by the requests module, but we can't tell. Also, before your "import requests" line, include these: import sys print(sys.version) After the "import requests" line, include this: print(requests.__version__) -- Terry Carroll carr...@tjc.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Wish to upgrade Python 3.6.5 to Python 3.6.6 for Linux Mint 19
On Sat, 14 Jul 2018, boB Stepp wrote: Linux Mint 19 comes with Python 3.6.5 pre-installed But after much searching I cannot find an _easy_ way to do this upgrade. Unlike many distributions, Mint's philosophy is *not* to install most updates by default, on the "if it's not broken, don't fix it" theory. That being said, if you do want to update to the latest version available for Mint, this command should do it for you: sudo apt-get install --only-upgrade python3 If Mint doesn't have a vetted 3.6.6 yet, I would leave it alone. -- Terry Carroll carr...@tjc.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regex not working as desired
On Mon, 26 Feb 2018, Terry Carroll wrote: Instead of looking fo re xcaprions.. Wow. That should read "Instead of looking for exceptions..." Something really got away from me there. -- Terry Carroll carr...@tjc.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regex not working as desired
On Mon, 26 Feb 2018, Roger Lea Scherer wrote: """ ensure input is no other characters than digits sudocode: if the input has anything other than digits return digits """ p = re.compile(r'[^\D]') I'm not so great at regular expressions, but this regex appears to be searching for a string that matches anything in the class start-of-string of non-digit. "[...]" says, look for anything in this set of characters; and you have two things: ^ : start-of-string \D : any non-digit Instead of looking fo re xcaprions, I would look for what you *do* want. this regex should do it for you: r'^\d+$' This is looking for a start-of-string ("^"); then a digit ("\d") that occurs at least once (the "+" qualifier); then an end-of string ("$"). In other words, one or more digits, with nothing else before or after. Here's a simple looping test to get you started (ignore the "from __future__" line; I'm running Python 2): from __future__ import print_function import re p = re.compile(r'^\d+$') test_data = ["4jkk33", "4k33", "4jjk4", "4334", "4","44", "444", ""] for thing in test_data: m = p.match(thing) if m is None: print("not all digits:", thing) else: print("all digits:", thing) -- Terry Carroll carr...@tjc.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Java equivalent of Python-Tutor?
In my early days of using Python I benefited greatly from this Tutor list, thanks to both Alan and Steven as well as as many contributors. I still check in now and then and try to chime in to help now that I have a bit more experience under my belt. I'm doing a few projects in Java now and would love to find a similar resource that covers that language, and the Eclipse IDE. Some of my questions are too newbie for a forum like stackoverflow (and most of the responses there assume a non-newbie level of knowledge). Any suggestions? (I acknowledge that this is a bit off-topic, but I hope the blatantly obsequious sucking up at the beginning of my note makes up for it.) -- Terry Carroll carr...@tjc.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Logging exceptions, but getting stderr output instead
On Wed, 25 May 2016, Alex Hall wrote: You're not missing anything; I wasn't clear. I wasn't sure if raise or sys.exit(1) were the preferred ways, or if there was some other way I didn't know about. If you're aborting because of the exception after unsuccessfully trying to handle it, you can always just use "raise" with no operands, which will re-raise the underlying exception. That's what I usually do: try: 1/0 except ZeroDivisionError: print "oops." raise prints: oops. Traceback (most recent call last): File "[...]\test.py", line 2, in 1/0 ZeroDivisionError: integer division or modulo by zero ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Getting started in testing
Thanks to Alan, Danny, Albert-Jan and Ben for their suggestions. I've now gotten my feet wet in unittest and have gone from not quite knowing where to start to making substantial progress, with a small suite of tests up and running. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Getting started in testing
Is anyone aware of any good tutorials on testing one's Python code? These days, I'm a hobby programmer, writing little things just for my own use, and don't really sweat testing much. But I do have one niche open-source project where I need to be a bit more regimented, and specifically need to develop a set of tests to be passed before releasing. Any resources would be helpful. I am aware of the docs on unittest, but I'm looking for a more tutorial approach. I use Python 2.7, and my project is a module with no GUI, if that matters. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sqlite
On Tue, 3 May 2016, Crusier wrote: I am just wondering if there is any good reference which I can learn how to program SQLITE using Python I can not find any book is correlated to Sqlite using Python. "The Definitive Guide to SQLite" is about SQLite, but includes a chapter on both PySQLite and APSW for Python access. One of the book co-authors, Michael Owens, is the original author of PySQLite. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Plotting with python
If you were going to get started doing some simple plotting with Python 2.7 (in my case, I'm simply plotting temperature against time-of-day) what would you use? - matplotlib [1] - gnuplot [2] - something else entirely? Assume no substantial familiarity with the underlying plotting software, let alone the Python bindings. The only thing I can think of that might be special is to specify the upper/lower bounds of the plot; for example, in my case, I know the temperatures vary between somewhere around 70-78 degrees F., so I'd want the Y-axis to go, say 60-90, not arbitrarily start at zero; but I suspect this is a pretty standard thing in almost any plotting package. [1] http://matplotlib.org/api/pyplot_api.html [2] http://gnuplot-py.sourceforge.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Your confirmation is required to leave the Tutor mailing list
On Mon, 10 Feb 2014, tutor-confirm+c3fa710640d780363ebaec9fd955eefa81f1b...@python.org wrote: Mailing list removal confirmation notice for mailing list Tutor We have received a request for the removal of your email address, carr...@tjc.com from the tutor@python.org mailing list. To confirm that you want to be removed from this mailing list, simply reply to this message, keeping the Subject: header intact. Or visit this web page: https://mail.python.org/mailman/confirm/tutor/c3fa710640d780363ebaec9fd955eefa81f1b46c Or include the following line -- and only the following line -- in a message to tutor-requ...@python.org: confirm c3fa710640d780363ebaec9fd955eefa81f1b46c Note that simply sending a `reply' to this message should work from most mail readers, since that usually leaves the Subject: line in the right form (additional Re: text in the Subject: is okay). If you do not wish to be removed from this list, please simply disregard this message. If you think you are being maliciously removed from the list, or have any other questions, send them to tutor-ow...@python.org. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] How can a CGI program get the URL that called it?
How can my Python 2.7 CGI program find the URL that caused the program to be called? I have a program that creates a JPG or PNG file on the fly, and needs to construct a URL to it. I know the path relative to my program is, for example, ../temp/tmpiicack.png (the filename generated by tempfile.NamedTemporaryFile). From this, I want to generate a URL for the image so it can be displayed. I invoke My CGI program (in testing) with the URL http://localhost:8000/cgi-bin/query.py?tmtype=stmnumber=76044902. It is usually invoked using a form at http://localhost:8000/cgi-bin/query.py, which generates the URL, but can also be invoked by directly going to the URL with parameters specified (I want to be able to email a complete URL, for example). In this instance, the URL I want to generate would be http://localhost:8000/temp/tmpiicack.png. The problem is, my program does not know the http://localhost:8000; part. Module urlparse has facilities for generating a URL from relative parts, but requires that I know a base URL to begin with. I've looked in os.environ to see if anything is presented, but the only thing close is os.environ['HTTP_REFERER'], which is only populated if the program is invoked from the form-click, not if directly entered (e.g. copy/paste). (That's my fall-back solution; but it will mean the image-support will fail if a URL is entered directly, and will work only if invoked from a form.) I've also checked os.environ['PATH_INFO'] as suggested in a post on stackoverflow,[1] but that is not populated. (I can't recall whether it's a zero-length string or None, but it has nothing useful). I'm testing using CGIHTTPServer as my server, if it matters. [1] http://stackoverflow.com/questions/4939067/catching-the-url-path-following-a-python-cgi-script/4939137#4939137 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How can a CGI program get the URL that called it?
Ah, I discovered what my problem was... On Fri, 10 Jan 2014, Alan Gauld wrote: its calling your file. You should know where your file is? My problem was that, I know where the file is in the host's file system, and relative to my CGI program. I do not have a URL to that file. If you want to create a png file and display it to the user then you just store the file somewhere in your web site and create an html file that has an img tag referencing that location. Right; I am producing HTML output (using the print command, not as a file), with an img tag. The img tag has a src attribute, which must provide the URL of the referenced image file. But I do not have that URL. I know where that file is in the file system, and relative to my CGI program. But I do not have a URL to the file. My thinking was that, if I have the URL to my program, it's pretty trivial to construct the URL to the file. And here's where my real problem was: I had tried specifying just a relative path in the src tag, and that did not work consistently; specifically, I found that it worked in Chrome, but not Firefox. As it turns out, since I was testing on a Windows box, os.path.relpath was (reasonably) using a '\' as the separator character (surprisingly, so does posixpath.relpath). By simply adding: relative_path = relative_path.replace('\\', '/') It uses the '/' required in a URL (even a relative-path URL) and works. Chrome was forgiving of the '\'; other browsers not so much. It was not until I posted the img tag into a draft of this reply that I noticed the '\' characters. I swear I looked at this for hours without noticing this before. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How can a CGI program get the URL that called it?
On Sat, 11 Jan 2014, Steven D'Aprano wrote: However, if you pass a path using \ to posixpath, it treats them as non-separators: That's apparenbtly what's happening. I didn't investigate much, once I found out that using posixpath didn't address the issue I was having; using replace() was pretty straightforward. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Which computer operating system is best for Python developers?
(Re Python on Windows 7) On Thu, 23 Feb 2012, Tim Golden wrote: On 23/02/2012 09:00, Alan Gauld wrote: If you do a reinstall, download the ActiveState version rather than the Python.org version. I also recommend the ActiveState distro. I am going to third Alan's and Tim's recommendations of the Activestate distribution; and further suggest that you use the 32-bit version, anod not the 64-bit version, even if you have the 64-bit Windows 7. Some Python extensions are built only for 32-bit Python and will not work with 64-bit. I recently ran into this on two modules; one was PIL and I cannot remember the other. The easy fix was to uninstall the 64-bit Python and install 32-bit in its place. I'm referrng to Python 2.7 above. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The Perennial 3.2 vs 2.7
On Thu, 17 Nov 2011, Wayne Werner wrote: On Thu, Nov 17, 2011 at 8:45 PM, Mark Lybrand mlybr...@gmail.com wrote: so, use my 2.7 and not my 3.2 for my study? Or use my 3.2 for study and then do what I have to in 2.7 after including those lines? Honestly it probably doesn't matter. Many 3rd party libraries have now been ported to Python 3.x, so unless you have a particular library you're interested in, I would start with 3.2 until you find something you can't do. Personally, that's the biggy for me, and why I remain on 2.7. wxPython has not yet been ported to Python 3, and that's a substantial big deal for me. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Stacks and Stack underflow/Stack overflow
On Sat, 19 Nov 2011, Joe Batt wrote: Hi All Could some kind soul please explain why you get a stack underflow and a stack overflow. I am getting the following error in Python 3 Traceback (most recent call last): File /Users/joebatt/Desktop/python/pickling puzzle 5.py, line 39, in module a=pickle.load(file) _pickle.UnpicklingError: unpickling stack underflow when I am running the following import pickle file=open('///Users/joebatt/Desktop/banner.p.webarchive','rb') a=pickle.load(file) file.close() print (a) When you created the pickle file, did you create it in binary form (with wb)?___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regexp
On Sat, 5 Nov 2011, Dinara Vakhitova wrote: I need to find the words in a corpus, which letters are in the alphabetical order (almost, my etc.) I started with matching two consecutive letters in a word, which are in the alphabetical order, and tried to use this expression: ([a-z])[\1-z], but it won't work, it's matching any sequence of two letters. I can't figure out why... Evidently I can't refer to a group like this, can I? But how in this case can I achieve what I need? First, I agree with the others that this is a lousy task for regular expressions. It's not the tool I would use. But, I do think it's doable, provided the requirement is not to check with a single regular expression. For simplicity's sake, I'll construe the problem as determining whether a given string consists entirely of lower-case alphabetic characters, arranged in alphabetical order. What I would do is set a variable to the lowest permissible character, i.e., a, and another to the highest permissible character, i.e., z (actually, you could just use a constant, for the highest, but I like the symmetry. Then construct a regex to see if a character is within the lowest-permissible to highest-permissible range. Now, iterate through the string, processing one character at a time. On each iteration: - test if your character meets the regexp; if not, your answer is false; on pass one, this means it's not lower-case alphabetic; on subsequent passes, it means either that, or that it's not in sorted order. - If it passes, update your lowest permissible character with the character you just processed. - regenerate your regexp using the updated lowest permissible character. - iterate. I assumed lower case alphabetic for simplicity, but you could modify this basic approach with mixed case (e.g., first transforming to all-lower-case copy) or other complications. I don't think there's a problem with asking for help with homework on this list; but you should identify it as homework, so the responders know not to just give you a solution to your homework, but instead provide you with hints to help you solve it. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Assigning variables with names set by other variables
On Fri, 4 Nov 2011, Max S. wrote: Is it possible to create a variable with a string held by another variable in Python? For example, It's possible, but in almost all cases where this comes up, the better approach is to use a dictionary.___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Mailing list archive oddity?
On Thu, 29 Sep 2011, Hugo Arts wrote: * someone from the future is in need of python help and is sending messages back in time. I'm betting this is Guido and his time machine again. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] xrange() with start or stop sys.maxint?
Is there any way to use xrange with a start or stop value that exceeds sys.maxint? import sys print sys.maxint 2147483647 start = sys.maxint-1 for i in xrange(start, start+1): ... pass ... start = sys.maxint for i in xrange(start, start+1): ... pass ... Traceback (most recent call last): File stdin, line 1, in module OverflowError: Python int too large to convert to C long Works okay with range, though: start = sys.maxint for i in range(start, start+1): ... pass ... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Overriding a method in a class
On Sat, 14 May 2011, Alan Gauld wrote: Is there any reason you can'tt override in the uisual way by inheritance? Doh! Of course I should. I've written plenty of classes before, but never one intended to be inherited, and now that you point it out, it's obvious that that's what I should be doing here. Thanks. On Sat, 14 May 2011, Peter Otten wrote: If you define a function in the class body and then instantiate that class [snip] You can find a thorough explanation at http://users.rcn.com/python/download/Descriptor.htm Thanks, Peter; that's very helpful. I'll read that page in detail. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Overriding a method in a class
I have a pretty basic point of confusion that I'm hoping I can have explained to me. I have a class in which I want to override a method, and have my method defined externally to the class definition invoked instead. But when I do so, my external method is invoked with a different argument signature than the method it overrides. (I'll illustrate with a toy example named toy.py that maintains a list of strings; the actual use case is a wxPython drag-and-drop shell that I find I keep re-using over and over, so I decided to try to turn it into a general-purpose module for my own use.) ### example 1 begin class Thing(object): def __init__(self): self.stuff = [] def addstuff(self, text): self.add_the_stuff(text) def add_the_stuff(self, s1): self.stuff.append(s1) A = Thing() A.addstuff(ABCDEFG) print A.stuff ### example 1 end So far, this works as expected. addstuff invokes add_the_stuff; and the line print A.stuff prints out as ['ABCDEFG'], as expected. Now, here's where I am getting befuddled, with the following additional lines: ### example, continued def addlower(self, s2): self.stuff.append(s2.lower()) # add it as lower case B = Thing() B.add_the_stuff=addlower B.addstuff(WXYZ) print B.stuff ### end My *intent* here is to patch the Thing object named B so that the B's add_the_stuff method is replaced with this additional addlower method that I define external to the object. My expectation would be that, just as add_the_stuff method was called with two arguments (self and the string), the patched-in addlower would also get called the same way. What I *expect* is to see ['abcdefg'] printed. What I get is: Traceback (most recent call last): File E:\Personal\py\DragDrop\toy.py, line 22, in module B.addstuff(WXYZ) File E:\Personal\py\DragDrop\toy.py, line 7, in addstuff self.add_the_stuff(text) TypeError: addlower() takes exactly 2 arguments (1 given) I'm assuming I'm missing some fundamental concept. What is it? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Overriding a method in a class
On Fri, 13 May 2011, Terry Carroll wrote: What I *expect* is to see ['abcdefg'] printed. What I get is: Sorry, mixed multiple examples; What I had expected was ['wxyz']. Still the same question though. BTW, I forgot to mention: Python 2.7.1, under Windows. (Still not used to the Python 2.x/3.x thing) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Overriding a method in a class
On Fri, 13 May 2011, David Knupp wrote: I think the problem is this bit about overriding an object's internal method with one that is defined externally. Yes, that describes the phenomenon. My gut feeilng is that you'd now have to explicitly pass the object (i.e., self) as well as the string, i.e.: B.addstuff(B, WXYZ) ...which seems clunky. But worse, it would mean that you couldn't have the default un-overridden method for those cases where you don't need to override it. I'm not familiar with this particular technique. Is it common to do something like this? Certainly the problem is common. For my specific case, I'm going to go with a Plan B of using callbacks; and provide default unbound callbacks present in the module, but not defined in the class itself. But I'd still like to have a better understanding of how the call gets transmuted from a two-argument call to a one-argument call based upon the target. I suspect something along these lines is going on: begin uninformed speculation When a bound method is called, Python inserts a reference to self as an additional first argument, before the other arguments. It does not do this with unbound methods. In my first case, when I created object A, the name add_the_stuff references the bound method add_the_stuff as defined in the Thing class. When add_stuff calls add_the_stuff, because add_the_stuff references a bound method, Python does the automagical insertion of the self argument. In my second case, when I created the object B, the name add_the_stuff *initially* references the bound method add_the_stuff; but then, when I override, I make B.add_the_stuff reference the unbound external method. Now, when addstuff calls add_the_stuff, Python sees that it is referencing an unbound method, and does not insert the self reference into the argument list. end uninformed speculation Now I'll wait for one of the experts to edify me. On Fri, 13 May 2011, Terry Carroll wrote: I have a pretty basic point of confusion that I'm hoping I can have explained to me. I have a class in which I want to override a method, and have my method defined externally to the class definition invoked instead. But when I do so, my external method is invoked with a different argument signature than the method it overrides. (I'll illustrate with a toy example named toy.py that maintains a list of strings; the actual use case is a wxPython drag-and-drop shell that I find I keep re-using over and over, so I decided to try to turn it into a general-purpose module for my own use.) ### example 1 begin class Thing(object): def __init__(self): self.stuff = [] def addstuff(self, text): self.add_the_stuff(text) def add_the_stuff(self, s1): self.stuff.append(s1) A = Thing() A.addstuff(ABCDEFG) print A.stuff ### example 1 end So far, this works as expected. addstuff invokes add_the_stuff; and the line print A.stuff prints out as ['ABCDEFG'], as expected. Now, here's where I am getting befuddled, with the following additional lines: ### example, continued def addlower(self, s2): self.stuff.append(s2.lower()) # add it as lower case B = Thing() B.add_the_stuff=addlower B.addstuff(WXYZ) print B.stuff ### end My *intent* here is to patch the Thing object named B so that the B's add_the_stuff method is replaced with this additional addlower method that I define external to the object. My expectation would be that, just as add_the_stuff method was called with two arguments (self and the string), the patched-in addlower would also get called the same way. What I *expect* is to see ['abcdefg'] printed. What I get is: Traceback (most recent call last): File E:\Personal\py\DragDrop\toy.py, line 22, in module B.addstuff(WXYZ) File E:\Personal\py\DragDrop\toy.py, line 7, in addstuff self.add_the_stuff(text) TypeError: addlower() takes exactly 2 arguments (1 given) I'm assuming I'm missing some fundamental concept. What is it? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Overriding a method in a class
On Fri, 13 May 2011, Terry Carroll wrote: For my specific case, I'm going to go with a Plan B of using callbacks; and provide default unbound callbacks present in the module, but not defined in the class itself. Here's a callback-with-default approach, which works: ### Thing.py def add_the_stuff(obj, s1): obj.stuff.append(s1) class Thing(object): def __init__(self, callback=add_the_stuff): self.stuff = [] self.cb=callback def addstuff(self, text): self.cb(self, text) # # toy2.py ### import Thing A = Thing.Thing() A.addstuff(ABCDEFG) print A.stuff def addlower(obj, s2): obj.stuff.append(s2.lower()) B = Thing.Thing(callback=addlower) B.addstuff(WXYZ) print B.stuff Which produces, as expected: toy2.py ['ABCDEFG'] ['wxyz'] But still... But I'd still like to have a better understanding of how the call gets transmuted from a two-argument call to a one-argument call based upon the target. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] module to parse XMLish text?
On Fri, 14 Jan 2011, Stefan Behnel wrote: Terry Carroll, 14.01.2011 03:55: Does anyone know of a module that can parse out text with XML-like tags as in the example below? I emphasize the -like in XML-like. I don't think I can parse this as XML (can I?). Sample text between the dashed lines:: - Blah, blah, blah AAA BING ZEBRA BANG ROOSTER BOOM GARBONZO BEAN BLIPSOMETHING ELSE/BLIP BASHSOMETHING DIFFERENT/BASH /AAA - You can't parse this as XML because it's not XML. The three initial child tags are not properly closed. Yeah, that's what I figured. If the format is really as you describe, i.e. one line per tag, regular expressions will work nicely. Now there's an idea! I hadn't thought of using regexs, probably because I'm terrible at all but the most simple ones. As it happens, I'm only interested in four of the tags' contents, so I could probably manage to write a seried of regexes that even I could maintain, one for each of the pieces of data I want to extract; if I try to write a grand unified regex, I'm bound to shoot myself in the foot. Thanks very much. On Fri, 14 Jan 2011, Karim wrote: from xml.etree.ElementTree import ElementTree I don't think straight XML parsing will work on this, as it's not valid XML; it just looks XML-like enough to cause confusion. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to the terminal?
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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing to the terminal?
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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Which non SQL Database ?
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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question on tkinter event binding
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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pyserial and invalid handle
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. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and Tkinter Programming by Grayson--New Version?
On Thu, 25 Nov 2010, Alan Gauld wrote: Yves Dextraze yd...@videotron.ca wrote Sent from my iPod There is no mention on Amazon of any new editions and they usually announce several months in advance... A pity a new Tkinter book using Tix and ttk instead of PMW would be a really useful resource! Odd -- Yves's note shows up on my system as a reply in a long-dormant thread from March 2009. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] %T as a strftime format identifier
On Sat, 27 Nov 2010, Steven D'Aprano wrote: [st...@sylar ~]$ python2.5 Python 2.5 (r25:51908, Nov 6 2007, 16:54:01) [GCC 4.1.2 20070925 (Red Hat 4.1.2-27)] on linux2 Type help, copyright, credits or license for more information. import time time.strftime(%T) '19:03:16' Interesting. On my Windows systems (Windows 7 and Vista, both Activestate Python 2.6.4.8) I get: import time time.strftime(%T) '' On Linux (Ubuntu 10.04 and 10.10, Python 2.6.5) I get: import time time.strftime(%T) '10:54:54' It may be an Activestate thing, hewing closely to the docs. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] %T as a strftime format identifier
Was %T ever a valid format specifier for time.strftime in Python? I just installed a Python streaming MP3 server called Edna (http://edna.sourceforge.net/). It was an easy install except that I got a ValueError on one line, essentially for: time.strftime(%a, %d %b %Y %T GMT) After a few seconds experimentation, I found that %T is not recognized, but that in ANSI C strftime, it's a shorthand for %H:%M:%S. I changed it to: time.strftime(%a, %d %b %Y %H:%M:%S PST) and it worked fine. My question: was %T ever a valid format specifier in Python? My best guess is that it was when Edna was written (the most current release is from 2006, and the docs say it needs at least Python 1.5.2, which gives you an example of its age). It seems odd that the format identifier would be dropped if it had existed, though; that seems like a needless upward compatibility issue. I got this working, so this is mere curiosity; anyone know? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] wxPython, Tkinter (was: program hangs in while loop using wx.yield
On Tue, 16 Nov 2010, Patty wrote: Hi Terry - I am an alumni of UCSC (University of California, Santa Cruz) and live really close so I can request books throughout the UC system just like you describe and there is no limit - or maybe an extremely high limit - to the number of books I can check out from McHenry Library. It is so worth paying alumni dues! There is no way they would transport books to the public library to make it easier for people though - not with the way the city and the university feel about each other :} You might be surprised. I would't have expected to get a book from San Diego State University sent to the San Jose Public Library, but it did. I just entered teh request, and a few hours later, I had anote saying it was on the way. The system finds the copy and obtains it. It's not on a one-to-one basis, i.e., as if SJPL had an arrangement with SDSU; it's more of the libraries deciding to patricipate in the pool. If I just can't figure out how to do this with Tkinter and the Python Imaging Library, is 'wxPython' the additional software I would want to install and try with? wxPython is an alternative to Tkinter. The advantage of Tkinter is that it comes as part of standard Python. You know that it will be installed on any reasonably current Python installation. If you write a program using Tkinter and send it to me, you can be sure that I can run it as long as I have Python installed (at least as far as the GUI is concerned; other things such as PIL might still be an issue). wxPython is an API over the cross-platform wxWidgets GUI. I think it provides a cleaner and more native look compared to Tkinter. For what my opinion is work (and that's not much -- I'm pretty inexperienced at GUI stuff), I find it at least as easy to use as Tkinter, but I recall a learning curve when I started. I don't use Tkinter any more, preferring wxPython, but opinions will vary. Here's a comparison of the two; it's hosted on a wxPython site, so it's undoubtedly slanted toward wxPython: http://wiki.wxpython.org/Choosing%20wxPython%20over%20Tkinter Another couple: http://www.llaisdy.com/static/tech/python/calc.html http://ojs.pythonpapers.org/index.php/tpp/article/viewArticle/61 However, if you would like an example of using Tkinter with PIL, I would be happy to provide you with a very rough program I wrote several years ago for my own use (when I still used Tkinter). It loads an image that was taken with a digital camera; reads the date the photo was taken from the image's EXIF data; adds a timestamp to the photo, and saves it. It's very rough; I have the habit of writing something only to the point where it's good enough for me, and then stop development on it. But you might find it helpful of a straightforward program that uses both. It's from about 2006 or so, and I am by no means a GUI programming expert and was even less so then, so my techniques may be suspect; but I'd be happy to send it to you for what it's worth. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] program hangs in while loop using wx.yield
On Sun, 14 Nov 2010, Alex Hall wrote: Is there a basic tutorial for this sort of thing? Chapter 3 (Working in an event-driven environment) of the book wxPython in Action is a pretty good tutorial on event-driven GUI programming in wxPython. The book in general is pretty good; I no longer buy many computer books, but this one was worth it. If you don't want to buy it, if you're in the U.S., you can go to http://www.worldcat.org/oclc/67122432 and plug in your zip code to see if a library near you[1] has it. [1] or a library that has inter-library loan arrangements with a library near you. I'm currently reading Essential SQLAlchemy, courtesy of the San Diego State University library. My local library (San Jose Public Library) borrowed it from SDSU and then lent it to me. It's kind of cool that one library will send a book 400 miles to another, just because I'm too cheap to buy a copy and asked for it. Apologies to anyone at SDSU who's learning SQLAlchemy and wondering why they can't find this book in their library. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Stupid bug
This isn't a question, I'm just offering it as a cautionary tale and an opportunity to laugh at my own stupidity. I have a small function to calculate the MD5 checksum for a file. It's nothing fancy: ### import hashlib def md5(filename, bufsize=65536): Compute md5 hash of the named file bufsize is 64K by default m = hashlib.md5() with open(filename,rb) as fd: content = fd.read(bufsize) while content != : m.update(content) content = fd.read(bufsize) return m.hexdigest() ### I've discovered a need to calculate the checksum on the first 10K or so bytes of the file (faster when processing a whole CDROM or DVDROM full of large files; and also allows me to find when one file is a truncated copy of another). This seemed like an easy enough variation, and I came up with something like this: ### def md5_partial(filename, bufsize=65536, numbytes=10240): Compute md5 hash of the first numbytes (10K by default) of named file bufsize is 64K by default m = hashlib.md5() with open(filename,rb) as fd: bytes_left = numbytes bytes_to_read = min(bytes_left, bufsize) content = fd.read(bytes_to_read) bytes_left = bytes_left - bytes_to_read while content != and bytes_left 0: m.update(content) bytes_to_read=min(bytes_left, bufsize) content = fd.read(bytes_to_read) bytes_left = bytes_left - bytes_to_read return m.hexdigest() ### Okay, not elegant, and violates DRY a little bit, but what the heck. I set up a small file (a few hundred bytes) and confirmed that md5 and md5_partial both returned the same value (where the number of bytes I was sampling exceeded the size of the file). Great, working as desired. But then when I tried a larger file, I was still getting the same checksum for both. It was clearly processing the entire file. I started messing with it; putting in counters and print statements, using the Gettysburg Address as sample daya and iterating over 20 bytes at a time, printing out each one, making sure it stopped appropriately. Still no luck. I spent 90 minutes over two sessions when I finally found my error. My invocation of the first checksum was: ### checksumvalue = my.hashing.md5(filename.txt) # (Not an error: I keep my own modules in Lib/site-packages/my/ ) print checksumvalue # # [several lines of code that among other things, define my new # function being tested] # checksumvalue2 = md5_partial(filename.txt, numbytes=200 print checksumvalue Turns out my function was working correctly all along; but with my typo, I was printing out the value from the first checksum each time. Doh! Well, no harm done, other than wasted time, and I did turn up a silly but harmless off-by-one error in the process. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Data Directory under site-packages
On Wed, 10 Nov 2010, Greg Lindstrom wrote: I'm writing my first module that I intend to put under our company's site-packages directory for everyone to use in their programs. The problem I'm having is that I want to place files in a data directory under the module directory (under site-packages) and I don't know how to set the path so I pick up the files. If I use open('./data/myfile') I get the path of the file importing the module (which could be just about anywhere). I've tried various combinations using os.path.abspath() and os.path.dirname() but have the same problem. Is there a way I can use files in the subdirectory (I really do not want dozens more files in the main directory)? I'm not sure I follow. You want to put data, i.e., non-python code, in the import path? That sounds unusual to me. You can find the filename from which a module is imported with the module's __file__ attribute; and then os.path.dirname() can get you the directory. So if you wanted to address a subdirectory named data in the same directory from which you imported a given module, or a file myfile.txt in that subdirectory, that's possible. Using the sqlite module as an example on my system: import sqlite3 sqlite3.__file__ 'C:\\Python26\\lib\\sqlite3\\__init__.pyc' import os os.path.dirname(sqlite3.__file__) 'C:\\Python26\\lib\\sqlite3' os.path.join(os.path.dirname(sqlite3.__file__), data) 'C:\\Python26\\lib\\sqlite3\\data' os.path.join(os.path.dirname(sqlite3.__file__), data, myfile.txt) 'C:\\Python26\\lib\\sqlite3\\data\\myfile.txt' Is this the kind of thing you're thinking of? Again, it's highly unusual to put non-code data in the import path; I've never heard of this being done before, and it makes me shiver in revulsion. I'm not sure I can articulate exactly what bad effects it will have, apart from the inherent messiness of it, but I don't like it. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Too different 2.6 vs 2.7?
On Mon, 8 Nov 2010, Jorge Biquez wrote: Are there really BIG differences between version 2.6 and 2.7? I'm in a similar boat. I use Ubuntu 10.04 aw well as Windows XP, Vista and 7. I keep to similar levels just to avoid confusion and at present run Python 2.6 on all systems. If I had an urgent enough need, I'd go to ActivePython 2.7 on the Windows boxes, but so far, no need other than to have the latest. The only feature I'm pining for at all is the new argparse; but it depends on your needs. Under my ubuntu configuration I can not install version 2.7, searching the why, but the version 2.6 is maintained and installed by the ubuntu software center. It's possible to install 2.7 on Ubuntu, but I'm not piqued enough to do so. If you're interested, a couple posts on Ubuntu Forums describe it: http://.ubuntuforums.org/showthread.php?t=1524491 http://ubuntuforums.org/showthread.php?t=1582739 As a newby , trying to learn all the features of all the libraries. Will I miss TOO much if I stay under version 2.6? Or it will be better to stay under 2.7 (in that case under Windows XP) For myself, I think I'll be fine in 2.6. I usually upgrade when I see a particular feature I want. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Interactive visualization in python
On Sun, 7 Nov 2010, Alan Gauld wrote: Most GUI toolkits have a tree widget like the Wiondows Explorer tree view. The Tkintrer version is included in the Tix module which extends the basic Tkinter widgets. I'm pretty sure wxPython will have one too. I haven't used it, but wxPython's tree widget is wx.TreeCtrl doc: http://wxpython.org/docs/api/wx.TreeCtrl-class.html example: http://wiki.wxpython.org/AnotherTutorial#wx.TreeCtrl ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading the CDROM in Linux
On Fri, 5 Nov 2010, Terry Carroll wrote: Aha, this looks like it will work; I was starting to think along these lines; I was thinking of reading the output of df, but this is cleaner. Just to close this out, here's what's working for me. It will need to be prettied up, and the /media/ parameterized, but it's my proof of concept that lets me know how to solve my problem: # import subprocess, os def getvolid(mountpoint): p = subprocess.Popen([volname, mountpoint], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (response, errmsg) = p.communicate() volid=response.rstrip() errmsg = errmsg.rstrip() if len(response)==0: volid=None return (volid, errmsg) # Test it things_to_try = [/dev/sr0,# VBOXADDITIONS_3.2.6_63112 /dev/cdrom1, # symlink to sr0 /dev/sr1,# MP_04_074 /dev/cdrom, # symlink to sr1 /dev/sr2]# no such mount point, return an error for thing in things_to_try: volid, error = getvolid(thing) print mount point=%s; volid=%s; errormsg=%s % (thing, volid, error) # Try the os.walk: (volid, errmsg) = getvolid(/dev/sr0) for (r, d, f) in os.walk(/media/+volid): print (r, d, f) # ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading the CDROM in Linux
On Sat, 6 Nov 2010, Steven D'Aprano wrote: Anyway, more modern Linux systems automatically mount CDs and DVDs. By convention, /mnt/... is used for manually mounts, and /media/... for automatic mounts of media. I am seeing my volume in /media ; however, I won't know the volume name when my program runs. I can't use whatever shows up in /media, because there could be more than one drive. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reading the CDROM in Linux
On Sat, 6 Nov 2010, Steven D'Aprano wrote: Terry Carroll wrote: I have a program that traverses the directory of a CDROM using os.walk. I do most of my work on Windows, but some on (Ubuntu) Linux, and I'd like this to work in both environments. On Windows, I do something along the lines of this: startpoint=D:/ What if the user has two CD drives? What if they have a second hard disk mounted on D:/, and a network drive on E:/, and use F:/ or A:/ or Z:/ for the CD drive? D:/ doesn't enter into it. That's on Windows, I'm asking about Linux. I used D:/ to show a single example of what works on Windows to explain what I am looking for on Linux. In practice, the drive specification will be coming from a config file. It would be D:? on some systems, E:/ on others or maybe both. But my question is not about Windows, which I already have covered. My question is, to put it succinctly: How can one use os.walk to walk a directory structure of a CDROM on LInux when the volume name is not known? On Unix and Linux systems, there are two conventions for mounting external media. One is that if you, the user, mount something by hand using the mount command, it gets placed in /mnt (old-school Unix sys admins had keyboards without vowels *wink*). Often people would use subdirectories under /mnt: /mnt/cdrom /mnt/floppy are the two most common ones. No such luck: t...@vraspberry:~$ ls -pal /mnt total 8 drwxr-xr-x 2 root root 4096 2010-04-23 03:23 ./ drwxr-xr-x 23 root root 4096 2010-10-04 10:42 ../ t...@vraspberry:~$ ls -pal /mnt/cdrom ls: cannot access /mnt/cdrom: No such file or directory t...@vraspberry:~$ ls -pal /mnt/floppy ls: cannot access /mnt/floppy: No such file or directory The other convention is that modern window/desktop managers like KDE and Gnome will automatically mount devices by name under /media. Yes, I mentioned this, but it requires knowing the volume name. If you only have one CD drive, and no other devices mounted, you can just look at /media and walk over that without caring what the CD drive is called. In other words, just use /media as the starting point, and let os.walk discover the name of the CD under it. But that has the same problem I already mentioned in the prior note: what if there's more than one device? The same thing you pointed out above about D:/ Well that was easy. You need to query the external tool volname, which should be present on just about any Linux system. Use the subprocess module to call volname /dev/cdrom. Aha, this looks like it will work; I was starting to think along these lines; I was thinking of reading the output of df, but this is cleaner. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Optparse question: if only certain values are acceptable
I'm tryng to use optparse for the first time. The toy summary is that I want to have the following command format: prognam -f FORMAT Where FORMAT, if specified, must be one of X, Y, or Z. In otherwords, if the user enters: progname -f X It runs, producing its output in format X. Similar if Y or Z is specified instead of X. But if the user specifies progname -f A I want it to spit up because A is not a recognized format. I don't see anything in the docs that directly addresses this. Is this something that I should use the callback parameter for? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Optparse question: if only certain values are acceptable
On Sat, 9 May 2009, Sander Sweers wrote: Is the below what you are looking for? It's exactly what I was looking for. Thanks very much. Now I'm going to have to re-read the docs and see why I couldn't pick that up from them. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parsing suggestion? (CUE file)
On Tue, 3 Feb 2009, Kent Johnson wrote: Ah, I see. I imagined something more ambitious, that treated the lines as fields. I find that the more ambitious my projects become, the less likely I am to complete them! With two toddlers, on a good day, I get 30 to 60 minutes of discretionary time, some of which I can do some coding! You are using csv to do kind of a smart split() function. That's an excellent summary. I would look at pyparsing for that, and make sure cuetools won't do what you want. csv seems to be working well for me, and it's a standard piece of Python, which I prefer to use. I don't think I'm twisting it out of its intended useh: despite its name, it's really about splitting up uniform delimited input lines. The ability to use other delimeters and to trim the left-padding seems consistent with my use of it. The time it took between thinking of using CSV and getting it to actually work was probably a lot less time than I would have spend downloading and installing pyparsing, and figuring out how it worked. Thanks for the cuetools tip. I hadn't heard of it before. I spent some time looking for tools related to Audacity and CUE files, and couldn't find much (apart from one forum posting pointing out that Audacity didn't support CUE files, but it would be pretty easy to convert a CUE file to an Audacity label file). ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Parsing suggestion? (CUE file)
I am parsing certai ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parsing suggestion? (CUE file)
On Tue, 3 Feb 2009, Terry Carroll wrote: I am parsing certai Sorry about that. I was composing this message last night when my Internet connection went down. When I logged on this morning, I had a partial message. I meant to cancel but unfortunately, in pine, the SEND key (CTRL-X) is adjacent to the CANCEL key (CTRL-C), and I hit the wrong one. The silver cloud to my temporary Internet outage was that I was able to solve my problem, in the process discovering that the csv module can parse a CUE file[1] quite nicely if you set up an appropriate csv.Dialect class. Comma Separated Values; it's not just for commas any more. [1] http://en.wikipedia.org/wiki/Cue_file ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Parsing suggestion? (CUE file)
On Tue, 3 Feb 2009, Kent Johnson wrote: On Tue, Feb 3, 2009 at 2:55 PM, Terry Carroll carr...@tjc.com wrote: The silver cloud to my temporary Internet outage was that I was able to solve my problem, in the process discovering that the csv module can parse a CUE file[1] quite nicely if you set up an appropriate csv.Dialect class. [1] http://en.wikipedia.org/wiki/Cue_file What is the dialect? That sounds like a useful trick. This seems to be working for me, at least with the sample CUE files I've tested with so far: ### import csv class cue(csv.Dialect): Describe the usual properties of CUE files. delimiter = ' ' quotechar = '' doublequote = True skipinitialspace = True lineterminator = '\r\n' quoting = csv.QUOTE_MINIMAL csv.register_dialect(cue, cue) f = open(test.cue, r) reader = csv.reader(f,dialect=cue) for row in reader: print row ### The dialect is the same as the standard excel dialect, which I cribbed out of csv.py, except for delimiter and skipinitialspace. My project is to write a program to convert a CUE file into a list of labels that can be imported into Audacity; and perhaps a file of ID3 info that can be imported into MP3 tagging software. If I had to parse the blank-separated fields of quoted text that included blanks, I don't know how long this would have taken me. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MP3Info class usage
On Thu, 18 Dec 2008, Gareth at Serif wrote: I've not installed it, I've just imported it in my main program. How do you install eyeD3, there's no installation package? I read the readme, which talks about executing 'configure', but that just reports back that it is not recognized as an internal or external command, operable program or batch file. I'm betting that you'e on windows, like me. configure is s shell script. You'll need a version os shell that runs on windows. make, too, I'll bet. I run Cygwin on windows, which is a pretty good thing to have apart from this. It's free and avaliable from http://www.cygwin.com/ By default, I believe make is not installed; you'll have to request it. You'll still get the error that configure is not recognized as a valid command, but invoking it with sh congigure works for me. It would be nice if Eyed3, which is not OS-specific, did not have such an OS-specific install process. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MP3Info class usage
On Fri, 19 Dec 2008, Terry Carroll wrote: configure is s shell script. You'll need a version os shell that runs on windows. make, too, I'll bet. I run Cygwin on windows, which is a pretty good thing to have apart from this. It's free and avaliable from http://www.cygwin.com/ ... It would be nice if Eyed3, which is not OS-specific, did not have such an OS-specific install process. You know, I just reinstalled Eyed3, and compared the installed result to the six files distributed in the zipped tarfile, and they're identical except that one file is named __init__.py.in instead of __init__.py, and has two doc variables defined with templates. If you want to avoid having to install a Unix-like environment, I suggest you just 1) unzip and untar the zipped tarfile, 2) copy the eyeD3 directory from eyeD3-0.6.16/src/ to Python's Lib/site-packages/ directory 3) rename __init__.py.in to __init__.py 4) (probably cosmetic) in __init__.py, edit the lines: eyeD3Version = @PACKAGE_VERSION@; eyeD3Maintainer = @PACKAGE_BUGREPORT@; to: eyeD3Version = 0.6.16; eyeD3Maintainer = Travis Shirk tra...@pobox.com; Someone else will now explain why this is a terrible idea. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] ftplib.storbinary and using a progress bar
On Sat, 6 Sep 2008, johnf wrote: I'm currently using ftplib.storbinary() to upload a file to a FTP server. However, I would like to inform the user of the progress being made during the file transfer (it could be a very long transfer). But ftplib.storbinary() has no callback like retrbinary() so does anyone have a thought on how I can update my user on the progress of the transfer. Callbacks are being added in 2.6, and are in the 2.6 source now. You can download the current version of ftplib at http://svn.python.org/view/python/trunk/Lib/ftplib.py?rev=63788view=log When I do something along these lines, I save it (usually under the local directory of the python program that uses it) under a modified name, i.e. ftplib26.py or myftplib.py; then import with: import ftplib26 as ftplib ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with Factorial algorithm using Python
On Sat, 6 Sep 2008, John Fouhy wrote: 2008/9/5 Terry Carroll [EMAIL PROTECTED]: So here's my routine to address the problem. It consists of making a multiplication table of coefficients that includes the factor such as 5, 25, 125, etc., and their values (1, 6, 31, etc). Then, starting with the highest ones first, successievely determining how many times each factor goes into the number of zeroes. For example, to do the prior example working backwards, what factorial will give you 71 zeroes? I think there is a simpler solution :-) Definitely, but most require calculating the factorial first, and for large factorials, that going to be a huge computational burden. You can count the number of fives in the prime decomposition of a number by just dividing by 5 repeatedly until you don't get a whole number. But that requires having the number first, doesn't it? In other words, don't you have to calculate N! in order to find out how many terminal zeroes N! has? The method I proposed determines the number of terminal zeroes in N! by examination of N, without calculating N!, which is a substantial computational savings when N is large; and in fact, makes the problem tractable for large values of N. Although I am curious to know how long it took your algorithm to find the answer for 7**20 ... Under a second. In a trial, I put print time.asctime() before and after the calculation, and it printed the same time in both print statements. How much under a second, I'm not sure. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with Factorial algorithm using Python
On Fri, 5 Sep 2008, Terry Carroll wrote: On Sat, 6 Sep 2008, John Fouhy wrote: You can count the number of fives in the prime decomposition of a number by just dividing by 5 repeatedly until you don't get a whole number. But that requires having the number first, doesn't it? In other words, don't you have to calculate N! in order to find out how many terminal zeroes N! has? Ah, never mind, I took a closer look at your code. We're on a very similar tracks. But the number of divisions you do scales up substantially for very large numbers. By working with successive powers of 5 instead, you would only need to do log(N,5) divmods; for N=7*20: math.log(7**20,5) 24.181239102443353 Only 25 divmods; then, of course, 25 multiplications to calculate the actual zero count. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with Factorial algorithm using Python
On Thu, 4 Sep 2008, Robert Berman wrote: It can easily be seen that 6! = 720 and has exactly one trailing zero. What is the lowest integer, x, such that x! has 7^20 trailing zeros? It does not, on the surface, appear to be a frontal lobe breaker. Design an algorithm to build factorials; count the number of trailing zeros, and the first time we hit it, we have the lowest integer. To test, I computed factorials for 50,000--12,499 trailing zeros,100,000 trailing zeros--24,999, 150,000 trailing zeros 37,498, and finally 200,000 trailing zeros of 49,998. Obviously, running a test on 100 would take some time and would not even be close to the required number of trailing zeros. At the risk of spoiling with a solution (don't worry, all code is at the end), the trick here is to realize that this is all about counting 5's. you get a zero on the end when a 10 gets into the factorial; and a 10 factors down to 2 x 5. At first glance, you'd have to count both 2s and 5s, but 2s are so plentiful in comparison to 5s, that you really only need to count 5s. So 5! has one 5, at 5. For that matter, so does 6!, 7!, 8! and 9!. All of those will have only one trailing zero. At 10! you have another 5, and now have 2 trailing zeroes; at 15!, you have 3; and at 20! you have 4. 25!, however, gives you 6 zeroes. That's because 25 = 5*5, so it's giving you 2 more fives instead of just one. 30! will get you 7 zeroes, etc. Here's how I started thinking about it: every 5 is worth one five. every 25 is worth 5 fives, plus one more five, for a total of 6 fives; and that score includes all numbers below it. every 125 is worth 5 twenty-fives, plus one more five, for a total of 31 fives. What does this get us? Well, let's say you want to know how many terminal zeroes in 290!. 290 is 2*125 + 1*25 + 3*5. A 125 is worth 31; a 25 is worth 6; and a 5 is worth 1; 2*31 + 1*6 + 3*1 = 71, so there should be 71 zeroes. That turns out to be right: [some code snipped] f = fact(290) print f, num_trailing_zeros(f) 60316116183878209766117976235613285674586630483425933084029202472047670485819885 24516473184964281054847594152584009528621417952272159399012893011898232929436576 79285114407298493208021847117743304953755459586044474858941338130407530467578458 33254416671760299033093062086252031499964109155039154660288221882477747977319966 16183514677158049698159171977934429370558012376711563782210013569092594615762035 60549776040291140480868660361895305429328142835207067479091087183725251309232283 5916639603827091909849433115110366248960 00 71 So here's my routine to address the problem. It consists of making a multiplication table of coefficients that includes the factor such as 5, 25, 125, etc., and their values (1, 6, 31, etc). Then, starting with the highest ones first, successievely determining how many times each factor goes into the number of zeroes. For example, to do the prior example working backwards, what factorial will give you 71 zeroes? Well, 71/31 gives you 2, remainder 9; so we know there are 2 125s; 9/6 gives you 1, remander 3; so we know there is 1 25; 3/1 gives you (duh) 3, so there are 3 5s. so the answer to the question what is the first factorial with 71 trailing zeros? is 2*125 + 1*25 + 3*5 = 290; i.e. 290! Code: def find_lowest_factbase(num_z): ''' This function returns a lowest value N for which N! terminates with num_z zeroes i.e., find_lowest_factbase(2) = 10; because 10! = 3,628,800 ''' coefflist=[] ''' coefflist is a list of lists, each sublist of which has two elements: 0: value of 5**N 1: weight of value ''' for i in range(1,100): #will break out well before a million value = 5**i if i == 1: weight = 1 else: weight = (coefflist[-1][1]*5)+1 if weight num_z: break coefflist.append([value, weight]) coefflist.reverse() productpairs=[] zerosleft=num_z for L in coefflist: (quotient, remainder) = divmod(zerosleft, L[1]) productpairs.append([quotient, L[0]]) zerosleft = remainder product=0 for pair in productpairs: product = product + pair[0]*pair[1] return product I tested it by comparing the output of the two sets, one testing through 45! and one testing through 10 zeros (which works out to be the same): def fact(n): m=1 for i in range(1,n+1): m = m*i return m def num_trailing_zeros(n): s = str(n) s_no_zeros = s.rstrip('0') numz = len(s)-len(s_no_zeros) return numz #brute force: max_nz=0 for i in range(1,50): k = fact(i) nz = num_trailing_zeros(k) if nz max_nz: print nz=%s at %s! (%s) % (nz, i , k) max_nz = nz # fives-algorithm: for i in range(1,11): k = find_lowest_factbase(i) print nz=%s at %s! (%s) % (i, k , fact(k)) And your answer?
Re: [Tutor] Need help with Factorial algorithm using Python
On Thu, 4 Sep 2008, Robert Berman wrote: Time to do some reading about regex. And here I thought I was slick working with lists and strings. You shouldn't need a regexp for this. An easy way to count the trailing zeros is: - convert the number to a string; - make a copy, stripping off the trailing zeroes, using rstrip('0') - compare the lengths of the two strings The difference in the lengths is the number of trailing zeroes. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Decimals 'not equal to themselves' (e.g. 0.2 equals 0.200000001)
On Sun, 3 Aug 2008, CNiall wrote: 0.2 0.20001 0.33 0.33002 As you can see, the last two decimals are very slightly inaccurate. However, it appears that when n in 1/n is a power of two, the decimal does not get 'thrown off'. How might I make Python recognise 0.2 as 0.2 and not 0.20001? It's not a Python thing, it's a computer thing. Thsi would be present regardless of what computer language you're using. An oversimplificationis that computers use binary, i.e., base-2. The only non-1 factor of 2 is 2 itself, and computers can only give you an exact representation of a fraction whose denominator only has factors of 2. So these are exact representations: .5 0.5 .5 # 1/2 0.5 .25# 1/4 0.25 .125 # 1/8 0.125 .875 # 7/8 0.875 But suppose you want 1/10. Well, 10 is factored into 2 and 5, and that pesky 5 makes it impossible for a computer to store exactly; same with 1/5: .1 # 1/10 0.10001 .2 # 1/5 0.20001 Do you remember when you learned decimal fractions in garde school, and realized you couldn't represent, for example, 1/3 or 1/7 exactly? You had to content yourself with 0.333 or 0.14285714285714285... with digits repeating forever? That's the equivalent problem in our usual base-10: we can't exactly represent any fraction unless the denominator factors only into 2s and 5s (which are the factors of 10). So representing 1/2, 1/4, 1/20 all are no problem in decimal; but 1/3 and 1/21 can't be exactly represented. A corallary of this, by the way, is that, because there are so many fractions that can't be exactly represented in binary notation, you should never compare floating point numbers looking for equality. It just won't work. Consider the following code: x = 0.0 while x != 1.0: ... print x ... x = x + 1.0/7.0 You might expect this to look through 0.0, 0.142, 0.285..., 0.428 up to 10., and then stop. But it won't. because it never quite equals 1.0. It goes right up to a number near 1.0, that might display as 1.0, but is not really 1.0, and blasts on through to 1.142... etc, in an endless loop. So when comparing floating point numbers you should either use a comparison like = (if using it as a limit) or a construct like if abs(x-y).1: to see if they're close enough to equal to keep you happy. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] adding a watermark to a sequence of images
On Sun, 20 Jul 2008, Christopher Spears wrote: Has anyone used Python to watermark of sequence of images? There's a recipe for watermarking using PIL here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879 I have a half-baked program that goes through a directory of images and time-stamps them with information from the EXIF data, which I'd be happy to share with you. I started writing it because my wife likes to have our photos timestamped, and I don't, so this was our compromise; but she later came around to my point of view so I never really cleaned it up. It works, but needs some polish. It would be easy to adapt to a fixed watermark. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advice for my function, isPrime(n), please
On Mon, 21 Jul 2008, Daniel Sarmiento wrote: What about the following function? if x == 0: return False return True I don't like it, myself. You have multiple points of exit, and, yes, you can see that the fallthough is only executed if the condition is not met, but it makes you do a double-take when reading it. If you insist on having the conditional, this is clearer: if x == 0: return False else: return True I'd rather have the condition test for truth, though: if x != 0: return True else: return False But that leads to what you don't like anyway, which I think is your best solution: return x != 0 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] adding a watermark to a sequence of images
On Mon, 21 Jul 2008, Christopher Spears wrote: By all means, share your script! Even if I don't use it, I can learn something from it! Well, maybe. If nothing else, perhaps you'll learn some new bad habits. The timestamp on this file shows that I dropped this project in June 2005, and you can see it's unfinished. It still has a few raw edges (including a prompt for a preserve EXIF feature that never got written; and lots of debugging print statements), but it's not bad as a proof-of-concept. The code imports and depends on Gene Cash's EXIF.py module; it now apparently lives at http://sourceforge.net/projects/exif-py/ rather than the URL in the code. But still, if the EXIF module doesn't exist, it just uses the file's timestamp as the date -- not exactly the safest way of figuring the date the photo was taken. At the time I wrote this, I was unaware that PIL had some then-undocumented EXIF support; I probably would have used that to avoid importing a non-standard module. Use this code however you wish, if you find it helpful. Ignore the copyright statement; BaishuSoft was just a little joke between my wife and me. --snip--- import Tkinter as Tk import tkFileDialog import os, sys, time import Image, ImageDraw, ImageFont, ImageTk class MyApp: Begin a Tkinter-based application def __init__(self, root): initializer for Tkinter-based application self.root=root self.root.title(Picture TimeStamper) NoticeFrame = Tk.Frame(self.root) NoticeFrame.pack() headertext = u Baishusoft Picture TimeStamper \U00A9 2005 Baishusoft LLP Tk.Label(NoticeFrame,text=headertext).pack() ButtonFrame = Tk.Frame(self.root) ButtonFrame.pack() SelButton = Tk.Button(ButtonFrame, text=Select Directory, command=self.select) SelButton.pack(side=left) QuitButton = Tk.Button(ButtonFrame, text=Quit, command=self.quit) QuitButton.pack(side=left) OptionsFrame = Tk.Frame(self.root) OptionsFrame.pack() self.EXIFvar = Tk.IntVar() self.EXIFCheckbox = Tk.Checkbutton( OptionsFrame, text=Preserve EXIF (requires JHead), variable = self.EXIFvar) self.EXIFCheckbox.pack(side=top) self.Progressvar = Tk.IntVar() self.ProgressCheckbox = Tk.Checkbutton( OptionsFrame, text=Show progress, variable = self.Progressvar) self.ProgressCheckbox.pack(side=left) self.StatusFrame = Tk.Frame(self.root) self.StatusFrame.pack() self.ImageLabel = Tk.Label(self.StatusFrame) self.ImageLabel.pack() self.FilenameLabel = Tk.Label(self.StatusFrame) self.FilenameLabel.pack() def select(self): dirname = tkFileDialog.askdirectory() if dirname != '': os.path.walk(dirname, self.process_files, None) print PROCESSING COMPLETED. SELECT MORE FILES OR QUIT. else: print NO DIRECTORY SELECTED. return def quit(self): print EXITING. sys.exit() def process_files(self, junk, dirpath, namelist): for filename in namelist: stamped_filename = self.getstampedfilename(filename) if stamped_filename is not None: if os.path.isfile(os.path.join(dirpath,stamped_filename)): print FILE EXISTS, SKIPPING:, stamped_filename else: self.updatestatus(dirpath, filename) datetext = self.timestamp(dirpath, filename, stamped_filename) print FILE IMPRINTED:, stamped_filename, datetext def updatestatus(self,dirpath,filename): im=Image.open(os.path.join(dirpath, filename)) print time.asctime(), thumbnailing..., filename, im.mode, im.size im.thumbnail((100,75)) print time.asctime(), showing..., filename, im.mode, im.size #im.show() self.Tkimage = ImageTk.PhotoImage(im) print created self.ImageLabel.config(image=self.Tkimage) self.ImageLabel.pack() self.FilenameLabel.config(text=filename) self.FilenameLabel.pack() self.StatusFrame.pack() self.root.update() def getstampedfilename(self, filename): fn, ft = os.path.splitext(filename) if ft.upper() in [.JPG, .JPEG] and \ not (fn.upper().endswith(-DATED)): return fn + -dated + ft else: return None def timestamp(self, dirpath, original_filename, new_filename): full_original_filename = os.path.join(dirpath, original_filename) full_new_filename = os.path.join(dirpath, new_filename) font=ImageFont.truetype(Arial.ttf, 40) datetext = GetFileDate(full_original_filename)
Re: [Tutor] Any way of monitoring a python program's memory utilization?
On Thu, 17 Jul 2008, Monika Jisswel wrote: Well, you can check whether your system has reached its limits or not, but what for ? So I can debug the problem. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Any way of monitoring a python program's memory utilization?
On Thu, 17 Jul 2008, Monika Jisswel wrote: I see no problem, if you open very BIG files then your memory will get filled up your system will halt, I'm going to disagree with you on this one. First, in general, it is not the case that opening a very large file will cause memory to be filled up. In general, only the portion of the file that is being processed needs to actually be in memory. A tarfile appears to be an exception to that general rule. The question is, why? Can that exception be avoided and a program that's processing a tar file be as well-behaved in terms of resource consumption as a program that processes other types of files? Second, although most resource constraint problems can often be addressed by buying more resource, that's not usually a good approach. Where, as here, the resource constraint surfaces only in rare cases (i.e., processing a tarfile), the better approach is to find out if something is out of whack with respect to that one case. Simply adding resources is a poor use of, um, resources, for a couple reasons. I'd rather spend my money on a nice dinner than on adding memory to a computer system that is perfectly adequate in every other way, other than in processing a single file. And adding resources, whether memory, disk, or CPU, is a band-aid: it gets you over this hump, but not the next. If I add enough memory to process a 4-Gb file, great, I can now process a 4-Gb file. But if I get a 6-Gb file in a couple months, I have to pull out the checkbook again. But managing the resource utilization is a scalable scheme. can you buy more food than your fridge can handle , and write to a list asking to invistigate the problem ? This is such an off-the wall analogy that it's difficult to respond to, but what the heck. First, I'm not writing to the list to ask it to investigate the problem. I'm writing to the list to find out what tools are available so that *I* can investigate the problem. Second, under this analogy, you're looking at a scenario where food is put into a refrigerator in containers, and when consumed, the containers are left in the refrigerator. Now, your solution here might be to keep buying more or larger refrigerators. Mine would be to see if I can get rid of all the empty containers that are uselessly occupying space in the refrigerator, so I can utilize the space for useful purposes (refrigerating food) rather than chilling empty containers for no reason. Back to the real Python case: now that I can monitor my memory usage, I can try various strategies to solve the problem, and I can do it with a subset of data. Instead of running the program on a 4Gb file and waiting to see if it blows up or halts my system in 15 minutes after processing a couple gig, I can run it with a much smaller 60 Mb file, and track its effects. For anyone who cares about the real issue: it seems that tarfile.py caches every member it processes in an internal list. The list isn't actually used if accessing the file as an iterator, so by reinitializing it to [], the memory consumption problem is avoided. This breaks other methods of the module, which are used to extract particular desired members, but in my case, that's okay. I'm currently experimenting to see if I can come up with a patch that will either allow both means of accessing the members (as an iterator and directly), or, as a less desirable alternative, if a parameter like cache=False is specified, allow access as an iterator and raise an exception if the other methods are used. Thanks to a couple tarfile.py experts on comp.lang.python for their insight on this. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Any way of monitoring a python program's memory utilization?
Is there any way of having my program see how much memory it's using? I'm iterating through a vey large tarfile (uncompressed, it would be about 2.4G, with about 2.5 million files in it) and I can see from some external monitors that its virtual storage usage just grows and grows, until my whole system finally grinds to a halt after about 1.2 million members have been processed. I'd like to try various strategies, but as I try them I'd like for it to monitor its own memory usage so I can assess the different strategies I use, and in any event, abort before it gets to the point of hanging my system. Is there anything within Python (I'm on 2.5) that can check this? Failing that, are there any Windows/XP line-oriented commands that I could invoke and parse the output of? (I also have Cygwin installed, in case there are any gnu-based commands that woudl work, too.) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Any way of monitoring a python program's memory utilization?
On Wed, 16 Jul 2008, Terry Carroll wrote: The obvious thing to do is to also filter by PID, which is the second element; Of course that opens a new question: how to find one's own PID from within Python. More googling awaits. And, while searching for that, I found out hwo to find memory utilization. For those who care: def memusage(): import win32process current_process = win32process.GetCurrentProcess() memory_info = win32process.GetProcessMemoryInfo(current_process) return memory_info[WorkingSetSize] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Anyone using tarfile?
I'm trying to use tarfile with no luck. Anyone on this list used it successfully? Here's a sample program pared down to illustrate the error. I'm arbitrarily trying to extract the 4th TARred file in the tarball (a file that I know from other debugging efforts is data/c410951c, and that I can see by inspection does exist in the tarball). My code (file playtar.py): import tarfile, os TARFILENAME = freedb-update-20080601-20080708.tar.bz2 assert os.path.exists(TARFILENAME) assert tarfile.is_tarfile(TARFILENAME) tf = tarfile.open(TARFILENAME, r:bz2) tf.debug=3 ; tf.errorlevel=2 tmembers = tf.getmembers() sample = tmembers[4] RC = tf.extract(sample) The result: C:\test\freedbplaytar.py Traceback (most recent call last): File C:\test\freedb\playtar.py, line 10, in module RC = tf.extract(sample) File C:\Python25\lib\tarfile.py, line 1495, in extract self._extract_member(tarinfo, os.path.join(path, tarinfo.name)) File C:\Python25\lib\tarfile.py, line 1562, in _extract_member if upperdirs and not os.path.exists(upperdirs): File C:\Python25\lib\ntpath.py, line 255, in exists st = os.stat(path) TypeError: stat() argument 1 must be (encoded string without NULL bytes), not str The file comes from here: http://ftp.freedb.org/pub/freedb/ The bzip2 compression is unrelated to this. If I manually bunzip the .bz2 file to a plain old tar file (and open it with mode r instead of r:bz2), I get an identical error. During some earlier poking around, I see some interesting things: instead of sample.name being data/c410951c (13 characters) or /data/c410951c (14 characters) as I would expect, it's a 169-character string: 11034624707 11032232071 /data/c410951c. I think those are zeroes, not blanks. Curiously, there is also another attribute named prefix that is not documented in the tarfile.py documentation. prefix is a 155-character string that is equal to the first 155 characters of this oddly-too-long name. In fact, if you cut off this prefix from the name, you're left with /data/c410951c, whic his kind of what I was expecting name to be in the first place. The deeper I look into this, the more mystified I become. Any ideas? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Anyone using tarfile?
On Tue, 15 Jul 2008, Kent Johnson wrote: What version of Python are you using? I have 2.5.2 and the line numbers in my tarfile.py are quite different than yours. The changelog for Python 2.5.2 shows many fixes to tarfile so an upgrade may be in order. And that was it! I pulled the most current version from http://svn.python.org/view/python/trunk/Lib/tarfile.py , saved it as tarfilex.py and used import tarfilex as tarfile, and it's now working. You know how sometimes when you're a newbie, every time you get an unexpected result, you jump to there must be a bug in the program I'm using? Well, I've been programming long enough that I tend to assume the opposite: I must be doing something wrong. My version of the source picks up a prefix string and adds it to the start of the path unless the file is a GNU sparse file. I saw that too, but it still struck me as odd. For one thing, I don't understand where sparseness of the tar file (which has to do with whether tar bothers storing all of sparse files -- files that have data space allocated but not initialized) would have anything to do with a file name. That's one of the things that led to my the deeper I look into this, the more mystified I become comment. For another thing, prefix still isn't documented; I'd have expected the variable name to be along the lines of _prefix, or not retained in the class instance if it wasn't intended for consumption. But that might just be my naivete. Anyway, problem solved. Thanks, Kent. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Traversing a two-dimensional array(was: (no subject)
On Wed, 18 Jun 2008, amit sethi wrote: Hi , Could you please tell me , how i can traverse a two dimensional array , i am sorry , it must be pretty simple but I am new to python am not able to understand. What many languages would call a two-dimensional array would be represented in Python as a list of lists; i.e., a list where each item in the list is itself another list. For example, if we have a list like this: list_of_lists = [ ... [1, 2, 3, 4], ... ['A', 'B', 'C', 'D'], ... [I, II, III, IV] ... ] list_of_lists [[1, 2, 3, 4], ['A', 'B', 'C', 'D'], ['I', 'II', 'III', 'IV']] You can traverse it like this: for inner_list in list_of_lists: ... print inner_list is:, inner_list ... for item in inner_list: ... print item:, item ... inner_list is: [1, 2, 3, 4] item: 1 item: 2 item: 3 item: 4 inner_list is: ['A', 'B', 'C', 'D'] item: A item: B item: C item: D inner_list is: ['I', 'II', 'III', 'IV'] item: I item: II item: III item: IV There's no need for each inner list to be of the same length, by the way; I just made it so for this example. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] do I need f.close()
On Wed, 11 Jun 2008, dave selby wrote: The whole topic came up because I just finished reading 'learning python' 3rd edition OReilly as a refresher where there are multiple instances of suggesting that you do the exact opposite eg ... [line.rstrip() for line in open('myfile')] ... p361 for line in open('script1.py') ... p261 p276 where it is described as 'best practice' for reading files line by line etc ... But that's for use of an input file. The question was about writing. I wouldn't worry too much about closing file that was used as input, as I am about one for output, where you want to be careful that the file contents are left in a particular desired state. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to get a script to open a text file with Python?
On Tue, 10 Jun 2008, Alan Gauld wrote: If TextPad is your default txt editor just use os.system(foo.txt) or os.startfile(foo.txt); sounds like the equivalent, but for some reason, I prefer it. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Little problem with math module
Tiago Katcipis [EMAIL PROTECTED] wrote def newton_divergente(x): return math.pow(x, 1.0/3.0) but when x = -20 it returns this error return math.pow(x, 1.0/3.0) ValueError: math domain error but why is that? is it impossible to calculate -20 ^ (1/3) ? Can I chime in late on this one? I'm catching up on some old email. It's already been pointed out that a negative number cannot be raised to a fractional power. But as long as you're only looking to calculate roots you should be able to calculate them unless the number is negative *and* the root is even. For example, you cannot (without getting into complex numbers) take the square root (an even root) of -8. But the cube root (an odd root) of -8 is -2. N**1/x is equal to -(-N**1/x), as long as the constraints above are met. Relying on this, you can easily write an nthroot routine that calculates the non-complex nth root of a number, unless the number is negative and the root-number is even: def nthroot(x, n): returns the non-complex nth root of x if x 0: return x**(1.0/n) elif n%2 == 1: return -(-x)**(1.0/n) else: raise ValueError, even root cannot be calculated for negative number For example: nthroot(100, 2) 10.0 nthroot(8, 3) 2.0 nthroot(-8, 3) -2.0 nthroot(-8, 2) # this is a no-no; negative number even root Traceback (most recent call last): File stdin, line 1, in module File nthroots.py, line 8, in nthroot raise ValueError, even root cannot be calculated for negative number ValueError: even root cannot be calculated for negative number Now, if you *are* willing to get into complex numbers, i.e., numbers with an imaginary component, you should be aware that there's no such thing as *the* Nth root of a number. Instead there are N Nth roots, evenly spaced in a circle centered on 0+0i. For example, the number 16 has two square roots, at 4 and -4; or more completely 4+0i and -4+0i. But it has 4 4th roots. Obviously it has 2 and -2 (2+0i and -2+0i); but it also has 0+2i and 0-2i: (0+2i)*(0+2i)*(0+2i)*(0+2i) = -4 * -4 = 16 (0-2i)*(0-2i)*(0-2i)*(0-2i) = 4 * 4 = 16 The following (barely-tested) routine should calculate all the Nth roots of a given x, even when x is negative and N is even: def nthroots(x, n): returns a list of all nth roots of x from math import pi, cos, sin roots = [] if x =1: startangle = 0 else: startangle = pi/2 firstroot = abs(x)**(1.0/n) for i in range(0, n): angle = (i*(2*pi)/n)+startangle root = complex(firstroot*cos(angle), firstroot*sin(angle)) roots.append(root) return roots Example: nthroots(16,4) [(2+0j), (1.2246063538223773e-016+2j), (-2+2.4492127076447545e-016j), (-3.6738190614671318e-016-2j)] Apart from precision issues, those are 2+0j, 0+2j, -2+0j and 0-2j. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Little problem with math module
On Wed, 21 May 2008, Terry Carroll wrote: The following (barely-tested) routine should calculate all the Nth roots of a given x, even when x is negative and N is even: I realize I'm probably talking to myself here, but for the benefit of the archives, I found a more elegant approach after reading http://everything2.com/node/1309141. The following routine instead uses the formula at the end of the page (although I must admit I needed to read the entire page very carefully before I actually understood it): def nthroots(x,n): returns a list of all nth roots of x; see http://everything2.com/node/1309141 from math import atan2, pi from cmath import exp i = 0+1j z=complex(x) # express the cartesian (x+yi) complex number z # as polar (modulus R argument theta) R = abs(z) theta = atan2(z.imag, z.real) coef = R**(1.0/n) return [coef * exp(i*(theta/n + 2*pi*k/n)) for k in range(0,n)] I'm not sure it's faster than the method from the prior email, but it certainly seems a bit less ham-handed. It could probably be sped up, at the cost of some obfuscation, by pulling a few more things out of the list comprehension, e.g., X = i*theta/n Y = (0+2j)*pi/n return [coeff * exp(X + Y*k) for k in range(0,n)] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] web programming tutorials?
On Fri, 18 Apr 2008, Che M wrote: Thank you, everyone, for all the responses! I will wade through them and try to get some understanding and then will no doubt come back with some questions in some days. Much appreciated. -Che Che, I've done several superficial web programs over the years, and I'm inclined to agree with those who suggest trying some imple CGI programs first to understand how it works under the covers. That being said, for an upcoming personal project, I'm going to be using at least CherryPy and perhaps some other Turbogears components, and perhaps even Turbogears itself. To that end, I've found the Turbogear parts of the book Professional Python Frameworks: Web 2.0 Programming with Django and Turbogears ( http://www.wrox.com/WileyCDA/WroxTitle/productCd-0470138092.html ) to be very helpful in understanding Turbogears. This may just be a point of personal preference, but I found it a better explanation than the Turbogears book rapid Web Applications with Turogears referred to at http://turbogears.org/. You may wish to try a few simple web programs, and then look at whether a framework (whether small like CherryPy or big like Turbogears, Zope or Django) would be appropriate for you. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Diff for Python
On Fri, 4 Apr 2008, Wayne Watson wrote: Is there a Linux diff-like command for Python code? I'd like to see the difference between two py files. You could just use diff. Python itself also has difflib: http://python.org/doc/2.5/lib/module-difflib.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Video file metadata? (MP4/WMV)
On Tue, 4 Mar 2008, Allen Fowler wrote: I can't seem to find a simple description of the MP4 sepc... it might be because there is not one. :) Does this help? http://www.digitalpreservation.gov/formats/fdd/fdd000155.shtml I think, though, that using Python to drive an already-written utility that understands the file format (like ffmpeg, as siggested by Ian) will probably be your best bet. I was going to suggest looking into Pymedia, http://pymedia.org/ , but don't see anything suggesting MP4 support on http://pymedia.org/features.html I don't know if it's being developed any longer either. Its last release was in 2005. http://sourceforge.net/projects/pymedia/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python oddity
On Wed, 27 Feb 2008, Keith Suda-Cederquist wrote: Hi, I'm using iPython and I've run into an occasional problem that I don't understand. Here is what I'm seeing: aa=range(0,10) bb=aa print aa [0,1,2,3,4,5,6,7,8,9] print bb [0,1,2,3,4,5,6,7,8,9] # okay, everything allright at this point bb[5]=0 #change bb print aa [0,1,2,3,4,0,6,7,8,9] #aa has changed!!! print bb [0,1,2,3,4,0,6,7,8,9] So the problem is that when I change bb, aa also changes even though I don't want it to. Is this supposed to happen? If it is can someone explain to me why this is a good thing? and finally, can someone give me some advice on how to avoid or work-around this problem. In addition to Luke's comments, let me try it from a slightly different angle: aa=range(0,10) bb=aa id(aa) 12169048 id(bb) 12169048 The id() function provides the unique ID number of an object. Note that aa and bb have the same ID; they are just two names for the same object. Once you realize that, it will be more clear that a change to aa is also a change to bb. In fact, here's something that really drives home they're the same object: aa is bb True Instead of using the assignment statement, which just puts a new label on an object, you can subject your list to an operation that produces another list that is identical to the list it operates on. One example: cc=list(aa) list() takes something as input and produces list from it. if you hand a list to list() you get a new list (i.e., a new object) that is identical to the list that was input. id(cc) # note: new object id 12185688 aa==cc # note: aa is equal to cc True aa is cc # but they're not the same object False Another approach is to take a list slice that just happens to slice out the entire list: dd=aa[:] id(dd) # note: new object id 12168408 aa==dd # note: aa is equal to dd True aa is dd # but they're not the same object False ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] library to create venn diagrams?
On Tue, 26 Feb 2008, John Fouhy wrote: On 25/02/2008, Danny Navarro [EMAIL PROTECTED] wrote: Hi all, Does anyone know a Python library to generate Venn diagrams with more than 3 datasets? The area of the datasets and the intersections should be proportional to the quantity of data. I don't ... I don't either, but... ... also, how would you draw in two dimensions a Venn diagram of four mutually-intersecting sets? I can't see how it is possible in general.. Although with most Venn diagrams, the enclosing shapes are generally circles, they don't need to be the same shapes, and you can have Venn diagrams with more than three sets. Some examples here: http://en.wikipedia.org/wiki/Venn_diagram Also, a Venn diagram technically is a subset of the more general Euler diagram. The Venn diagram illustrates regions for all possible combinations of intersections, e.g., for three sets ABC, A-only, B-only, C-only, A+B only, A+C only, B+C only and A+B+C. A Euler diagram can be used where, for example, A intersects B but not C. More info at http://en.wikipedia.org/wiki/Euler_diagram I note the External Links section on the Venn diagram lists links to several tools to create Venn diagrams. I don't know if any of them are python, but if the OP is not limited to python-only solutions, they may help. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Change dictionary value depending on a conditional statement.
I don't think I saw anyone point this out yet, but, using list as a variable name is a bad idea, because it hides the list method. x = list(abcdefg) x ['a', 'b', 'c', 'd', 'e', 'f', 'g'] This works. You now have a variable named x that is a list. list = list(hijklmnop) list ['h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'] This works, sort of. You now have a variable named list that is a list. But since you reused the name list, it can no longer point to the list function. y = list(qrstuv) Traceback (most recent call last): File stdin, line 1, in module TypeError: 'list' object is not callable And so this fails. On Tue, 12 Feb 2008, Norman Khine wrote: Thank you all, very nice. Steve Willoughby wrote: Kent Johnson wrote: Try list.append({'id': 'name', 'link': ('YY','XX')[total 0]}) I'd caution against that, though. It's clever and cute, sure, but the meaning of it is obfuscated enough to be unpythonic because [total 0] as a subscript doesn't mean anything unless you know you're taking advantage of an implementation detail that booleans are 0 for false and 1 for true. No matter how reliable that fact may be, I don't think that value should be stuck into a numeric context like that. Or, in Python 2.5, list.append({'id': 'name', 'link': ('XX' if total 0 else 'YY')}) This is much more clear, and would IMHO be fine. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] What web framework?
I'm writing a pretty small database program. It tracks CDROMs with archives of, say, MP3 files; although I'm writing it with an eye to to generalize to, for example, support a collection of image files, or actual audio CDs, as well; or just about any types of files that might lend themselves to being stored on CDROM in a consistently organized manner. I've decided to split out the program(s) to add and delete data from the querying. Data will be added by reading a CDROM and processing all the files; all the data going into the database is derived from the files themselves, so there's no data entry. Similarly, data will only be deleted by deleting all rows that relate back to a particular volume; I'll probably do that with a separate program that does just that function. Neither of these is really worthy of a flashy GUI interface. But querying's different. I'd initially planed on making this a wxPython application, but I think it might be simpler to have it as a web app, even though I'll be the only user, and the db will be resident on the same program I'm querying from. This calls for using a web framework, and I'm casting about for advice on which one to use. This probably isn't the best-formed question, because I don't know what factors should influence a choice of framework. Some factors that come to mind are: - this application will be query-only; no updating, adding or deleting. - the database is SQLite. - security and authentication are not important; there's no private data here, I'm the only user, and I'm firewalled. If a particular framework has security features, I'll use them, but I don't see them as important. (Famous last words.) I'd like something that doesn't take forever to get up to speed on, but that isn't short on features. Although this is the only framework project I see in the immediate future, I can imagine I might want to do another project sometime in the future. I really want to be framework-monogamous here: I don't program for a living, and getting up to speed on a framework seems like work to me. It looks like the leading candidates here are Turbogears and Django. Turbogears looks kind of nice to me, but it looks like it's going through some big changes right now. Apparently, on the database side, SQLObject is being replaced with SQLAlchemy, and on the templating side, Kid is being replaced with Genshi. I worry that a lot of the time put into learning TG in its present form will ultimately be time wasted. Any thoughts on these two frameworks, or another that I might be overlooking? Oh, since I don't know enough about frameworks to even know what factors are worth considering: what factors would I be considering? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Projects
On Wed, 23 Jan 2008, Jason Massey wrote: An example routine to translate a number into [its] english equivalent was given (again, this is Java): static String convertDigitToEnglish(int d) { switch ( d ) { case 1: return one; case 2: return two; case 3: return three; case 4: return four; case 5: return five; case 6: return six; case 7: return seven; case 8: return eight; case 9: return nine; default: return \nFatal Error!\n; // should I abort pgm? } // end of switch } // end of convertDigitToEnglish In Python I'd just use a dictionary. I'm no Java expert, but isn't Java's Map more or less equivalent to Python's dictionary? http://java.sun.com/javase/6/docs/api/java/util/Map.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] data structure question
On Fri, 18 Jan 2008, Alexander wrote: I'm trying to write a small todo list/task manager... Hi, Alexander. Not to derail your actual question, but have you looked at Task Coach? It's a small todo list/task manager, written in Python using wxPython. It does much, perhaps all, of what you're looking for, and it's open source. You might be able to take it and either use it as-is, or modify it to meet your needs. (And donate back the changes, if you feel generous.) http://www.taskcoach.org/ http://sourceforge.net/projects/taskcoach/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 2 problems in a small script
On Fri, 12 Oct 2007, Kent Johnson wrote: Dick Moores wrote: I'm just not comfortable with list comprehensions yet, Hmm, too bad. I use list comps and generator expressions constantly. It took me a while to get list comprehensions. It's one of those things that suddenly snaps into place and you're suddenly comfortable with. What took me the longest time is to realize that it's really a way of generating one list from another list. Once I got that, I started naturally turning to list comprehensions whenever I found myself generating one list from another. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.rename anomaly in Python 2.3 on Windows XP
On Tue, 9 Oct 2007, Tony Cappellini wrote: Unfortunately,os.listdir() returns the same string as glob.glob, for the problem file I mentioned. Tony -- Try specifying the argument to os.listdir as a unicode string. I've found that cures many ailments like this. e.g., instead of something like: filelist = os.listdir('.') use: filelist = os.listdir(u'.') I don't think glob supports this. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] random number generator
On Thu, 4 Oct 2007, Jim Hutchinson wrote: Any idea what I'm doing wrong? while (guess != number): This is your problem. Like all^h^h^h most numbers in computing, floating point numbers are stored in binary. They only approximate the decimal values they print out as. Two numbers can print as the same value but actually have different values. If you want to see if the numbers are identical to 10 decimal places, use this instead: while (abs(guess-number) 0.01): ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] random number generator
On Thu, 4 Oct 2007, Jerry VanBrimmer wrote: I'm no Python wizard, I'm still learning myself. But I think you need another if statement to check if guess is equal to number. if guess == number: print Congratulations! No, he's got the equivalent function in his while statement: while (guess != number): The idea is to stay in the loop as long as they're UNequal, and then drop out to the Congratulations part. But comparing floats as equal or not equal is never a very robust idea. http://www.python.org/doc/faq/general/#why-are-floating-point-calculations-so-inaccurate ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help speeding up algorithm.
Ian, thanks for cleaning this up and submitting it. I was curious what its performance would be. On Wed, 26 Sep 2007, Ian Witham wrote: while count 0: fivecount += count count /= 5 This is a great improvement, and I'm embarrased that I didn't think of it myself! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] detecing palindromic strings
On Fri, 28 Sep 2007, Christopher Spears wrote: I'm trying to write a script that detects if a string is palindromic (same backward as it is forward). This is what I have so far: my_str = raw_input(Enter a string: ) string_list = [] Here you are creating a list and assiging the name string_list to it. for s in my_str: string_list.append(s) Now you've manipulated this list string_list_orig = string_list Here is the problem: you're thinking you're creating another list, named string_list_orig, which is a copy of string_list. you're not. What you're actually doing is assigning another name, string_list_orig, to the existing list that was created above. string_list.reverse() Now you're reversing that single list. print string_list_orig print string_list Now you're printing that list twice, once using each name. The problem is that the script gives results like so: [EMAIL PROTECTED] ./chap6 117 python palindromic.py Enter a string: abc ['c', 'b', 'a'] ['c', 'b', 'a'] Using your approach, the easiest quick fix is to replace the line: string_list_orig = string_list with: string_list_orig = string_list[:] This creates a new list by slicing from string_list, and assigns it a name string_list_orig. Then you're dealing with two different lists, and reversing one does not affect the other. The notation string_list[:] produces a slice of a list. You usually see it with numbers in there, a la: L = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] L[2:5] ['C', 'D', 'E'] This slices from position 2 up to (but not including) position 5. ('A' is at position 0). The default for where to start is 0, so L[:5] is the same as L[0:5] : L[:5] ['A', 'B', 'C', 'D', 'E'] The default for where to end is the end of the list, so L[2:] is the same as L starting at 2 and going to the end of the list: L[2:] ['C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] Putting those two defaults together, leaving both the start and end out means to start at the first element and go all the way to the end: L[:] ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'] So that's what the funky [:] notation gets you. How do I get around this? Is there a better way to write this script? I can't figure out how to loop through a string starting from the last character. You can slice strings the same way you can slice lists. And what's more, you can specify a third parameter on the slice that specifies the stepping of the slicing; the default is 1, but, for example, using 2 will select every other element: S = ABCDEFGHIJ S[::2] 'ACEGI' And, here's the thing: if you use a negative number for ttep, it works from the right side of the string, rather than the left. So, if you use a step of -1 you get: S[::-1] 'JIHGFEDCBA' And that's the easiest way to reverse a string, rather than converting it to a list and reversing the list. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] largest and smallest numbers
On Tue, 25 Sep 2007, John Fouhy wrote: You've got upper and lower bounds - maybe you could do a binary search to find the max exactly? It should only take the same number of steps again... I thought of that; and then I thought I'd rather go home and have dinner. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help speeding up algorithm.
On Tue, 25 Sep 2007, Ian Witham wrote: As I was using a list comprehension I wasn't sure how to make the calculations stop when the result of integer division == 0. I don't see how to do that, either. Someone on this list (sorry, I forget who) once suggested that the list comprehension should support a while predicate, similar to the if filter. Such a predicate would not just filter the generated list, but actually stop its computation. I think that would be a great idea, and this is a good use case why. Absent that capability, your only alternatives that I see are either 1) use the if-filter to filter out the list entries beyond those needed; or 2) force the programmer to work backwards to figure out when that while-condition would be hit, and bake that into something like a range (as you did). The downside of #1 is the computational expense: you generate a lot of potential list enties, just to throw them away. The downside of #2 is that it's programmer-intensive, wasting programmer time, and prone ro errors, as you discovered. Now that you've solved your code, here's the function I came up with. As I said, it resists implimentation as a list comprehension: def numfaczeroes(n): return the count of trailing zeroes from n! e.g., 10! = 3628800; count of trailing zeros = 2 exponent = 1 fivecount = 0 while (n//(5**exponent)) 0: fivecount += n//(5**exponent) exponent += 1 return fivecount But what I like about is is that the only math is integer division and addition (and all the addition is simple incrementation). No use of logarithms, not even any multiplication (which is just perverse, when you think about the fact that it's analyzing factorials, which are pure multiplication). ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help speeding up algorithm.
On Wed, 26 Sep 2007, Ian Witham wrote: My solution still took over 5 seconds on the Sphere Judge machine. How much data are they throwing at you? For the sample data they provide on the website, your first slow solution finished on my machine almost instantaneously. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] quick question
On Mon, 24 Sep 2007, max baseman wrote: for example how hard would it be to write a program that take's a input of a in out table and finds the rule ex: in out 10 23 5 13 1 5 0 3 the rule is in*2+3 This is called linear regression. A search for Python Linear Regression should help you out. Some sample code is at http://www.answermysearches.com/how-to-do-a-simple-linear-regression-in-python/124/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] largest and smallest numbers
On Mon, 24 Sep 2007, Christopher Spears wrote: How can I find the largest float and complex numbers? That's an interesting question.. I just tried this: x = 2.0 while True: x = x*2 print x if repr(x) == 1.#INF: break to just keep doubling X until Python began representing it as infinity. My output: 4.0 8.0 16.0 32.0 64.0 128.0 . . . 137438953472.0 274877906944.0 549755813888.0 1.09951162778e+012 2.1990232e+012 4.3980465111e+012 . . . 2.24711641858e+307 4.49423283716e+307 8.98846567431e+307 1.#INF So I'd say, the answer is somewhere between 8.98846567431e+307 and double that. On complex numbers, I'm not so sure. My math is rusty. Is there a concept of greater than or largest in complex numbers on different axis? Which is larger, 4+2i or 2+4i? complex(4,2) (4+2j) complex(2,4) (2+4j) complex(4,2) complex(2,4) Traceback (most recent call last): File stdin, line 1, in module TypeError: no ordering relation is defined for complex numbers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help speeding up algorithm.
On Tue, 25 Sep 2007, Ian Witham wrote: I am attempting to do this https://www.spoj.pl/problems/FCTRL/ project for the Sphere Online Judge. (Summary of the problem: for a given integer N, determine the number of trailing zeroes in N! For example, for N=10, N! = 3628800, so the number of trailing zeroes is 2.) for d in testnums: maxer = int(d ** .2) + 1 g = sum(d / (5 ** x) for x in xrange(1, maxer)) sys.stdout.write('%d\n' % g) Any ideas on how to speed this up? I think you're on the right track, but it can actually be much less computationally intensive. The track I think you're on is that each trailing zero means that the factorial, if factored out, had both a 2 and 5 among its factors (because 2x5 is 10, and the only way of getting 10). Furthermore, because there are so many 2s, it's really about counting the number of times 5 is multiplied in. For example, in 10!, you have two occurances of 5: at 5 itself, and at 10 (2x5). For 30!, you'd have fives at 5, 10, 15, 20, 25, and 30, which at first glance is 6; but since the 25 is actually 5x5, it counts for two, for a total of 7. And, sure enough 20! is 26525285981219105863630848000, with 7 trailing zeoes. So, an approach might be: 1. Integer-divide N by 5. That gives the number of times it's divisible by 5. 2. Integer-divide N by 5*5, i.e., 25; That gives the number of times it's also divisible by 25. 3. Integer-divide N by 5*5*5, i.e, 125. That gives the number of times it's also divided by 125 4. through whatever: keep doing this. You can stop when the result of the integer division is zero. If you add up all the counts you got in the previous steps, you now have a number that tells you, if you were to have actually calculated N!, and then factored it, the number of times 5 would have been a factor. And since for each one of these there's at least one 2, you happen to also have the count of how many times N! was multipled by 2*5, or 10, which is also the number of trailing zeroes. I just tried this approach out, using the values given at that web page, and it works both quickly and accurately: if __name__ == __main__: sampleinput = [3, 60, 100, 1024, 23456, 8735373] sampleoutput = [0, 14, 24, 253, 5861, 2183837] actualoutput = [] for n in sampleinput: actualoutput.append(numfaczeroes(n)) assert actualoutput == sampleoutput print actualoutput C:\test\FCTRLfctrl.py [0, 14, 24, 253, 5861, 2183837] I'll leave the coding of the numfaczeroes function as an exercise. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding even and odd numbers
On Wed, 19 Sep 2007, Boykie Mackay wrote: if not n1: return false The above should return false for all even numbers,numbers being represented by n.I have tried to wrap my head around the 'not n1' but I'm failing to understand what's going on.Could someone please explain the statement. Others have explained why this works, but let me rail against this code for a moment. IMHO, a bitwise operation is appropriate only when the field being operated on is a set of bits with bitwise information; for example, if the right-most bit was a flag bit of some sort, N1 is a perfectly appropriate operation. But evenness/oddness is a property of numbers, and the code would be much easier to understand if the parameter was treated as a number, rather than as a field of bits, i.e.: def isodd(n): ... return bool(n%2) ... isodd(4) False isodd(7) True Mind you, I'll bet that the bit-checking method is slightly faster than the modulus operation in my little piece of code above. I'll also bet that, if you added up the amount of time that was saved by using the bitwise approach over the modulus approach, over all executions of the program everywhere, it probably would be a smaller amount of time than it would take for a programmmer maintaining the program to find out what that line of code is supposed to do. /soapbox ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] referencing vars()
On Sat, 15 Sep 2007, John wrote: I'm trying to do the above, but of course get an error because vardict is only referencing vars(), thus changes size... also, I tried vardict=[vars()], but this fails as well?? How about import copy vardict=copy.copy(vars()) That way, you've made a copy of vars, and any changes that happen in the meantime don't matter. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] evaluating AND
On Fri, 14 Sep 2007, Rikard Bosnjakovic wrote: For me, if x would be enough. If you think it's a bad thing when x is of the wrong data, then you really should check that it contains *correct* data as well. That's an okay approach, but, but it's also non-Pythoninc; more of the look-before-you-leap approach rather than ask-forgiveness-not-permission. Using the the two function of yours, setting x to an integer: x = 2 print test01(x) Traceback (most recent call last): File stdin, line 1, in ? File /usr/tmp/python-3716vZq, line 3, in test01 TypeError: unsubscriptable object print test02(x) Traceback (most recent call last): File stdin, line 1, in ? File /usr/tmp/python-3716vZq, line 8, in test02 TypeError: unsubscriptable object which is exactly what I would want it to do: raise an exception on bad data. Rewriting your test01-function into this: def test01(x): if (type(x) == type((0,)) and (x is not None) and (length(x) == 2)): if x[0]0: return x[1]/x[0] and testing again: x = 2 print test01(x) None This silently produces an incorrect result, which is a Bad Thing; and it took a lot more code to do it, too. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Just bought Python in a Nutshell
On Thu, 13 Sep 2007, Lamonte Harris wrote: http://www.powells.com/biblio/63-9780596001889-7 Used, has anyone read this book. Any additional information that you like,dislike about this book? I love it, and use it more than any other Python book. Much as I hate to be the bearer of bad news here, though, the URL you give above is for the first edition, published four years ago. Alex put out a second edition just last year. The edition you bought covers through Python 2.2 or 2.3. The current edition covers through 2.5. That being said... it's still very useful, and while I've thought about buying a second edition, I still find the first edition to be extremely useful. I'd suggest reviewing the Python release notes for 2.4 and 2.5 to see what's changed since then. I've bought a *lot* of slightly-out-of-date computer books used and cheap, and you can still learn an awful lot from them. [I like having real books and stead of ebooks because its better on the eyes.] I'm with you. I like to do my reading laying on my bed before going to sleep; and even when using a book as a reference while coding, I like to look from my editor and leaf back and forth in the book; much more effective, to me, than screen swapping. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] remove blank list items
On Fri, 14 Sep 2007, Michael Langford wrote: What you want is a set, not a list. Lucky for you, a python dict uses a set for its keys, so cheat and use that. Is that true? d={1:a, 2:b} k=d.keys() type(k) type 'list' ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] evaluating AND
On Thu, 13 Sep 2007, Orest Kozyar wrote: Given a variable x that can either be None or a tuple of two floats [i.e. (0.32, 4.2)], which syntax is considered most appropriate under Python coding standards? if x and x[0] 0: pass =OR= if x: if x[0] 0: pass I would like either one if instead of if x you used if x is not None; that seems a lot easier to me to read. It's a bit jarring to see the same variable used in one expression as both a boolean and a list/tuple. Besides, suppose somehow x got set to zero. It would pass without error, something you wouldn't want to have happen. Even if you've set things up so that it couldn't happen, it's not obvious from looking at this code that it couldn't happen. If you really want to test for x being non-None, test for x being non-None. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] evaluating AND
On Thu, 13 Sep 2007, Adam Bark wrote: The problem is what if it's an empty list or tuple? It would pass but have not value whereas if x would work fine. Exactly. The poster stated that x is supposed to be either None or a tuple of two floats. Just to put a bit of meat on the example, let's create a function whose job is to return x[1]/x[0], but only if x[0] 0. Otherwise, it just falls off, i.e., returning None. Here are two versions, one using if x is None; the other using just if x def test01(x): ... if x is not None: ... if x[0]0: ...return x[1]/x[0] ... def test02(x): ... if x: ... if x[0]0: ...return x[1]/x[0] When x is None, both work: x = None print test01(x) None print test02(x) None When x is, in fact, a tuple of two floats, both work: x = (2.0, 5.0) print test01(x) 2.5 print test02(x) 2.5 Now... if x is an empty tuple: x = tuple() print test01(x) Traceback (most recent call last): File stdin, line 1, in module File stdin, line 3, in test01 IndexError: tuple index out of range print test02(x) None The first one, which checks if x is None fails. This is a good thing. The second one, which just checks if x and is satisfied with any false value, including an empty tuple, does not raise the error condition, even though the data is bad. This is a bad thing. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor