[program-l] Python: Overriding Functions In The cmd Module

  • From: "Homme, James" <james.homme@xxxxxxxxxxxx>
  • To: "program-l@xxxxxxxxxxxxx" <program-l@xxxxxxxxxxxxx>
  • Date: Tue, 9 Oct 2012 15:32:17 +0000

Hi,
This may not be strictly a Python issue. It may actually be a general 
programming issue. I'm asking anyway, because I'm sneaking up on learning OO 
again. I have two questions. When I try to override functions, I don't always 
know what I'm doing wrong when I read the trace back messages. Is the bottom 
thing in the trace what went wrong in my module? When do I return something 
from my class, and when do I write a return statement that calls a function 
from the class I'm using?

This is going to get long. I want to make sure that I understand what I'm doing 
first.

I'm going to put my code in here piece by piece with comments before each line. 
Note that the e-mail probably messed up the indentation.

# ChessMoves.py
# Make moves in a chess game.

# Bring in the cmd module
import cmd

# Use the cmd class from the cmd module.
class ChessMoves(cmd.Cmd):

# Doc string for this class.

                """Make moves in a chess game."""

# Override variables from the cmd class.

                Self.prompt = '>'
                Self.ruler = '-'
                self.intro = """Welcome to chess.
Type the word help or ? to get help.
Type help <command> to get help on that command.
Type commands at the prompt.
Some commands have more than one word.
For example: move e2e4.
"""
                self.doc_header = "Commands are:\n"

# Create a move command.
# Later, we will actually put some meat in here, but let's just make it
# so that we know the functions actually get called.
# the cmd class uses the doc strings by default for its help function.
# This function and others take the line of input as an argument to work on.

                def do_move(self, line):
                                """Enter a move in the form e2e4 with no 
spaces."""
                                print "You moved " + line

# Add a help function that is friendlier than the default one.
# If we left out this function, the cmd.help function would use the doc string 
from the above function.

                def help_move(self):
                                """Help for the move method."""
                                print "Type the word move, then a space, then 
the from square and to square."
                                print "Example: move e2e4."

# Override the cmd emptyline function.
# It would normally trigger the previous command again.
# We don't want that, especially  if we are really playing the game.

                def emptyline(self):
                                """Do nothing when just Enter is pressed."""
                                pass

                def default(self, line):
                                """Overrides the default command when the 
command is invalid."""
                                print "Type help to see commands."

                def do_EOF(self, line):
                                """Process end of commands"""
                                return True


                def postloop(self):
                """Print a blank line to separate this output from the next 
output."""
                                print

# --------------------
# Main part of ourmodule.
if __name__ == '__main__':
                ChessMoves().cmdloop()







________________________________

This e-mail and any attachments to it are confidential and are intended solely 
for use of the individual or entity to whom they are addressed. If you have 
received this e-mail in error, please notify the sender immediately and then 
delete it. If you are not the intended recipient, you must not keep, use, 
disclose, copy or distribute this e-mail without the author's prior permission. 
The views expressed in this e-mail message do not necessarily represent the 
views of Highmark Inc., its subsidiaries, or affiliates.

Other related posts: