Skip to content

Commit

Permalink
Fixed logging in pysolorie
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
aaghamohammadi committed Dec 23, 2023
1 parent e482f89 commit 61b87c0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/pysolorie/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 2 additions & 3 deletions src/pysolorie/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]]:
Expand All @@ -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."
)
Expand Down
8 changes: 3 additions & 5 deletions src/pysolorie/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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."
Expand Down

0 comments on commit 61b87c0

Please sign in to comment.