forked from werhereitacademy/Python_Modul_Week_3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_class.py
More file actions
106 lines (97 loc) · 3.29 KB
/
base_class.py
File metadata and controls
106 lines (97 loc) · 3.29 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
96
97
98
99
100
101
102
103
104
105
106
import json,sys
class base_class:
def __init__(self):
self.__status = ['completed','Pending']
def menu_list(self):
create_menu_list = [
{
"text":"Add a new task Press 1",
"type":"int"
},
{
"text":"Complete a task Press 2",
"type":"int"
},
{
"text":"Delete a task Press 3",
"type":"int"
},
{
"text":"List completed tasks Press 4",
"type":"int"
},
{
"text":"List all tasks with their status Press 5",
"type":"int"
}]
return create_menu_list
def add_new_task(self):
try:
task = input("Please Enter Task")
status = input("Please Enter status")
data = self.__read_json_data()
id = self.__create_sequence_id()
data.append({"id":id,"task":task,"status":status})
self.__write_data_in_file(data)
return "New customer added"
except Exception as e:
print(e)
def complete_task(self):
try:
id = int(input("Please Enter the task id which you want to update "))
rows = self.__read_json_data()
for i in rows:
i["status"] = "completed"
self.__write_data_in_file(rows)
return "Successfully updated"
except Exception as e:
print(e)
def delete_task(self):
try:
id = int(input("Please Enter the task id which you want to delete "))
data = self.__read_json_data()
filtered_data = [item for item in data if item["id"] != id]
self.__write_data_in_file(filtered_data)
return "Successfully deleted"
except:
print("There is some Error")
def view_list_task(self):
try:
data = self.__read_json_data()
text = ''
for i in data:
text += "SR.NO: "+str(i['id'])+" Task: "+i['task']+" Status: "+i["status"]+"\n"
return text
except Exception as e:
print("There is some error to read data")
def __find_missing_numbers(self,seq):
if not seq:
return 1
full_range = set(range(min(seq), max(seq) + 1))
missing = sorted(full_range - set(seq))
if not missing:
return max(seq) + 1
else:
return missing[0]
def __create_sequence_id(self):
data = self.__read_json_data()
list_id = []
for ids in data:
list_id.append(ids["id"])
return self.__find_missing_numbers(list_id)
def __read_json_data(self):
try:
f = open('data.json')
data = json.load(f)
f.close()
return data
except Exception as e:
print("There is some error to reada data")
def __write_data_in_file(self,data):
try:
data = sorted(data, key=lambda x: x["id"])
with open("data.json",'w') as file:
file.seek(0)
return json.dump(data, file, indent = 4)
except:
return "There is some error to data write in file"