-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(main): implement paragraph choosing
- Loading branch information
Showing
2 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""Module with string-related utility function.""" | ||
|
||
|
||
def clip_text(text: str, max_length: int) -> str: | ||
""" | ||
Clip `text` if its length exceeds `max_length`. | ||
If the text was clipped, it has three dots `...` at the end. Otherwise, | ||
the text is returned unchanged. | ||
Args: | ||
text (str): Text to clip. | ||
max_length (int): Maximum allowed length. It will not be exceeded. | ||
Returns: | ||
str: Clipped text. | ||
""" | ||
if max_length <= 4: | ||
raise ValueError( | ||
'Minimal reasonable value of `max_length` is 4 (one character ' | ||
f'and three dots). Supplied value of {max_length}.' | ||
) | ||
text_length = len(text) | ||
if text_length > max_length: | ||
return text[: max_length - 3] + '...' | ||
return text |