diff --git a/Converter/converter.py b/Converter/converter.py index c21fb90b..8c2ca6ea 100644 --- a/Converter/converter.py +++ b/Converter/converter.py @@ -1,4 +1,4 @@ -from converter_values import * # import required files +from converter_values import options, CATEGORIES def main(): print(options["help"]) # prints help menu @@ -6,14 +6,14 @@ def main(): while res.lower() != "q": # program loop try: - res = res.strip().split(" ") + tokens = res.strip().split(" ") - if len(res) == 1: - display_help(res[0]) # display help menu - elif len(res) == 4: - perform_conversion(res) # perform unit conversion + if len(tokens) == 1: + display_help(tokens[0]) # display help menu + elif len(tokens) == 4: + perform_conversion(tokens) # perform unit conversion else: - print("Invalid command") + print("Invalid command. Format: ") except Exception as e: print("Error:", e) @@ -22,15 +22,44 @@ def main(): def display_help(command): """Display help menu.""" - print(options[command]) + if command in options: + print(options[command]) + else: + print(f"Unknown command '{command}'. Type 'help' or 'symbols'.") def perform_conversion(res): - """Perform unit conversion.""" - for i in res[3].split(','): - value = round(eval("{} * {}['{}'] / {}['{}']".format(res[2], res[0], i, res[0], res[1])), 6) # calculating - print("{} \t : {}".format(i, value)) # displaying + """Perform unit conversion cleanly without unsafe eval.""" + category_code = res[0].upper() + src_unit = res[1] + + if category_code not in CATEGORIES: + print(f"Invalid category '{res[0]}'. Valid categories: {', '.join(CATEGORIES.keys())}") + return + + unit_dict = CATEGORIES[category_code] + + if src_unit not in unit_dict: + print(f"Invalid source unit '{src_unit}' for category '{category_code}'.") + return + + try: + raw_val = float(res[2]) + except ValueError: + print(f"Invalid numeric value '{res[2]}'.") + return + + target_units = [u.strip() for u in res[3].split(',')] + for target in target_units: + if target not in unit_dict: + print(f"Invalid target unit '{target}' for category '{category_code}'.") + continue + + # Calculation: val * unit_dict[target] / unit_dict[src_unit] + calc_value = round((raw_val * unit_dict[target]) / unit_dict[src_unit], 6) + print("{} \t : {}".format(target, calc_value)) if __name__ == "__main__": main() + diff --git a/Converter/converter_values.py b/Converter/converter_values.py index 3cc9c104..976619eb 100644 --- a/Converter/converter_values.py +++ b/Converter/converter_values.py @@ -7,7 +7,7 @@ inch : in kilometer : km meter : m -micrometer ; um +micrometer : um mile : mi millimeter : mm nanometer : nm @@ -21,7 +21,7 @@ sq inch : in2 sq km : km2 sq m : m2 -sq mile : mi +sq mile : mi2 sq yard : yd2 VOLUME : V ---------------------------------------------------------------- @@ -115,3 +115,12 @@ "min":1440 , "sec":86400 } +CATEGORIES = { + "L": L, + "A": A, + "V": V, + "M": M, + "T": T +} + + diff --git a/NASA_Image_Extraction/Astro_Images/2022-11-25_NGC 6744: Extragalactic Close-Up.mp3 b/NASA_Image_Extraction/Astro_Images/2022-11-25_NGC 6744: Extragalactic Close-Up.mp3 deleted file mode 100644 index f5611fcf..00000000 Binary files a/NASA_Image_Extraction/Astro_Images/2022-11-25_NGC 6744: Extragalactic Close-Up.mp3 and /dev/null differ