From 49c01b753f21584bb13cf53cfa541056c38e3e82 Mon Sep 17 00:00:00 2001 From: Tim Lucas Date: Sat, 23 Mar 2024 15:41:31 +1100 Subject: [PATCH 1/3] Make paths relative --- main.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 8f90e32..1f09fc3 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,17 @@ +from pathlib import Path +import sys + def main(): - history = "/home/chrisstirrup/workspace/github.com/ChrisStirrup/bookbot/books/History.txt" - shelly = "/home/chrisstirrup/workspace/github.com/ChrisStirrup/bookbot/books/frankenstein.txt" - book_path = input('Please enter a path to your .txt document: ') + shelly = Path(__file__).parent / "books/frankenstein.txt" + history = Path(__file__).parent / "books/History.txt" + + book_path = input('Please enter which document to analyze. \'shelly\' or \'history\':') if book_path == 'shelly' or book_path == '': book_path = shelly - if book_path == 'history': + elif book_path == 'history': book_path = history - + else: + sys.exit("Invalid book name") text = get_book_text(book_path) print("Book Report") From f0a397a090c546e96a8eac73216f4bb3138ee687 Mon Sep 17 00:00:00 2001 From: Tim Lucas Date: Sat, 23 Mar 2024 15:42:49 +1100 Subject: [PATCH 2/3] Make error message more useful --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 1f09fc3..4daa3f2 100644 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ def main(): elif book_path == 'history': book_path = history else: - sys.exit("Invalid book name") + sys.exit("Invalid book name. Please type 'shelly' or 'history'.") text = get_book_text(book_path) print("Book Report") From bc7d57548cf0ccc15a97be9c501f6e5e408c8686 Mon Sep 17 00:00:00 2001 From: Tim Lucas Date: Sat, 23 Mar 2024 16:00:21 +1100 Subject: [PATCH 3/3] Accep the path as a command line arg --- main.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/main.py b/main.py index 4daa3f2..4e95c2a 100644 --- a/main.py +++ b/main.py @@ -1,19 +1,18 @@ -from pathlib import Path +import argparse +import os.path import sys def main(): - shelly = Path(__file__).parent / "books/frankenstein.txt" - history = Path(__file__).parent / "books/History.txt" - - book_path = input('Please enter which document to analyze. \'shelly\' or \'history\':') - if book_path == 'shelly' or book_path == '': - book_path = shelly - elif book_path == 'history': - book_path = history - else: - sys.exit("Invalid book name. Please type 'shelly' or 'history'.") - - text = get_book_text(book_path) + parser = argparse.ArgumentParser( + prog='BookBot', + description='Parses text files and produces a letter frequency report') + parser.add_argument("book_path", help="path to the text file") + args = parser.parse_args() + + if not os.path.isfile(args.book_path): + sys.exit(f"Error: path '{args.book_path}' does not exist") + + text = get_book_text(args.book_path) print("Book Report") print(f"There are {word_count(text)} words found in this document") for thing in sort_letter_frequency(text):