-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary operations{ dict }.py
More file actions
95 lines (84 loc) · 2.97 KB
/
Binary operations{ dict }.py
File metadata and controls
95 lines (84 loc) · 2.97 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pickle
import sys
import time
#make sure to use the file format of .dat extension
def read_data_from_file(file_path):
try:
with open(file_path, "rb") as file:
data = pickle.load(file)
except (EOFError, FileNotFoundError): #if file doesn't exist or is empty then it'll make sure the code doesn't terminates.
data = {}
return data
def write_data_to_file(data, file_path):
with open(file_path, "wb") as file:
pickle.dump(data, file)
def insert():
file_path = r"C:\Users\Administrator\Desktop\txt files\omg.dat"
data = read_data_from_file(file_path)
nw_id = int(input("Enter news Id or Code= "))
news = input("Enter a brief title for the news= ")
data[nw_id] = news
write_data_to_file(data, file_path)
print("News data has been successfully inserted in the Log.")
news_log()
def update():
file_path = r"C:\Users\Administrator\Desktop\txt files\omg.dat"
data = read_data_from_file(file_path)
temp = int(input("Enter News ID or Code to find= "))
if temp in data.keys():
print("Log found.")
nw_id = int(input("Enter new news id or code= "))
news = input("Enter new news title for the news= ")
data[nw_id] = news
write_data_to_file(data, file_path)
print("Data successfully updated.")
print(nw_id, " = ", data[nw_id])
else:
print("No such log found.")
news_log()
def delete():
file_path = r"C:\Users\Administrator\Desktop\txt files\omg.dat"
data = read_data_from_file(file_path)
nw_id = int(input("Enter news id or code to delete= "))
if data.pop(nw_id, None) is None:
print("No such log found. Deletion process terminated.")
else:
print("Log deleted successfully. ")
write_data_to_file(data, file_path)
news_log()
def search():
file_path = r"C:\Users\Administrator\Desktop\txt files\omg.dat"
data = read_data_from_file(file_path)
nw_id = int(input("Enter news id or code to search log= "))
if data.get(nw_id, None) is None:
print("No such log found.")
else:
print("Data Found.\n", nw_id, data[nw_id])
news_log()
def display():
file_path = r"C:\Users\Administrator\Desktop\txt files\omg.dat"
data = read_data_from_file(file_path)
for key, value in data.items():
print(key, "=", value)
news_log()
#menu for all operations.
def news_log():
while True:
print("1. Insert data\n2. Update data\n3. Delete data\n4. Search data\n5. Display all data\n6. Exit")
time.sleep(2)
ch = int(input("Enter your choice [1-6]= "))
if ch == 1:
insert()
if ch == 2:
update()
if ch == 3:
delete()
if ch == 4:
search()
if ch == 5:
display()
if ch == 6:
sys.exit()
else:
print("Enter a valid choice!! \n")
news_log()