我第一次使用express-validator,如果两个字段相等,我找不到断言的方法(如果可以完成的话).
示例:提交包含2次电子邮件地址(一个作为标准确认)的表单.我想检查字段是否匹配.
我发现自己的解决方法有效,但我想知道我是不是只做了一些不必要的事情.这是代码(数据来自ajax调用):
//routes.js
function validator(req, res, next) {
req.checkBody('name', 'cannot be empty').notEmpty();
req.checkBody('email', 'not valid email').isEmail();
var errors = req.validationErrors(); // up to here standard express-validator
// Custom check to see if confirmation email matches.
if (!errors) errors = [];
if (email !== email_confirm){
errors.push({param: 'email_confirm', msg: 'mail does not match!', value: email_confirm})
}
if (errors.length > 0) {
res.json({msg: 'validation', errors:errors}); // send back the errors
}
else {
// I don't want to insert the email …Run Code Online (Sandbox Code Playgroud) 我是在Python中使用队列的新手,我最近开始使用PriorityQueue。
我期望根据优先级数字将元素插入队列。也就是说,如果我做了类似的事情:
from Queue import PriorityQueue
q = PriorityQueue()
q.put((1, '1'))
q.put((4, 'last'))
q.put((2, '2'))
q.put((3, '3'))
print q.queue
Run Code Online (Sandbox Code Playgroud)
我期待着这个输出:
[(1, '1'), (2, '2'), (3, '3'), (4, 'last')].
Run Code Online (Sandbox Code Playgroud)
相反,我得到:
[(1, '1'), (3, '3'), (2, '2'), (4, 'last')]
Run Code Online (Sandbox Code Playgroud)
但是,如果我通过以下方式使元素脱离队列:
while not q.empty():
item = q.get()
print item
Run Code Online (Sandbox Code Playgroud)
我确实得到了我期望的输出:
(1, '1')
(2, '2')
(3, '3')
(4, 'last')
Run Code Online (Sandbox Code Playgroud)
我试图通过在某些时候打印队列来调试某些东西,并发现元素与我期望的顺序不符。除非我没有错过,否则Queue的文档不会提及任何有关排序的内容。我期望它被排序只是错了吗?出于效率的原因,是否可以简单地以这种方式实施?
我是 Bokeh 和 Flask 的新手。我浏览了相关的问题、教程,并查看了 Bokeh 文档,但无法弄清楚我做错了什么。
话虽如此,我想创建一个简单的网络应用程序,在其中我将各种数据报告和图表“组合在一起”。
根据我阅读的内容,我想出了以下内容:
应用程序.py:
... # imports
app = Flask(__name__, static_url_path='/static')
@app.route("/")
def index():
return render_template("index.html")
@app.route("/bokeh_test")
def bokeh_test():
script, div = components(sample_plot())
return render_template("bokeh_test.html", script=script, div=div)
def sample_plot():
"""
A random plot just for testing purposes.
:return: plot
"""
PLOT_OPTIONS = dict(plot_width=600, plot_height=400)
SCATTER_OPTIONS = dict(size=12, alpha=0.5)
data = lambda: [random.choice([i for i in range(100)]) for r in range(10)]
plot = figure(sizing_mode='scale_both', tools='pan', **PLOT_OPTIONS)
plot.scatter(data(), data(), color="red", **SCATTER_OPTIONS)
# show(plot)
return plot
Run Code Online (Sandbox Code Playgroud)
散景测试.html: …