-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_database.py
55 lines (43 loc) · 2.08 KB
/
json_database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json
#TODO: page_id'yi notion so ya göre konrol et eğer yoksa json içersinden sil.
class JsonDatabase:
def __init__(self, file_name):
self.file_name = "Json/db/" + file_name
try:
with open(self.file_name, 'r') as f:
self.data = json.load(f)
except FileNotFoundError:
self.data = {'pages': []}
def write_data(self, page_id, normalized_name:str):
# Check if the page_id already exists in the "pages" list
if any(d['page_id'] == page_id for d in self.data['pages']):
raise ValueError(f"page_id {page_id} already exists in the database.")
# Append the new data as a dictionary to the "pages" list
self.data['pages'].append({'page_id': page_id, 'normalized_name': normalized_name.lower()})
# Write the data to the JSON file
with open(self.file_name, 'w') as f:
json.dump(self.data, f)
def read_data(self):
# Read and return the "pages" list from the JSON file
return self.data['pages']
def delete_data(self, page_id):
# Remove the data with the given page_id from the "pages" list
self.data['pages'] = [d for d in self.data['pages'] if d['page_id'] != page_id]
# Write the data to the JSON file
with open(self.file_name, 'w') as f:
json.dump(self.data, f)
def update_data(self, page_id, new_normalized_name:str):
# Find the data with the given page_id in the "pages" list and update its normalized_name
for d in self.data['pages']:
if d['page_id'] == page_id:
d['normalized_name'] = new_normalized_name.lower()
# Write the data to the JSON file
with open(self.file_name, 'w') as f:
json.dump(self.data, f)
def find_data(self, search_term:str):
# Search for data by page_id or normalized_name
results = []
for d in self.data['pages']:
if str(d['page_id']) == search_term or d['normalized_name'].lower() == search_term.lower():
results.append(d)
return results