On Fri, 24 May 2013 16:35:59 -0500 "Edward K. Ream" <[email protected]> wrote:
> On Thu, May 23, 2013 at 2:41 PM, Terry Brown <[email protected]>wrote: > > > I've added [an abbreviation] "persist;;" > > > [snip] > > How do you use the code inserted by the abbreviation? I suspect I'm not The corrected version is below for reference - I had `except` instead of `finally` in the first version, so you data was only saved if something went wrong :-) It's just a framework for providing a dictionary the contents of which persist between runs of the program. I've used it for code that's making lots of small url requests against a server, to cache the results so that during development the crash / debug / re-run cycle is faster because each piece of data is requested only once, ever, not once every run. More recently I was using it to store key value pairs of filenames and a comment on the issue in the file, knowing that completing the analysis of the issues in the files would take more than one run of the program which was identifying the issues. So in the example below, the content of the cache_info dictionary is persistent, whatever main() does with it is seen next time the program's run. Nothing Leo specific except that it's really just an import statement plus 5 lines of code and doesn't seem worth its own file, but is more than you want to re-type all the time either, so ideal for an abbreviation in your favorite editor. I've added a couple of comments in the version below. Cheers -Terry ---cut here--- import json # name for persistent data store json_state_file = "cache_info_file.json" if not os.path.exists(json_state_file): # create persistent data store if it doesn't exist json.dump({'cache_items':{}}, open(json_state_file, 'w')) # load persistent data cache_info = json.load(open(json_state_file)) def main(): # do stuff with cache_info... if __name__ == '__main__': try: main() finally: # save altered persistent data json.dump(cache_info, open(json_state_file, 'w'), indent=4) ---cut here--- > the only one who doesn't understand what you are trying to do... > > Edward > -- You received this message because you are subscribed to the Google Groups "leo-editor" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/leo-editor?hl=en-US. For more options, visit https://groups.google.com/groups/opt_out.
