簡體   English   中英

如何使用 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()

這是我得到的 output

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 “線程”-local,從而導致此錯誤。 您不想使用線程局部值來定義 function 參數輸入。

您在這里遇到的問題是您希望在函數調用時而不是在其定義時評估 function arguments 的默認值:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM