Change values in a kivy storage

Question:

Can someone tell me how to change a value in a kivy storage (JsonStore) ?
Here is an example of what I have :

from kivy.storage.jsonstore import JsonStore

store = JsonStore("Test.json")

store["MyDict"] = {"0":"H", "1":"A", "2":"Y"}
print(store["MyDict"])

store["MyDict"]["1"] = "E"
print(store["MyDict"])

This code work but when I look in the Test.json file, there is this dictionnary {"0":"H", "1":"A", "2":"Y"} instead of this one {"0":"H", "1":"E", "2":"Y"}

I would like to use this as a normal dictionary.

Asked By: Tarezze

||

Answers:

you cannot directly do this because that store object is not a dictionary.
however, you can store your whole dictionary as one item in the store.
In this example, entry is a Python dict. According to the kivy.storage documentation, the storage objects have put(), get(), exists(), delete() and find() methods. This code shows a printout of protected memeber _data of the object which is a dict but it should not be modified directly.

from kivy.storage.jsonstore import JsonStore

store = JsonStore("Test.json")

MyDict = {"0": "H", "1": "A", "2": "Y"}
store.put("settings", MyDict=MyDict)

entry = store.get('settings')['MyDict']
entry["1"] = "E"
store.put("settings", MyDict=MyDict)
entry = store.get('settings')['MyDict']
print(f"protected member {store._data}")
print(entry["1"])
Answered By: Mark
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.