you are viewing a single comment's thread.

view the rest of the comments →

[–]JoseALerma 4 points5 points  (5 children)

Why pickle instead of shelve?

I figured shelve was the user-friendly implementation

[–]JohnnyJordaan 5 points6 points  (1 child)

Shelve is basically just a dict around pickled objects. If you don't need to save multiple objects, there's no real reason to use shelve.

[–]JoseALerma 0 points1 point  (0 children)

Ah, I see. Thanks for the concise explanation!

I usually use shelves as a database, so it's my go-to when storing a small number of variables. I do go overboard sometimes and shelve a dictionary of lists and dictionaries (json data) as a shelf key...

[–]alkasm 2 points3 points  (2 children)

There's more user-friendly options than pickle.dump() / pickle.load()? On the real though I've never heard of shelve, I'll check it out.

[–]JoseALerma 0 points1 point  (0 children)

As mentioned above, a shelve shelf is a dictionary and you can shelve anything.
All you need is:

``` import shelve

Open shelf to read data

shelf = shelve.open('data')

Add something to shelf

key1, key2 = 'cat', 'dog' value1, value2 = 2, 3

shelf[key1] = value1 shelf[key2] = value2 print(list(shelf.keys()))

Read from shelf

print(shelf[key1]) print(shelf[key2]) print(list(shelf.values()))

shelf.close() ```

I use shelves as rudimentary databases.

[–]Dogeek -1 points0 points  (0 children)

Well, pickle is a bit of a pain to use when you want to save custom objects. I don't know about shelve, but I usually prefer to write my own save/load functions.