Skip to content

Commit 1077c6c

Browse files
author
Amir Ali
committed
refactor: doing some refactor in extractors.py module to make it more readable and reducing th length of functions
style: adding comments in extractors.py module
1 parent d711a90 commit 1077c6c

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/extractors.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def export_extracted_data_to_csv(self, output_file : str, patterns : Patterns ,c
2929
writer.writerows(extracted_data)
3030

3131
if WithLogging.with_logging:
32-
return DataExtractor.log_found_data(extracted_data_copy)
32+
return DataExtractor.get_extracted_data_string(extracted_data_copy)
3333

3434
class ExcelFileExtractor:
3535
def __init__(self, col_var, exact_var):
@@ -100,7 +100,7 @@ def export_extracted_data_to_excel(self, output_file : str, sheet_name : str, pa
100100
wb.close()
101101

102102
if WithLogging.with_logging:
103-
return DataExtractor.log_found_data(extracted_data_copy)
103+
return DataExtractor.get_extracted_data_string(extracted_data_copy)
104104

105105
class DataExtractor:
106106
def __init__(self, excel_var, log_text, col_var, exact_var):
@@ -122,14 +122,17 @@ def extract_data(patterns : Patterns, content : str) -> ExtractedData:
122122
def prepare_to_extract_data(self, output_file : str, input_file : str, sheet_name : str, patterns : Patterns) -> None:
123123
try:
124124
assert patterns, 'There is no patterns to extract data'
125+
assert output_file, 'The name of output file is required.'
126+
127+
# --- read the content of input file ---
125128

126129
with open(input_file,encoding=ENCODING) as f:
127130
try:
128131
content = f.read()
129132
except UnicodeDecodeError:
130133
raise ValueError('The input file cannot be a binary file')
131134

132-
assert output_file, 'The name of output file is required.'
135+
# --- deciding what to do with the output file ---
133136

134137
output_file_extention = os.path.splitext(output_file)[1].lower()
135138

@@ -141,14 +144,11 @@ def prepare_to_extract_data(self, output_file : str, input_file : str, sheet_nam
141144

142145
else:
143146
log_string = self.csv_extractor.export_extracted_data_to_csv(output_file,patterns,content)
147+
148+
# --- handling the log ----
144149

145150
if WithLogging.with_logging:
146-
log_string += f'\n{output_file!r} saved.' + '\n'
147-
self.log_text.config(state='normal')
148-
self.log_text.delete('1.0','end')
149-
self.log_text.insert('end', log_string)
150-
self.log_text.config(state='disabled')
151-
self.log_text.see('end')
151+
self.log_found_data()
152152

153153
except (FileNotFoundError, AssertionError, PermissionError, ValueError, re.PatternError) as err:
154154
show_error(err)
@@ -157,13 +157,21 @@ def prepare_to_extract_data(self, output_file : str, input_file : str, sheet_nam
157157
show_error("You cannot place multiple groups in a pattern")
158158

159159
@staticmethod
160-
def log_found_data(extracted_data_copy : ExtractedData) -> str:
160+
def get_extracted_data_string(extracted_data_copy : ExtractedData) -> str:
161161
log_string = ''
162162

163163
for data_list in extracted_data_copy:
164164
log_string += '\n'.join(data_list) + '\n'
165165

166166
return log_string
167+
168+
def log_found_data(self):
169+
log_string += f'\n{output_file!r} saved.' + '\n'
170+
self.log_text.config(state='normal')
171+
self.log_text.delete('1.0','end')
172+
self.log_text.insert('end', log_string)
173+
self.log_text.config(state='disabled')
174+
self.log_text.see('end')
167175

168176
@staticmethod
169177
def create_column_order(extracted_data : ExtractedData) -> tuple[tuple[str]]:

0 commit comments

Comments
 (0)