def purge_test_database(): User.mdbc().remove() Ride.mdbc().remove() Terminal.mdbc().remove() User.setup_mongo_indexes() Ride.setup_mongo_indexes() Terminal.setup_mongo_indexes()
def ask(): print(Terminal.paintHeader("Please answer the following questions")) total_correct = 0 total_correct_by_user = 0 user_raw_input = None for i, question in enumerate(ALL_QUESTIONS): trials = 0 time.sleep(1) while True: print(f"\t{i+1}) {question.textContent} {max_trials - trials} trials left. Score: {total_correct_by_user}") correctAnswers = [] answers = question.allAnswers for idx, a in enumerate(answers): the_letter = ALL_LETTERS[idx] print(f"\t\t{the_letter}) {a['answer'].textContent}") if a['correctNess'] == True: correctAnswers.append(the_letter) user_raw_input = input("[AskLoop:] Enter answer: ") user_inputs = user_raw_input.split(',') if user_raw_input == 'quit': print("leaving while loop in ask()...") break num_correct = len(correctAnswers) num_correct_by_user = countHowManyCorrect(user_inputs, correctAnswers) print(f"You have chosen: {user_inputs}") allCorrect = num_correct_by_user == num_correct feedbackMsg = f'{Terminal.paintFail("INCORRECT!")} Your result is: ' if allCorrect: feedbackMsg = f'{Terminal.paintGreen("CORRECT!")} Your result is: ' print(f"{feedbackMsg}{num_correct_by_user}/{num_correct}") if allCorrect or trials == max_trials: total_correct += num_correct total_correct_by_user += num_correct_by_user break else: trials += 1 if user_raw_input == 'quit': print("leaving for loop in ask()...") break print(Terminal.paintSummary(f"\nFinal score: {total_correct_by_user}/{total_correct}\n")) return user_raw_input
def init_ui(self): self.setStyleSheet( "QWidget { background-color: #2B2B2B ; color: #A9B7C6}") self.terminal = Terminal() self.tree = QtWidgets.QTreeView() self.tree.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) self.model = QtWidgets.QFileSystemModel() self.index_model = QtCore.QModelIndex() self.model.setRootPath("") self.tree.setContextMenuPolicy(Qt.CustomContextMenu) self.tree.customContextMenuRequested.connect(self.open_menu) self.tree.setAnimated(True) self.tree.setIndentation(15) self.tree.setSortingEnabled(True) self.tree.setWindowTitle("Dir View") self.tree.doubleClicked.connect(self.tree_clk) self.get_open_path() if self.open_path is not None: self.terminal.executeCommand("cd " + self.open_path) self.init_text_edit() self.text_edit_layout = QtWidgets.QHBoxLayout() self.text_edit_layout.addWidget(self.number_bar) self.text_edit_layout.addWidget(self.text) self.grid = QtWidgets.QGridLayout() self.grid.addLayout(self.text_edit_layout, 0, 2, 4, 6) self.grid.addWidget(self.tree, 0, 0, 4, 2) self.grid.addWidget(self.terminal, 4, 0, 2, 8) self.setLayout(self.grid)
def trueFalseColored(input_bool): return Terminal.paintGreen( input_bool) if input_bool else Terminal.paintFail(input_bool)
def scrape_and_store_terminals(): all_airports_info = scrape_all_airports_info() response = Terminal.store_terminal_info(all_airports_info)
def get_terminals(self, airport): terminals = Terminal.get_terminal_info_by_airport(airport) if not terminals: return ApiResponse.TERMINAL_NOT_FOUND else: return terminals
from model.Terminal import Terminal print(Terminal.paintHeader(Terminal.writeHeader('Welcome to the Quiz!'))) print('Options:') print('\t- To give a quiz, write "go"') print('\t- To exit, write "quit"') while True: user_input = input("Enter option: ") if user_input == 'quit': print(Terminal.paintFooter(Terminal.writeFooter('Exitting'))) break elif user_input == 'go': print(Terminal.paintBlue("Starting quiz...")) else: print(Terminal.paintFail("Invalid input, try again..."))
- All questions and the answers of each """ print("") QA.displayAllQA() print("") Question.displayAllQuestions() print("") Answer.displayAllAnswers() print("") QA.showAllQA() """ STDIN Now we will ask and correct it """ print(Terminal.paintHeader(Terminal.writeHeader('Welcome to the Quiz!'))) print('Options:') print('\t- To create a quiz, write "create"') print('\t- To give a quiz, write "go"') print('\t- To exit, write "quit"') while True: user_input = input("[Mainloop] Enter option: ") if user_input == 'quit': print( Terminal.paintFooter( Terminal.writeFooter("[Mainloop] Leaving..."))) break elif user_input == 'create': all_answerDicts = [] print(Terminal.paintBlue("[Step 1]: Create a question"))
class TextEditor(QtWidgets.QWidget): def __init__(self): super(TextEditor, self).__init__() self.save_path = None self.open_path = None self.local_data = {} self.init_ui() def init_ui(self): self.setStyleSheet( "QWidget { background-color: #2B2B2B ; color: #A9B7C6}") self.terminal = Terminal() self.tree = QtWidgets.QTreeView() self.tree.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5()) self.model = QtWidgets.QFileSystemModel() self.index_model = QtCore.QModelIndex() self.model.setRootPath("") self.tree.setContextMenuPolicy(Qt.CustomContextMenu) self.tree.customContextMenuRequested.connect(self.open_menu) self.tree.setAnimated(True) self.tree.setIndentation(15) self.tree.setSortingEnabled(True) self.tree.setWindowTitle("Dir View") self.tree.doubleClicked.connect(self.tree_clk) self.get_open_path() if self.open_path is not None: self.terminal.executeCommand("cd " + self.open_path) self.init_text_edit() self.text_edit_layout = QtWidgets.QHBoxLayout() self.text_edit_layout.addWidget(self.number_bar) self.text_edit_layout.addWidget(self.text) self.grid = QtWidgets.QGridLayout() self.grid.addLayout(self.text_edit_layout, 0, 2, 4, 6) self.grid.addWidget(self.tree, 0, 0, 4, 2) self.grid.addWidget(self.terminal, 4, 0, 2, 8) self.setLayout(self.grid) def open_menu(self, position): self.contextMenu = QtWidgets.QMenu() actionNewFolder = QtWidgets.QAction("New Folder", None) actionNewFolder.triggered.connect(self.make_new_folder) actionNewFile = QtWidgets.QAction("New File", None) actionNewFile.triggered.connect(self.make_new_c_file) action_delete = QtWidgets.QAction("Delete", None) action_delete.triggered.connect(self.delete_node) action_refresh = QtWidgets.QAction("Refresh", None) action_refresh.triggered.connect(self.refresh_table) action_rename = QtWidgets.QAction("Rename", None) action_rename.triggered.connect(self.rename_node) if self.open_path is not None: self.contextMenu.addAction(actionNewFolder) self.contextMenu.addAction(actionNewFile) self.contextMenu.addAction(action_delete) if self.tree.currentIndex().internalPointer() is not None: self.contextMenu.addAction(action_rename) self.contextMenu.addAction(action_refresh) self.contextMenu.exec_(self.tree.viewport().mapToGlobal(position)) def refresh_table(self): self.tree.repaint() def delete_node(self): name = str(self.tree.currentIndex().data()) message = name + " will be deleted permanently !" flags = QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No button = QtWidgets.QMessageBox.question(self, "Are you sure ?", message, flags) if (button == QtWidgets.QMessageBox.Yes): base_path = self.open_path if self.tree.currentIndex().parent() != self.tree.rootIndex(): base_path = os.path.split( self.get_path_for_making(base_path))[0] del_path = os.path.join(base_path, name) try: shutil.rmtree(del_path) except NotADirectoryError: os.remove(del_path) self.refresh_table() if del_path == self.save_path: self.save_path = None def rename_node(self): ret_text, ok_button = QtWidgets.QInputDialog.getText( self, "Rename", "Insert new name: ") if ok_button: if ret_text.strip() == "": QtWidgets.QMessageBox.critical(self, "Error", "Empty directory name! ") return path = self.get_path_for_making(self.open_path) os.rename(path, os.path.join(os.path.split(path)[0], ret_text)) self.tree.repaint() def make_new_folder(self): base_path = self.open_path if self.tree.currentIndex().parent() != self.tree.rootIndex( ) or not "." in self.tree.currentIndex().data(): base_path = self.get_path_for_making(self.open_path) if "." in base_path: base_path = os.path.split(base_path)[0] ret_text, ok_button = QtWidgets.QInputDialog.getText( self, "Directory name", "Insert directory name: ") if ok_button: if ret_text.strip() == "": QtWidgets.QMessageBox.critical(self, "Error", "Empty directory name! ") return final_path = os.path.join(base_path, ret_text.split(".")[0]) os.mkdir(final_path) self.tree.repaint() def make_new_c_file(self): base_path = self.open_path if self.tree.currentIndex().parent() != self.tree.rootIndex( ) or not "." in self.tree.currentIndex().data(): base_path = self.get_path_for_making(self.open_path) if "." in base_path: base_path = os.path.split(base_path)[0] ret_text, ok_button = QtWidgets.QInputDialog.getText( self, "File name", "Insert file name: ") if ok_button: if ret_text.strip() == "": QtWidgets.QMessageBox.critical(self, "Error", "Empty file name! ") return if "." not in ret_text: ret_text = ret_text + ".c" final_path = os.path.join(base_path, ret_text) with open(final_path, "w") as new_file: pass self.refresh_table() return final_path return None def init_text_edit(self): self.completer = MyDictionaryCompleter() self.text = MyTextEdit(self) self.text.setCompleter(self.completer) self.text.setStyleSheet("QPlainTextEdit { color: rgb(169, 183, 198) }") self.number_bar = NumberBar() self.number_bar.setTextEdit(self.text) self.font = QtGui.QFont() self.font.setFamily('Ariel') self.font.setFixedPitch(True) self.font.setPointSize(10) self.text.setFont(self.font) self.highlighter = Highlighter(self.text.document()) self.text.textChanged.connect(self.save_locally) with open(os.path.join(start_directory, "../viewer/local_data.txt"), "r") as f: lines = f.read() self.text.setPlainText(lines) if lines.strip() == "" and self.save_path is not None: with open(self.save_path, "r") as f: lines = f.read() self.text.setPlainText(lines) new_completer = check_for_header_files(self.text.toPlainText(), self.completer.my_keywords) self.text.setCompleter(new_completer) self.text.installEventFilter(self) self.text.viewport().installEventFilter(self) def eventFilter(self, object, event): # Update the line numbers for all events on the text edit and the viewport. # This is easier than connecting all necessary singals. if object in (self.text, self.text.viewport()): self.number_bar.update() return False return QFrame.eventFilter(object, event) def set_font(self, value): if int(value) >= 8 and int(value) <= 50: self.font.setPointSize(int(value)) self.text.setFont(self.font) else: self.font.setPointSize(10) self.text.setFont(self.font) def save_locally(self): if self.save_path is None: return text = self.text.toPlainText() with open(os.path.join(start_directory, "../viewer/local_data.txt"), "w") as f: f.write(text) if self.save_path not in self.local_data.keys(): new_save = SaveNode(self.save_path) new_save.data = text self.local_data[self.save_path] = new_save else: if text != self.local_data[self.save_path].data: self.local_data[self.save_path].data = text self.local_data[self.save_path].saved = False # print(self.local_data[self.save_path].data) def save_file(self): if self.save_path is None: QtWidgets.QMessageBox.information( self, "Specify location", "Please specify a location where you would like to save.") if self.open_path is None or self.open_path == "": self.open_file() path = self.make_new_c_file() if path is None: return else: self.save_path = path self.tree.setCurrentIndex(self.model.index(self.save_path)) self.save_locally() try: with open(self.save_path, "w") as file: text = self.text.toPlainText() file.write(text) self.local_data[self.save_path].saved = True except: QtWidgets.QMessageBox.information( self, "Error", "You can't save if you don't specify location!") self.save_path = None def open_file(self): file_path = QtWidgets.QFileDialog.getExistingDirectory( self, "Open directory") if file_path == "": return self.save_path = None if self.open_path is None: self.tree.setModel(self.model) for i in range(1, 4): self.tree.hideColumn(i) self.open_path = file_path self.write_path() self.tree.setRootIndex(self.model.index(self.open_path)) self.terminal.executeCommand("cd " + self.open_path) self.update_hints() def make_new_project(self): file_path = QtWidgets.QFileDialog.getExistingDirectory( self, "Choose location") if file_path == "": return self.save_path = None if self.open_path is None: self.tree.setModel(self.model) for i in range(1, 4): self.tree.hideColumn(i) self.open_path = file_path self.write_path() self.tree.setRootIndex(self.model.index(self.open_path)) self.terminal.executeCommand("cd " + self.open_path) self.terminal.executeCommand("mkdir " + os.path.join(self.open_path, "Headers")) self.terminal.executeCommand("mkdir " + os.path.join(self.open_path, "Sources")) self.terminal.executeCommand("mkdir " + os.path.join(self.open_path, "Resources")) self.tree.setCurrentIndex( self.model.index(os.path.join(self.open_path, "Sources"))) base_path = self.open_path if self.tree.currentIndex().parent() != self.tree.rootIndex( ) or not "." in self.tree.currentIndex().data(): base_path = self.get_path_for_making(self.open_path) if "." in base_path: base_path = os.path.split(base_path)[0] print(base_path) final_path = os.path.join(base_path, "main.c") with open(final_path, "w") as new_file: new_file.write( """/* Thank you for using Brick, go into File -> New Project to get started, \n autocompleter shortcut is Ctrl + SPACE or Ctrl + E, press Ctrl + I to \n optimise and refresh imports. Enjoy <3 */\n\n#include <stdio.h>\n#include <stdlib.h>\n\nint main(void){\n printf("Hello World!");\n return 0;\n}""" ) new_file.flush() self.refresh_table() self.tree.setCurrentIndex( self.model.index(os.path.join(self.open_path, "Sources/main.c"))) base_path = self.get_path_for_making(self.open_path) self.save_path = base_path with open(self.save_path, "r") as file: text = file.read() new_save = SaveNode(self.save_path) new_save.data = text self.local_data[self.save_path] = new_save self.text.setPlainText(text) self.write_path() def write_path(self): with open(os.path.join(start_directory, "../viewer/temp.txt"), "w") as f: f.write(self.open_path + "|" + str(self.save_path)) def get_open_path(self): with open(os.path.join(start_directory, "../viewer/temp.txt"), "r") as f: path = f.read() if path == "": return paths = path.split("|") self.open_path = paths[0] self.save_path = paths[1] if self.open_path != "None": self.tree.setModel(self.model) for i in range(1, 4): self.tree.hideColumn(i) self.tree.setRootIndex(self.model.index(self.open_path)) else: self.open_path = None if self.save_path != "None": self.tree.setCurrentIndex(self.model.index(self.save_path)) else: self.save_path = None def get_path_for_making(self, base_path): if self.tree.currentIndex().data() is None: return self.open_path current = self.tree.currentIndex() path = [] while current != self.tree.rootIndex(): print(self.tree.currentIndex().data()) path.append(current.data()) current = current.parent() path.reverse() final_path = "" for node in path: final_path = final_path + "/" + node final_path = base_path + final_path return final_path def tree_clk(self): if self.tree.currentIndex().data().endswith( ".c") or self.tree.currentIndex().data().endswith(".h"): base_path = os.path.join(self.open_path, self.tree.currentIndex().data()) if self.tree.currentIndex().parent() != self.tree.rootIndex(): base_path = self.get_path_for_making(self.open_path) self.save_path = base_path if self.save_path not in self.local_data.keys(): with open(self.save_path, "r") as file: text = file.read() new_save = SaveNode(self.save_path) new_save.data = text self.local_data[self.save_path] = new_save self.text.setPlainText(text) else: self.text.setPlainText(self.local_data[self.save_path].data) self.write_path() self.update_hints() elif self.tree.currentIndex().data().endswith(".txt"): base_path = self.get_path_for_making(self.open_path) self.terminal.executeCommand("gedit " + base_path) def update_hints(self): new_completer = check_for_header_files(self.text.toPlainText(), self.completer.my_keywords) self.text.setCompleter(new_completer) def safe_to_close(self): for file in self.local_data.values(): if not file.saved: return False return True def get_unsaved_files(self): for_ret = [] for path in self.local_data.keys(): if not self.local_data[path].saved: for_ret.append(path.split("/")[-1]) return for_ret