Files
Igor-kr/Final control/4/Otvet.py
T
Igor 6a401a35b0
CI Check / lint-and-check (push) Failing after 9s
add KR
2026-07-18 23:37:11 +03:00

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()