adds csv task manager
This commit is contained in:
parent
103178b3ac
commit
3c0cfad959
|
@ -1972,6 +1972,163 @@ Go back to you *currency converter* program and add a `dict` with multiple curre
|
||||||
|
|
||||||
# Coding challenge - Task manager
|
# Coding challenge - Task manager
|
||||||
|
|
||||||
|
## CSV based task manager
|
||||||
|
|
||||||
|
```python3
|
||||||
|
import csv
|
||||||
|
|
||||||
|
FILE = "mytasks.csv"
|
||||||
|
|
||||||
|
def read_db():
|
||||||
|
db = []
|
||||||
|
with open(FILE, "r") as fp:
|
||||||
|
data = fp.readlines()
|
||||||
|
for task in data:
|
||||||
|
task = task.strip()
|
||||||
|
db.append(task)
|
||||||
|
db = [task.strip() for task in data] # list comprehension
|
||||||
|
return db
|
||||||
|
|
||||||
|
|
||||||
|
def write_db(tasks):
|
||||||
|
with open(FILE, "w") as fp:
|
||||||
|
for task in tasks:
|
||||||
|
fp.write("{}\n".format(task))
|
||||||
|
|
||||||
|
|
||||||
|
def read_csv():
|
||||||
|
db = []
|
||||||
|
with open(FILE, "r") as fp:
|
||||||
|
reader = csv.DictReader(fp)
|
||||||
|
for item in reader:
|
||||||
|
db.append(item)
|
||||||
|
return db
|
||||||
|
|
||||||
|
|
||||||
|
def write_csv(tasks):
|
||||||
|
with open(FILE, "w") as fp:
|
||||||
|
keys = tasks[0].keys()
|
||||||
|
writer = csv.DictWriter(fp, keys)
|
||||||
|
writer.writeheader()
|
||||||
|
for task in tasks:
|
||||||
|
writer.writerow(task)
|
||||||
|
|
||||||
|
|
||||||
|
def delete_task(index, task_list):
|
||||||
|
if index <= len(task_list):
|
||||||
|
task = task_list.pop(index)
|
||||||
|
print("deleting: {}".format(task))
|
||||||
|
else:
|
||||||
|
print("cannot find your task")
|
||||||
|
|
||||||
|
|
||||||
|
def mark_task_done(index, task_list):
|
||||||
|
if index <= len(task_list):
|
||||||
|
task = task_list[index]
|
||||||
|
task["done"] = "0"
|
||||||
|
print("marking done: {}".format(task["description"]))
|
||||||
|
|
||||||
|
|
||||||
|
def add_task(task, task_list):
|
||||||
|
task_list.append(task)
|
||||||
|
print("you now have {} tasks to do...".format(len(task_list)))
|
||||||
|
|
||||||
|
|
||||||
|
def show_tasks(task_list):
|
||||||
|
for task in task_list:
|
||||||
|
if task["done"] == "0":
|
||||||
|
continue
|
||||||
|
index = task_list.index(task)
|
||||||
|
key = "description"
|
||||||
|
print("\t{}:\t{}:\t{}".format(index, key, task[key]))
|
||||||
|
key = "priority"
|
||||||
|
print("\t\t{}:\t\t{}".format(key, task[key]))
|
||||||
|
print("<--")
|
||||||
|
|
||||||
|
# interface
|
||||||
|
|
||||||
|
def ask_for_action():
|
||||||
|
print("what do you want to do?")
|
||||||
|
print("1. add a task")
|
||||||
|
print("2. delete a task")
|
||||||
|
print("3. mark task done")
|
||||||
|
print("4. show all tasks")
|
||||||
|
print("5. save and quit")
|
||||||
|
while True:
|
||||||
|
action = input("--> ")
|
||||||
|
if action.isdigit():
|
||||||
|
action = int(action)
|
||||||
|
break
|
||||||
|
print("please input a number...")
|
||||||
|
return action
|
||||||
|
|
||||||
|
|
||||||
|
def ask_for_index():
|
||||||
|
while True:
|
||||||
|
index = input("which index do you want? ")
|
||||||
|
if index.isdigit():
|
||||||
|
index = int(index)
|
||||||
|
break
|
||||||
|
print("only digits please...")
|
||||||
|
return index
|
||||||
|
|
||||||
|
|
||||||
|
def ask_for_priority():
|
||||||
|
while True:
|
||||||
|
index = input("which priority should the task have? ")
|
||||||
|
if index.isdigit():
|
||||||
|
index = int(index)
|
||||||
|
break
|
||||||
|
print("only digits please...")
|
||||||
|
return index
|
||||||
|
|
||||||
|
|
||||||
|
def ask_for_description():
|
||||||
|
min = 5
|
||||||
|
while True:
|
||||||
|
description = input("what do you have to do? ")
|
||||||
|
if len(description) >= min:
|
||||||
|
break
|
||||||
|
print("description needs to be longer than {} characters".format(min))
|
||||||
|
return description
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
tasks = read_db()
|
||||||
|
tasks = read_csv()
|
||||||
|
while True:
|
||||||
|
action = ask_for_action()
|
||||||
|
if action == 1:
|
||||||
|
description = ask_for_description()
|
||||||
|
priority = ask_for_priority()
|
||||||
|
task = {
|
||||||
|
"description": description,
|
||||||
|
"done": "1",
|
||||||
|
"priority": priority,
|
||||||
|
}
|
||||||
|
add_task(task, tasks)
|
||||||
|
elif action == 2:
|
||||||
|
show_tasks(tasks)
|
||||||
|
index = ask_for_index()
|
||||||
|
delete_task(index, tasks)
|
||||||
|
show_tasks(tasks)
|
||||||
|
elif action == 3:
|
||||||
|
show_tasks(tasks)
|
||||||
|
index = ask_for_index()
|
||||||
|
mark_task_done(index, tasks)
|
||||||
|
show_tasks(tasks)
|
||||||
|
elif action == 4:
|
||||||
|
show_tasks(tasks)
|
||||||
|
elif action == 5:
|
||||||
|
write_csv(tasks)
|
||||||
|
print("bye bye!")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
```
|
||||||
|
|
||||||
Can you create me a *task manager* please?
|
Can you create me a *task manager* please?
|
||||||
I made one that is run from the command line with different arguments to modify it's behaviour.
|
I made one that is run from the command line with different arguments to modify it's behaviour.
|
||||||
If this looks too challenging I can tell you that the *full code* is 64 lines long, including empty lines!
|
If this looks too challenging I can tell you that the *full code* is 64 lines long, including empty lines!
|
||||||
|
|
Loading…
Reference in New Issue