Repository navigation
Description
On #2366 and also on some of the application wrappers recently updated to follow the black style (#2008), we had some code assigning a multi-line string defined with triple quotes to a variable (which is different in appearance to triple quotes as used in docstrings). How should these be indented?
This issue is to review all the existing use of multiline triple quote strings as variables (not the special case of docstrings).
Background
This may look nice, but is not doing what the author might have expected:
def usage():
tool_name = "example"
help_text = """This is the first line for help for %s.
This is the third line (second line blank).
And 4th line with trailing new line character.
""" % tool_name
print(help_text)
usage()Output:
$ python /tmp/bad.py
This is the first line for help for example.
This is the third line (second line blank).
And 4th line with trailing new line character.
The following to me looks wrong/ugly, but gives the desired output:
def usage():
tool_name = "example"
help_text = """This is the first line for help for %s.
This is the third line (second line blank).
And 4th line with trailing new line character.
""" % tool_name
print(help_text)
usage()$ python /tmp/good.py
This is the first line for help for example.
This is the third line (second line blank).
And 4th line with trailing new line character.
To improve on this, @mdehoon has often used the following style which is nice in that it explicitly puts all the lines of the multi-line string flush left (including the first line):
def write(motifs):
"""Write the representation of a motif in TRANSFAC format."""
blocks = []
try:
version = motifs.version
except AttributeError:
pass
else:
if version is not None:
block = """\
VV %s
XX
//
""" % versionDoing this and running black, my example becomes:
def usage():
tool_name = "example"
help_text = (
"""\
This is the first line for help for %s.
This is the third line (second line blank).
And 4th line with trailing new line character.
"""
% tool_name
)
print(help_text)
usage()Application wrappers
For example, https://github.com/biopython/biopython/blob/biopython-175/Bio/Phylo/Applications/_Fasttree.py#L68 (and still currently over-indented on the master):
def __init__(self, cmd="fasttree", **kwargs):
"""Initialize the class."""
self.parameters = [
_Switch(["-nt", "nt"],
"By default FastTree expects protein alignments, use -nt for nucleotides",
),
_Option(["-n", "n"],
"""-n -- read N multiple alignments in.
This only works with phylip interleaved format. For example, you can
use it with the output from phylip's seqboot. If you use -n, FastTree
will write 1 tree per line to standard output.
""",
checker_function=_is_int,
equate=False,
),
...If we look at the help output,
>>> from Bio.Phylo.Applications import FastTreeCommandline
>>> help(FastTreeCommandline())
we get:
...
|
| n
| -n -- read N multiple alignments in.
|
| This only works with phylip interleaved format. For example, you can
| use it with the output from phylip's seqboot. If you use -n, FastTree
| will write 1 tree per line to standard output.
|
|
| This controls the addition of the -n parameter and its associated value. Set this property to the argument value required.
|
...
The help text is clearly over-indented (although the effect is largely cosmetic).
The command line property help didn't work on the API documentation built for version 1.74 (fixed since in #2220), but you can see this in the current dev version:
Fixing the indentation we could write this as:
def __init__(self, cmd="fasttree", **kwargs):
"""Initialize the class."""
self.parameters = [
_Switch(["-nt", "nt"],
"By default FastTree expects protein alignments, use -nt for nucleotides",
),
_Option(["-n", "n"],
"""\
-n -- read N multiple alignments in.
This only works with phylip interleaved format. For example, you can
use it with the output from phylip's seqboot. If you use -n, FastTree
will write 1 tree per line to standard output.
""",
checker_function=_is_int,
equate=False,
),
...However, the application wrapper help text is a special case - in this example I see no reason to provide the text in the triple quote style with embedded line breaks. This would probably be better, allowing the line wrapping to be done later to suit the output mode:
def __init__(self, cmd="fasttree", **kwargs):
"""Initialize the class."""
self.parameters = [
_Switch(["-nt", "nt"],
"By default FastTree expects protein alignments, use -nt for nucleotides",
),
_Option(["-n", "n"],
"-n -- read N multiple alignments in. "
"This only works with phylip interleaved format. For example, you can "
"use it with the output from phylip's seqboot. If you use -n, FastTree "
"will write 1 tree per line to standard output.",
checker_function=_is_int,
equate=False,
),
...Related to this, many of the application wrapper property docstrings could be reviewed anyway for better display as RST on the formatted API documentation pages.
Other examples
This example from BioSQL is largely harmless since the extra white space in the resultant SQL command are meaningless:
sql = """
INSERT INTO bioentry (
biodatabase_id,
taxon_id,
name,
accession,
identifier,
division,
description,
version)
VALUES (
%s,
%s,
%s,
%s,
%s,
%s,
%s,
%s)"""Nevertheless, that could be re-written.
Another example is https://github.com/biopython/biopython/blob/biopython-175/Bio/AlignIO/FastaIO.py#L125 where the error message is over-indented compared to my intent as the original author - but the result is cosmetic and harmless.
Activity
graingert-osi commentedon Dec 3, 2019
regarding python stuffs
"The following to me looks wrong/ugly, but gives the desired output":
This is how to write it in a way that does "look nice", and "is doing what the author ... expected":
Fix triple-quote indentation; remove EMBOSS 6.1.0 refs
Fix triple-quote indentation; remove EMBOSS 6.1.0 refs
Fix triple-quote indentation; remove EMBOSS 6.1.0 refs
Fix triple-quote indentation; remove EMBOSS 6.1.0 refs
Fix triple-quote indentation; remove EMBOSS 6.1.0 refs
Fix triple quotes in Bio/Blast/Applications.py
Fix triple quotes in Bio/Blast/Applications.py
peterjc commentedon Nov 10, 2020
I've had a quick grep, and don't see any of these cases remaining. I am therefore closing this issue.
If there are any in the application wrappers and their multi-line help strings, that problem will go away if #3344 gets merged.