code style/efficiency question: text file templates

Chris Liechti cliechti at gmx.net
Sat Aug 3 22:23:49 EDT 2002


Chris <<>> wrote in news:f8voku40m0kiij0cph0fkj2qt8ddi8sp9s at 4ax.com:
> simple stuff here.
> 
> If I want to generate 100 subtly different templated HTML pages that
> are each about 150 lines long, do you think it better (efficiency
> and/or style) to create one file, then read, modify necessary parts,
> and save as new file name, or to run through a loop of 150
> file.write('') for each file name?  
> 
> So either I read the 150 lines and replace strings, or write 150
> lines.

if your files fit easily into memory, and i think they should as htmls ;-), 
then i would use either file.read() or .readlines(). which one depends on 
your algorithm if you want to work with lines, take the later.
last time i did something like that i used 
out.write(file.read().replace(...))
if you decide to go for lines, don't forget that there is .writelines() 
which lets you efficiently write a list of strings (not only lines)
 
> The page to page deviations are minor (one image name, one caption
> string from image name).  I was assuming I would do the latter, but
> the former occurred to me tonight.  Either way I obviously have to
> devise and type the HTML code once, obviously.

maybe you're interested in HTML generators like ht2html.sf.net. that one 
uses .ht files that contain only the body of the page (the 
"contents"/information) and the tool generated the layout around it, 
including navigation, styles etc.

> Saving femtoseconds isn't critical, but I'd like to get some real
> programmers' views on which is better and maybe learn some higher
> principle.

i guess that the differences in efficiency are small enough so that writing 
this mail consumed as much time as the difference of many 1000 runs of the 
slower implemenattion compared to the faster ;-)

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list