texted package

Module contents

class texted.Selection[source]

Bases: ABC

enumerate(lines: list[str]) Iterator[tuple[int, str]][source]

Iterate over the selected lines

texted.add_prefix(prefix: str, skip: Predicate | None = <texted._predicate._Predicate object>) Edition[source]

Add a fixed prefix string to every line in the selection for which the skip function does not evaluate to True. When no skip function is provided, blank lines are skipped.

texted.contains(part: str) _Predicate[source]

See str.__contains__.

>>> predicate = contains("world")
>>> predicate("hi, hello world!")
True
>>> predicate("hello José")
False
texted.edit(text, select, edition=None)[source]

Apply the operations to a text. You can stack a series of select operations, but only one edit operation is allowed.

>>> from texted import edit, remove_prefix, add_prefix, find, blank, contains
>>> new_text = edit(
...     "hello\n* world",
...     remove_prefix("* "),
... )
>>> print(new_text)
hello
world
>>> new_text = edit(
...     "hello\n* world",
...     find(blank),
...     add_prefix("% "),
... )  # No match, no change
>>> print(new_text)
hello
* world
>>> new_text = edit(
...     "hello\n\nworld",
...     add_prefix("%", skip=contains("hello")),
... )
>>> print(new_text)
hello
%
%world
>>> new_text = edit(
...     "hello\n\nworld",
...     add_prefix("%", skip=None),
... )
>>> print(new_text)
%hello
%
%world
texted.endswith(suffix: str) _Predicate[source]

See str.endswith.

>>> predicate = endswith("hello")
>>> predicate("hello world")
False
>>> predicate = endswith("world")
>>> predicate("hello world")
True
>>> predicate = startswith("hello") & ~endswith("world")
>>> predicate("hello world")
False
texted.everything(lines: list[str], base: slice | None) slice[source]
texted.find(pred: Predicate) _Select[source]

Select the first line for which the predicate function evaluates to True.

>>> select = find(lambda line: "write" in line)
>>> lines = 'import sys\n\nsys.stdout.write("hello world")\n'.splitlines()
>>> for i, line in select.enumerate(lines):
...     print(f"# {i} - {line}")
# 2 - sys.stdout.write("hello world")
texted.fullmatch(pattern: str, flags: int = 0) _Predicate[source]

See re.fullmatch.

>>> predicate = fullmatch("hello .*", re.I)
>>> predicate("hi, hello world!")
False
>>> predicate("hello José")
True
texted.glob(pattern: str, flags: int = 0) _Predicate[source]

See fnmatch.translate.

>>> predicate = fullmatch("hello .*", re.I)
>>> predicate = glob("he*")
>>> predicate("hi, hello world!")
False
>>> predicate("hello José")
True
texted.match(pattern: str, flags: int = 0) _Predicate[source]

See re.match.

>>> predicate = match("he.*", re.I)
>>> predicate("hi, hello world!")
False
>>> predicate("hello José")
True
texted.negate(fn: Predicate) _Predicate[source]

Logical not that can be applied to a predicate.

>>> predicate = lambda line: "python" in line.lower()
>>> predicate("Python is a programming language")
True
>>> opposite_predicate = negate(predicate)
>>> opposite_predicate("Python is a programming language")
False
texted.pred(fn: Callable[[str], Any]) _Predicate[source]

Create a Predicate object from any function fn(str)

texted.remove_prefix(prefix, skip: Predicate | None = <texted._predicate._Predicate object>) Edition[source]

Remove a fixed prefix string to every line in the selection for which the skip function does not evaluate to True. Please note that if the line does not start with the prefix, it is skipped.

texted.replace(fn: Callable[[str], str]) Edition[source]

Replace a chunk of text. The provided function will be called with the selected text as argument and its return value will be used as replacement.

texted.search(pattern: str, flags: int = 0) _Predicate[source]

See re.search.

>>> predicate = search("w.*", re.I)
>>> predicate("hi, hello WORLD!")
True
>>> predicate("hello José")
False
texted.startswith(prefix: str) _Predicate[source]

See str.startswith.

>>> predicate = startswith("hello")
>>> predicate("hello world")
True
>>> predicate("HELLO world")
False
>>> predicate = str.lower >> startswith("hello")
>>> predicate("HELLO WORLD")
True
texted.until(pred: Predicate) _SingleSelection[source]

Extend the current selection for contiguous lines stopping just before the predicate function evaluates to True.

>>> lines = "a b c d e f g h i j k l".split()
>>> select = until(lambda line: "f" in line)
>>> for i, line in select.enumerate(lines):
...     print(f"# {i} - {line}")
# 0 - a
# 1 - b
# 2 - c
# 3 - d
# 4 - e
>>> select = (
...     find(lambda line: "c" in line) >>  # select the first line
...     until(lambda line: "f" in line)    # add continuous lines
... )
>>> for i, line in select.enumerate(lines):
...     print(f"# {i} - {line}")
# 2 - c
# 3 - d
# 4 - e
texted.whilist(pred: Predicate) _SingleSelection[source]

Extend the current selection for contiguous lines while the predicate function evaluates to True.

>>> lines = "a b c d e f g h i j k l".split()
>>> select = whilist(lambda line: ord(line) < ord("f"))
>>> for i, line in select.enumerate(lines):
...     print(f"# {i} - {line}")
# 0 - a
# 1 - b
# 2 - c
# 3 - d
# 4 - e
>>> select = (
...     find(lambda line: "c" in line) >>         # select the first line
...     whilist(lambda l: ord(l) <= ord("d")) >>  # add continuous lines
...     whilist(lambda l: ord(l) > ord("f"))      # no continuous lines to add ...
... )
>>> for i, line in select.enumerate(lines):
...     print(f"# {i} - {line}")
# 2 - c
# 3 - d