Search code examples
pythonpython-2.7for-loopiterator

How to check next iter item without proceeding in For loop in Python


When iterating via next(iter) I am wanting to basically group together networks, I do this by calculating how many items appear in each line, if it has 7 items, it's the start of a entry.

I then look for lines beneath the entry line, until I find another line with 7 items. The process then starts over.

Only problem is when I'm access the "next line" via line = next(bgp_table_iter) and break the loop, if the "next line" has 7 items meaning start a new entry, when it starts back at for line in bgp_table_iter: it iterates again to the next line skipping the original "next line" that should have 7 items and is the start of a new entry.

Which end's up skipping every other entry.

How could I prevent a the skip? Or make the For loop trigger going to the previous iteration on break?

def show_ip_bgp():
    data = test_output_short.split('RPKI validation codes: V valid, I invalid, N Not found\n\n     Network          Next Hop            Metric LocPrf Weight Path\n')
    bgp_table = data[1]
    bgp_table = re.sub("     Network          Next Hop            Metric LocPrf Weight Path\n","",bgp_table)
    bgp_table = re.sub("\*","",bgp_table)

    bgp_table_list = bgp_table.split('\n')

    bgp_table_iter = iter(bgp_table_list)

    overall_dict = {}
    index = 0
    for line in bgp_table_iter:
        # print line.split(), len(line.split())

        current_line = line.split()
        # If 7 items are in the list it's the start of a network
        if len(current_line) == 7:
            best = True if '>' in current_line[0] else False
            network = current_line[1]
            attached_ip = current_line[2]
            print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
            for i in range(0,1000):
                line = next(bgp_table_iter)
                subline_line = line.split()
                best_sub = 'True' if '>' in subline_line[0] else 'False'
                if len(subline_line) == 6:
                    print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
                elif len(subline_line) == 5:
                    print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
                elif len(subline_line) == 7:
                    break



test_output_short = '''BGP table version is 3538854, local router ID is 10.15.1.81
Status codes: s suppressed, d damped, h history, * valid, > best, i - internal,
              r RIB-failure, S Stale, m multipath, b backup-path, f RT-Filter,
              x best-external, a additional-path, c RIB-compressed,
Origin codes: i - IGP, e - EGP, ? - incomplete
RPKI validation codes: V valid, I invalid, N Not found

     Network          Next Hop            Metric LocPrf Weight Path
 * i 10.0.1.0/24      10.8.111.45              0    115      0 i
 *>i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i 10.0.3.0/29      10.8.111.45              0    115      0 i
 *>i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i                  10.8.11.45               0    120      0 i
 * i 10.8.0.0/16      10.9.0.1                 0     50      0 i
 * i                  10.8.0.1                 0     50      0 i
 *>                   0.0.0.0                  0         32768 i
 * i 10.9.0.0/16      10.9.0.1                 0     50      0 i
 * i                  10.8.0.1                 0     50      0 i
 *>                   0.0.0.0                  0         32768 i
 *>i 10.10.2.0/24     10.8.10.2                0    100      0 i
 * i                  10.8.10.2                0    100      0 i
 * i                  10.8.10.2                0    100      0 i
 * i 10.10.5.0/24     10.8.142.15              0     85      0 i
 *>i                  10.8.42.15               0    100      0 i
 * i                  10.8.42.15               0    100      0 i
 * i                  10.8.42.15               0    100      0 i
 *>i 10.10.7.0/24     10.8.40.84               0    100      0 i
 * i                  10.8.40.84               0    100      0 i
 * i                  10.8.40.84               0    100      0 i
 *>i 10.10.8.0/24     10.8.10.8                0    100      0 i
 * i                  10.8.110.8               0     85      0 i
 * i                  10.8.10.8                0    100      0 i
 * i                  10.8.10.8                0    100      0 i
 *>i 10.10.11.0/24    10.8.42.8                0    100      0 i
 * i                  10.8.42.8                0    100      0 i
 * i                  10.8.42.8                0    100      0 i
 * i                  10.9.42.8                0    100      0 i
 * i 10.10.12.0/24    10.8.10.12               0    100      0 i
 * i                  10.8.10.12               0    100      0 i
 *>i                  10.8.10.12               0    100      0 i'''

