Skip to content

Instantly share code, notes, and snippets.

@rosswf
Last active March 19, 2021 06:46
Show Gist options
  • Save rosswf/13e69482fe2d659a38c702828c76074d to your computer and use it in GitHub Desktop.
Save rosswf/13e69482fe2d659a38c702828c76074d to your computer and use it in GitHub Desktop.
Beginner.py weekly coding prompt 18th March. Build a tool to help people plan ahead for the new season.
import pathlib
import sqlite3
import sys
from rich import box
from rich.align import Align
from rich.console import Console
from rich.prompt import Prompt
from rich.table import Table
DATABASE = "planner.sqlite"
database_path = pathlib.Path(__file__).parent / DATABASE
class Database:
def __init__(self, path):
self.con = sqlite3.connect(path)
self.cur = self.con.cursor()
self.create_table()
def create_table(self):
schema = """CREATE TABLE IF NOT EXISTS todo (
id INTEGER PRIMARY KEY AUTOINCREMENT,
task TEXT NOT NULL,
complete INTEGER DEFAULT 0
);"""
self.cur.execute(schema)
def list_tasks(self, complete=False):
if complete:
query = "SELECT * FROM todo"
else:
query = "SELECT * FROM todo WHERE complete = 0"
tasks = self.cur.execute(query)
return tasks
def insert_task(self, task_name):
query = "INSERT INTO todo (task) VALUES (?)"
self.cur.execute(query, (task_name,))
self.con.commit()
def complete_task(self, task_id):
query = "UPDATE todo SET complete = 1 WHERE id = (?)"
self.cur.execute(query, (task_id,))
self.con.commit()
def delete_task(self, task_id):
query = "DELETE FROM todo WHERE id = (?)"
self.cur.execute(query, (task_id,))
self.con.commit()
class Todo:
def __init__(self, db):
self.db = db
def display_table(self, complete=False):
console = Console()
table = Table(
"ID",
"TASK",
show_header=True,
header_style="bold rgb(42,245,228)",
title="[not italic][bold][rgb(42,245,228)]My Awesome TODO List",
box=box.HORIZONTALS,
)
tasks = self.db.list_tasks(complete)
for task in tasks:
if task[2]:
table.add_row(f"[red][strike]{task[0]}", f"[red][strike]{task[1]}")
else:
table.add_row(str(task[0]), task[1])
console.print(table)
def onboard(self):
first_task = Prompt.ask(
"Welcome! It's your first time here. What is your first task?"
)
self.db.insert_task(first_task)
def add_task(self):
task = Prompt.ask("What would you like to add to your list? ")
self.db.insert_task(task)
def complete_task(self):
task = Prompt.ask("What is the ID of the task you have completed? ")
self.db.complete_task(task)
def delete_task(self):
task = Prompt.ask("What is the ID of the task you would like to delete? ")
self.db.delete_task(task)
def todo_list(self):
self.display_table()
def full_list(self):
self.display_table(True)
if __name__ == "__main__":
db = Database(DATABASE)
todo = Todo(db)
if not todo.db.list_tasks(True).fetchall():
todo.onboard()
todo.display_table()
options = {
"a": todo.add_task,
"c": todo.complete_task,
"d": todo.delete_task,
"t": todo.todo_list,
"f": todo.full_list,
"q": sys.exit,
}
option = ""
while True:
option = Prompt.ask(
"[bold rgb(42,245,228)]a[/]dd a task,"
"[bold rgb(42,245,228)]c[/]omplete a task,"
"[bold rgb(42,245,228)]d[/]elete a task,"
"[bold rgb(42,245,228)]t[/]odo list,"
"[bold rgb(42,245,228)]f[/]ull list,"
"[bold rgb(42,245,228)]q[/]uit",
choices=["a", "c", "d", "t", "f", "q"],
)
options[option]()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment