Skip to content

Instantly share code, notes, and snippets.

@brizandrew
Created August 5, 2016 19:46
Show Gist options
  • Save brizandrew/77b667519557ba852988cd20655a9f20 to your computer and use it in GitHub Desktop.
Save brizandrew/77b667519557ba852988cd20655a9f20 to your computer and use it in GitHub Desktop.
Series of functions to handle save files
import pickle
"""
@function save
Saves the saveData global variable into a pickle file
"""
def save():
global saveData
with open('filename.pickle','wb') as f:
pickle.dump(saveData, f)
"""
@function clearKey
Resets the value of a given key in saveData to its default
@param {str} key: The name of the key to reset
"""
def clearKey(key):
global saveData
saveData[key] = emptySave()[key]
"""
@function clearSave
Resets all values in saveData to their default value
"""
def clearSave():
global saveData
saveData = emptySave()
with open('filename.pickle','wb') as f:
pickle.dump(saveData, f)
"""
@function emptySave
Returns a new dictionary with the default values of saveData
@return {dict}: the empy dictionary
"""
def emptySave():
return # Default Value
"""
@function importLastSave
Attempts to open a save file.
"""
def importLastSave():
global saveData
try:
with open('filename.pickle','rb') as f:
saveData = pickle.load(f)
except FileNotFoundError:
# Handle if no file exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment