Coverage for src / lstautorta / utils / string.py: 100%
26 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-10 11:56 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-08-10 11:56 +0000
1def split_string_around_template_var(string: str, var_identifier: str) -> list[str]:
2 """Return a list of substring in string, using `var_identifier{...}` as the delimiter, where ... can be anything
4 Parameters
5 ----------
6 string : str
7 Input string to split.
8 var_identifier : str
9 String used to indicate the start of a template variable. Eg "@" indicates that "@{t}" uses template variable `t`.
11 Returns
12 -------
13 List[str]
14 List of substrings in string, splitted using the template variables found with `var_identifier` as delimiter.
16 Examples
17 --------
18 >>> split_string_around_template_var("@{tel_id}_§{processIndex}_r0_dl1", "§")
19 ["@{tel_id}_", "_r0_dl1"]
20 """
21 # try to find delimiter
22 splitted_string = []
23 start = string.find(f"{var_identifier}{{")
24 if start < 0:
25 return [string]
27 # if we found the start, find the end of the template variable
28 end = string.find("}", start)
29 if end < 0:
30 # could not find ending "}": there won't be any more template vars in string
31 return [string]
32 # got a match: split start and run recusively on the remaining string
33 splitted_string.append(string[:start])
34 splitted_string.extend(split_string_around_template_var(string[end + 1 :], var_identifier))
35 return splitted_string
38def split_string_around_multiple_template_vars(string: str, var_identifiers: list[str]) -> list[str]:
39 """Returns a list of substring in string, using `var_identifier{...}` as the delimiter for all var_identifier
41 Parameters
42 ----------
43 string : str
44 String to split.
45 var_identifiers : List[str]
46 List of strings used to indicate the start of a template variable. For instance ["@", "$"] means that
47 "@{...}" and "${...}" will be used as delimiter to split the string.
49 Returns
50 -------
51 List[str]
52 List of substrings in string, splitted using the template variables found with `var_identifier` as delimiter.
53 """
54 # run split_string_around_template_var on the splitted string of previous delimiters
55 splitted_strings = [string]
56 for v in var_identifiers:
57 new_splitted_strings = []
58 for s in splitted_strings:
59 new_splitted_strings.extend(split_string_around_template_var(s, v))
60 splitted_strings = new_splitted_strings
61 return splitted_strings
64def substrings_in_string(string: str, substrings: list[str]) -> bool:
65 """Check if `substrings` appear in order in `string` (can be arbitrary content between substrings)
67 Parameters
68 ----------
69 string : str
70 String to check
71 substrings : List[str]
72 List of substrings to search in `string`
74 Returns
75 -------
76 bool
77 True if all sub-strings appear in order in `string`
78 """
79 for sub in substrings:
80 idx = string.find(sub)
81 if idx < 0:
82 return False
83 string = string[idx + len(sub) :]
85 return True