Feb-24-2018, 05:02 PM
Hi, I'm trying to call a function defined out of exec() within the exec(), but I'm getting an error saying that the fuction I'm trying to run is not found. It works if I do something like this:
def a():
print("hi")
txt = "a()"
exec(txt)but In my case it is something more like this i'm simplifying cause my code is almost 500 line:class b():
def __init__(self):
self.button()
def button(self):
def a():
print("hi")
txt = "a()\n print("example")"
exec(txt) here is what this portion of my code looks like:def BRun(self, e):
def Pt(self, x, y):
plt.scatter(x, y)
try:
canvas.draw()
except NameError:
pass
self._mgr.Update()
file = open(File)
exec(file.read())and here is the file that is being opened:##-- temp --##
import matplotlib.pyplot as plt
import random
##-- end --##
# DataX is just for graphing purposes
#DATAX = []
# Raw data to be read
#DATAY = [1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
# Running average graphing
AvgY = []
AvgY2 = []
AvgX = []
##----------------------------------------#
#######-- FPGA Memory Buffer --############
Mem0Width = 5
Mem0 = []
Mem1 = []
Mem2 = []
sum0 = 0
sum1 = 0
sum2 = 0
## raise detectiom
rise0 = False
#rise1 = False
comp0 = False
comp1 = False
comp2 = False
##----------------------------------------#
#######-- Simulate fpga run time --#######
##----------------------------------------#
def runtime():
global Mem0Width
global Mem0
global Mem1
global Mem2
global AvgX
global AvgY
global AvgY2
# peak det
global rise0
#global rise1
global comp0
global comp1
global comp2
global sum0
global sum1
global sum2
for i in range(0, len(DATAY), Mem0Width):
Mem2 = Mem1
Mem1 = Mem0
if not i <= Mem0Width or not len(DATAY)-Mem0Width:
Mem0 = DATAY[i-Mem0Width:i]
if i <= Mem0Width:
Mem0 = []
for z in range(Mem0Width-i):
Mem0.append(DATAY[z])
for z in range(i):
Mem0.append(DATAY[z])
if i >= len(DATAY)-Mem0Width:
Mem0 = []
for z in range(-i+len(DATAY)):
Mem0.append(DATAY[i])
for z in range(+i-len(DATAY)+Mem0Width):
Mem0.append(DATAY[z])
##----------------------------------------#
########-- add runtime code here --########
##----------------------------------------#
######-- code to be applied in FPGA--######
##----------------------------------------#
## running average
Sum = 0
for z in range(len(Mem0)):
Sum += Mem0[z]
##-- memory -- ##
sum2 = sum1
sum1 = sum0
sum0 = Sum
##-- peak det --##
comp2 = comp1
comp1 = comp0
if sum0 > sum1:
comp0 = True
if sum0 <= sum1:
comp0 = False
## rise detect
if comp0 and comp1 and comp2:
rise0 = True
## when rise starts to fall log max value
if rise0 is True and comp0 is False and comp1 is True and comp2 is True:
peak = 0
for q in range(len(Mem1)):
if Mem1[q] > peak:
peak = Mem1[q]
print(peak)
Pt(i, peak)
if comp0 is False:
rise0 = False
## for graphing only
AvgY.append(Sum)
AvgY2.append(Sum/Mem0Width)
##----------------------------------------#
#######-- Generate noise & x axis --#######
################-- Test --#################
##----------------------------------------#
def xGen():
for i in range(len(DATAY)):
DATAX.append(i)
DATAY[i] += random.random()
#xGen()
runtime()
#plt.plot(DATAX, DATAY, color = 'red') # data
#plt.plot(DATAX, AvgY, color = 'blue') #sum
#plt.plot(DATAX, AvgY2, color = 'blue')
#plt.show()if I leave out Pt(i, peak) from the code it runs, but with it I get the following error message:Error:Traceback (most recent call last):
File "C:\Users\Daniel\eclipse-workspace\program\src\Prog.py", line 301, in BRun
exec(file.read())
File "<string>", line 121, in <module>
File "<string>", line 102, in runtime
NameError: name 'Pt' is not defined
