Skip to content

lstautorta.utils.string

Functions:

Name Description
split_string_around_multiple_template_vars

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

split_string_around_template_var

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

substrings_in_string

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

split_string_around_multiple_template_vars

split_string_around_multiple_template_vars(string, var_identifiers)

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

Parameters:

Name Type Description Default
string str

String to split.

required
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.

required

Returns:

Type Description
List[str]

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

Source code in src/lstautorta/utils/string.py
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

split_string_around_template_var

split_string_around_template_var(string, var_identifier)

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

Parameters:

Name Type Description Default
string str

Input string to split.

required
var_identifier str

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

required

Returns:

Type Description
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"]
Source code in src/lstautorta/utils/string.py
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]
    # 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

substrings_in_string

substrings_in_string(string, substrings)

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

Parameters:

Name Type Description Default
string str

String to check

required
substrings List[str]

List of substrings to search in string

required

Returns:

Type Description
bool

True if all sub-strings appear in order in string

Source code in src/lstautorta/utils/string.py
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
        string = string[idx + len(sub) :]

    return True