From e606c6919d160fddc049414d5bd089f04cc548dd Mon Sep 17 00:00:00 2001 From: mahdiar Date: Sat, 14 Nov 2026 16:57:22 +0330 Subject: [PATCH] =?UTF-8?q?=D8=A7=D8=B6=D8=A7=D9=81=D9=87=20=D8=B4=D8=AF?= =?UTF-8?q?=D9=86=20=DA=A9=D9=84=D8=A7=D8=B3=20=D8=AF=DB=8C=D8=AA=D8=A7=20?= =?UTF-8?q?=D8=A8=DB=8C=D8=B3=20=D8=AC=DB=8C=D8=B3=D9=88=D9=86=DB=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jsondb.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 jsondb.py diff --git a/jsondb.py b/jsondb.py new file mode 100644 index 0000000..da81e0f --- /dev/null +++ b/jsondb.py @@ -0,0 +1,29 @@ +import json +import os + +class JsonDB: + def __init__(self, file_name='db.json'): + self.file_name = file_name + if not os.path.exists(file_name): + with open(file_name, 'w', encoding='utf-8') as f: + f.write("{}") + + def write(self, data): + fd = self._load() + fd.update(data) + self._save(data=fd) + + def read(self): + return self._load() + + def _load(self): + with open(self.file_name, 'r', encoding="utf-8") as f: + content = f.read().strip() + if not content: # اگه خالی بود + return {} + return json.loads(content) + + def _save(self, data): + with open(self.file_name, 'w', encoding="utf-8") as f: + json.dump(data, f, indent=4, ensure_ascii=False) + \ No newline at end of file