-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssigment06.py
More file actions
183 lines (150 loc) · 6.73 KB
/
Assigment06.py
File metadata and controls
183 lines (150 loc) · 6.73 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
# ---------------------------------------------------------------------------- #
# Title: Assignment 06
# Description: Working with functions in a class,
# When the program starts, load each "row" of data
# in "ToDoToDoList.txt" into a python Dictionary.
# Add the each dictionary "row" to a python list "table"
# ChangeLog (Who,When,What): Timothy McDowell, 5-24-22, Assignmnet 06
# RRoot,1.1.2030,Created started script
# Timothy McDowell, 5-24-22, Modified code to complete assignment 06
# ---------------------------------------------------------------------------- #
# Data ---------------------------------------------------------------------- #
# Declare variables and constants
file_name_str = "ToDoFile.txt" # The name of the data file
file_obj = None # An object that represents a file
row_dic = {} # A row of data separated into elements of a dictionary {Task,Priority}
table_lst = [] # A list that acts as a 'table' of rows
choice_str = "" # Captures the user option selection
# Processing --------------------------------------------------------------- #
class Processor:
""" Performs Processing tasks """
@staticmethod
def read_data_from_file(file_name, list_of_rows):
""" 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
"""
list_of_rows.clear() # clear current data
file = open(file_name, "r")
for line in file:
task, priority = line.split(",")
row = {"Task": task.strip(), "Priority": priority.strip()}
list_of_rows.append(row)
file.close()
return list_of_rows
@staticmethod
def add_data_to_list(task, priority, list_of_rows):
""" Adds data to a list of dictionary rows
:param task: (string) with name of task:
:param priority: (string) with name of priority:
:param list_of_rows: (list) you want filled with file data:
:return: (list) of dictionary rows
"""
row = {"Task": str(task).strip(), "Priority": str(priority).strip()}
# TODO: Add Code Here!
list_of_rows.append(row)
return list_of_rows
@staticmethod
def remove_data_from_list(task, list_of_rows):
""" Removes data from a list of dictionary rows
:param task: (string) with name of task:
:param list_of_rows: (list) you want filled with file data:
:return: (list) of dictionary rows
"""
# TODO: Add Code Here!
for row in list_of_rows:
if row["Task"].lower() == task.lower():
list_of_rows.remove(row)
print(task,"removed")
return list_of_rows
@staticmethod
def write_data_to_file(file_name, list_of_rows):
""" Writes data from a list of dictionary rows to a File
:param file_name: (string) with name of file:
:param list_of_rows: (list) you want filled with file data:
:return: (list) of dictionary rows
"""
# TODO: Add Code Here!
file = open(file_name, "w")
for row in list_of_rows:
file.write(row["Task"] + "," + row["Priority"] + "\n")
file.close()
return list_of_rows
# Presentation (Input/Output) -------------------------------------------- #
class IO:
""" Performs Input and Output tasks """
@staticmethod
def output_menu_tasks():
""" Display a menu of choices to the user
:return: nothing
"""
print('''
Menu of Options
1) Add a new Task
2) Remove an existing Task
3) Save Data to File
4) Exit Program
''')
print() # Add an extra line for looks
@staticmethod
def input_menu_choice():
""" Gets the menu choice from a user
:return: string
"""
choice = str(input("Which option would you like to perform? [1 to 4] - ")).strip()
print() # Add an extra line for looks
return choice
@staticmethod
def output_current_tasks_in_list(list_of_rows):
""" Shows the current Tasks in the list of dictionaries rows
:param list_of_rows: (list) of rows you want to display
:return: nothing
"""
print("******* The current tasks ToDo are: *******")
for row in list_of_rows:
print(row["Task"] + " (" + row["Priority"] + ")")
print("*******************************************")
print() # Add an extra line for looks
@staticmethod
def input_new_task_and_priority():
""" Gets task and priority values to be added to the list
:return: (string, string) with task and priority
"""
#pass # TODO: Add Code Here!
task = str(input("What is the task?: ")).strip()
priority = str(input("What is the priority?: - ")).strip()
return task, priority
@staticmethod
def input_task_to_remove():
""" Gets the task name to be removed from the list
:return: (string) with task
"""
#pass # TODO: Add Code Here!
task = str(input("What is the item? - ")).strip()
return task
# Main Body of Script ------------------------------------------------------ #
# Step 1 - When the program starts, Load data from ToDoFile.txt.
Processor.read_data_from_file( file_name=file_name_str, list_of_rows=table_lst) # read file data
# Step 2 - Display a menu of choices to the user
while (True):
# Step 3 Show current data
IO.output_current_tasks_in_list(list_of_rows=table_lst) # Show current data in the list/table
IO.output_menu_tasks() # Shows menu
choice_str = IO.input_menu_choice() # Get menu option
# Step 4 - Process user's menu choice
if choice_str.strip() == '1': # Add a new Task
task, priority = IO.input_new_task_and_priority()
table_lst = Processor.add_data_to_list(task=task, priority=priority, list_of_rows=table_lst)
continue # to show the menu
elif choice_str == '2': # Remove an existing Task
task = IO.input_task_to_remove()
table_lst = Processor.remove_data_from_list(task=task, list_of_rows=table_lst)
continue # to show the menu
elif choice_str == '3': # Save Data to File
table_lst = Processor .write_data_to_file(file_name=file_name_str, list_of_rows=table_lst)
print("Data Saved!")
continue # to show the menu
elif choice_str == '4': # Exit Program
print("Goodbye!")
break # by exiting loop