我看过 Tornado 文档和示例,其中 self.write 方法广泛用于在 HTML 上呈现某些值,其中 POST 请求在处理程序中运行。但我无法清楚地了解如何将响应返回给客户端。
例如,我从客户端调用 Tornado 服务器上的 POST 请求。接受post请求的代码是:
class strest(tornado.web.RequestHandler):
def post(self):
value = self.get_argument('key')
cbtp = cbt.main(value)
Run Code Online (Sandbox Code Playgroud)
通过这个,我可以找到 cbtp 的值,并且通过self.write(cbtp),我可以将其以 HTML 格式打印。但相反,我想以 JSON 格式将此值返回给客户端,就像{'cbtp':cbtp}
我想知道如何修改我的代码以便将此响应发送到客户端,或者给我一些文档,其中对此进行了流畅的解释。
做类似的事情
res = {cbtp: cbtp}
return cbtp
Run Code Online (Sandbox Code Playgroud)
抛出一个BadYieldError: yielded unknown object
我正在编写一个具有集合属性的接口,我想要只读它.我不希望界面的用户能够修改集合.我发现创建只读集合属性的典型建议是将属性的类型设置为IEnumerable,如下所示:
private List<string> _mylist;
public IEnumerable<string> MyList
{
get
{
return this._mylist;
}
}
Run Code Online (Sandbox Code Playgroud)
然而,这并不妨碍用户将IEnumerable转换回List并对其进行修改.
如果我使用Yield关键字而不是_mylist直接返回,则会阻止我的界面用户修改集合.我是这么认为的,因为那时我只是逐个返回对象,而不是实际的集合.
private List<string> _mylist;
public IEnumerable<string> MyList
{
get
{
foreach(string str in this._mylist)
{
yield return str;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我希望能够通过A遍历的所有B.我尝试了以下代码:
object Test {
class A
class B extends A
class C extends A
var someAs: Traversable[A] = ...
def theBofSomeAs: Traversable[B] = for(a <- someAs) {
a match {
case b:B => yield b
case _ =>
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是这不会编译,因为它表示表达式具有类型Unit.怎么实现呢?
所以这是我正在尝试使用的代码片段.
function* genBubble(arr) {
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
yield arr; // returning arr after every iteration
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1); // removed swap for brevity
}
}
}
}
const tempArray = [3, 5, 8, 4, 1, 9, -2];
const genForLoop = genBubble(tempArray);
do {
console.log(genForLoop.next());
} while (!genForLoop.next().done);
Run Code Online (Sandbox Code Playgroud)
这是一个简单的冒泡排序实现.如你所知,冒泡排序有 …
我有一个列表,我正在尝试编写一个函数returnMatchedElement(x:Int,y:Int,f:(Int,Int)=>Boolean),如果某个条件匹配列表的元素,它将返回该元素.这是我到目前为止所得到的:
def returnMatchedElement(x:Int,l:List[Int],f:(Int,Int)=>Boolean):Int={
for (y<-l if f(x,y)) yield y
0}
def matchElements(a:Int,b:Int):Boolean= {if a==b true else false}
val l1=List(1,2,3,4,5)
returnMatchedElement(3,l1,matchElements)
res13: Int = 0
Run Code Online (Sandbox Code Playgroud)
我猜我在理解yield关键字方面遇到了问题.我在这里弄错了什么?
下面的答案有效(感谢),但前提是f返回布尔值.我尝试了另一个这样的例子
def matchElements(a:Int,b:Int):Int= {if (a==b) 1 else 0}
def returnMatchedElement(x:Int,l:List[Int],f:(Int,Int)=>Int):Option[Int]={l.find(y => f(x, y))}
Run Code Online (Sandbox Code Playgroud)
现在编译器说
<console>:8: error: type mismatch;
found : Int
required: Boolean
def returnMatchedElement(x:Int,l:List[Int],f:(Int,Int)=>Int):Option[Int]={l.find(y => f(x, y))}
Run Code Online (Sandbox Code Playgroud) 为了说明这个问题,假设我们有这个简单的生成器:
def firstn(n):
num = 0
while num < n:
yield num
num += 1
for i in firstn(10):
print i
Run Code Online (Sandbox Code Playgroud)
这将打印数字 0 到 9。但是如果我们有:
def firstn(n):
num = 0
while num < 5 < n:
yield num
num += 1
for i in firstn(10):
print i
Run Code Online (Sandbox Code Playgroud)
(更改在while语句中。)然后它仅打印数字 0 到 4。一旦num >= 5,生成器将不再生成值。
我很好奇的是幕后发生的事情:我使用PythonTutor来逐步执行代码,我的印象是,一旦while语句不再True,函数就会隐式返回None,循环for以某种方式检测到,然后也断了。我使用next内置函数来更仔细地检查这一点:
>>> def firstn(n):
... num = 0
... while num …Run Code Online (Sandbox Code Playgroud) generator ×2
python ×2
scala ×2
.net ×1
bubble-sort ×1
c# ×1
iteration ×1
javascript ×1
json ×1
list ×1
post ×1
properties ×1
tornado ×1