Skip to content

Commit 7945abf

Browse files
committed
Add DelimitedList to stringlist.py
1 parent 4394a83 commit 7945abf

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

domdf_python_tools/stringlist.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,23 @@ def with_indent_type(self, indent_type: str = "\t"):
416416
yield
417417
finally:
418418
self.indent_type = original_indent_type
419+
420+
421+
class DelimitedList(List[str]):
422+
"""
423+
Subclass of :class:`list` that supports custom delimiters in format strings.
424+
425+
**Example:**
426+
427+
.. code-block::
428+
429+
>>> l = DelimitedList([1, 2, 3, 4, 5])
430+
>>> format(l, ", ")
431+
'1, 2, 3, 4, 5'
432+
>>> f"Numbers: {l:, }"
433+
'Numbers: 1, 2, 3, 4, 5'
434+
435+
"""
436+
437+
def __format__(self, format_spec: str) -> str:
438+
return format_spec.join([str(x) for x in self])

0 commit comments

Comments
 (0)