def mktag(tag, data, **attrs): tag = tag.lower() attrs = "".join([' %s="%s"' % (k.lower(), v) for k, v in attrs.items()]) if data: return "<%s%s>%s</%s>" % (tag, attrs, data, tag) else: return "<%s%s />" % (tag, attrs)More XHTML-compliant (lowercase tags, empty tags) Too bad there's no easy way to HTML-escape data strings, but not the tags that are generated.for tag in ("a", "ul", "li", "img"): # etc exec "%s = lambda d=None, **a: mktag(%r, d, **a)" % (tag, tag)
if __name__ == '__main__': print a("Google", href="http://www.google.com") print ul(li("item1") + li("item2") + li("item3")) print img(src="foo.jpg")