Python Forum
RuntimeError: dictionary changed size during iteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RuntimeError: dictionary changed size during iteration
#1
Hi All,

parsing configuration, and updating dict, in second loop I am parsing network routes, post matching, updating dict. But facing error, is this code correct?

Error:
Traceback (most recent call last): File "ipstaticroutes-1.py", line 57, in <module> for value in customer_details.items(): RuntimeError: dictionary changed size during iteration
from ciscoconfparse import CiscoConfParse
from ciscoconfparse.ccp_util import IPv4Obj
from mysql.connector import Error
from mysql.connector import errorcode
import os
import re
from netaddr import *
import pprint
import mysql.connector
from ipaddress import *
if __name__ == "__main__":
    global customer_details
    customer_details = {}
    #mydb = mysql.connector.connect(host="localhost",
    #                         user='root',
    #                         password='Compaqnx6320',
    #                         database='illcustomers')
    #mycursor = mydb.cursor()

    # Reading all configuration files
    conf_files_list = [x for x in os.listdir('.') if x.endswith('.conf')]
    for files in conf_files_list:
        confparse = CiscoConfParse(files)
        # get the system name
        system_regex_pattern = r"^sysname"
        hostname = confparse.find_lines(system_regex_pattern)
        for line in hostname:
            sysname = line.strip().split(' ')[1]
            #Start checking ip route-static block
            ip_static_routes= confparse.find_blocks(r"^ip route-static ")
            for lines in ip_static_routes:
                #Find ip routes
                routes = re.findall( r'[0-9]+(?:\.[0-9]+){3}', lines )
                #list of ip route should be 3, Null0 routes are not considered
                if (len(routes)) == 3:
                       route = (' '.join(routes))
                       lan_network = route.split(' ')
                       lan_ip_pool = lan_network[0]+"/"+lan_network[1]
                       #converted to subnet form
                       ip_add = IPNetwork(lan_ip_pool)
                       ip_address = str(ip_add)
                       next_hop = lan_network[2]
                       # required next  hop
                       next_hop_int_ip = str(IPv4Address(next_hop)- 1)
                       customer_details.update({'sysname':sysname, 'ip_address': ip_address,'next_hop': next_hop, 'next_hop_int_ip':next_hop_int_ip})
            #Checking for BGP network routes
            ip_bgp_pools = confparse.find_blocks(r"network")
            for bgp_subnets in ip_bgp_pools:
                bgp_subnet = re.findall( r'[0-9]+(?:\.[0-9]+){3}',bgp_subnets )
                if len(bgp_subnet) == 2:
                   bgp_network = (' '.join(bgp_subnet))
                   bgp_network = bgp_network.split(' ')
                   bgp_anouced_subnet = bgp_network[0]+"/"+bgp_network[1]
                   #Converted to subnet form
                   bgp_pool = IPNetwork(bgp_anouced_subnet)
                   #Checking BGP ip pools in Dict by comparing dict vlaue
                   for value in customer_details.items():
                       if (customer_details['ip_address']) == bgp_pool:
                          # updating dict if mact
                          customer_details.update({'bgp_status':bgp_pool})
                       else:
                          customer_details.update({'bgp_status': "no_bgp"})
            print(customer_details)
Reply
#2
You have to change the code beginning with line 57:

                   for value in customer_details.items():
                       if (customer_details['ip_address']) == bgp_pool:
                          # updating dict if mact
                          customer_details.update({'bgp_status':bgp_pool})
                       else:
                          customer_details.update({'bgp_status': "no_bgp"})
to

                   if customer_details['ip_address'] == bgp_pool:
                      # updating dict if mact
                      customer_details.update({'bgp_status': bgp_pool})
                   else:
                      customer_details.update({'bgp_status': 'no_bgp'})
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
changed to

for bgp_subnets in ip_bgp_pools:
                bgp_subnet = re.findall( r'[0-9]+(?:\.[0-9]+){3}',bgp_subnets )
                if len(bgp_subnet) == 2:
                   bgp_network = (' '.join(bgp_subnet))
                   bgp_network = bgp_network.split(' ')
                   bgp_annouced_subnet = bgp_network[0]+"/"+bgp_network[1]
                   #Converted to subnet form
                   bgp_pool = IPNetwork(bgp_annouced_subnet)
                   print(bgp_pool)
                   #Checking BGP ip pools in Dict by comparing dict vlaue
                   for value in customer_details.items():
                       if customer_details['ip_address'] == bgp_pool:
                          # updating dict if match
                          customer_details.update({'bgp_status': bgp_pool})
                       else:
                          customer_details.update({'bgp_status': 'no_bgp'})
but same error
Error:
Traceback (most recent call last): File "ipstaticroutes-1.py", line 58, in <module> for value in customer_details.items(): RuntimeError: dictionary changed size during iteration
Reply
#4
in python 3.6, resolved this issue, as below

for value in list(customer_details.items()):
Reply
#5
some weired results

