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

Hina/task3 #4

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
## Weather Task

29 changes: 22 additions & 7 deletions file_path_matcher_with_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from collections import namedtuple


months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']


class FilePathMatcherWithDate:
class FilePathsProviderMatchedWithDate:

def __init__(self, data_folder_path) -> None:
self.month = None
Expand All @@ -19,13 +19,13 @@ def setDate(self, date):
splitted_date = date.split("/") # date = yyyy/m

if len(splitted_date) > 1:
self.month = splitted_date[1]
self.month = Months[int(splitted_date[1]) - 1]
self.year = splitted_date[0]

def get_files_path(self):
def get_matched_files_path(self):

if self.month and self.year:
return ""
return self.get_specific_month_year_file_paths()
elif self.year:
return self.get_specific_year_file_paths()
else:
Expand All @@ -35,15 +35,15 @@ def get_specific_year_file_paths(self):
file_paths_of_a_year = []

for file_name in os.listdir(self.data_folder_path):
file_year = self.get_file_month_year(file_name).year
file_year = self.get_file_month_year_from(file_name).year

if self.year == file_year:
file_paths_of_a_year.append(
os.path.join(self.data_folder_path, file_name))

return file_paths_of_a_year

def get_file_month_year(self, filename):
def get_file_month_year_from(self, filename):
matched_result = list(re.finditer(
r'(?P<year>\d+)_(?P<month>\w+)', filename))

Expand All @@ -56,3 +56,18 @@ def get_file_month_year(self, filename):
return date_object

return DateObj()

def get_specific_month_year_file_paths(self):
file_paths_of_a_month_in_year = []

for file_name in os.listdir(self.data_folder_path):
file_date = self.get_file_month_year_from(file_name)
file_month, file_year = file_date.month, file_date.year

if self.month == file_month and self.year == file_year:
file_paths_of_a_month_in_year.append(
os.path.join(self.data_folder_path, file_name))
break
# since the given month of the given year has only 1 file

return file_paths_of_a_month_in_year
104 changes: 89 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,103 @@
import sys
import os
from file_path_matcher_with_date import FilePathMatcherWithDate
from report_generator import ReportGenerator
from file_path_matcher_with_date import FilePathsProviderMatchedWithDate
from weather_statistics_calculator import WeatherStatisticsCalculator

CURRENT_DIRECTORY = os.getcwd()
MIN_REQUIRED_ARGS = 4


def isNotValidLengthOfARGS(arguments):
return len(arguments) < MIN_REQUIRED_ARGS


def isNotValidDirectoryPath(folder_path):
print(folder_path)
return not os.path.isdir(folder_path)


def isOdd(args):
return len(args) % 2 != 0


def generate_report(flag, value, data_folder):
match flag:
case '-e':
generateReportHighLowTempHumidity(value, data_folder)
case '-a':
generateReportAverageTempHumidity(value, data_folder)
case '-c':
generateTemperatureChartReport(value, data_folder)
case '-cs':
generateTemperatureChartReport(
value, data_folder, single_chart=True)
case _:
print("No flag sent")


def get_statistics_calculator_instance(date, data_folder):

file_paths_provider = FilePathsProviderMatchedWithDate(data_folder)
file_paths_provider.setDate(date)
matched_file_paths_with_year = file_paths_provider.get_matched_files_path()

statistics_calculator = WeatherStatisticsCalculator(
matched_file_paths_with_year)
return statistics_calculator


def generateReportHighLowTempHumidity(year, data_folder):

statistics_calculator = get_statistics_calculator_instance(
year, data_folder)
report_data = statistics_calculator.calc_low_and_high_temp_and_humidity()

generate_report = ReportGenerator()
generate_report.highest_lowest_temp_and_humidity(report_data)


def generateReportAverageTempHumidity(date, data_folder): # date = year/month

statistics_calculator = get_statistics_calculator_instance(
date, data_folder)
report_data = statistics_calculator.calc_average_temp_and_humidity()

generate_report = ReportGenerator()
generate_report.average_max_min_temp_and_mean_humidity(report_data)


# date = year/month
def generateTemperatureChartReport(date, data_folder, single_chart=False):

statistics_calculator = get_statistics_calculator_instance(
date, data_folder)
report_data = statistics_calculator.get_charts_data()

generate_report = ReportGenerator()
if single_chart:
generate_report.high_low_temperature_single_chart(report_data)
else:
generate_report.high_low_temperature_charts(report_data)


if __name__ == "__main__":

if len(sys.argv) < 4:
if isNotValidLengthOfARGS(sys.argv):
print("You must specify files path and the command with proper flag")
sys.exit(0)

weather_data_files_path = CURRENT_DIRECTORY + sys.argv[1]
if not os.path.isdir(weather_data_files_path):
print("Invalid path to directory : ", weather_data_files_path)
weather_data_folder_path = CURRENT_DIRECTORY + sys.argv[1]
if isNotValidDirectoryPath(weather_data_folder_path):
print("Invalid path to directory : ", weather_data_folder_path)
sys.exit(0)

print(weather_data_files_path)
arg_flags_with_values = sys.argv[2:]

