Skip to content

Commit

Permalink
- Improve directory structure
Browse files Browse the repository at this point in the history
 - Files operation using OOP
  • Loading branch information
AliAdnanSohail committed Nov 6, 2019
1 parent dbc15ca commit 6635d33
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 79 deletions.
14 changes: 14 additions & 0 deletions file_handler/excel_file_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from file_handler.file_handler import FileHandler
from pandas import read_excel


class ExcelFileHandler(FileHandler):
def __init__(self, folder_path, file_name):
super().__init__(folder_path, file_name)

def read_file_data(self):
self.file_data = read_excel(self.file_path)

def format_file_data(self):
formatted_rows = self.file_data.to_dict('records')
return formatted_rows
7 changes: 7 additions & 0 deletions file_handler/file_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class FileHandler:
def __init__(self, folder_path, file_name):
self.folder_path = folder_path
self.file_name = file_name
self.file_path = folder_path + file_name
self.file_data = []
self.formatted_data = []
25 changes: 25 additions & 0 deletions file_handler/file_processor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from file_handler.text_file_handler import TextFileHandler
from file_handler.excel_file_handler import ExcelFileHandler
from file_handler.tsv_file_handler import TSVFileHandler


class FileProcessor:
def __init__(self, folder_path, file_name):
self.folder_path = folder_path
self.file_name = file_name

def format_file_data(self):
if self.file_name.endswith('.txt'):
text_file = TextFileHandler(self.folder_path, self.file_name)
text_file.read_file_data()
return text_file.format_file_data()
elif self.file_name.endswith('.xlsx'):
excel_file = ExcelFileHandler(self.folder_path, self.file_name)
excel_file.read_file_data()
return excel_file.format_file_data()
elif self.file_name.endswith('.tsv'):
tsv_file = TSVFileHandler(self.folder_path, self.file_name)
tsv_file.read_file_data()
return tsv_file.format_file_data()
else:
raise Exception('Invalid file extension')
24 changes: 24 additions & 0 deletions file_handler/text_file_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from file_handler.file_handler import FileHandler


class TextFileHandler(FileHandler):
def __init__(self, folder_path, file_name):
super().__init__(folder_path, file_name)

def read_file_data(self):
with open(self.file_path, 'r') as file_ref:
self.file_data = file_ref.read()

def format_file_data(self):
rows = self.file_data.split('\n')
rows = list(filter(None, rows))
header = rows.pop(0).split(',')
header = list(map(str.strip, header))
formatted_rows = []
for row in rows:
row_dict = {}
row_data = row.split(',')
for idx, value in enumerate(row_data):
row_dict[header[idx]] = value
formatted_rows.append(row_dict)
return formatted_rows
23 changes: 23 additions & 0 deletions file_handler/tsv_file_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from file_handler.file_handler import FileHandler
import csv


class TSVFileHandler(FileHandler):
def __init__(self, folder_path, file_name):
super().__init__(folder_path, file_name)

def read_file_data(self):
with open(self.file_path) as file_ref:
rows = csv.reader(file_ref, delimiter="\t", quotechar='"')
self.file_data = list(rows)

def format_file_data(self):
data = list(filter(None, self.file_data))
header = data.pop(0)
formatted_rows = []
for row in data:
row_dict = {}
for idx, value in enumerate(row):
row_dict[header[idx]] = value
formatted_rows.append(row_dict)
return formatted_rows
2 changes: 1 addition & 1 deletion zip_file.py → file_handler/zip_file_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import fnmatch


class ZipFile:
class ZipFileProcessor:
def __init__(self, path, zip_name, directory_name):
self.path = path
self.zip_name = zip_name
Expand Down
65 changes: 0 additions & 65 deletions file_processor.py

This file was deleted.

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from date_parser import DateParser
from file_record import FileRecord
from weatherman_helper import calculate_average, format_average_values_output
from values_calculator.file_record import FileRecord
from helpers.weatherman_helper import calculate_average, format_average_values_output


class AverageCalculator(FileRecord):
Expand All @@ -23,4 +23,4 @@ def display_average_for_file(self, file_name):

print(format_average_values_output('Max TemperatureC', avg_max_temp))
print(format_average_values_output('Min TemperatureC', avg_min_temp))
print(format_average_values_output('Mean Humidity', avg_mean_humidity))
print(format_average_values_output('Mean Humidity', avg_mean_humidity))
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from weatherman_helper import display_bar_line
from helpers.weatherman_helper import display_bar_line
from date_parser import DateParser
from file_record import FileRecord
from values_calculator.file_record import FileRecord


class BarChartGenerator(FileRecord):
Expand Down
2 changes: 1 addition & 1 deletion file_record.py → values_calculator/file_record.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from file_processor import FileProcessor
from file_handler.file_processor import FileProcessor


class FileRecord:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from weatherman_helper import format_highest_values_output
from file_record import FileRecord
from helpers.weatherman_helper import format_highest_values_output
from values_calculator.file_record import FileRecord


class HighestValueCalculator(FileRecord):
Expand Down
10 changes: 5 additions & 5 deletions weatherman.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import sys

from zip_file import ZipFile
from highest_value_calculator import HighestValueCalculator
from average_calculator import AverageCalculator
from bar_chart_generator import BarChartGenerator
from file_handler.zip_file_processor import ZipFileProcessor
from values_calculator.highest_value_calculator import HighestValueCalculator
from values_calculator.average_calculator import AverageCalculator
from values_calculator.bar_chart_generator import BarChartGenerator


def main():
extract_to_path = sys.argv[1]
zip_name = 'weatherfiles.zip'
directory_name = 'weatherfiles'
zip_file = ZipFile(extract_to_path, zip_name, directory_name)
zip_file = ZipFileProcessor(extract_to_path, zip_name, directory_name)
zip_file.extract_files()
print("Files extracted to /" + zip_file.path)

Expand Down

0 comments on commit 6635d33

Please sign in to comment.