need dict to maintain order
rihad
rihad at mail.ru
Sat Jan 19 09:03:57 EST 2002
More information about the Python-list mailing list
Sat Jan 19 09:03:57 EST 2002
- Previous message (by thread): need dict to maintain order
- Next message (by thread): need dict to maintain order
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Fri, 18 Jan 2002 11:39:36 -0500, "Steve Holden" <sholden at holdenweb.com> wrote: >Rather than using a dictionary and then (presumably) a while loop until the >dictionary is empty, consider setting up a list or tuple of tuples, such as > > links = (('index.html', 'Home'), ('file0.html', 'Some foo')) > >The you can use > > for link, caption in links: > # generate nav bar line from link and capion > Here's what I was able to come up with (constantly referring to the man/tut :)). Maybe someone finds it... hmmm... useful and/or enjoys commenting newbies' code :) (sorry, didn't `tabs2spaces' the code since tabs are natural (?) in python anyway) 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()()
- Previous message (by thread): need dict to maintain order
- Next message (by thread): need dict to maintain order
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list