def save_to_file(self, txt_file: TextIO): """ Given the file output from an open('w') method, writes to the file the data within self """ for row_object in self.row_dict.values(): txt_file.write(row_object.tabulate()) txt_file.close() logging.debug( f'{type(self).__name__} object successfully saved to file')
def load_from_file(self, txt_file: TextIO): """ Given the output from an open() method, populates self with data from lines of text file """ txt_lines = txt_file.readlines() for row in txt_lines: obj_info = list() padded_fields = row.split( FIELD_ESCAPE_STR) # split line/row by separator for field in padded_fields: if field != '\n': field = field.strip() # remove trailing/padding whitespace field = field.replace(LINEBREAK_ESCAPE_STR, '\n') # re-insert escaped linebreaks obj_info.append(field) self.add_row(*obj_info) # add new row/obj to table txt_file.close() logging.debug( f'{type(self).__name__} object successfully populated from file - ' f'added {len(txt_lines)} {self.row_class.__name__} objects')
def close_data(file: TextIO): file.close()