From 48394898d0194bfc75c4abe5279ccadca3b9db61 Mon Sep 17 00:00:00 2001 From: HiIAmTzeKean Date: Fri, 29 Nov 2024 20:23:31 +0800 Subject: [PATCH 1/2] fix: logging --- streamsight/__init__.py | 14 +++++--------- streamsight/utils/directory_tools.py | 26 +++++++++++++++++--------- streamsight/utils/util.py | 28 ++++++++++++++++++++-------- 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/streamsight/__init__.py b/streamsight/__init__.py index 1a03e65..85fdc3a 100644 --- a/streamsight/__init__.py +++ b/streamsight/__init__.py @@ -10,16 +10,12 @@ import logging import logging.config -from streamsight.utils import ( - prepare_logger, - log_level, - log_level_by_name, - suppress_warnings, - suppress_specific_warnings, -) +from streamsight.utils import (log_level, log_level_by_name, prepare_logger, + suppress_specific_warnings, suppress_warnings) -LOGGING_CONFIG = "LOGGING_CONFIG.yaml" +LOGGING_CONFIG_FILENAME = "LOGGING_CONFIG.yaml" -prepare_logger(LOGGING_CONFIG) +prepare_logger(LOGGING_CONFIG_FILENAME) logger = logging.getLogger(__name__) +logger.info("streamsight package loaded.") diff --git a/streamsight/utils/directory_tools.py b/streamsight/utils/directory_tools.py index 543e1bc..cca7fe7 100644 --- a/streamsight/utils/directory_tools.py +++ b/streamsight/utils/directory_tools.py @@ -1,11 +1,14 @@ import os +from typing import Final import yaml +LOG_FILE: Final[str] = "streamsight.log" + def safe_dir(path): """Check if directory is safe - + Check if the directory exists, if not create it. :param path: The path to the directory. @@ -15,17 +18,24 @@ def safe_dir(path): os.makedirs(path) -def create_config_yaml(path: str): +def create_config_yaml(config_filename: str): """ Create a configuration file for the logger. - + Writes a default configuration file for the logger in YAML format. The configuration file specifies the format of the log messages, the output stream, and the log level. - + :param path: The name of the file to be created. :type path: str """ + # Get the current working directory + current_directory = os.getcwd() + + # Define the default log file path in the current directory + log_file_path = os.path.join(current_directory, "logs/streamsight.log") + yaml_file_path = os.path.join(current_directory, config_filename) + default_config = { "version": 1, "disable_existing_loggers": False, @@ -33,9 +43,7 @@ def create_config_yaml(path: str): "detailed": { "format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s" }, - "simple": { - "format": "%(levelname)s - %(message)s" - }, + "simple": {"format": "%(levelname)s - %(message)s"}, }, "handlers": { "console": { @@ -48,7 +56,7 @@ def create_config_yaml(path: str): "class": "logging.FileHandler", "level": "DEBUG", "formatter": "detailed", - "filename": "logs/streamsight.log", + "filename": log_file_path, }, }, "loggers": { @@ -65,5 +73,5 @@ def create_config_yaml(path: str): } # Write the YAML content - with open(path, "w") as file: + with open(yaml_file_path, "w") as file: yaml.dump(default_config, file, default_flow_style=False) diff --git a/streamsight/utils/util.py b/streamsight/utils/util.py index 11a53a7..6658bf5 100644 --- a/streamsight/utils/util.py +++ b/streamsight/utils/util.py @@ -3,6 +3,8 @@ import os from typing import Union +import pyfiglet + import numpy as np import progressbar import yaml @@ -117,31 +119,41 @@ def __call__(self, block_num, block_size, total_size): self.pbar.finish() -def prepare_logger(path: str) -> dict: +def prepare_logger(log_config_filename: str) -> dict: """Prepare the logger. Prepare the logger by reading the configuration file and setting up the logger. If the configuration file does not exist, it will be created. - :param path: Path to the configuration file. - :type path: str + :param log_config_filename: Name of configuration file. + :type log_config_filename: str :return: Configuration dictionary. :rtype: dict """ - if not os.path.exists(path): - create_config_yaml(path) + if not os.path.exists(log_config_filename): + create_config_yaml(log_config_filename) try: - with open(path, "r") as stream: + with open(log_config_filename, "r") as stream: config = yaml.load(stream, Loader=yaml.FullLoader) except FileNotFoundError: - raise FileNotFoundError(f"Configuration file not found at {path}.") + raise FileNotFoundError(f"Configuration file not found at {log_config_filename}.") except yaml.YAMLError as e: raise ValueError(f"Error parsing YAML configuration: {e}") - dir_name = os.path.dirname(config["handlers"]["file"]["filename"]) + # Get the log file path from the configuration + log_file = config["handlers"]["file"]["filename"] + + # Ensure the log file directory exists + dir_name = os.path.dirname(log_file) safe_dir(dir_name) + # Write ASCII art to the log file + with open(log_file, "w") as log: + ascii_art = pyfiglet.figlet_format("streamsight") + log.write(ascii_art) + log.write("\n") + logging.config.dictConfig(config) logging.captureWarnings(True) return config From 03985e38d3952d91af3752efc80afa11bb5d608d Mon Sep 17 00:00:00 2001 From: HiIAmTzeKean Date: Fri, 29 Nov 2024 20:23:45 +0800 Subject: [PATCH 2/2] chore: versioning 1.0.0 --- poetry.lock | 13 ++++++++++++- pyproject.toml | 6 ++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/poetry.lock b/poetry.lock index 68b13b5..6a0a1b9 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1122,6 +1122,17 @@ files = [ {file = "pycparser-2.22.tar.gz", hash = "sha256:491c8be9c040f5390f5bf44a5b07752bd07f56edf992381b05c701439eec10f6"}, ] +[[package]] +name = "pyfiglet" +version = "1.0.2" +description = "Pure-python FIGlet implementation" +optional = false +python-versions = ">=3.9" +files = [ + {file = "pyfiglet-1.0.2-py3-none-any.whl", hash = "sha256:889b351d79c99e50a3f619c8f8e6ffdb27fd8c939fc43ecbd7559bd57d5f93ea"}, + {file = "pyfiglet-1.0.2.tar.gz", hash = "sha256:758788018ab8faaddc0984e1ea05ff330d3c64be663c513cc1f105f6a3066dab"}, +] + [[package]] name = "pygments" version = "2.18.0" @@ -2019,4 +2030,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "3.12.5" -content-hash = "c1fff704b4b70aa6a29d0c90f6e4bd361b7baec97307a0c085341042720ef992" +content-hash = "31b34176fbf0f88dd34229d0175ad60e4e1370d9b62616afde9387ebc6b8f939" diff --git a/pyproject.toml b/pyproject.toml index 3a988b8..97e5bbc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,12 +1,12 @@ [tool.poetry] name = "streamsight" -version = "0.2.11" +version = "1.0.0" description = "A toolkit for offline evaluation of Recommender Systems." authors = ["Ng Tze Kean "] readme = "README.md" repository = "https://github.com/HiIAmTzeKean/Streamsight" documentation = "https://hiiamtzekean.github.io/Streamsight/" -keywords = ["recsys", "toolkit", "streamsight", "evaluation"] +keywords = ["recsys", "toolkit", "practical evaluation", "global timeline"] [tool.poetry.dependencies] python = "3.12.5" @@ -22,6 +22,7 @@ progressbar = "^2.5" typing-extensions = "^4.12.2" deprecation = "^2.1.0" sphinxcontrib-pdfembed = {git = "https://github.com/SuperKogito/sphinxcontrib-pdfembed"} +pyfiglet = "^1.0.2" [tool.poetry.group.dev.dependencies] pytest = "^8.3.2" @@ -33,6 +34,7 @@ setuptools = "^73.0.0" sphinx = "7.4.7" sphinx-rtd-theme = "^2.0.0" ipywidgets = "^8.1.5" +pyfiglet = "^1.0.2" [build-system] requires = ["poetry-core"]