RuntimeError: dictionary changed size during iteration - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: RuntimeError: dictionary changed size during iteration (/thread-16229.html) |
RuntimeError: dictionary changed size during iteration - anna - Feb-19-2019 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?
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) RE: RuntimeError: dictionary changed size during iteration - DeaD_EyE - Feb-19-2019 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'}) RE: RuntimeError: dictionary changed size during iteration - anna - Feb-19-2019 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
RE: RuntimeError: dictionary changed size during iteration - anna - Feb-20-2019 in python 3.6, resolved this issue, as below for value in list(customer_details.items()): RE: RuntimeError: dictionary changed size during iteration - anna - Feb-20-2019 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 updating Dict and printing, 'ip_address' is same till loop completes
|