Skip to content

Instantly share code, notes, and snippets.

@cpdean
Last active December 14, 2015 07:19
Show Gist options
  • Save cpdean/5049834 to your computer and use it in GitHub Desktop.
Save cpdean/5049834 to your computer and use it in GitHub Desktop.
import pickle
path = "temp.db"
def get_db():
#touch the file. if it didn't exist, return empty dict
with open(path, 'a+') as f:
if len(f.read()) == 0:
print "file was empty"
return dict()
with open(path, 'rb') as pf:
database = pickle.load(pf)
return database
def save_db(database_dict):
# wb should add the file if it didn't exist
with open(path, "wb") as pf:
pickle.dump(database_dict, pf)
db = get_db()
db["test"] = 1
save_db(db)
test = get_db()
print "db in memory: ", db
print "db from disk after save: ", test
assert test["test"] == db["test"]
@cpdean
Copy link
Author

cpdean commented Feb 27, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment