Seite 1 von 1

Docstrings wrappen

Verfasst: Montag 28. Juni 2010, 07:04
von snafu
Der folgende Schnipsel ist zum Einrücken von Text, vornehmlich von Docstrings gedacht. `indent` ist die Anzahl der führenden Leerzeichen und die Breite ist defaultmäßig PEP8 konform eingestellt.

Code: Alles auswählen

from textwrap import fill

def wrap_text(s, indent=None, width=72, strip_chars='\n'):
    stripped = s.strip(strip_chars)
    if indent is None:
        indent = get_leading_spaces(stripped)
    clean = ' '.join(stripped.split())
    return fill(clean, width=width, initial_indent=' ' * indent,
                                 subsequent_indent=' ' * indent)

def get_leading_spaces(s):
    for (spaces, char) in enumerate(s):
        if not char == ' ':
            return spaces