I am trying to change an octet based on the mask I have.
Lets say I have IP: 194.216.35.54 and mask- 27, I need to change the 4th octet (3 when counted from 0). I am trying to change the values in the work_octets
variable. But, somehow, the values in the network_octets
are also changing. Why is this happening? both network_octets
and work_octets
are two different lists created by looping over the values and slicing the ip_dict_list
. How is it that network_octets
getting changed- can any one please help me understand what am I doing wrong here?
Note: I can deepcopy and change this behavior, but I need to know what is it that I am doing wrong here. Any explanation in this regard is highly appreciated.
Code:
octet = 3
ip_dict_list = dc(self.ip_dict_list) # looks like this: [{1: {128: 0, 64: 0, 2: 0, 4: 0, 1: 0, 16: 0, 32: 0, 8: 0}}, {2: {128: 0, 64: 0, 2: 0, 4: 0, 1: 0, 16: 0, 32: 0, 8: 0}}, {3: {128: 0, 64: 0, 2: 0, 4: 0, 1: 0, 16: 0, 32: 0, 8: 0}}, {4: {128: 0, 64: 0, 2: 0, 4: 0, 1: 0, 16: 0, 32: 0, 8: 0}}]
vals = [v for i in ip_dict_list for k, v in i.items()]
network_octets = vals[:octet]
work_octets = vals[octet:]
ip_list = ip.split('.')
iof = int(ip_list[octet]) # ip octet in focus (54)
for i in range(len(work_octets)):
for ob in self.bit_placement: # loop over the list [128, 64, 32, 16, 8, 4, 2, 1]
# 32 < 54
if ob <= iof:
print "Iof: {}, Ob: {}".format(iof, ob)
iof = iof - ob # 54-32 = 22; 22-16 = 6; 6-4: 2; 2-2 = 0 (done)
work_octets[i][ob] = 1 # {32: 1, 16: 0, 8: 0, 4: 0, 2: 0, 1:0 }
if iof == 0:
break
Iof: 54, Ob: 32
Iof: 22, Ob: 16
Iof: 6, Ob: 4
Iof: 2, Ob: 2
print work_octets # as expected
[{128: 0, 64: 0, 2: 1, 4: 1, 1: 0, 8: 0, 16: 1, 32: 1}]
Now network_octets also got changed and I expect it to remain the same - not as expected
print network_octets
[{128: 0, 64: 0, 2: 1, 4: 1, 1: 0, 8: 0, 16: 1, 32: 1}, {128: 0, 64: 0, 2: 1, 4: 1, 1: 0, 8: 0, 16: 1, 32: 1}, {128: 0, 64: 0, 2: 1, 4: 1, 1: 0, 8: 0, 16: 1, 32: 1}]
This should be unchanged and should look like this:
network_octets
[{128: 0, 64: 0, 2: 0, 4: 0, 1: 0, 8: 0, 16: 0, 32: 0}, {128: 0, 64: 0, 2: 0, 4: 0, 1: 0, 8: 0, 16: 0, 32: 0}, {128: 0, 64: 0, 2: 0, 4: 0, 1: 0, 8: 0, 16: 0, 32: 0}]
The variable vals is also changing after the for loop:
vals
[{128: 0, 64: 0, 2: 1, 4: 1, 1: 0, 8: 0, 16: 1, 32: 1}, {128: 0, 64: 0, 2: 1, 4: 1, 1: 0, 8: 0, 16: 1, 32: 1}, {128: 0, 64: 0, 2: 1, 4: 1, 1: 0, 8: 0, 16: 1, 32: 1}, {128: 0, 64: 0, 2: 1, 4: 1, 1: 0, 8: 0, 16: 1, 32: 1}]
I expect only the work_octets to have the changes in the values of the dict element and the network_octets and vals should remain same. But all the variables are getting modified after the for loop