Skip to content

Strings

django_rubble.utils.strings

sort_title(title)

Sort a title by the first word, ignoring articles.

Articles include "a", "an", and "the".

Examples:

>>> sort_title("The Cat in the Hat")
'Cat in the Hat, The'
>>> sort_title("A Tale of Two Cities")
'Tale of Two Cities, A'
>>> sort_title("My Fair Lady")
'My Fair Lady'

Parameters:

Name Type Description Default
title str

The title to be sorted.

required

Returns:

Type Description
str

The sorted title.

Source code in django_rubble\utils\strings.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def sort_title(title: str) -> str:
    """Sort a title by the first word, ignoring articles.

    Articles include "a", "an", and "the".

    Examples:
        >>> sort_title("The Cat in the Hat")
        'Cat in the Hat, The'
        >>> sort_title("A Tale of Two Cities")
        'Tale of Two Cities, A'
        >>> sort_title("My Fair Lady")
        'My Fair Lady'

    Args:
        title: The title to be sorted.

    Returns:
        The sorted title.
    """
    articles = {"a", "an", "the"}

    title = title.lower()

    first, _, rest = title.partition(" ")
    return f"{rest}, {first}" if first in articles else title

truncate_secret(secret, *, max_length, mask='*', mask_short=False)

Truncate a secret to a maximum length and mask truncated characters.

The secret is truncated to the specified length by removing characters from the middle of the path.

Examples:

>>> truncate_secret("i72BPzV54LH7lwaez5F5BF9gRuvX5Phy", max_length=20, mask=".")
'i72BPzV...9gRuvX5Phy'
>>> truncate_secret("C:/Users/username/Documents/file.txt", max_length=30)
'i72BPzV54LH7***5F5BF9gRuvX5Phy'
>>> truncate_secret(
    "i72BPzV54LH7lwaez5F5BF9gRuvX5Phy",
    max_length=40,
    mask_short=True
)
'****************************************'
>>> truncate_secret(
    "i72B",
    max_length=8,
    mask_short=True
)
'********'

Parameters:

Name Type Description Default
secret str

The secret to be truncated.

required
max_length int

The maximum length of the truncated string.

required
mask str

The character to use for masking the truncated characters.

'*'
mask_short bool

Whether to mask the secret if it is already shorter than the maximum length.

False

Returns:

Name Type Description
str str

The truncated string.

Raises:

Type Description
ValueError

If the secret is already shorter than the maximum length and mask_short is False.

Source code in django_rubble\utils\strings.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def truncate_secret(
    secret: str, *, max_length: int, mask: str = "*", mask_short: bool = False
) -> str:
    """Truncate a secret to a maximum length and mask truncated characters.

    The secret is truncated to the specified length by removing characters from the
    middle of the path.

    Examples:
        >>> truncate_secret("i72BPzV54LH7lwaez5F5BF9gRuvX5Phy", max_length=20, mask=".")
        'i72BPzV...9gRuvX5Phy'
        >>> truncate_secret("C:/Users/username/Documents/file.txt", max_length=30)
        'i72BPzV54LH7***5F5BF9gRuvX5Phy'
        >>> truncate_secret(
            "i72BPzV54LH7lwaez5F5BF9gRuvX5Phy",
            max_length=40,
            mask_short=True
        )
        '****************************************'
        >>> truncate_secret(
            "i72B",
            max_length=8,
            mask_short=True
        )
        '********'

    Args:
        secret (str): The secret to be truncated.
        max_length (int): The maximum length of the truncated string.
        mask (str): The character to use for masking the truncated characters.
        mask_short (bool): Whether to mask the secret if it is already shorter than the
            maximum length.

    Returns:
        str: The truncated string.

    Raises:
        ValueError: If the secret is already shorter than the maximum length and
            mask_short is False.
    """

    if len(secret) <= max_length and not mask_short:
        msg = f"Secret is already shorter than max_length [{max_length}]"
        raise ValueError(msg)
    if len(secret) <= max_length:
        return mask * max_length

    tail_length = max_length // 2
    head_length = max_length - tail_length - 3

    head = secret[:head_length]
    tail = secret[-tail_length:]

    new_string = f"{head}{3 * mask}{tail}"

    assert len(new_string) <= max_length

    return new_string

truncate_string(string_input, num_char, *, postfix='...')

Truncate string and add postfix to end.

Examples:

>>> truncate_string("This is a long string", 10)
'This is...'
>>> truncate_string("This is a long string", 10, postfix="..")
'This is..'
>>> truncate_string("This is a long string", 20, postfix="..")
'This is a long str..'
>>> truncate_string("This is a long string", 25)
'This is a long string'

Parameters:

Name Type Description Default
string_input str

string to be truncated

required
num_char int

number of characters in returned string

required
postfix str

characters to use at end of string

'...'

Returns:

Type Description
str

Shortened string including postfix if truncation was required, else original string is returned.

Source code in django_rubble\utils\strings.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
def truncate_string(string_input: str, num_char: int, *, postfix: str = "...") -> str:
    """Truncate string and add postfix to end.

    Examples:
        >>> truncate_string("This is a long string", 10)
        'This is...'
        >>> truncate_string("This is a long string", 10, postfix="..")
        'This is..'
        >>> truncate_string("This is a long string", 20, postfix="..")
        'This is a long str..'
        >>> truncate_string("This is a long string", 25)
        'This is a long string'

    Args:
      string_input: string to be truncated
      num_char: number of characters in returned string
      postfix: characters to use at end of string

    Returns:
      Shortened string including postfix if truncation was required, else original
        string is returned.
    """
    string_input = string_input.strip()
    if len(string_input) > num_char:
        if len(postfix) >= num_char:
            return postfix[:num_char]
        string_length = num_char - len(postfix)
        new_string = string_input[:string_length]
        new_string = new_string.strip()
        return f"{new_string}{postfix}"

    return string_input