[Tutor] Using a dictionary to map functions

Alan Gauld alan.gauld at yahoo.co.uk
Tue Apr 26 04:42:37 EDT 2016


On 26/04/16 05:30, Colby Christensen wrote:

> import re
> from store_point import store_point
> 
> try:
>     infile = open(raw_input("Enter input file name; name.txt:"),'r')
> except:
>     print "Invalid filename"
>     exit()
> 
> templist = []
> pt_table = {}
> cmd_table = {5:"store_point", 19: "line_line_int"}

Almost...

You need to store the actual function name not a string:

cmd_table = {
              5 : store_point,
             19 : line_line_int
             # etc...
            }

Notice, no quotes.


> for line in infile:
>     #print line
>     line = line.rstrip()
>     if re.search('^[0-9]+', line):
>         a = line.split()
>         templist.append(a)
> 
> for line in templist:
>     #use dictionary to call and pass arguments to function

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list