I have 2 functions contained in 2 separate files.
The functions are similar and use the same names for local variables within them. The argument, 'eachtickerlist', that is passed into each function is also the same. eachtickerlist is a list of lists that look like this
[[ticker,price,year,month,day,seconds from epoch],[ticker,price,year.........
For some reason, the Local variable within each function 'amlist' is holding onto the data from function 1 when it is executing function 2 even though it is re-initialized in the second function and even though it is a local variable. I know this because when I print out 'amlist' at the end of function 2 in order to test it, the strings 'undefined', 'up', or 'down' are listed 2 times. If I don't call function 1 in the main program, this does not happen which proves that what is happening in function 1 is affecting function 2...which I totally don't understand.
In my main program I call each function as shown below:
eachtickerlist=sorted(eachtickerlist,key=operator.itemgetter(6)) #This is the argument to be passed
upsvsdownsresult=upsvsdowns.upsvsdowns(eachtickerlist) #This sends the argument to the first function and stores the return value for use later
swingsresult=swings.swings(eachtickerlist) #This sends the same argument to the second function and stores the return value for use later
Here is function 1:
def upsvsdowns(eachtickerlist):
amlist=[]
for thing in eachtickerlist:
if (thing[5]=='8'):
amlist.append(thing)
else:
pass
try:
amlist[0].append('undefined')
length=len(amlist)
for x in range(1,length):
price=float(amlist[x][1])
yesterdaysprice=float(amlist[(x-1)][1])
if ((price-yesterdaysprice)>0):
amlist[x].append('up')
else:
amlist[x].append('down')
upcount=0
totalcount=0
for y in amlist:
if (y[7]=='up'):
upcount=upcount+1
totalcount=totalcount+1
else:
totalcount=totalcount+1
percentage=round(float(upcount)/float(totalcount),3)
returnlist=[amlist[0][0],str(percentage)]
return (returnlist)
except (IndexError):
returnlist=[eachtickerlist[0][0],'No Data']
return (return list)
Here is function 2:
def swings(eachtickerlist):
amlist=[]
for thing in eachtickerlist:
if (thing[5]=='8'):
amlist.append(thing)
else:
pass
try:
amlist[0].append('undefined')
length=len(amlist)
for x in range(1,length):
price=float(amlist[x][1])
yesterdaysprice=float(amlist[(x-1)][1])
if ((price-yesterdaysprice)>0):
amlist[x].append('up')
else:
amlist[x].append('down')
upcount=0
downcount=0
ups=[]
downs=[]
print amlist
for y in amlist:
if (y[7]=='up'):
if (downcount!=0):
downs.append(downcount)
else:
pass
downcount=0
upcount=upcount+1
elif (y[7]=='down'):
if (upcount!=0):
ups.append(upcount)
else:
pass
upcount=0
downcount=downcount+1
if (upcount!=0):
ups.append(upcount)
elif (downcount!=0):
downs.append(downcount)
else:
pass
#print ups
#print downs
try:
averageup=round(sum(ups)/float(len(ups)),3)
except(ZeroDivisionError):
averageup=round(0.0,3)
try:
averagedown=round(sum(downs)/float(len(downs)),3)
except(ZeroDivisionError):
averagedown=round(0.0,3)
returnlist=[amlist[0][0],str(averageup),str(averagedown)]
return (returnlist)
except (IndexError):
returnlist=[eachtickerlist[0][0],'No Data']
return (return list)
Here is the output from the print statement in the second function. Notice the 2 undefined's, up's and down's in each list.
['AAIT', '35.09', '2014', '7', '28', '8', '2409480.0', 'undefined', 'undefined'], ['AAIT', '35.21', '2014', '7', '29', '8', '2494662.0', 'up', 'up'], ['AAIT', '40', '2014', '7', '29', '8', '2494662.5', 'up', 'up'], ['AAIT', '42.3', '2014', '7', '29', '8', '2494663.0', 'up', 'up']]
any help would be appreciated.
-Brandon