45 lines
1.0 KiB
Python
45 lines
1.0 KiB
Python
import json
|
|
|
|
|
|
class Config():
|
|
def __init__(self, filename):
|
|
self.filename = filename
|
|
|
|
def load(self):
|
|
try:
|
|
with open(self.filename, "r", encoding="utf-8") as file:
|
|
return json.load(file)
|
|
except FileNotFoundError:
|
|
return {}
|
|
|
|
def save(self, data):
|
|
with open(self.filename, "w", encoding="utf-8") as file:
|
|
json.dump(data, file,indent=4, ensure_ascii=False)
|
|
|
|
def get(self, key, default=None):
|
|
data = self.load()
|
|
return data.get(key, default)
|
|
|
|
|
|
def set(self, key, value):
|
|
data = self.load()
|
|
data[key]=value
|
|
self.save(data)
|
|
|
|
|
|
class Application():
|
|
def __init__(self, config):
|
|
self.config = config
|
|
|
|
def run(self):
|
|
username = self.config.get("username","гость")
|
|
print(f"Hello,{username}")
|
|
|
|
def set_username(self, name):
|
|
self.config.set("username", name)
|
|
|
|
config = Config("settings.json")
|
|
ap = Application(config)
|
|
ap.set_username("Иван")
|
|
ap.run()
|