[Python] How to Optimize PyQt6 Applicaitons

Discussão em 'Python' iniciado por Stack, Junho 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm building Python IDE and i ran into this problem. there are some function that makes my IDE delay, lags and freezes, i did some debugging and i found what makes my app freezes its this function

    import ast


    func_class_instances = {}

    def list_classes_functions(code):
    try:
    func_class_instances.clear()
    tree = ast.parse(code)

    for node in ast.walk(tree):
    if isinstance(node, ast.Assign):
    if isinstance(node.value, ast.Call) and isinstance(node.value.func, ast.Name):
    class_name = node.value.func.id
    if class_name == 'int' or class_name == 'len' or class_name == 'str':
    continue
    else:
    func_class_instances[class_name] = 'class'


    elif isinstance(node, ast.Call):
    func = node.func
    if isinstance(func, ast.Attribute):
    func_class_instances[func.attr] = 'function'


    return func_class_instances

    except Exception:
    func_class_instances.clear()
    return func_class_instances


    this code return a dict of funciton calls and classes called in the current file.

    here is how i used it:

    import ast
    from PyQt6.QtGui import QTextCursor, QColor, QTextCharFormat, QTextCursor, QColor
    from PyQt6.QtCore import QTimer

    from func_classes import list_classes_functions


    class ShowErrors:
    def __init__(self, parent, highlighter):
    parent.textChanged.connect(self.schedule_check)
    self.timer = QTimer()
    self.timer.setSingleShot(True)
    self.timer.timeout.connect(self.check_syntax)
    self.error_label = None
    self.parent = parent
    self.highlighter = highlighter

    def schedule_check(self):
    self.timer.start(500)

    def check_syntax(self):
    code = self.parent.toPlainText()


    self.clear_error_highlighting()
    try:
    ast.parse(code)
    if self.error_label:
    self.error_label.setText("✅ No syntax errors")

    self.analyze_code(self.parent)


    except (SyntaxError, NameError) as e:
    if self.error_label:
    self.error_label.setText(f"❌ Line {e.lineno}: {e.msg}")

    self.underline_error(e.lineno, e.offset)

    def clear_error_highlighting(self):
    cursor = self.parent.textCursor()

    cursor.select(QTextCursor.SelectionType.Document)
    fmt = QTextCharFormat()
    fmt.setUnderlineStyle(QTextCharFormat.UnderlineStyle.NoUnderline)
    cursor.setCharFormat(fmt)

    def underline_error(self, line, column):
    cursor = self.parent.textCursor()

    cursor.movePosition(QTextCursor.MoveOperation.Start)
    for _ in range(line - 1):
    cursor.movePosition(QTextCursor.MoveOperation.Down)

    cursor.movePosition(QTextCursor.MoveOperation.Right, n=column - 1)
    cursor.movePosition(QTextCursor.MoveOperation.Right, QTextCursor.MoveMode.KeepAnchor)

    fmt = QTextCharFormat()
    fmt.setUnderlineStyle(QTextCharFormat.UnderlineStyle.SpellCheckUnderline)
    fmt.setUnderlineColor(QColor("red"))
    cursor.setCharFormat(fmt)

    def analyze_code(self, main_text):
    code = main_text.toPlainText()
    self.highlighter.set_code(code)

    instances = list_classes_functions(code)
    self.highlighter.get_calls(instances)

    self.highlighter.rehighlight()


    and a second function is the highlighting logic which highlights keywords, functions, etc.

    cant really show the code here its 300+lines but here is the github repo.

    tried to make it update if only the dict has been changed but it did not worked

    Continue reading...

Compartilhe esta Página