Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Need advice on how to improve this function

Reply
Thread Tools

Need advice on how to improve this function

 
 
Matthew Wilson
Guest
Posts: n/a
 
      08-20-2006
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/wilson...asAndMakefiles
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      08-21-2006
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...html/main.html

-Larry
 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      08-21-2006
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>

 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      08-22-2006
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...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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Need help/advice to improve script sopan.shewale@gmail.com Perl Misc 24 07-26-2010 10:06 PM
Advice on using templates and vector to improve exception handling jdm C++ 4 02-01-2010 06:01 PM
Can i improve this function?i need your help... gbattine Java 8 07-16-2006 12:36 PM
improve my search and replace function pembed2003 C++ 10 06-23-2004 08:12 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57