From 61b87c09e9cba7a2ab39c09aa6942746a9a623f0 Mon Sep 17 00:00:00 2001 From: Alireza Aghamohammadi Date: Sat, 23 Dec 2023 17:25:21 +0330 Subject: [PATCH] Fixed logging in pysolorie - Fixed the `logger_decorator` in `logger.py` to set the logger on `self` instead of a local variable. - Removed unnecessary `import logging` statements in `plotter.py` and `report.py`. - Added the `logger_decorator` to the `_calculate_optimal_orientations` method in `plotter.py` and the `_calculate_optimal_orientation_and_irradiation` method in `report.py`. - Replaced the local `logger` instances in `plotter.py` and `report.py` with `self.logger`. - Added `# type: ignore` comments to suppress mypy errors related to the dynamic `logger` attribute. --- src/pysolorie/logger.py | 8 ++++---- src/pysolorie/plotter.py | 5 ++--- src/pysolorie/report.py | 8 +++----- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/pysolorie/logger.py b/src/pysolorie/logger.py index 6c6d431..76e8f54 100644 --- a/src/pysolorie/logger.py +++ b/src/pysolorie/logger.py @@ -24,11 +24,11 @@ def logger_decorator(func): @functools.wraps(func) def wrapper(self, *args, **kwargs): - logger = logging.getLogger(func.__name__) - logger.setLevel(logging.INFO) - logger.info(f"Running '{func.__name__}'") + self.logger = logging.getLogger(func.__name__) + self.logger.setLevel(logging.INFO) + self.logger.info(f"Running '{func.__name__}'") result = func(self, *args, **kwargs) - logger.info(f"Finished '{func.__name__}'") + self.logger.info(f"Finished '{func.__name__}'") return result return wrapper diff --git a/src/pysolorie/plotter.py b/src/pysolorie/plotter.py index e9c9f5d..14d82f3 100644 --- a/src/pysolorie/plotter.py +++ b/src/pysolorie/plotter.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import logging from pathlib import Path from typing import Dict, List, Optional, Tuple @@ -109,6 +108,7 @@ def plot_total_direct_irradiation( self._plot(days, total_direct_irradiations, path, plot_kwargs, savefig_kwargs) + @logger_decorator def _calculate_optimal_orientations( self, irradiation_calculator: IrradiationCalculator, from_day: int, to_day: int ) -> Tuple[List[int], List[float]]: @@ -117,8 +117,7 @@ def _calculate_optimal_orientations( for day in range(from_day, to_day): beta = irradiation_calculator.find_optimal_orientation(day) - logger = logging.getLogger(self._calculate_optimal_orientations.__name__) - logger.info( + self.logger.info( # type: ignore f"On day {day}," + f"the solar panel's optimal orientation is {beta} degrees." ) diff --git a/src/pysolorie/report.py b/src/pysolorie/report.py index 03f0fca..87055aa 100644 --- a/src/pysolorie/report.py +++ b/src/pysolorie/report.py @@ -14,7 +14,6 @@ import csv import json -import logging import xml.etree.ElementTree as ET from pathlib import Path from typing import Dict, List, Union @@ -29,6 +28,7 @@ class ReportGenerator: of solar panels and the total direct irradiation. """ + @logger_decorator def _calculate_optimal_orientation_and_irradiation( self, irradiation_calculator: IrradiationCalculator, @@ -57,10 +57,8 @@ def _calculate_optimal_orientation_and_irradiation( total_direct_irradiation = ( irradiation_calculator.calculate_direct_irradiation(beta, day) ) - logger = logging.getLogger( - self._calculate_optimal_orientation_and_irradiation.__name__ - ) - logger.info( + + self.logger.info( # type: ignore f"On day {day}, the solar panel's optimal orientation is " f"{beta} degrees, and the total direct irradiation is " f"{total_direct_irradiation} Megajoules per square meter."