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.
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"])
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.
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"])