[Python-ideas] New str whitespace cleaning method?
Joao S. O. Bueno
jsbueno at python.org.br
Tue May 21 14:07:29 CEST 2013
What about an STR method for just formatting whitespace?
I propose starting with something that would just replace all occurrences of
white-space sequences with a single white-space, and another mode that
does the same, but preserves newlines.
That way multiline strings could be used in a straight way to enter
any desired construct.
I know of textwrap.dedent - but it is not quite the same behavior, and this is a
case wher having method call postfixed to the string has clear advantages
over a function call with the same string - since the function name
would have to be placed between the "strign destiantion" - which
denotes its purpose, and the string text itself:
Ex.:
log.warn(dedent("""Hello dear sir,
I am sorry to inform you
the spanish inquisition
has arrived"""))
Against:
log.warn("""Hello dear sir,
I am sorry to inform you
the spanish inquisition
has arrived""".lint())
That would put everything on the same line as
per my proposal - calling "lint(newlines=True)" would
preserve the line breaks, and maybe ".lint(strict=True)"
would simply strip all whitespace before and after any newline
(as opposed to reduce it to a single white space)
In time: I have no good strong preference on such
method's name, but it rather be a short name. like
"lint" and not "space_lint()"
Justficative:
With the discussions going on about deprecating implicit string
concatenation, we
we are struggling around good alternatives for entering in code long strings -
with less typing, more readability, and more control over the final string.
(I just came around this code that has to mix in javascript snippets, and -
definitely, implicit concatenation does not make me happy -
still, there is no clear way out among the proposals so far:
https://github.com/collective/collective.z3cform.datetimewidget/blob/master/src/collective/z3cform/datetimewidget/widget_date.py
The poor options of current state is clearly visible there: in up to 4 lines,
the author keeps with multi-line implicit concatenation -
more lines than that, he just gives up and go for a multiline string -
with all the spacing issues on the resulting string.
(Imagine if they where Python snippets instead)
)
More information about the Python-ideas
mailing list