如何使用 Flask 框架在 python 中编写 switch 语句?

How can i write a switch statement in python using flask framework?

这是代码

switcher = {
    "1": func1(),
    "2": func2(),
    "3": func3(),
    "4": func4(),
    "5": func5(),
    "6": func6(),
    "9": func7(),
    "11": func8(),
    "12":func9(),
    "13": func10()
}

@app.route('/ussdcallback', methods=['GET', 'POST'])
def switch(text = request.values.get("text", None)):
    session_id = request.values.get("sessionId", None)
    service_code = request.values.get("serviceCode", None)
    func = switcher.get(text, "none")
    return func()

这是我得到的输出

Traceback (most recent call last):
2020-05-19T05:44:17.416279+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2020-05-19T05:44:17.416279+00:00 app[web.1]:     worker.init_process()
2020-05-19T05:44:17.416280+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 119, in init_process
2020-05-19T05:44:17.416280+00:00 app[web.1]:     self.load_wsgi()
2020-05-19T05:44:17.416280+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2020-05-19T05:44:17.416280+00:00 app[web.1]:     self.wsgi = self.app.wsgi()
2020-05-19T05:44:17.416281+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
2020-05-19T05:44:17.416281+00:00 app[web.1]:     self.callable = self.load()
2020-05-19T05:44:17.416281+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2020-05-19T05:44:17.416281+00:00 app[web.1]:     return self.load_wsgiapp()
2020-05-19T05:44:17.416282+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2020-05-19T05:44:17.416282+00:00 app[web.1]:     return util.import_app(self.app_uri)
2020-05-19T05:44:17.416282+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/gunicorn/util.py", line 358, in import_app
2020-05-19T05:44:17.416282+00:00 app[web.1]:     mod = importlib.import_module(module)
2020-05-19T05:44:17.416283+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
2020-05-19T05:44:17.416284+00:00 app[web.1]:     return _bootstrap._gcd_import(name[level:], package, level)
2020-05-19T05:44:17.416285+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2020-05-19T05:44:17.416285+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2020-05-19T05:44:17.416285+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2020-05-19T05:44:17.416285+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2020-05-19T05:44:17.416286+00:00 app[web.1]:   File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2020-05-19T05:44:17.416286+00:00 app[web.1]:   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2020-05-19T05:44:17.416286+00:00 app[web.1]:   File "/app/run.py", line 1, in <module>
2020-05-19T05:44:17.416286+00:00 app[web.1]:     from app import app
2020-05-19T05:44:17.416286+00:00 app[web.1]:   File "/app/app/__init__.py", line 359, in <module>
2020-05-19T05:44:17.416287+00:00 app[web.1]:     from app import routes, usrmgt, safeDeposit
2020-05-19T05:44:17.416287+00:00 app[web.1]:   File "/app/app/routes.py", line 22, in <module>
2020-05-19T05:44:17.416287+00:00 app[web.1]:     "1": radar(),
2020-05-19T05:44:17.416287+00:00 app[web.1]:   File "/app/app/radar.py", line 161, in radar
2020-05-19T05:44:17.416287+00:00 app[web.1]:     resp = make_response(response, 200)
2020-05-19T05:44:17.416288+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/helpers.py", line 223, in make_response
2020-05-19T05:44:17.416288+00:00 app[web.1]:     return current_app.make_response(args)
2020-05-19T05:44:17.416288+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
2020-05-19T05:44:17.416288+00:00 app[web.1]:     return getattr(self._get_current_object(), name)
2020-05-19T05:44:17.416288+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/werkzeug/local.py", line 306, in _get_current_object
2020-05-19T05:44:17.416289+00:00 app[web.1]:     return self.__local()
2020-05-19T05:44:17.416289+00:00 app[web.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/flask/globals.py", line 52, in _find_app
2020-05-19T05:44:17.416311+00:00 app[web.1]:     raise RuntimeError(_app_ctx_err_msg)
2020-05-19T05:44:17.416312+00:00 app[web.1]: RuntimeError: Working outside of application context.
2020-05-19T05:44:17.416312+00:00 app[web.1]: 
2020-05-19T05:44:17.416313+00:00 app[web.1]: This typically means that you attempted to use functionality that needed
2020-05-19T05:44:17.416313+00:00 app[web.1]: to interface with the current application object in some way. To solve
2020-05-19T05:44:17.416313+00:00 app[web.1]: this, set up an application context with app.app_context().  See the
2020-05-19T05:44:17.416319+00:00 app[web.1]: documentation for more information.
2020-05-19T05:44:17.416716+00:00 app[web.1]: [2020-05-19 05:44:17 +0000] [11] [INFO] Worker exiting (pid: 11)
2020-05-19T05:44:18.050667+00:00 app[web.1]: [2020-05-19 05:44:18 +0000] [4] [INFO] Shutting down: Master
2020-05-19T05:44:18.050834+00:00 app[web.1]: [2020-05-19 05:44:18 +0000] [4] [INFO] Reason: Worker failed to boot.
2020-05-19T05:44:18.145123+00:00 heroku[web.1]: Process exited with status 3
2020-05-19T05:44:18.190532+00:00 heroku[web.1]: State changed from up to crashed

TL;DR - 在 Flask 处理请求之前,您正在使用 request "thread"-local,导致此错误。您不想使用线程局部值来定义函数参数输入。

您在这里遇到的问题是,您期望函数参数的默认值在函数的 调用 时计算,而不是在其 定义时计算 时间:

def when_is_this_called(): print("NOW!")

def some_func(an_arg=when_is_this_called()):
    pass

print("Functions defined")
some_func()

将打印:

NOW!
Functions defined

而不是:

Functions defined
NOW!

如您最初所料。

而不是:

def switch(text = request.values.get("text", None)):

获取方法体中的值:

def switch():
    text = request.values.get("text", None)
    # ... snip ...

我认为您的问题在于您实际上是在 switcher 字典中调用函数并保留调用结果,而不是将它们作为对函数的引用。如果您从 switcher 字典中的函数中删除括号,您就可以获得对函数本身的引用,然后像您一样使用 func() 调用它们。

switcher = {
    "1": func1,  # No ()
    ...
}

此操作的简单示例:

>>> def x(): print("test")
...
>>> def y(): print("another test")
...
>>> z = {1:x, 2:y}
>>> z[1]()
test
>>> z[2]()
another test