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.
Current code:
predicate = str.strip
is faster thandef predicate(line)
''.join(x)
converts input iterable to sequence. Using generator just makes overhead.prefix + line
is avoidable.Linked PRs
The text was updated successfully, but these errors were encountered: