Need advice on how to improve this function

Discussion in 'Python' started by Matthew Wilson, Aug 20, 2006.

  1. I wrote a function that converts a tuple of tuples into html. For
    example:

    In [9]: x
    Out[9]:
    ('html',
    ('head', ('title', 'this is the title!')),
    ('body',
    ('h1', 'this is the header!'),
    ('p', 'paragraph one is boring.'),
    ('p',
    'but paragraph 2 ',
    ('a', {'href': 'http://example.com'}, 'has a link'),
    '!')))


    In [10]: as_html(x, sys.stdout)
    <html>

    <head>

    <title>this is the title!</title>

    </head>

    <body>

    <h1>this is the header!</h1>

    <p>paragraph one is boring.</p>

    <p>but paragraph 2 <a href="http://example.com">has a link</a>!</p>

    </body>

    </html>


    I'd like to know ways to make it better (more efficient, able to deal
    with enormous-size arguments, etc). How would I write this as a
    generator?

    Here's the definition for as_html:

    def as_html(l, s):
    "Convert a list or tuple into html and write it to stream s."
    if isinstance(l, (tuple, list)):
    tagname = l[0]
    if isinstance(l[1], dict):
    attributes = ' '.join(['%s="%s"' % (k, l[1][k]) for k in l[1]])
    s.write('<%s %s>' % (tagname, attributes))
    else:
    s.write('<%s>' % tagname)
    if tagname in ('html', 'head', 'body'):
    s.write('\n\n')
    for ll in l[1:]:
    as_html(ll, s)
    s.write('</%s>' % tagname)
    if tagname not in ('a', 'b', 'ul'):
    s.write('\n\n')
    elif isinstance(l, str):
    s.write(l)


    All comments welcome. TIA

    --
    A better way of running series of SAS programs:
    http://overlook.homelinux.net/wilsonwiki/SasAndMakefiles
     
    Matthew Wilson, Aug 20, 2006
    #1
    1. Advertising

  2. Matthew Wilson

    Larry Bates Guest

    Matthew Wilson wrote:
    > I wrote a function that converts a tuple of tuples into html. For
    > example:
    >
    > In [9]: x
    > Out[9]:
    > ('html',
    > ('head', ('title', 'this is the title!')),
    > ('body',
    > ('h1', 'this is the header!'),
    > ('p', 'paragraph one is boring.'),
    > ('p',
    > 'but paragraph 2 ',
    > ('a', {'href': 'http://example.com'}, 'has a link'),
    > '!')))
    >
    >
    > In [10]: as_html(x, sys.stdout)
    > <html>
    >
    > <head>
    >
    > <title>this is the title!</title>
    >
    > </head>
    >
    > <body>
    >
    > <h1>this is the header!</h1>
    >
    > <p>paragraph one is boring.</p>
    >
    > <p>but paragraph 2 <a href="http://example.com">has a link</a>!</p>
    >
    > </body>
    >
    > </html>
    >
    >
    > I'd like to know ways to make it better (more efficient, able to deal
    > with enormous-size arguments, etc). How would I write this as a
    > generator?
    >
    > Here's the definition for as_html:
    >
    > def as_html(l, s):
    > "Convert a list or tuple into html and write it to stream s."
    > if isinstance(l, (tuple, list)):
    > tagname = l[0]
    > if isinstance(l[1], dict):
    > attributes = ' '.join(['%s="%s"' % (k, l[1][k]) for k in l[1]])
    > s.write('<%s %s>' % (tagname, attributes))
    > else:
    > s.write('<%s>' % tagname)
    > if tagname in ('html', 'head', 'body'):
    > s.write('\n\n')
    > for ll in l[1:]:
    > as_html(ll, s)
    > s.write('</%s>' % tagname)
    > if tagname not in ('a', 'b', 'ul'):
    > s.write('\n\n')
    > elif isinstance(l, str):
    > s.write(l)
    >
    >
    > All comments welcome. TIA
    >

    Before you put too much work into this you might want to take a look
    at HTMLgen: http://www.python.net/crew/friedrich/HTMLgen/html/main.html

    -Larry
     
    Larry Bates, Aug 21, 2006
    #2
    1. Advertising

  3. Matthew Wilson wrote:

    > I'd like to know ways to make it better (more efficient, able to deal
    > with enormous-size arguments, etc). How would I write this as a
    > generator?


    what makes you think that a generator would be the right tool for this
    task? what's the use case?

    (btw, generating "enormous-size" html pages strikes me as a rather
    pointless exercise...)

    </F>
     
    Fredrik Lundh, Aug 21, 2006
    #3
  4. At Monday 21/8/2006 12:03, Larry Bates wrote:

    > > I wrote a function that converts a tuple of tuples into html. For
    > > example:
    > > I'd like to know ways to make it better (more efficient, able to deal
    > > with enormous-size arguments, etc). How would I write this as a
    > > generator?

    >Before you put too much work into this you might want to take a look
    >at HTMLgen: http://www.python.net/crew/friedrich/HTMLgen/html/main.html


    Another very good library is <http://dustman.net/andy/python/HyperText>
    (Don't be afraid of the date - it's just that HTML standards haven't
    changed very much lately :) )



    Gabriel Genellina
    Softlab SRL





    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
     
    Gabriel Genellina, Aug 22, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gbattine
    Replies:
    8
    Views:
    444
    Philipp Leitner
    Jul 16, 2006
  2. Anand

    Need to Improve my skill set in C

    Anand, Jan 11, 2004, in forum: C Programming
    Replies:
    3
    Views:
    587
    Darrell Grainger
    Jan 11, 2004
  3. jdm
    Replies:
    4
    Views:
    360
  4. Replies:
    24
    Views:
    238
  5. SSS Develop

    Need suggestions to improve code

    SSS Develop, Aug 24, 2013, in forum: Perl Misc
    Replies:
    2
    Views:
    209
    Xho Jingleheimerschmidt
    Aug 27, 2013
Loading...

Share This Page