Skip to content

Commit 4178176

Browse files
committed
Add splitlines method to StringList for textwrap.indent
1 parent e3b2529 commit 4178176

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

domdf_python_tools/stringlist.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,18 @@ def with_indent_type(self, indent_type: str = '\t'):
463463
finally:
464464
self.indent_type = original_indent_type
465465

466+
def splitlines(self, keepends: bool = False) -> List[str]:
467+
"""
468+
Analagous to :meth:`str.splitlines`.
469+
470+
.. versionadded:: 3.8.0
471+
"""
472+
473+
if keepends:
474+
return [line + '\n' for line in self]
475+
else:
476+
return self
477+
466478

467479
class DelimitedList(List[_S]):
468480
"""

tests/test_stringlist.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# stdlib
22
import pickle
3+
import textwrap
34
from textwrap import dedent
5+
from typing import no_type_check
46

57
# 3rd party
68
import pytest
@@ -558,3 +560,11 @@ def test_splitlines(string, lines):
558560
@joinlines_splitlines_param
559561
def test_joinlines(string, lines):
560562
assert string == joinlines(lines)
563+
564+
565+
@no_type_check
566+
def test_textwrap_indent():
567+
sl = StringList(['', '', "hello", "world", '', '', '', "1234"])
568+
assert textwrap.indent(sl, " ") == "\n\n hello\n world\n\n\n\n 1234\n"
569+
assert textwrap.indent(sl, '\t') == "\n\n\thello\n\tworld\n\n\n\n\t1234\n"
570+
assert textwrap.indent(sl, ">>> ") == "\n\n>>> hello\n>>> world\n\n\n\n>>> 1234\n"

0 commit comments

Comments
 (0)