小编Kub*_*uba的帖子

如何grep git diff?

有没有办法显示由给定模式过滤的git-diff.

就像是

git grepdiff pattern

changed file
+++ some sentence with pattern
changed file 2
--- some other pattern
Run Code Online (Sandbox Code Playgroud)

不幸的是,最简单的解决方案还不够好

git diff | grep pattern 

+++ some sentence with pattern
--- some other pattern
# not an option as doesn't put the filename close to the match
Run Code Online (Sandbox Code Playgroud)

我使用awk来解决方法

git diff | awk "/\+\+\+/{f = \$2}; /PATTERN/ {print f \$0} "
Run Code Online (Sandbox Code Playgroud)

但是很想知道有这方面的命令.

git diff grep

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

没有显式数据库名称的当前数据库上的ALTER DATABASE?

我希望能够编写更改数据库的sql查询,以便我当前登录.

例:

$ psql my_db
psql(9.1.1)
my_db=> ALTER DATABASE my_db SET some_variable = '0';
                       ^^^^^
Run Code Online (Sandbox Code Playgroud)

有没有办法避免在此查询中指定数据库名称?

sql postgresql

10
推荐指数
2
解决办法
1700
查看次数

java允许使用功能接口作为方法吗?

With the new java lambdas and the concept of functional interfaces, will it be possible to treat those functional interfaces as methods?

interface Func { void execute(int i); }

void call(Func f)
{
    f(1); //instead of f.execute(1);
}
Run Code Online (Sandbox Code Playgroud)

I found a lot of information about the syntax of actual lambda expressions, but nothing about this.

java lambda closures java-8

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

矩阵乘法张量流与numpy的差异

我有一个案例,其中两个具有一定维度的矩阵的矩阵乘法在numpy中工作,但在tensorflow中不起作用.

x = np.ndarray(shape=(10,20,30), dtype = float)
y = np.ndarray(shape=(30,40), dtype = float)
z = np.matmul(x,y)
print("np shapes: %s x %s = %s" % (np.shape(x), np.shape(y), np.shape(z)))
Run Code Online (Sandbox Code Playgroud)

这按预期工作并打印:

np shapes: (10, 20, 30) x (30, 40) = (10, 20, 40)
Run Code Online (Sandbox Code Playgroud)

但是在tensorflow中,当我尝试将占位符和与上面的numpy数组相同形状的变量相乘时,我得到一个错误

x = tf.placeholder(tf.float32, shape=(10,20,30))
y = tf.Variable(tf.truncated_normal([30,40], name='w'))
print("tf shapes: %s x %s" % (x.get_shape(), y.get_shape()))
tf.matmul(x,y)
Run Code Online (Sandbox Code Playgroud)

结果是

