我正在使用mongo 2.2.3和java驱动程序.我的困境,我必须把一个字段和值推入一个数组,但我似乎无法弄清楚如何做到这一点.我的数据样本:
"_id" : 1,
"scores" : [
{
"type" : "homework",
"score" : 78.97979
},
{
"type" : "homework",
"score" : 6.99
},
{
"type" : "quiz",
"score" : 99
}
]
Run Code Online (Sandbox Code Playgroud)
我可以$推入shell:
db.collection.update({_id:1},{$push:{scores:{type:"quiz", score:99}}})
Run Code Online (Sandbox Code Playgroud)
但是当我把它翻译成java时,我迷惑自己并将键盘放在墙上.
到目前为止我的java代码(不完整和错误):
DBObject find = new BasicDBObject("_id", 1);
DBObject push = new BasicDBObject("$push", new BasicDBObject(
"scores", new BasicDBObject()));
Run Code Online (Sandbox Code Playgroud) 我试图只忽略C901 too complex一个函数的警告。我已经尝试了几乎所有# noqa: C901我能看到的排列,但仍然出现错误。我会认为# noqa函数(方法?)上面的注释就足够了。我什至尝试将评论与def声明放在同一行,如下所示:
class Klass():
def my_complex_method(self): # noqa: C901
"""
lots of if's and return's
"""
Run Code Online (Sandbox Code Playgroud)
这是我收到的消息的示例flake8:
src/test/_resource.py:147:5: C901 'Resource.render' is too complex (22)
def render(self, request): # noqa: C901
^
Run Code Online (Sandbox Code Playgroud)
快速搜索只产生如何忽略全局或整个文件。这不是我想要的,因为文件中的其他函数如果太复杂,我确实想捕获。有谁知道我该如何解决我的问题?
我需要做一些非常快速的脏输入消毒,我想基本上将所有转换<, >为<, >.
我希望得到相同的结果,'<script></script>'.replace('<', '<').replace('>', '>')而不必多次迭代字符串.我知道maketrans与str.translate(即http://www.tutorialspoint.com/python/string_translate.htm)相关但这仅从1个char转换为另一个char.换句话说,人们做不到这样的事情:
inList = '<>'
outList = ['<', '>']
transform = maketrans(inList, outList)
Run Code Online (Sandbox Code Playgroud)
是否有builtin可以在单次迭代中执行此转换的函数?
我想使用builtin功能而不是外部模块.我已经知道了Bleach.
我有一个很大的相对 url 文件,我想用 Scrapy 抓取它,我已经编写了一些代码来逐行读取这个文件,并为我的蜘蛛构建请求进行解析。下面是一些示例代码。
蜘蛛:
def start_requests(self):
with open(self._file) as infile:
for line in infile:
inlist = line.replace("\n","").split(",")
item = MyItem(data = inlist[0])
request = scrapy.Request(
url = "http://foo.org/{0}".format(item["data"]),
callback = self.parse_some_page
)
request.meta["item"]
yield request
def parse_some_page(self,response):
...
request = scrapy.Request(
url = "http://foo.org/bar",
callback = self.parse_some_page2
)
yield request
Run Code Online (Sandbox Code Playgroud)
这工作正常,但对于一个大的输入文件,我看到parse_some_page2在start_requests完成所有初始请求之前不会调用它。有什么方法可以让 Scrapy 更早地开始调用回调吗?最终,我不想在开始看到项目流过管道之前等待一百万个请求。
我正在测试我的消费者,它使用scope['url_route']但使用HttpCommunicatoror ApplicationCommunicator,该参数未设置。我该如何设置这个参数?测试文档非常有限,并且没有关于此https://channels.readthedocs.io/en/latest/topics/testing.html的任何文档。
测试.py
from channels.testing import HttpCommunicator
import pytest
from my_app import consumers
@pytest.mark.asyncio
async def test_consumers():
communicator = HttpCommunicator(consumers.MyConsumer, 'GET', '/project/1')
response = await communicator.get_response()
assert response['status'] == 400
Run Code Online (Sandbox Code Playgroud)
my_app.py
import json
from channels.db import database_sync_to_async
from channels.generic.http import AsyncHttpConsumer
from projects import model
class MyConsumer(AsyncHttpConsumer):
async def handle(self, body):
_id = int(self.scope['url_route']['kwargs'].get('project_id', 1))
record = await database_sync_to_async(models.Project.objects.get)(id=_id)
data = json.dumps({"id": _id})
await self.send_response(200, data.encode('utf8'))
Run Code Online (Sandbox Code Playgroud) python ×2
flake8 ×1
forms ×1
input ×1
java ×1
jongo ×1
mongo-java ×1
mongodb ×1
sanitization ×1
scrapy ×1
twisted ×1
validation ×1