[Tkinter-discuss] Tkinter-discuss Digest, Vol 126, Issue 3
alessandro medici
alexxandro.medici at gmail.com
Tue Aug 26 16:00:34 CEST 2014
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-request at python.org>:
> Send Tkinter-discuss mailing list submissions to
> tkinter-discuss at 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-request at python.org
>
> You can reach the person managing the list at
> tkinter-discuss-owner at 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.Vlachoudis at cern.ch>
> To: "tkinter-discuss at python.org" <tkinter-discuss at python.org>
> Subject: [Tkinter-discuss] Text() highlight
> Message-ID:
> <0BC70B5D93E054469872FFD0FE07220EC0C2B03F at 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 <klappnase at web.de>
> To: tkinter-discuss at python.org
> Subject: Re: [Tkinter-discuss] Text() highlight
> Message-ID: <20140826103421.f25184a997aad67e7c6bbd93 at web.de>
> Content-Type: text/plain; charset=US-ASCII
>
> On Mon, 25 Aug 2014 14:15:59 +0000
> Vasilis Vlachoudis <Vasilis.Vlachoudis at 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 at python.org
> https://mail.python.org/mailman/listinfo/tkinter-discuss
>
>
> ------------------------------
>
> End of Tkinter-discuss Digest, Vol 126, Issue 3
> ***********************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20140826/b0b66e5e/attachment-0001.html>
More information about the Tkinter-discuss
mailing list