[program-l] Re: 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 17:29:14 +0000

Hi Richard,
Thanks for this answer. I think that what may be happening is that sometimes I 
don't put self as the first thing in the parentheses. For example, one time I 
tried to override the generalized help function, and when I ran my code and 
typed help, the help function that comes with the interpreter ran instead. I 
feel like my dad, who hated to read instructions, then put toys together and 
found missing parts.

Jim


From: program-l-bounce@xxxxxxxxxxxxx [mailto:program-l-bounce@xxxxxxxxxxxxx] On 
Behalf Of R Dinger
Sent: Tuesday, October 09, 2012 1:10 PM
To: program-l@xxxxxxxxxxxxx
Subject: [program-l] Re: Python: Overriding Functions In The cmd Module

Hi Jim,

After a quick pass through your code, this looks like it should work.  What 
happens?

To override a method of a base class, include a new version of the method in 
your sub class.  It will not call the base class method.  If you also want the 
base class method called, you must call it yourself from the sub class method.

The last item in the trace  is the deepest in the call stack and usually where 
the error is to be found.  Although missing quotes, parens etc are often on a 
previous line.

I am not clear on your return question.


----- Original Message -----
From: Homme, James<mailto:james.homme@xxxxxxxxxxxx>
To: program-l@xxxxxxxxxxxxx<mailto:program-l@xxxxxxxxxxxxx>
Sent: Tuesday, October 09, 2012 8:32 AM
Subject: [program-l] Python: Overriding Functions In The cmd Module

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: