ribs
ribs

Reputation: 59

Best Practices for Save Files - a python pickle use case

I'm a non-programmer who got into simple python programming for automation of GIS-related tasks at work. I've found I really enjoy it, however, and have be "expanding" my skills outside of work.

I'm currently playing with using the Pickle module for saving class instances, and realized that I have a few options for structuring that save data. My question relates to which option is the "better" or more "acceptable / best practice" option, or if there is a better option all-together.

I'll use a "playstation-esque" example, just for kicks:

There are multiple users. Each user has a set of trophies for a particular game. Each user might also have multiple Player profiles for a particular game, and each of those characters can have multiple saved instances of gameplay.

So when someone logs on, they choose which user they are, from that saved list of user objects (or create a new one - but for now I will be ignoring the create new scenarios). Depending on which user you are logged in as, the appropriate trophies object can get loaded from the save file, as well as any Player profiles associated with that user. When the user goes to load a saved game, they would chose the appropriate Player profile, and the appropriate saved game info assotiated with that character could be listed. The user would chose the save they want, and the game data would be loaded.

The way I see it, I have a few options:

1) Seperate the save files based on class. Ex:

A) "users" - this could contain a list of the User() objects
B) "<username>_trophies" - this could contain a list of the Trophy() objects assotiated with the user indicated in the filename
C) "<username>_characters" - this could contain a list of the Player() objects assotiated with the user indicated in the filename
D) "<username>_<charactername>_save<#>" - this could contain a save of the game info (probably as a dictionary with various info) assotiated with the username and charactername indicated in the filename

With this setup, each file could be unpickled as needed, so that the trophy info need never be loaded if the user never choses to look at their trophies.

However, I wonder if this is breaking it up too much and creating too many files. Would I be better off with this option?:

2) Have one comprehensive save file with all the info in dictionary form. For ex:

{User:[[Trophy,Trophy,Trophy], {Character: [(game info),
                                            (game info),
                                            (game info)];
                                Character:  [(game info),
                                            (game info),
                                            (game info)]}];
 User:[[Trophy,Trophy,Trophy], {Character: [(game info),
                                            (game info),
                                            (game info)];
                                Character:  [(game info),
                                             (game info),
                                             (game info)]}]}

So which of these is "better" ? Are either ok? Neither?

Thanks for any and all help / opinions!

Upvotes: 1

Views: 2355

Answers (2)

codedstructure
codedstructure

Reputation: 806

One of the key things regarding saving information of any sort is consistency. Unless you have large amounts of data, performance of file-systems is usually not an issue, as the filesystem cache means that you're rarely waiting on the disk itself.

I don't think there would be any benefits to splitting up the data in this sort of application, and the chances of them getting 'out-of-sync' is high. I'd strongly recommend putting things in a single file.

As far as a 'better option all-together' option, I'd suggest looking at the sqlite3 module (http://docs.python.org/2/library/sqlite3.html) - many applications (from time trackers, note takers etc to web browsers) use an sqlite database to store their config, and one of the great things about it is that there's just a single file containing the data, which can easily be moved around, backed up etc (with the caveat that it shouldn't be moved / backed-up when in use!)

You might also want to look at the json module, which has a similar interface to the pickle module (load/loads/dump/dumps) but the files it writes are - to a large extent - human readable and modifiable. The downside is that only basic types (strings, bools, numbers) as well as lists and dictionaries of these simple types can be stored.

Upvotes: 1

Ivan Nevostruev
Ivan Nevostruev

Reputation: 28723

If it's one user at the time, then you can just put everything into single file. This will be the easiest options to implement.

When number of users growth you'll see that changing single file from multiple processes will not work. Two processes read the file, changes it in memory and writes back on disk. Only changes saved last will preserve. So you'll naturally split your data into different files.

But again same problem can happen to file per user implementation. Then you should look into other kind of storage like databases.

Upvotes: 0

Related Questions