小编sla*_*fer的帖子

Python模块库

Dumper在python中寻找类似于perl 功能的东西.所以在谷歌上搜索后我发现了一个很好的服务@ https://gist.github.com/1071857#file_dumper.pyamazon

所以我下载并安装它,它工作正常.

但后来我遇到了PyPI:http://pypi.python.org/pypi,看起来像CPAN等效于python.

所以我在那里搜索了Dumper模块,我找不到它.我希望这看起来像一个非常基本的模块,应该在PyPI中列出.

所以我的问题是,如果我必须安装一个python模块,那么我应该首先在PyPI中搜索,如果我没有找到,那么在谷歌上寻找其他地方?

或者除了PyPI之外还有其他任何Python模块存储库吗?

我正在学习python,因此这个问题.

谢谢.

python

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

将perl转换为python - 这一行做了什么(类变量混淆)

$self->{DES} ->{$id} = join("\t",@tmp[0 ..7]);

这一行是构造函数所在的perl类内部函数的一部分

sub new {
    my $class=shift;
    my $self ={};
    bless($self,$class);
    return $self;
}
Run Code Online (Sandbox Code Playgroud)

我解释它的方式是我们将id行存储为类变量DES,它是一个散列,其成员是$ id:.它是否正确?

我还想对=~运算符进行一些澄清(它似乎总是在正则表达式之前).据我所知,它基本上与python中的相同,re.X其中X依赖于perl中的正则表达式之后的标志(例如i).它是否正确?

python perl

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

将sed One-Liner转换为perl

sed在我的perl剧本中使用这个单行程.但是,有没有办法(我确定有)在perl中做同样的工作?

这是我的档案:

$ cat all_log1.txt 
Looking into the table etl_f_gl_balance
# of -1 values in  etl_f_gl_balance.row_key:
0
# of -1 values in  etl_f_gl_balance.ledger_key:
1020
# of -1 values in  etl_f_gl_balance.gl_account_key:
1020
# of -1 values in  etl_f_gl_balance.legal_entity_key:
1020
# of -1 values in  etl_f_gl_balance.cost_center_key:
995
# of -1 values in  etl_f_gl_balance.natural_account_key:
1020
# of -1 values in  etl_f_gl_balance.posting_period_fiscal_key:
5
# of -1 values in  etl_f_gl_balance.posting_period_key:
5
Run Code Online (Sandbox Code Playgroud)

我想要输出如下,这是我的sed解决方案:

$ sed ':a; N; $!ba; s/:\n/: /g' …
Run Code Online (Sandbox Code Playgroud)

regex bash perl sed

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

如何忽略异常并继续运行脚本

我正在运行一个Vmware脚本,它在中途通过然后失败并出现以下错误并退出:

Can't use an undefined value as an ARRAY reference at test.pl line 49.

这是在vCetner库存上运行的,该库存有大约4000个VM.所以我希望脚本忽略这些错误并继续运行直到它完成.目前,脚本在出现上述错误后退出任何输出.

这是我的代码,它给了我上面的错误.我只粘贴几行错误来自:

foreach my $vm_mo_ref ( @{ $host_view->vm } )
{
        print $vm_mo_ref."\n";
}
Run Code Online (Sandbox Code Playgroud)

这就是我忽略错误的方法:

