我想以下列方式创建支持GET请求的资源:
/bar?key1=val1&key2=val2
Run Code Online (Sandbox Code Playgroud)
我尝试了这段代码,但它没有用
app = Flask(__name__)
api = Api(app)
class BarAPI(Resource):
def get(key1, key2):
return jsonify(dict(data=[key1, key2]))
api.add_resource(BarAPI, '/bar', endpoint='bar')
Run Code Online (Sandbox Code Playgroud)
谢谢!
python3中引入了一个新功能 - 异常链接.出于某些原因,我需要在代码中针对某些异常禁用它.
这是示例代码:
try:
print(10/0)
except ZeroDivisionError as e:
sys.exc_info()
raise AssertionError(str(e))
Run Code Online (Sandbox Code Playgroud)
我所看到的:
Traceback (most recent call last):
File "draft.py", line 19, in main
print(10/0)
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero
Run Code Online (Sandbox Code Playgroud)
我想看到的:
Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试修补__new__
一个类的方法,但它没有按我的预期工作。
from contextlib import contextmanager
class A:
def __init__(self, arg):
print('A init', arg)
@contextmanager
def patch_a():
new = A.__new__
def fake_new(cls, *args, **kwargs):
print('call fake_new')
return new(cls, *args, **kwargs)
# here I get error: TypeError: object.__new__() takes exactly one argument (the type to instantiate)
A.__new__ = fake_new
try:
yield
finally:
A.__new__ = new
if __name__ == '__main__':
A('foo')
with patch_a():
A('bar')
A('baz')
Run Code Online (Sandbox Code Playgroud)
我期望以下输出:
A init foo
call fake_new
A init bar
A init baz
Run Code Online (Sandbox Code Playgroud)
但是在call fake_new
我收到错误之后(请参阅代码中的注释)。对我来说,我似乎只是装饰一个 …
flask_cache.Cache.memoize
不与flask_restful.Resource
这是示例代码:
from flask import Flask, request, jsonify
from flask_restful import Resource, Api
from flask_cache import Cache
app = Flask(__name__)
api = Api(app)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})
class MyResource(Resource):
JSONIFY = True
PATH = None
ENDPOINT = None
def dispatch_request(self, *args, **kwargs):
kw = dict(**kwargs)
kw.update(request.args.items())
r = super().dispatch_request(*args, **kw)
if self.JSONIFY:
return jsonify(r)
else:
return r
class DebugResource(MyResource):
PATH = '/debug'
ENDPOINT = 'debug'
@cache.memoize(timeout=30)
def get(self, **kwargs):
print('cache is not used!')
return kwargs
for r …
Run Code Online (Sandbox Code Playgroud) 我想启用 CORS;为此,我可以在其中添加两行elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
Run Code Online (Sandbox Code Playgroud)
它有效。有没有办法做同样的事情但不改变elasticsearch.yml
并且只使用环境变量?我试图以类似的方式设置它们。这是它在我的docker-compose.yml
:
environment:
- http.cors.enabled=true
- http.cors.allow-origin="*"
Run Code Online (Sandbox Code Playgroud)
它不能正常工作。虽然它以某种方式工作,因为即使我将这些参数添加到elasticsearch.yml
环境中,环境设置 CORS 也会停止工作。
我想将 jupyter Notebook 安装到我的 docker 映像中。在容器中安装并运行它后,我无法从浏览器访问它。
\n这是我的最小设置:
\nDockerfile:
\nFROM python:3.7.5\nRUN pip install jupyterlab\nEXPOSE 8888\nCMD jupyter-lab --allow-root\n
Run Code Online (Sandbox Code Playgroud)\n命令
\nFROM python:3.7.5\nRUN pip install jupyterlab\nEXPOSE 8888\nCMD jupyter-lab --allow-root\n
Run Code Online (Sandbox Code Playgroud)\n容器输出:
\n To access the server, open this file in a browser:\n file:///root/.local/share/jupyter/runtime/jpserver-7-open.html\n Or copy and paste one of these URLs:\n http://localhost:8888/lab?token=e6b9a5dd573aabc52e6753e7b21f0daf6270e685055afa50\n or http://127.0.0.1:8888/lab?token=e6b9a5dd573aabc52e6753e7b21f0daf6270e685055afa50\n
Run Code Online (Sandbox Code Playgroud)\n我尝试打开这些链接中的任何一个,并在浏览器中收到此消息:
\nThis page isn\xe2\x80\x99t working\nlocalhost didn\xe2\x80\x99t send any data.\nERR_EMPTY_RESPONSE\n
Run Code Online (Sandbox Code Playgroud)\n我究竟做错了什么?
\n在表上创建唯一索引时遇到问题。语句失败并显示以下消息
>CREATE UNIQUE INDEX CONCURRENTLY my_table_pkey_new ON my_table (new_id);
ERROR: canceling statement due to lock timeout
Run Code Online (Sandbox Code Playgroud)
目前尚不清楚为什么我会遇到锁定超时。并发索引创建不应锁定表。
我也尝试增加锁定超时但没有成功
test=> show lock_timeout;
lock_timeout
--------------
5min
(1 row)
test=> set lock_timeout to 99999999;
SET
test=> show lock_timeout;
lock_timeout
--------------
5min
(1 row)
Run Code Online (Sandbox Code Playgroud) python ×4
flask ×2
python-3.x ×2
caching ×1
class ×1
docker ×1
flask-cache ×1
postgresql ×1
rest ×1