0

我正在为 Sublime Text 2 编写一个小插件。工作流程需要我多次提示用户以获得一系列答案。

我正在使用 show_input_panel 来获取输入,并且我正在定义一个函数来收集每个答案。

这是代码示例:

import sublime, sublime_plugin

class SampleCommand(sublime_plugin.TextCommand):

    #############################
    # Main
    #############################

    def run(self, edit):
        self.edit = edit

        self.window = sublime.active_window()

        # first prompt
        self.window.show_input_panel('First question', '', self.on_first_answer, None, None)

    #############################
    # Async Handlers
    #############################

    def on_first_answer(self, answer_a):
        self.answer_a = answer_a
        self.window.show_input_panel('Second question', '', self.on_second_answer, None, None)

    def on_second_answer(self, answer_b):
        self.answer_b = answer_b
        self.window.show_input_panel('Third question', '', self.on_third_answer, None, None)

    def on_third_answer(self, answer_c):
        answers = self.answer_a + self.answer_b + answer_c
        sublime.message_dialog('Your answers: ' + answers)

我是 Python 新手,所以我想知道是否有更好的方法来组织这些多个回调。

在 JavaScript 中,我会开始使用匿名闭包,这样我就不用在对象上存储每个答案。当闭包还不够时,我会尝试类似 promise 的东西。

在 Python 中管理这样的场景的最佳方法是什么?

4

1 回答 1

2

generator.send对于这种事情非常有用:

import sublime

def prompt_sequence(g):
    def progress(result):
        try:
            progress.caption, progress.initial_text = g.send(result)
            sublime.active_window().show_input_panel(
                progress.caption,
                progress.initial_text,
                progress, None, None
            )
        except StopIteration:
            pass

    progress(None)

def foo():
    first_answer = yield ('First question', '')
    second_answer = yield ('Second question', '')
    third_answer = yield ('Thirdquestion', '')

    sublime.message_dialog('Your answers: ' + answers)

prompt_sequence(foo())

或以另一种方式编写(可能不起作用):

def self_referencing(gen_func):
    @functools.wraps(gen_func)
    def wrapped(*args, **kwargs):
        g = gen_func(lambda: g, *args, **kwargs)
        return g
    return wrapped



class SampleCommand(sublime_plugin.TextCommand):
    @self_referencing
    def get_things(gen_self, self):
        sublime.active_window().show_input_panel(
            'First question', '',
            gen_self().send, None, None
        )
        result_a = yield
        sublime.active_window().show_input_panel(
            'Second question', '',
            gen_self().send, None, None
        )
        result_b = yield
        sublime.active_window().show_input_panel(
            'Third question', '',
            gen_self().send, None, None
        )
        result_c = yield
于 2013-07-06T22:12:55.983 回答