小编Tha*_*ela的帖子

grep和perl的正/负前瞻

我的login.txt文件包含以下条目

abc def
abc 123
def abc
abc de
tha ewe
Run Code Online (Sandbox Code Playgroud)

当我使用perl做正向前瞻时,我得到以下结果

cat login.txt | perl -ne 'print if /(?)abc\s(?=def)/'
abc def
Run Code Online (Sandbox Code Playgroud)

当我使用grep时,我得到以下结果

cat login.txt | grep -P '(?<=abc)\s(?=def)'
abc def
Run Code Online (Sandbox Code Playgroud)

来自perl和grep的负面外观结果如下.

 cat login | perl -ne 'print if /(?)abc\s(?!def)/'
abc 123
def abc
abc de
Run Code Online (Sandbox Code Playgroud)

grep结果

cat login.txt | grep -P '(?<=abc)\s(?!def)'
abc 123
abc de
Run Code Online (Sandbox Code Playgroud)

perl与def abc匹配负向前瞻.但它不应该与def abc匹配,因为我正在检查abc然后def模式.grep返回正确的结果.

在我的perl模式中缺少什么?

regex bash perl grep negative-lookahead

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

输入错误Iter - Python3

有人可以解释为什么以下代码给出

TypeError: iter() returned non-iterator of type 'counter'  in python 3
Run Code Online (Sandbox Code Playgroud)

这是在python 2.7.3中工作,没有任何错误.

#!/usr/bin/python3

class counter(object):

    def __init__(self,size):
        self.size=size
        self.start=0

    def __iter__(self):
        print("called __iter__",self.size)
        return self

    def next(self):
        if self.start < self.size:
            self.start=self.start+1
            return self.start
        raise StopIteration

c=counter(10)
for x in c:
    print(x)
Run Code Online (Sandbox Code Playgroud)

python python-2.7 python-3.x

9
推荐指数
2
解决办法
3354
查看次数

Packer、Dockramp 与 Dockerfile

有人可以在构建容器映像时使用以下内容而不是使用 dockerfile 来解释优点/缺点吗?

  1. Packer - 用于从单个源配置为多个平台创建机器和容器映像的工具

  2. Dockramp - 客户端驱动的 Docker 容器映像生成器

linux continuous-integration packer docker kubernetes

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

相当于Python 3中的coerce()

coerce() 哪个功能

使用与算术运算相同的规则,返回由转换为通用类型的两个数字参数组成的元组

在Python 3中已弃用。我想知道Python 3.x中是否引入了等效的功能?

python python-3.x

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

Erlang案例陈述

我有以下Erlang代码,当我尝试编译它时,它给出如下警告,但这是有道理的.函数需要两个参数,但我需要匹配"其他所有"而不是x,y或z.

-module(crop).
-export([fall_velocity/2]).

fall_velocity(P, D) when D >= 0 ->
case P of
x -> math:sqrt(2 * 9.8 * D);
y -> math:sqrt(2 * 1.6 * D);
z -> math:sqrt(2 * 3.71 * D);
(_)-> io:format("no match:~p~n")
end.

crop.erl:9: Warning: wrong number of arguments in format call. 
Run Code Online (Sandbox Code Playgroud)

我在io:format之后尝试了一个匿名变量,但它仍然不开心.

erlang erl erlang-shell

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

Erlang命令行

我需要将两个参数传递给我的Erlang代码.它在Erlang shell中运行良好.

2> crop:fall_velocity(x,23).
  21.23205124334434
Run Code Online (Sandbox Code Playgroud)

但是如何在没有Erlang shell的情况下运行Erlang代码.像普通的python,c程序../program_name(不传递$ 1 $ 2参数).

我正在尝试这个

erl -noshell -s crop fall_velocity(x,20) -s init stop
Run Code Online (Sandbox Code Playgroud)

但它给出了意外的令牌错误.

erlang erl erlang-shell

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

python中的__str__方法

我想知道为什么 python 魔术方法 ( str ) 总是寻找 return 语句而不是打印方法?

class test:

   def __init__(self):
       print("constructor called")

   def __call__(self):
        print("callable")

   def __str__(self):
        return "string method"



  obj=test()  ## print constructor called


  obj()    ### print callable


  print(obj) ## print string method
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么我不能在str方法中使用这样的东西

 def __str__(self):
        print("string method")
Run Code Online (Sandbox Code Playgroud)

python python-3.x

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

Erlang if语句并返回true

我想知道,erlang的if语句和返回值背后的想法(在这种情况下是true-> true).这是我的代码片段

if
 (Velocity > 40) -> io:format(" processing fast !~n") ;
 true -> true
end,
Run Code Online (Sandbox Code Playgroud)

我知道Erlang不允许你有一个if if without true statement选项.但即使我可以使用true-> false但它对最终输出无关紧要.

实际上if子句和返回值背后的想法是什么.

erlang erlang-shell

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

__call__ 在类内部调用方法时

我试图理解__call__(python3)的含义。写了这个区分每个方法__init____call__以及测试方法。

#!/usr/bin/python3

class thara(object):

   def __init__(self):
        print("init called")

   def __call__(self):
       print("call called")

   def test(self):
       print("test called")

x=thara()  ### constructor calling here
x()   ## __call__  calling here
x.test() ## test method calling here
Run Code Online (Sandbox Code Playgroud)

我的问题是当我启动时x.test(),为什么不调用__call__?我在想的是,如果我启动 x.test() 将启动实例x(),它应该__call__自动调用该 方法。但根据我的输出__call__只会在启动时调用x()

有人可以解释一下。

python python-3.x

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