Coverage for lst_auto_rta/utils/string.py: 100%

27 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-17 14:47 +0000

1from typing import List 

2 

3 

4def split_string_around_template_var(string: str, var_identifier: str) -> List[str]: 

5 """Return a list of substring in string, using `var_identifier{...}` as the delimiter, where ... can be anything 

6 

7 Parameters 

8 ---------- 

9 string : str 

10 Input string to split. 

11 var_identifier : str 

12 String used to indicate the start of a template variable. Eg "@" indicates that "@{t}" uses template variable `t`. 

13 

14 Returns 

15 ------- 

16 List[str] 

17 List of substrings in string, splitted using the template variables found with `var_identifier` as delimiter. 

18 

19 Examples 

20 -------- 

21 >>> split_string_around_template_var("@{tel_id}_§{processIndex}_r0_dl1", "§") 

22 ["@{tel_id}_", "_r0_dl1"] 

23 """ 

24 # try to find delimiter 

25 splitted_string = [] 

26 start = string.find(f"{var_identifier}" "{") 

27 if start < 0: 

28 return [string] 

29 

30 # if we found the start, find the end of the template variable 

31 end = string.find("}", start) 

32 if end < 0: 

33 # could not find ending "}": there won't be any more template vars in string 

34 return [string] 

35 else: 

36 # got a match: split start and run recusively on the remaining string 

37 splitted_string.append(string[:start]) 

38 splitted_string.extend(split_string_around_template_var(string[end + 1 :], var_identifier)) 

39 return splitted_string 

40 

41 

42def split_string_around_multiple_template_vars(string: str, var_identifiers: List[str]) -> List[str]: 

43 """Returns a list of substring in string, using `var_identifier{...}` as the delimiter for all var_identifier 

44 

45 Parameters 

46 ---------- 

47 string : str 

48 String to split. 

49 var_identifiers : List[str] 

50 List of strings used to indicate the start of a template variable. For instance ["@", "$"] means that 

51 "@{...}" and "${...}" will be used as delimiter to split the string. 

52 

53 Returns 

54 ------- 

55 List[str] 

56 List of substrings in string, splitted using the template variables found with `var_identifier` as delimiter. 

57 """ 

58 # run split_string_around_template_var on the splitted string of previous delimiters 

59 splitted_strings = [string] 

60 for v in var_identifiers: 

61 new_splitted_strings = [] 

62 for s in splitted_strings: 

63 new_splitted_strings.extend(split_string_around_template_var(s, v)) 

64 splitted_strings = new_splitted_strings 

65 return splitted_strings 

66 

67 

68def substrings_in_string(string: str, substrings: List[str]) -> bool: 

69 """Check if `substrings` appear in order in `string` (can be arbitrary content between substrings) 

70 

71 Parameters 

72 ---------- 

73 string : str 

74 String to check 

75 substrings : List[str] 

76 List of substrings to search in `string` 

77 

78 Returns 

79 ------- 

80 bool 

81 True if all sub-strings appear in order in `string` 

82 """ 

83 

84 for sub in substrings: 

85 idx = string.find(sub) 

86 if idx < 0: 

87 return False 

88 else: 

89 string = string[idx + len(sub) :] 

90 

91 return True