0

我正在发送一个 ajax 请求,其中包含一个包含字符串的“数据”字段,以及用户设置的多个选项。每个选项将是“启用”或“禁用”,并且将用于启用或禁用类中的功能。我想知道是否有更好的方法来组织此代码,而不涉及调用每个函数并检查它是启用还是禁用?

class Parse(webapp2.RequestHandler):
    def post(self):
        data = self.request.get("data")
        func1_option = self.request.get("func1_option") 
        func2_option = self.request.get("func2_option") 
        func3_option = self.request.get("func3_option") 

        newData = func1(func2(func3(data)))
        self.response.write(newData)

        def func1(foo):
            if funct1_option = "disabled":
                return foo
            else:
                return some.function(foo)

        def func2(foo):
            if funct2_option = "disabled":
                return foo
            else:
                return some.function(foo)

        def func3(foo):
            if funct3_option = "disabled":
                return foo
            else:
                return some.function(foo)
4

1 回答 1

1
class Parse(webapp2.RequestHandler):
    def post(self):
        data = self.request.get("data")

        # Note - order reversed
        funcs = [
            ("func3_option", some.other_other_function),
            ("func2_option", some.other_function),
            ("func1_option", some.function)
        ]
        for opt_name, func in funcs:
            if self.request.get(opt_name) != "disabled":
                data = func(data)

        self.response.write(data)
于 2013-05-05T00:11:12.060 回答