[Tutor] script question

Kent Johnson kent37 at tds.net
Tue Sep 27 00:54:57 CEST 2005

Chris Hallman wrote:
> Here is the script:
> import ConfigParser, string, sys
> section = sys.argv[1]
> port = sys.argv[3]
> INI=ConfigParser.ConfigParser()
> INI.read("interfaces.ini")
> passwordentries=[p for p in INI.options(section)]
> passwordlist=[INI.get(section, pw) for pw in passwordentries]
> print passwordlist
> for i in passwordlist:
>     if i == port:
>         os.system("d:\\tnd\\bin\\cawto.exe -cat NetNet " + sys.argv[1] + 
> " " + sys.argv[2] + " " + sys.argv[3] + " " + sys.argv[4])
> I'd like to know if there is a more efficient way to do this and if 
> there is a way to have these functions performed with the least amount 
> of time possible (the ini file could grow quite large).

*TEST FIRST* Don't optimize until you know it is too slow and you have a test case that you can time to see if your 'optimizations' are making it faster.

That said...
RawConfigParser should be faster than ConfigParser because it doesn't try to interpolate into the values.

RawConfigParser is built on Python dicts which are very fast, and the parsing is pretty simple so that should be fast too. It does copy sections before giving you the items so that could be a performance hit.

Since it appears that all you really need to know is if a particular section has a particular value, you might be able to write a simple line-by-line file scanner to figure it out. That might be faster than ConfigParser - you don't have to build the dicts, and you can stop when you find the section of interest.

BUT, again, try it first! Python is pretty fast for this kind of stuff. How many entries do you expect to have? How fast does it have to be?


More information about the Tutor mailing list