小编ajw*_*ood的帖子

如何创建一个接受数组*或*可变数量标量的子程序?

我正在寻找一个子程序mysub,它应该表现为以下两个调用实际上是相同的.

mysub(["values", "in", "a", "list"]);
mysub("Passing", "scalar", "values");
Run Code Online (Sandbox Code Playgroud)

实现这一目标的正确语法是什么?

arrays perl scalar

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

如何用numpy进行数组查找/替换?

我有一个2D numpy数组,A包含另一个数组的索引,B.什么是C从numpy 获得AB使用numpy 的好方法?

A = array([[1, 1, 0, 2],
           [1, 0, 0, 2],
           [1, 1, 0, 2]])

B = array([0, 5, 3])

C = array([[5, 5, 0, 3],
           [5, 0, 0, 3],
           [5, 5, 0, 3]])
Run Code Online (Sandbox Code Playgroud)

python numpy

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

如何在numpy中执行此数组索引

给定一个索引数组I,如何设置D索引不存在的数据数组的值I

示例:如何我得到AID

I = array( [[1,1], [2,2], [3,3]] )

D = array( [[ 1, 2, 3, 4, 5, 6],
            [ 7, 8, 9, 1, 2, 3],
            [ 4, 5, 6, 7, 8, 9],
            [ 1, 2, 3, 4, 5, 6],
            [ 7, 8, 9, 1, 2, 3]] )

A = array( [[ 0, 0, 0, 0, 0, 0],
            [ 0, 8, 0, 0, 0, 0],
            [ 0, 0, …
Run Code Online (Sandbox Code Playgroud)

python arrays numpy

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

在perl测试中计划死亡

有没有办法为你期望死的Perl调用编写测试?我想验证某些调用会因格式错误的输入而死亡.

sub routine_a {
   my $arg = shift;
   die if $arg eq 'FOO';
   print "routine_a: $arg\n";
}
sub routine_b {
   my $arg = shift;
   die if $arg eq 'BAR';
   print "routine_b: $arg\n";
}

sub test_all {
   assert( routine_a("blah") );
   assert( routine_b("blab") );
   assert_death( routine_a("FOO") );
   assert_death( routine_b("BAR") );
}
Run Code Online (Sandbox Code Playgroud)

testing perl unit-testing

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

NumPy linspace舍入错误

有人可以解释这个四舍五入的问题numpy.linspace吗?

import numpy as np

np.linspace(0, 1, 6) == np.around( np.linspace(0, 1, 6), 10 )
# array([ True,  True,  True, False,  True,  True], dtype=bool)
Run Code Online (Sandbox Code Playgroud)

这是我到达这里的方式......

import numpy as np

## Two ways of defining the same thing
A = np.array([ 0., 0.2, 0.4, 0.6, 0.8, 1. ])
B = np.linspace(0, 1, 6)

## A and B appear to be the same
A # array([ 0., 0.2, 0.4, 0.6, 0.8, 1. ])
B # array([ 0., 0.2, 0.4, 0.6, …
Run Code Online (Sandbox Code Playgroud)

python floating-point numpy

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

如何在 pytest 夹具中获取参数 ID?

是否可以ID在 Pytest 夹具中获取参数?

import pytest

@pytest.fixture(
     params = ['a', 'b', 'c'],
     ids    = ['x', 'y', 'z'])
def foo(request):
   myParam = request.param
   myID    = "How do I get the current ID?"
Run Code Online (Sandbox Code Playgroud)

fixtures pytest

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

为什么这些时间戳与Perl Time :: HiRes无关?

我看到Perl Time::HiRes模块报告的时间戳有些奇怪的行为.

我有一个获得三个时间戳的脚本:

  1. 获取时间戳 Time::HiRes::time
  2. 创建一个新文件,并获取其修改时间 Time::HiRes::stat
  3. 获取时间戳 Time::HiRes::time

我希望订购时间戳1 < 2 < 3,但情况并非总是如此; 通常(但不总是),stat在2.中报告的时间是 1 之前的时间戳之前.

我在Ext4文件系统上.这是一个实验:

use Time::HiRes qw/ time stat /;

while( 1 ){
    # t0
    my $t0 = time;

    # Create a file
    my $f = '/tmp/dummy.test';
    open(my $fh, '>', $f) || die;
    print $fh "hi\n";
    close($fh) || die;

    # FS: file modification time, according to the filestystem
    my $fs = (stat($f))[9];

    # t1
    my $t1 = …
Run Code Online (Sandbox Code Playgroud)

unix perl timestamp ext4

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

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

如何在熊猫中枚举组内的组

我有一个像这样的数据帧:

      name visit foo
0   andrew    BL   a
1   andrew    BL   a
2   andrew    BL   b
3   andrew    BL   b
4      bob    BL   c
5      bob    BL   c
6      bob    BL   d
7      bob    BL   d
8      bob   M12   e
9      bob   M12   e
10     bob   M12   f
11     bob   M12   g
12   carol    BL   h
13   carol    BL   i
14   carol    BL   j
15   carol    BL   k
Run Code Online (Sandbox Code Playgroud)

我怎样才能创建一个新的列来枚举每组的foo['name', 'visit'],像这样?

      name visit foo  enum
0   andrew …
Run Code Online (Sandbox Code Playgroud)

python pandas

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

无法让Perl的群体工作

我似乎无法使Perl flock工作.我正在锁定一个文件,检查返回值以确保它实际上已被锁定,而且我仍然可以打开并写入它,就像没有任何事情一样.

这是我如何锁定文件

#!/usr/bin/perl -w

use strict;

use Fcntl ':flock';

$| = 1;

my $f = $ARGV[0];

open( my $fh, '>>', $f ) or die "Could not open '$f' - $!";
print "locking '$f'...";
flock($fh, LOCK_EX) or die "Could not lock '$f' - $!";
print "locked\n";

sleep 10;
print "waking up and unlocking\n";
close( $fh );
Run Code Online (Sandbox Code Playgroud)

当该脚本处于休眠状态时,我可以使用来自不同进程的相同文本文件

#!/usr/bin/perl -w

use strict;

my $f = $ARGV[0];

open( my $fh, '>>', $f ) or die "Could not open '$f' - $!"; …
Run Code Online (Sandbox Code Playgroud)

perl locking file

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