小编JCh*_*hao的帖子

我可以只获取Cursor对象中的第一项(pymongo)吗?

所以我创建了一个Cursor对象

cdb=self.mongo['bleh_bleh_bleh_setup_here']
data=cdb[collection].find(query_commands_here)
Run Code Online (Sandbox Code Playgroud)

不要担心上面的语法.假设我可以成功创建这样的游标对象

我知道我可以for循环遍历对象,但我想要的只是这个对象的第一项.有没有比循环更有效的方法?

编辑:

为了使事情更清楚,'bleh_bleh_bleh_setup_here'只是连接到所需mongoDB的路径,'query_commands_here'就是这样的查询{field1:{'$gt':num1}, field2:{'$ne':num2}}.这条线

data=cdb[collection].find(query_commands_here)
Run Code Online (Sandbox Code Playgroud)

将给我一个Cursor对象,我可以循环迭代for.所以像

for item in data:
    print item
Run Code Online (Sandbox Code Playgroud)

将打印出对象中的每个条目.它工作得很好.但是,根据文档,此游标对象应该有调用的方法.hasNext(),如果有下一个条目,则应该返回True.到目前为止,我还没有找到一种方法让它因某些奇怪的原因而起作用.data.next()确实给了我一个入口.我想确保我可以有这个条件,以确保我不要求.next()一个不包含任何内容的游标对象,虽然我不预见会发生这种情况,但我会认为它会在某个时刻发生.

python mongodb pymongo

15
推荐指数
1
解决办法
1万
查看次数

有没有一种方法可以通过Airflow API创建/修改连接

通过Admin -> Connections,我们可以创建/修改连接的参数,但是我想知道是否可以通过API进行相同的操作,以便可以以编程方式设置连接

airflow.models.Connection似乎它只处理实际连接到实例,而不是将其保存到列表中。似乎应该已经实现了一个功能,但是我不确定在哪里可以找到该特定功能的文档。

python airflow

12
推荐指数
4
解决办法
3874
查看次数

通过Chalice API调用将文件上载到AWS S3

我正试图通过Chalice将文件上传到我的S3存储桶(我现在正在玩它,对此仍然是新手).但是,我似乎无法做到正确.

我正确地设置了AWS,成功完成了本教程后回复了一些消息.然后我尝试做一些上传/下载,问题出现了.

s3 = boto3.resource('s3', region_name=<some region name, in this case oregon>)
BUCKET= 'mybucket'
UPLOAD_FOLDER = os.path.abspath('')  # the file I wanna upload is in the same folder as my app.py, so I simply get the current folder name

@app.route('/upload/{file_name}', methods=['PUT'])
def upload_to_s3(file_name):
    s3.meta.client.upload_file(UPLOAD_FOLDER+file_name, BUCKET, file_name)
    return Response(message='upload successful',
                    status_code=200,
                    headers={'Content-Type': 'text/plain'}
    )
Run Code Online (Sandbox Code Playgroud)

请不要担心我如何设置文件路径,当然,除非这是问题.

我收到了错误日志:

没有相应的文件和目录: ''

在这种情况下file_name只是mypic.jpg.

我想知道为什么这UPLOAD_FOLDER部分没被拿起来.另外,作为参考,似乎使用绝对路径对于Chalice来说会很麻烦(在测试时,我看到代码被移动到/var/task/)

有谁知道如何正确设置它?

编辑:

完整的脚本

from chalice import Chalice, Response

import boto3

app = Chalice(app_name='helloworld') …
Run Code Online (Sandbox Code Playgroud)

python python-3.x boto3 chalice

9
推荐指数
1
解决办法
2379
查看次数

React Flash Message:如何在不刷新页面但刷新200的情况下显示消息

我想FlashMessage在所需时间重新加载页面时显示错误(或成功)消息

我正在使用FlashMessage,我的代码看起来像

render() {
  return (
    {this.state.error ? <FlashMessage duration={5000}><strong>{this.state.error.message}</strong></FlashMessage> : ''}
    //Some table here presenting data
    <Button variant="outline-info" type="submit" size="lg" block className="button-custom" onClick={this.handleSubmit.bind(this)}>
          Submit
        </Button>
  )}
Run Code Online (Sandbox Code Playgroud)

对于我的有状态组件,error

handleSubmit(event) {
let data = {
  name: this.state.name,
  unit_price: this.state.unit_price,
  length: this.state.length,
  time: this.state.selectedDate,
  instructor: this.state.instructor,
}
ApiService.postLesson(data)
.then(res => {
  this.setState({
    message: 'Success!'
  });
})
.catch(error => {
  this.setState({
    message: error,
    error: error,
  });
  console.log(error);
})
Run Code Online (Sandbox Code Playgroud)

};

而我的ApiService.postLesson

const instance = axios.create({
  headers: …
Run Code Online (Sandbox Code Playgroud)

javascript flash-message reactjs

7
推荐指数
1
解决办法
460
查看次数

在 lambda 函数中解析私有主机名

我希望我描述正确......

我遇到了 Lambda 无法解析像http://example.com:1234这样的 url 的问题

我必须改用ip。我想知道如何确保可以解析 url,尤其是当我使用的 url 是私有的时。谷歌的所有研究都将我指向 Route 53,但没有解释到底应该如何做到这一点。

有人遇到过类似的问题吗?

编辑:

为了更清楚:

  1. 我所做的就是使用 pyhtonrequests并调用我的 elasticsearch来插入一些数据,所以

    response = requests.post(es_url, data=some_data, timeout=some_timeout)

这里es_url<ip>:9200/some_index/some_type/

我想更改ip为人类可读的域,例如my_es.example.com在我的 EC2 实例中工作的域,但我无法在 lambda 函数中解析此名称

  1. 我相信我的 lambda 函数已经连接到 VPC。我不在乎访问公共 IP。我需要访问驻留在同一个 VPC 中的 ES。除非我的设置不正确......

aws-lambda

6
推荐指数
2
解决办法
1万
查看次数

AWS boto3——“batch_writer”和“batch_write_item”之间的区别

我目前正在将 boto3 与 dynamodb 一起应用,我注意到有两种类型的批量写入

batch_writer在教程中使用,似乎你可以迭代不同的 JSON 对象来执行插入(当然,这只是一个示例)

batch_write_items在我看来是发电机特定的功能。然而,我对此并不是 100% 确定,而且我不确定这两个函数之间有什么区别(性能、方法等等)

他们做同样的事情吗?如果是的话,为什么有两个不同的功能?如果不是,有什么区别?性能对比如何?

amazon-web-services amazon-dynamodb boto3

5
推荐指数
1
解决办法
4890
查看次数

当 managed=False 时如何运行 django 测试

我有一些具有managed=False. 因此,由于无法找到表,我的测试失败

我已经按照这个链接https://dev.to/patrnk/testing-against-unmanaged-models-in-django然后https://github.com/henriquebastos/django-test-without-migrations来设置一个自定义测试运行器。

跑步者确实跑了,但我仍然遇到同样的问题,我不知道为什么。

我的 Django 版本是 2.1

我如何测试什么时候managed=False

python django

5
推荐指数
1
解决办法
933
查看次数

检查IP是否在Python的CIDR范围内

我知道这里有一些类似的问题,但是他们大多想要找到范围本身(它使用一些库,比如stackoverflow说的是我的问题的一个例子),并且是另一种语言.

我有办法将子网转换为子网中ip的范围的开头和结尾(好吧,措辞不好,就像1.1.1.1/16 -> (1.1.0.0 , 1.1.255.255))

我现在想检查是否1.1.2.2在此子网内.我可以简单地做一个><比较?

ip_range = ('1.1.0.0', '1.1.255.255')
if '1.1.2.2' >= ip_range[0] and '1.1.2.2' <= ip_range[1]:
     return True
Run Code Online (Sandbox Code Playgroud)

当我测试它时,它可以工作,但我不知道它是否总能用于任何ipv4 ip.我假设我只是比较ASCII顺序,所以这应该总是有效,但有没有例外?

python ip

2
推荐指数
3
解决办法
6936
查看次数

Hadoop MapReduce 作业卡住,因为 auxService:mapreduce_shuffle 不存在

我已经用相同的问题检查了多个帖子,解决方案总是将以下内容添加到 yarn-site.xml

<?xml version="1.0"?>
<!--
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations …
Run Code Online (Sandbox Code Playgroud)

hadoop mapreduce

2
推荐指数
1
解决办法
1519
查看次数

Python elasticsearch-dsl 多字段排序

我正在尝试使用 elasticsearch-dsl 形成用于排序的命令。但是我无法以正确的格式传递变量。

格式应该是

s=Search()
s = s.sort({"time":{"order":"asc"}}, {"anoter_field":{"order":"desc"}})
s.execute()
Run Code Online (Sandbox Code Playgroud)

问题是我试图把它{"time":{"order":"asc"}}, {"anoter_field":{"order":"desc"}}作为一个变量,但我似乎无法以正确的语法得到它。我尝试使用 dict、list 和 string,但似乎都不起作用。

我的输入将是一个看起来像的字典

input = {"time":"asc", "another_field":"desc"}
Run Code Online (Sandbox Code Playgroud)

python elasticsearch-dsl

2
推荐指数
1
解决办法
1312
查看次数