Last active
December 14, 2015 07:19
-
-
Save cpdean/5049834 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/cpdean/5049834#file-pickletest-py-L20: ugh.