Source code for lst_auto_rta.utils.string

from typing import List


[docs] def split_string_around_template_var(string: str, var_identifier: str) -> List[str]: """Return a list of substring in string, using `var_identifier{...}` as the delimiter, where ... can be anything Parameters ---------- string : str Input string to split. var_identifier : str String used to indicate the start of a template variable. Eg "@" indicates that "@{t}" uses template variable `t`. Returns ------- List[str] List of substrings in string, splitted using the template variables found with `var_identifier` as delimiter. Examples -------- >>> split_string_around_template_var("@{tel_id}_§{processIndex}_r0_dl1", "§") ["@{tel_id}_", "_r0_dl1"] """ # try to find delimiter splitted_string = [] start = string.find(f"{var_identifier}" "{") if start < 0: return [string] # if we found the start, find the end of the template variable end = string.find("}", start) if end < 0: # could not find ending "}": there won't be any more template vars in string return [string] else: # got a match: split start and run recusively on the remaining string splitted_string.append(string[:start]) splitted_string.extend(split_string_around_template_var(string[end + 1 :], var_identifier)) return splitted_string
[docs] def split_string_around_multiple_template_vars(string: str, var_identifiers: List[str]) -> List[str]: """Returns a list of substring in string, using `var_identifier{...}` as the delimiter for all var_identifier Parameters ---------- string : str String to split. var_identifiers : List[str] List of strings used to indicate the start of a template variable. For instance ["@", "$"] means that "@{...}" and "${...}" will be used as delimiter to split the string. Returns ------- List[str] List of substrings in string, splitted using the template variables found with `var_identifier` as delimiter. """ # run split_string_around_template_var on the splitted string of previous delimiters splitted_strings = [string] for v in var_identifiers: new_splitted_strings = [] for s in splitted_strings: new_splitted_strings.extend(split_string_around_template_var(s, v)) splitted_strings = new_splitted_strings return splitted_strings
[docs] def substrings_in_string(string: str, substrings: List[str]) -> bool: """Check if `substrings` appear in order in `string` (can be arbitrary content between substrings) Parameters ---------- string : str String to check substrings : List[str] List of substrings to search in `string` Returns ------- bool True if all sub-strings appear in order in `string` """ for sub in substrings: idx = string.find(sub) if idx < 0: return False else: string = string[idx + len(sub) :] return True