Output:

>>> show_ip_bgp()
Found Starting Network 10.0.1.0/24 10.8.111.45 Best Path: False
Sub: 10.8.11.45 Best Path: True
Sub: 10.8.11.45 Best Path: False
Sub: 10.8.11.45 Best Path: False
Found Starting Network 10.8.0.0/16 10.9.0.1 Best Path: False
Sub: 10.8.0.1 Best Path: False
Sub: 0.0.0.0 Best Path: True
Found Starting Network 10.10.2.0/24 10.8.10.2 Best Path: True
Sub: 10.8.10.2 Best Path: False
Sub: 10.8.10.2 Best Path: False
Found Starting Network 10.10.7.0/24 10.8.40.84 Best Path: True
Sub: 10.8.40.84 Best Path: False
Sub: 10.8.40.84 Best Path: False
Found Starting Network 10.10.11.0/24 10.8.42.8 Best Path: True
Sub: 10.8.42.8 Best Path: False
Sub: 10.8.42.8 Best Path: False
Sub: 10.9.42.8 Best Path: False

UPDATE:

The suggested fix appeared to work until I ran tests. It does make all networks appear sort of.

def show_ip_bgp(): data = test_output_very_short.split('RPKI validation codes: V valid, I invalid, N Not found\n\n Network Next Hop Metric LocPrf Weight Path\n') bgp_table = data[1] bgp_table = re.sub(" Network Next Hop Metric LocPrf Weight Path\n","",bgp_table) bgp_table = re.sub("*","",bgp_table)

bgp_table_list = bgp_table.split('\n')

bgp_table_iter = iter(bgp_table_list)

overall_dict = {}
index = 0
for i, line in enumerate(bgp_table_list):
    # print line.split(), len(line.split())

    current_line = line.split()
    # If 7 items are in the list it's the start of a network
    if len(current_line) == 7:
        best = True if '>' in current_line[0] else False
        network = current_line[1]
        attached_ip = current_line[2]
        print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
        for i in range(0,1000):
            line = bgp_table_list[i+1]
            print i
            subline_line = line.split()
            best_sub = 'True' if '>' in subline_line[0] else 'False'
            if len(subline_line) == 6:
                print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
            elif len(subline_line) == 5:
                print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
            elif len(subline_line) == 7:
                line = bgp_table_list[i+1]
                break

Output:

Found Entry Network:  10.0.1.0/24 Nexthop: 10.8.111.45 Best Path: False Pref: 115
0
Found Network:  10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.0.1.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.0.3.0/29 Nexthop: 10.8.111.45 Best Path: False Pref: 115
0
Found Network:  10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.0.3.0/29 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.8.0.0/16 Nexthop: 10.9.0.1 Best Path: False Pref: 50
0
Found Network:  10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.8.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.9.0.0/16 Nexthop: 10.9.0.1 Best Path: False Pref: 50
0
Found Network:  10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.9.0.0/16 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.2.0/24 Nexthop: 10.8.10.2 Best Path: True Pref: 100
0
Found Network:  10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.2.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.5.0/24 Nexthop: 10.8.142.15 Best Path: False Pref: 85
0
Found Network:  10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.5.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.7.0/24 Nexthop: 10.8.40.84 Best Path: True Pref: 100
0
Found Network:  10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.7.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.8.0/24 Nexthop: 10.8.10.8 Best Path: True Pref: 100
0
Found Network:  10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.8.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.11.0/24 Nexthop: 10.8.42.8 Best Path: True Pref: 100
0
Found Network:  10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.11.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.12.0/24 Nexthop: 10.8.10.12 Best Path: False Pref: 100
0
Found Network:  10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.12.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.15.0/24 Nexthop: 10.8.10.15 Best Path: False Pref: 100
0
Found Network:  10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.15.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3
Found Entry Network:  10.10.27.0/24 Nexthop: 10.8.41.81 Best Path: False Pref: 100
0
Found Network:  10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: True Pref: 120
1
Found Network:  10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
2
Found Network:  10.10.27.0/24 Nexthop: 10.8.11.45 Best Path: False Pref: 120
3

