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

Som06 report 1 #2

wants to merge 10 commits into from

Conversation

som06
Copy link
Owner

@som06 som06 commented Jul 14, 2020

No description provided.

@som06
Copy link
Owner Author

som06 commented Jul 14, 2020

Changes made to existing code

@som06
Copy link
Owner Author

som06 commented Jul 14, 2020

@sadan A

@som06 som06 requested a review from sadan July 14, 2020 11:09
Copy link
Collaborator

@sadan sadan left a comment

Choose a reason for hiding this comment

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

Please improve your variable and function naming conventions. I also see lot's of unnecessary comments, please remove them as well.

Personally, I believe functions and variable names should be named like so it's easy to understand the code by reading it but sometimes it's also good to add comments on sections where there is some calculation involved. Or to avoid inline comment you can add docstrings in functions to explain how the function actually works.

weather.py Outdated Show resolved Hide resolved
weather.py Outdated
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

weather.py Outdated
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

weather.py Outdated Show resolved Hide resolved
weather.py Outdated
if f.__contains__(y):
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

weather.py Outdated
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__()
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

weather.py Outdated Show resolved Hide resolved
weather.py Outdated Show resolved Hide resolved
weather.py Outdated Show resolved Hide resolved
weather.py Outdated


months =[]
data_folder = Path("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 will still not work on my local machine if I try to run.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Yes, that's correct. i have to give a generic folder name only like weatherdata and pathlib is resolve it for all kind of OS i guess

Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you also look into os.path https://docs.python.org/3/library/os.path.html

weather.py Outdated
count = 0
str_mo = ''.join(mo)
dt = datetime.strptime(str_mo, "%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.

weather.py Outdated Show resolved Hide resolved
weather.py Outdated
count = 0
str_mo = ''.join(mo)
dt = datetime.strptime(str_mo, "%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.

Then why are you converting str into str agaian?

@@ -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

weather.py Outdated
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"))
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

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

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants