小编Bri*_*hon的帖子

你如何在python中生成动态(参数化)单元测试?

我有一些测试数据,想为每个项目创建一个单元测试.我的第一个想法是这样做:

import unittest

l = [["foo", "a", "a",], ["bar", "a", "b"], ["lee", "b", "b"]]

class TestSequence(unittest.TestCase):
    def testsample(self):
        for name, a,b in l:
            print "test", name
            self.assertEqual(a,b)

if __name__ == '__main__':
    unittest.main()
Run Code Online (Sandbox Code Playgroud)

这样做的缺点是它在一次测试中处理所有数据.我想在运行中为每个项目生成一个测试.有什么建议?

python unit-testing parameterized-unit-test

211
推荐指数
10
解决办法
8万
查看次数

Python str vs unicode类型

使用Python 2.7,我想知道使用类型unicode而不是str因为它们两者似乎能够保存Unicode字符串的真正优势.除了能够unicode使用转义字符串在字符串中设置Unicode代码之外,还有什么特殊原因\吗?:

执行模块:

# -*- coding: utf-8 -*-

a = 'á'
ua = u'á'
print a, ua
Run Code Online (Sandbox Code Playgroud)

结果:á,á

编辑:

使用Python shell进行更多测试:

>>> a = 'á'
>>> a
'\xc3\xa1'
>>> ua = u'á'
>>> ua
u'\xe1'
>>> ua.encode('utf8')
'\xc3\xa1'
>>> ua.encode('latin1')
'\xe1'
>>> ua
u'\xe1'
Run Code Online (Sandbox Code Playgroud)

因此,unicode字符串似乎使用latin1而不是编码utf-8,原始字符串使用utf-8?我现在更加困惑了!:S

python string unicode

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

从案例类中获取字段名称列表

我只需要获取案例类的字段名称.我对它的价值观不感兴趣.我以为getClass.getDeclaredFields.map(_.getName)会返回一个字段名称列表.

scala> case class User(id: Int, name: String)
defined class User

scala> User.getClass.getDeclaredFields
res14: Array[java.lang.reflect.Field] = Array(public static final User$ User$.MODULE$)

scala> User.getClass.getDeclaredFields.toList
res15: List[java.lang.reflect.Field] = List(public static final User$ User$.MODULE$)

scala> val user = User(1, "dude")
user: User = User(1,dude)

scala> user.getClass.getDeclaredFields.toList
res16: List[java.lang.reflect.Field] = List(private final int User.id, private final java.lang.String User.name)
Run Code Online (Sandbox Code Playgroud)

什么是这个用户$ .MODULE $?那是什么?

当你有一个case类的实例时,方法getDeclaredFields可以正常工作,但是我不想创建一个实例以便只获取字段.

为什么这不是真的: User.getClass.getDeclaredFields.map(_.getName) == List("id", "name")

scala

36
推荐指数
5
解决办法
3万
查看次数

Scala贴图 - >运算符

符号在Scala ->中的含义是什么意思Map

Scala的Predef类提供了一种隐式转换,允许一次写入 key -> value作为该对的替代语法(key, value).我在ScalaByExample中阅读了它,但未能看到它对Maps的工作原理.

dictionary scala

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

如何从NetBeans中的"Resources"文件夹中正确获取图像

我在NetBeans 7.0中有一个Java项目.

我想动态地将一些图像添加到某个标签.图像将根据程序的状态而有所不同.

我把一个这样的图像'filling.jpg'放在我项目的'resources'文件夹中.

我想正确地访问此文件(不是通过绝对路径或相对路径,因为这会在构建jar文件时导致问题).

所以我找到了这个方法:

ImageIcon fillingIcon = new ImageIcon(getClass().getClassLoader().getResource("filling.jpg"));
labelFontFilling.setIcon(fillingIcon);
Run Code Online (Sandbox Code Playgroud)

它一直给我java.lang.NullPointerException.但我确信有那个图像,因为我可以从NetBeans Properties菜单中为该标签分配图像(但我不想这样,我想通过Java代码添加图像).

我做错了什么,我怎样才能正确地获得该图像?

java resources netbeans

31
推荐指数
3
解决办法
17万
查看次数

使用es6 spread来连接多个数组

我们都知道你可以这样做:

let arr1 = [1,2,3];
let arr2 = [3,4,5];
let arr3 = [...arr1, ...arr2]; // [1,2,3,3,4,5]
Run Code Online (Sandbox Code Playgroud)

但是,如何使这种动态连接N阵列?

javascript ecmascript-6

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

如何在Google Compute Engine中设置ssh密钥?

我正在尝试通过Google Cloud Platform控制台设置ssh密钥.我在Puttygen中制作了一个键,但将它粘贴到控制台的格式是什么?我收到了这个错误:

Error: Invalid key. Required format: <protocol> <key-blob> <username@example.com>
Run Code Online (Sandbox Code Playgroud)

cloud ssh putty google-compute-engine google-cloud-platform

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

document.head诉document.getElementsByTagName("head")[0]

使用document.head和使用有document.getElementsByTagName("head")[0]什么区别?我跑的测试表明它们都需要大约一毫秒.

我也见过

document.head||document.getElementsByTagName("head")[0];
Run Code Online (Sandbox Code Playgroud)

这会让我相信它document.head更快,而另一个更兼容,除了我做的测试反驳了这一点.

如果一个更兼容,为什么还要使用另一个?

更新:正如一些人所指出的,我的测试是错误的.

javascript dom

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

Java 8是否有尾调用优化?

我尝试在网上挖掘以解答我的问题.我找到了一些与达芬奇项目有关的文件.这被标记为JSR 292,它与在JVM中包含闭包有关.这个项目是否实现了,它是Java 8的一部分吗?

java tail-call-optimization java-8

14
推荐指数
2
解决办法
9512
查看次数

AngularJS指令中的可选表达式属性

我有一个自定义导航指令需要一个可选的"禁用"属性,我不确定它是否可能.

在我的主控制器中:

.controller('NavCtrl', ['UserResource','RoleResource'], function(UserResource,RoleResource){
      var user = UserResource.getUser();
      var roles = RoleResource.getRoles();
      UserService.init(user, roles); //????

});
Run Code Online (Sandbox Code Playgroud)

在我的指令中:

.directive('navItem', function(){
    return{
        restrict: 'A',
        scope: {
            text: '@',
            href: '@',
            id: '@',
            disable: '&'

        },
        controller: function($scope, $element, $attrs){
            $scope.disabled = ''; //Not sure I even need a controller here
        },
        replace: true,
        link: function(scope, element, attrs){
            scope.$eval(attrs.disable);
        },
        template: '<li class="{{disabled}}"><a href="{{href}}" id="{{id}}">{{text}}</a></li>'

    }

});
Run Code Online (Sandbox Code Playgroud)

在我的HTML中,我想做这样的事情:

<div data-nav-item text="My Text" href="/mytemplate.html" id="idx"
     disable="UserService.hasRole('ADMIN,BILLING') && someOtherFn(xxx) || ...">
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-directive

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