-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
class calculations: | ||
|
||
def yearly_required_fields(self, data): | ||
req_data = {} | ||
req_data['max_temp'] = data['max_temp'] | ||
req_data['max_temp_date'] = data['date'] | ||
req_data['min_temp'] = data['min_temp'] | ||
req_data['min_temp_date'] = data['date'] | ||
req_data['max_humidity'] = data['max_humid'] | ||
req_data['max_humidity_date'] = data['date'] | ||
|
||
return req_data | ||
|
||
def yearly_weather_calculation(self, weather_data, date): | ||
default = 0 | ||
req_data = {} | ||
for data in weather_data: | ||
if data['date'].year == date.year: | ||
default += 1 | ||
if default == 1: | ||
req_data = self.yearly_required_fields(data) | ||
else: | ||
if req_data['max_temp'] < data['max_temp']: | ||
req_data['max_temp'] = data['max_temp'] | ||
req_data['max_temp_date'] = data['date'] | ||
|
||
if req_data['min_temp'] > data['min_temp']: | ||
req_data['min_temp'] = data['min_temp'] | ||
req_data['min_temp_date'] = data['date'] | ||
|
||
if req_data['max_humidity'] < data['max_humid']: | ||
req_data['max_humidity'] = data['max_humid'] | ||
req_data['max_humidity_date'] = data['date'] | ||
|
||
return req_data | ||
|
||
def monthly_weather_calculation(self, weather_data, date): | ||
req_data = {} | ||
total_records = 0 | ||
sum_max_temp = 0 | ||
sum_min_temp = 0 | ||
sum_mean_humidity = 0 | ||
for data in weather_data: | ||
if data['date'].year == date.year and data['date'].month == date.month: | ||
total_records += 1 | ||
sum_max_temp += data['max_temp'] | ||
sum_min_temp += data['min_temp'] | ||
sum_mean_humidity += data['mean_humid'] | ||
req_data['avg_max'] = int(sum_max_temp/total_records) | ||
req_data['avg_min'] = int(sum_min_temp/total_records) | ||
req_data['avg_humidity'] = int(sum_mean_humidity/total_records) | ||
|
||
return req_data | ||
|
||
def bar_charts_calculation(self, weather_data, date): | ||
bar_chart_records = [] | ||
for data in weather_data: | ||
if data['date'].year == date.year and data['date'].month == date.month: | ||
bar_chart_records.append(data) | ||
|
||
return bar_chart_records | ||
|
||
|
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import os | ||
import csv | ||
from datetime import datetime | ||
|
||
|
||
class file_parser: | ||
|
||
def validation(self, value): | ||
if value.strip(): | ||
return int(value) | ||
else: | ||
return None | ||
|
||
def is_valid(self, data): | ||
if data['Max TemperatureC'].strip() and \ | ||
data['Min TemperatureC'].strip() and \ | ||
data['Max Humidity'].strip() and \ | ||
data[' Mean Humidity'].strip(): | ||
return True | ||
else: | ||
return False | ||
|
||
def required_weather_data(self, row_data): | ||
req_field = {} | ||
if 'PKST' in row_data: | ||
req_field['date'] = datetime.strptime(row_data['PKST'], '%Y-%m-%d') | ||
else: | ||
req_field['date'] = datetime.strptime(row_data['PKT'], '%Y-%m-%d') | ||
req_field['max_temp'] = self.validation(row_data['Max TemperatureC']) | ||
req_field['min_temp'] = self.validation(row_data['Min TemperatureC']) | ||
req_field['max_humid'] = self.validation(row_data['Max Humidity']) | ||
req_field['mean_humid'] = self.validation(row_data[' Mean Humidity']) | ||
|
||
return req_field | ||
|
||
def read_files_data(self, path): | ||
weather_data = [] | ||
for filename in os.listdir(path): | ||
if filename.endswith('.txt'): | ||
with open(os.path.join(path, filename)) as weather_file: | ||
data_reader = csv.DictReader(weather_file) | ||
for row in data_reader: | ||
if self.is_valid(row): | ||
weather_data.append(self.required_weather_data(row)) | ||
return weather_data |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import argparse | ||
import os | ||
from datetime import datetime | ||
|
||
from file_parser import file_parser | ||
from calculations import calculations | ||
from reports import reports | ||
|
||
|
||
def is_dir(path): | ||
if os.path.isdir(path): | ||
return path | ||
else: | ||
print("Invalid directory path") | ||
|
||
|
||
def validate_year_date(date): | ||
try: | ||
return datetime.strptime(date, '%Y') | ||
except ValueError: | ||
print(f'{date} is not valid') | ||
|
||
|
||
def validate_month_date(date): | ||
try: | ||
return datetime.strptime(date, '%Y/%m') | ||
except ValueError: | ||
print(f'{date} is not valid') | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser(description="Parse Command Line Arg") | ||
parser.add_argument("path", type=is_dir) | ||
parser.add_argument("-e", "--yearly_date", type=validate_year_date) | ||
parser.add_argument("-a", "--monthly_date", type=validate_month_date) | ||
parser.add_argument("-c", "--bar_chart", type=validate_month_date) | ||
return parser.parse_args() | ||
|
||
|
||
if __name__ == '__main__': | ||
|
||
args = parse_args() | ||
if args.path: | ||
file_reader = file_parser() | ||
weather_data = file_reader.read_files_data(args.path) | ||
weather_calculation = calculations() | ||
|
||
if args.yearly_date: | ||
calculated_year_data = weather_calculation.yearly_weather_calculation(weather_data, args.yearly_date) | ||
reports.generate_yearly_report(calculated_year_data) | ||
|
||
if args.monthly_date: | ||
calculated_monthly_data = weather_calculation.monthly_weather_calculation(weather_data, args.monthly_date) | ||
reports.generate_monthly_report(calculated_monthly_data) | ||
|
||
if args.bar_chart: | ||
calculated_bar_charts_data = weather_calculation.bar_charts_calculation(weather_data, args.bar_chart) | ||
reports.generate_bar_charts_reports(calculated_bar_charts_data, args.bar_chart) | ||
#reports.bonus_task_reports(calculated_bar_charts_data, args.bar_chart) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
class reports: | ||
|
||
def generate_yearly_report(data): | ||
print( | ||
f'Highest: {data["max_temp"]}C on {data["max_temp_date"].strftime("%B")} {data["max_temp_date"].day}\n' | ||
f'Lowest: {data["min_temp"]}C on {data["min_temp_date"].strftime("%B")} {data["min_temp_date"].day}\n' | ||
f'Humidity: {data["max_humidity"]}% on ' | ||
f'{data["max_humidity_date"].strftime("%B")} {data["max_humidity_date"].day}\n' | ||
) | ||
|
||
def generate_monthly_report(data): | ||
print( | ||
f'Highest Average: {data["avg_max"]}C \n' | ||
f'Lowest Average: {data["avg_min"]}C \n' | ||
f'Average Mean Humidity: {data["avg_humidity"]}%' | ||
) | ||
|
||
def generate_bar_charts_reports(data, date): | ||
RED = '\033[31m' | ||
BLUE = '\033[34m' | ||
color_end = '\033[00m' | ||
print(f'\n{date.strftime("%B")} {date.year}\n') | ||
for row in data: | ||
max_temp = '+' * row['max_temp'] | ||
min_temp = '+' * row['min_temp'] | ||
print( | ||
f'{row["date"].day} {RED+max_temp+color_end} {row["max_temp"]}C\n' | ||
f'{row["date"].day} {BLUE + min_temp + color_end} {row["min_temp"]}C\n' | ||
) | ||
|
||
def bonus_task_reports(data, date): | ||
RED = '\033[31m' | ||
BLUE = '\033[34m' | ||
color_end = '\033[00m' | ||
print(f'{date.strftime("%B")} {date.year}\n') | ||
for row in data: | ||
max_temp = '+' * row['max_temp'] | ||
min_temp = '+' * row['min_temp'] | ||
print( | ||
f'{row["date"].day} {BLUE + min_temp + color_end}' | ||
f'{RED + max_temp + color_end} {row["min_temp"]}C - {row["max_temp"]}C \n' | ||
) |