From e9a6262492d8a72e6131d5a9c87134854045243a Mon Sep 17 00:00:00 2001 From: Sadra Sabouri <43045767+sadrasabouri@users.noreply.github.com> Date: Mon, 4 Nov 2024 06:20:27 -0800 Subject: [PATCH] Logger Updated (#182) * add : `log` function added to `functions.py`. * update : `logger` function in engines updated. * update : tests updated. * log : changes logged. * rename : `log` function renamed to `save_log`. * fix : f"" --> .format. * fix : minor bug fixed in erg engine. * fix : changelog fixed. * fix : f"" --> .format in `pyrgg`. * fix : merge issue fixed. * fix : changelog fixed. --------- Co-authored-by: sepandhaghighi --- CHANGELOG.md | 2 + pyrgg/engines/erdos_reyni.py | 25 +++-------- pyrgg/engines/erdos_reyni_gilbert.py | 28 ++++--------- pyrgg/engines/pyrgg.py | 53 +++++++----------------- pyrgg/functions.py | 23 ++++++++++ test/engines/erdos_reyni_gilbert_test.py | 2 +- 6 files changed, 55 insertions(+), 78 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f02ef204..5abf0c23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Added - `pyrgg.engines.erdos_reyni` module +- `save_log` function ### Changed +- `logger` function format for `erdos_reyni_gilbert` changed - GitHub actions are limited to the `dev` and `master` branches - `README.md` modified - `build_exe.bat` modified diff --git a/pyrgg/engines/erdos_reyni.py b/pyrgg/engines/erdos_reyni.py index 746f8708..68d8c29b 100644 --- a/pyrgg/engines/erdos_reyni.py +++ b/pyrgg/engines/erdos_reyni.py @@ -1,18 +1,8 @@ # -*- coding: utf-8 -*- """Erdős-Rényi Engine module.""" -import datetime from random import shuffle from pyrgg.params import ENGINE_MENU, PYRGG_LOGGER_ERROR_MESSAGE - -LOGGER_TEMPLATE = """{0} -Filename : {1} -Vertices : {2} -Total Edges : {3} -Directed : {4} -Engine : {5} ({6}) -Elapsed Time : {7} -------------------------------- -""" +from pyrgg.functions import save_log def edge_gen(n, m, direct): @@ -102,13 +92,10 @@ def logger(file, file_name, elapsed_time, input_dict): :return: None """ try: - file.write(LOGGER_TEMPLATE.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), - file_name, - str(input_dict["vertices"]), - str(input_dict["edge_number"]), - str(bool(input_dict["direct"])), - input_dict["engine"], - ENGINE_MENU[input_dict["engine"]], - elapsed_time)) + text = "Vertices : {0}\n".format(input_dict['vertices']) + text += "Total Edges : {0}\n".format(input_dict['edge_number']) + text += "Directed : {0}\n".format(bool(input_dict['direct'])) + text += "Engine : {0} ({1})\n".format(input_dict['engine'], ENGINE_MENU[input_dict['engine']]) + save_log(file, file_name, elapsed_time, text) except Exception: print(PYRGG_LOGGER_ERROR_MESSAGE) diff --git a/pyrgg/engines/erdos_reyni_gilbert.py b/pyrgg/engines/erdos_reyni_gilbert.py index 4cf34508..14e12f75 100644 --- a/pyrgg/engines/erdos_reyni_gilbert.py +++ b/pyrgg/engines/erdos_reyni_gilbert.py @@ -1,19 +1,8 @@ # -*- coding: utf-8 -*- """Erdős-Rényi-Gilbert Engine module.""" -import datetime from random import random from pyrgg.params import ENGINE_MENU, PYRGG_LOGGER_ERROR_MESSAGE - -LOGGER_TEMPLATE = """{0} -Filename : {1} -Probability : {2} -Vertices : {3} -Total Edges : {4} -Directed : {5} -Engine : {6} ({7}) -Elapsed Time : {8} -------------------------------- -""" +from pyrgg.functions import save_log def edge_gen(n, p, direct): @@ -98,14 +87,11 @@ def logger(file, file_name, elapsed_time, input_dict): :return: None """ try: - file.write(LOGGER_TEMPLATE.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), - file_name, - str(input_dict["probability"]), - str(input_dict["vertices"]), - str(input_dict["edge_number"]), - str(bool(input_dict["direct"])), - input_dict["engine"], - ENGINE_MENU[input_dict["engine"]], - elapsed_time)) + text = "Vertices : {0}\n".format(input_dict['vertices']) + text += "Probability : {0}\n".format(input_dict['probability']) + text += "Total Edges : {0}\n".format(input_dict['edge_number']) + text += "Directed : {0}\n".format(bool(input_dict['direct'])) + text += "Engine : {0} ({1})\n".format(input_dict['engine'], ENGINE_MENU[input_dict['engine']]) + save_log(file, file_name, elapsed_time, text) except Exception: print(PYRGG_LOGGER_ERROR_MESSAGE) diff --git a/pyrgg/engines/pyrgg.py b/pyrgg/engines/pyrgg.py index 028c3752..dcd013f9 100644 --- a/pyrgg/engines/pyrgg.py +++ b/pyrgg/engines/pyrgg.py @@ -1,29 +1,11 @@ # -*- coding: utf-8 -*- """PyRGG Engine module.""" -import datetime import os from random import randint, uniform, choice from pyrgg.params import ENGINE_MENU, PYRGG_LOGGER_ERROR_MESSAGE from pyrgg.functions import is_weighted, get_precision, threshold_calc from pyrgg.functions import get_min_max_weight, is_multigraph - -LOGGER_TEMPLATE = """{0} -Filename : {1} -Vertices : {2} -Total Edges : {3} -Max Edge : {4} -Min Edge : {5} -Directed : {6} -Signed : {7} -Multigraph : {8} -Self Loop : {9} -Weighted : {10} -Max Weight : {11} -Min Weight : {12} -Engine : {13} ({14}) -Elapsed Time : {15} -------------------------------- -""" +from pyrgg.functions import save_log def branch_gen( @@ -265,23 +247,20 @@ def logger(file, file_name, elapsed_time, input_dict): :return: None """ try: - file.write(LOGGER_TEMPLATE.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), - file_name, - str(input_dict["vertices"]), - str(input_dict["edge_number"]), - str(input_dict["max_edge"]), - str(input_dict["min_edge"]), - str(bool(input_dict["direct"])), - str(bool(input_dict["sign"])), - str(bool(input_dict["multigraph"])), - str(bool(input_dict["self_loop"])), - str(is_weighted(input_dict["max_weight"], - input_dict["min_weight"], - bool(input_dict["sign"]))), - str(input_dict["max_weight"]), - str(input_dict["min_weight"]), - input_dict["engine"], - ENGINE_MENU[input_dict["engine"]], - elapsed_time)) + text = "Vertices : {0}\n".format(input_dict['vertices']) + text += "Total Edges : {0}\n".format(input_dict['edge_number']) + text += "Max Edge : {0}\n".format(input_dict['max_edge']) + text += "Min Edge : {0}\n".format(input_dict['min_edge']) + text += "Directed : {0}\n".format(bool(input_dict['direct'])) + text += "Signed : {0}\n".format(bool(input_dict['sign'])) + text += "Multigraph : {0}\n".format(bool(input_dict['multigraph'])) + text += "Self Loop : {0}\n".format(bool(input_dict['self_loop'])) + text += "Weighted : {0}\n".format( + is_weighted(input_dict['max_weight'], input_dict['min_weight'], bool(input_dict['sign']))) + text += "Max Weight : {0}\n".format(input_dict['max_weight']) + text += "Min Weight : {0}\n".format(input_dict['min_weight']) + text += "Engine : {0} ({1})\n".format( + input_dict['engine'], ENGINE_MENU[input_dict['engine']]) + save_log(file, file_name, elapsed_time, text) except Exception: print(PYRGG_LOGGER_ERROR_MESSAGE) diff --git a/pyrgg/functions.py b/pyrgg/functions.py index 3fa27a54..9b85f6ce 100644 --- a/pyrgg/functions.py +++ b/pyrgg/functions.py @@ -6,6 +6,7 @@ from json import dump as json_dump from pickle import dump as pickle_dump from yaml import safe_dump as yaml_dump +import datetime import pyrgg.params @@ -554,3 +555,25 @@ def check_for_config(input_func=input): pyrgg.params.CONFIG_FILE_FORMAT.format("")): configs.append(file) return _print_select_config(configs, input_func) + + +def save_log(file, file_name, elapsed_time, text): + """ + Save generated graph logs. + + :param file: file to write log into + :type file: file object + :param file_name: file name + :type file_name: str + :param elapsed_time: elapsed time + :type elapsed_time: str + :param text: rest part of the text to write + :type text: str + :return: None + """ + text2file = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + "\n" + text2file += "Filename : {0}\n".format(file_name) + text2file += text + text2file += "Elapsed Time : {0}\n".format(elapsed_time) + text2file += "-------------------------------\n" + file.write(text2file) diff --git a/test/engines/erdos_reyni_gilbert_test.py b/test/engines/erdos_reyni_gilbert_test.py index 8c14cb4b..7c47210c 100644 --- a/test/engines/erdos_reyni_gilbert_test.py +++ b/test/engines/erdos_reyni_gilbert_test.py @@ -16,8 +16,8 @@ >>> file = open('logfile.log','r') >>> print("\\n".join(file.read().splitlines()[1:-1])) Filename : test -Probability : 0.5 Vertices : 100 +Probability : 0.5 Total Edges : 50 Directed : False Engine : 2 (erg)