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

Hina/refactoring - Final Branch of work #5

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ __pycache__/
.DS_Store
weatherfiles (1).zip
venv
weatherfiles
weatherfiles
.vscode/
.idea/
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,103 @@
## Weather Task


### How to Run:


First Rename the header of all files (trimming the spaces in file columns name)
```commandline
python3 files_data_cleaner.py
```

> For Question 1:
```bash

python3 main.py /path-to-files -e {year}

ex:
`python3 main.py /weatherfiles -e 2005`

```

> For Question 2:
```bash

python3 main.py /path-to-files -a {year}/{month_number}

ex:
`python3 main.py /weatherfiles -a 2006/6`

```



> For Question 3:
```bash
python3 main.py /path-to-files -c {year}/{month_number}

ex:
`python3 main.py /weatherfiles -c 2005/3`

```



> For Question 4:
```bash

python3 main.py /path-to-files -e {year} -a {year}/{month} -c {year}/{month}

ex:
`python3 main.py /weatherfiles -c 2009/5 -e 2005 -a 2014/4`

```


> For Question 5:
```bash

python3 main.py /path-to-files -cs {year}/{month_number}

ex:
`python3 main.py /weatherfiles -cs 2005/8`
# c --> charts, s --> single line
```







### Code Output:

#### 1- High Temperature, Low Temperature and Humidity

![Image of Question 1](./images/task1.png)

<br />

#### 2- Average Highest Temperature, Average Lowest Temperature and Average mean Humidity

![Image of Question 2](./images/task2.png)

<br />

#### 3- Charts for Highest Temperature and Lowest Temperature on Each day

![Image of Question 3](./images/task3.png)

<br />

#### 4- Multiple Reports

This task is long on terminal, so can't capture a image

<br />

#### 5- Charts for Highest Temperature and Lowest Temperature on Each day in One line

![Image of Question 5](./images/task4.png)

<br />

9 changes: 9 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os

CURRENT_DIRECTORY = os.getcwd()


class COLORS:
BLUE = '\033[96m'
RED = '\033[91m'
ENDC = '\033[0m'
Binary file added constants.pyc
Binary file not shown.
19 changes: 19 additions & 0 deletions custom_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from collections import namedtuple

HighLowTempHumidityReportType = namedtuple(
"HighestLowestTemperatureHumidityReport", [
'highest_temperature',
'lowest_temperature',
'high_humidity'],
defaults=['', '', '']
)

AverageTempHumidReportType = namedtuple(
"AverageTempHumidityReportType", [
'average_max_temperature',
'average_min_temperature',
'average_mean_humidity'],
defaults=['', '', '']
)

DateObj = namedtuple("DateObj", ['month', 'year'], defaults=['', ''])
16 changes: 16 additions & 0 deletions exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class BaseExceptionClass(Exception):
pass


class FolderDoesNotExist(BaseExceptionClass):
pass


class YearRequired(BaseExceptionClass):
pass


class MonthIsOutOfRange(BaseExceptionClass):
pass


58 changes: 0 additions & 58 deletions file_path_matcher_with_date.py

This file was deleted.

34 changes: 34 additions & 0 deletions files_data_cleaner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pandas as pd
import os
import fileinput


def remove_spaces_from_header_files(folder):
""" Rename the file headers - Trim the spaces in header names of files and
then save again """

for filename in os.listdir(folder):
file_name = os.path.join(folder, filename)
df = pd.read_csv(file_name, nrows=0) # read only the header row
df.rename(
columns={" Min Humidity": "Min Humidity",
" Mean Humidity": "Mean Humidity"}, inplace=True
)
header_row = ','.join(df.columns)

# modify header in csv file
f = fileinput.input(file_name, inplace=True)
for line in f:
if fileinput.isfirstline():
print(header_row)
else:
print(line, end='')
f.close()


if __name__ == '__main__':
print("Files Data Cleaner")
remove_spaces_from_header_files(
'/Users/hinakhadim/Documents/Arbisoft/Python/weather_task/weatherfiles'
)
print("done")
Binary file added images/task1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/task2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/task3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/task4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 84 additions & 19 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,94 @@
import sys
import os
from file_path_matcher_with_date import FilePathMatcherWithDate
from constants import CURRENT_DIRECTORY

CURRENT_DIRECTORY = os.getcwd()
from validations import (check_directory_path_exists,
validate_year_month,
validate_year
)
from report_data_provider import ReportDataProvider
from report_generator import (HighLowTemperatureAndHumidityReport,
AverageTemperatureAndHumidityReport,
TemperatureChartsReport
)

import argparse

if __name__ == "__main__":

if len(sys.argv) < 4:
print("You must specify files path and the command with proper flag")
sys.exit(0)
arg_parser = argparse.ArgumentParser(
description="Generates reports from the weather data files"
)

arg_parser.add_argument(
"data_folder",
type=str,
action='store',
nargs=1,
metavar="path/to/folder",
help="Folder path where data files are stored"
)

arg_parser.add_argument(
'-display_report',
type=validate_year,
metavar="year",
help="To display the highest, lowest "
"temperature and highest humidity"
)

arg_parser.add_argument(
'-average_stats_report',
type=validate_year_month,
metavar="year/mm",
help="To display the average high, low "
"temperature and average mean humidity"
)
arg_parser.add_argument(
'-charts',
type=validate_year_month,
metavar="year/mm",
help="Display charts of high and low "
"temperature for each day"
)

arg_parser.add_argument(
'-single_line_chart',
type=validate_year_month,
metavar="year/mm",
help="Display one line charts of high and low "
"temperature for each day"
)

args = arg_parser.parse_args()

weather_data_folder_path = CURRENT_DIRECTORY + args.data_folder[0]
check_directory_path_exists(weather_data_folder_path)

if args.display_report:
data_provider = ReportDataProvider(
args.display_report, weather_data_folder_path
)
report_data = data_provider.get_high_low_temp_high_humidity_data()

weather_data_files_path = CURRENT_DIRECTORY + sys.argv[1]
if not os.path.isdir(weather_data_files_path):
print("Invalid path to directory : ", weather_data_files_path)
sys.exit(0)
report = HighLowTemperatureAndHumidityReport(report_data)
report.print_report()

print(weather_data_files_path)
if args.average_stats_report:
data_provider = ReportDataProvider(
args.average_stats_report, weather_data_folder_path
)
report_data = data_provider.get_average_temp_and_humidity()

system_args = sys.argv[2:]
report = AverageTemperatureAndHumidityReport(report_data)
report.print_report()

year = sys.argv[3]
chart_arguments = {'c': args.charts, 'cs': args.single_line_chart}
for arg, value in chart_arguments.items():

file_path_matcher = FilePathMatcherWithDate(weather_data_files_path)
file_path_matcher.setDate(year)
matched_file_paths_with_year = file_path_matcher.get_files_path()
if value:
data_provider = ReportDataProvider(value, weather_data_folder_path)
report_data = data_provider.get_chart_data()

# for index, argument in enumerate(system_args):
# pass
report = TemperatureChartsReport(
report_data, single_line_chart=(arg == 'cs')
)
report.print_report()
Loading