""" a script for plotting recorded values """ import Gnuplot from Numeric import * from threading import * from time import * class pyRecorder: def __init__(self): """ class pyRecorder for plotting recorded value """ self.measures=[] self.datas=[] self.mean=[] self.gnuplotCommand="" self.g=Gnuplot.Gnuplot() self.g("set xdata time") self.g("set timefmt \"%H:%M:%S\"") self.g("set format x \"%H:%M:%S\"") self.g(" set style data linespoints") #self.filename=filename #self.many=many def __calculateGnuplotCommand(self): self.gnuplotCommand="plot " for i in range(len(self.measures)): self.gnuplotCommand+="\'"+self.filename+"\'" self.gnuplotCommand+=" using 1:"+str(i+2) self.gnuplotCommand+=" title "+str(i+2) self.gnuplotCommand+="," self.gnuplotCommand=self.gnuplotCommand[:len(self.gnuplotCommand)-1]#remove the last , def addMeasure(self,name,measure,*args): """ add a measure (function) to be recorded """ self.measures.append([name,measure,args]) def __measure(self,n): #launch the measures and save the data in position n chaine=str(localtime()[3])+":"+str(localtime()[4])+":"+str(localtime()[5])+"\t" for i in range(len(self.measures)): m=self.measures[i] res=m[1](*m[2]) if self.many>0: self.data[i,n]=res if n>0: self.mean[i]=sum(self.data[i,0:n+1])/n chaine+=str(res)+"\t" #enregistre output=open(self.filename,"a") output.write(chaine+"\n") output.close() #plot self.g(self.gnuplotCommand) def start(self,filename="tempo.txt",many=0,scheduler=1): """ launch the recording filename : the filenama where the data are saved many : recording number (if 0 then no limi, stop with ctrl+C) scheduler : time in seconds between each measures (if 0 then no waiting time) """ self.filename=filename self.many=many self.__calculateGnuplotCommand() output=open(self.filename,"w") header="time" for i in range(len(self.measures)): header+="\t"+self.measures[i][0] output.write(header+"\n") output.close() counter=0 if self.many==0: test=True else: test=(counter0: return self.data else: return if __name__=='__main__': import time import random def r(x,y): return x*5/2*6.335*y*random.random() def x2(): return random.random() def x3(x): return random.random()+x def waitstop(): 2*2 return t=pyRecorder() t.addMeasure("rand",x2) t.addMeasure("rand2",x3,10) t.start(many=100,scheduler=1)