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 でそのようなシナリオを管理する最善の方法は何ですか?