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).

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.

Any pointers, suggestions, etc. are welcome.

One last thing - is there an equivalent of the "use strict" and "use
warnings" pragmas in Python?  Thanks.
-- 

yours,

William



#!/usr/bin/python

import os, sys, random, imghdr

# This is a little program I call via cron to change my desktop every
# few minutes.  With no arguments it goes to my directory of backdrop
# images and picks a valid image at random.  If I specify a path and a
# file the program will put it up as the display.

# I don't want to fill up my inbox with emails from cron telling me that
# X isn't running, so I check first.
xisrunning = os.popen("pidof /usr/bin/X11/X").read()

def changebackdrop():
    # The below command works for transparent Eterm or Urxvt terminals,
    # populating their backgrounds with the image they occlude.  xli or
    # xsetroot can be called, but they don't work as desired for
    # transparent terminals.
    command = "/usr/bin/Esetroot"
    # If I was logging into X remotely, this would change.
    commandargs = " -display :0.0 "

    # 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')
    else:
        files = os.listdir(picdir)
        os.chdir(picdir)
        pics = []
        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 
            # what it can and cannot run.
            if imghdr.what(file):
                pics.append(file)
                
        randpic = random.choice(pics)
        doit = command + commandargs + picdir + randpic
        os.popen(doit, 'r')

if xisrunning:
    changebackdrop()
else:
    exit

Attachment: signature.asc
Description: Digital signature

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to