# XML-tag function

I'm an experienced programmer, but just a beginner in Python, and would like some input on a small function that's working but which I'm not very happy with.

It's used to produce XML tags (strings). The content argument can be a string, a number etc., but if it's a datetime I have to format it properly. Also, if there is no content, the tag should not be rendered (return empty string).

def tag(tagName, content):
if isinstance(content, datetime):
content = content.strftime('%Y%m%d%H%M')
else:
content = str(content)

if content:
return "<" + tagName + ">" + content + "</" + tagName + ">"

return ""


-

Try this:

return "<%s>%s</%s>" % (tagName, content, tagName)


e.g:

def tag(tagName, content):
if isinstance(content, datetime):
content = content.strftime('%Y%m%d%H%M')
else:
content = str(content)

if content:
return "<%s>%s</%s>" % (tagName, content, tagName)

return ""


Slightly more attractive if that's what you want?

-
Since there have been no other concrete suggestions actually answering the question I guess 1) there is nothing really wrong or non-idiomatic with my code, and 2) you deserve to get your answer accepted. –  Torbjørn Jul 8 '11 at 13:36
To be honest I think text based UI's are an ancient paradigm, but are mostly the best we have in terms of practicality for the moment. There are a whole array of approaches, node based UIs, wiki based UIs, social coding environments, batteries massively included libraries brimming with start convenience functions that might make logic / code elegance more obvious. Maybe it's offtopic or meandering but if you feel dissatisfied in general with the elegance - the problem isn't the language as such, it's the medium. It's steadily evolving. –  Luke Stanley Jul 8 '11 at 21:54
Code Bubbles is one such thing I was thinking of: youtube.com/watch?v=PsPX0nElJ0k –  Luke Stanley Jul 8 '11 at 23:38

Always use an XML library to read or write XML. Stitching XML together with string concatenation misses the point of using XML.

-
If this simple function is all the XML functionality I need, I don't want to learn or take a dependency on a library. The question is not about that though, but would like to know if the function can be refactored into something more pretty. –  Torbjørn Jun 14 '11 at 12:12
I'm not sure why you're asking - how could it be simpler or more readable than it is now? But if content may contain arbitrary strings, you're already producing invalid XML, so I'd focus on that problem first. –  reinierpost Jun 14 '11 at 13:00
"how could it be simpler or more readable than it is now?" I don't know. It's what I'm asking. I know how to make it better in other languages. It feels like there should be better ways, and I'm just wondering if Python provides any nice features I could use here. (My real problem is that I think Python is ugly, and I want someone to show me that it can be beautiful after all. I'm still waiting (-: ) –  Torbjørn Jun 14 '11 at 17:16
Maybe you can give an example of what you'd do in a different language. I have no idea what you have in mind. –  reinierpost Jun 15 '11 at 9:02

I'll criticise the general design of the tag function.

tag can't accept general input, because that would produce invalid xml. It special-cases empty contents, which prevents it from being generally useful; there is no way to use it to get an empty tag (breaking a weak requirement of good APIs: making simple things possible). It only makes sense as a private part of a slightly larger program that produces valid xml, which you did not include.

-
I totally agree. And it is a private part of a slightly larger program, pswinpy: github.com/tormaroe/pswinpy –  Torbjørn Jun 15 '11 at 6:57
Well that settles the type-safety question, unfavourably. content needs to be properly quoted. –  Tobu Jun 15 '11 at 22:36