Configuration settings library
-
Is there a library or method to store settings(preferably in json) that survives reboots.
-
Haven't tried yet, but assume the file has to have an initial json string first?
-
@misterlisty I'm not sure what you want me to say, other than the fact that you probably have a syntax error in your JSON file
-
i get an error... syntax error in JSON
-
@misterlisty You need to make an object instead of using it like a method, and not use keyed params. Example:
from config import Config
c = Config()
c.get("existing_key")
c.set("existing_key","whatever_value")
c.save()
-
from config import Config
try:
Config.set(key="here",value="there") #failes with function missing required positional argument #0
Config.save()
devname = Config.get("here")
print("devname",devname)
except Exception as e:
print("config testfailed",e)
-
Just use the json lib and open/write it manually. Something like this
import json class Config: def __init__(self,filename="config.json"): self.filename = filename self.data = self.__read_file() def __read_file(self): with open(self.filename) as data: return json.load(data) def set(self,key,value): self.data[key] = value def get(self,key): return self.data[key] def save(self): with open(self.filename,'w') as f: f.write(json.dumps(self.data))