Repository navigation
Expand file tree
/
Copy pathAssignment07.py
More file actions
269 lines (223 loc) · 10.4 KB
/
Copy pathAssignment07.py
File metadata and controls
269 lines (223 loc) · 10.4 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
## ------------------------------------------------------------------------ #
# Title: Assignment 07
# Description: Pickling anf structured error handling application
# modified the script of assignment 06 to use picking instead of
# text file; and to use error handling structure
# When the program starts, load each "row" of data
# in "ToDoFile.txt" into a python Dictionary.
# Add the each dictionary "row" to a python list "table"
# ChangeLog (Who,When,What):
# RRoot,1.1.2030,Created started script
# RRoot,1.1.2030,Added code to complete assignment 5
# RRoot,1.1.2030,Fixed bug by clearing the list before it was refilled
# Tao Ye, 5.19.2020, Modified code to complete assignment 6
# Tao Ye, 5.20.2020, Revision to correct FileProcessor.FindFile method
# Tao Ye, 5.25.2020, Modified code to complete assignment 7
# ------------------------------------------------------------------------ #
import pickle
# Data -------------------------------------------------------------------- #
# Declare variables and constants
strFileName = "ToDoFile.dat" # The name of the binary data file for pickling
#objFile = None # An object that represents a file
strData = "" # A row of text data from the file
dicRow = {} # A row of data separated into elements of a dictionary {Task,Priority}
lstTable = [] # A dictionary that acts as a 'table' of rows
strChoice = "" # Capture the user option selection
# Data -------------------------------------------------------------------- #
# Processing ------------------------------------------------------------- #
class FileProcessor:
""" Processing the data to and from a text file """
@staticmethod
def ReadFileDataToList(file_name, list_of_rows):
"""
Desc - Reads data from a file into a list of dictionary rows
:param file_name: (string) with name of file
:param list_of_rows: (list) you want filled with file data
:return: (list) of dictionary rows
"""
with (open(file_name, "rb")) as objFile:
while True:
try:
row = pickle.load(objFile)
list_of_rows.append(row)
except EOFError as e:
print("Read reached the end of the file.")
print("Built-In Pythons error info: ")
print(type(e))
print(e.__doc__)
print(e.__str__())
break
@staticmethod
def WriteListDataToFile(file_name, list_of_rows):
"""
Desc - Save a list of dictionary rows to a file
:param file_name: (string) with name of file
:param list_of_rows: (list) you want to save to the file
:return: (string) message to the user about the file save
"""
with open(file_name, "wb") as objFile:
for row in list_of_rows:
pickle.dump(row, objFile)
@staticmethod
def FindFile(file_name):
"""
Desc - Locate a file for subsequent file read
:param file_name: (string) with name of file
:return: (Boolean) True: file exists; False: file not found
"""
try:
file = open(file_name)
file.close()
return True
except FileNotFoundError:
return False
class ListOperations:
""" Operations to manipulate the list of dictionary rows """
@staticmethod
def AddNewItemToList(list_of_rows, task, priority):
""" Add a new item to the list
:param list_of_rows: (list) of rows to which a new item is added
:param task: (string) new task name
:param priority: (string) new task priority
:return: list is passed by reference, and will be updated
"""
taskExist = False
for row in list_of_rows:
if (row["Task"].lower() == task.lower()):
taskExist = True
break
if (not taskExist):
row = {"Task": task, "Priority": priority} # Create a new dictionary row
list_of_rows.append(row) # Add the new row to the list/table
else:
print("I am sorry, duplicate task in the list")
print()
@staticmethod
def RemoveItemFromList(list_of_rows, taskToRemove):
""" Remove an item from the list
:param list_of_rows: (list) of rows from which a task is removed
:param taskToRemove: (string) task to be removed
:return: list is passed by reference, and will be updated
"""
taskExist = False
# Search though the table or rows for a match to the user's input
for row in list_of_rows:
if (row["Task"].lower() == taskToRemove.lower()):
list_of_rows.remove(row)
taskExist = True
# Update user on the status of the search
if (taskExist):
print("The task was removed.")
else:
print("I'm sorry, but I could not find that task.")
print() # Add an extra line for looks
# Processing ------------------------------------------------------------- #
# Presentation (Input/Output) -------------------------------------------- #
class IO:
""" A class for perform Input and Output """
@staticmethod
def OutputMenuItems():
""" Display a menu of choices to the user
:return: nothing
"""
print('''
Menu of Options
1) Show current data
2) Add a new item.
3) Remove an existing item.
4) Save Data to File
5) Reload Data from File
6) Exit Program
''')
print() # Add an extra line for looks
@staticmethod
def InputMenuChoice():
""" Gets the menu choice from a user
:return: string
"""
choice = str(input("Which option would you like to perform? [1 to 6] - ")).strip()
print() # Add an extra line for looks
return choice
@staticmethod
def ShowCurrentItemsInList(list_of_rows):
""" Shows the current items in the list of dictionaries rows
:param list_of_rows: (list) of rows you want to display
:return: nothing
"""
print("******* The current items ToDo are: *******")
for row in list_of_rows:
print(row["Task"] + " (" + row["Priority"] + ")")
print("*******************************************")
print() # Add an extra line for looks
@staticmethod
def InputTaskAndPriority():
""" Ask the user to enter task and priority
:param none:
:return: (string) task and priority
"""
task = str(input("What is the task? - ")).strip()
priority = str(input("What is the priority? [high|low] - ")).strip()
return task, priority
# Presentation (Input/Output) -------------------------------------------- #
# Main Body of Script ---------------------------------------------------- #
# Step 1 - When the program starts, Load data from ToDoFile.txt.
# If the file already exists, load data from the file
if FileProcessor.FindFile(strFileName):
FileProcessor.ReadFileDataToList(strFileName, lstTable) # read file data
else:
print("File", strFileName, "does not exist yet.")
# Step 2 - Display a menu of choices to the user
while(True):
IO.OutputMenuItems() # Shows menu
strChoice = IO.InputMenuChoice() # Get menu option
# Step 3 - Process user's menu choice
# Step 3.1 Show current data
if (strChoice.strip() == '1'):
IO.ShowCurrentItemsInList(lstTable) # Show current data in the list/table
continue # to show the menu
# Step 3.2 - Add a new item to the list/Table
elif(strChoice.strip() == '2'):
# Ask user for new task and priority
strTask, strPriority = IO.InputTaskAndPriority()
print() # Add an extra line for looks
# Add item to the List/Table
ListOperations.AddNewItemToList(lstTable, strTask, strPriority)
# Show current data in the list/table
IO.ShowCurrentItemsInList(lstTable)
continue # to show the menu
# Step 3.3 - Remove an item from the list/Table
elif(strChoice == '3'):
# Ask user for item to be removed
strKeyToRemove = input("Which TASK would you like removed? - ")
# Remove the item from the list
ListOperations.RemoveItemFromList(lstTable, strKeyToRemove)
# Show the current items in the table
IO.ShowCurrentItemsInList(lstTable) # Show current data in the list/table
continue # to show the menu
# Step 3.4 - Save tasks to the ToDoFile.txt file
elif(strChoice == '4'):
# Show the current items in the table
IO.ShowCurrentItemsInList(lstTable) # Show current data in the list/table
# Ask user if they want to save that data
if("y" == str(input("Save this data to file? (y/n) - ")).strip().lower()):
FileProcessor.WriteListDataToFile(strFileName, lstTable)
input("Data saved to file! Press the [Enter] key to return to menu.")
else: # Let the user know the data was not saved
input("New data was NOT Saved, but previous data still exists! Press the [Enter] key to return to menu.")
continue # to show the menu
# Step 3.5 - Reload data from the ToDoFile.txt file (clears the current data from the list/table)
elif (strChoice == '5'):
print("Warning: This will replace all unsaved changes. Data loss may occur!") # Warn user of data loss
strYesOrNo = input("Reload file data without saving? [y/n] - ") # Double-check with user
if (strYesOrNo.lower() == 'y'):
lstTable.clear() # Added to fix bug 1.1.2030
FileProcessor.ReadFileDataToList(strFileName, lstTable) # Replace the current list data with file data
IO.ShowCurrentItemsInList(lstTable) # Show current data in the list/table
else:
input("File data was NOT reloaded! Press the [Enter] key to return to menu.")
IO.ShowCurrentItemsInList(lstTable) # Show current data in the list/table
continue # to show the menu
# Step 3.6 - Exit the program
elif (strChoice == '6'):
break # and Exit
# Main Body of Script ---------------------------------------------------- #