From 13512a44cff3a26b0f1c7b9639fd0e1195d32a4a Mon Sep 17 00:00:00 2001 From: Sadra Sabouri Date: Mon, 14 Oct 2024 23:36:38 -0700 Subject: [PATCH 1/4] fix : `--verbose` flag added to CLI. --- samila/__main__.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/samila/__main__.py b/samila/__main__.py index d0b29c3..82dd414 100644 --- a/samila/__main__.py +++ b/samila/__main__.py @@ -19,6 +19,7 @@ def main(): parser = argparse.ArgumentParser() parser.add_argument('--version', help='version', action='store_true', default=False) parser.add_argument('--info', help='info', action='store_true', default=False) + parser.add_argument('--verbose', help='verbose', action='store_true', default=False) parser.add_argument('--load-config', help='load config', type=str) parser.add_argument('--load-data', help='load data', type=str) @@ -76,6 +77,8 @@ def main(): data=args.load_data, config=args.load_config, ) + if args.verbose: + print("[LOG] GenerativeImage object created.") gi.generate( seed=args.seed, start=args.start, @@ -83,6 +86,8 @@ def main(): stop=args.stop, mode=args.mode, ) + if args.verbose: + print("[LOG] GenerativeImage generated.") gi.plot( color=args.color, bgcolor=args.bgcolor, @@ -95,15 +100,23 @@ def main(): linewidth=args.linewidth, rotation=args.rotation, ) + if args.verbose: + print("[LOG] GenerativeImage plotted.") if not args.no_display: plt.show() if args.save_image: - gi.save_image(args.save_image, args.depth) + result = gi.save_image(args.save_image, args.depth) + if args.verbose: + print("[LOG] Image save. Result: ", result) if args.save_data: - gi.save_data(args.save_data) + result = gi.save_data(args.save_data) + if args.verbose: + print("[LOG] Data save. Result: ", result) if args.save_config: - gi.save_config(args.save_config) + result= gi.save_config(args.save_config) + if args.verbose: + print("[LOG] Config save. Result: ", result) if __name__ == "__main__": From b1412676668577dd28e36679c762f4974dcbf0d4 Mon Sep 17 00:00:00 2001 From: Sadra Sabouri Date: Mon, 14 Oct 2024 23:40:28 -0700 Subject: [PATCH 2/4] edit : `init_argparse` function separated from `main`. --- samila/__main__.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/samila/__main__.py b/samila/__main__.py index 82dd414..dd033b8 100644 --- a/samila/__main__.py +++ b/samila/__main__.py @@ -10,11 +10,11 @@ from .genimage import GenerativeImage -def main(): +def init_argparse(): """ - CLI main function. + Initialize argparse. - :return: None + :return: parser """ parser = argparse.ArgumentParser() parser.add_argument('--version', help='version', action='store_true', default=False) @@ -61,6 +61,17 @@ def main(): parser.add_argument('--save-data', help='save data', type=str) parser.add_argument('--save-config', help='save config', type=str) parser.add_argument('--no-display', help='no display', action='store_true', default=False) + + return parser + + +def main(): + """ + CLI main function. + + :return: None + """ + parser = init_argparse() args = parser.parse_args() if args.version: From adf6c714d2bc8a9ca12382c2adfbc8f348111da6 Mon Sep 17 00:00:00 2001 From: Sadra Sabouri Date: Tue, 15 Oct 2024 11:55:07 -0700 Subject: [PATCH 3/4] apply : Sepand's comments applied. --- samila/__main__.py | 24 ++++++++++++++++++------ samila/params.py | 10 ++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/samila/__main__.py b/samila/__main__.py index dd033b8..d6ac7a8 100644 --- a/samila/__main__.py +++ b/samila/__main__.py @@ -6,6 +6,9 @@ from art import tprint from .params import SAMILA_VERSION, GenerateMode, Projection, Marker +from .params import LOG_GI_CREATED, LOG_GI_GENERATED, LOG_GI_PLOTTED +from .params import LOG_IMG_SAVED, LOG_DATA_SAVED, LOG_CONFIG_SAVED +from .params import ERR_IMG_SAVE_FAILED, ERR_DATA_SAVE_FAILED, ERR_CONFIG_SAVE_FAILED from .functions import samila_help from .genimage import GenerativeImage @@ -89,7 +92,7 @@ def main(): config=args.load_config, ) if args.verbose: - print("[LOG] GenerativeImage object created.") + print(LOG_GI_CREATED) gi.generate( seed=args.seed, start=args.start, @@ -98,7 +101,7 @@ def main(): mode=args.mode, ) if args.verbose: - print("[LOG] GenerativeImage generated.") + print(LOG_GI_GENERATED) gi.plot( color=args.color, bgcolor=args.bgcolor, @@ -112,22 +115,31 @@ def main(): rotation=args.rotation, ) if args.verbose: - print("[LOG] GenerativeImage plotted.") + print(LOG_GI_PLOTTED) if not args.no_display: plt.show() if args.save_image: result = gi.save_image(args.save_image, args.depth) if args.verbose: - print("[LOG] Image save. Result: ", result) + if result['status']: + print(LOG_IMG_SAVED.format(result['message'])) + else: + print(ERR_IMG_SAVE_FAILED.format(result['message'])) if args.save_data: result = gi.save_data(args.save_data) if args.verbose: - print("[LOG] Data save. Result: ", result) + if result['status']: + print(LOG_DATA_SAVED.format(result['message'])) + else: + print(ERR_DATA_SAVE_FAILED.format(result['message'])) if args.save_config: result= gi.save_config(args.save_config) if args.verbose: - print("[LOG] Config save. Result: ", result) + if result['status']: + print(LOG_CONFIG_SAVED.format(result['message'])) + else: + print(ERR_CONFIG_SAVE_FAILED.format(result['message'])) if __name__ == "__main__": diff --git a/samila/params.py b/samila/params.py index e78d301..af6d619 100644 --- a/samila/params.py +++ b/samila/params.py @@ -56,6 +56,16 @@ COLOR_NOT_FOUND_WARNING = "color '{0}' not found. Replacing it with '{1}'" DEPRECATION_WARNING = "`{}` is deprecated and may be removed in future releases." +LOG_GI_CREATED = "[LOG] GenerativeImage object created." +LOG_GI_GENERATED = "[LOG] GenerativeImage generated." +LOG_GI_PLOTTED = "[LOG] GenerativeImage plotted." +LOG_IMG_SAVED = "[LOG] Image saved. File address: {0}" +LOG_DATA_SAVED = "[LOG] Data saved. File address: {0}" +LOG_CONFIG_SAVED = "[LOG] Config saved. File address: {0}" +ERR_IMG_SAVE_FAILED = "[ERROR] Image save failed. Message: {0}" +ERR_DATA_SAVE_FAILED = "[ERROR] Data save failed. Message: {0}" +ERR_CONFIG_SAVE_FAILED = "[ERROR] Config save failed. Message: {0}" + class GenerateMode(Enum): """ From 109ab258668932ca6ca93e63f90b7e6f9f1d0de0 Mon Sep 17 00:00:00 2001 From: Sadra Sabouri Date: Tue, 15 Oct 2024 13:13:51 -0700 Subject: [PATCH 4/4] fix : `main` function became simpler. --- samila/__main__.py | 63 ++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/samila/__main__.py b/samila/__main__.py index d6ac7a8..1bb6678 100644 --- a/samila/__main__.py +++ b/samila/__main__.py @@ -68,6 +68,27 @@ def init_argparse(): return parser +def log_results(is_verbose, result, log_success, log_fail): + """ + Save result function. + + :param is_verbose: is verbose + :type is_verbose: bool + :param result: result dictionary + :type result: dict + :param log_success: log for successful result + :type log_success: str + :param log_fail: log for failed result + :type log_fail: str + :return: None + """ + if is_verbose: + if result['status']: + print(log_success.format(result['message'])) + else: + print(log_fail.format(result['message'])) + + def main(): """ CLI main function. @@ -85,34 +106,22 @@ def main(): samila_help() else: gi = GenerativeImage( - function1=args.function1, - function2=args.function2, - func_seed=args.function_seed, - data=args.load_data, - config=args.load_config, + function1=args.function1, function2=args.function2, + func_seed=args.function_seed, data=args.load_data, config=args.load_config, ) if args.verbose: print(LOG_GI_CREATED) gi.generate( seed=args.seed, - start=args.start, - step=args.step, - stop=args.stop, + start=args.start, step=args.step, stop=args.stop, mode=args.mode, ) if args.verbose: print(LOG_GI_GENERATED) gi.plot( - color=args.color, - bgcolor=args.bgcolor, - cmap=args.cmap, - spot_size=args.spot_size, - size=args.size, - projection=args.projection, - marker=args.marker, - alpha=args.alpha, - linewidth=args.linewidth, - rotation=args.rotation, + color=args.color, bgcolor=args.bgcolor, cmap=args.cmap, spot_size=args.spot_size, + size=args.size, projection=args.projection, marker=args.marker, alpha=args.alpha, + linewidth=args.linewidth, rotation=args.rotation, ) if args.verbose: print(LOG_GI_PLOTTED) @@ -121,25 +130,13 @@ def main(): if args.save_image: result = gi.save_image(args.save_image, args.depth) - if args.verbose: - if result['status']: - print(LOG_IMG_SAVED.format(result['message'])) - else: - print(ERR_IMG_SAVE_FAILED.format(result['message'])) + log_results(args.verbose, result, LOG_IMG_SAVED, ERR_IMG_SAVE_FAILED) if args.save_data: result = gi.save_data(args.save_data) - if args.verbose: - if result['status']: - print(LOG_DATA_SAVED.format(result['message'])) - else: - print(ERR_DATA_SAVE_FAILED.format(result['message'])) + log_results(args.verbose, result, LOG_DATA_SAVED, ERR_DATA_SAVE_FAILED) if args.save_config: result= gi.save_config(args.save_config) - if args.verbose: - if result['status']: - print(LOG_CONFIG_SAVED.format(result['message'])) - else: - print(ERR_CONFIG_SAVE_FAILED.format(result['message'])) + log_results(args.verbose, result, LOG_CONFIG_SAVED, ERR_CONFIG_SAVE_FAILED) if __name__ == "__main__":