From 7efac2f9aced26add0a7fb35e538a5696a50ca2b Mon Sep 17 00:00:00 2001 From: reese sapphire Date: Thu, 29 Jun 2023 13:43:05 -0500 Subject: [PATCH 1/2] =?UTF-8?q?add=20shopping=20list=20script=20(python=20?= =?UTF-8?q?=F0=9F=90=8D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/shopping | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100755 scripts/shopping diff --git a/scripts/shopping b/scripts/shopping new file mode 100755 index 0000000..90e7487 --- /dev/null +++ b/scripts/shopping @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 + +# record shopping list items + +from sys import argv, exit + +list_file = "wishlist.txt" + + +## utilities + +def die(msg: str): + print(msg) + exit(1) + +def fetch_list() -> list: + try: + with open(list_file, 'r') as f: + return list(map(lambda item: item.strip(), f.readlines())) + except: + die("wuh oh, i can't open the shopping list file!") + +def save_list(the_list: list): + try: + with open(list_file, 'w') as f: + f.write("\n".join(the_list)) + except: + die("oh noes! the shopping list couldn't be saved!") + + +## shopping list management + +def add(item: str): + if len(item) == 0: + die("what do u want to add?") + the_list = fetch_list() + the_list.append(item) + save_list(the_list) + print(f"#{len(the_list)-1}: \"{item}\" added!") + +def remove(index: int): + the_list = fetch_list() + if index < 0 or index >= len(the_list): + die("that item does not exist!") + new_list = filter(lambda item: item != the_list[index], the_list) + save_list(new_list) + print(f"#{index} removed!") + +def show(): + the_list = fetch_list() + for i,item in enumerate(the_list): + print(f"{i}: {item}") + exit(42) + +def usage(): + print("!neko wishlist ") + print("commands:") + print(" add add an item") + print(" remove remove an item") + print(" show show the list") + exit(42) + + +## entry point +if __name__ == '__main__': + cmd = arg = "" + try: + cmd, arg = argv[1].split(' ', 1) + except: + cmd = argv[1] + + match cmd: + case "add": add(arg) + case "remove": remove(int(arg)) + case "show": show() + case _: usage() -- 2.45.2 From 04589322264245d723a9624f04ab489ffa06a3d1 Mon Sep 17 00:00:00 2001 From: reese sapphire Date: Thu, 29 Jun 2023 14:12:33 -0500 Subject: [PATCH 2/2] typo --- scripts/shopping | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/shopping b/scripts/shopping index 90e7487..76b8dd8 100755 --- a/scripts/shopping +++ b/scripts/shopping @@ -53,7 +53,7 @@ def show(): exit(42) def usage(): - print("!neko wishlist ") + print("!neko shopping ") print("commands:") print(" add add an item") print(" remove remove an item") -- 2.45.2