Reputation: 143
I know how to do this with a single object in a pickle file or with a single object missing, but I don't know how to do something like this if more than 1 object is missing from the pickle file.
What I got now:
try:
user1 = pickle.load(open("users.pkl", "rb"))
except (OSError, IOError) as e:
user1 = users("user1")
pickle.dump(user1, open("users.pkl", "wb"))
What I'd like to achieve:
try:
user1 = pickle.load(open("users.pkl", "rb"))
user2 = pickle.load(open("users.pkl", "rb"))
user3 = pickle.load(open("users.pkl", "rb"))
except (OSError, IOError) as e:
if if 'user1' not in locals():
user1 = users("user1")
pickle.dump(user1, open("users.pkl", "wb"))
if if 'user2' not in locals():
user2 = users("user2")
pickle.dump(user2, open("users.pkl", "wb"))
if if 'user3' not in locals():
user3 = users("user3")
pickle.dump(user3, open("users.pkl", "wb"))
Problem with this is that it looks quite messy and doesn't work correctly if 'user1' doesn't exist while 'user2' does (because it immediately goes to exception). Is there a better, more pythonic way of doing something like this? AFAIK python documentation tells us that try statement is the way to go when it comes to stuff like this but as you can see above I don't know how to implement this idea properly (unless I'd use a separate try statement for every object in the file, which would lead to tons of code).
Upvotes: 2
Views: 609
Reputation: 37123
Pickle files are written sequentially. Since the length of an object's pickle can vary, it is generally not possible to replace one pickle by another in-place inside an existing file, any more than you could replace a short line inside a text file with a longer one.
Your existing code reads the same pickle into user1
, user2
and user3
, so I am unsure how you would get different values from the exact same expression.
Consider instead using the shelve
module, which acts like an on-disk dictionary in many ways, and which allows you to very easily test for the presence or absence of specific keys.
Here's some code that should give you a hint as to how to proceed:
>>> with shelve.open("my_database") as db:
... db["user1"] = [1, 2, 3]
... db["user2"] = [2, 3, 4]
...
>>> with shelve.open("my_database") as db:
... if "user1" in db:
... print(db["user1"])
... else:
... print("Entry needs creating!")
...
[1, 2, 3]
Upvotes: 4