2
\$\begingroup\$

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 ""

Please enlighten me!

\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

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?

\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$
    – Torbjørn
    Jul 8, 2011 at 13:36
  • \$\begingroup\$ 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. \$\endgroup\$ Jul 8, 2011 at 21:54
  • \$\begingroup\$ Code Bubbles is one such thing I was thinking of: youtube.com/watch?v=PsPX0nElJ0k \$\endgroup\$ Jul 8, 2011 at 23:38
4
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – Torbjørn
    Jun 14, 2011 at 12:12
  • \$\begingroup\$ 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. \$\endgroup\$ Jun 14, 2011 at 13:00
  • \$\begingroup\$ "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 (-: ) \$\endgroup\$
    – Torbjørn
    Jun 14, 2011 at 17:16
  • \$\begingroup\$ Maybe you can give an example of what you'd do in a different language. I have no idea what you have in mind. \$\endgroup\$ Jun 15, 2011 at 9:02
2
\$\begingroup\$

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.