1

I want to make a python program (with a PyQt GUI, but I don't know whether that is relevant) that has to save some information that I want to store even when the program closes. Example for information I want to store:

  1. The user can search for a file in a file dialog window. I want to start the file dialog window in the previously used directory, even if the program is closed in between file searches.

  2. The user can enter their own categories to sort items, building up on some of my predefined categories. These new categories should be available the next time the program starts.

Now I'm wondering what the proper way to store such information is. Should I use pickle? A proper database (I know a tiny bit of sqlite3, but would have to read up on that)? A simple text file that I parse myself? One thing for data like in example 1., another for data like in example 2.?

Also, whatever way to store it I use, where would I put that file?

I'm asking in the context that I might want to later make my program available to others as a standalone application (using py2app, py2exe or PyInstaller).

Right now I'm just saving a pickle file in the directory that my .py file is in, like this answer reconmends, but the answer also specifically mentions:

for a personal project it might be enough.

(emphasis mine)

Is using pickle also the "proper, professional" way, if I want to make the program available to other people as a standalone application?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Freya W
  • 487
  • 3
  • 11
  • 1
    SQLite might be a better solution. You can check it here: https://www.sqlite.org/index.html – Moldovan Daniel Jan 23 '19 at 10:16
  • I voted to close your question as primarily opinion based - not that it's a bad question by itself, but your only criteria - "the proper professional way" - is not quantifiable and no answer based on this criteria alone can be backed by facts. Actually, my own definition of "the proper professional way" is "the simplest solution that gets the job done reliably", so I really woudn't consider sqlite as "the proper professional way" here. For your needs, a mere json file seems like a very obvious choice. – bruno desthuilliers Jan 23 '19 at 11:38
  • NB : sqlite _would_ be a sound choice if you had to deal with a larger set of relational data and don't want to depend on an external rdbms. – bruno desthuilliers Jan 23 '19 at 11:40
  • @brunodesthuilliers, I get why you would vote to close it in that case, but as someone new to needng persistent data, I didn't know there wasn't some "proper, professional" way, defined in some styleguide / coding convention that I didn't know about. – Freya W Jan 23 '19 at 14:21
  • Well, actually being "professionnal" only means that you get paid for you job - it doesn't say anything about your work's quality, and I can tell you that an awful lot of "professional" code I see is definitly not "proper" in any way. And no, there's no "style guide" or "coding convention" (both of which are purely arbitrary rules FWIW) for such design choices. At best there are "good practices", which consist mostly in choosing something that is robust, proven, stable, well known, well maintained, and as much as possible language-agnostic. And adapted to your use case of course. – bruno desthuilliers Jan 23 '19 at 14:31
  • @brunodesthuilliers thank you for your input! I think a json file and an sqlite db would both fulfill the good practice requirements that you mention. – Freya W Jan 24 '19 at 08:48

2 Answers2

3

Choice depends on your approach to data you store, which is yours?:

  • user should be able to alter it without usage of my program
  • user should be prevented from altering it with program other than my program

If first you might consider deploying JSON open-standard file format, for which Python has ready library called json. In effect you get text (which you can save to file) which is human-readable and can be edited in text editor. Also there exist JSON file viewers and editors which made viewing/editing of JSON files easier.

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • That's something I haven't considered and is certainly a good point to keep in mind: do I *want* people to be able to read and edit the stored data? And what happens if the user manipulates the file in a certain way that would cause unexpected behaviour... – Freya W Jan 23 '19 at 14:26
  • I chose this as my accepted answer. After brunodesthuilliers comments I think both a json file and an sqlite db would be the "right" way to do it, but I will go with a JSON file, since it is (for me) easier to implement, and the user can edit it more easily themselves, if e.g. they prefer adding categories via editing the JSON file instead of in the program. – Freya W Jan 24 '19 at 08:52
2

I think SQLite3 is the better solution in this case as Moldovan commented.

There is a problem in pickle, sometimes pickling format can be change across python versions and there are greater advantages of using sqlite3.

  • OK, but if I bundle the script as a standalone application, would it really matter if pickle changes across versions? Also, could you elaborate on other greater advantages of using sqlite3? – Freya W Jan 23 '19 at 14:25