Archive for February, 2006

Python – A Better os.system with Time Out

Thursday, February 16th, 2006

(Disclaimer: Only better for me, only for win32)

Here’s the Python function I now use in Windows instead of os.system(inspired by David Stubbs):

import time
import win32process

PROCESS_STILL_RUNNING=259

def system(command_string,seconds_until_time_out):
    """Works like os.system but has timeout and
    only for win32"""
    pinfo = win32process.CreateProcess(
        None, command_string, None, None, 0, 0,
        None, None, win32process.STARTUPINFO())
    retval = PROCESS_STILL_RUNNING
    while retval == PROCESS_STILL_RUNNING:
        time.sleep(1)
        seconds_until_time_out = seconds_until_time_out - 1
        if seconds_until_time_out <= 0:
            win32process.TerminateProcess(pinfo[0], 1)
            print "ERROR: Process timeout"
            break
        retval = win32process.GetExitCodeProcess(pinfo[0])
    return retval

You'll need the Win32 Extensions for Python to run it.

Basically what is does is use the Windows process handling abilities of the Python Win32 Extensions to launch your command, monitor the status of that process, and terminate it if need be.

Please post questions and improvements here.

Here are some searches I made before finding/writing this.

  1. python Exception after timeout
  2. python os.system hangs
  3. python os.system bug

Fixing My Error in win32process.CreateProcess

Thursday, February 16th, 2006

I got this error:
pywintypes.error: (193, 'CreateProcess', '%1 is not a valid Win32 application.')

when running this command:
pinfo = win32process.CreateProcess(
None, command_string, None, None, 0, 0,
None, None, win32process.STARTUPINFO())

The solution is that when using win32process.CreateProcess you can’t simply call a file name and expect Windows to handle it as an os.system call works. Instead you must supply the application executable and its argument file path.

Thus as an example:

For me this didn’t work:
command_string="C:\Documents and Settings\gpinero\Desktop\ScreenShot_2006_02_1415_31_37.jpg"'

And this did work:
command_string=r'"C:\WINDOWS\system32\mspaint.exe" "C:\Documents and Settings\gpinero\Desktop\ScreenShot_2006_02_1415_31_37.jpg"'

[tags]pythonwin, win32 error, process[/tags]

Gnuplot – Getting a Label at Each Data Point + General Help

Monday, February 13th, 2006

Gnuplot is a very impressive graphing/plotting program I just learned about. Today I was confused for a while on how to label each data point but I found my answer here:

How can I print values at each datapoint?

The quick answer is that you just tell Gnuplot:

gnuplot> set label "(0.4,80)" at 0.4,80
gnuplot> set label "(3.0,70)" at 3.0,70

… and so on for each data point you want to label.

Note: this also works for 3 dimensional or surface plots:

gnuplot> set label "(1,1,1)" at 1,1,1
gnuplot> set label "(0,1,2)" at 0,1,2

And here are some general Gnuplot links (mostly for my own reference):

Gnuplot.py – A Python library for Gnuplot
All kinds of Gnuplot tips
A helpful looking page
Lengthy tutorial

Getting Started with the FedEx API – Part 2

Monday, February 13th, 2006

Here you will learn how to write a working program to utilize part of the FedEx API. Although the program we will write looks short, it represents hours of trial and error, 5 support requests, and a lively discussion on the Python mailing list! So make sure you savor this advice thouroughly ;-)

(more…)

When Will The Legend of Zelda: Twilight Princess Be Released?

Thursday, February 9th, 2006

Update: Slashdot and this place say that it will be delayed until “fall”, 2006.

April 15th, 2006 says this GameSpy page.

Seems like a silly tidbit of info to post here, but search answers were not 100% forthcoming for me.