Coverage for /usr/local/lib/python3.12/site-packages/prefect/utilities/text.py: 24%

17 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 13:38 +0000

1import difflib 1a

2from collections.abc import Iterable 1a

3from typing import Optional 1a

4 

5 

6def truncated_to(length: int, value: Optional[str]) -> str: 1a

7 if not value: 

8 return "" 

9 

10 if len(value) <= length: 

11 return value 

12 

13 half = length // 2 

14 

15 beginning = value[:half] 

16 end = value[-half:] 

17 omitted = len(value) - (len(beginning) + len(end)) 

18 

19 proposed = f"{beginning}...{omitted} additional characters...{end}" 

20 

21 return proposed if len(proposed) < len(value) else value 

22 

23 

24def fuzzy_match_string( 1a

25 word: str, 

26 possibilities: Iterable[str], 

27 *, 

28 n: int = 1, 

29 cutoff: float = 0.6, 

30) -> Optional[str]: 

31 match = difflib.get_close_matches(word, possibilities, n=n, cutoff=cutoff) 

32 return match[0] if match else None