小编dcr*_*sta的帖子

在Python中将列表迭代为对(当前,下一个)

我有时需要在Python中迭代一个列表,查看"current"元素和"next"元素.到目前为止,我已经完成了以下代码:

for current, next in zip(the_list, the_list[1:]):
    # Do something
Run Code Online (Sandbox Code Playgroud)

这有效并且符合我的预期,但是有更多惯用或有效的方法来做同样的事情吗?

python

121
推荐指数
7
解决办法
4万
查看次数

是否有Amazon.com API来检索产品评论?

是否有任何AWS API /服务可以访问亚马逊销售的商品的产品评论?我有兴趣通过(ASIN,user_id)元组查找评论.我可以看到Product Advertising API返回包含URL的页面的URL(用于嵌入IFRAME),但如果可能的话,我对审阅数据的机器可读格式感兴趣.

amazon-web-services

73
推荐指数
3
解决办法
9万
查看次数

如何使用Jenkinsfile设置github提交状态不使用拉取请求构建器

我们有Jenkins 2设置构建每次推送到github,我们不使用Pull Request构建器(虽然提交是拉请求的一部分,显然也会构建).在GitHub的集成插件说,它只有与拉请求生成器的工作原理,所以这不会为我们工作.

我也尝试了github-notify插件,但它似乎不适用于我们的情况(可能是因为repo是私有的和/或作为Organizaiton的一部分而不是个人用户拥有).我试图让它推断设置以及手动指定credentialsId,account,repo,当然status参数,都没有运气.

这是我Jenkinsfile的缩写版本:

pipeline {
    agent { label "centos7" }

    stages {
        stage("github => pending") {
            steps {
                githubNotify status: "PENDING", credentialsId: "my-credentials-id", account: "my-account", repo: "my-repo"
            }
        }
        stage("build") {
            ...
        }
    }

    post {
        success {
            githubNotify status: "SUCCESS", credentialsId: "my-credentials-id", account: "my-account", repo: "my-repo"
        }
        failure {
            githubNotify status: "FAILURE", credentialsId: "my-credentials-id", account: "my-account", repo: "my-repo"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行构建时,我得到以下内容:

java.lang.IllegalArgumentException: The suplied …
Run Code Online (Sandbox Code Playgroud)

github jenkins jenkins-pipeline jenkins-2

19
推荐指数
2
解决办法
7986
查看次数

使用MongoDB 10gen驱动程序进行查询投影

最近我和mongodb官方司机一起玩.我遇到的问题是如何进行查询投影.示例,如果我有一个持久化对象

class A{
  id
  PropA
  PropB
  List<LargeObjects>
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能检索id,PropA和PropB而不是检索整个对象?如何使用mongodb官方c#驱动程序?

c# mongodb

12
推荐指数
3
解决办法
5884
查看次数

帮助定制Jinja2扩展

我一直在努力让这个Jinja2定制扩展工作 - 当他们说写一个不是为了"平民"时,文档并不是在开玩笑 - 最后设法得到了这个工作代码:

class WrapperExtension(Extension):

    tags = set(['wrap'])

    def parse(self, parser):
        lineno = parser.stream.next().lineno
        args = [parser.parse_expression()]
        args.append(nodes.Const(args[0].name))
        return nodes.CallBlock(
            self.call_method('_render', args),
            [], [], []).set_lineno(lineno)

    def _render(self, value, name, *args, **kwargs):
        if some_condition():
            return '<wrapper id="%s">%s</wrapper>' % (name, value)
        return value
Run Code Online (Sandbox Code Playgroud)

正如我所说,这现在正在发挥作用.什么我不确定就是为什么我需要返回nodes.CallBlockparse(),而不是self.call_method()(它返回一个nodes.Call对象).如果有人有任何见解 - 或者可以指向我编写扩展的教程 - 请告诉我.

python jinja2

10
推荐指数
1
解决办法
1796
查看次数

更新MongoDB中数组内部的嵌入对象

我有文件喜欢

{
    id : 100,
    heros:[
        {
           nickname : "test",
           spells : [
             {spell_id : 61, level : 1},
             {spell_id : 1, level : 2}
           ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我不能$set拼的level : 3spell_id : 1spells那里面heros有绰号"测试我尝试此查询:

db.test.update({"heros.nickname":"test", "heros.spells.spell_id":1}, 
{$set:{"heros.spells.$.level":3}});
Run Code Online (Sandbox Code Playgroud)

我看到的错误是

无法使用字符串字段名称附加到数组[spells]感谢您的帮助.

mongodb mongodb-query

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

自定义Django可以过滤访问request.user吗?

是否可以从自定义模板过滤器访问当前User(即user在模板上下文中)?

显然我可以将用户作为参数传递,但如果可以只抓住当前用户,那将更方便.

django django-templates django-template-filters

8
推荐指数
1
解决办法
6869
查看次数

如何使用JIRA SOAP API在问题上设置标签

有没有办法在使用SOAP API创建或更新JIRA票证时为票证设置"标签"字段?在WSDL中搜索"label"没有显示任何内容,并且当使用我知道已设置标签的API获取票证时,结果中没有迹象表明存在标签.

soap jira

7
推荐指数
2
解决办法
3826
查看次数

生成序列的"有序子集"的最有效方法

我需要生成Python中序列的所有"有序子集"(如果我没有使用正确的数学术语,请道歉),省略的元素替换为None.Given [1, 2],我想要[(1, 2), (1, None), (None, 2), (None, None)].每个"有序子集"应该具有这样的属性:在每个位置,它与种子序列中的元素完全相同,或者它是None.

我可以很容易地生成缺少以下元素的子集:

from itertools import combinations
for length in xrange(len(items), 0, -1):
    for combination in combinations(items, length):
        yield combination
Run Code Online (Sandbox Code Playgroud)

但我无法弄清楚重建缺失元素的最有效方法是什么.我的第一个想法是做这样的事情:

from itertools import combinations
indexes = range(len(items))
for length in xrange(len(items), 0, -1):
    for combination in combinations(indexes, length):
        yield tuple(items[i] if i in combination else None for i in indexes)
Run Code Online (Sandbox Code Playgroud)

只是想知道是否有人能发现任何明显的缺陷,或者是否有一个我错过的更有效的解决方案.(请注意,这items将是一个相当短的列表,通常在10个元素之下,所以我不关心内部循环中"组合"的O(N)搜索.

python

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

如何构建一个小型程序的迷你网络互相馈送数据?

我正在尝试模拟一个实时网络,其中节点是消费者和不同费率的生产者.我如何使用Python快速实现此示例?我想象我会为每个节点编写一个简单的程序,但我不确定如何将它们相互连接.

python simulation

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