小编Bon*_*io2的帖子

System.out.println()的JUnit测试

我需要为一个设计很差的旧应用程序编写JUnit测试,并将大量错误消息写入标准输出.当getResponse(String request)方法行为正确时,它返回XML响应:

@BeforeClass
public static void setUpClass() throws Exception {
    Properties queries = loadPropertiesFile("requests.properties");
    Properties responses = loadPropertiesFile("responses.properties");
    instance = new ResponseGenerator(queries, responses);
}

@Test
public void testGetResponse() {
    String request = "<some>request</some>";
    String expResult = "<some>response</some>";
    String result = instance.getResponse(request);
    assertEquals(expResult, result);
}
Run Code Online (Sandbox Code Playgroud)

但是当它得到格式错误的XML或者不理解它返回的请求并将null一些东西写入标准输出时.

有没有办法在JUnit中断言控制台输出?要抓住像这样的案例:

System.out.println("match found: " + strExpr);
System.out.println("xml not well formed: " + e.getMessage());
Run Code Online (Sandbox Code Playgroud)

java console junit

346
推荐指数
8
解决办法
22万
查看次数

如何"完美"覆盖一个字典?

我怎样才能使"完美"成为dict的子类?最终目标是有一个简单的字典,其中键是小写的.

似乎应该有一些微小的原语我可以覆盖以使这项工作,但根据我的所有研究和尝试似乎情况并非如此:

这是我的第一次尝试,get()不起作用,毫无疑问还有许多其他小问题:

class arbitrary_dict(dict):
    """A dictionary that applies an arbitrary key-altering function
       before accessing the keys."""

    def __keytransform__(self, key):
        return key

    # Overridden methods. List from 
    # https://stackoverflow.com/questions/2390827/how-to-properly-subclass-dict

    def __init__(self, *args, **kwargs):
        self.update(*args, **kwargs)

    # Note: I'm using dict directly, since super(dict, self) doesn't work. …
Run Code Online (Sandbox Code Playgroud)

python inheritance dictionary get set

204
推荐指数
5
解决办法
10万
查看次数

docker错误:/var/run/docker.sock:没有这样的文件或目录

我是码头工人的新手.我有一个shell脚本将数据加载到impala中,我想要一个运行的docker文件构建一个映像并运行容器.我在mac上,安装了boot2docker并DOCKER_HOST设置了env.