foreach my $vm_mo_ref ( @{ $host_view->vm } )
{
    if (defined $vm_mo_ref)
    {   
            print $vm_mo_ref."\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

foreach my $vm_mo_ref ( @{ $host_view->vm } )
{
    if (exists $vm_mo_ref)
    {   
            print $vm_mo_ref."\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

foreach my $vm_mo_ref ( @{ $host_view->vm } )
{
    if ($vm_mo_ref)
    {   
            print $vm_mo_ref."\n"; …
Run Code Online (Sandbox Code Playgroud)

perl

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

perl"DateTime"模块打印错误的时间

我正在使用DateTime模块.但它提供了错误的时间.请考虑以下代码:

#!/usr/bin/perl -w
use strict;

use Time::localtime;
my $now = ctime();
print $now."\n";

print "------------------------------\n";

use DateTime;
my $dt = DateTime->now;
print $dt."\n";
Run Code Online (Sandbox Code Playgroud)

它的输出是:

Wed Dec 26 22:11:52 2012
------------------------------
2012-12-27T06:11:52
Run Code Online (Sandbox Code Playgroud)

所以,正如你所看到的,DateTime输出是8小时,这是错误的.这是Linux date命令输出:

# date
Wed Dec 26 22:13:17 PST 2012
Run Code Online (Sandbox Code Playgroud)

因此,date命令输出与输出的输出匹配time::localtime.

你能帮我理解我在使用DateTime模块时出错了吗?

-谢谢.

更新:

从hte CPAN文档:

DateTime->now( ... )

This class method is equivalent to calling from_epoch() with the value returned from Perl's time() function. Just as with …
Run Code Online (Sandbox Code Playgroud)

perl

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

Ruby +递归函数+定义全局变量

我正在使用Ruby来提取bitbucket repo列表.来自bitbucket的响应将只包含10个存储库和下一页的标记,其中将有另外10个存储库等等...(他们称之为分页)

所以,我编写了一个递归函数,如果存在下一个页面标记,它会调用自身.这将一直持续到最后一页.

这是我的代码:

#!/usr/local/bin/ruby
require 'net/http'
require 'json'
require 'awesome_print'

@repos = Array.new

def recursive(url)

    ### here goes my net/http code which connects to bitbucket and pulls back the response in a JSON as request.body 
    ### hence, removing this code for brevity

    hash = JSON.parse(response.body)

    hash["values"].each do |x|
        @repos << x["links"]["self"]["href"]
    end

    if hash["next"]
        puts "next page exists"
        puts "calling recusrisve with: #{hash["next"]}"
        recursive(hash["next"])
    else
        puts "this is the last page. No more recursions"
    end
end

repo_list = recursive('https://my_bitbucket_url') …
Run Code Online (Sandbox Code Playgroud)

ruby recursion

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

拆分字段,然后删除重复项

样本文件:

# cat test1 
-rw-r--r-- 1 root root   19460 Feb 10 03:56 catalina.2015-02-10.log
-rw-r--r-- 1 root root  206868 May  4 15:05 catalina.2015-05-04.log
-rw-r--r-- 1 root root  922121 Jun 24 09:26 catalina.out
-rw-r--r-- 1 root root       0 Feb 10 02:27 host-manager.2015-02-10.log
-rw-r--r-- 1 root root       0 May  4 04:17 host-manager.2015-05-04.log
-rw-r--r-- 1 root root    2025 Feb 10 03:56 localhost.2015-02-10.log
-rw-r--r-- 1 root root    8323 May  4 15:05 localhost.2015-05-04.log
-rw-r--r-- 1 root root     873 Feb 10 03:56 localhost_access_log.2015-02-10.txt
-rw-r--r-- 1 root root …
Run Code Online (Sandbox Code Playgroud)

awk

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

完全删除数组元素

目标:从数组中删除特定值

我写了一个脚本,它工作正常,但我写的方式并不高兴.所以我很想知道是否有更好的方法来编写它.请考虑以下用例:

我有一个嵌套的哈希/哈希/数组......如下所示.我需要删除local名称中包含的任何数组值:

#!/usr/bin/perl -w
use strict;
use Data::Dumper;

my $hash = { esx1 => 
                    { cluster   => "clu1",
                      fd        => "fd1",
                      ds        => [ 
                                        'ds1',
                                        'ds2',
                                        'localds',
                                    ],  
                    },  
            esx2 => 
                    { cluster   => "clu2",
                      fd        => "fd2",
                      ds        => [ 
                                        'ds3',
                                        'ds4',
                                        'dslocal',
                                    ],  
                    },  
            };  


foreach my $a ( keys %$hash )
{
    foreach ( 0..$#{ $hash->{$a}->{ds} } ) 
    {   
        delete $hash->{$a}->{ds}->[$_] if $hash->{$a}->{ds}->[$_] =~ /local/i;
        @{ $hash->{$a}->{ds} } = grep defined, @{ $hash->{$a}->{ds} …
Run Code Online (Sandbox Code Playgroud)

perl

0
推荐指数
1
解决办法
395
查看次数

找到三个浮点数中最小的一个

我没有意识到这个简单的问题很难破解.

无论如何,我有一个包含3个浮点数的数组.我需要找出所有三个中最小的一个(在这种情况下最小的三个,我的实际代码将包含多于3个浮点数)

这是我的代码:

#!/usr/bin/perl -w
use strict;
use List::Util qw /min/;
my @array = ("2.7.4", "2.7.0", "2.7.0");
print min(@array);
Run Code Online (Sandbox Code Playgroud)

OUTPUT:

Argument "2.7.4" isn't numeric in subroutine entry at min.pl line 6.
Argument "2.7.0" isn't numeric in subroutine entry at min.pl line 6.
Argument "2.7.0" isn't numeric in subroutine entry at min.pl line 6.
2.7.4
Run Code Online (Sandbox Code Playgroud)

所以它输出了很多错误,输出数也是错误的.

我拍摄的另一个镜头是对数组进行排序(sort {$a <=> $b} @array)然后找到最小的一个但是它再次无法完成这项工作.

我可以通过数字比较进行数字编码,但是如果数组中的元素数量超过3,则代码看起来很讨厌.

你能帮忙吗.

谢谢.

perl

0
推荐指数
1
解决办法
562
查看次数

从最后但一行打印特定字段

我试图解析此输出:

$ top -b -d 1 -n 1 -p 5997
top - 08:54:39 up 86 days,  2:42,  1 user,  load average: 0.00, 0.03, 0.05
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):  8.9%us,  1.5%sy,  0.0%ni, 89.5%id,  0.1%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   3869696k total,  2010912k used,  1858784k free,   162404k buffers
Swap:        0k total,        0k used,        0k free,   736120k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 5997 tomcat7   20   0 1209m 534m …
Run Code Online (Sandbox Code Playgroud)

bash perl awk

0
推荐指数
1
解决办法
146
查看次数

标签 统计

perl ×7

awk ×2

bash ×2

python ×2

recursion ×1

regex ×1

ruby ×1

sed ×1