from ciscoconfparse import CiscoConfParse
from ciscoconfparse.ccp_util import IPv4Obj
from mysql.connector import Error
from mysql.connector import errorcode
import os
import re
from netaddr import *
import pprint
import mysql.connector
from ipaddress import *
if __name__ == "__main__":
    #global customer_details
    customer_details = {}
    conf_files_list = [x for x in os.listdir('.') if x.endswith('.conf')]
    for files in conf_files_list:
        confparse = CiscoConfParse(files)
        # get the system name
        system_regex_pattern = r"^sysname"
        hostname = confparse.find_lines(system_regex_pattern)
        for line in hostname:
            sysname = line.strip().split(' ')[1]
            #Start checking ip route-static block
            ip_static_routes= confparse.find_blocks(r"^ip route-static ")
            for lines in ip_static_routes:
                #Find ip routes
                routes = re.findall( r'[0-9]+(?:\.[0-9]+){3}', lines )
                #list of ip route should be 3, Null0 routes are not considered
                if (len(routes)) == 3:
                       route = (' '.join(routes))
                       lan_network = route.split(' ')
                       lan_ip_pool = lan_network[0]+"/"+lan_network[1]
                       #converted to subnet form
                       ip_add = IPNetwork(lan_ip_pool)
                       ip_address = str(ip_add)
                       #print(ip_address)
                       next_hop = lan_network[2]
                       # required next  hop
                       next_hop_int_ip = str(IPv4Address(next_hop)- 1)
                       customer_details.update({'sysname':sysname, 'ip_address': ip_address,'next_hop': next_hop, 'next_hop_int_ip':next_hop_int_ip})
                       print(customer_details)
            ip_bgp_pools = confparse.find_blocks(r"network")
            for bgp_subnets in ip_bgp_pools:
                bgp_subnet = re.findall( r'[0-9]+(?:\.[0-9]+){3}',bgp_subnets )
                   #print(bgp_subnet)
                   if len(bgp_subnet) == 2:
                      bgp_network = (' '.join(bgp_subnet))
                      bgp_network = bgp_network.split(' ')
                      bgp_annouced_subnet = bgp_network[0]+"/"+bgp_network[1]
                      #Converted to subnet form
                      bgp_pool = IPNetwork(bgp_annouced_subnet)
                      bgp_pool = str(bgp_pool)
                      #Checking BGP ip pools in Dict by comparing dict vlaue
                      print(customer_details['ip_address'])
                      for value in list(customer_details.items()):
                          if customer_details['ip_address'] == bgp_pool:
                             customer_details.update({'bgp_status': bgp_pool})
                          else:
                             customer_details.update({'bgp_status': 'No'})
                      print(customer_details)
Dict printing, no issues

Output:
{'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.8/29', 'next_hop': '49.249.251.254', 'next_hop_int_ip': '49.249.251.253'} {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.40/29', 'next_hop': '182.156.235.250', 'next_hop_int_ip': '182.156.235.249'} {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.56/29', 'next_hop': '182.156.235.18', 'next_hop_int_ip': '182.156.235.17'} {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.88/29', 'next_hop': '49.249.251.222', 'next_hop_int_ip': '49.249.251.221'} {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.96/29', 'next_hop': '49.249.233.130', 'next_hop_int_ip': '49.249.233.129'} {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.104/29', 'next_hop': '61.12.65.150', 'next_hop_int_ip': '61.12.65.149'} {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.112/29', 'next_hop': '49.249.251.194', 'next_hop_int_ip': '49.249.251.193'} {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.128/29', 'next_hop': '49.249.251.238', 'next_hop_int_ip': '49.249.251.237'} {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.136/29', 'next_hop': '111.93.132.58', 'next_hop_int_ip': '111.93.132.57'} {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49'}
updating Dict and printing, 'ip_address' is same till loop completes

Output:
14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': '14.98.0.192/29'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'} 14.98.0.192/29 {'sysname': 'BAN:YES-NE40E-X8-IAG-B5', 'ip_address': '14.98.0.192/29', 'next_hop': '111.93.151.50', 'next_hop_int_ip': '111.93.151.49', 'bgp_status': 'No'}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help RuntimeError: no running event loop marpaslight 5 3,754 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  The behavior of tune model has changed Led_Zeppelin 5 4,460 Oct-21-2021, 06:52 PM
Last Post: jefsummers
  bleak library RuntimeError: This event loop is already running alice93 3 4,117 Sep-30-2021, 08:06 AM
Last Post: alice93
  how can a variable change if I haven't changed it? niminim 5 3,082 Apr-07-2021, 06:57 PM
Last Post: niminim
  RuntimeError: generator raised StopIteration quest 1 5,824 Mar-28-2021, 08:11 PM
Last Post: quest
  new help with dictionary and dataframe iteration AlphFinan 0 1,534 Oct-13-2020, 11:04 PM
Last Post: AlphFinan
  RuntimeError: This event loop is already running newbie2019 2 6,963 Sep-30-2020, 06:59 PM
Last Post: forest44
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 6,157 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  Dictionary iteration and creation a new dictionary from duplicates xrsxlnx 2 2,142 Mar-30-2020, 10:43 AM
Last Post: xrsxlnx
  size of set vs size of dict zweb 0 2,152 Oct-11-2019, 01:32 AM
Last Post: zweb

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020