Newbie Class Questions

Ed Suominen ed-no at spam-eepatents.com
Sat Mar 27 17:39:53 EST 2004


I think it would be cool for my "AutoText" class (see below, hereby GPL'd)
to be able to tell what subclasses inherit from it and instantiate objects
for all of those subclasses to implement a "AutoAll" function.. Any ideas?

Also, are there any comments on my use of "import" in the parent class?

I have all of 3 days experience with OOP and Python, so please bear with me. 

For a short while at least, the code is also available at
http://rafb.net/paste/results/PyX42087.html

Thanks,
Ed Suominen

------------------------------------------------------------------------
class AutoText(HTMLgen.RawText):
    """
    Implements Wiki-like markups of text, returning text in an RawText
object
    to preserve any tags.
    """
    import sre

    def __init__(self, text):
        HTMLgen.RawText.__init__(self, text=text)


class AutoUrl(AutoText):
    """
    Scans string (or string-like object) for urls and replaces them with
    lookalike links. Returns the text in an RawText object to preserve any
    tags. Passes keywords on to href except text and url.
    """
    def __init__(self, text='', **kw):
        text = str(text)
        for i in ['text', 'url']:
            if i in kw: del kw[i]
        while 1:
            m = sre.search('(\s+|\()(http://[a-zA-Z0-9\-\_\.\/\:\?\&]+)',
text)
            if m is None: break
            url = m.group(2).rstrip('.?')
            x = HTMLgen.Href(url=url, text=url, **kw)
            text = text.replace(url, str(x))
        AutoText.__init__(self, text)


class AutoEmphasis(AutoText):
    """
    Scans string (or string-like object) for Wiki-type emphasis markups and
    replaces them with HTML markup. Returns the text in an RawText object to
    preserve any tags.
    """
    def __init__(self, text=''):
        text = str(text)
        for xy in [("""''""", 'Emphasis'), ("""'''""", 'Strong')]:
            while 1:
                m = sre.search('\s+'+xy[0]+'([^\'].+?)'+xy[0]+'\s+',text)
                if m is None: break
                x = eval('HTMLgen.'+xy[1]+'(\''+m.group(1)+'\')')
                text = text.replace(xy[0]+m.group(1)+xy[0], str(x))
        AutoText.__init__(self, text)



More information about the Python-list mailing list