[Tutor] Key Error

Alan Gauld alan.gauld at btinternet.com
Mon Jul 9 01:17:24 CEST 2007


"Sara Johnson" <sarliz73 at yahoo.com> wrote 

>  I apologize...but what is, 'grep'?  

The General Regular Expression Parser - at least that's one 
of the explanations. (Another is that it is the ed search 
command, and there are at least two more, it's part of the 
Unix mythology, debated for at least 30 years! :-)

More pragmatically it is the standard Unix tool for searching 
text files for strings. Its available on other OS too thanks to GNU.

Thus grep "NEWI" *.py

will find all the occurences of NEWI in your python files.
(grep -f will list only the files containing it.) If you use emacs 
or vi as your editor you can get the editor to step through the 
results in the same way as you step through the compilation 
errors after a make...

You can search for sophisticated regex patterns and 
include or exclude patterns etc. grep should be a basic 
tool of any programmer regardless of language used or OS.

> But if I can't get the program to initialize this 'NEWI' then 
> I don't know how any values can come from it. 

Thats the critical factor here. You need to initialise the 
dictionary in the first place. I notice from another post tat its 
being loaded from a pickle file. Try looking to see where it 
gets "dumped" to the file, that might help (grep again!)

If the dump only happens when the program closes down 
then maybe just writing some default values on the first 
load of the program will do - thats where the get() method 
comes in again...

It looks suspiciuously to me as if this NEWI key is some 
extra feature thats been added to the code and the initialisation 
code has not been updated to cope. Thats a common error 
especially in programs where the initialisation is in another 
program that only got run once a long time ago!

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list