bash-3.2$ docker info
Containers: 0
Images: 0
Storage Driver: aufs
Root Dir: /mnt/sda1/var/lib/docker/aufs
Dirs: 0
Execution Driver: native-0.2
Kernel Version: 3.15.3-tinycore64
Debug mode (server): true
Debug mode (client): false
Fds: 10
Goroutines: 10
EventsListeners: 0
Init Path: /usr/local/bin/docker
Sockets: [unix:///var/run/docker.sock tcp://0.0.0.0:2375]
Run Code Online (Sandbox Code Playgroud)

我试图使用以下方法安装预先构建的图像:

sudo docker pull busybox
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

sudo docker pull busybox 2014/08/18 17:56:19 Post http:///var/run/docker.sock/images/create?fromImage=busybox&tag=: dial unix /var/run/docker.sock: no such file or directory

我的码头设置有问题吗?

当我这样做时docker pull busybox,它会拉动图像并完成下载.

bash-3.2$ docker pull busybox
Pulling repository busybox
a9eb17255234: …
Run Code Online (Sandbox Code Playgroud)

docker boot2docker

85
推荐指数
8
解决办法
11万
查看次数

如何创建一个Django查询集过滤器,比较同一模型中的两个日期字段

试图在我的Solr索引中查询活动记录陈旧的查询.我想检查Activity.updated数据库中的日期是否大于Activity.added_toSolr_date同一记录的日期.

stale_activities_queryset = Activity.objects.filter(updated__gte = self.added_toSolr_date) 
Run Code Online (Sandbox Code Playgroud)

模型

class Activity(models.Model):
    # Last time entry / metric was updated in the Activity model database
    updated =  models.DateTimeField( verbose_name="CRUD date")
    # When it was added to Solr Index Date
    added_toSolr_date = models.DateTimeField(blank=True, null=True, verbose_name="Added to Solr Index Date")
Run Code Online (Sandbox Code Playgroud)

我引用了Django Query文档:https: //docs.djangoproject.com/en/1.4/ref/models/querysets/ 以及样本的单元测试:https: //github.com/django/django/blob/master/tests/ modeltests/or_lookups/tests.py

还在Stackoverflow上搜索了这里.所有示例都使用输入日期,而不是比较同一模型中的两个日期字段.

django django-queryset

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

如何在node.js中存根process.env?

我想存根process.env.FOO使用bar.

var sinon = require('sinon');
var stub = sinon.stub(process.env, 'FOO', 'bar');
Run Code Online (Sandbox Code Playgroud)

我糊涂了.我看过文件,但我还是不明白.sinonjs docs

sinonjs是一个例子,不是sinonjs没关系.

stub node.js sinon

68
推荐指数
5
解决办法
4万
查看次数

找不到生成器rspec:install.

我想在这里学习这个教程:http://railstutorial.org/chapters/static-pages#top

当我跑:

$ rails生成rspec:install

我明白了:

找不到生成器rspec:install.

可能是什么问题,只要我使用Rails 3.0.0.rc

谢谢.

rspec

55
推荐指数
4
解决办法
3万
查看次数

从一行获取jq的输出

我使用以下输出:https://stackoverflow.com/a/40330344

 (.issues[] | {key, status: .fields.status.name, assignee: .fields.assignee.emailAddress})
Run Code Online (Sandbox Code Playgroud)

输出:

 {  
   "key": "SEA-739",
   "status": "Open",
   "assignee": null
 }
 {
   "key": "SEA-738",
   "status": "Resolved",
   "assignee": "user2@mycompany.com"
 }
Run Code Online (Sandbox Code Playgroud)

但我需要解析每一行,但就关键组而言,很难确定哪个受让人是哪个密钥.这有可能使用jq在一行中生成一堆吗?

预期产量:

{ "key": "SEA-739", "status": "Open", "assignee": null }
{ "key": "SEA-738", "status": "Resolved", "assignee": "user2@mycompany.com"}
Run Code Online (Sandbox Code Playgroud)

要么

{ "SEA-739", "Open", null }
{ "SEA-738", "Resolved", user2@mycompany.com }
Run Code Online (Sandbox Code Playgroud)

json jq

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

在Scikit Learn中运行SelectKBest后获取功能名称的最简单方法

我想进行有监督的学习.

到现在为止,我知道要对所有功能进行监督学习.

但是,我还想进行K最佳功能的实验.

我阅读了文档,发现在Scikit中学习了SelectKBest方法.

不幸的是,我不确定在找到这些最佳功能后如何创建新的数据帧:

我们假设我想进行5个最佳功能的实验:

from sklearn.feature_selection import SelectKBest, f_classif
select_k_best_classifier = SelectKBest(score_func=f_classif, k=5).fit_transform(features_dataframe, targeted_class)
Run Code Online (Sandbox Code Playgroud)

现在,如果我要添加下一行:

dataframe = pd.DataFrame(select_k_best_classifier)
Run Code Online (Sandbox Code Playgroud)

我将收到一个没有功能名称的新数据帧(只有索引从0到4开始).

我应该把它替换为:

dataframe = pd.DataFrame(fit_transofrmed_features, columns=features_names)
Run Code Online (Sandbox Code Playgroud)

我的问题是如何创建features_names列表?

我知道我应该使用:select_k_best_classifier.get_support()

返回布尔值数组.

数组中的真值表示右列中的索引.

我应该如何使用这个布尔数组与我可以通过该方法获得的所有功能名称的数组:

feature_names = list(features_dataframe.columns.values)
Run Code Online (Sandbox Code Playgroud)

python feature-selection pandas scikit-learn

38
推荐指数
4
解决办法
3万
查看次数

是否有Scala的Option或Either的Python等价物?

我非常喜欢在Scala中使用Option和Either monad.Python中有这些东西的等价物吗?如果没有,那么什么是pythonic方式处理错误或"缺乏价值"而不抛出异常?

python functional-programming scala

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

使用awk删除不需要的字符

我有一个命令输出,我想从中删除双引号".

正则表达式:

strings -a libAddressDoctor5.so |\
grep EngineVersion |
awk '{if(NR==2)print}' |
awk '{print$2}'
Run Code Online (Sandbox Code Playgroud)

输出:

EngineVersion="5.2.5.624"
Run Code Online (Sandbox Code Playgroud)

我想知道如何用awk或删除不需要的字符sed.

linux awk replace sed

29
推荐指数
3
解决办法
10万
查看次数