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 3 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
182 changes: 118 additions & 64 deletions weather.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,118 @@
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


months =[]

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':
yr = sys.argv[2]
sadan marked this conversation as resolved.
Show resolved Hide resolved
val1 = yearcalc(yr)
reportone(val1)
elif str.upper(sys.argv[1]) == 'MONTH':
mn = sys.argv[2].split('/')
val2 = monthcalc(mn)
reporttwo(val2)
else:
print("Please check input parameters")
exit


def yearcalc(y):
Copy link
Collaborator

Choose a reason for hiding this comment

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

function names are always underscore "_" separated. e.g., year_calculator or yearly_report

Copy link
Owner Author

Choose a reason for hiding this comment

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

changed the function names as per the above

mx_tmp = 0
mx_hum = 0
mn_tmp = 100
os.chdir(r'C:\Som\Personal Projects\weatherdata')
Copy link
Collaborator

Choose a reason for hiding this comment

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

This directory structure is not valid for Linux based systems. So if I clone this application I can't run this locally as this directory does not exist. Please find a better and generic way to read files from directory.

Copy link
Owner Author

Choose a reason for hiding this comment

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

changed with pathlib module which is generic for all OS

for f in os.listdir():
if f.__contains__(y):
sadan marked this conversation as resolved.
Show resolved Hide resolved
months.append(f)
for month in months:
with open (f'C:\\Som\\Personal Projects\\weatherdata\\{month}','r') as data_file:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again, please check directory structure.

Copy link
Owner Author

Choose a reason for hiding this comment

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

changed with pathlib module

#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__()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Again, avoid abuse of dunder methods. Check other ways to skip lines while reading a file.

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 one, i am not sure how to achieve. i will explore more

csv_data.__next__()
for line in csv_data:
if line[0].__contains__('<!'):
sadan marked this conversation as resolved.
Show resolved Hide resolved
break
else:
if line[1] == '' and line[3] == '' and line[7] == '':
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):
''.join(dt)
dt_obj = datetime.strptime(dt,'%Y-%m-%d')
c_dt = dt_obj.strftime("%b %d")
return 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}")
return [mx_tmp,cmx_dt,mn_tmp,cmn_dt,mx_hum,chum_dt]

def monthcalc(m):
highest_tmp = 0
lowest_tmp = 0
mean_hum = 0
cnt = 0
str_mn = ''.join(m)
dt = datetime.strptime(str_mn, "%Y%m")
c_dt = str(dt.strftime("%Y_%b"))
Copy link
Collaborator

Choose a reason for hiding this comment

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

what does strftime() returns?

Copy link
Owner Author

Choose a reason for hiding this comment

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

it returns string

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then why are you converting str into str agaian?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I think, i have done it mistakenly.

os.chdir(r'C:\Som\Personal Projects\weatherdata')
for f in os.listdir():
#print(f)
if f.__contains__(c_dt):
with open (f'C:\\Som\\Personal Projects\\weatherdata\\{f}','r') as data_file:

csv_data = csv.reader(data_file, delimiter=',')
som06 marked this conversation as resolved.
Show resolved Hide resolved
csv_data.__next__()
csv_data.__next__()
for line in csv_data:
if line[0].__contains__('<!'):
break
else:
if line[1] == '' and line[3] == '' and line[7] == '':
continue
else:
highest_tmp += int(line[1])
lowest_tmp += int(line[3])
mean_hum += int(line[8])
cnt += 1
avg_high = highest_tmp/cnt
avg_low = lowest_tmp/cnt
avg_hum = mean_hum/ cnt
return [avg_high,avg_low,avg_hum]

def reportone(v1):
print(f"Highest: {v1[0]}C on {v1[1]}")
print(f"Lowest: {v1[2]}C on {v1[3]}")
print(f"Humidity: {v1[4]}% on {v1[5]}")

def reporttwo(v2):
print(f"Highest Aerage: {v2[0]}C")
print(f"Lowest Average: {v2[1]}C")
print(f"Average Mean Humidity: {v2[2]}%")

if __name__ == '__main__':
main()