<br><br><div class="gmail_quote">On Thu, Feb 14, 2013 at 4:05 PM, Dave Angel <span dir="ltr"><<a href="mailto:davea@davea.name" target="_blank">davea@davea.name</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On 02/14/2013 04:33 PM, Prasad, Ramit wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Dave Angel wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 02/14/2013 12:35 PM, Prasad, Ramit wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
neubyr wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I am not sure how to save an object in memory to a file before exiting the program. Any examples or<br>
related documentation links would be really helpful. I am guessing it would be using some kind of<br>
before teardown method, but not sure about it. Any help?<br>
</blockquote>
<br>
Look at the pickle or shelve modules.<br>
<a href="http://www.doughellmann.com/PyMOTW/pickle/index.html" target="_blank">http://www.doughellmann.com/<u></u>PyMOTW/pickle/index.html</a><br>
<a href="http://www.doughellmann.com/PyMOTW/shelve/index.html" target="_blank">http://www.doughellmann.com/<u></u>PyMOTW/shelve/index.html</a><br>
<br>
</blockquote>
<br>
You miss the point.  The OP wants to make sure the text file is saved no<br>
matter how the program happens to exit.  He's not asking how to format<br>
the file.<br>
<br>
</blockquote>
<br>
Hmm. Good point Dave, I did miss that point.<br>
<br>
My knee jerk response is a try/finally block, but I am sure there<br>
are better ways.<br>
<br>
# UNTESTED<br>
stored_data = {}<br>
try:<br>
     stored_data = load_data()<br>
     while True:<br>
         #<do program><br>
except Exception:<br>
     raise # reraise exception to keep trace and still<br>
           # propogate error for attention<br>
finally:<br>
     store_data(stored_data) # Save data since we are exiting<br>
                             # (intentionally or not).<br>
</blockquote>
<br></div></div>
That would be my reaction as well.  I would, however make it conditional on some changes having been made.  That way if this program run only made queries, the effort and risk of saving can be avoided.<br>
<br>
The other thing I'd recommend is to store the data in an alternate file, and only delete the original when the alternate is ready to rename. That way, you can't readily get into trouble if something crashes while saving.<div class="HOEnZb">
<div class="h5"><br>
<br>
<br><br>
</div></div></blockquote></div><br><div>Thanks Ramit and Dave!</div><div><br></div><div>I haven't had chance to code/learn further, but I will give play with this soon.</div><div><br></div><div>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. </div>
<div><br></div><div>Hope to try it out soon!</div><div><br></div><div><br></div><div>- N</div><div><br></div>