0

trying to store the values in a nested dictionary in a shelve file with setdefult(). Is there any easy way to do this? The following code seems to make the values immutable, for example, the last line is unable to change the 'price' value to 25.

room_data = shelve.open("data")

room_data.setdefault("key", {"type": "Standard Single", "available": 5, "price": 50, "adults": 1, "children": 0})

room_data["key"]["price"] = 25

I was hoping to get it functioning with shelve then add SQL later but it might be easier to just learn that now. Let me know what you think. Thanks.

martineau
  • 119,623
  • 25
  • 170
  • 301
WastedHat
  • 15
  • 1
  • 6
  • 1
    I cannot understand your problem – Stavros Avramidis May 24 '17 at 10:00
  • **If** the problem is that the value associated with `"price"` in the nested dictionary does not seem to be changed, it's because `shelve` doesn't automatically know when mutable values like dictionaries in it are changed. You need to either call `room_data.sync()` or `room_data.close()` to get changes written to the backing file. – martineau May 24 '17 at 10:35

1 Answers1

0

You have to set writeback=True:

room_data = shelve.open("data", writeback=True)

And then call room_data.sync() after mutating a value:

room_data.setdefault("key", {"type": "Standard Single", "available": 5, "price": 50, "adults": 1, "children": 0})

room_data["key"]["price"] = 25

room_data.sync()

Otherwise the value is set but the set value can't be mutated.

From note on shelve.open:

Because of Python semantics, a shelf cannot know when a mutable persistent-dictionary entry is modified. By default modified objects are written only when assigned to the shelf (see Example). If the optional writeback parameter is set to True, all entries accessed are also cached in memory, and written back on sync() and close(); this can make it handier to mutate mutable entries in the persistent dictionary, but, if many entries are accessed, it can consume vast amounts of memory for the cache, and it can make the close operation very slow since all accessed entries are written back (there is no way to determine which accessed entries are mutable, nor which ones were actually mutated).

Dan D.
  • 73,243
  • 15
  • 104
  • 123