From eb83402e10508cdfdf9ee5dc24d2fe8af504e0ed Mon Sep 17 00:00:00 2001 From: Mahhheshh <100200105+Mahhheshh@users.noreply.github.com> Date: Sun, 26 Nov 2023 16:03:14 +0000 Subject: [PATCH 1/3] handle file not exists error --- lesp/autocorrect.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/lesp/autocorrect.py b/lesp/autocorrect.py index c3c7d8a..6f1324e 100644 --- a/lesp/autocorrect.py +++ b/lesp/autocorrect.py @@ -1,18 +1,29 @@ import concurrent.futures +# from os import exit def load_config(): - with open("config", "r") as f: - config = f.read() - return config + try: + with open("config", "r") as f: + config = f.read() + return config + except FileNotFoundError: + raise FileNotFoundError(f"Config File not found!") def load_wordlist(): - with open(wordlistpath, "r") as f: - wordlist = f.read().split("\n") - return wordlist + try: + with open(wordlistpath, "r") as f: + wordlist = f.read().split("\n") + return wordlist + except FileNotFoundError: + raise FileNotFoundError(f"{wordlistpath} not found!") -config = load_config() -wordlistpath = config.split("wordlist=\"")[1].split("\"")[0] -wordlist = load_wordlist() +try: + config = load_config() + wordlistpath = config.split("wordlist=\"")[1].split("\"")[0] + wordlist = load_wordlist() +except FileNotFoundError as error: + print(error) + exit() def is_correct(word): if word in wordlist: From a63bff84d27b50b1d013c78a4c3b65707113214b Mon Sep 17 00:00:00 2001 From: Mahhheshh <100200105+Mahhheshh@users.noreply.github.com> Date: Sun, 26 Nov 2023 16:05:37 +0000 Subject: [PATCH 2/3] remove commented import --- lesp/autocorrect.py | 1 - 1 file changed, 1 deletion(-) diff --git a/lesp/autocorrect.py b/lesp/autocorrect.py index 6f1324e..9f9b1d3 100644 --- a/lesp/autocorrect.py +++ b/lesp/autocorrect.py @@ -1,5 +1,4 @@ import concurrent.futures -# from os import exit def load_config(): try: From d8eb062c40880fba716ba2f76d9f7194c82f6973 Mon Sep 17 00:00:00 2001 From: Mahhheshh <100200105+Mahhheshh@users.noreply.github.com> Date: Sun, 26 Nov 2023 17:33:19 +0000 Subject: [PATCH 3/3] update demo.py --- demo.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/demo.py b/demo.py index 955f247..7ae93f5 100644 --- a/demo.py +++ b/demo.py @@ -1,11 +1,18 @@ from lesp.autocorrect import is_correct, get_similar def load_config(): - with open("demo_config", "r") as f: - config = f.read() - return config + try: + with open("demo_config", "r") as f: + config = f.read() + return config + except FileNotFoundError: + raise FileNotFoundError("demo_config not found!") -config = load_config() +try: + config = load_config() +except FileNotFoundError as error: + print(error) + exit() # showallsimilarities="True" showallsimilarities = config.split("showallsimilarities=\"")[1].split("\"")[0]