system_args = sys.argv[2:]

year = sys.argv[3]

file_path_matcher = FilePathMatcherWithDate(weather_data_files_path)
file_path_matcher.setDate(year)
matched_file_paths_with_year = file_path_matcher.get_files_path()
if isOdd(arg_flags_with_values):
print("All flags must have values")
sys.exit(0)

# for index, argument in enumerate(system_args):
# pass
for i in range(0, (len(arg_flags_with_values) // 2) + 1, 2):
flag, value = arg_flags_with_values[i], arg_flags_with_values[i + 1]
generate_report(flag, value, weather_data_folder_path)
139 changes: 139 additions & 0 deletions report_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@


from typing import List, Optional
from report_types import AverageTempHumidReportType
from report_types import HighLowTempHumidityReportType
from weather_record import WeatherRecord


Months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']


class ReportGenerator:

def highest_lowest_temp_and_humidity(self, report_data:
Optional[
HighLowTempHumidityReportType
]):

print("\n================== Report =================")
print("================== Highest and Lowest Temperature and Maximum"
" Humidity =================")

if not report_data:
print("\nThere is no data for generating a report\n")
return

highest_temp = report_data.highest_temperature
lowest_temp = report_data.lowest_temperature
high_humid = report_data.high_humidity

print(
f"Highest Temperature: {highest_temp.max_temp}°C on "
f"{self.format_date_from(highest_temp.date)}")
print(
f"Lowest Temperature: {lowest_temp.min_temp}°C on "
f"{self.format_date_from(lowest_temp.date)}")
print(
f"Highest Humidity: {high_humid.max_humidity}% on "
f"{self.format_date_from(high_humid.date)}\n")

def format_date_from(self, date):
year, month, day = date.split("-")
return f"{Months[int(month) - 1]} {day}, {year}"

def average_max_min_temp_and_mean_humidity(self, report_data:
Optional[
AverageTempHumidReportType
]):
print("\n================== Report =================")
print("================== Average Highest and Lowest Temperature and"
" Mean Humidity =================")

if not report_data:
print("\nThere is no data for generating a report\n")
return

average_highest_temp = report_data.average_max_temperature
average_low_temp = report_data.average_min_temperature
average_humid = report_data.average_mean_humidity

print(
f"Highest Average Temperature: {average_highest_temp}°C")
print(
f"Lowest Average Temperature: {average_low_temp}°C")
print(
f"Average Mean Humidity: {average_humid}%\n")

def get_date_from(self, dateString):
year, month, day = dateString.split("-")
return (day, month, year)

def high_low_temperature_charts(self, report_data: List[WeatherRecord]):

print("\n================== Report =================")

if not report_data:
print("\nThere are no data to draw the charts\n")

_, month, year = self.get_date_from(report_data[0].date)
print("================== Temperature Charts"
f" {Months[int(month) - 1]}, {year} =================")

for record in report_data:
self.draw_2_charts_of_high_low_temp(record)

def high_low_temperature_single_chart(self, report_data:
List[WeatherRecord]):

print("\n================== Report =================")

if not report_data:
print("\nThere are no data to draw the charts\n")

_, month, year = self.get_date_from(report_data[0].date)
print("================== Temperature Charts"
f" {Months[int(month) - 1]}, {year} =================")

for record in report_data:
self.draw_1_chart_of_high_low_temp(record)

def draw_2_charts_of_high_low_temp(self, record: WeatherRecord):
day = self.get_date_from(record.date)[0]
formatted_day = self.append_zero_to_start_in_day(day)

print(colors.RED + formatted_day, end=" ")
self.draw_temperature_chart(record.max_temp)
print(f" {record.max_temp}°C")

print(colors.BLUE + formatted_day, end=" ")
self.draw_temperature_chart(record.min_temp)
print(f" {record.min_temp}°C")

def draw_1_chart_of_high_low_temp(self, record: WeatherRecord):
day = self.get_date_from(record.date)[0]
formatted_day = self.append_zero_to_start_in_day(day)

print(formatted_day, end=" ")
print(colors.BLUE, end="")
self.draw_temperature_chart(record.min_temp)
print(colors.RED, end="")
self.draw_temperature_chart(record.max_temp)
print(colors.ENDC, end="")
print(f" {record.min_temp}°C - {record.max_temp}°C")

def draw_temperature_chart(self, tempString):
print("+" * self.get_integer_value_from(tempString), end="")

def append_zero_to_start_in_day(self, day):
return '0' + day if int(day) < 10 else day

def get_integer_value_from(self, string):
return int(string) if string else 0


class colors:
BLUE = '\033[96m'
RED = '\033[91m'
ENDC = '\033[0m'
16 changes: 16 additions & 0 deletions report_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from collections import namedtuple

HighLowTempHumidityReportType = namedtuple(
"HighestLowestTemperatureHumidityReport", [
'highest_temperature',
'lowest_temperature',
'high_humidity'],
defaults=['', '', ''])


AverageTempHumidReportType = namedtuple(
"AverageTempHumidityReportType", [
'average_max_temperature',
'average_min_temperature',
'average_mean_humidity'],
defaults=['', '', ''])
Loading