stringlist
A list of strings that represent lines in a multiline string.
Changed in version 1.0.0: String should now be imported from domdf_python_tools.typing.
Classes:
|
Subclass of |
|
Represents an indent, having a symbol/type and a size. |
|
A list of strings that represent lines in a multiline string. |
Data:
Invariant |
Functions:
|
Given a list of two-element tuples, each containing a line and a newline character (or empty string), return a single string. |
|
Split |
-
class
DelimitedList(iterable=(), /)[source] Bases:
List[~_S]Subclass of
listthat supports custom delimiters in format strings.Example:
>>> l = DelimitedList([1, 2, 3, 4, 5]) >>> format(l, ", ") '1, 2, 3, 4, 5' >>> f"Numbers: {l:, }" 'Numbers: 1, 2, 3, 4, 5'
New in version 1.1.0.
-
class
Indent(size=0, type='\t')[source] Bases:
objectRepresents an indent, having a symbol/type and a size.
- Parameters
Methods:
__eq__(other)Return
self == other.__iter__()Returns the size and type of the
Indent.__repr__()Returns the string representation of the
Indent.__str__()Returns the
Indentas a string.Attributes:
The indent size.
The indent character.
-
class
StringList(iterable=(), convert_indents=False)[source] -
A list of strings that represent lines in a multiline string.
- Parameters
Methods:
Returns the
StringListas bytes.__eq__(other)Returns whether the other object is equal to this
StringList.__getitem__(index)Returns the line with the given index.
__setitem__(index, line)Replaces the given line with new content.
__str__()Returns the
StringListas a string.append(line)Append a line to the end of the
StringList.blankline([ensure_single])Append a blank line to the end of the
StringList.copy()Returns a shallow copy of the
StringList.Returns a count of the blank lines in the
StringList.extend(iterable)Extend the
StringListwith lines fromiterable.insert(index, line)Insert a line into the
StringListat the given position.set_indent(indent[, size])Sets the indent to insert at the beginning of new lines.
set_indent_size([size])Sets the size of the indent to insert at the beginning of new lines.
set_indent_type([indent_type])Sets the type of the indent to insert at the beginning of new lines.
splitlines([keepends])Analagous to
str.splitlines().with_indent(indent[, size])Context manager to temporarily use a different indent.
with_indent_size([size])Context manager to temporarily use a different indent size.
with_indent_type([indent_type])Context manager to temporarily use a different indent type.
Attributes:
Whether indents at the start of lines should be converted.
The indent to insert at the beginning of new lines.
The current indent size.
The current indent type.
-
__bytes__()[source] Returns the
StringListas bytes.New in version 2.1.0.
- Return type
-
__eq__(other)[source] Returns whether the other object is equal to this
StringList.- Return type
-
__getitem__(index)[source] Returns the line with the given index.
- Parameters
index (
Union[SupportsIndex,slice])- Return type
- Overloads
__getitem__(index:SupportsIndex) ->str__getitem__(index:slice) ->~_SL
Changed in version 1.8.0: Now returns a
StringListwhenindexis aslice.Changed in version 3.2.0: Changed
intin the type annotation toSupportsIndex.
-
__setitem__(index, line)[source] Replaces the given line with new content.
If the new content consists of multiple lines subsequent content in the
StringListwill be shifted down.Changed in version 3.2.0: Changed
intin the type annotation toSupportsIndex.- Overloads
__setitem__(index:SupportsIndex, line:String) ->None__setitem__(index:slice, line:Iterable[String] ) ->None
-
__str__()[source] Returns the
StringListas a string.- Return type
-
append(line)[source] Append a line to the end of the
StringList.- Parameters
line (
String)
-
blankline(ensure_single=False)[source] Append a blank line to the end of the
StringList.
-
convert_indents Type:
boolWhether indents at the start of lines should be converted.
Only applies to lines added after this is enabled/disabled.
Can only be used when the indent is
'\t'or'␣'.
-
copy()[source] Returns a shallow copy of the
StringList.Equivalent to
a[:].- Return type
-
count_blanklines()[source] Returns a count of the blank lines in the
StringList.New in version 0.7.1.
- Return type
-
extend(iterable)[source] Extend the
StringListwith lines fromiterable.- Parameters
iterable (
Iterable[String]) – An iterable of string-like objects to add to the end of theStringList.
-
insert(index, line)[source] Insert a line into the
StringListat the given position.- Parameters
index (
SupportsIndex)line (
String)
Changed in version 3.2.0: Changed
intin the type annotation toSupportsIndex.
-
set_indent_size(size=0)[source] Sets the size of the indent to insert at the beginning of new lines.
- Parameters
size (
int) – The indent size to use for new lines. Default0.
-
set_indent_type(indent_type='\t')[source] Sets the type of the indent to insert at the beginning of new lines.
- Parameters
indent_type (
str) – The type of indent to use for new lines. Default'\t'.
-
splitlines(keepends=False)[source] Analagous to
str.splitlines().New in version 3.8.0.
-
with_indent(indent, size=0)[source] Context manager to temporarily use a different indent.
>>> sl = StringList() >>> with sl.with_indent(" ", 1): ... sl.append("Hello World")
-
splitlines(string)[source] Split
stringinto a list of two-element tuples, containing the line content and the newline character(s), if any.New in version 3.2.0.
See also
-
joinlines(lines)[source] Given a list of two-element tuples, each containing a line and a newline character (or empty string), return a single string.
New in version 3.2.0.
See also
-
_SL= TypeVar(_SL, bound=StringList) Type:
TypeVarInvariant
TypeVarbound todomdf_python_tools.stringlist.StringList.