tf shapes: (10, 20, 30) x (30, 40)
InvalidArgumentError: 
Shape must be rank 2 but is rank 3 for 'MatMul_12' 
(op: …
Run Code Online (Sandbox Code Playgroud)

python numpy matrix tensorflow

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

私人领域的垃圾收集

鉴于下面的代码.

class A {
    private B b;
    public A() {
        b = new B();
    }
}

class Main {
    public static void main(String[] args) {
        A a = new A(); // two objects are created (a and b)
        // <-- is B object, referenced only by private a.b eligible for garbage collection?
        keepAlive(a);
    }
}
Run Code Online (Sandbox Code Playgroud)

在创建A对象后,B对象可以被垃圾收集吗?

java garbage-collection

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

涉及外键约束的死锁

我想更好地理解postgres中的锁定机制.

假设树可以有苹果(通过苹果桌上的外键).似乎在选择树时进行更新锁定是在苹果上获得的.但是,即使其他人已经锁定了这个苹果,操作也不会被阻止.

为什么会这样?

ps请不要建议删除"选择更新".

脚本

Transaction 1      Transaction 2
BEGIN              .
update apple;      .
.                  BEGIN
.                  select tree for update;
.                  update apple;
.                  --halts because of the other transaction locking an apple
update apple;      .
-- deadlock        .
                   COMMIT
                   --transaction succeeds
Run Code Online (Sandbox Code Playgroud)

如果你想在你的postgres中尝试 - 这是一个你可以复制/粘贴的代码.

我有一个以下的数据库架构

CREATE TABLE trees (
    id       integer primary key
);

create table apples (
    id       integer primary key,
    tree_id  integer references trees(id)
);
Run Code Online (Sandbox Code Playgroud)

而且非常简单的数据

insert into trees values(1);
insert into apples values(1,1);
Run Code Online (Sandbox Code Playgroud)

有两个简单的交易.一个是更新苹果,第二个是锁定树并更新苹果. …

postgresql deadlock database-deadlocks

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

正则表达式搜索是否保证返回第一个匹配?

我正在寻找一种方法来返回字符串中给定正则表达式的第一个匹配.它看起来就像re.search我正在寻找的方法.

但是,文档没有明确说明是否保证从搜索方法返回第一个匹配项.

该文档声称该方法"扫描字符串",它告诉我它从字符串的开头这样做.

但是我需要一些强有力的论据.仅仅是测试一个cat1被发现cat1cat2是不够的.

最好的是对官方文档或实现的暗示.

python regex

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

添加具有不同尺寸的矩阵

在numpy和tensorflow中,如果较小矩阵的形状是较大矩阵的后缀,则可以添加不同维度的矩阵(或张量).这是一个例子:

x = np.ndarray(shape=(10, 7, 5), dtype = float)
y = np.ndarray(shape=(7, 5), dtype = float)
Run Code Online (Sandbox Code Playgroud)

对于这两个矩阵,操作x+y是一个快捷方式:

for a in range(10):
    for b in range(7):
        for b in range(5):
            result[a,b,c] = x[a,b,c] + y[b,c]
Run Code Online (Sandbox Code Playgroud)

在我的情况,但是我有形状的矩阵(10,7,5)(10,5)与同样予想执行+使用类似的逻辑运算:

for a in range(10):
    for b in range(7):
        for b in range(5):
            result[a,b,c] = x[a,b,c] + y[a,c]
                                         ^
Run Code Online (Sandbox Code Playgroud)

在这种情况下,x+y操作失败,因为numpy和tensorflow都不了解我想要做的事情.有没有什么方法可以有效地执行此操作(不自己编写python循环)?

到目前为止,我已经想过我可以使用einsum 引入一个临时z的形状矩阵,(10,7,5)如下所示:

z = np.einsum('ij,k->ikj', y, np.ones(7))
x + z
Run Code Online (Sandbox Code Playgroud)

但这会创建一个明确的三维矩阵(或张量),如果可能的话我宁愿避免这种情况.

python numpy matrix tensorflow

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

如何配置我的IPython笔记本,以便始终将执行时间显示为输出的一部分?

有时我执行一个需要很长时间才能计算的方法

In [1]:

long_running_invocation()

Out[1]:
Run Code Online (Sandbox Code Playgroud)

我常常有兴趣知道花了多少时间,所以我必须写下这个:

In[2]:
    import time

    start = time.time()
    long_running_invocation()
    end = time.time()
    print end - start
Out[2]: 1024
Run Code Online (Sandbox Code Playgroud)

有没有办法配置我的IPython笔记本,以便它自动打印我正在制作的每个调用的执行时间,如下例所示?

In [1]:

long_running_invocation()

Out[1] (1.59s):
Run Code Online (Sandbox Code Playgroud)

ipython-notebook

4
推荐指数
2
解决办法
1780
查看次数

postgres处理重复的程度如何?

我有一个表,我存储相对较大的字符串(10000个字符).

我会有很多行(数百万?),但99%的行将具有相同的大字符串值.

有谁知道postgres是否有机制来处理这个案子?(即不要使用gigabates存储)

postgresql

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