services
About python Careers Visitors Services News & Events Tutorials Further Resources Editors BDFL

Despite what assembly code and C coders might tell us, high-level languages do have their place in every programmer's toolbox, and some of them are much more than a computer-science curiosity. Out of the many high-level languages we can choose from today, Python seems to be the most interesting for those who want to learn something new and do real work at the same time. Its no-nonsense implementation of object-oriented programming and its clean and easy-to-understand syntax make it a language that is fun to learn and use, which is not something we can say about most other languages.





import re
from Tkinter import *
import tkMessageBox

def get(root):
    if not hasattr(root, "_searchengine"):
        root._searchengine = SearchEngine(root)
        # XXX This will never garbage-collect 
    return root._searchengine

class SearchEngine:

    def __init__(self, root):
        self.root = root
       

    # Access methods

    def getpat(self):
        return self.patvar.get()

    def setpat(self, pat):
        self.patvar.set(pat)

    def isre(self):
        return self.revar.get()

    def iscase(self):
        return self.casevar.get()

    def isword(self):
        return self.wordvar.get()

    def iswrap(self):
        return self.wrapvar.get()

    def isback(self):
        return self.backvar.get()

    # Higher level access methods

    def getcookedpat(self):
        pat = self.getpat()
        if not self.isre():
            pat = re.escape(pat)
        if self.isword():
            pat = r"\b%s\b" % pat
        return pat

    def getprog(self):
        pat = self.getpat()
        if not pat:
            self.report_error
            return None
        pat = self.getcookedpat()
        flags = 0
        if not self.iscase():
            flags = flags | re.IGNORECASE
        try:
            prog = re.compile(pat, flags)
        except re.error, what:
            try:
                msg, col = what
            except:
                msg = str(what)
                col = -1
            self.report_error(pat, msg, col)
            return None
        return prog

    def report_error(self, pat, msg, col=-1):
        # Derived class could overrid this with 
        msg = "Error: " + str(msg)
        if pat:
            msg = msg + "\np\Pattern: " + str(pat)
        if col >= 0:
            msg = msg + "\nOffset: " + str(col)
        tkMessageBox.showerror("Regular expression error",
                               msg, master=self.root)

    def setcookedpat(self, pat):
        if self.isre():
            pat = re.escape(pat)
        self.setpat(pat)

    def search_text(self, text, prog=None, ok=0):
        """Search a text widget for the pattern.

        If prog is given, it should be the precompiled pattern.
        Return a tuple (lineno, matchobj); None if not found.

        This obeys the wrap and direction (back) settings.


Announcements


Willison: Would you say that a lot of the interesting work and excitement that's going on now in Python, or more of it at least than in the past, is on the application side? That is, applications of Python rather than new features of Python?

van Rossum: Yes. My own perception of that is somewhat colored by where people ask my advice, which is still, of course, about changes to Python internals or at least standard libraries. But I see an enormous amount of stuff going on where people are just developing very exciting applications. I think Zope is probably the prime example at the moment.

Willison: These next items aren't exactly applications, but two areas where there's a lot of interest and activity with Python now are JPython and Python on Win32. I wondered if you had a sense of how much Python activity is now happening on platforms other than, say, Unix platforms?

Related Reading Python Programming On Win32 Help for Windows Programmers By Mark�Hammond, Andy�Robinson

van Rossum: Well, I think, a lot. The Windows platform especially gets a lot of attention. It also gets attention from some very specialized people. I think you're also working with Mark Hammond.

Willison: Yes. We have a book that's going into production, which means it's about three years overdue.







whe