You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defindent(text, prefix, predicate=None):
"""Adds 'prefix' to the beginning of selected lines in 'text'. If 'predicate' is provided, 'prefix' will only be added to the lines where 'predicate(line)' is True. If 'predicate' is not provided, it will default to adding 'prefix' to all non-empty lines that do not consist solely of whitespace characters. """ifpredicateisNone:
defpredicate(line):
returnline.strip()
defprefixed_lines():
forlineintext.splitlines(True):
yield (prefix+lineifpredicate(line) elseline)
return''.join(prefixed_lines())
predicate = str.strip is faster than def predicate(line)
''.join(x) converts input iterable to sequence. Using generator just makes overhead.
Activity
methane commentedon Jul 28, 2023
before: 2.8msec
after: 2.1msec (-25%)
gh-107369: optimize textwrap.indent() (#107374)
gh-107369: Optimise ``textwrap.indent()`` (#131923)
hroncok commentedon Apr 14, 2025
methane commentedon Apr 15, 2025
@hroncok This issue is about indent, not dedent.
See #131919 instead.
hroncok commentedon Apr 15, 2025
hroncok commentedon Apr 15, 2025
Moved to #131919 (comment)
methane commentedon Apr 15, 2025
It references #130167, not this issue.
#131923 references this issue because it makes textwrap.indent faster again. It is unrelated to textwrap.dedent.
hroncok commentedon Apr 15, 2025
Sorry for my confusion.