Configuration settings library
-
Is there a library or method to store settings(preferably in json) that survives reboots.
-
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)) -
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) -
@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() -
i get an error... syntax error in JSON
-
@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
-
Haven't tried yet, but assume the file has to have an initial json string first?
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login