def execute_query(id): query = (q.query_list['get_query'], (id, )) result = db.get_data('sqlite', CONN, query) if result['status']: #query,num_of_rows,date_submitted,status,date_executed,application query = (result['data'][0][0], ) num_of_rows = result['data'][0][1] status = result['data'][0][3] application = result['data'][0][5] database = result['data'][0][6] if status == 'SUBMITTED': conn_str = cn.get_connection(application, database) update_result = db.update_data(config.get_sql_type('database'), conn_str, query, num_of_rows) if update_result['status']: query = ( q.query_list['update_query'], (datetime.now().strftime('%Y-%m-%d'), update_result['data'], id), ) querytable_update = db.update_data('sqlite', CONN, query, 1) return update_result else: { 'data': None, 'status': False, 'message': "Query is not in SUBMITTED " }
def save_value(self): label = self.label_id(self.button_group_2.checkedId()) if label : updated_list = [] for letter_obj,value_obj in obj_list: letter = letter_obj.text().toUtf8() value = value_obj.text() objs = letter,value updated_list.append(objs) database.update_data(updated_list,label)
def execute_query(incoming_query, query_name, app, db_name): query = (q.query_list['get_updated_row'],(query_name,)) result = db.get_data('sqlite',CONN,query) if result['status']: conn_str = cn.get_connection(app,db_name) update_result = db.update_data(config.get_sql_type('database'),conn_str,(incoming_query,),result['data'][0]['expected_rows']) return update_result else: return {'data':None,'status':False,'message':'Invalid Query Name'}
def download_file(unique_numbers, link): """сохранить файл и предать имя в базу""" try: headers = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" } response = session.post(url=link, headers=headers, stream=True) file_name = response.headers['Content-Disposition'].split('"')[-2] print(f'Имя файла {file_name} был удачно сохранено') # with open(os.path.join(documents_directory_test, file_name), 'wb') as fd: # for chunk in response.iter_content(chunk_size=1024): # fd.write(chunk) database.update_data(unique_numbers, file_name) except Exception as e: print(f"ошибка сохранения: имя отсутствует - в базу будет передано 'Файл отсутствует'") print(e) database.update_data(unique_numbers, "Файл отсутствует")
def preprocess_dataset(dataset_info): """ Preprocess the file information and insert into the database. The file type could be csv, txt and xls. The file information should hardcord in the config file. The function allow a increnmental way adding information to the database. @param dataset_info: The preprocess dataset information list. Each item in the list is a dictionary which contain the dataset name and all the insert task names. The insert task name should define in the config. @return: None """ for info in dataset_info: dataset_name, insert_tasks = info["dataset"], info["insert_tasks"] # get dataset preprocess config and basic information config = get_preprocess_config(dataset_name, insert_tasks) print("dataset: ", dataset_name) dataset = db[dataset_name] # delete all the data in the current dataset, may uncomment when developing # delete_all_date(dataset) # get all the patient id in the current dataset all_patient_id = { patient_id["patient_id"] for patient_id in query_field(dataset, field={ "_id": 0, "patient_id": 1 }) } # get the raw data for increnmental adding raw_data = { result["patient_id"]: { field: result[field] for field in result if field != "patient_id" } for result in query_field(dataset) } data = defaultdict(lambda: dict()) # for each sub dataset task for insert_task in insert_tasks: # get sub dataset basic information filenames = config[insert_task]["filename"] fields = config[insert_task]["select_column"] # ASSUMPTION: all the insert task has field patient_id and the meaning is the same. # D1NAMO break the assumption and will adhoc get the patient id from file name. patient_idx = sum( [i for i in range(len(fields)) if fields[i] == "patient_id"]) for filename in filenames: # get the file real path file = os.path.join( os.path.join(config["root_dir"], config["dataset"]), filename) print("processing file", file) # ASSUMPTION: all the file type in the insert task is the same. # get the file reader and line count if config[insert_task]["file_type"] == "xls": cnt = line_count_xls(file) readable = Reader( xlrd.open_workbook(file).sheets()[0], config[insert_task]["file_type"]) # file type is txt or csv else: cnt, readable = line_count(file), Reader( open(file), config[insert_task]["file_type"]) # use tqdm to show the process progress with tqdm(total=cnt) as bar: for line_cnt in range(cnt): # get file content line = readable.readline() # if the line is not the header if line_cnt != 0: # get patient_id if dataset_name == "D1NAMO": patient_id = int(file.split("/")[-2]) else: patient_id = str(int(float(line[patient_idx]))) # if the patient id is not in the dataset, add this patient to the database. if patient_id not in all_patient_id: insert_one_data(dataset, {"patient_id": patient_id}) all_patient_id.add(patient_id) # get line timestamp. if there is no timestamp, it will be 0 timestamp = 0 if "datetime" in fields: timestamp += sum( datetime_to_int( line[i], config[insert_task] ["basedate"], config[insert_task] ["pattern"]) for i in range(len(fields)) if fields[i] == "datetime") else: if "date" in fields: timestamp += sum( date_to_int( line[i], config[insert_task] ["basedate"], config[insert_task] ["pattern"]) for i in range(len(fields)) if fields[i] == "date") if "timestamp" in fields: timestamp += sum( timestamp_to_int( line[i], config[insert_task] ["pattern"]) for i in range(len(fields)) if fields[i] == "timestamp") row_combine_field = dict() for idx in range(len(line)): if idx >= len(line): continue content, field = line[idx], config[ insert_task]["select_column"][idx] # if the field should not append or there is no content in the line, continue if field == '' or len(content) == 0: continue # if the field is patient_id or timestamp related, continue if field in { "patient_id", "datetime", "date", "timestamp" }: continue # if the field is a status, the field content will not store in list style. if field in status_field_set: # adhoc for field trouble_sleep_inverse if field == "trouble_sleep_inverse": data[patient_id][ "trouble_sleep"] = str( 5 - int(content)) # adhoc for field low_gl elif field == "low_gl": data[patient_id][ "low_gl"] = content.split(" ")[0] else: data[patient_id][field] = content # adhoc for field weight_units (weight should in data before) elif field == "weight_units": if content == "lbs": data[patient_id]["weight"] = str( LBS_TO_KG * float(data[patient_id]["weight"])) # if the field is need store with timestamp elif field in timestamp_field_set: # adhoc for field raw_gl if field == "raw_gl": content = str(float(content) * 18) field = "gl" # if field not in patient's data, initial from raw data in database if field not in data[patient_id]: data[patient_id][field] = \ list() if patient_id not in raw_data or field not in raw_data[patient_id] \ else raw_data[patient_id][field] # append the content with timestamp data[patient_id][field].append( [content, timestamp]) # if the field needs to combine to another field elif field in combine_field_set: combine_field = combine_field_set[field] if combine_field not in row_combine_field: row_combine_field[combine_field] = 0 row_combine_field[combine_field] += float( content) # for the common field, store in list style else: # if field not in patient's data, initial from raw data in database if field not in data[patient_id]: data[patient_id][field] = \ list() if patient_id not in raw_data or field not in raw_data[patient_id] \ else raw_data[patient_id][field] data[patient_id][field].append(content) # ASSUMPTION: the combine field is the common field (not status or store with timestamp) for field in row_combine_field: if field not in data[patient_id]: data[patient_id][field] = list() data[patient_id][field].append( str(row_combine_field[field])) # update the progress bar bar.update() # insert the preprocessed data to the database print("start to insert data to:", dataset_name) start = time.clock() for patient_id in data: for field in data[patient_id]: # update the field in the database update_data(dataset, {"patient_id": patient_id}, {'$set': { field: data[patient_id][field] }}) print("use time to insert:", time.clock() - start)
def handle_command(self, command): """Here the command given from the gui is handled""" #Main Menu if self.pos == 0: if command == "begin": self.pos = 1 return (self.get_description(),"clear Wooden House first check") else: return("Please type in one of the above.","") if self.pos == 666: return("RESTART THE GAME TO TRY AGAIN.","") if self.pos == 7777: return("RESTART THE GAME IF YOU WANT TO PLAY AGAIN!","") #General #~Note elif self.pos == self.note.position and command in self.takeNote \ and self.note.status == "dropped": self.note.status = "picked up" return ("You picked up a note.", "note") elif self.note.status == "picked up" and command == "read note": self.readNote = True return (self.note.description, "") elif command == "drop note" and self.note.status == "picked up": self.note.position = self.pos self.note.status = "dropped" return("You dropped the note.", "dropNote") elif command in self.noteInfo: return(self.note.get_position(),"") #~Ruby elif self.pos == self.ruby.position and command in self.takeRuby \ and self.ruby.status == "dropped" and self.stone_correct == True: self.ruby.status = "picked up" return ("You picked up a ruby.", "ruby") elif command == "drop ruby" and self.ruby.status == "picked up": self.ruby.position = self.pos self.ruby.status = "dropped" return("You dropped the ruby.", "dropRuby") elif command in self.rubyInfo: found = 0 if self.bandit_conv_over == True: found = 1 if self.stone_conv_over == True: found = 2 return(self.ruby.get_position(found),"") elif command in self.info: return (self.get_description(),"check") #~Save and Load elif command == "save": self.save = True return('Enter your name to create a new save, "overwrite" to overwrite or "delete" to delete',"") elif self.save == True: self.save = False if command == "overwrite": self.overwrite = True saves = self.get_saves() saves += "Enter the ID of the save you wish to overwrite." return(saves,"") else: save_name = command database.save_data((save_name, self.pos, self.readNote, \ self.note.status,self.note.position, self.bandit_conv_over)) return("Your name is "+save_name+" and you have saved succesfully.","") elif self.overwrite == True: saveID = int(command) database.update_data((self.pos, self.readNote, \ self.note.status,self.note.position, self.bandit_conv_over,saveID)) return("You have overwritten save number: "+str(saveID)+" succesfully.","") elif command == "load": self.load = True saves = self.get_saves() saves += "Enter the ID of the save you wish to load." return(saves,"") elif self.load == True: self.load = False id = command selected_data = database.select_data(id) self.pos = [x[1] for x in selected_data][0] self.readNote = [x[2] for x in selected_data][0] self.note.status = [x[3] for x in selected_data][0] self.note.position = [x[4] for x in selected_data][0] self.bandit_conv_over = [x[5] for x in selected_data][0] print(self.pos, self.readNote, self.note.status, self.bandit_conv_over) return(self.get_description(),"") elif command == "delete": self.delete = True saves = self.get_saves() saves += "Enter the ID of the save you wish to delete" return(saves,"") elif self.delete == True: self.delete = False id = command database.delete_data(id) return("You have deleted save number: "+str(command)+" succesfully.","") #Room 1 Wooden House elif self.pos == 1: if command in self.compass[1]: self.pos = 2 return(self.get_description(),"clear Forest check") elif command in self.compass[0] or command == "enter house": return("The door is locked.","") elif command == "describe house" or command == "examine house": return(self.one.house_description,"") elif command in self.lockpick: return("You don't have a lockpick.","") elif command == self.break_door: return("Don't overestimate your strength.","") elif command == "break window": if self.window == "whole": self.window = "broken" return(self.break_window,"") elif self.window == "broken": return("The window is already broken.","") elif command in self.compass[2] or command in self.compass[3] or \ command in self.compass[4] or command in self.compass[5] or \ command in self.compass[6] or command in self.compass[7]: return("The forest is to dense, you can't go there.","") else: return ("I beg your pardon?", "") #Room 2 Forest elif self.pos == 2: if command in self.compass[3]: self.pos = 1 if self.quest == "complete": self.pos = 7777 return("YOU WON THE GAME!","clear Wooden House") return (self.get_description(),"clear Wooden House check") elif command in self.compass[1]: self.pos = 3 return (self.get_description(),"clear Clearing check") elif command in self.compass[0] or command in self.compass[2] or \ command in self.compass[4] or command in self.compass[5] or \ command in self.compass[6] or command in self.compass[7]: return("The forest is to dense, you can't go there.","") else: return ("I beg your pardon?", "") #Room 3 Clearing elif self.pos == 3: if command in self.compass[1]: return("You're not Jesus, you can't walk on water.","") elif command in self.swim_west: return("You never learned how to swim.","") elif command in self.compass[0]: self.pos = 4 return(self.get_description(),"clear Cave check") elif command in self.compass[2]: self.pos = 5 if self.quest == "declined": self.pos = 666 return(self.bandit_lose,"clear") elif self.quest == "incomplete": if self.ruby.status == "picked up" and self.bandit_conv_over == True: self.ruby.position = 123123 self.quest = "complete" self.pos = 3 return(self.happybandit,"Clearing dropRuby") else: return(("Bandit: Do you have the Ruby?\nYou: No. \n" "Bandit: Then go get it if you want the dwarf!"),"") else: return(self.get_description(),"clear BanditCamp check") elif command in self.compass[3]: self.pos = 2 return(self.get_description(),"clear Forest check") elif command in self.compass[4] or command in self.compass[5] or \ command in self.compass[6] or command in self.compass[7]: return("The forest is to dense, you can't go there.","") else: return ("I beg your pardon?", "") #Room 4 Cave entrance elif self.pos == 4: if command in self.compass[2]: self.pos = 3 return(self.get_description(),"clear Clearing check") elif command in self.compass[4]: self.pos = 6 return(self.get_description(),"clear Cave check") elif command in self.compass[0] or command in self.compass[1] or \ command in self.compass[3] or command in self.compass[5] or \ command in self.compass[6] or command in self.compass[7]: return("It's to dark for you to go there.","") #Room 6 Stone Guardian elif self.pos == 6: if command in self.stone_conv_start and self.stone_conv_over == False: self.pos = 6.1 return(self.stone_opener,"") elif command in self.compass[7]: self.pos = 4 return(self.get_description(),"clear Cave check") elif command in self.compass[0] or command in self.compass[1] or \ command in self.compass[2] or command in self.compass[3] or \ command in self.compass[4] or command in self.compass[5] or \ command in self.compass[6]: return("It's not recommended to talk into a wall.","") else: return("I beg your pardon?","") elif self.pos == 6.1: if command == "yes": self.pos = 6.2 return(self.stone_conversation(),"") elif command == "no": self.pos = 6 return("You may leave or speak to the guardian again. ","clear") else: return("I require a yes or no answer.","") elif self.pos == 6.2: if command == "david": self.pos = 6 self.stone_conv_over = True self.stone_correct = True string_for_print = ("Correct. \n" "The treasure is up for grabs; a shining red ruby \ lays in the rooms before you.") return(string_for_print,"") elif command != "david": print(self.chances) self.chances += 1 if self.chances == 1: chance_for_print = "Wrong. Two chances remaining." elif self.chances == 2: chance_for_print = "Wrong. One chance remaining" elif self.chances == 3: self.stone_conv_over = True self.stone_correct = False self.pos = 6 string_for_print = ("All wrong. Leave. \n" "The door is closed and your chances of saving the \ dwarf with the ruby looks slim.") return(string_for_print,"") return(chance_for_print,"") #Room 5 Bandit Camp elif self.pos == 5: if self.quest == "incomplete": if self.bandit_conv_over == True: if command in self.compass[0]: self.pos = 3 return(self.get_description(),"clear Clearing check") else: return("Leave the place by taking the northern path seems" "like your only option.","") if self.quest == "not started": if self.bandit_conv_over == False: if command in self.bandit_conv_start: self.pos = 5.1 return(self.bandit_conversation(),"cursorTop") else: return("Talk to the bandits before doing anything else.","") elif self.pos == 5.1: if command == "yes": self.pos = 5 self.quest = "incomplete" return(self.bandit_accept,"") elif command == "no": self.pos = 3 self.quest = "declined" return(self.bandit_decline,"Clearing") else: return("Bandit: Answer me yes or no.","") else: return ("I beg your pardon?", "")