[Twisted-web] Menu flattening

Donovan Preston twisted-web@twistedmatrix.com
Thu, 11 Dec 2003 11:56:24 -0500


On Dec 11, 2003, at 11:01 AM, Justin Johnson wrote:

> This seems to do what I want.  I should probably rethink this though,
> since dict's aren't ordered.
>
> def flatten_menu(menu_items):
>     if isinstance(menu_items, dict):
>         html_menu = ul[
>             [ (li[k], [flatten_menu(i) for i in menu_items[k]]) \
>                 for k in menu_items.keys() if k]
>         ]
>     else:
>         html_menu = ul[ li[menu_items] ]
>     return html_menu

Yes, this works too, and really shows off how stan expressions can 
shorten complex HTML generation code. By the way, you don't need the \ 
character to continue the line there, because python will allow you to 
have linebreaks while there is an outstanding brace that hasn't been 
closed. (), {}, and []

dp