need dict to maintain order

rihad rihad at mail.ru
Sat Jan 19 09:47:58 EST 2002


On Sat, 19 Jan 2002 14:36:24 GMT, Alex Martelli <aleax at aleax.it>
wrote:

>This makes further commenting too hard.  Please use spaces,
>not tabs, in code you post or otherwise publish, because tools
>vary in their ability to handle tabs (and it's unlikely I'll change
>newsreaders just because the two I prefer don't handle tabs
>correctly:-).
>
I KNEW I would be be caught on this one, some day :( Sorry. Here's the
same thing with tabs2spaces applied and backslashes removed :)

import os

class navbar:
    def __init__(self):
        self.links = (
            ('index.html', 'Home'),
            ('prog.html', 'Programming'),
            ('doc.html', 'Docs'),
        )
        self.dirname = self.__class__.__name__ 
        self.cur_link = 0
        try:
            os.mkdir(self.dirname)
        except OSError:
            pass
    def __call__(self):
        while self.cur_link < len(self.links):
            self.start()
            for link, target in self.links:
                self.start_link()
                if self.cur_link != self.num_done:
                    self.f.write("""<a href="%s">%s</a>""" % (link,
target))
                else:
                    self.f.write("""<span
class="disabled-link">%s</span>""" % target)
                self.end_link()
            self.end()
    def start(self):
        fullname = self.dirname + '\\' + self.links[self.cur_link][0]
+ '.nav'
        self.f = file(fullname, "w")
        self.num_done = 0
    def start_link(self):
        pass
    def end_link(self):
        self.num_done += 1
    def end(self):
        self.f.close()
        self.cur_link += 1

class navbar_horz(navbar):
    def __init__(self):
        navbar.__init__(self)
    def start(self):
        navbar.start(self)
        self.f.write("""<div class="navbar-horz">\n[\n""")
    def start_link(self):
        navbar.start_link(self)
    def end_link(self):
        navbar.end_link(self)
        if self.num_done < len(self.links):
            self.f.write("\n | ")
    def end(self):
        self.f.write("\n]\n</div>\n")
        navbar.end(self)

class navbar_vert(navbar):
    def __init__(self):
        navbar.__init__(self)
    def start(self):
        navbar.start(self)
        self.f.write("""<div class="navbar-vert">\n""")
    def start_link(self):
        navbar.start_link(self)
    def end_link(self):
        navbar.end_link(self)
        if self.num_done < len(self.links):
            self.f.write("<br><br>\n")
    def end(self):
        self.f.write("\n</div>\n")
        navbar.end(self)

if __name__ == '__main__':
    navbar_horz()()
    navbar_vert()()




More information about the Python-list mailing list