Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logger Updated #182

Merged
merged 13 commits into from
Nov 4, 2024
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 6 additions & 19 deletions pyrgg/engines/erdos_reyni.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
28 changes: 7 additions & 21 deletions pyrgg/engines/erdos_reyni_gilbert.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
53 changes: 16 additions & 37 deletions pyrgg/engines/pyrgg.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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)
23 changes: 23 additions & 0 deletions pyrgg/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion test/engines/erdos_reyni_gilbert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading