Skip to content

String utils

camel_case(string)

Convert a string to Camel Case by splitting on underscores and capitalizing each word.

Parameters:

Name Type Description Default
string str

The input string to be converted.

required

Returns:

Type Description
str

The converted Camel Case string.

Source code in blue/utils/string_utils.py
 9
10
11
12
13
14
15
16
17
18
19
20
def camel_case(string):
    """
    Convert a string to Camel Case by splitting on underscores and capitalizing each word.

    Parameters:
        string (str): The input string to be converted.

    Returns:
        (str): The converted Camel Case string.
    """
    words = string.split("_")
    return " ".join(word.capitalize() for word in words)

decode_websafe_no_padding(encoded_data)

Decodes a url-safe base64 string without padding.

Parameters:

Name Type Description Default
encoded_data str

input url-safe base64 string to be decoded

required

Raises:

Type Description
ValueError

If the input encoded_data is empty.

Returns:

Source code in blue/utils/string_utils.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def decode_websafe_no_padding(encoded_data: str) -> str:
    """Decodes a url-safe base64 string without padding.

    Parameters:
        encoded_data:  input url-safe base64 string to be decoded

    Raises:
        ValueError:  If the input encoded_data is empty.

    Returns:

    """
    if not pydash.is_empty(encoded_data):
        """decodes a url-safe base64 string (with or without padding)."""
        missing_padding = len(encoded_data) % 4
        if missing_padding:
            encoded_data += '=' * (4 - missing_padding)
        decoded_bytes = base64.urlsafe_b64decode(encoded_data)
        return decoded_bytes.decode('utf-8')
    raise ValueError('Empty encoded_data')

encode_websafe_no_padding(data)

Encodes a string to a url-safe base64 string without padding.

Parameters:

Name Type Description Default
data str

input string to be encoded

required

Raises:

Type Description
ValueError

If the input data is empty.

Returns:

Type Description
str

Url-safe base64 encoded string without padding.

Source code in blue/utils/string_utils.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def encode_websafe_no_padding(data: str) -> str:
    """Encodes a string to a url-safe base64 string without padding.

    Parameters:
        data: input string to be encoded

    Raises:
        ValueError: If the input data is empty.

    Returns:
        Url-safe base64 encoded string without padding.
    """
    if not pydash.is_empty(data):
        bytes_data = data.encode('utf-8')
        return base64.urlsafe_b64encode(bytes_data).decode("utf-8").rstrip('=')
    raise ValueError('Empty data')

remove_non_alphanumeric(input_string)

Remove non-alphanumeric characters from a string, also replaces spaces with underscores.

Parameters:

Name Type Description Default
input_string str

input string to be cleaned

required

Returns:

Type Description
str

Cleaned string with only alphanumeric characters and underscores.

Source code in blue/utils/string_utils.py
48
49
50
51
52
53
54
55
56
57
58
59
60
def remove_non_alphanumeric(input_string):
    """Remove non-alphanumeric characters from a string, also replaces spaces with underscores.

    Parameters:
        input_string (str): input string to be cleaned

    Returns:
        (str): Cleaned string with only alphanumeric characters and underscores.
    """
    # Use regex to remove all non-alphanumeric characters
    cleaned_string = re.sub(r"[^a-zA-Z0-9 ]", "", input_string)
    cleaned_string = cleaned_string.replace(" ", "_")
    return cleaned_string

safe_substitute(ts, **mappings)

Recursively substitute variables in a template string using both string.Template and Jinja2.

Parameters:

Name Type Description Default
ts str

The template string containing placeholders.

required
**mappings

Key-value pairs for substitution.

{}

Returns:

Type Description

The string with all placeholders substituted.

Source code in blue/utils/string_utils.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def safe_substitute(ts, **mappings):
    """
    Recursively substitute variables in a template string using both string.Template and Jinja2.

    Parameters:
        ts (str): The template string containing placeholders.
        **mappings: Key-value pairs for substitution.

    Returns:
        The string with all placeholders substituted.
    """
    ## basic string template first
    t = string.Template(ts)
    r = t.safe_substitute(**mappings)

    ## jinja
    e = Environment(loader=BaseLoader()).from_string(r)
    r = e.render(**mappings)

    if r == ts:
        return r
    else:
        return safe_substitute(r, **mappings)
Last update: 2025-10-05