Frage

I am inserting a value in a Json file using json store first from a function from current code and then from a function from a different code using , but when i update the value second time it resets the value which i set in first code .

I am sure you will understand it better from the sample code below :

I have 2 files :

  1. jsonstore.py - which will further import importedmodule which is file 2
  2. importedmodule.py

PLease see below code on both files .

jsonstore.py

from kivy.storage.jsonstore import JsonStore
import importedmodule as jb

JsonFileName = 'a.json'
store = JsonStore(JsonFileName)

def hello():
    store.put('ten', v=int(10))

hello()
jb.hello()

importedmodule.py

from kivy.storage.jsonstore import JsonStore

JsonFileName = 'a.json'
store = JsonStore(JsonFileName)

def hello():
    store.put('twenty', v=int(20))

I want that when i run 1st file code i.t. jsonstore.py its output should be as :

{"twenty": {"v": 20}, "ten": {"v": 10}}

but i am getting output as

{"twenty": {"v": 20}, "ten": {"v": 0}}

I am not sure why is it not updating value of ten as 10 . Not sure what is wrong or what i am doing wrong . Can anyone help me or advice .?

War es hilfreich?

Lösung

You're creating two JsonStores. This is like opening the same text file in two separate editors, then making changes and saving each copy of the file - only one copy will be saved, and the other will be overwritten. Try using the same store for each method.

jsonstore.py

from kivy.storage.jsonstore import JsonStore
import importedmodule as jb

JsonFileName = 'a.json'
store = JsonStore(JsonFileName)

def hello(s):
    s.put('ten', v=int(10))

hello(store)
jb.hello(store)

importedmodule.py

def hello(s):
    s.put('twenty', v=int(20))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top