Skip to content

Commit 9008398

Browse files
committed
refactor(gui-base64): clean up code
1 parent b877cc8 commit 9008398

1 file changed

Lines changed: 51 additions & 16 deletions

File tree

‎Base64_examples/gui_base64.py‎

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,49 @@
88
import traceback
99
import sys
1010

11-
from os.path import split as path_split
11+
from pathlib import Path
1212

1313
try:
14-
from PyQt6.QtWidgets import *
15-
from PyQt6.QtCore import *
14+
from PyQt6.QtWidgets import (
15+
QMessageBox,
16+
QWidget,
17+
QApplication,
18+
QPushButton,
19+
QComboBox,
20+
QSizePolicy,
21+
QCheckBox,
22+
QHBoxLayout,
23+
QVBoxLayout,
24+
QPlainTextEdit,
25+
QLabel,
26+
QSplitter,
27+
QErrorMessage,
28+
QTextEdit,
29+
)
30+
from PyQt6.QtCore import Qt, qVersion
1631

1732
QSizePolicy.Expanding = QSizePolicy.Policy.Expanding
1833
QSizePolicy.Preferred = QSizePolicy.Policy.Preferred
1934
Qt.TextSelectableByMouse = Qt.TextInteractionFlag.TextSelectableByMouse
2035

2136
except ImportError:
22-
from PyQt5.QtWidgets import *
23-
from PyQt5.QtCore import *
37+
from PyQt5.QtWidgets import (
38+
QMessageBox,
39+
QWidget,
40+
QApplication,
41+
QPushButton,
42+
QComboBox,
43+
QSizePolicy,
44+
QCheckBox,
45+
QHBoxLayout,
46+
QVBoxLayout,
47+
QPlainTextEdit,
48+
QLabel,
49+
QSplitter,
50+
QErrorMessage,
51+
QTextEdit,
52+
)
53+
from PyQt5.QtCore import Qt, qVersion
2454

2555

2656
def log_uncaught_exceptions(ex_cls, ex, tb) -> None:
@@ -35,10 +65,11 @@ def log_uncaught_exceptions(ex_cls, ex, tb) -> None:
3565
sys.excepthook = log_uncaught_exceptions
3666

3767

68+
# TODO:
3869
# from encodings.aliases import aliases
3970
# print(aliases)
4071
# OR:
41-
STANDART_ENCODINGS = [
72+
STANDART_ENCODINGS: list[str] = [
4273
"ascii",
4374
"big5",
4475
"big5hkscs",
@@ -138,7 +169,7 @@ class MainWindow(QWidget):
138169
def __init__(self) -> None:
139170
super().__init__()
140171

141-
self.setWindowTitle(path_split(__file__)[1])
172+
self.setWindowTitle(f"{Path(__file__).stem}. Qt v{qVersion()}")
142173

143174
self.button_direct = QPushButton()
144175
self.button_direct.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
@@ -175,11 +206,11 @@ def __init__(self) -> None:
175206
self.button_detail_error.setToolTip("Detail error")
176207
self.button_detail_error.clicked.connect(self.show_detail_error_massage)
177208

178-
self.last_error_message = None
179-
self.last_detail_error_message = None
209+
self.last_error_message: str | None = None
210+
self.last_detail_error_message: str | None = None
180211

181-
# True -- кодирование текста, False -- раскодирование
182-
self.direct_encode_text = True
212+
# True - кодирование текста, False - раскодирование
213+
self.is_direct_encode_text: bool = True
183214

184215
# Первый вызов, чтобы у кнопки появился текст (заодно это сменит режим кодирования)
185216
self.change_convert_direct()
@@ -214,7 +245,7 @@ def show_detail_error_massage(self) -> None:
214245
# выбрать тип текста, то делаем такой хак.
215246
mb.findChild(QTextEdit).setPlainText(message)
216247

217-
mb.exec_()
248+
mb.exec()
218249

219250
def input_text_changed(self) -> None:
220251
self.label_error.clear()
@@ -227,7 +258,7 @@ def input_text_changed(self) -> None:
227258
codec_name = self.cb_encoding.currentText()
228259
in_text = self.text_edit_input.toPlainText().encode(encoding=codec_name)
229260

230-
if self.direct_encode_text:
261+
if self.is_direct_encode_text:
231262
text = base64.b64encode(in_text)
232263
else:
233264
text = base64.b64decode(in_text)
@@ -256,19 +287,23 @@ def input_text_changed(self) -> None:
256287
self.label_error.setText("Error: " + self.last_error_message)
257288

258289
def change_convert_direct(self) -> None:
259-
self.direct_encode_text = not self.direct_encode_text
290+
self.is_direct_encode_text = not self.is_direct_encode_text
260291
self.button_direct.setText(
261-
"text -> base64" if self.direct_encode_text else "base64 -> text"
292+
"text -> base64" if self.is_direct_encode_text else "base64 -> text"
262293
)
263294

264295
self.input_text_changed()
265296

266297

267-
if __name__ == "__main__":
298+
def main() -> None:
268299
app = QApplication(sys.argv)
269300

270301
mw = MainWindow()
271302
mw.resize(650, 500)
272303
mw.show()
273304

274305
sys.exit(app.exec())
306+
307+
308+
if __name__ == "__main__":
309+
main()

0 commit comments

Comments
 (0)