0

I have a couple "lengthy" lines like this one:

content = "<!DOCTYPE html>\n<html>\n<head>\n  <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n  <title>{0} - {1}</title>\n  <link rel=\"stylesheet\" type=\"text/css\" href=\"{3}/{2}.{4}\" />\n</head>\n<body>\n  <!-- layout... -->\n  <script type\"text/javascript\" src=\"{5}/{2}.{6}\"></script>\n</body>\n</html>\n".format(settings["app.name"], ncc, name, settings["dir"]["static.style"], settings["lang.style"], settings["dir"]["static.script"], settings["lang.script"])

Just by looking at such a line it you can tell that it is quite large and that it is in some ways hard to read and / or understand.

Is there a more efficient and readable way to dynamically insert lots of values into large strings?

dot.Py
  • 5,007
  • 5
  • 31
  • 52
  • you should take a look at jinja2 or mako... they're not specifically designed for strings, but for creating dynamic html pages/reports. – dot.Py Jun 07 '16 at 17:39
  • @Dot_Py thank you for the suggestion I will look into them. –  Jun 07 '16 at 17:56

3 Answers3

1

Another option would be to do:

content = (
    "<!DOCTYPE html>\n<html>\n<head>\n  "
    "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">\n"
    "  <title>{0} - {1}</title>\n  "
    "<link rel=\"stylesheet\" type=\"text/css\" href=\"{3}/{2}.{4}\" />\n</head>\n<body>\n"
    "  <!-- layout... -->\n  "
    "<script type\"text/javascript\" src=\"{5}/{2}.{6}\"></script>\n</body>\n</html>\n"
).format(
    settings["app.name"], ncc, name, settings["dir"]["static.style"],
    settings["lang.style"], settings["dir"]["static.script"],
    settings["lang.script"]
)

Note that there is no , at the end of each line.

However the best way to generate HTML is probably via some templating framework, e.g. jinja, mako, etc.

freakish
  • 54,167
  • 9
  • 132
  • 169
  • Thank you very much for the answer, I did not know you could do this. +1 I really appreciate the time and effort you put into it. –  Jun 07 '16 at 17:56
  • plus1 for jinja2 / mako option – dot.Py Jun 07 '16 at 17:56
0

by having the strings in a list and then doing a join as in ' '.join(list). This will be a lot faster than simple concatenation.

list = ["The quick brown", "fox"] result = " ".join(list) Here is a link to see the difference is performance

Community
  • 1
  • 1
limbo
  • 684
  • 9
  • 18
  • Have you actually checked that it will be faster then raw string with `.format`? Cause it is **highly** unlikely. – freakish Jun 07 '16 at 17:44
  • Check the link I provided. You can see how many concats per second each approach does – limbo Jun 07 '16 at 17:46
  • Yes, the link you provided talks about `+` operator. That's absolutely not the case here. Please read questions carefuly next time. – freakish Jun 07 '16 at 17:47
  • the answer over there provides a link where there are a couple of methods compared. However you have a point I do not know how .format fits in the comparison. That being said I cannot image it is very fast because of the extra work it has to do. – limbo Jun 07 '16 at 17:52
  • Thank you very much for the answer, +1 I really appreciate the time and effort you put into it. –  Jun 07 '16 at 17:55
0

You could use a multiline string:

args = (settings["app.name"], ncc, name, settings["dir"]["static.style"], 
        settings["lang.style"], settings["dir"]["static.script"], 
        settings["lang.script"])

content = """\
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>{0} - {1}</title>
  <link rel="stylesheet" type="text/css" href="{3}/{2}.{4}" />
</head>
<body>
  <!-- layout... -->
  <script type"text/javascript" src="{5}/{2}.{6}"></script>
</body>
</html>""".format(args)
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Thank you very much for the answer, +1 I really appreciate the time and effort you put into it. –  Jun 07 '16 at 17:55