zengleo 发表于 2013-1-15 02:55:10

python的音乐本地搜索统计的小工具(速度优化版)

加入了stackless微线程,速度大大的提高!!#coding=gb2312import osimport fnmatchimport wximport reimport timeimport stacklessdef get_allfile_name(path):   #获得全部文件夹的文件名    l=[]    for p,d,f in os.walk(path):      for i in f:            l.append(os.path.join(p,i))    return lclass Search_music(wx.Frame):    def __init__(self):      wx.Frame.__init__(self,None,-1,"音乐查找 v2.0",size=(300,330),style=wx.DEFAULT_FRAME_STYLE^wx.RESIZE_BORDER)      panel=wx.Panel(self,-1)      self.mymusicList=['mp3','wav','wma','cue']    #------------------------------------------------------------------------------ 文件夹      self.st1=wx.StaticText(panel,-1,"给我一个路径 ,我将翻透你的硬盘 :",(15,15))      self.st1_pos_y=self.st1.GetPosition().y+self.st1.GetSize().height      self.t1=wx.TextCtrl(panel,-1,"",style=0,size=(185,25),pos=(15,self.st1_pos_y+10))      self.t1_pos_x=self.t1.GetPosition().x+self.t1.GetSize().width+10      self.b1=wx.Button(panel,-1,"浏览",pos=(self.t1_pos_x,self.st1_pos_y+10),size=(45,25),style=0)      self.Bind(wx.EVT_BUTTON, self.GetDir, self.b1)#------------------------------------------------------------查询数据类型-----------------      self.st2=wx.StaticText(panel,-1,"音乐查询类型 :",(15,self.st1_pos_y+45))      self.st2_pos_y=self.st2.GetPosition().y+self.st2.GetSize().height      self.st2_pos_x=self.st2.GetPosition().y+self.st2.GetSize().width      lb=wx.CheckListBox(panel,-1,(15,self.st2_pos_y+10),wx.DefaultSize,self.mymusicList)      lb.SetSelection(0)      self.lb=lb      pos_y=lb.GetPosition().y+lb.GetSize().height+10      pos_x=lb.GetPosition().x+lb.GetSize().width+15#------------------------------------------------------------------------------ 统计面板      multiLabel=wx.StaticText(panel,-1,"统计结果",(self.st2_pos_x-10,self.st1_pos_y+45))      multiLabel_pos_y=multiLabel.GetPosition().y+multiLabel.GetSize().height+15      self.multiText=wx.TextCtrl(panel,-1,            "",size=(140,190),style=wx.TE_MULTILINE,pos=(self.st2_pos_x-10,self.st2_pos_y+10))#创建一个文本控件      self.multiText.SetInsertionPoint(0)#设置插入点      #------------------------------------------------------------------------------ 进度条      self.gauage=wx.Gauge(panel,-1,100,(15,pos_y+40),(70,20))      self.gauage.SetBezelFace(3)      self.gauage.SetShadowWidth(3)                              #-------------------------------------------------------------------------------总数据发送按钮                btn=wx.Button(panel,-1,'搜索', (15, pos_y+70))      self.Bind(wx.EVT_BUTTON,self.OnTestButton,btn)                  def OnTestButton(self,event):      dict={}      all_word=[]#所有要显示在面板上的      all_songs_num=0   #歌曲总数      mycue_number=0                                        for i in self.lb.GetCheckedStrings():#做一个字典{文件类型:[文件路径名]}            dict=[]      if self.t1.GetValue()=='' or self.lb.GetCheckedStrings()==():            wx.MessageBox('靓仔!信息不完整哦!','出错了',wx.ICON_EXCLAMATION)            s=''      elif not os.path.isdir(self.t1.GetValue()):            wx.MessageBox('靓仔!路径不合法!','出错了',wx.ICON_EXCLAMATION)            s=''      else:            self.gauage.SetValue(10) #标记进度条                              All_files=get_allfile_name(self.t1.GetValue())            def my_stackless_task(my_str):                for f in All_files:                  if fnmatch.fnmatch(f,'*.%s'%my_str):                        dict.append(f)                                       self.gauage.SetValue(50) #标记进度条         # if 'ape' in self.lb.GetCheckedStrings():                        for i in self.lb.GetCheckedStrings():                stackless.tasklet(my_stackless_task)(i)            stackless.run()                        self.gauage.SetValue(90)                              for i in self.lb.GetCheckedStrings():                #print i,len(dict)                if i!='cue':                  all_songs_num=all_songs_num+len(dict)                  all_word.append('%s有:%s \n'%(i,len(dict)))                else:                  the_cue_num=len(dict)                            if 'cue' in self.lb.GetCheckedStrings() and len(dict['cue'])!=0:                #print dict['cue']               for mycuefile in dict['cue']:                  #print mycuefile                  mycue_number=mycue_number+self.find_audionumber_in_ape(mycuefile)                all_word.append('ape内曲目:%d'%(mycue_number))                           all_word.insert(0, '歌曲【%s】总数(含ape内音频):%d\n'%(str(self.lb.GetCheckedStrings()),(all_songs_num+mycue_number)))                        s="\n".join(all_word)            self.gauage.SetValue(100)#标记进度条                      self.multiText.SetValue(s)            self.gauage.SetValue(0)#标记进度条               def find_audionumber_in_ape(self,cuefile):#对所有的cue文件进行操作      try:            #print cuefile            myfile=open(r"%s"%cuefile,'r')            all_str=myfile.readlines()            myfile.close()            all_str.reverse()            for n in all_str:                if re.search("^TRACK",n):                  break            p=re.compile('..')            return int(p.search(n).group())                  except:            errorfile=open('music_error.log','a')            errorfile.write('\n 时间:%s    文件【%s】找不到了哇\n'%(time.ctime(time.time()),cuefile))            errorfile.close()             return 0                                       def GetDir(self,event):      self.t1.SetValue(wx.DirSelector("我的音乐目录",defaultPath="",style=0,pos=wx.DefaultPosition,parent=None))if __name__=='__main__':    app=wx.PySimpleApp()    frame=Search_music()    frame.Show()    app.MainLoop()
页: [1]
查看完整版本: python的音乐本地搜索统计的小工具(速度优化版)