views:

241

answers:

2

Developing a project of mine I realize I have a need for some level of persistence across sessions, for example when a user executes the application, changes some preferences and then closes the app. The next time the user executes the app, be it after a reboot or 15 minutes, I would like to be able to retain the preferences that had been changed.

My question relates to this persistence. Whether programming an application using the win32 API or the MFC Framework .. or using the newer tools for higher level languages such as wxPython or wxRuby, how does one maintain the type of persistence I refer to? Is it done as a temporary file written to the disk? Is it saved into some registry setting? Is there some other layer it is stored in that I am unaware of?

+1  A: 

There are different methods to do this that have evolved over the years.

These methods include (but not limited to):

  1. Registry entries.
  2. INI files.
  3. XML Files
  4. Simple binary/text files
  5. Databases

Nowadays, most people do this kind of thing with XML files residing in the user specific AppData folders. It is your choice how you do it. For example, for simple things, databases can be overkill and for huge persisted objects, registry would not be appropriate. You have to see what you are doing and do it accordingly.

Here is a very good discussion on this topic

Aamir
Thank you for your suggestions. Ultimately the configuration makes the most sense. I can interface it from within the program and it takes care of itself. No files to worry about.
Matt1776
+1  A: 

I would advice to do it in two steps.

  1. First step is to save your prefs. as string, for that you can

    a) Use any xml lib or output xml by hand to output string and read similarly from string

    b) Just use pickle module to dump your prefs object as a string

    c) Somehow generate a string from prefs which you can read back as prefs e.g. use yaml, config , JSON etc actually JSON is a good option when simplejson makes it so easy.

  2. Once you have your methods to convert to and from string are ready, you just need to store it somewhere where it is persisted and you can read back next time, for that you can

    a) Use wx.Config which save to registry in windows and to other places depending on platform so you don't have to worry where it saves, you can just read back values in platform independent way. But if you wish you can just use wx.Config for directly saving reading prefs.

    b) Directly save prefs. string to a file in a folder assigned by OS to your app e.g. app data folder in windows.

Benefit of saving to a string and than using wx.Config to save it, is that you can easily change where data is saved in future e.g. in future if there is a need to upload prefs. you can just upload prefs. string.

Anurag Uniyal
Thank you, very concise and detailed. The configuration object allows me to store the data in a secure way without need worry about files or where to store them or anything. This way I have a OO interface and everything else is handled.
Matt1776