On 16 Mai 2005, [EMAIL PROTECTED] wrote: > Thanks to all who helped me with my questions regarding testing for > commandline arguments and list assignment. I have finished my first > Python program (included below). It is slightly more secure than the > Perl program I rewrote, but also about a tenth of a second slower (0.6 > seconds for Perl on average (100 trials) and 0.7 seconds for Python).
0.1 secs isn't much. It's not easy to see if such small differences are according to the program or some side-effects from the OS. Also the time till the interpreter/compiler starts will be significant if the total time is so short. > Is that typical of Python programs? I like Python so far, and I'm not > going to go crazy optimizing working code, but I am curious. It depends. > Any pointers, suggestions, etc. are welcome. I'll write some annotations how you *could* perhaps speed things a _little_ bit up. > One last thing - is there an equivalent of the "use strict" and "use > warnings" pragmas in Python? Thanks. No. There is no need for them IMO since Python doesn't allow such unsafe constructs as Perl a priori. > def changebackdrop(): [...] > command = "/usr/bin/Esetroot" > # If I was logging into X remotely, this would change. > commandargs = " -display :0.0 " I would write these tow as formal parameters (with default values) since they can change. > # This is where my backdrops live > picdir = "/home/willyyam/misc/bmps/" > > if sys.argv[1:]: > doit = command + commandargs + sys.argv[1] > os.popen(doit, 'r') Why you create the variable doit? Just write directly: os.popen(command + commandargs + sys.argv[1] > else: > files = os.listdir(picdir) No need for that variable. > os.chdir(picdir) > pics = [] No need for that variable. > for file in files: > # This is a test for valid images - it includes rgb files, > # which are not supported by my image software, but the > # error thrown is not terrible - the image software knows=20 > # what it can and cannot run. > if imghdr.what(file): > pics.append(file) > No need for that loop. > randpic = random.choice(pics) randpic = random.choice(filter(imghdr.what, os.listdir(picdir))) That's IMO easier to read and should be a bit faster. One problem I forgot to mention with that solution is: if there are not only files but also directories in that picdir you first have to filter the files since imghdr.what throws an exception if it gets a directory as argument. > doit = command + commandargs + picdir + randpic > os.popen(doit, 'r') The same as above. Or you create the variable `doit' but you write the `os.popen' only once (since after the if statement it will hold the right value for the command to execute. Karl -- Please do *not* send copies of replies to me. I read the list _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor