From daccbbd9c73a5292ea9d6746560d7009e5aa666d Mon Sep 17 00:00:00 2001 From: Terry Truong Date: Wed, 7 Sep 2022 11:37:37 +1000 Subject: Add python type annotations Also use consistent quote symbols Also use 'is None' instead of '== None' Also use 'if list1' instead of 'if len(list1) > 0' --- backend/tolData/reviewImgsToGen.py | 118 ++++++++++++++++++------------------- 1 file changed, 59 insertions(+), 59 deletions(-) (limited to 'backend/tolData/reviewImgsToGen.py') diff --git a/backend/tolData/reviewImgsToGen.py b/backend/tolData/reviewImgsToGen.py index dcf18bc..f3791bc 100755 --- a/backend/tolData/reviewImgsToGen.py +++ b/backend/tolData/reviewImgsToGen.py @@ -1,6 +1,6 @@ #!/usr/bin/python3 -import sys, re, os, time +import os, time import sqlite3 import tkinter as tki from tkinter import ttk @@ -20,25 +20,25 @@ have already been made. """, formatter_class=argparse.RawDescriptionHelpFormatter) parser.parse_args() -eolImgDir = "eol/imgs/" -enwikiImgDir = "enwiki/imgs/" -dbFile = "data.db" -outFile = "imgList.txt" +eolImgDir = 'eol/imgs/' +enwikiImgDir = 'enwiki/imgs/' +dbFile = 'data.db' +outFile = 'imgList.txt' IMG_DISPLAY_SZ = 400 -PLACEHOLDER_IMG = Image.new("RGB", (IMG_DISPLAY_SZ, IMG_DISPLAY_SZ), (88, 28, 135)) +PLACEHOLDER_IMG = Image.new('RGB', (IMG_DISPLAY_SZ, IMG_DISPLAY_SZ), (88, 28, 135)) onlyReviewPairs = True -print("Opening database") +print('Opening database') dbCon = sqlite3.connect(dbFile) dbCur = dbCon.cursor() -nodeToImgs = {} # Maps otol-ids to arrays of image paths -print("Iterating through images from EOL") +nodeToImgs: dict[str, list[str]] = {} # Maps otol-ids to arrays of image paths +print('Iterating through images from EOL') if os.path.exists(eolImgDir): for filename in os.listdir(eolImgDir): # Get associated EOL ID - eolId, _, _ = filename.partition(" ") - query = "SELECT nodes.id FROM nodes INNER JOIN eol_ids ON nodes.name = eol_ids.name WHERE eol_ids.id = ?" + eolId, _, _ = filename.partition(' ') + query = 'SELECT nodes.id FROM nodes INNER JOIN eol_ids ON nodes.name = eol_ids.name WHERE eol_ids.id = ?' # Get associated node IDs found = False for (otolId,) in dbCur.execute(query, (int(eolId),)): @@ -47,15 +47,15 @@ if os.path.exists(eolImgDir): nodeToImgs[otolId].append(eolImgDir + filename) found = True if not found: - print(f"WARNING: No node found for {eolImgDir}{filename}") -print(f"Result: {len(nodeToImgs)} nodes with images") -print("Iterating through images from Wikipedia") + print(f'WARNING: No node found for {eolImgDir}{filename}') +print(f'Result: {len(nodeToImgs)} nodes with images') +print('Iterating through images from Wikipedia') if os.path.exists(enwikiImgDir): for filename in os.listdir(enwikiImgDir): # Get associated page ID - (wikiId, _, _) = filename.partition(".") + wikiId, _, _ = filename.partition('.') # Get associated node IDs - query = "SELECT nodes.id FROM nodes INNER JOIN wiki_ids ON nodes.name = wiki_ids.name WHERE wiki_ids.id = ?" + query = 'SELECT nodes.id FROM nodes INNER JOIN wiki_ids ON nodes.name = wiki_ids.name WHERE wiki_ids.id = ?' found = False for (otolId,) in dbCur.execute(query, (int(wikiId),)): if otolId not in nodeToImgs: @@ -63,34 +63,34 @@ if os.path.exists(enwikiImgDir): nodeToImgs[otolId].append(enwikiImgDir + filename) found = True if not found: - print(f"WARNING: No node found for {enwikiImgDir}{filename}") -print(f"Result: {len(nodeToImgs)} nodes with images") -print("Filtering out already-made image choices") + print(f'WARNING: No node found for {enwikiImgDir}{filename}') +print(f'Result: {len(nodeToImgs)} nodes with images') +print('Filtering out already-made image choices') oldSz = len(nodeToImgs) if os.path.exists(outFile): with open(outFile) as file: for line in file: line = line.rstrip() - if " " in line: - line = line[:line.find(" ")] + if ' ' in line: + line = line[:line.find(' ')] del nodeToImgs[line] -print(f"Filtered out {oldSz - len(nodeToImgs)} entries") +print(f'Filtered out {oldSz - len(nodeToImgs)} entries') class ImgReviewer: - " Provides the GUI for reviewing images " + """ Provides the GUI for reviewing images """ def __init__(self, root, nodeToImgs): self.root = root - root.title("Image Reviewer") + root.title('Image Reviewer') # Setup main frame - mainFrame = ttk.Frame(root, padding="5 5 5 5") + mainFrame = ttk.Frame(root, padding='5 5 5 5') mainFrame.grid(column=0, row=0, sticky=(tki.N, tki.W, tki.E, tki.S)) root.columnconfigure(0, weight=1) root.rowconfigure(0, weight=1) # Set up images-to-be-reviewed frames self.eolImg = ImageTk.PhotoImage(PLACEHOLDER_IMG) self.enwikiImg = ImageTk.PhotoImage(PLACEHOLDER_IMG) - self.labels = [] + self.labels: list[ttk.Label] = [] for i in (0, 1): frame = ttk.Frame(mainFrame, width=IMG_DISPLAY_SZ, height=IMG_DISPLAY_SZ) frame.grid(column=i, row=0) @@ -101,10 +101,10 @@ class ImgReviewer: for child in mainFrame.winfo_children(): child.grid_configure(padx=5, pady=5) # Add keyboard bindings - root.bind("", self.quit) - root.bind("", lambda evt: self.accept(0)) - root.bind("", lambda evt: self.accept(1)) - root.bind("", lambda evt: self.reject()) + root.bind('', self.quit) + root.bind('', lambda evt: self.accept(0)) + root.bind('', lambda evt: self.accept(1)) + root.bind('', lambda evt: self.reject()) # Set fields self.nodeImgsList = list(nodeToImgs.items()) self.listIdx = -1 @@ -116,94 +116,94 @@ class ImgReviewer: # Initialise images to review self.getNextImgs() def getNextImgs(self): - " Updates display with new images to review, or ends program " + """ Updates display with new images to review, or ends program """ # Get next image paths while True: self.listIdx += 1 if self.listIdx == len(self.nodeImgsList): - print("No more images to review. Exiting program.") + print('No more images to review. Exiting program.') self.quit() return self.otolId, imgPaths = self.nodeImgsList[self.listIdx] # Potentially skip user choice if onlyReviewPairs and len(imgPaths) == 1: with open(outFile, 'a') as file: - file.write(f"{self.otolId} {imgPaths[0]}\n") + file.write(f'{self.otolId} {imgPaths[0]}\n') continue break # Update displayed images self.eolImgPath = self.enwikiImgPath = None imageOpenError = False for imgPath in imgPaths: - img = None + img: Image try: img = Image.open(imgPath) img = ImageOps.exif_transpose(img) except PIL.UnidentifiedImageError: - print(f"UnidentifiedImageError for {imgPath}") + print(f'UnidentifiedImageError for {imgPath}') imageOpenError = True continue - if imgPath.startswith("eol/"): + if imgPath.startswith('eol/'): self.eolImgPath = imgPath self.eolImg = ImageTk.PhotoImage(self.resizeImgForDisplay(img)) - elif imgPath.startswith("enwiki/"): + elif imgPath.startswith('enwiki/'): self.enwikiImgPath = imgPath self.enwikiImg = ImageTk.PhotoImage(self.resizeImgForDisplay(img)) else: - print(f"Unexpected image path {imgPath}") + print(f'Unexpected image path {imgPath}') self.quit() return # Re-iterate if all image paths invalid - if self.eolImgPath == None and self.enwikiImgPath == None: + if self.eolImgPath is None and self.enwikiImgPath is None: if imageOpenError: self.reject() self.getNextImgs() return # Add placeholder images - if self.eolImgPath == None: + if self.eolImgPath is None: self.eolImg = ImageTk.PhotoImage(self.resizeImgForDisplay(PLACEHOLDER_IMG)) - elif self.enwikiImgPath == None: + elif self.enwikiImgPath is None: self.enwikiImg = ImageTk.PhotoImage(self.resizeImgForDisplay(PLACEHOLDER_IMG)) # Update image-frames self.labels[0].config(image=self.eolImg) self.labels[1].config(image=self.enwikiImg) # Update title - title = f"Images for otol ID {self.otolId}" - query = "SELECT names.alt_name FROM" \ - " nodes INNER JOIN names ON nodes.name = names.name" \ - " WHERE nodes.id = ? and pref_alt = 1" + title = f'Images for otol ID {self.otolId}' + query = 'SELECT names.alt_name FROM' \ + ' nodes INNER JOIN names ON nodes.name = names.name' \ + ' WHERE nodes.id = ? and pref_alt = 1' row = dbCur.execute(query, (self.otolId,)).fetchone() - if row != None: - title += f", aka {row[0]}" - title += f" ({self.listIdx + 1} out of {len(self.nodeImgsList)})" + if row is not None: + title += f', aka {row[0]}' + title += f' ({self.listIdx + 1} out of {len(self.nodeImgsList)})' self.root.title(title) def accept(self, imgIdx): - " React to a user selecting an image " + """ React to a user selecting an image """ imgPath = self.eolImgPath if imgIdx == 0 else self.enwikiImgPath - if imgPath == None: - print("Invalid selection") + if imgPath is None: + print('Invalid selection') return with open(outFile, 'a') as file: - file.write(f"{self.otolId} {imgPath}\n") + file.write(f'{self.otolId} {imgPath}\n') self.numReviewed += 1 self.getNextImgs() def reject(self): - " React to a user rejecting all images of a set " + """"" React to a user rejecting all images of a set """ with open(outFile, 'a') as file: - file.write(f"{self.otolId}\n") + file.write(f'{self.otolId}\n') self.numReviewed += 1 self.getNextImgs() def quit(self, e = None): global dbCon - print(f"Number reviewed: {self.numReviewed}") + print(f'Number reviewed: {self.numReviewed}') timeElapsed = time.time() - self.startTime - print(f"Time elapsed: {timeElapsed:.2f} seconds") + print(f'Time elapsed: {timeElapsed:.2f} seconds') if self.numReviewed > 0: - print(f"Avg time per review: {timeElapsed/self.numReviewed:.2f} seconds") + print(f'Avg time per review: {timeElapsed/self.numReviewed:.2f} seconds') dbCon.close() self.root.destroy() def resizeImgForDisplay(self, img): - " Returns a copy of an image, shrunk to fit it's frame (keeps aspect ratio), and with a background " + """ Returns a copy of an image, shrunk to fit it's frame (keeps aspect ratio), and with a background """ if max(img.width, img.height) > IMG_DISPLAY_SZ: if (img.width > img.height): newHeight = int(img.height * IMG_DISPLAY_SZ/img.width) @@ -217,7 +217,7 @@ class ImgReviewer: int((IMG_DISPLAY_SZ - img.height) / 2))) return bgImg # Create GUI and defer control -print("Starting GUI") +print('Starting GUI') root = tki.Tk() ImgReviewer(root, nodeToImgs) root.mainloop() -- cgit v1.2.3