-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from aaghamohammadi/dev
Add ReportGenerator and Logging
- Loading branch information
Showing
5 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2023 Alireza Aghamohammadi | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import logging | ||
|
||
|
||
def logger_decorator(cls): | ||
class WithLogging(cls): # type: ignore | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.logger = logging.getLogger(cls.__name__) | ||
self.logger.setLevel(logging.INFO) | ||
self.logger.propagate = False | ||
logging.basicConfig( | ||
level=logging.INFO, | ||
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", | ||
) | ||
|
||
return WithLogging |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright 2023 Alireza Aghamohammadi | ||
|
||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
|
||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import csv | ||
from pathlib import Path | ||
|
||
from .logger import logger_decorator | ||
from .numerical_integration import IrradiationCalculator | ||
|
||
|
||
@logger_decorator | ||
class ReportGenerator: | ||
def generate_optimal_orientation_csv_report( | ||
self, | ||
path: Path, | ||
irradiation_calculator: IrradiationCalculator, | ||
from_day: int, | ||
to_day: int, | ||
) -> None: | ||
r""" | ||
This method generates a report of | ||
optimal solar panel orientation in CSV format. | ||
:param path: A Path object that points to the CSV file | ||
where the report will be written. | ||
:type path: Path | ||
:param irradiation_calculator: An instance of the IrradiationCalculator class. | ||
:type irradiation_calculator: pysolorie.IrradiationCalculator | ||
:param from_day: The starting day of the report. | ||
:type from_day: int | ||
:param to_day: The ending day of the report. | ||
:type to_day: int | ||
""" | ||
with open(path, "w", newline="") as file: | ||
writer = csv.writer(file) | ||
writer.writerow(["Day", "Beta (degrees)"]) | ||
|
||
for day in range(from_day, to_day): | ||
beta = irradiation_calculator.find_optimal_orientation(day) | ||
logger_name = "logger" | ||
getattr(self, logger_name).info( | ||
f"On day {day}," | ||
+ "the solar panel's optimal orientation is {beta} degrees." | ||
) | ||
|
||
# Write the result to the CSV file | ||
writer.writerow([day, beta]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters