def __init__(self): self.score = Database() self.root = Root(self) self.settings = Setting() self.timer = "" self.board = [] self.buttons = [] # This "fake" image fixes button size to pixels instead of text self.image = tk.PhotoImage(width=0, height=1)
def data(name, value=None, *, command=None, check=True, delete=False): import __main__ as main # Save PEP 3122! if "." in __name__: from .sqlite import Database else: from sqlite import Database if hasattr(main, "__file__"): cmd = os.path.basename(main.__file__) else: cmd = "?" if command is None: command = cmd elif check is True: if command != cmd: raise ValueError("%r != %r" % (command, cmd)) base = env("base") if not base: # or if base is None? raise ValueError("Missing SAXO_BASE") key = command + "." + name if value is not None: if not isinstance(value, str): raise ValueError("Expected value to be a str instance") if delete is True: raise ValueError("delete=True is incompatible with a value") path = os.path.join(base, "database.sqlite3") with Database(path) as db: if delete is True: query = "DELETE FROM saxo_data WHERE key = ?" db.execute(query, key) db.commit() return True elif value is not None: import sqlite3 try: db["saxo_data"].replace((key, value)) except sqlite3.OperationalError: db["saxo_data"].create(("key", "TEXT PRIMARY KEY"), ("value", str)) db["saxo_data"].replace((key, value)) else: query = "SELECT * FROM saxo_data where key = ?" # TODO: Table.__getitem__ try: result = list(db.query(query, key))[0][1] except: result = None return result
def database(name=None, dotdir=False): # Save PEP 3122! if "." in __name__: from .sqlite import Database else: from sqlite import Database if name is None: base = env("base") if base is None: if dotdir is True: base = os.path.expanduser("~/.saxo") else: raise ValueError("No SAXO_BASE found") name = os.path.join(base, "database.sqlite3") return Database(name)
from sqlite import Database vt = Database('voicetube.db') vt.table = 'Sound_Euphonium' vt.select() print(vt.fetchall())
class Menu: def __init__(self): self.score = Database() self.root = Root(self) self.settings = Setting() self.timer = "" self.board = [] self.buttons = [] # This "fake" image fixes button size to pixels instead of text self.image = tk.PhotoImage(width=0, height=1) # adds minesweeper buttons def start_game(self, size): self.board = MinesweeperBoard(self.settings.board_size[size], self.settings.board_size[size]) self.timer = Timer(self.root) for i in range(self.board.rows): temp_buttons = [] for j in range(self.board.columns): button = tk.Button(self.root, image=self.image, bg=self.settings.colors["button"], width=22, height=22, text=" ", compound="c") button.config( command=lambda btn=button, board_btn=self.board.buttons[i][ j], row=i, col=j: self.reveal_buttons(row, col)) button.bind("<Button-2>", self.place_flag) button.bind("<Button-3>", self.place_flag) button.place(x=self.board.buttons[i][j].x, y=self.board.buttons[i][j].y) temp_buttons.append(button) self.buttons.append(temp_buttons) # Reveals the pressed button and if it is empty triggers all around to reveal itself. def reveal_buttons(self, row, col): self.buttons[row][col].config(text=self.board.buttons[row][col].value, state=tk.DISABLED, relief=tk.RIDGE, disabledforeground="#000000") if self.board.buttons[row][col].value == " ": for i in range(-1, 2, 1): for j in range(-1, 2, 1): if self.board.rows > row+i >= 0 \ and self.board.columns > col+j >= 0 \ and self.board.buttons[row+i][col+j].value != "X" \ and self.buttons[row+i][col+j]["state"] != tk.DISABLED: self.buttons[row + i][col + j].config( text=self.board.buttons[row + i][col + j].value, state=tk.DISABLED, relief=tk.RIDGE, disabledforeground="#000000") self.reveal_buttons((row + i), (col + j)) self.game_status_check(row, col) # Checks the state of game. def game_status_check(self, row, col): if self.board.buttons[row][col].value == "X": self.buttons[row][col].config(bg=self.settings.colors["mine"]) self.end_game("lost") else: bombs_remaining = len(self.buttons) * len(self.buttons[0]) // 10 buttons_remaining = 0 for row in self.buttons: for button in row: if button["state"] != tk.DISABLED: buttons_remaining += 1 if bombs_remaining == buttons_remaining: self.end_game("won") # Toggles "flag" color on right/middle click def place_flag(self, event): if event.widget["bg"] == self.settings.colors[ "button"] and event.widget["state"] != tk.DISABLED: event.widget.configure(bg=self.settings.colors["flag"]) else: event.widget.configure(bg=self.settings.colors["button"]) # Triggers different things depending on condition def end_game(self, end_condition): self.timer.isActive = False if end_condition == "won": for row in self.buttons: for button in row: if button["state"] != tk.DISABLED: button.config(bg=self.settings.colors["victory"]) self.disable_all_buttons() self.check_highscore() elif end_condition == "lost": self.disable_all_buttons() elif end_condition == "quit": for row in range(self.board.rows): for col in range(self.board.columns): self.buttons[row][col].destroy() self.buttons = [] self.timer.time_label.destroy() self.root.main_menu_mode() # Checks if current time score is lower then top 3 highscore. def check_highscore(self): scores = self.score.fetch_by_size(self.root.size_button.get().lower()) if len(scores) < 3: self.root.minesweeper_highscore_mode() return for score in scores: if self.timer.time < score[2]: self.root.minesweeper_highscore_mode() break # Disables all minesweeper buttons def disable_all_buttons(self): for row in range(self.board.rows): for col in range(self.board.columns): self.buttons[row][col].config(state=tk.DISABLED, disabledforeground="#000000")
import requests from bs4 import BeautifulSoup from sqlite import Database def fetch_text(text): text = re.match(r"[\n\s]*(.+\w.)[\n\s]*", text).group(1) text = text.replace("'", "''") text = text.replace("\\", "\\\\") return text url = "https://tw.voicetube.com/videos/79574" selector = '#show-caption-table tr td:last-child' res = requests.get(url) soup = BeautifulSoup(res.text, 'html.parser') sequence = soup.select(selector) vt = Database('voicetube.db') vt.create('Sound_Euphonium', seq_id='integer', text='text') for index, obj in enumerate(sequence): text = fetch_text(obj.text) vt.insert(index, text) print(f"save {index}, {text}") vt.commit() vt.close()