From 5a714e36173ea495ac55c5c33feef9ff9768b993 Mon Sep 17 00:00:00 2001 From: Vlad Gheorghiu Date: Tue, 10 Sep 2024 10:48:05 -0400 Subject: [PATCH] Updated spell.py Signed-off-by: Vlad Gheorghiu --- spell.py | 72 ++++++++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 39 deletions(-) diff --git a/spell.py b/spell.py index 39a03bc..a1641d7 100644 --- a/spell.py +++ b/spell.py @@ -5,7 +5,9 @@ # Spells text using various alphabets. By default, uses the NATO phonetic # alphabet. # -# Type python3 spell.py --help for help +# Execute +# python3 spell.py --help +# for help # Copyright (c) 2018 - 2024 Vlad Gheorghiu. All rights reserved. # @@ -35,20 +37,30 @@ # Spells a string using a specified dictionary -def spell(string, dic): +def spell(string, dict): for char in string: - if char in dic: - print("{key} | {value}".format(key=char, value=dic[char])) - elif char.upper() in dic: - print("{key} | {value}".format(key=char, value=dic[char.upper()])) - elif char.lower() in dic: - print("{key} | {value}".format(key=char, value=dic[char.lower()])) + if char in dict: + print("{key} | {value}".format(key=char, value=dict[char])) + elif char.upper() in dict: + print("{key} | {value}".format(key=char, value=dict[char.upper()])) + elif char.lower() in dict: + print("{key} | {value}".format(key=char, value=dict[char.lower()])) elif char == "\n": print("----- newline -----") else: print(char) +# Loads a custom dictionary +def load_dict(path): + try: + with open(path, "rt") as f: + return json.load(f) + except (IOError, json.JSONDecodeError) as e: + print(f"Error loading dictionary '{path}': {e}") + sys.exit(-1) + + if __name__ == "__main__": parser = argparse.ArgumentParser( description="Spells text using phonetic alphabet(s)" @@ -61,23 +73,10 @@ def spell(string, dic): ) args = parser.parse_args() - # Custom dictionary - if args.dict is not None: - try: - with open(args.dict, "rt") as f: - try: - dictionary = json.load(f) - except json.decoder.JSONDecodeError as json_exception: - print("Cannot parse the dictionary '{dict}'".format(dict=args.dict)) - print(json_exception) - sys.exit(-1) - except IOError as io_exception: - print("Cannot open the dictionary '{dict}'".format(dict=args.dict)) - print(io_exception) - sys.exit(-1) - else: - # Use the NATO phonetic dictionary by default - dictionary = { + dictionary = ( + load_dict(args.dict) + if args.dict + else { "a": "Alpha", "b": "Bravo", "c": "Charlie", @@ -115,18 +114,13 @@ def spell(string, dic): "9": "Nine", "0": "Zero", } + ) - # Text passed from the command line - if args.text is not None: - spell(args.text, dictionary) - else: - # We have text taken from the standard input - try: - while True: - line = sys.stdin.readline() - # Stops on CTRL+D (UNIX) or CTRL+Z (Windows) - if line == "": - break - spell(line, dictionary) - except KeyboardInterrupt: - sys.exit() + # Text from the command line, or from the standard input + # If from standard input, stops on CTRL+D (UNIX) or CTRL+Z (Windows) + text = args.text or sys.stdin + try: + for line in text: + spell(line, dictionary) + except (KeyboardInterrupt, EOFError): + sys.exit()