اضافه شدن کلاس دیتا بیس جیسونی
This commit is contained in:
@@ -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)
|
||||||
|
|
||||||
Reference in New Issue
Block a user