Hick
Hick

Reputation: 36414

str is not callable error in python

import sys
import md5
from TOSSIM import *
from RadioCountMsg import *

t = Tossim([]) #The Tossim object is defined here 

m = t.mac()#The mac layer is defined here , in which the communication takes place
r = t.radio()#The radio communication link object is defined here , as the communication needs Rf frequency to transfer

t.addChannel("RadioCountToLedsC", sys.stdout)# The various channels through which communication will take place 
t.addChannel("LedsC", sys.stdout)

#The no of nodes that would be required in the simulation has to be entered here
print("enter the no of nodes you want ")
n=input()

for i in range(0, n):
  m = t.getNode(i)
  m.bootAtTime((31 + t.ticksPerSecond() / 10) * i + 1)
#The booting time is defined so that the time at which the node would  be booted is given

f = open("topo.txt", "r") #The topography is defined in topo.txt  so that the RF frequencies of the transmission between nodes are are set 
lines = f.readlines()
for line in lines:
  s = line.split()
  if (len(s) > 0):
    if (s[0] == "gain"):
      r.add(int(s[1]), int(s[2]), float(s[3])) #The topogrography is added to the radio object


noise = open("meyer-heavy.txt", "r") #The noise model is defined for the nodes 
lines = noise.readlines()
for line in lines:
  str = line.strip()
  if (str != ""):
    val = int(str)
    for i in range(0, 4):
      t.getNode(i).addNoiseTraceReading(val)

for i in range (0, n): 
  t.getNode(i).createNoiseModel() #The noise model is created for each node

for i in range(0,n):
  t.runNextEvent()
fk=open("key.txt","w")
for i in range(0,n):
    if i ==0 :
        key=raw_input()
        fk.write(key)
        ak=key
    key=md5.new()
    key.update(str(ak))
    ak=key.digest()
    fk.write(ak)
fk.close()
fk=open("key.txt","w")
plaint=open("pt.txt")
for i in range(0,n):
    msg = RadioCountMsg()
    msg.set_counter(7)
    pkt = t.newPacket()#A packet is defined according to a certain format 
    print("enter message to be transported") 
    ms=raw_input()#The message to be transported is taken as input
    #The RC5 encryption has to be done here 
    plaint.write(ms)
    pkt.setData(msg.data) 
    pkt.setType(msg.get_amType())
    pkt.setDestination(i+1)#The destination to which the packet will be sent is set 

    print "Delivering " + " to" ,i+1  
    pkt.deliver(i+1, t.time() + 3)


fk.close()

print "the key to be displayed"
ki=raw_input()
fk=open("key.txt")

for i in range(0,n):
    if i==ki:
        ms=fk.readline()



for i in range(0,n):
    msg=RadioCountMsg()
    msg.set_counter(7)
    pkt=t.newPacket()
    msg.data=ms
    pkt.setData(msg.data)
    pkt.setType(msg.get_amType())
    pkt.setDestination(i+1)
    pkt.deliver(i+1,t.time()+3)

#The key has to be broadcasted here so that the decryption can take place 

for i in range(0, n):
  t.runNextEvent();

this code gives me error here key.update(str(ak)) . when i run a similar code on the python terminal there is no such error but this code pops up an error . why so?

Upvotes: 0

Views: 2035

Answers (2)

Josh Wright
Josh Wright

Reputation: 2505

On line 35 you reassign 'str' from the built-in it originally references to a different object. Then, on line 53, you try to use it as the original built-in again. If you want to use 'str' as 'str()' on line 53, you need to use a different variable name up on line 35 (and 36, and 37)

Don't use 'str' here:

for line in lines:
  str = line.strip()
  if (str != ""):
    for i in range(0, 4):
      t.getNode(i).addNoiseTraceReading(val)

Upvotes: 3

Fred Larson
Fred Larson

Reputation: 62113

Don't use the name str for variables. It's a the name of a type in Python.

Upvotes: 2

Related Questions