[Tutor] associating two objects without ORM and processing a text file

Alan Gauld alan.gauld at btinternet.com
Fri Feb 15 09:58:40 CET 2013


On 15/02/13 03:09, neubyr wrote:

> I do have a doubt regarding this - e.g. how would I implement this if my
> program/application is web based. For example, loading the text file
> during web server start and stop.

For a long running process like a web server this is probably the wrong 
approach. You probably want to save the data more regularly - maybe even 
at the end of every user transaction. But with a web server we have the 
additional problem of usually wanting to handle multiple requests in 
parallel so storing data in memory gets more complicated - which takes 
us back to using a data base which pretty much handles all of that for you.

If you will only have a single request running at a time then you can 
use the same try/finally approach in your transaction processing code. 
But because they run  so often I'd add the 'dirty flag' idea that 
somebody else mentioned too so that you don;t sabve if no changes have 
been made.

A dirty flag is simply a global (or class) level variable (isDirty) that 
gets set by your code anytime you change the data. If a book changes its 
state it sets the flag to True. The save code then does something like

with open(filename) as store
    if Book.isDirty:
       for book in Book.instances:
           book.save(store)

That will ensure that any changes are saved but we don't waste time
if no changes exist.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list