[program-l] Re: Python, Passing a parameter as int instead of STR when using the CMD modual?

  • From: "Homme, James" <james.homme@xxxxxxxxxxxx>
  • To: "program-l@xxxxxxxxxxxxx" <program-l@xxxxxxxxxxxxx>
  • Date: Wed, 24 Oct 2012 12:02:39 +0000

Hi Guys,
You have two chances, the way cmd is coded, to get at the command. Right before 
it gets passed to your code, and right after. Check out what I pasted from 
cmd.py below.

    def precmd(self, line):
        """Hook method executed just before the command line is
        interpreted, but after the input prompt is generated and issued.

        """
    def postcmd(self, stop, line):
        """Hook method executed just after a command dispatch is finished."""

I think I may be kind of catching onto this class stuff. Here's what I did. I 
made a Python file that really doesn't do anything yet except print out 
messages that its functions are running. I wrote that file, because I don't 
want to learn GUI right now, while I have so many other things to learn. The 
top of that file looks like this. The comments are to remind me about what's 
going on. You normally wouldn't see them in a real program. It's just for me to 
feel better about myself learning this.

# Document this module.
"""The command line interface to chess."""

# This means that we now can use all the functions in the cmd module.
import cmd

# Make our own class.
# If we make functions that have the same names as the ones in cmd,
# Python uses ours instead.

class ChessCommands(cmd.Cmd):

Once I get done with this file, instead of printing out that its functions 
work, it's going to import another file that does all the work. This files 
functions will just return the results of the function calls in the other file. 
That means that when I'm ready to make a GUI instead of a command line 
interface, I can just work on my GUI file and know that my program can still 
function. I can just swap this file out and use my GUI file. Right now I have 
an empty file called chessboard.py. It's going to do things like validate 
moves. Right now, I'm going to force the user to type a command line that 
includes the word move followed by starting square and ending square with no 
spaces inbetween. Perhaps later on, I'll do things like validate lines of 
standard algebraic notation, but I think that my program would need to 
interpret that and turn it into start square and end square anyway. So when the 
chessboard file knows that it has a valid move, it's going to add to the 
mechanism that stores the moves of the game and move the piece. I'll gradually 
work on my move validation code so that it understands the real rules for 
making moves. I know I could just use the chess engine protocol and hook my 
program up to a chess engine, and maybe I'll play with that some time, but I 
thought that playing with it this way would force me to use more programming 
features, and keep me interested in what I'm doing.

I digress.

If I want to work on my command line module all by itself, I can do that, 
because I have code like this at the bottom:

# If we are running this file as if it were a program ...
if __name__ == '__main__':
        ChessCommands().cmdloop()

Now, here is my final file. All it does is bring in my command line module. I 
have another file, called chessboard.py, which will actually do all the work. I 
talked about it above. Here's the code in the third file.

Well actually, I'm not completely sure how I'll divide all of the work. For 
instance, Maybe I'll use yet another file to write my data to disk, but I 
haven't thought that far yet. I'm just biting off little bits of learning and 
keeping anxiety at bay.

Here we go.

"""Chess playing program
        Command line interface.
        Import the command line interface.
        Call the command loop.
"""

# Import the command line interface and start the game loop.

import chesscommands

# Instantiate the chess commands class.
c = chesscommands.ChessCommands()
# Call the command loop.
c.cmdloop()

Jim

-----Original Message-----
From: program-l-bounce@xxxxxxxxxxxxx [mailto:program-l-bounce@xxxxxxxxxxxxx] On 
Behalf Of R Dinger
Sent: Tuesday, October 23, 2012 10:00 PM
To: program-l@xxxxxxxxxxxxx
Subject: [program-l] Re: Python, Passing a parameter as int instead of STR when 
using the CMD modual?

Hi Al,

I would have to experiment, but you may be able to convert the command tail 
string to numbers in the parseline function or as Jim noted in precmd.  I'm not 
sure how they would automaticly pass to your function though.

An alternative idea is to have a standard helper routine that your function 
calls to convert its tail string in the function.  This could be keyed to the 
function name and have a small footprint.  I can give you a short example if 
you want.

In either case don't you have to convert to int or float or something else 
depending on the parameter?

Richard
----- Original Message -----
From: "Al Puzzuoli" <alpuzz@xxxxxxxxx>
To: "Program L" <program-l@xxxxxxxxxxxxx>
Sent: Tuesday, October 23, 2012 3:02 PM
Subject: [program-l] Python, Passing a parameter as int instead of STR when 
using the CMD modual?


I've begun playing with Python's CMD module, and I  now have a functioning 
command line interface to my program. I was initially perplexed though as I was 
seeing odd errors when executing many of my methods. Long story short, I 
realized that this was due to the fact that CMD passes parameters in as 
strings, and my functions expected ints.
It's really not a big deal, I can just convert to int within each method, but 
it seems there should be a more elegant way to deal with this.

Thanks,

Al

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

________________________________

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.
Nï^jïïïïbïï%ïï(ïï^ïiïv&ïz\ïïYhï)ïï)äïïïkï
ÚZÞçïïzXïïï+ïËïï-

Other related posts: