Skip to content
Snippets Groups Projects
Commit bcff2767 authored by Hugo Hörnquist's avatar Hugo Hörnquist
Browse files

Add string around procedures.

These will later be used for arrows pointing at errors.
parent a8346496
No related branches found
No related tags found
No related merge requests found
...@@ -56,3 +56,36 @@ def concatenate(lstlst: Iterable[Iterable[T]]) -> list[T]: ...@@ -56,3 +56,36 @@ def concatenate(lstlst: Iterable[Iterable[T]]) -> list[T]:
# result['extra'].update({k: result.pop(k) for k in custom_key}) # result['extra'].update({k: result.pop(k) for k in custom_key})
# #
# return msg, result # return msg, result
def string_around(string: str, point: int, back: int = 5, front: int = 5) -> str:
"""
Return substring around the point in the given string.
:param string:
The source string to extract a substring from.
:param point:
The focal which to center the string around.
:param back:
Number of characters to include before the point.
:param front:
Number of characters to incrlude after the point.
:return:
"""
tmp = string[point - back:point] \
+ string[point] \
+ string[point + 1:point + 1 + front]
return ''.join(chr(0x2400 + x if 0 <= x < 0x20
else 0x2421 if x == 127
else x) for x in tmp.encode('UTF-8'))
def len_before(string: str, point: int, back: int) -> int:
"""
Return length of string from point back to at most point chars.
:param string:
:param point:
:param back:
"""
return len(string[max(0, point - back):point])
from muppet.util import group_by, concatenate from muppet.util import (
group_by,
concatenate,
string_around,
len_before
)
def test_group_by(): def test_group_by():
...@@ -12,3 +17,15 @@ def test_group_by(): ...@@ -12,3 +17,15 @@ def test_group_by():
def test_concatenate(): def test_concatenate():
assert concatenate([[1, 2], [3, 4]]) == [1, 2, 3, 4] assert concatenate([[1, 2], [3, 4]]) == [1, 2, 3, 4]
assert concatenate([[1, [2]], [3, 4]]) == [1, [2], 3, 4] assert concatenate([[1, [2]], [3, 4]]) == [1, [2], 3, 4]
def test_string_around():
assert "5" == string_around("0123456789", 5, 0, 0)
assert "456" == string_around("0123456789", 5, 1, 1)
assert "3456" == string_around("0123456789", 5, 2, 1)
assert "4567" == string_around("0123456789", 5, 1, 2)
assert "0123456789" == string_around("0123456789", 5, 100, 100)
def test_len_before():
assert 5 == len_before("0123456789", 5, 100)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment