check to see if value can be an integer instead of string

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Jan 18 23:15:55 CET 2006


On Wed, 18 Jan 2006 04:23:17 -0800, nephish wrote:

> it isn't really that i will want to change it to an integer anyway. the
> script uses a table to reference a value to a key, if the key is a
> group of letters, that code tells the script to do something. if the
> value is a number, it means an equipment failure. The thing is, all the
> values come out as strings (they are read from a text file).
> so what you put first with the try/except looks like my best answer.


def do_this():
    print "Do this"

def do_that():
    print "Doing something else"

code_table  = { "abc": do_this, "def": do_that, "xyz": do_this }
error_table  = { "001": "Broken widget", "002": "Overloaded doohickey"}

if code_table.has_key(value_from_hardware):
    code_table[value_from_hardware]()
else:
    try:
        print error_table[value_from_hardware]
    except KeyError:
        raise CustomHardwareError("Unknown value!")


-- 
Steven.




More information about the Python-list mailing list