我正在做一些套接字IO,并使用bytearray对象作为缓冲区.我想使用csock.recv_into接收带有此偏移量的数据,如下所示,以避免创建中间字符串对象.不幸的是,似乎字节数不能以这种方式使用,并且下面的代码不起作用.
buf = bytearray(b" " * toread)
read = 0
while(toread):
nbytes = csock.recv_into(buf[read:],toread)
toread -= nbytes
read += nbytes
Run Code Online (Sandbox Code Playgroud)
所以不是我使用下面的代码,它不会使用一个临时的字符串(和作品)...
buf = bytearray(b" " * toread)
read = 0
while(toread):
tmp = csock.recv(toread)
nbytes = len(tmp)
buf[read:] = tmp
toread -= nbytes
read += nbytes
Run Code Online (Sandbox Code Playgroud)
是否有更优雅的方法来执行此操作,不需要复制中间字符串?
我正在开发一个运行良好的 Flask 应用程序,直到我尝试将以下行添加到要渲染的模板中:
{% do totals.update({tier: 0}) %}
Run Code Online (Sandbox Code Playgroud)
当前渲染模板的代码使用 Flask 的render_template():
from flask import Flask, Response, request, session
from flask import render_template
app = Flask(__name__)
..
return render_template(<template.htlm>,...)
Run Code Online (Sandbox Code Playgroud)
此操作失败并出现以下错误:
TemplateSyntaxError: Encountered unknown tag 'do'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.
Run Code Online (Sandbox Code Playgroud)
明显的解决方法是将jinja2.ext.do扩展添加到 jinja。我已经能够直接使用 Jinja2 成功做到这一点,如下所示:
from jinja2 import Environment, PackageLoader
ENV = Environment(loader=PackageLoader('ticket_app', 'templates'), extensions=['jinja2.ext.do'])
...
TEMP = ENV.get_template('div_' + div_id + '.html') …Run Code Online (Sandbox Code Playgroud)