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 1 commit
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
96 changes: 43 additions & 53 deletions weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,42 @@

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]
yr_data = year_calculator(yr)
yearly_report(yr_data)
year = sys.argv[2]
year_data = year_calculator(year)
yearly_report(year_data)
elif str.upper(sys.argv[1]) == 'MONTH':
mo = sys.argv[2].split('/')
mo_data = month_calculator(mo)
monthly_report(mo_data)
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(yr):
def year_calculator(year):
max_tmp = 0
max_hum = 0
min_tmp = 100
os.chdir(data_folder)
for file in os.listdir():
if yr in file:
if year in file:
with open (f'{data_folder}/{file}','r') as data_file:
csv_data = csv.reader(data_file, delimiter=',')
next(csv_data)
next(csv_data)
# csv_data.__next__()
# csv_data.__next__()
for line in csv_data:
if line[0].startswith("<!"):
break
data_file.readline()
reader = csv.DictReader(data_file, restkey=None, restval=None)
for line in reader:
if line["Max TemperatureC"] is None or line["Max TemperatureC"] == '':
continue
else:
if line[1] == '' and line[3] == '' and line[7] == '':
continue
else:
if int(line[1]) >= max_tmp:
max_tmp = int(line[1])
maxtmp_dt = line[0]
if int(line[3]) <= min_tmp:
min_tmp = int(line[3])
mintmp_dt = line[0]
if int(line[7]) >= max_hum:
max_hum = int(line[7])
maxhum_dt = line[0]
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"]

c_maxtmp_dt = string_to_date(maxtmp_dt)
c_mintmp_dt = string_to_date(mintmp_dt)
Expand All @@ -61,48 +56,43 @@ def string_to_date(dt):
c_dt = dt_obj.strftime("%b %d")
return c_dt


def month_calculator(mo):
'''Calculation based on year input'''
def month_calculator(month):
sum_high_tmp = 0
sum_low_tmp = 0
sum_mean_hum = 0
count = 0
str_mo = ''.join(mo)
dt = datetime.strptime(str_mo, "%Y%m")
str_month = ''.join(month)
dt = datetime.strptime(str_month, "%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(data_folder)
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 one way of doing this, but I don't see a reason to change the directory in the middle of code execution. Please check method scandir() and then you can do something like:

with os.scandir(DATA_FOLDER) as data_files:
    for file in data_files:
        pass

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 scandir

for file in os.listdir():
if c_dt in file:
with open (f'{data_folder}/{file}','r') as data_file:
csv_data = csv.reader(data_file, delimiter=',')
next(csv_data)
next(csv_data)
for line in csv_data:
if line[0].startswith("<!"):
break
data_file.readline()
reader = csv.DictReader(data_file, restkey=None, restval=None)
for line in reader:
if line["Max TemperatureC"] is None or line["Max TemperatureC"] is '':
continue
else:
if line[1] == '' and line[3] == '' and line[7] == '':
continue
else:
sum_high_tmp += int(line[1])
sum_low_tmp += int(line[3])
sum_mean_hum += int(line[8])
count += 1
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]}")

def monthly_report(data_mo):
print(f"Highest Aerage: {data_mo[0]}C")
print(f"Lowest Average: {data_mo[1]}C")
print(f"Average Mean Humidity: {data_mo[2]}%")
'''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()

main()