0

I was messing around with the modules in my site-packages folder when I found a Python file: bottle.py.

From what I can read, it is used for small web applications. I was curious to read its source so I got into it.

However, in the last part of the script (lines: 3720-3753) they made a strange use of the Python's multiline string... Here's the code:

ERROR_PAGE_TEMPLATE = """
%%try:
    %%from %s import DEBUG, HTTP_CODES, request, touni
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html>
        <head>
            <title>Error: {{e.status}}</title>
            <style type="text/css">
              html {background-color: #eee; font-family: sans;}
              body {background-color: #fff; border: 1px solid #ddd;
                    padding: 15px; margin: 15px;}
              pre {background-color: #eee; border: 1px solid #ddd; padding: 5px;}
            </style>
        </head>
        <body>
            <h1>Error: {{e.status}}</h1>
            <p>Sorry, the requested URL <tt>{{repr(request.url)}}</tt>
               caused an error:</p>
            <pre>{{e.body}}</pre>
            %%if DEBUG and e.exception:
              <h2>Exception:</h2>
              <pre>{{repr(e.exception)}}</pre>
            %%end
            %%if DEBUG and e.traceback:
              <h2>Traceback:</h2>
              <pre>{{e.traceback}}</pre>
            %%end
        </body>
    </html>
%%except ImportError:
    <b>ImportError:</b> Could not generate the error page. Please add bottle to
    the import path.
%%end
""" % __name__

What does the %% operator do exactly? I tried to search for it on Google but I couldn't find much.

Also, what are its other uses?

PS: the {{variable}} expressions are possible in every multiline script in Python?

Seintian
  • 151
  • 1
  • 1
  • 11
  • This has nothing to do with bottle. You're using the `%` operator to format the string. `%%` is used to generate literal `%` in the result, since `%` is replaced with the parameter to `%`. – Barmar Aug 17 '22 at 19:09
  • The `{{abc}}` syntax is part of the bottle templating system. That's how they do substitution. It's similar to the `f"{abc}"` notation in modern Python's f-strings. – Tim Roberts Aug 17 '22 at 19:13
  • so that's like another "marker" that they replace in the compile phase, I understood, thank u! – Seintian Aug 17 '22 at 19:19
  • To be more specific, `"%%"` and `"{{xxx}}`" mean nothing in a multi-ling string. It's just a string. `"%%"` means something to the `%` operator, and `{{xxx}}` means something to bottle. – Tim Roberts Aug 17 '22 at 19:20
  • alright, everything's more clear now, thank you so much! However... now should I leave the post as it is or delete it because it's closed? I never know what to do in this cases – Seintian Aug 17 '22 at 19:35

1 Answers1

0

It acts as an escape character:

>>> x = 'abc'
>>> '%s' % x
'abc'
>>> '%s %%' % x
'abc %'
The Thonnu
  • 3,578
  • 2
  • 8
  • 30
  • oh, now I understand... so that isn't an "operator", it's just a marker that they needed to process those lines of code by themselves, right? – Seintian Aug 17 '22 at 19:17