python - 我的 Python 代码还有改进的空间吗?

标签 python pygtk gedit

我正在使用 Python(和 PyGTK)开发 Gedit 插件,但我确实没有太多使用 Python,所以我不知道我是否在编写 Python 代码。

我自己的所有代码都包含在__init__.py中。还有一些其他文件,但它们来 self 正在连接的外部库。我的__init__.py如下:

#
# @file __init__.py
# Does the heavy lifting behind connecting Zen Coding to Gedit.
#

import gedit, gobject, string, gtk, re, zen_core

class ZenCodingPlugin(gedit.Plugin):
    """
    A Gedit plugin to implement Zen Coding's HTML and CSS shorthand expander.

    This file adds the menu items and keyboard shortcuts to the UI and connects
    those items with the good stuff (i.e., the code expansion).
    """

    def __init__(self):
        gedit.Plugin.__init__(self)

    def activate(self, window):
        "Gedit callback: install the expansion feature into the UI"

        ui_manager = window.get_ui_manager()
        action_group = gtk.ActionGroup("GeditZenCodingPluginActions")

        # Create the GTK action to be used to connect the key combo
        # to the Zen Coding expansion (i.e., the good stuff).
        complete_action = gtk.Action(name="ZenCodingAction",
                                     label="Expand Zen code...",
                                     tooltip="Expand Zen Code in document to raw HTML",
                                     stock_id=gtk.STOCK_GO_FORWARD)

        # Connect the newly created action with key combo
        complete_action.connect("activate",
                                lambda a: self.expand_zencode(window))
        action_group.add_action_with_accel(complete_action,
                                           "<Ctrl><Shift>E")

        ui_manager.insert_action_group(action_group, 0)

        # @TODO: Figure out what these lines do
        ui_merge_id = ui_manager.new_merge_id()
        ui_manager.add_ui(ui_merge_id,
                          "/MenuBar/EditMenu/EditOps_5",
                          "ZenCoding",
                          "ZenCodingAction",
                          gtk.UI_MANAGER_MENUITEM, False)
        ui_manager.__ui_data__ = (action_group, ui_merge_id)

    def deactivate(self, window):
        "Gedit callback: get rid of the expansion feature"

        ui_manager = window.get_ui_manager()
        (action_group, ui_merge_id) = ui_manager.__ui_data__

        # Remove the UI data, action group, and UI itself from Gedit
        del ui_manager.__ui_data__
        ui_manager.remove_action_group(action_group)
        ui_manager.remove_ui(ui_merge_id)


    def expand_zencode(self, window):
        "The action which handles the code expansion itself."

        view = window.get_active_view()
        buffer = view.get_buffer()

        # Grab the current cursor position.
        cursor_iter = buffer.get_iter_at_mark(buffer.get_insert())

        # Grab the first character in the line.
        line_iter = cursor_iter.copy()
        line_iter.set_line_offset(0)

        # Grab the text from the start of the line to the cursor.
        line = buffer.get_text(line_iter, cursor_iter)

        # Find the last space in the line and remove it, setting a variable
        # 'before' to the current line.
        words = line.split(" ")
        before = words[-1].lstrip()
        if not before:
            return

        # Get the language of the current document. Second line prevents an error
        # if first line returns None.
        lang = window.get_active_document().get_language()
        lang = lang and lang.get_name()

        # Using the 'before' variable, convert it from Zen Code
        # to expanded code. If there isn't anything, just return.
        if lang == 'CSS':
            after = zen_core.expand_abbreviation(before,'css','xhtml')
        else:
            after = zen_core.expand_abbreviation(before,'html','xhtml')
        if not after:
            return

        # Grab the line's indentation and store it.
        indent = re.match(r"\s*", line).group()

        # Automatically indent the string and replace \t (tab) with the
        # correct number of spaces.
        after = zen_core.pad_string(after,indent)
        if view.get_insert_spaces_instead_of_tabs():
            tabsize = view.get_tab_width()
            spaces = " " * tabsize
            after = after.replace("\t",spaces)

        # We are currently lame and do not know how to do placeholders.
        # So remove all | characters from after.
        after = after.replace("|", "")

        # Delete the last word in the line (i.e., the 'before' text, aka the
        # Zen un-expanded code), so that we can replace it.
        word_iter = cursor_iter.copy()
        position_in_line = cursor_iter.get_line_index() - len(before)
        word_iter.set_line_index(position_in_line)
        buffer.delete(word_iter, cursor_iter)

        # Insert the new expanded text.
        buffer.insert_at_cursor(after)

我只是问,因为上面的内容看起来不太面向对象,而且在我看来,将这么多逻辑放入 __init__.py 中是一个坏主意,但在这方面是新手,我不确定。

还有改进的空间吗?如果是这样,怎么办?

(我试图回避该插件的实际功能,因为我更多地寻找编码风格审查而不是对数审查,但如果您需要查看外部库中的代码,整个插件是在here)

最佳答案

我从未做过 GEdit 插件,所以我无法评论 __init__.py 问题,但一般说明:

  • 在没有新参数的情况下调用父级的 __init__ 不是多余的吗——你不能去掉这两行吗?

  • 你创建的局部变量比我多。我必须不断环顾四周,看看某个值从哪里来,或者它是否被再次使用。例如:

    tabsize = view.get_tab_width()
    spaces = " " * tabsize
    after = after.replace("\t",spaces)
    

    可能是:

    after = after.replace("\t", " " * view.get_tab_width())
    
  • 这里有一点冗余:

    if lang == 'CSS':
        after = zen_core.expand_abbreviation(before,'css','xhtml')
    else:
        after = zen_core.expand_abbreviation(before,'html','xhtml')
    

    比较:

    after = zen_core.expand_abbreviation(before, 'css' if lang == 'CSS' else 'html', 'xhtml')
    

除此之外,在我看来,它看起来是相当不错的 Python 代码。

关于python - 我的 Python 代码还有改进的空间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2284809/

相关文章:

python - 将数百万个文件移动到 Amazon Glacier

Python-如何比较字符串中两个列表的内容

colors - Pygtk:更改小部件的边框颜色

python - GEdit/Python 执行插件?

notepad++ - Notepad++ 中的那些垂直线叫什么?那些因匹配括号或标签等而亮起的

python - 如何修复已弃用的 plotly.plotly 模块

python - 等待不终止的子进程的输出

python - 运行过程中进度条不更新

python - 无法在 Python 中导入 GST

Ruby:空间可能出现的 Ubuntu Gedit 问题