def write_to_json(self, export_fp: str): """ get all QASet objects from self.pool convert every QASet object into a dictionary write the list of dictionaries into json """ export_fp: Path = Path(export_fp) if export_fp.is_file(): overwrite = input( f"{export_fp} already exist, overwrite it (y/n)? ") if overwrite in ("Y", "y", "yes"): logger.warning(f"Overwriting {export_fp}") else: logger.error(f"Export aborted") return lst = [] # gather all data from pool for qa_set in self.pool: lst.append( {tag: getattr(qa_set, tag) for tag in QASet.property_list}) with export_fp.open(mode="w") as f: json.dump(lst, f, indent=4)
def export_excel_to_json(qa_bank_excel: str): qa_bank_excel = Path(qa_bank_excel) if not qa_bank_excel.is_file(): logger.error(f"Filepath '{str(qa_bank_excel)}' does not exist.") return if str(qa_bank_excel)[-5:] != ".xlsx": logger.error( f"QA Bank Filepath requires an Excel file. '{str(qa_bank_excel)}' doesn't match with '*.xlsx'." ) return pool = QASetPool() pool.load_from_excel(qa_bank_excel) export_fp = qa_bank_excel.parent / f"{qa_bank_excel.stem}.json" pool.write_to_json(export_fp) return
def load_from_json(self, json_fp: str): """ 1. read from json 2. create QASet objects from json data 3. add QASet objects into self.pool """ assert Path(json_fp).is_file() try: qadict_lst = json.load(open(json_fp)) logger.debug(f"{json_fp} has been loaded") except: logger.error(f"Failed loading {json_fp}") return for qa_dict in qadict_lst: self.add_to_pool(QASet(**qa_dict))
def load_from_excel(self, excel_fp: str): """ 1. read from excel 2. create QASet objects from excel data 3. add QASet objects into self.pool """ qa_sheet = pandas.read_excel(excel_fp) rows_num, cols_num = qa_sheet.shape logger.debug(f"rows_num: {rows_num}") logger.debug(f"cols_num: {cols_num}\n") for i in range(0, rows_num): try: qa_set_id = int(qa_sheet.iloc[i][2]) logger.debug(f"ID: {qa_set_id}") except Exception as e: logger.error("Failed getting QASet ID from excel row") continue qa_set_qn: str = qa_sheet.iloc[i][3] logger.debug(f"qa_set_qn: {qa_set_qn}") if not self.get_by_id(qa_set_id): qa_set_type: str = qa_sheet.iloc[i][0] logger.debug(f"qa_set_type: {qa_set_type}") qa_set_subtype: str = qa_sheet.iloc[i][1] logger.debug(f"qa_set_subtype: {qa_set_subtype}") qa_set_options: OrderedSet = OrderedSet( str(qa_sheet.iloc[i][j]) for j in range(4, cols_num)) # remove emtpy option, which is 'nan' in pandas qa_set_options.discard("nan") logger.debug(f"qa_set_options: {qa_set_options}") qa_set: QASet = QASet( id=qa_set_id, type=qa_set_type, subtype=qa_set_subtype, qns=qa_set_qn, options=qa_set_options, ) self.add_to_pool(qa_set) logger.debug(">>> Added new QASet into pool\n") # logger.debug(qa_set) else: self.get_by_id(qa_set_id).append_rephrase(qa_set_qn) logger.debug( "### Added new rephrased version into existing QASet\n")
def subtype(self, value): logger.error( f"QASet subtype is immutable, and it remains as {self._subtype}") return
def id(self, value): logger.error(f"QASet ID is immutable, and it remains as {self._id}") return
def options(self, value): logger.error( f"QASet options is immutable, and it remains as {self._options}") return