In experimenting with pickle, I’ve put together some code for a (very) simple blog. It’s intended to display time-stamped blog contents from a pickle’d list in reverse chron order. It then prompts whether you want to add to the blog. (As you can see, I’m new to programming). Please critique this and/or suggest ways to make this even more efficient. This was written for Python 2.7. Also, I’ve saved this as Journal.py. I call it by issuing “reload(Journal)”. How would I call this module without the “reload” command ?Thank you.
import pickle
import time
from time import strftime
archive_log = []
new_log = []
with open('journal.pickle','r') as f:
archive_log = pickle.load(f)
for item in reversed(archive_log):
print item
proceed = 'y'
cont = []
while proceed == 'y':
cont = raw_input('Add an entry ? ')
if cont == 'n':
break
else:
new_log = (strftime('%Y%m%d %H:%M:%S *%a* ') + raw_input('Enter new info '))
archive_log.append(new_log)
with open('journal.pickle', 'w') as f:
pickle.dump(archive_log, f)
To answer your immediate question, you can run this code by using:
and things should mostly work (I didn’t test your code, just glanced at it). To improve upon it, you should look at Guido’s guide to using main() here and possibly utilizing a #! (reference) so you can do: