迭代到正则表达式匹配然后继续下一场比赛

时间:2016-05-29 23:38:32

标签: python loops python-3.x iteration

我正在构建一个脚本来解析交换机配置文件中的数据并且有点打嗝(使用Python 3.5)

这是我正在努力解决的伪代码:

LIBRARY IEEE;
    USE IEEE.STD_LOGIC_1164.ALL;
    USE IEEE.STD_LOGIC_UNSIGNED.ALL;

ENTITY algor_booth IS
    PORT(oper1 :  IN STD_LOGIC_VECTOR(7 DOWNTO 0);
         oper2 :  IN STD_LOGIC_VECTOR(7 DOWNTO 0);
         sel :    IN STD_LOGIC;
         result : OUT STD_LOGIC_VECTOR(15 DOWNTO 0)
            );
END ENTITY;

ARCHITECTURE algor OF algor_booth IS
BEGIN
    PROCESS (sel)
        VARIABLE A, S, P: STD_LOGIC_VECTOR(16 DOWNTO 0);
        VARIABLE Ma2: STD_LOGIC_VECTOR(7 DOWNTO 0);
        --VARIABLE flag: STD_LOGIC;
    BEGIN

        IF sel = '0' THEN
            Ma2 := (NOT oper1) + 1;
            A   := oper1 & "00000000" & '0';
            S   := Ma2 & "00000000" & '0';
            P   := "00000000" & oper2 & '0';
        ELSE
            --flag := '0';
            FOR i IN 1 TO 8 LOOP
                IF P(1 DOWNTO 0) = "01" THEN
                    P := P + A;
                    --flag := '0';
                    --P(17) := flag;
                ELSIF P(1 DOWNTO 0) = "10" THEN
                    P := P + S;
                    --flag := '1';
                    --P(17) := flag;
                END IF;
                --P(17) := flag;
                P :=    P(16) & P(16 downto 1);
                --P(17) := flag;
            END LOOP;
            result <= P(16 DOWNTO 1);

        END IF;
    END PROCESS;
END algor;

这是我到目前为止的代码,我尝试了很多变体,包括if re.match(patter0): continue to match pattern1 if re.match(pattern1): test if < 10 minutes return pass or fail 循环,for-in循环和嵌套while语句,但我没有在哪里。

if

上次运行时,如果下面显示的每个def net1639andnet1624(input_file): maxTimeOut = 10 pattern0 = re.compile("^line vty (([0-9] [0-9])|([0-9] [0-9][0-9]))$") pattern1 = re.compile( "^ exec-timeout\s([0-9]|[0-9][0-9]|[[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9])\s([0-9]|[0-9][0-9]|[[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9])$") for i, line in enumerate(open(input_file)): for match in re.finditer(pattern0, line): print(line) for match in re.finditer(pattern1, line): minutes = match.group(1) seconds = match.group(2) print(line) if int (minutes) > maxTimeOut: print('fail') else: print('pass') 应返回1,则返回None:

line vty

所以基本上,我试图达到它告诉我的地步

!
line con 0
 exec-timeout 0 30
 stopbits 1
line vty 0 3
 access-class 182 in
 exec-timeout 1440 30
 transport input telnet
line vty 4 15
 access-class 182 in
 no exec
 exec-timeout 0 1
 transport input none
!
end

1 个答案:

答案 0 :(得分:0)

我可能已经找到了一个解决方案,虽然它不是很动态,似乎它可能会在将来出现问题。就像我遇到一个超过三个vty的开关一样,代码需要调整。这是我今晚能够想到的,也会考虑思想或其他答案:

def net1639(input_file):
    maxTimeOut = 10
    p = 'pass'
    f = 'fail'
    pattern0 = re.compile("^line vty (([0-9] [0-9])|([0-9] [0-9][0-9]))$")
    pattern1 = re.compile(
        "^ exec-timeout\s([0-9]|[0-9][0-9]|[[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9])\s([0-9]|[0-9][0-9]|[[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]|[0-9][0-9][0-9][0-9][0-9])$")

    elements = []
    elements.append([])
    elements.append([])
    elements.append([])

    for i, line in enumerate(open(input_file)):
        for match in re.finditer(pattern0, line):
            if elements[0] is not None:
                elements[0].append(line.strip())
            elif elements[1] is not None:
                elements[1].append(line.strip())
            elif elements[2] is not None:
                elements[2].append(line.strip())
        for match in re.finditer(pattern1, line):
            minutes = match.group(1)
            seconds = match.group(2)
            if elements[0]:
                elements[0].append("Mintues: %s" % minutes)
                if int(minutes) <= maxTimeOut:
                    if int(minutes) == 0:
                        elements[0].append("Seconds: %s" % seconds)
                    elements[0].append(p)
                else:
                    elements[0].append(f)
            elif elements[1]:
                elements[1].append(minutes)
                if int(minutes) > maxTimeOut:
                    if int(minutes) == 0:
                        elements[1].append("Seconds: %s" % seconds)
                   elements[1].append(p)
                else:
                    elements[1].append(f)
            elif elements[2]:
                elements[2].append(minutes)
                if int(minutes) > maxTimeOut:
                    if int(minutes) == 0:
                        elements[1].append("Seconds: %s" % seconds)
                        elements[2].append(p)
                else:
                    elements[2].append(f)


    for row in elements:
        for column in row:
            print(column)

以下是输出图片:

enter image description here