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