From 2398b5a6262f6e5595e4ebfa17c7a8748c8865d5 Mon Sep 17 00:00:00 2001 From: Dpeta <69427753+Dpeta@users.noreply.github.com> Date: Sun, 19 Feb 2023 21:10:35 +0100 Subject: [PATCH] Remove most unnecessary str() typecasts. --- convo.py | 25 +++++++++--------- dataobjs.py | 8 +++--- generic.py | 6 ++--- irc.py | 2 +- logviewer.py | 11 +++----- memos.py | 50 +++++++++++++++-------------------- menus.py | 70 +++++++++++++++++++++++-------------------------- parsetools.py | 19 ++++++-------- pytwmn.py | 18 ++++++------- randomer.py | 2 +- toast.py | 4 +-- user_profile.py | 2 +- 12 files changed, 99 insertions(+), 118 deletions(-) diff --git a/convo.py b/convo.py index 2ee33dc..c2879f1 100644 --- a/convo.py +++ b/convo.py @@ -218,7 +218,7 @@ class PesterTabWindow(QtWidgets.QFrame): i = self.tabs.tabAt(self.mapFromGlobal(QtGui.QCursor.pos())) if i == -1: i = self.tabs.currentIndex() - handle = str(self.tabs.tabText(i)) + handle = self.tabs.tabText(i) self.clearNewMessage(handle) def convoHasFocus(self, handle): @@ -267,13 +267,13 @@ class PesterTabWindow(QtWidgets.QFrame): self.tabs.setTabIcon(tabi, c.icon()) currenttabi = self.tabs.currentIndex() if currenttabi >= 0: - currentHandle = str(self.tabs.tabText(self.tabs.currentIndex())) + currentHandle = self.tabs.tabText(self.tabs.currentIndex()) self.setWindowIcon(self.convos[currentHandle].icon()) self.defaultTabTextColor = self.getTabTextColor() @QtCore.pyqtSlot(int) def tabClose(self, i): - handle = str(self.tabs.tabText(i)) + handle = self.tabs.tabText(i) self.mainwindow.waitingMessages.messageAnswered(handle) # print(self.convos.keys()) # I, legit don' t know why this is an issue, but, uh, yeah- @@ -296,7 +296,7 @@ class PesterTabWindow(QtWidgets.QFrame): return if self.currentConvo == convo: currenti = self.tabs.currentIndex() - currenth = str(self.tabs.tabText(currenti)) + currenth = self.tabs.tabText(currenti) self.currentConvo = self.convos[currenth] self.currentConvo.raiseChat() @@ -307,7 +307,7 @@ class PesterTabWindow(QtWidgets.QFrame): if self.changedTab: self.changedTab = False return - handle = str(self.tabs.tabText(i)) + handle = self.tabs.tabText(i) convo = self.convos[handle] if self.currentConvo: self.layout.removeWidget(self.currentConvo) @@ -344,7 +344,7 @@ class PesterMovie(QtGui.QMovie): if text.mainwindow.config.animations(): movie = self url = text.urls[movie].toString() - html = str(text.toHtml()) + html = text.toHtml() if html.find(url) != -1: try: # PyQt6 @@ -606,7 +606,7 @@ class PesterText(QtWidgets.QTextEdit): if url[0] == "#" and url != "#pesterchum": self.parent().mainwindow.showMemos(url[1:]) elif url[0] == "@": - handle = str(url[1:]) + handle = url[1:] self.parent().mainwindow.newConversation(handle) else: if event.modifiers() == QtCore.Qt.KeyboardModifier.ControlModifier: @@ -664,7 +664,7 @@ class PesterInput(QtWidgets.QLineEdit): def keyPressEvent(self, event): if event.key() == QtCore.Qt.Key.Key_Up: - text = str(self.text()) + text = self.text() next = self.parent().history.next(text) if next is not None: self.setText(next) @@ -1091,14 +1091,13 @@ class PesterConvo(QtWidgets.QFrame): @QtCore.pyqtSlot() def sentMessage(self): - # Offloaded to another function, like its sisters. - # Fetch the raw text from the input box. - text = self.textInput.text() - text = str(self.textInput.text()) + """Offloaded to another function, like its sisters. + Fetch the raw text from the input box. + """ return parsetools.kxhandleInput( self, - text, + self.textInput.text(), flavor="convo", irc_compatible=self.mainwindow.config.irc_compatibility_mode(), ) diff --git a/dataobjs.py b/dataobjs.py index ef0c5b4..721ee5e 100644 --- a/dataobjs.py +++ b/dataobjs.py @@ -147,7 +147,7 @@ class pesterQuirks: newlist = [] for i, o in enumerate(lexed): - if type(o) not in [str, str]: + if not isinstance(o, str): if i == 0: string = " " for p in prefix: @@ -250,7 +250,7 @@ class pesterQuirks: newlist.append(string) final = [] for n in newlist: - if type(n) in [str, str]: + if isinstance(n, str): final.extend(lexMessage(n)) else: final.append(n) @@ -456,7 +456,7 @@ class PesterProfile: opinit, self.colorhtml(), ", ".join(initials), - str(reason), + reason, ) ) else: @@ -477,7 +477,7 @@ class PesterProfile: opinit, self.colorhtml(), initials, - str(reason), + reason, ) ) diff --git a/generic.py b/generic.py index a013854..99316a4 100644 --- a/generic.py +++ b/generic.py @@ -39,7 +39,7 @@ class PesterList(list): class PesterIcon(QtGui.QIcon): def __init__(self, *x): super().__init__(x[0]) - if type(x[0]) in [str, str]: + if isinstance(x[0], str): self.icon_pixmap = QtGui.QPixmap(x[0]) else: self.icon_pixmap = None @@ -117,7 +117,7 @@ class MultiTextDialog(QtWidgets.QDialog): if r == QtWidgets.QDialog.DialogCode.Accepted: retval = {} for name, widget in self.inputs.items(): - retval[name] = str(widget.text()) + retval[name] = widget.text() return retval else: return None @@ -150,7 +150,7 @@ class MovingWindow(QtWidgets.QFrame): self.moving = event.globalPos() - self.pos() except AttributeError as e: print("PyQt <= 5.14?") - print(str(e)) + print(e) if event.button() == 1: self.moving = event.globalPos() - self.pos() diff --git a/irc.py b/irc.py index 371c327..948c2ee 100644 --- a/irc.py +++ b/irc.py @@ -437,7 +437,7 @@ class PesterIRC(QtCore.QThread): """Update and send color, slot is called from main thread.""" # Update color metadata field color = self.mainwindow.profile().color - self._send_irc.metadata("*", "set", "color", str(color.name())) + self._send_irc.metadata("*", "set", "color", color.name()) # Send color messages for convo in list(self.mainwindow.convos.keys()): self._send_irc.privmsg( diff --git a/logviewer.py b/logviewer.py index b3eb669..d6773af 100644 --- a/logviewer.py +++ b/logviewer.py @@ -42,10 +42,7 @@ class PesterLogHighlighter(QtGui.QSyntaxHighlighter): def highlightBlock(self, text): for i in range(0, len(text) - (len(self.searchTerm) - 1)): - if ( - str(text[i : i + len(self.searchTerm)]).lower() - == str(self.searchTerm).lower() - ): + if text[i : i + len(self.searchTerm)].lower() == self.searchTerm.lower(): self.setFormat(i, len(self.searchTerm), self.hilightstyle) @@ -297,7 +294,7 @@ class PesterLogViewer(QtWidgets.QDialog): if len(self.tree.currentItem().text(0)) > len("September 2011"): self.loadLog(self.timeToFile(self.tree.currentItem().text(0))) - def loadLog(self, fname): + def loadLog(self, fname: str): fp = codecs.open( "%s/%s/%s/%s/%s" % (self.logpath, self.handle, self.chum, self.format, fname), @@ -318,7 +315,7 @@ class PesterLogViewer(QtWidgets.QDialog): # textCur.movePosition(1) self.textArea.setTextCursor(textCur) self.instructions.setText( - "Pesterlog with " + self.chum + " on " + self.fileToTime(str(fname)) + "Pesterlog with " + self.chum + " on " + self.fileToTime(fname) ) def logSearch(self, search): @@ -359,7 +356,7 @@ class PesterLogText(PesterText): if url[0] == "#" and url != "#pesterchum": self.parent().parent.showMemos(url[1:]) elif url[0] == "@": - handle = str(url[1:]) + handle = url[1:] self.parent().parent.newConversation(handle) else: QtGui.QDesktopServices.openUrl( diff --git a/memos.py b/memos.py index e612e78..39f8f75 100644 --- a/memos.py +++ b/memos.py @@ -240,7 +240,7 @@ class TimeInput(QtWidgets.QLineEdit): @QtCore.pyqtSlot() def setSlider(self): - value = str(self.text()) + value = self.text() timed = txt2delta(value) if isinstance(timed, mysteryTime): self.timeslider.setValue(0) @@ -1008,10 +1008,9 @@ class PesterMemo(PesterConvo): PchumLog.debug("updateChanModes(%s, %s)", modes, op) if not hasattr(self, "modes"): self.modes = "" - chanmodes = list(str(self.modes)) + chanmodes = list((self.modes)) if chanmodes and chanmodes[0] == "+": chanmodes = chanmodes[1:] - modes = str(modes) if op: systemColor = QtGui.QColor(self.mainwindow.theme["memos/systemMsgColor"]) chum = self.mainwindow.profile() @@ -1428,7 +1427,7 @@ class PesterMemo(PesterConvo): @QtCore.pyqtSlot() def sentMessage(self): - text = str(self.textInput.text()) + text = self.textInput.text() return parsetools.kxhandleInput( self, @@ -1439,8 +1438,7 @@ class PesterMemo(PesterConvo): @QtCore.pyqtSlot(str) def namesUpdated(self, channel): - c = str(channel) - if c.lower() != self.channel.lower(): + if channel.lower() != self.channel.lower(): return # get namesdb (unused) # namesdb = self.mainwindow.namesdb @@ -1457,8 +1455,7 @@ class PesterMemo(PesterConvo): @QtCore.pyqtSlot(str) def closeInviteOnly(self, channel): - c = str(channel) - if c.lower() == self.channel.lower(): + if channel.lower() == self.channel.lower(): self.mainwindow.inviteOnlyChan[str].disconnect(self.closeInviteOnly) if self.parent(): PchumLog.info(self.channel) @@ -1470,7 +1467,7 @@ class PesterMemo(PesterConvo): msgbox.setStyleSheet( "QMessageBox{ %s }" % self.mainwindow.theme["main/defaultwindow/style"] ) - msgbox.setText("%s: Invites only!" % (c)) + msgbox.setText(f"{channel}: Invites only!") msgbox.setInformativeText( "This channel is invite-only. " "You must get an invitation from someone on the inside before entering." @@ -1480,8 +1477,7 @@ class PesterMemo(PesterConvo): @QtCore.pyqtSlot(str, str) def closeForbidden(self, channel, reason): - c = str(channel) - if c.lower() == self.channel.lower(): + if channel.lower() == self.channel.lower(): self.mainwindow.forbiddenChan[str, str].disconnect(self.closeForbidden) if self.parent(): PchumLog.info(self.channel) @@ -1493,7 +1489,7 @@ class PesterMemo(PesterConvo): msgbox.setStyleSheet( "QMessageBox{ %s }" % self.mainwindow.theme["main/defaultwindow/style"] ) - msgbox.setText("%s: D: CANT JOIN MEMO!!!" % (c)) + msgbox.setText(f"{channel}: D: CANT JOIN MEMO!!!") msgbox.setInformativeText(reason) msgbox.setStandardButtons(QtWidgets.QMessageBox.StandardButton.Ok) msgbox.exec() @@ -1568,12 +1564,9 @@ class PesterMemo(PesterConvo): del self.netsplit @QtCore.pyqtSlot(str, str, str) - def userPresentChange(self, handle, channel, update): - # print("handle: %s, channel: %s, update: %s" % (handle, channel, update)) - h = str(handle) - c = str(channel) - update = str(update) - # PchumLog.debug("h=%s\nc=%s\nupdate=%s" % (h,c,update)) + def userPresentChange(self, handle: str, channel: str, update: str): + h = handle + c = channel if update[0:4] == "kick": # yeah, i'm lazy. l = update.split(":") update = l[0] @@ -1790,7 +1783,7 @@ class PesterMemo(PesterConvo): for c in chums: c.op = True self.iconCrap(c) - if str(c.text()) == self.mainwindow.profile().handle: + if (c.text()) == self.mainwindow.profile().handle: self.userlist.optionsMenu.addAction(self.opAction) self.userlist.optionsMenu.addAction(self.voiceAction) self.userlist.optionsMenu.addAction(self.banuserAction) @@ -1807,7 +1800,7 @@ class PesterMemo(PesterConvo): for c in chums: c.op = False self.iconCrap(c) - if str(c.text()) == self.mainwindow.profile().handle: + if (c.text()) == self.mainwindow.profile().handle: self.userlist.optionsMenu.removeAction(self.opAction) self.userlist.optionsMenu.removeAction(self.voiceAction) self.userlist.optionsMenu.removeAction(self.banuserAction) @@ -1823,7 +1816,7 @@ class PesterMemo(PesterConvo): for c in chums: c.halfop = True self.iconCrap(c) - if str(c.text()) == self.mainwindow.profile().handle: + if (c.text()) == self.mainwindow.profile().handle: self.userlist.optionsMenu.addAction(self.opAction) self.userlist.optionsMenu.addAction(self.voiceAction) self.userlist.optionsMenu.addAction(self.banuserAction) @@ -1840,7 +1833,7 @@ class PesterMemo(PesterConvo): for c in chums: c.halfop = False self.iconCrap(c) - if str(c.text()) == self.mainwindow.profile().handle: + if (c.text()) == self.mainwindow.profile().handle: self.userlist.optionsMenu.removeAction(self.opAction) self.userlist.optionsMenu.removeAction(self.voiceAction) self.userlist.optionsMenu.removeAction(self.banuserAction) @@ -1888,21 +1881,21 @@ class PesterMemo(PesterConvo): user = self.userlist.currentItem() if not user: return - user = str(user.text()) + user = user.text() self.mainwindow.newConversation(user) @QtCore.pyqtSlot() def addChumSlot(self): if not self.userlist.currentItem(): return - currentChum = PesterProfile(str(self.userlist.currentItem().text())) + currentChum = PesterProfile((self.userlist.currentItem().text())) self.mainwindow.addChum(currentChum) @QtCore.pyqtSlot() def banSelectedUser(self): if not self.userlist.currentItem(): return - currentHandle = str(self.userlist.currentItem().text()) + currentHandle = self.userlist.currentItem().text() (reason, ok) = QtWidgets.QInputDialog.getText( self, "Ban User", "Enter the reason you are banning this user (optional):" ) @@ -1913,21 +1906,21 @@ class PesterMemo(PesterConvo): def opSelectedUser(self): if not self.userlist.currentItem(): return - currentHandle = str(self.userlist.currentItem().text()) + currentHandle = self.userlist.currentItem().text() self.mainwindow.setChannelMode.emit(self.channel, "+o", currentHandle) @QtCore.pyqtSlot() def voiceSelectedUser(self): if not self.userlist.currentItem(): return - currentHandle = str(self.userlist.currentItem().text()) + currentHandle = self.userlist.currentItem().text() self.mainwindow.setChannelMode.emit(self.channel, "+v", currentHandle) @QtCore.pyqtSlot() def killQuirkUser(self): if not self.userlist.currentItem(): return - currentHandle = str(self.userlist.currentItem().text()) + currentHandle = self.userlist.currentItem().text() self.mainwindow.killSomeQuirks.emit(self.channel, currentHandle) def resetSlider(self, time, send=True): @@ -1960,7 +1953,6 @@ class PesterMemo(PesterConvo): "Enter the chumhandle of the user you'd like to invite:", ) if ok: - chum = str(chum) self.mainwindow.inviteChum.emit(chum, self.channel) self.invitechums = None diff --git a/menus.py b/menus.py index ac32eff..c19cc3c 100644 --- a/menus.py +++ b/menus.py @@ -29,7 +29,7 @@ class PesterQuirkItem(QtWidgets.QTreeWidgetItem): parent = None QtWidgets.QTreeWidgetItem.__init__(self, parent) self.quirk = quirk - self.setText(0, str(quirk)) + self.setText(0, str(quirk)) # Typecast required. def update(self, quirk): self.quirk = quirk @@ -235,7 +235,6 @@ class PesterQuirkList(QtWidgets.QTreeWidget): self, "Add Group", "Enter a name for the new quirk group:" ) if ok: - gname = str(gname) if re.search(r"[^A-Za-z0-9_\s]", gname) is not None: msgbox = QtWidgets.QMessageBox() msgbox.setInformativeText("THIS IS NOT A VALID GROUP NAME") @@ -323,7 +322,7 @@ class QuirkTesterWindow(QtWidgets.QDialog): @QtCore.pyqtSlot() def sentMessage(self): - text = str(self.textInput.text()) + text = self.textInput.text() return parsetools.kxhandleInput( self, @@ -596,8 +595,8 @@ class PesterQuirkTypes(QtWidgets.QDialog): page.itemAt(3).layout().itemAt(0).widget().setCheckState( QtCore.Qt.CheckState(int(q["checkstate"])) ) - except (KeyError, ValueError) as e: - print("KeyError: %s" % str(e)) + except (KeyError, ValueError): + PchumLog.exception("Exception setting replace quirk.") elif q["type"] == "regexp": page.itemAt(2).layout().itemAt(1).layout().itemAt(1).widget().setText( q["from"] @@ -609,8 +608,8 @@ class PesterQuirkTypes(QtWidgets.QDialog): page.itemAt(2).layout().itemAt(3).layout().itemAt( 0 ).widget().setCheckState(QtCore.Qt.CheckState(int(q["checkstate"]))) - except (KeyError, ValueError) as e: - print("KeyError: %s" % str(e)) + except (KeyError, ValueError): + PchumLog.exception("Exception setting regexp quirk.") elif q["type"] == "random": self.regexp.setText(q["from"]) for v in q["randomlist"]: @@ -619,8 +618,8 @@ class PesterQuirkTypes(QtWidgets.QDialog): page.itemAt(2).layout().itemAt(2).layout().itemAt( 0 ).widget().setCheckState(QtCore.Qt.CheckState(int(q["checkstate"]))) - except (KeyError, ValueError) as e: - print("KeyError: %s" % str(e)) + except (KeyError, ValueError): + PchumLog.exception("Exception setting random quirk.") elif q["type"] == "spelling": self.slider.setValue(q["percentage"]) try: @@ -628,7 +627,7 @@ class PesterQuirkTypes(QtWidgets.QDialog): QtCore.Qt.CheckState(int(q["checkstate"])) ) except (KeyError, ValueError) as e: - print("KeyError: %s" % str(e)) + PchumLog.exception("Exception setting spelling quirk.") self.setLayout(layout_0) @@ -667,11 +666,11 @@ class PesterQuirkTypes(QtWidgets.QDialog): @QtCore.pyqtSlot(int) def printValue(self, value): - self.current.setText(str(value) + "%") + self.current.setText(f"{value}%") @QtCore.pyqtSlot() def addRandomString(self): - text = str(self.replaceinput.text()) + text = self.replaceinput.text() item = QtWidgets.QListWidgetItem(text, self.replacelist) self.replaceinput.setText("") self.replaceinput.setFocus() @@ -815,10 +814,10 @@ class PesterChooseQuirks(QtWidgets.QDialog): vdict["type"] = types[self.quirkadd.pages.currentIndex() - 1] page = self.quirkadd.pages.currentWidget().layout() if vdict["type"] in ("prefix", "suffix"): - vdict["value"] = str(page.itemAt(1).layout().itemAt(1).widget().text()) + vdict["value"] = page.itemAt(1).layout().itemAt(1).widget().text() elif vdict["type"] == "replace": - vdict["from"] = str(page.itemAt(1).layout().itemAt(1).widget().text()) - vdict["to"] = str(page.itemAt(2).layout().itemAt(1).widget().text()) + vdict["from"] = page.itemAt(1).layout().itemAt(1).widget().text() + vdict["to"] = page.itemAt(2).layout().itemAt(1).widget().text() try: # PyQt6 vdict["checkstate"] = str( @@ -830,10 +829,10 @@ class PesterChooseQuirks(QtWidgets.QDialog): page.itemAt(3).layout().itemAt(0).widget().checkState() ) elif vdict["type"] == "regexp": - vdict["from"] = str( + vdict["from"] = ( page.itemAt(2).layout().itemAt(1).layout().itemAt(1).widget().text() ) - vdict["to"] = str( + vdict["to"] = ( page.itemAt(2).layout().itemAt(2).layout().itemAt(1).widget().text() ) try: @@ -1069,7 +1068,7 @@ class PesterChooseProfile(QtWidgets.QDialog): @QtCore.pyqtSlot() def validateProfile(self): if not self.profileBox or self.profileBox.currentIndex() == 0: - handle = str(self.chumHandle.text()) + handle = self.chumHandle.text() if not PesterProfile.checkLength(handle): self.errorMsg.setText("PROFILE HANDLE IS TOO LONG") return @@ -1084,7 +1083,7 @@ class PesterChooseProfile(QtWidgets.QDialog): @QtCore.pyqtSlot() def deleteProfile(self): if self.profileBox and self.profileBox.currentIndex() > 0: - handle = str(self.profileBox.currentText()) + handle = self.profileBox.currentText() if handle == self.parent.profile().handle: problem = QtWidgets.QMessageBox() # karxi Will probably change this to its own name later. @@ -1175,7 +1174,7 @@ class PesterMentions(QtWidgets.QDialog): def addMention(self, mitem=None): d = {"label": "Mention:", "inputname": "value"} if mitem is not None: - d["value"] = str(mitem.text()) + d["value"] = mitem.text() pdict = MultiTextDialog("ENTER MENTION", self, d).getText() if pdict is None: return @@ -1730,7 +1729,7 @@ class PesterOptions(QtWidgets.QDialog): def addAutoJoin(self, mitem=None): d = {"label": "Memo:", "inputname": "value"} if mitem is not None: - d["value"] = str(mitem.text()) + d["value"] = mitem.text() pdict = MultiTextDialog("ENTER MEMO", self, d).getText() if pdict is None: return @@ -1773,7 +1772,7 @@ class PesterOptions(QtWidgets.QDialog): @QtCore.pyqtSlot(int) def printValue(self, v): - self.currentVol.setText(str(v) + "%") + self.currentVol.setText(f"{v}%") @QtCore.pyqtSlot() def openMentions(self): @@ -1796,7 +1795,7 @@ class PesterOptions(QtWidgets.QDialog): def updateMentions(self): m = [] for i in range(self.mentionmenu.mentionlist.count()): - m.append(str(self.mentionmenu.mentionlist.item(i).text())) + m.append((self.mentionmenu.mentionlist.item(i).text())) self.parent().userprofile.setMentions(m) self.mentionmenu = None @@ -1860,10 +1859,9 @@ class PesterUserlist(QtWidgets.QDialog): return self.userarea.clear() for n in names: - if ( - str(self.searchbox.text()) == "" - or n.lower().find(str(self.searchbox.text()).lower()) != -1 - ): + if (self.searchbox.text()) == "" or n.lower().find( + self.searchbox.text().lower() + ) != -1: # Strip channel membership prefixes n = n.strip("~").strip("@").strip("+").strip("&").strip("%") item = QtWidgets.QListWidgetItem(n) @@ -1875,18 +1873,16 @@ class PesterUserlist(QtWidgets.QDialog): @QtCore.pyqtSlot(str, str, str) def updateUserPresent(self, handle, channel, update): - h = str(handle) - c = str(channel) if update == "quit": - self.delUser(h) - elif update == "left" and c == "#pesterchum": - self.delUser(h) - elif update == "join" and c == "#pesterchum": + self.delUser(handle) + elif update == "left" and channel == "#pesterchum": + self.delUser(handle) + elif update == "join" and channel == "#pesterchum": if ( - str(self.searchbox.text()) == "" - or h.lower().find(str(self.searchbox.text()).lower()) != -1 + self.searchbox.text() == "" + or handle.lower().find(self.searchbox.text().lower()) != -1 ): - self.addUser(h) + self.addUser(handle) def addUser(self, name): item = QtWidgets.QListWidgetItem(name) @@ -1936,7 +1932,7 @@ class MemoListItem(QtWidgets.QTreeWidgetItem): def __lt__(self, other): column = self.treeWidget().sortColumn() - if str(self.text(column)).isdigit() and str(other.text(column)).isdigit(): + if (self.text(column)).isdigit() and (other.text(column)).isdigit(): return int(self.text(column)) < int(other.text(column)) return self.text(column) < other.text(column) diff --git a/parsetools.py b/parsetools.py index 3718938..c0fa2c5 100644 --- a/parsetools.py +++ b/parsetools.py @@ -66,7 +66,7 @@ def lexer(string, objlist): for oType, regexp in objlist: newstringlist = [] for stri, s in enumerate(stringlist): - if type(s) not in [str, str]: + if not isinstance(s, str): newstringlist.append(s) continue lasti = 0 @@ -268,9 +268,8 @@ class mecmd(lexercon.Chunk): kxpclexer = lexercon.Pesterchum() -def kxlexMsg(string): - # Do a bit of sanitization. - msg = str(string) +def kxlexMsg(msg: str): + """Do a bit of sanitization.""" # TODO: Let people paste line-by-line normally. Maybe have a mass-paste # right-click option? msg = msg.replace("\n", " ").replace("\r", " ") @@ -327,7 +326,7 @@ def balance(lexed): balanced.append(colorEnd("")) if len(balanced) == 0: balanced.append("") - if type(balanced[len(balanced) - 1]) not in [str, str]: + if not isinstance(balanced[len(balanced) - 1], str): balanced.append("") return balanced @@ -336,12 +335,12 @@ def convertTags(lexed, format="html"): if format not in ["html", "bbcode", "ctag", "text"]: raise ValueError("Color format not recognized") - if type(lexed) in [str, str]: + if isinstance(lexed, str): lexed = lexMessage(lexed) escaped = "" # firststr = True for i, o in enumerate(lexed): - if type(o) in [str, str]: + if isinstance(o, str): if format == "html": escaped += ( o.replace("&", "&").replace(">", ">").replace("<", "<") @@ -370,7 +369,7 @@ def _max_msg_len(mask=None, target=None, nick=None, ident=None): if mask is not None: # Since this will be included in what we send - limit -= len(str(mask)) + limit -= len(mask) else: # Since we should always be able to fetch this # karxi: ... Which we can't, right now, unlike in the old script. @@ -996,9 +995,7 @@ def parseRegexpFunctions(to): return parsed -def img2smiley(string): - string = str(string) - +def img2smiley(string: str): def imagerep(mo): return reverse_smiley[mo.group(1)] diff --git a/pytwmn.py b/pytwmn.py index b88023c..8543a1e 100755 --- a/pytwmn.py +++ b/pytwmn.py @@ -54,8 +54,8 @@ def init(host="127.0.0.1", port=None): class Notification: def __init__(self, title="", msg="", icon=""): - self.title = str(title) - self.msg = str(msg) + self.title = title + self.msg = msg if icon.startswith("file://"): icon = icon[7:] self.icon = icon @@ -68,16 +68,16 @@ class Notification: try: if self.time is None: s.send( - "" + self.title + "" - "" + self.msg + "" - "" + self.icon + "" + f"{self.title}" + f"{self.msg}" + f"{self.icon}" ) else: s.send( - "" + self.title + "" - "" + self.msg + "" - "" + self.icon + "" - "" + str(self.time) + "" + f"{self.title}" + f"{self.msg}" + f"{self.icon}" + f"{self.time}" ) except: raise TwmnError(TwmnError.NO_TWMND) diff --git a/randomer.py b/randomer.py index 7dc809f..4460d71 100644 --- a/randomer.py +++ b/randomer.py @@ -77,6 +77,6 @@ class RandomHandler(QtCore.QObject): msgbox.setInformativeText("Try again later :(") msgbox.exec() return - name = str(l[1]) + name = l[1] PchumLog.info("Random Encounter name is: %s", name) self.mainwindow.newConversation(name) diff --git a/toast.py b/toast.py index 04e914c..66afb06 100644 --- a/toast.py +++ b/toast.py @@ -303,7 +303,7 @@ class PesterToast(QtWidgets.QWidget, DefaultToast): self.msg.setText( PesterToast.wrapText( self.msg.font(), - str(self.msg.text()), + self.msg.text(), self.parent().theme["toasts/width"], self.parent().theme["toasts/content/style"], ) @@ -328,7 +328,7 @@ class PesterToast(QtWidgets.QWidget, DefaultToast): def done(self): QtWidgets.QWidget.hide(self) t = self.machine.toasts[0] - if t.title == str(self.title.text()) and t.msg == str(self.content): + if t.title == self.title.text() and t.msg == self.content: self.machine.toasts.pop(0) self.machine.displaying = False if self.machine.on: diff --git a/user_profile.py b/user_profile.py index d6b4db6..25abf07 100644 --- a/user_profile.py +++ b/user_profile.py @@ -621,7 +621,7 @@ class userProfile: self.chat = user self.userprofile = { "handle": user.handle, - "color": str(user.color.name()), + "color": user.color.name(), "quirks": [], "theme": "pesterchum", }