Prev: pyc runtime error
Next: Ancient C string conventions (was Re: Why Is Escaping DataConsidered So Magical?)
From: Nico Grubert on 30 Jun 2010 04:34 Dear list members I have this python list that represets a sitemap: tree = [{'indent': 1, 'title':'Item 1', 'hassubfolder':False}, {'indent': 1, 'title':'Item 2', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 1', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 1.1', 'hassubfolder':False}, {'indent': 2, 'title':'Sub Item 1.2', 'hassubfolder':False}, {'indent': 1, 'title':'Item 3', 'hassubfolder':False}, {'indent': 1, 'title':'Folder 2', 'hassubfolder':True}, {'indent': 2, 'title':'Sub Item 2.1', 'hassubfolder':False}, {'indent': 2, 'title':'Folder 2.1', 'hassubfolder':True}, {'indent': 3, 'title':'Sub Item 2.1.1', 'hassubfolder':False}, {'indent': 3, 'title':'Sub Item 2.1.2', 'hassubfolder':False}, ] From that list I want to create the following HTML code: <ul id="tree"> <li>Item 1</li> <li>Item 2</li> <li>Folder 1 <ul> <li>Sub Item 1.1</li> <li>Sub Item 1.2</li> </ul> </li> <li>Item 3</li> <li>Folder 2 <ul> <li>Sub Item 2.1</li> <li>Folder 2.1 <ul> <li>Sub Item 2.1.1</li> <li>Sub Item 2.1.2</li> </ul> </li> </ul> </li> </ul> If an item of the list has 'True' for the 'hassubfolder' key than a new "<ul><li>" must be created instead of "</li>" after its title. (See "Folder 2" node in the HTML code above. My problem is: How do I keep track of the closing tags while iterating over the python list? Any help is much appreciated. Regards Nico
From: Kushal Kumaran on 30 Jun 2010 06:02 On Wed, Jun 30, 2010 at 2:04 PM, Nico Grubert <nicogrubert(a)yahoo.de> wrote: > Dear list members > > I have this python list that represets a sitemap: > > tree = [{'indent': 1, 'title':'Item 1', 'hassubfolder':False}, > Â Â Â Â {'indent': 1, 'title':'Item 2', 'hassubfolder':False}, > Â Â Â Â {'indent': 1, 'title':'Folder 1', 'hassubfolder':True}, > Â Â Â Â {'indent': 2, 'title':'Sub Item 1.1', 'hassubfolder':False}, > Â Â Â Â {'indent': 2, 'title':'Sub Item 1.2', 'hassubfolder':False}, > Â Â Â Â {'indent': 1, 'title':'Item 3', 'hassubfolder':False}, > Â Â Â Â {'indent': 1, 'title':'Folder 2', 'hassubfolder':True}, > Â Â Â Â {'indent': 2, 'title':'Sub Item 2.1', 'hassubfolder':False}, > Â Â Â Â {'indent': 2, 'title':'Folder 2.1', 'hassubfolder':True}, > Â Â Â Â {'indent': 3, 'title':'Sub Item 2.1.1', 'hassubfolder':False}, > Â Â Â Â {'indent': 3, 'title':'Sub Item 2.1.2', 'hassubfolder':False}, > Â Â Â ] > > From that list I want to create the following HTML code: > > <ul id="tree"> > Â <li>Item 1</li> > Â <li>Item 2</li> > Â <li>Folder 1 > Â Â <ul> > Â Â Â <li>Sub Item 1.1</li> > Â Â Â <li>Sub Item 1.2</li> > Â Â </ul> > Â </li> > Â <li>Item 3</li> > Â <li>Folder 2 > Â Â <ul> > Â Â Â <li>Sub Item 2.1</li> > Â Â Â <li>Folder 2.1 > Â Â Â Â <ul> > Â Â Â Â Â <li>Sub Item 2.1.1</li> > Â Â Â Â Â <li>Sub Item 2.1.2</li> > Â Â Â Â </ul> > Â Â Â </li> > Â Â </ul> > Â </li> > </ul> > > If an item of the list has 'True' for the 'hassubfolder' key than a new > "<ul><li>" must be created instead of "</li>" after its title. (See "Folder > 2" node in the HTML code above. > > My problem is: How do I keep track of the closing tags while iterating over > the python list? > Use a stack? Whenever you start a new list, push the corresponding closing tag onto a stack. Whenever your "indent level" decreases, pop the stack and write out the closing tag you get. It's straightforward to use a python list as a stack. -- regards, kushal
From: Stefan Behnel on 30 Jun 2010 06:47 Nico Grubert, 30.06.2010 10:34: > I have this python list that represets a sitemap: > > tree = [{'indent': 1, 'title':'Item 1', 'hassubfolder':False}, > {'indent': 1, 'title':'Item 2', 'hassubfolder':False}, > {'indent': 1, 'title':'Folder 1', 'hassubfolder':True}, > {'indent': 2, 'title':'Sub Item 1.1', 'hassubfolder':False}, > {'indent': 2, 'title':'Sub Item 1.2', 'hassubfolder':False}, > {'indent': 1, 'title':'Item 3', 'hassubfolder':False}, > {'indent': 1, 'title':'Folder 2', 'hassubfolder':True}, > {'indent': 2, 'title':'Sub Item 2.1', 'hassubfolder':False}, > {'indent': 2, 'title':'Folder 2.1', 'hassubfolder':True}, > {'indent': 3, 'title':'Sub Item 2.1.1', 'hassubfolder':False}, > {'indent': 3, 'title':'Sub Item 2.1.2', 'hassubfolder':False}, > ] > > From that list I want to create the following HTML code: > > <ul id="tree"> > <li>Item 1</li> > <li>Item 2</li> > <li>Folder 1 > <ul> > <li>Sub Item 1.1</li> > <li>Sub Item 1.2</li> > </ul> > </li> > <li>Item 3</li> > <li>Folder 2 > <ul> > <li>Sub Item 2.1</li> > <li>Folder 2.1 > <ul> > <li>Sub Item 2.1.1</li> > <li>Sub Item 2.1.2</li> > </ul> > </li> > </ul> > </li> > </ul> > > If an item of the list has 'True' for the 'hassubfolder' key than a new > "<ul><li>" must be created instead of "</li>" after its title. (See > "Folder 2" node in the HTML code above. > > My problem is: How do I keep track of the closing tags while iterating > over the python list? Don't. Just use a tool for generating the XML, such as ElementTree's builder. http://effbot.org/zone/element-builder.htm http://svn.effbot.org/public/stuff/sandbox/elementlib/builder.py Stefan
From: Nico Grubert on 30 Jun 2010 07:15 > Use a stack? > > Whenever you start a new list, push the corresponding closing tag onto > a stack. Whenever your "indent level" decreases, pop the stack and > write out the closing tag you get. > > It's straightforward to use a python list as a stack. Thanks for the tip, Kushal. Do you have a short code example for me? Regards Nico
From: Dave Angel on 30 Jun 2010 09:30 Nico Grubert wrote: > Use a stack? >> >> Whenever you start a new list, push the corresponding closing tag onto >> a stack. Whenever your "indent level" decreases, pop the stack and >> write out the closing tag you get. >> >> It's straightforward to use a python list as a stack. > > Thanks for the tip, Kushal. > Do you have a short code example for me? > > Regards > Nico > > mylist = [3, 4, 5] mylist.append[42] print mylist 3,4,5,42 item = mylist.pop() returns the 42, removing it from the list So, use append as a push, and pop without arguments as a pop. And use len() to decide how big the list currently is. DaveA
|
Next
|
Last
Pages: 1 2 Prev: pyc runtime error Next: Ancient C string conventions (was Re: Why Is Escaping DataConsidered So Magical?) |