EgonDev

March 2, 2010

Some Validation

Filed under: What I Did Today — Tags: — egoncasteel @ 10:33 pm

I started in web programing and have found that you can never underestimate ether the users stupidity or malicious intent. That means validation on every piece of info needs to be done. In the end you will be glad you added it even thou it is annoying and boring to program.

Now there isn’t really a good reason other then the practice to add validation to the exercises you do while going through a programing book, but it just seems wrong to take input and not validate to me. So here are a couple of ways I have started doing it in Python

Pick your option

Here is a common validation task you will see a lot. The user must choose form a list of options.

print "temp conversion program"
conType = "none"
while not(conType == "ctf" or conType == "ftc"):
    conType = raw_input("cel to fahr. or fahr to cel (enter ctf or ftc): ")
sTemp = "none"

So in this chunk of code the user is being asked if he wants to go from cel to fahr. or fahr. to cel.  I only want to except ctf or ftc. Any thing else will mess up the rest of the program.

Notice I set the conType var before the while loop that holds the prompt for the user. This is so the program will ask the use for input on the first time through. Now if the user inputs one of the chosses I want to see the while loop will eval false and the program will move on. If the user enters something I don’t want to see the while loop will eval true and give the user a chance to try again.

Getting the right type of input

Here is another validation problem that comes up a lot. Getting the right type of input. In this case the “type” I am talking about is data type (str, int, float, …).

change = "none"
while change == "none":
    try:
        change = float(raw_input("enter the change: "))
    except:
        print "** input must be a float **"
        change = "none"
        pass

In this code sample I am trying to get the user to give me something I can turn in to a float (ex. 12.56). Now I have still wrapped the input section in a while loop, but now we have the try and except statements in the mix. Here is what I am telling Python to do:

  • Try to
    • Take the the users input
    • Change the users input in to a float
    • Assign that float
  • If any of the things you try raise an error
    • Tell the user they failed
    • Set change back to “none” (so the while loop will execute again)
    • Don’t show the user any errors or stop the program
      • That is what the “pass” keyword does in the except block

This will work for any of Python’s type convert functions.  With this method you get the best of both worlds. You get your input converted and you take care of validation.  If you did this with a regular expression you would have all the over head of the regular expression engine, and you still wouldn’t be 100% sure if Python can convert the type because you haven’t asked it to yet .

Advertisements

Python Modules :: Building a little Library

Filed under: What I Did Today — Tags: — egoncasteel @ 9:20 pm

Here is what I did for my little dev library for Python

  • Made a directory in my home folder called /home/egon/pyModules
  • Added an empty file to my home directory, the pyModules director, and inside any sub-directories named __init__.py

So what does that get you?

Well now I can import scripts buy using a statement like this:

import pyModules.easygui.easygui

This will import the easygui module from the /home/egon/pyModules/easygui directory. You can now use functions defined in the easygui module calling them as such:

pyModule.easygui.easygui.msgbox('hello')

don’t worry there is a short cut coming

Now you really don’t want to type pyModules.stuff.stuff every time you want to access a method so here is the shortcut:

egui = pyModule.easygui.easygui
egui.msgbox('hello')

See told you not to worry

Once you assign the module you imported to a new name you can use that name instead of the full reference.

So what is going on here?

Well in Ubuntu your home directory is part of your Python path (where Python gos looking when you try to import a module). By adding the __init__.py files it tells Python to treat those directories as modules. That allows us to you the dot notation to get to the module you want to import.

Why bother?

I just came up with this scheme to organize the modules I would using while learning Python. I didn’t want to just drop them into my home directory or the main Python directory. This seemed like a good way to do it to me, but I would never dare say it is a good idea for everyone. Also this maybe  a really ugly way to do it. I don’t know enough about Python yet to say. Feel free to put a better solution in comments.


Back on the Snake

Filed under: What I Did Today — Tags: — egoncasteel @ 8:01 pm

It has been awhile, but I thought I would post some more. About 2 years ago I spent a couple of weeks learning Python, and I really liked it. Well everything I learned is now forgotten, but I thought I would take another crack at it. So you can expect to see some more Python posts here soon. Just remember I am learning Python as I go so I am sure that some of this will not be best practices. Hell some of it is going to be down right wrong I am sure.

Blog at WordPress.com.