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 5, 2019
1 parent dbc15ca commit 937c090
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 62 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 = []
File renamed without changes.
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
File renamed without changes.
64 changes: 12 additions & 52 deletions file_processor.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,25 @@
import csv
from pandas import read_excel
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
self.file_path = folder_path + file_name
self.file_data = []
self.formatted_data = []

def format_file_data(self):
if self.file_name.endswith('.txt'):
self.read_text_file_data()
return self.format_text_file_data()
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'):
self.read_excel_file_data()
return self.format_excel_file_data()
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'):
self.read_tsv_file_data()
return self.format_tsv_file_data()
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')

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

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

def read_tsv_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_text_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

def format_excel_file_data(self):
formatted_rows = self.file_data.to_dict('records')
return formatted_rows

def format_tsv_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
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 file_handler.file_record import FileRecord
from helpers.weatherman_helper import calculate_average, format_average_values_output


class AverageCalculator(FileRecord):
Expand Down
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 file_handler.file_record import FileRecord


class BarChartGenerator(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 file_handler.file_record import FileRecord


class HighestValueCalculator(FileRecord):
Expand Down
8 changes: 4 additions & 4 deletions weatherman.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 import ZipFile
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():
Expand Down

0 comments on commit 937c090

Please sign in to comment.