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

Som06 report 1 #2

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
164 changes: 100 additions & 64 deletions weather.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,100 @@
import os
import sys
import csv
from datetime import datetime


def main():
mx_tmp = 0
mx_hum = 0
mn_tmp = 100
months =[]
os.chdir(r'C:\Som\Personal Projects\weatherdata')
for f in os.listdir():
#fname = str(f.split('_'))
#print(fname[2])
#months.append(f)
if f.__contains__(sys.argv[1]):
months.append(f)
#print(len(months))
for month in months:
with open (f'C:\Som\Personal Projects\weatherdata\{month}','r') as data_file:
#with open (r'C:\Som\Personal Projects\weatherdata\lahore_weather_1996_Dec.txt','r') as data_file:
csv_data = csv.reader(data_file, delimiter=',')
csv_data.__next__()
csv_data.__next__()
for line in csv_data:
if line[0].__contains__('<!'):
break
else:
if line[1] == '' or line[3] == '' :
continue
else:
if int(line[1]) >= mx_tmp:
mx_tmp = int(line[1])
mx_dt = line[0]
if int(line[3]) <= mn_tmp:
mn_tmp = int(line[3])
mn_dt = line[0]
if int(line[7]) >= mx_hum:
mx_hum = int(line[7])
hum_dt = line[0]


def convert_list_to_date(dt, seperator=''):
seperator.join(dt)
dt_obj = datetime.strptime(dt,'%Y-%m-%d')
c_dt = dt_obj.strftime("%b %d")
return seperator.join(c_dt)

# converted_dt = convert_list_to_string(mx_dt)
# dt_obj = datetime.strptime(converted_dt,'%Y-%m-%d')
# c_dt = dt_obj.strftime("%b %d")
cmx_dt = convert_list_to_date(mx_dt)
cmn_dt = convert_list_to_date(mn_dt)
chum_dt = convert_list_to_date(hum_dt)

print(f"Highest: {mx_tmp}C on {cmx_dt}")
print(f"Lowest: {mn_tmp}C on {cmn_dt}")
print(f"Humidity: {mx_hum}% on {chum_dt}")

if __name__ == '__main__':
main()


import os
import sys
import csv
from datetime import datetime
from pathlib import Path

DATA_FOLDER = os.path.dirname(os.path.abspath(__file__))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global variables and specifically constants must always be named in all caps, DATA_FOLDER

def main():
if str.upper(sys.argv[1]) == 'YEAR':
year = sys.argv[2]
year_data = year_calculator(year)
yearly_report(year_data)
elif str.upper(sys.argv[1]) == 'MONTH':
month = sys.argv[2].split('/')
month_data = month_calculator(month)
monthly_report(month_data)
else:
print("Please check input parameters")
exit

'''Calculation based on year input'''

def year_calculator(year):
max_tmp = 0
max_hum = 0
min_tmp = 100

with os.scandir(DATA_FOLDER) as data_files:
for file in data_files:
if year in file.name:
with open (f'{file.name}','r') as data_f:
data_f.readline()
reader = csv.DictReader(data_f, restkey=None, restval=None)
for line in reader:
if line["Max TemperatureC"] is None or line["Max TemperatureC"] == '':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference between the two checks?

a = None
b = ""
if a:
    print("a")
if b:
    print("b")

What do you think the output of above code.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally both are same. but in my code if i use only one check i am getting the error for the others. That's why i used both check in this condition.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, what I was trying to display here is the way of writing this if statement. you can simplify this like

if line["Max TemperatureC"]:
    pass

continue
else:
if int(line["Max TemperatureC"]) >= max_tmp:
max_tmp = int(line["Max TemperatureC"])
maxtmp_dt = line["PKT"]
if int(line["Min TemperatureC"]) <= min_tmp:
min_tmp = int(line["Min TemperatureC"])
mintmp_dt = line["PKT"]
if int(line["Max Humidity"]) >= max_hum:
max_hum = int(line["Max Humidity"])
maxhum_dt = line["PKT"]
else:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What iss the purpose of this else?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not required. forgot to remove.

pass
c_maxtmp_dt = string_to_date(maxtmp_dt)
c_mintmp_dt = string_to_date(mintmp_dt)
c_maxhum_dt = string_to_date(maxhum_dt)

return [max_tmp,c_maxtmp_dt,min_tmp,c_mintmp_dt,max_hum,c_maxhum_dt]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is okay but not good. Now I have to go back and forth in yearly_report function to see what are you passing in the arguments.

Copy link
Owner Author

@som06 som06 Jul 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get that. Do you mean the arguments like maxhum_dt, mintmp_dt etc..?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you must not return these in an array.


def string_to_date(dt):
dt_obj = datetime.strptime(dt,'%Y-%m-%d')
return dt_obj.strftime("%b %d")

'''Calculation based on year input'''
def month_calculator(month):
sum_high_tmp = 0
sum_low_tmp = 0
sum_mean_hum = 0
count = 0

dt = datetime.strptime(str(month), "['%Y', '%m']")
c_dt = dt.strftime("%Y_%b")

with os.scandir(DATA_FOLDER) as data_files:
for file in data_files:
if c_dt in file.name:
with open (f'{file.name}','r') as data_f:
data_f.readline()
reader = csv.DictReader(data_f, restkey=None, restval=None)
for line in reader:
if line["Max TemperatureC"] is None or line["Max TemperatureC"] is '':
continue
else:
sum_high_tmp += int(line["Max TemperatureC"])
sum_low_tmp += int(line["Min TemperatureC"])
sum_mean_hum += int(line[" Mean Humidity"])
count += 1
avg_high = sum_high_tmp/count
avg_low = sum_low_tmp/count
avg_hum = sum_mean_hum/ count
return [avg_high,avg_low,avg_hum]
'''Report-1'''
def yearly_report(data_year):
print(f"Highest: {data_year[0]}C on {data_year[1]}")
print(f"Lowest: {data_year[2]}C on {data_year[3]}")
print(f"Humidity: {data_year[4]}% on {data_year[5]}")
'''Report-2'''
def monthly_report(data_month):
print(f"Highest Aerage: {data_month[0]}C")
print(f"Lowest Average: {data_month[1]}C")
print(f"Average Mean Humidity: {data_month[2]}%")

if __name__ == '__main__':
main()