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/task2 #3

Open
wants to merge 3 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
88 changes: 73 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,87 @@
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':
pass
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()
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
file_paths_provider = FilePathsProviderMatchedWithDate(data_folder)
file_paths_provider.setDate(date)
matched_file_paths_with_date = file_paths_provider.get_matched_files_path()

statistics_calculator = get_statistics_calculator_instance
report_data = statistics_calculator.calc_average_temp_and_humidity()

generate_report = ReportGenerator()
generate_report.average_max_min_temp_and_mean_humidity(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, 2):
flag, value = arg_flags_with_values[i], arg_flags_with_values[i + 1]
generate_report(flag, value, weather_data_folder_path)
66 changes: 66 additions & 0 deletions report_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@


from typing import Optional
from report_types import AverageTempHumidReportType
from report_types import HighLowTempHumidityReportType


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.max_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")
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=['', '', ''])
81 changes: 0 additions & 81 deletions stat.ipynb

This file was deleted.

7 changes: 2 additions & 5 deletions weather_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ def __init__(self, record_item) -> None:
self.date = record_item['PKT']
self.min_temp = record_item['Mean TemperatureC']
self.max_temp = record_item['Max TemperatureC']
self.avg_temp = record_item['Min Temperature']
self.mean_temp = record_item['Min TemperatureC']
self.min_humidity = record_item[' Min Humidity']
self.max_humidity = record_item['Max Humidity']
self.avg_humidity = record_item[' Mean Humidity']

def __lt__(self, nxt):
return self.avg_temp < nxt.avg_temp
self.mean_humidity = record_item[' Mean Humidity']
Loading