The index number is going back to 0 each time show here printing out the index of the line being checked.


Solution

  • Instead of forcing it to be an iter, keep using it as a list, then use enumerate to know your location in the list.

    Edit: Chaning first enumerate to use index, the second loop's range to start with 1 instead of 0, and the line check to line = bgp_table_list[index+i] with an except clause for when it reaches end of file.

    for index, line in enumerate(bgp_table_list):
        # print line.split(), len(line.split())
    
        current_line = line.split()
        # If 7 items are in the list it's the start of a network
        if len(current_line) == 7:
            best = True if '>' in current_line[0] else False
            network = current_line[1]
            attached_ip = current_line[2]
            print "Found Starting Network", network, attached_ip, "Best Path: {}".format(best)
            for i in range(1, 1000):
                try:
                    line = bgp_table_list[index+i]
                except IndexError:
                    break
                subline_line = line.split()
                best_sub = 'True' if '>' in subline_line[0] else 'False'
                if len(subline_line) == 6:
                    print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
                elif len(subline_line) == 5:
                    print 'Sub:', subline_line[1], "Best Path: {}".format(best_sub)
                elif len(subline_line) == 7:
                    break
    

    My output (not sure if correct, providing so you can judge)

    Found Starting Network 10.0.1.0/24 10.8.111.45 Best Path: False
    Sub: 10.8.11.45 Best Path: True
    Sub: 10.8.11.45 Best Path: False
    Sub: 10.8.11.45 Best Path: False
    Found Starting Network 10.0.3.0/29 10.8.111.45 Best Path: False
    Sub: 10.8.11.45 Best Path: True
    Sub: 10.8.11.45 Best Path: False
    Sub: 10.8.11.45 Best Path: False
    Found Starting Network 10.8.0.0/16 10.9.0.1 Best Path: False
    Sub: 10.8.0.1 Best Path: False
    Sub: 0.0.0.0 Best Path: True
    Found Starting Network 10.9.0.0/16 10.9.0.1 Best Path: False
    Sub: 10.8.0.1 Best Path: False
    Sub: 0.0.0.0 Best Path: True
    Found Starting Network 10.10.2.0/24 10.8.10.2 Best Path: True
    Sub: 10.8.10.2 Best Path: False
    Sub: 10.8.10.2 Best Path: False
    Found Starting Network 10.10.5.0/24 10.8.142.15 Best Path: False
    Sub: 10.8.42.15 Best Path: True
    Sub: 10.8.42.15 Best Path: False
    Sub: 10.8.42.15 Best Path: False
    Found Starting Network 10.10.7.0/24 10.8.40.84 Best Path: True
    Sub: 10.8.40.84 Best Path: False
    Sub: 10.8.40.84 Best Path: False
    Found Starting Network 10.10.8.0/24 10.8.10.8 Best Path: True
    Sub: 10.8.110.8 Best Path: False
    Sub: 10.8.10.8 Best Path: False
    Sub: 10.8.10.8 Best Path: False
    Found Starting Network 10.10.11.0/24 10.8.42.8 Best Path: True
    Sub: 10.8.42.8 Best Path: False
    Sub: 10.8.42.8 Best Path: False
    Sub: 10.9.42.8 Best Path: False
    Found Starting Network 10.10.12.0/24 10.8.10.12 Best Path: False
    Sub: 10.8.10.12 Best Path: False
    Sub: 10.8.10.12 Best Path: True