Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
muppet-strings
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hugo Hörnquist
muppet-strings
Commits
bcff2767
Commit
bcff2767
authored
Sep 18, 2023
by
Hugo Hörnquist
Browse files
Options
Downloads
Patches
Plain Diff
Add string around procedures.
These will later be used for arrows pointing at errors.
parent
a8346496
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
muppet/util.py
+33
-0
33 additions, 0 deletions
muppet/util.py
tests/test_util.py
+18
-1
18 additions, 1 deletion
tests/test_util.py
with
51 additions
and
1 deletion
muppet/util.py
+
33
−
0
View file @
bcff2767
...
...
@@ -56,3 +56,36 @@ def concatenate(lstlst: Iterable[Iterable[T]]) -> list[T]:
# result['extra'].update({k: result.pop(k) for k in custom_key})
#
# 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
])
This diff is collapsed.
Click to expand it.
tests/test_util.py
+
18
−
1
View file @
bcff2767
from
muppet.util
import
group_by
,
concatenate
from
muppet.util
import
(
group_by
,
concatenate
,
string_around
,
len_before
)
def
test_group_by
():
...
...
@@ -12,3 +17,15 @@ def test_group_by():
def
test_concatenate
():
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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment