[Tutor] text processing by reading in a character at a time (original) (raw)

Magnus Lyckå magnus at thinkware.se
Sun Jul 18 12:42:43 CEST 2004


I'm bouncing this back to the list. Hope you don't mind.

At 02:41 2004-07-18 -0600, ppareek wrote:

Thanks Magnus. You are right, i am worried about the efficiency of the program.

One big advantage with Python is that it's quick and easy to try out different approaches and technical solutions.

Basically my programme has to work similar to the online discussion systems. It reads in the user's comments once the user submits them and adds them after finding the last comment on the html page. That is the reason that I want to process the file one character at a time and find the last comment on the page. this is the place where i append the new comment.

Do I understand that you have a file which looks something like this:

[head] [comment1] [comment2] [comment3] ... [commentn] [tail]

Your plan was to scan through that file, and insert [commentn+1] between [commentn] and [tail], right?

I wouldn't have done that.

I do understand that this would be very inefficient give that this is server side scripting, but this is my only plan for now

I have several other approches in mind.

One simple solution is to keep your original data in two files, one contaning:

[head] [comment1] [comment2] [comment3] ... [commentn]

and a separate with:

[tail]

It's trivial to append [commentn+1] to the end of the first file, make a copy of that, add [tail] to the copy, remove your old public HTML file, and rename the new file with all the data you need to the name of the file you serve.

Another approach is to have a template such as

templ = """[snip...] %s [snip some more]"""

keep the comments in way so that they can be retrieved into a python list and do something like

html = templ % "

".join(comment_list)

I don't know how big your file is supposed to be, but huge HTML files aren't very meaningful anyway... Maybe you even want something like

html = templ % "

".join(comment_list[-50:])

to get the last 50 comments. Perhaps you want to do...

html = templ % "

".join(comment_list[start:stop+1])

...and have parameters passed into your script to get a particular slice of comments from the whole list. Of course, if you have a huge amount of comments, you'd store them in a database and do the selection of rows when you fetch them from the database.

-- Magnus Lycka (It's really Lyckå), magnus at thinkware.se Thinkware AB, Sweden, www.thinkware.se I code Python ~ The Agile Programming Language



More information about the Tutor mailing list