git.net

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

In praise of triple-quoted strings


Python's triple-quoted multiline string is a wonderful feature. It
vastly simplifies the writing of polyglot scripts (where the same file
contains multiple different languages of code in it), hiding any
non-Python code inside a triple quoted string. Had need of a
Makefile+Python setup today...

(Note that if the formatting is messed up, just make sure the
"@python3 Makefile" line is indented with exactly one tab.)

# Makefile+Python script
hide_from_python = """ "
all: README.md
        @python3 Makefile

define now_hide_from_make =
" """
# At this point, it's all Python. :)
import this
print("Hello, world")

# Postscript to clean everything up.
""" "
endef
end_all_hiding = " """


-----------

The same trick can be done with quite a few other languages. The
non-Python part might be a shell script that locates an interpreter,
an Markdown file that explains what to do if you accidentally
displayed this file instead of downloading it, a Pike server that runs
a supporting server (that one's easy since Pike has a multiline string
spelled #" ... ", which looks like a comment to Python), a systemd
service file to symlink into your jobs directory... you name it. The
only hard part is making the OTHER language ignore the parts it needs
to ignore! :)

ChrisA