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
(text, prefix, predicate=None)
| 468 | |
| 469 | |
| 470 | def indent(text, prefix, predicate=None): |
| 471 | """Adds 'prefix' to the beginning of selected lines in 'text'. |
| 472 | |
| 473 | If 'predicate' is provided, 'prefix' will only be added to the lines |
| 474 | where 'predicate(line)' is True. If 'predicate' is not provided, |
| 475 | it will default to adding 'prefix' to all non-empty lines that do not |
| 476 | consist solely of whitespace characters. |
| 477 | """ |
| 478 | if predicate is None: |
| 479 | def predicate(line): |
| 480 | return line.strip() |
| 481 | |
| 482 | def prefixed_lines(): |
| 483 | for line in text.splitlines(True): |
| 484 | yield (prefix + line if predicate(line) else line) |
| 485 | return ''.join(prefixed_lines()) |
| 486 | |
| 487 | |
| 488 | if __name__ == "__main__": |
no test coverage detected