473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Need advice on how to improve this function

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
Aug 20 '06 #1
3 1218
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
Aug 21 '06 #2
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>

Aug 21 '06 #3
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

Aug 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
4
by: pcunix | last post by:
I'm looking for general advice, pointers to web pages, books, whatever. I have a moderately successful web site. The major complaint that I get, time after time, is "It's UUUGLY" As I have...
9
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
7
by: fox | last post by:
Hi, Lacking javascript knowledge, I just realized why my project has a bug. I am using ASP to loop through a set of records while it creates URLs with a querystring that has a single value pair....
1
by: Mark C | last post by:
Hi I am in the progress of developing a web site whereby developers can do free online tests on various programming languages. I would just like some advice or feedback on ways to improve the...
5
by: Mark C | last post by:
Hi I am in the progress of developing a web site whereby developers can do free online tests on various programming languages. I would just like some advice or feedback on ways to improve the...
2
by: OracleNewbie | last post by:
Hi Guys, I've been trying for over 5 days just to get it right. It seems i got it but I'm having problems in the output and getting a lot of errors. I made all type of research and tried it again...
6
by: shapper | last post by:
Hello, I am creating a form that includes a few JQuery scripts and TinyMCE Editor: http://www.27lamps.com/Beta/Form/Form.html I am having a few problems with my CSS: 1. Restyling the Select
2
by: GHUM | last post by:
Hello, I am pylinting some software of mine. Now pylint throws messages, and I know of pylint --help-msg to get some more text. What is missing out are explanation, WHY some things are bad,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.