943,978 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 930
  • Python RSS
Jun 28th, 2008
0

Append Results to ListBox

Expand Post »
Hi,

I have 2 modules, grepforapp.py and myApp.py. Basically i'm trying to make a 'search' utility. myApp makes the UI with one textbox for taking input, start button and a list box which displays the file names where the pattern was found.
grepforApp walks through the dir tree and searches the pattern in each file. In my previous code i used to read and store everything and then would display the results in one shot. Then i changed the code to call the grepforapp.search in a loop and try to display the results as they come for each dir. but this is not happening. If you see the code i'm doing a ListBox.Append in a loop but still the final result is shown in one shot and not in steps. Can anyone see what's wrong with the code?

myApp module
python Syntax (Toggle Plain Text)
  1. import wx
  2. import grepforapp
  3. import os
  4. import pdb
  5. import time
  6.  
  7. #pdb.set_trace()
  8.  
  9. class myApp(wx.Frame):
  10. def __init__(self,parent,id,title):
  11. wx.Frame.__init__(self,parent,id,title)
  12. self.tc1 = None
  13. self.Show()
  14.  
  15. def addButton(self,ID,butStr,coord):
  16. wx.Button(self,ID,butStr,coord)
  17. self.Bind(wx.EVT_BUTTON,self.addStaticBox,id=ID)
  18.  
  19. def addStaticBox(self,event):
  20. superDict = {}
  21. tc2 = None
  22. tc2 = wx.ListBox(self, 26, (5,80), (350, 150),style=wx.LB_SINGLE)
  23. tc2.SetForegroundColour('#RRGGBB')
  24. pattern = self.tc1.GetValue()
  25. superDict = {}
  26. if pattern != '':
  27. for roots,dirs,files in os.walk(os.getcwd()):
  28. myFileList = []
  29. fileList = []
  30. superDict.clear()
  31. myFileList = self.getFileList(files)
  32. superDict = grepforapp.searchPattern(pattern,1,myFileList,roots)
  33. # for key in superDict.keys():
  34. # print key
  35. fileList = superDict.keys()
  36. tc2.AppendItems(fileList)
  37. time.sleep(10)
  38.  
  39. def getFileList(self,files):
  40. # print roots,dirs,files
  41. i = 0
  42. i = len(files)
  43. searchAll = 1
  44. # print i
  45. myFileList = []
  46. for j in range(i):
  47. filename,fileext = os.path.splitext(files[j])
  48. # print fileext
  49. if(searchAll):
  50. myFileList.append(files[j])
  51. else:
  52. if fileext == '.cpp' or fileext == '.h' or fileext == '.hpp' or fileext == '.py':
  53. myFileList.append(files[j])
  54. return myFileList
  55. # print myFileList
  56.  
  57. def addTextCtrl(self):
  58. self.tc1 = wx.TextCtrl(self,pos=(5,5),size=(200,30))
  59.  
  60. def main():
  61. App = wx.App()
  62. app = myApp(None,-1,'Search')
  63. #app.addMenu()
  64. app.addTextCtrl()
  65. app.addButton(1,'Stop',(100,40))
  66. app.addButton(2,'Search',(5,40))
  67. #app.addStaticBox()
  68. App.MainLoop()
  69.  
  70. if __name__ == "__main__":
  71. main()

pgrepforapp code
python Syntax (Toggle Plain Text)
  1. import os,re,sys
  2. import glob,getopt
  3. import pdb
  4.  
  5. #pdb.set_trace()
  6.  
  7. def searchPattern(pattern,searchAll,myFileList,roots):
  8. if pattern == '':
  9. usage()
  10. valueList = []
  11. superDict = {}
  12. superDict.clear()
  13. print '---------------Results---------------\n'
  14. for fil in myFileList:
  15. # print fil
  16. try:
  17. valueList = []
  18. matchFound = 0
  19. fullpath = roots + '/' + fil
  20. # print fullpath
  21. fileObj = open(fullpath)
  22. lines = fileObj.readlines()
  23. count = 0
  24. for count,line in enumerate(lines):
  25. if (re.search(pattern.lower(),line.lower())):
  26. matchFound = 1
  27. # print fullpath
  28. matchline = 'line#'+ str(count+1),line
  29. # #print matchline
  30. valueList.append(matchline)
  31. if matchFound:#
  32. superDict[fullpath] = valueList
  33. # print superDict.keys()
  34. except IOError:
  35. pass
  36. except KeyboardInterrupt:
  37. print 'so u got bored.. me too.. ok bye..'
  38. sys.exit(2)
  39. # print superDict.keys()
  40. return superDict
  41.  
  42. def usage():
  43. print 'invalid option'
  44. print 'usage: pgrep [-a] -p pattern'
  45. print '-a: search in all files'
  46. sys.exit()
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Jun 28th, 2008
0

Re: Append Results to ListBox

Take a look at the control variables section at the following link. A control variable can be linked to several widgets and will update the widget automatically. http://infohost.nmt.edu/tcc/help/pubs/tkinter/
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Question on List
Next Thread in Python Forum Timeline: Socket Server for game - too many threads?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC