Just my 5 cents: 1) list comprehensions if possible (and, for bigger dict, from list to dict after) 2) defining patterns outside the repeated method 3) profiling all 4) editor is a Class? look (profiling) into it methods. 5) better of all: multithreading the tagger routine. Many on line for thats.
Bye and sorry for my poor english Alex 2014-08-26 12:00 GMT+02:00 <tkinter-discuss-requ...@python.org>: > Send Tkinter-discuss mailing list submissions to > tkinter-discuss@python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tkinter-discuss > or, via email, send a message with subject or body 'help' to > tkinter-discuss-requ...@python.org > > You can reach the person managing the list at > tkinter-discuss-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tkinter-discuss digest..." > > > Today's Topics: > > 1. Text() highlight (Vasilis Vlachoudis) > 2. Re: Text() highlight (Michael Lange) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Mon, 25 Aug 2014 14:15:59 +0000 > From: Vasilis Vlachoudis <vasilis.vlachou...@cern.ch> > To: "tkinter-discuss@python.org" <tkinter-discuss@python.org> > Subject: [Tkinter-discuss] Text() highlight > Message-ID: > <0bc70b5d93e054469872ffd0fe07220ec0c2b...@cernxchg53.cern.ch> > Content-Type: text/plain; charset="iso-8859-1" > > Hi all, > > I am trying to implement a small g-code editor and real time viewer in > tkinter. > I am using the Text() widget as an editor, but I have troubles > in highlighting the text during editing. Its slow when editing big files. > > below is the highlighting routine > > The routine is scanning for all patterns and picks the next one to > highlight > (found dictionary). It assigns a tag and re-scans all the tags that > became invalid (falling within the area of the the just assigned tag. > e.g. a valid command inside a comment) > > Running it on the whole text file on every modification is out of the > question, > it makes the editing very very slow on big files. > > In order to minimize the calls, I do them with a delay of 50ms, using the > after() > command during a period of inactivity and only for the 100lines following > the first one to be displayed. Still is slow and lacks interactivity. > > I would like your suggestions, on what is the best way to do the > highlighting > during the editing of the code? > > Thanks in advance > Vasilis > > > def highlight(self): > self._highAfter = None > # List contains order of match with regular expression > patterns = {"C": (r"\(.*\)", "Blue") , > "X": (r"[xX][+\-]?\d*\.?\d*", "DarkRed" ), > "Y": (r"[yY][+\-]?\d*\.?\d*", "DarkBlue" ), > "Z": (r"[zZ][+\-]?\d*\.?\d*", "DarkGreen"), > > "I": (r"[iI][+\-]?\d*\.?\d*", "Maroon"), > "J": (r"[jJ][+\-]?\d*\.?\d*", "Maroon"), > "K": (r"[kK][+\-]?\d*\.?\d*", "Maroon"), > "R": (r"[rR][+\-]?\d*\.?\d*", "Maroon"), > > "G": (r"[gG]\d+", "Dark Orchid"), > "M": (r"[mM]\d+", "DarkGrey"), > "F": (r"[fF][+\-]?\d*\.?\d*", "Yellow4"), > "P": (r"[pP]\d+", "DarkGrey") } > > for tag in patterns.keys(): > self.editor.tag_delete(tag) > > count = IntVar() > start = self.editor.index("%d.0"%(self._highStart)) > end = self.editor.index("%d.0"%(self._highStart+100)) > > # First search for the first occurance of all patterns > found = {} > for tag,(pat,color) in patterns.items(): > index = self.editor.search(pat, start, end, count=count, > regexp=True) > if index != "": > found[tag] = (index, count.get()) > #print "Found:", tag, index, count.get() > > # Main loop > while True: > # Find the top-most pattern to highlight > nextTag = None > nextIndex = end > nextCount = 0 > for tag,(index,c) in found.items(): > if self.editor.compare(index,"<",nextIndex): > nextTag = tag > nextIndex = index > nextCount = c > > #print "Minimum:", nextTag, nextIndex, nextCount > if nextTag is None: break > start = self.editor.index("%s+%sc"%(nextIndex,nextCount)) > self.editor.tag_add(nextTag, nextIndex, start) > > # Update tags > foundItems = found.items() > for tag,(index,c) in foundItems: > #print ">>",tag,index > if self.editor.compare(index,"<",start): > index = self.editor.search(patterns[tag][0], > start, end, > count=count, > regexp=True) > if index != "": > #print "Update:", tag, index, count.get() > found[tag] = (index, count.get()) > else: > #print "Update:", tag, "-None-" > del found[tag] > > # Set properties to tags > for tag,(pat,color) in patterns.items(): > self.editor.tag_config(tag,foreground=color) > > # > ---------------------------------------------------------------------- > def highlightAfter(self): > if self._highAfter is not None: self.after_cancel(self._highAfter) > self._highAfter = self.after(10, self.highlight) > > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/tkinter-discuss/attachments/20140825/1961befb/attachment-0001.html > > > > ------------------------------ > > Message: 2 > Date: Tue, 26 Aug 2014 10:34:21 +0200 > From: Michael Lange <klappn...@web.de> > To: tkinter-discuss@python.org > Subject: Re: [Tkinter-discuss] Text() highlight > Message-ID: <20140826103421.f25184a997aad67e7c6bb...@web.de> > Content-Type: text/plain; charset=US-ASCII > > On Mon, 25 Aug 2014 14:15:59 +0000 > Vasilis Vlachoudis <vasilis.vlachou...@cern.ch> wrote: > > > Hi all, > > > > I am trying to implement a small g-code editor and real time viewer in > > tkinter. I am using the Text() widget as an editor, but I have troubles > > in highlighting the text during editing. Its slow when editing big > > files. > (...) > > I don't have much experience with the text widget's advanced > capabilities, so I can just give a general hint on how to speed up > repeating tasks in Tkinter. > In my experience calls to Python commands are usually fast enough, the > bottleneck is the calls to Tk, though I am not sure if this is because Tk > is slow in itself or if it is the mechanism of the Tcl interpreter that > is somehow "embedded" into Python that makes things slow. > So as a rule of thumb I suggest that if you want to speed up things I > would try to eliminate as many of the calls to Tkinter widgets from the > repeating routine as possible. > If you want to track down which of these calls are particularly time > expensive sometimes it proved useful to add some calls to time.time() to > measure the duration a particular command took. > So my suggestion is to see, if you can store some of the information > about the tags that you query in your highlight() routine again and again > e.g. with the text widgets search() or compare() methods in a python list > or dictionary and access the information from there. This might > complicate parts of the code but will probably result in a remarkable > speed-up of your routine. > > Regards > > Michael > > > .-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-. > > Kirk to Enterprise -- beam down yeoman Rand and a six-pack. > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Tkinter-discuss mailing list > Tkinter-discuss@python.org > https://mail.python.org/mailman/listinfo/tkinter-discuss > > > ------------------------------ > > End of Tkinter-discuss Digest, Vol 126, Issue 3 > *********************************************** >
_______________________________________________ Tkinter-discuss mailing list Tkinter-discuss@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss