[Tutor] Help with signals, please?

Sheila King sheila@thinkspot.net
Sun, 13 May 2001 22:47:21 -0700


OK, I'm not sure why this isn't working. I've copied exactly from the example in
the 2.0 documentation, here:
http://www.python.org/doc/2.0/lib/Signal_Example.html

At the top of my script I have:

import signal

then, before the main script, I have:

def timesUp(signum, frame):
    print
    print "Time's Up!"


And then, inside the main program, I have:

   signal.signal(signal.SIGALRM, timesUp)
    print "The two minute time begins NOW!\n"
    signal.alarm(2*60)
    testGame.displayBoard()
    while (1):
        submittedwordlist.append(raw_input("Enter words:\n"))
    signal.alarm(0)
 

It won't run. It gives the following error message:

Traceback (most recent call last):
  File "E:\Python\Python20\Pythonwin\pywin\framework\scriptutils.py", line 301,
in RunScript
    exec codeObject in __main__.__dict__
  File "E:\Python\Python20\testprograms\Boggle.py", line 64, in ?
    signal.signal(signal.SIGALRM, timesUp)
AttributeError: SIGALRM

There is even an example like this in Mark Lutz' _Programming Python_ on p. 124.

I'm stumped.

Just in case, I've pasted the full code below. But it was running fine, before I
tried to work with the signals.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/


# So far, this game rolls the sixteen dice, and places them in
# random locations in the game board, and displays the game board.
# it times for two minutes of display, and then prints "GAME OVER"
# suggested extensions:
#   1. Add scoring, so that the player gets a score
#   2. Using a dictionary file, check the submitted words against
#        the dictionary. Only permit words in the dictionary to
#        count. Allow the user the option to add words to the dictionary.
#   3. Have the computer check the board, finding ALL words that are
#        in the table and in the dictionary. Have the computer display
#        this list after giving the player their score. Total the
#        the computer's score, also.

import time, random, signal

diceList = [ ('A', 'A', 'C', 'O', 'T', 'I'), \
            ('M', 'A', 'S', 'O', 'H', 'R'), \
            ('Y', 'H', 'I', 'F', 'E', 'E'), \
            ('V', 'A', 'N', 'D', 'E', 'Z'), \
            ('A', 'Y', 'B', 'T', 'L', 'I'), \
            ('B', 'I', 'R', 'O', 'F', 'X'), \
            ('A', 'P', 'D', 'C', 'M', 'E'), \
            ('P', 'I', 'S', 'E', 'H', 'N'), \
            ('R', 'U', 'W', 'I', 'L', 'G'), \
            ('U', 'K', 'G', 'E', 'L', 'Y'), \
            ('I', 'V', 'G', 'N', 'T', 'E'), \
            ('D', 'E', 'W', 'O', 'N', 'S'), \
            ('C', 'L', 'A', 'R', 'E', 'S'), \
            ('L', 'E', 'T', 'S', 'U', 'P'), \
            ('J', 'O', 'M', 'A', 'B', 'QU'), \
            ('D', 'T', 'O', 'K', 'N', 'U')]

class BoggleGame:

    def __init__(self):
        self.board = {}
        for row in range(1,5):
            for col in range(1,5):
                self.board[(row,col)]= None
        for loc in range(0, 16):
            while (1):
                row = random.randrange(1, 5)
                col = random.randrange(1, 5)
                if self.board[(row, col)] == None:
                    break
            letter = random.choice(diceList[loc])
            self.board[(row, col)] = letter

    def displayBoard(self):
        print # start board display on a newline
        for row in range(1,5):
            for col in range(1,5):
                print self.board[(row,col)], " ",  # extra ending comma
                                                   # prevents newline
            print # empty line print

def timesUp(signum, frame):
    print
    print "Time's Up!"

if __name__ == '__main__':
    testGame = BoggleGame()
    submittedwordlist = []
    signal.signal(signal.SIGALRM, timesUp)
    print "The two minute time begins NOW!\n"
    signal.alarm(2*60)
    testGame.displayBoard()
    while (1):
        submittedwordlist.append(raw_input("Enter words:\n"))
    signal.alarm(0)
    print "Here are the words you entered:\n"
    for word in submittedwordlist:
        print word


--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/