Utils¶
The helpers/utils subpackage provides utility classes with static methods for common operations including date/time
handling, string manipulation, file I/O, JWT tokens, passwords, TOTP, Keycloak integration, OpenTelemetry, and
application-level utilities.
Base Utils¶
Base utility class providing foundational helpers shared across other utility classes.
Shared base utility helpers.
archipy.helpers.utils.base_utils.IRANIAN_MOBILE_LOCAL_LEN
module-attribute
¶
archipy.helpers.utils.base_utils.IRANIAN_NATIONAL_CODE_LEN
module-attribute
¶
archipy.helpers.utils.base_utils.NATIONAL_CODE_CHECKSUM_THRESHOLD
module-attribute
¶
archipy.helpers.utils.base_utils.NATIONAL_CODE_MODULUS
module-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils ¶
Bases: ErrorUtils, DatetimeUtils, PasswordUtils, JWTUtils, TOTPUtils, FileUtils, StringUtils
A utility class that combines multiple utility functionalities into a single class.
This class inherits from various utility classes to provide a centralized place for common utility methods.
Source code in archipy/helpers/utils/base_utils.py
24 25 26 27 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 86 87 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
archipy.helpers.utils.base_utils.BaseUtils.arabic_vowel_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_akoolad_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_be_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_pe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_te_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_se_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_jim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_che_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_he_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_khe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_dal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_zal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_re_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_ze_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_zhe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_sin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_shin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_sad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_zad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_ta_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_za_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_eyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_gheyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_fe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_ghaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_kaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_gaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_lam_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_mim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_nun_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_vav_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_ha_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.alphabet_ye_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table1
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table2
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table3
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table4
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table5
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table7
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table8
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table9
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.ascii_punctuation_patterns
class-attribute
instance-attribute
¶
ascii_punctuation_patterns: list[
tuple[Pattern[str], str]
] = compile_patterns(
[
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff]),|,(?=\\s*[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])",
"،",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff\\u061f])\\?",
"؟",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff\\u061b]);",
"؛",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])%",
"٪",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])_(?=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])",
"ـ",
),
]
)
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table12
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_translate_table13
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.character_refinement_patterns
class-attribute
instance-attribute
¶
character_refinement_patterns: list[
tuple[Pattern[str], str]
] = compile_patterns(
[(" +", " "), ("\\n\\n+", "\n"), (" ?\\.\\.\\.", " …")]
)
archipy.helpers.utils.base_utils.BaseUtils.punctuation_after
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_before
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_spacing_patterns
class-attribute
instance-attribute
¶
punctuation_spacing_patterns = compile_patterns(
[
(f" ([{punctuation_after}])", "\\1"),
(f"([{punctuation_before}]) ", "\\1"),
(
f"(?<![A-Za-z0-9])([{punctuation_after[:3]}])([^ {punctuation_after}"
+ "\\d])",
"\\1 \\2",
),
(
f"([{punctuation_after[3:]}])([^ {punctuation_after}])",
"\\1 \\2",
),
(
f"([^ {punctuation_before}])([{punctuation_before}])",
"\\1 \\2",
),
]
)
archipy.helpers.utils.base_utils.BaseUtils.number_zero_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_one_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_two_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_three_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_four_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_five_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_six_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_seven_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_eight_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.number_nine_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.base_utils.BaseUtils.punctuation_persian_marks_to_space_translate_table
class-attribute
instance-attribute
¶
punctuation_persian_marks_to_space_translate_table = (
str.maketrans(
dict.fromkeys(".:!،؛؟»])}«[({-ـ٪!'\"#+/,?;%_", " ")
)
)
archipy.helpers.utils.base_utils.BaseUtils.sanitize_iranian_landline_or_phone_number
staticmethod
¶
Sanitize an Iranian landline or mobile phone number.
Removes non-numeric characters and standardizes the format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
landline_or_phone_number
|
str
|
The phone number to sanitize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The sanitized phone number in a standardized format. |
Source code in archipy/helpers/utils/base_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_iranian_phone_number
classmethod
¶
Validates an Iranian mobile phone number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phone_number
|
str
|
The phone number to validate. |
required |
Raises:
| Type | Description |
|---|---|
InvalidPhoneNumberError
|
If the phone number is invalid. |
Source code in archipy/helpers/utils/base_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_iranian_landline_number
classmethod
¶
Validates an Iranian landline number.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
landline_number
|
str
|
The landline number to validate. |
required |
Raises:
| Type | Description |
|---|---|
InvalidLandlineNumberError
|
If the landline number is invalid. |
Source code in archipy/helpers/utils/base_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_iranian_national_code_pattern
classmethod
¶
Validates an Iranian National ID number using the official algorithm.
To see how the algorithm works, see http://www.aliarash.com/article/codemeli/codemeli.htm
The algorithm works by: 1. Checking if the ID is exactly 10 digits 2. Multiplying each digit (except the last) by its position weight 3. Summing these products 4. Calculating the remainder when divided by 11 5. Comparing the check digit based on specific rules
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
national_code
|
str
|
A string containing the national ID to validate. |
required |
Raises:
| Type | Description |
|---|---|
InvalidNationalCodeError
|
If the ID is invalid due to length or checksum. |
Source code in archipy/helpers/utils/base_utils.py
archipy.helpers.utils.base_utils.BaseUtils.remove_arabic_vowels
classmethod
¶
Removes Arabic vowels (tashkeel) from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Arabic vowels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Arabic vowels removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.normalize_persian_chars
classmethod
¶
Normalizes Persian characters to their standard forms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Persian characters. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Persian characters normalized. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.normalize_punctuation
classmethod
¶
Normalizes punctuation marks in the text.
ASCII ,, ?, ;, % and _ are converted to their Persian forms only next to Persian text, so
numbers (1,000), URLs, e-mails and identifiers are left intact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing punctuation marks. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with punctuation marks normalized. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.normalize_numbers
classmethod
¶
Normalizes numbers in the text to English format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with numbers normalized to English format. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.clean_spacing
classmethod
¶
Cleans up spacing issues in the text, such as non-breaking spaces and zero-width non-joiners.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text with spacing issues. |
required |
keep_zwnj
|
bool
|
Keep zero-width non-joiners, which Persian words such as "میخواهم" need. When
|
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with spacing cleaned up. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.normalize_punctuation_spacing
classmethod
¶
Applies proper spacing around punctuation marks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text with punctuation spacing issues. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with proper spacing around punctuation marks. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.remove_punctuation_marks
classmethod
¶
Removes punctuation marks from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing punctuation marks. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with punctuation marks removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.mask_urls
classmethod
¶
Masks URLs in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing URLs. |
required |
mask
|
str | None
|
The mask to replace URLs with. Defaults to "MASK_URL". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with URLs masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.mask_emails
classmethod
¶
Masks email addresses in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing email addresses. |
required |
mask
|
str | None
|
The mask to replace emails with. Defaults to "MASK_EMAIL". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with email addresses masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.mask_phones
classmethod
¶
Masks phone numbers in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing phone numbers. |
required |
mask
|
str | None
|
The mask to replace phone numbers with. Defaults to "MASK_PHONE". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with phone numbers masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.convert_english_number_to_persian
classmethod
¶
Converts English numbers to Persian numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing English numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with English numbers converted to Persian numbers. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.convert_numbers_to_english
classmethod
¶
Converts Persian/Arabic numbers to English numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Persian/Arabic numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Persian/Arabic numbers converted to English numbers. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.convert_add_3digit_delimiter
classmethod
¶
Adds thousand separators to numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
The number to format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted number with thousand separators. Non-integer values are returned as |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.remove_emoji
classmethod
¶
Removes emoji characters from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing emojis. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with emojis removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.replace_currencies_with_mask
classmethod
¶
Masks currency symbols in the text (the amounts next to them are not masked).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing currency symbols. |
required |
mask
|
str | None
|
The mask to replace currency symbols with. Defaults to "MASK_CURRENCIES". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with currency symbols masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.replace_numbers_with_mask
classmethod
¶
Masks numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing numbers. |
required |
mask
|
str | None
|
The mask to replace numbers with. Defaults to "MASK_NUMBERS". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with numbers masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.is_string_none_or_empty
classmethod
¶
Checks if a string is None or empty (after stripping whitespace).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str | None
|
The input string to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.normalize_persian_text
classmethod
¶
normalize_persian_text(
text: str,
*,
remove_vowels: bool = True,
normalize_punctuation: bool = True,
normalize_numbers: bool = True,
normalize_persian_chars: bool = True,
mask_urls: bool = False,
mask_emails: bool = False,
mask_phones: bool = False,
mask_currencies: bool = False,
mask_all_numbers: bool = False,
remove_emojis: bool = False,
url_mask: str | None = None,
email_mask: str | None = None,
phone_mask: str | None = None,
currency_mask: str | None = None,
number_mask: str | None = None,
clean_spacing: bool = True,
remove_punctuation: bool = False,
normalize_punctuation_spacing: bool = False,
keep_zwnj: bool = False,
) -> str
Normalizes Persian text with configurable options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text to normalize. |
required |
remove_vowels
|
bool
|
Whether to remove Arabic vowels. Defaults to |
True
|
normalize_punctuation
|
bool
|
Whether to normalize punctuation marks. Defaults to |
True
|
normalize_numbers
|
bool
|
Whether to normalize numbers to English format. Defaults to |
True
|
normalize_persian_chars
|
bool
|
Whether to normalize Persian characters. Defaults to |
True
|
mask_urls
|
bool
|
Whether to mask URLs. Defaults to |
False
|
mask_emails
|
bool
|
Whether to mask email addresses. Defaults to |
False
|
mask_phones
|
bool
|
Whether to mask phone numbers. Defaults to |
False
|
mask_currencies
|
bool
|
Whether to mask currency symbols and amounts. Defaults to |
False
|
mask_all_numbers
|
bool
|
Whether to mask all numbers. Defaults to |
False
|
remove_emojis
|
bool
|
Whether to remove emojis. Defaults to |
False
|
url_mask
|
str | None
|
The mask to replace URLs with. Defaults to |
None
|
email_mask
|
str | None
|
The mask to replace email addresses with. Defaults to |
None
|
phone_mask
|
str | None
|
The mask to replace phone numbers with. Defaults to |
None
|
currency_mask
|
str | None
|
The mask to replace currency symbols and amounts with. Defaults to |
None
|
number_mask
|
str | None
|
The mask to replace numbers with. Defaults to |
None
|
clean_spacing
|
bool
|
Whether to clean up spacing issues. Defaults to |
True
|
remove_punctuation
|
bool
|
Whether to remove punctuation marks. Defaults to |
False
|
normalize_punctuation_spacing
|
bool
|
Whether to apply proper spacing around
punctuation marks. Defaults to |
False
|
keep_zwnj
|
bool
|
Whether to keep zero-width non-joiners when cleaning spacing instead of turning them
into spaces. Defaults to |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The normalized text. |
Source code in archipy/helpers/utils/string_utils.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
archipy.helpers.utils.base_utils.BaseUtils.snake_to_camel_case
classmethod
¶
Converts snake_case to camelCase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text in snake_case format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text converted to camelCase format. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.camel_to_snake_case
classmethod
¶
Converts camelCase to snake_case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text in camelCase format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text converted to snake_case format. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.base_utils.BaseUtils.create_secure_link
classmethod
¶
create_secure_link(
path: str,
minutes: int | None = None,
file_config: FileConfig | None = None,
) -> str
Creates a secure link with expiration for file access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The file path to create a secure link for. |
required |
minutes
|
int | None
|
Number of minutes until link expiration.
Defaults to the config's |
None
|
file_config
|
FileConfig | None
|
Optional file configuration object. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A secure link with a hash and expiration timestamp. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the |
OutOfRangeError
|
If |
Source code in archipy/helpers/utils/file_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_file_name
classmethod
¶
Validates a file name based on allowed extensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
The file name to validate. |
required |
file_config
|
FileConfig | None
|
Optional file configuration object. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If |
Source code in archipy/helpers/utils/file_utils.py
archipy.helpers.utils.base_utils.BaseUtils.generate_totp
classmethod
¶
Generates a TOTP code using the configured hash algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret
|
str | UUID
|
The secret key used to generate the TOTP code. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A tuple containing the generated TOTP code and its expiration time. The expiration |
datetime
|
is |
tuple[str, datetime]
|
stays valid for that whole period: this helper keeps no state, so callers must enforce single use and |
tuple[str, datetime]
|
attempt limits ( |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the secret is invalid or empty. |
Source code in archipy/helpers/utils/totp_utils.py
archipy.helpers.utils.base_utils.BaseUtils.verify_totp
classmethod
¶
verify_totp(
secret: str | UUID,
totp_code: str,
auth_config: AuthConfig | None = None,
replay_guard: Callable[[int], bool] | None = None,
) -> bool
Verifies a TOTP code against the provided secret.
This helper keeps no state. Two protections therefore belong to the caller: counting failed attempts
(TOTP_MAX_ATTEMPTS / TOTP_LOCKOUT_TIME are not enforced here) and rejecting a code that was already
used. For the second one pass replay_guard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret
|
str | UUID
|
The secret key used to generate the TOTP code. |
required |
totp_code
|
str
|
The TOTP code to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
replay_guard
|
Callable[[int], bool] | None
|
Optional callback invoked with the matched time step (an |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the secret is invalid or empty. |
InvalidTokenError
|
If the TOTP code format is invalid. |
Source code in archipy/helpers/utils/totp_utils.py
archipy.helpers.utils.base_utils.BaseUtils.generate_secret_key_for_totp
staticmethod
¶
Generates a random secret key for TOTP initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A base32-encoded secret key for TOTP initialization. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the TOTP_SECRET_KEY is not configured. |
InternalError
|
If there is an error generating the secret key. |
Source code in archipy/helpers/utils/totp_utils.py
archipy.helpers.utils.base_utils.BaseUtils.create_token
classmethod
¶
create_token(
data: Mapping[str, object],
expires_in: int,
additional_claims: Mapping[str, object] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates a JWT token with enhanced security features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[str, object]
|
Base claims data to include in the token. |
required |
expires_in
|
int
|
Token expiration time in seconds. |
required |
additional_claims
|
Mapping[str, object] | None
|
Optional additional claims to include in the token. They
cannot redefine |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded JWT token. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If data is empty, the expiration is invalid, |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.create_access_token
classmethod
¶
create_access_token(
user_uuid: UUID,
additional_claims: Mapping[str, object] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates an access token for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_uuid
|
UUID
|
The user's UUID to include in the token. |
required |
additional_claims
|
Mapping[str, object] | None
|
Optional additional claims to include in the token. |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded access token. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.create_refresh_token
classmethod
¶
create_refresh_token(
user_uuid: UUID,
additional_claims: Mapping[str, object] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates a refresh token for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_uuid
|
UUID
|
The user's UUID to include in the token. |
required |
additional_claims
|
Mapping[str, object] | None
|
Optional additional claims to include in the token. |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded refresh token. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.decode_token
classmethod
¶
decode_token(
token: str,
verify_type: str | None = None,
auth_config: AuthConfig | None = None,
) -> dict[str, object]
Decodes and verifies a JWT token with enhanced security checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The JWT token to decode. |
required |
verify_type
|
str | None
|
Optional token type to verify (e.g., "access" or "refresh"). |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
dict[str, object]: The decoded token payload. |
Raises:
| Type | Description |
|---|---|
TokenExpiredError
|
If the token has expired. |
InvalidTokenError
|
If the token is invalid (e.g., invalid signature, audience, issuer, or type). |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.verify_access_token
classmethod
¶
Verifies an access token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The access token to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
dict[str, object]: The decoded access token payload. |
Raises:
| Type | Description |
|---|---|
InvalidTokenError
|
If the token is invalid or not an access token. |
TokenExpiredError
|
If the token has expired. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.verify_refresh_token
classmethod
¶
Verifies a refresh token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The refresh token to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
dict[str, object]: The decoded refresh token payload. |
Raises:
| Type | Description |
|---|---|
InvalidTokenError
|
If the token is invalid or not a refresh token. |
TokenExpiredError
|
If the token has expired. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.extract_user_uuid
staticmethod
¶
Extracts the user UUID from the token payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
Mapping[str, object]
|
The decoded token payload. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
UUID |
UUID
|
The user's UUID. |
Raises:
| Type | Description |
|---|---|
InvalidTokenError
|
If the user identifier is invalid or missing. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_token_expiry
classmethod
¶
Gets the token expiry timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The JWT token. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The token expiry timestamp in seconds. |
Raises:
| Type | Description |
|---|---|
InvalidTokenError
|
If the token is invalid. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.base_utils.BaseUtils.hash_password
staticmethod
¶
Hashes a password using PBKDF2 with SHA256.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to hash. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A self-describing string in the format |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.base_utils.BaseUtils.verify_password
staticmethod
¶
verify_password(
password: str,
stored_password: str,
auth_config: AuthConfig | None = None,
) -> bool
Verifies a password against a stored hash.
Both the current pbkdf2_sha256$... format and the legacy format (base64 of salt + hash, written by
earlier versions) are accepted. Legacy hashes carry no parameters, so they are tried with the configured
HASH_ITERATIONS and, if that differs, with the former default of 100000.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to verify. |
required |
stored_password
|
str
|
The stored password hash to compare against. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the password matches the stored hash, False otherwise. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_password
staticmethod
¶
Validates a password against the password policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to validate. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidPasswordError
|
If the password does not meet the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.base_utils.BaseUtils.generate_password
staticmethod
¶
Generates a random password that meets the policy requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A randomly generated password that meets the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.base_utils.BaseUtils.validate_password_history
classmethod
¶
validate_password_history(
new_password: str,
password_history: list[str],
auth_config: AuthConfig | None = None,
lang: LanguageType | None = None,
) -> None
Validates a new password against the password history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_password
|
str
|
The new password to validate. |
required |
password_history
|
list[str]
|
A list of previous password hashes. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
lang
|
LanguageType
|
The language to use for error messages. Defaults to Persian. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidPasswordError
|
If the new password has been used recently or does not meet the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.base_utils.BaseUtils.convert_to_jalali
staticmethod
¶
Converts a Gregorian date to a Jalali (Persian) date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_date
|
date
|
The Gregorian date to convert. |
required |
Returns:
| Type | Description |
|---|---|
date
|
jdatetime.date: The corresponding Jalali date. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.is_holiday_in_iran
classmethod
¶
Determines if the target date is a holiday in Iran.
This method leverages caching and an external API to check if the given date is a holiday.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_date
|
date
|
The date to check for holiday status. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the date is a holiday, False otherwise. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.ensure_timezone_aware
classmethod
¶
Ensures a datetime object is timezone-aware, converting it to UTC if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
datetime
|
The datetime object to make timezone-aware. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The timezone-aware datetime object. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.daterange
classmethod
¶
Generates a range of dates from start_date to end_date, exclusive of end_date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime
|
The start date of the range. |
required |
end_date
|
datetime
|
The end date of the range. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
date |
Generator[date]
|
Each date in the range. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_string_datetime_from_datetime
classmethod
¶
Converts a datetime object to a formatted string. Default format is ISO 8601.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
datetime
|
The datetime object to format. |
required |
format_
|
str | None
|
The format string. If None, uses ISO 8601. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.standardize_string_datetime
classmethod
¶
Standardizes a datetime string to the default format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_string
|
str
|
The datetime string to standardize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The standardized datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_datetime_from_string_datetime
classmethod
¶
Parses a string to a datetime object using the given format, or ISO 8601 by default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_string
|
str
|
The datetime string to parse. |
required |
format_
|
str | None
|
The format string. If None, uses ISO 8601. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The parsed timezone-aware datetime. Strings without an offset are read as UTC. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_string_datetime_now
classmethod
¶
Gets the current datetime as a formatted string. Default format is ISO 8601.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_datetime_now
classmethod
¶
Gets the current local datetime.
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The current local datetime. |
archipy.helpers.utils.base_utils.BaseUtils.get_datetime_utc_now
classmethod
¶
Gets the current UTC datetime.
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The current UTC datetime. |
archipy.helpers.utils.base_utils.BaseUtils.get_epoch_time_now
classmethod
¶
Gets the current time in seconds since the epoch.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The current epoch time. |
archipy.helpers.utils.base_utils.BaseUtils.get_datetime_before_given_datetime_or_now
classmethod
¶
get_datetime_before_given_datetime_or_now(
weeks: int = 0,
days: int = 0,
hours: int = 0,
minutes: int = 0,
seconds: int = 0,
datetime_given: datetime | None = None,
) -> datetime
Subtracts time from a given datetime or the current datetime if not specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weeks
|
int
|
The number of weeks to subtract. |
0
|
days
|
int
|
The number of days to subtract. |
0
|
hours
|
int
|
The number of hours to subtract. |
0
|
minutes
|
int
|
The number of minutes to subtract. |
0
|
seconds
|
int
|
The number of seconds to subtract. |
0
|
datetime_given
|
datetime | None
|
The datetime to subtract from. If None, uses the current datetime. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The resulting datetime after subtraction. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_datetime_after_given_datetime_or_now
classmethod
¶
get_datetime_after_given_datetime_or_now(
weeks: int = 0,
days: int = 0,
hours: int = 0,
minutes: int = 0,
seconds: int = 0,
datetime_given: datetime | None = None,
) -> datetime
Adds time to a given datetime or the current datetime if not specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weeks
|
int
|
The number of weeks to add. |
0
|
days
|
int
|
The number of days to add. |
0
|
hours
|
int
|
The number of hours to add. |
0
|
minutes
|
int
|
The number of minutes to add. |
0
|
seconds
|
int
|
The number of seconds to add. |
0
|
datetime_given
|
datetime | None
|
The datetime to add to. If None, uses the current datetime. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The resulting datetime after addition. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.base_utils.BaseUtils.capture_exception
staticmethod
¶
Deprecated: use archipy.helpers.errors.capture_exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseException
|
The exception to capture and report. |
required |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.base_utils.BaseUtils.async_handle_fastapi_exception
async
staticmethod
¶
Deprecated: use archipy.helpers.errors.fastapi.handle_fastapi_exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_request
|
Request
|
The incoming FastAPI request. |
required |
exception
|
BaseError
|
The exception to handle. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
JSONResponse |
JSONResponse
|
A JSON response containing the exception details. |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.base_utils.BaseUtils.get_fastapi_exception_responses
staticmethod
¶
get_fastapi_exception_responses(
exceptions: list[type[BaseError]],
) -> dict[int, dict[str, object]]
Deprecated: use archipy.helpers.errors.fastapi.get_fastapi_exception_responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exceptions
|
list[type[BaseError]]
|
A list of exception types to generate responses for. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, dict[str, object]]
|
dict[int, dict[str, object]]: A dictionary mapping HTTP status codes to their corresponding response schemas. |
Source code in archipy/helpers/utils/error_utils.py
options: show_root_toc_entry: false heading_level: 3
App Utils¶
Application-level utilities for runtime environment inspection and process management.
Application bootstrap and lifecycle utilities.
archipy.helpers.utils.app_utils.CreateGrpcServerType
module-attribute
¶
archipy.helpers.utils.app_utils.create_grpc_server
module-attribute
¶
archipy.helpers.utils.app_utils.FastAPIUtils ¶
Utility class for FastAPI configuration and setup.
Source code in archipy/helpers/utils/app_utils.py
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 86 87 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
archipy.helpers.utils.app_utils.FastAPIUtils.custom_generate_unique_id
staticmethod
¶
Generates a unique ID for API routes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
route
|
APIRoute
|
The route for which to generate a unique ID. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A unique ID for the route. |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils.setup_cors
staticmethod
¶
Configures CORS middleware.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
config
|
BaseConfig
|
The configuration object containing CORS settings. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils.setup_gzip
staticmethod
¶
Configures GZip response compression middleware if enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
config
|
BaseConfig
|
The configuration object containing GZip middleware settings. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils.setup_trusted_host
staticmethod
¶
Configures TrustedHost middleware if enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
config
|
BaseConfig
|
The configuration object containing TrustedHost middleware settings. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils.setup_https_redirect
staticmethod
¶
Configures HTTPS redirect middleware if enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
The FastAPI application instance. |
required |
config
|
BaseConfig
|
The configuration object containing HTTPS redirect middleware settings. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.FastAPIUtils.build_otel_telemetry_config
staticmethod
¶
Build the telemetry argument for FastAPI(...) from ArchiPy OTel settings.
FastAPI instruments requests natively, so the config must be passed to the constructor.
Providers come from OtelUtils and are never replaced by globals. Signals without a
real provider are disabled. auto_configure is off because ArchiPy owns the exporters.
FastAPI log records (which carry stack traces) are opt-in via OTEL.FASTAPI_LOGS_ENABLED.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
Application configuration containing OTel settings. |
required |
Returns:
| Type | Description |
|---|---|
TelemetryConfig
|
Telemetry settings for |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AsyncGrpcAPIUtils ¶
async grpc api utilities.
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AsyncGrpcAPIUtils.setup_otel_interceptor
staticmethod
¶
Configure OpenTelemetry server interceptor for an async gRPC server.
Inserts the OTel interceptor at position 0 so it wraps later interceptors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
Application configuration containing OTel settings. |
required |
interceptors
|
list[Any]
|
Mutable list of gRPC interceptors. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AsyncGrpcAPIUtils.setup_rate_limit_interceptor
staticmethod
¶
Configures rate-limit interceptor for async gRPC server when enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
The configuration object containing gRPC rate-limit settings. |
required |
interceptors
|
List
|
List of gRPC interceptors to add the rate-limit interceptor to. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.GrpcAPIUtils ¶
grpc api utilities.
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.GrpcAPIUtils.setup_otel_interceptor
staticmethod
¶
Configure OpenTelemetry server interceptor for a sync gRPC server.
Inserts the OTel interceptor at position 0 so it wraps later interceptors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
Application configuration containing OTel settings. |
required |
interceptors
|
list[Any]
|
Mutable list of gRPC interceptors. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.GrpcAPIUtils.setup_rate_limit_interceptor
staticmethod
¶
Configures rate-limit interceptor for gRPC server when enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
The configuration object containing gRPC rate-limit settings. |
required |
interceptors
|
List
|
List of gRPC interceptors to add the rate-limit interceptor to. |
required |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AppUtils ¶
Utility class for creating and configuring FastAPI applications.
Source code in archipy/helpers/utils/app_utils.py
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 | |
archipy.helpers.utils.app_utils.AppUtils.create_fastapi_app
classmethod
¶
create_fastapi_app(
config: BaseConfig | None = None,
*,
configure_exception_handlers: bool = True,
include_common_responses: bool = True,
lifespan: Callable[..., AbstractAsyncContextManager]
| None = None,
) -> FastAPI
Create and configure a FastAPI application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig | None
|
Custom configuration. If not provided, uses global config. |
None
|
configure_exception_handlers
|
bool
|
Whether to configure exception handlers. Defaults to True. |
True
|
include_common_responses
|
bool
|
Whether to configure common response definitions for all endpoints. Defaults to True. |
True
|
lifespan
|
Callable[..., AbstractAsyncContextManager] | None
|
Custom lifespan context manager for the app. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
FastAPI |
FastAPI
|
The configured FastAPI application instance. |
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AppUtils.create_async_grpc_app
classmethod
¶
create_async_grpc_app(
config: BaseConfig,
customized_interceptors: Sequence[object]
| Set[object]
| None = None,
compression: Compression | None = None,
*,
validation_errors_as_internal: bool = False,
) -> GrpcAioServer
Create and configure an async gRPC application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
The application config. |
required |
customized_interceptors
|
Sequence[object] | Set[object] | None
|
Extra interceptors appended after the built-in ones. |
None
|
compression
|
Compression | None
|
The server compression. |
None
|
validation_errors_as_internal
|
bool
|
Report a pydantic |
False
|
Source code in archipy/helpers/utils/app_utils.py
archipy.helpers.utils.app_utils.AppUtils.create_grpc_app
classmethod
¶
create_grpc_app(
config: BaseConfig,
customized_interceptors: Sequence[object]
| Set[object]
| None = None,
compression: Compression | None = None,
*,
validation_errors_as_internal: bool = False,
) -> grpc.Server
Create and configure a synchronous gRPC server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
The application config. |
required |
customized_interceptors
|
Sequence[object] | Set[object] | None
|
Extra interceptors appended after the built-in ones. |
None
|
compression
|
Compression | None
|
The server compression. |
None
|
validation_errors_as_internal
|
bool
|
Report a pydantic |
False
|
Source code in archipy/helpers/utils/app_utils.py
options: show_root_toc_entry: false heading_level: 3
Datetime Utils¶
Utilities for timezone-aware date and time operations with microsecond precision.
Date and time utility helpers.
archipy.helpers.utils.datetime_utils.DatetimeUtils ¶
A utility class for handling date and time operations, including conversions, caching, and API integrations.
This class provides methods for working with both Gregorian and Jalali (Persian) calendars, as well as utility functions for timezone-aware datetime objects, date ranges, and string formatting.
Source code in archipy/helpers/utils/datetime_utils.py
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 86 87 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 | |
archipy.helpers.utils.datetime_utils.DatetimeUtils.convert_to_jalali
staticmethod
¶
Converts a Gregorian date to a Jalali (Persian) date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_date
|
date
|
The Gregorian date to convert. |
required |
Returns:
| Type | Description |
|---|---|
date
|
jdatetime.date: The corresponding Jalali date. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.is_holiday_in_iran
classmethod
¶
Determines if the target date is a holiday in Iran.
This method leverages caching and an external API to check if the given date is a holiday.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target_date
|
date
|
The date to check for holiday status. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the date is a holiday, False otherwise. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.ensure_timezone_aware
classmethod
¶
Ensures a datetime object is timezone-aware, converting it to UTC if necessary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
datetime
|
The datetime object to make timezone-aware. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The timezone-aware datetime object. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.daterange
classmethod
¶
Generates a range of dates from start_date to end_date, exclusive of end_date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime
|
The start date of the range. |
required |
end_date
|
datetime
|
The end date of the range. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
date |
Generator[date]
|
Each date in the range. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_string_datetime_from_datetime
classmethod
¶
Converts a datetime object to a formatted string. Default format is ISO 8601.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
datetime
|
The datetime object to format. |
required |
format_
|
str | None
|
The format string. If None, uses ISO 8601. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.standardize_string_datetime
classmethod
¶
Standardizes a datetime string to the default format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_string
|
str
|
The datetime string to standardize. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The standardized datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_datetime_from_string_datetime
classmethod
¶
Parses a string to a datetime object using the given format, or ISO 8601 by default.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date_string
|
str
|
The datetime string to parse. |
required |
format_
|
str | None
|
The format string. If None, uses ISO 8601. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The parsed timezone-aware datetime. Strings without an offset are read as UTC. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_string_datetime_now
classmethod
¶
Gets the current datetime as a formatted string. Default format is ISO 8601.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted datetime string. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_datetime_now
classmethod
¶
Gets the current local datetime.
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The current local datetime. |
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_datetime_utc_now
classmethod
¶
Gets the current UTC datetime.
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The current UTC datetime. |
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_epoch_time_now
classmethod
¶
Gets the current time in seconds since the epoch.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The current epoch time. |
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_datetime_before_given_datetime_or_now
classmethod
¶
get_datetime_before_given_datetime_or_now(
weeks: int = 0,
days: int = 0,
hours: int = 0,
minutes: int = 0,
seconds: int = 0,
datetime_given: datetime | None = None,
) -> datetime
Subtracts time from a given datetime or the current datetime if not specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weeks
|
int
|
The number of weeks to subtract. |
0
|
days
|
int
|
The number of days to subtract. |
0
|
hours
|
int
|
The number of hours to subtract. |
0
|
minutes
|
int
|
The number of minutes to subtract. |
0
|
seconds
|
int
|
The number of seconds to subtract. |
0
|
datetime_given
|
datetime | None
|
The datetime to subtract from. If None, uses the current datetime. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The resulting datetime after subtraction. |
Source code in archipy/helpers/utils/datetime_utils.py
archipy.helpers.utils.datetime_utils.DatetimeUtils.get_datetime_after_given_datetime_or_now
classmethod
¶
get_datetime_after_given_datetime_or_now(
weeks: int = 0,
days: int = 0,
hours: int = 0,
minutes: int = 0,
seconds: int = 0,
datetime_given: datetime | None = None,
) -> datetime
Adds time to a given datetime or the current datetime if not specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weeks
|
int
|
The number of weeks to add. |
0
|
days
|
int
|
The number of days to add. |
0
|
hours
|
int
|
The number of hours to add. |
0
|
minutes
|
int
|
The number of minutes to add. |
0
|
seconds
|
int
|
The number of seconds to add. |
0
|
datetime_given
|
datetime | None
|
The datetime to add to. If None, uses the current datetime. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
datetime |
datetime
|
The resulting datetime after addition. |
Source code in archipy/helpers/utils/datetime_utils.py
options: show_root_toc_entry: false heading_level: 3
String Utils¶
Utilities for string manipulation including slugification, truncation, random string generation, and HTML sanitization.
String manipulation utility helpers.
archipy.helpers.utils.string_utils.StringUtils ¶
Bases: StringUtilsConstants
String utilities for text normalization, cleaning, and masking.
This class provides methods for handling Persian and Arabic text, including normalization, punctuation cleaning, number conversion, and masking of sensitive information like URLs, emails, and phone numbers.
Source code in archipy/helpers/utils/string_utils.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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 86 87 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 | |
archipy.helpers.utils.string_utils.StringUtils.arabic_vowel_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_akoolad_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_be_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_pe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_te_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_se_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_jim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_che_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_he_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_khe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_dal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_zal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_re_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_ze_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_zhe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_sin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_shin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_sad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_zad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_ta_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_za_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_eyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_gheyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_fe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_ghaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_kaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_gaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_lam_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_mim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_nun_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_vav_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_ha_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.alphabet_ye_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table1
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table2
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table3
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table4
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table5
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table7
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table8
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table9
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.ascii_punctuation_patterns
class-attribute
instance-attribute
¶
ascii_punctuation_patterns: list[
tuple[Pattern[str], str]
] = compile_patterns(
[
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff]),|,(?=\\s*[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])",
"،",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff\\u061f])\\?",
"؟",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff\\u061b]);",
"؛",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])%",
"٪",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])_(?=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])",
"ـ",
),
]
)
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table12
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_translate_table13
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.character_refinement_patterns
class-attribute
instance-attribute
¶
character_refinement_patterns: list[
tuple[Pattern[str], str]
] = compile_patterns(
[(" +", " "), ("\\n\\n+", "\n"), (" ?\\.\\.\\.", " …")]
)
archipy.helpers.utils.string_utils.StringUtils.punctuation_after
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_before
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_spacing_patterns
class-attribute
instance-attribute
¶
punctuation_spacing_patterns = compile_patterns(
[
(f" ([{punctuation_after}])", "\\1"),
(f"([{punctuation_before}]) ", "\\1"),
(
f"(?<![A-Za-z0-9])([{punctuation_after[:3]}])([^ {punctuation_after}"
+ "\\d])",
"\\1 \\2",
),
(
f"([{punctuation_after[3:]}])([^ {punctuation_after}])",
"\\1 \\2",
),
(
f"([^ {punctuation_before}])([{punctuation_before}])",
"\\1 \\2",
),
]
)
archipy.helpers.utils.string_utils.StringUtils.number_zero_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_one_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_two_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_three_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_four_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_five_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_six_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_seven_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_eight_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.number_nine_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils.StringUtils.punctuation_persian_marks_to_space_translate_table
class-attribute
instance-attribute
¶
punctuation_persian_marks_to_space_translate_table = (
str.maketrans(
dict.fromkeys(".:!،؛؟»])}«[({-ـ٪!'\"#+/,?;%_", " ")
)
)
archipy.helpers.utils.string_utils.StringUtils.remove_arabic_vowels
classmethod
¶
Removes Arabic vowels (tashkeel) from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Arabic vowels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Arabic vowels removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.normalize_persian_chars
classmethod
¶
Normalizes Persian characters to their standard forms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Persian characters. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Persian characters normalized. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.normalize_punctuation
classmethod
¶
Normalizes punctuation marks in the text.
ASCII ,, ?, ;, % and _ are converted to their Persian forms only next to Persian text, so
numbers (1,000), URLs, e-mails and identifiers are left intact.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing punctuation marks. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with punctuation marks normalized. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.normalize_numbers
classmethod
¶
Normalizes numbers in the text to English format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with numbers normalized to English format. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.clean_spacing
classmethod
¶
Cleans up spacing issues in the text, such as non-breaking spaces and zero-width non-joiners.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text with spacing issues. |
required |
keep_zwnj
|
bool
|
Keep zero-width non-joiners, which Persian words such as "میخواهم" need. When
|
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with spacing cleaned up. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.normalize_punctuation_spacing
classmethod
¶
Applies proper spacing around punctuation marks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text with punctuation spacing issues. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with proper spacing around punctuation marks. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.remove_punctuation_marks
classmethod
¶
Removes punctuation marks from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing punctuation marks. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with punctuation marks removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.mask_urls
classmethod
¶
Masks URLs in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing URLs. |
required |
mask
|
str | None
|
The mask to replace URLs with. Defaults to "MASK_URL". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with URLs masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.mask_emails
classmethod
¶
Masks email addresses in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing email addresses. |
required |
mask
|
str | None
|
The mask to replace emails with. Defaults to "MASK_EMAIL". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with email addresses masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.mask_phones
classmethod
¶
Masks phone numbers in the text with a specified mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing phone numbers. |
required |
mask
|
str | None
|
The mask to replace phone numbers with. Defaults to "MASK_PHONE". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with phone numbers masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.convert_english_number_to_persian
classmethod
¶
Converts English numbers to Persian numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing English numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with English numbers converted to Persian numbers. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.convert_numbers_to_english
classmethod
¶
Converts Persian/Arabic numbers to English numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing Persian/Arabic numbers. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with Persian/Arabic numbers converted to English numbers. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.convert_add_3digit_delimiter
classmethod
¶
Adds thousand separators to numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
The number to format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The formatted number with thousand separators. Non-integer values are returned as |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.remove_emoji
classmethod
¶
Removes emoji characters from the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing emojis. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with emojis removed. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.replace_currencies_with_mask
classmethod
¶
Masks currency symbols in the text (the amounts next to them are not masked).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing currency symbols. |
required |
mask
|
str | None
|
The mask to replace currency symbols with. Defaults to "MASK_CURRENCIES". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with currency symbols masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.replace_numbers_with_mask
classmethod
¶
Masks numbers in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text containing numbers. |
required |
mask
|
str | None
|
The mask to replace numbers with. Defaults to "MASK_NUMBERS". |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text with numbers masked. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.is_string_none_or_empty
classmethod
¶
Checks if a string is None or empty (after stripping whitespace).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str | None
|
The input string to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.normalize_persian_text
classmethod
¶
normalize_persian_text(
text: str,
*,
remove_vowels: bool = True,
normalize_punctuation: bool = True,
normalize_numbers: bool = True,
normalize_persian_chars: bool = True,
mask_urls: bool = False,
mask_emails: bool = False,
mask_phones: bool = False,
mask_currencies: bool = False,
mask_all_numbers: bool = False,
remove_emojis: bool = False,
url_mask: str | None = None,
email_mask: str | None = None,
phone_mask: str | None = None,
currency_mask: str | None = None,
number_mask: str | None = None,
clean_spacing: bool = True,
remove_punctuation: bool = False,
normalize_punctuation_spacing: bool = False,
keep_zwnj: bool = False,
) -> str
Normalizes Persian text with configurable options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text to normalize. |
required |
remove_vowels
|
bool
|
Whether to remove Arabic vowels. Defaults to |
True
|
normalize_punctuation
|
bool
|
Whether to normalize punctuation marks. Defaults to |
True
|
normalize_numbers
|
bool
|
Whether to normalize numbers to English format. Defaults to |
True
|
normalize_persian_chars
|
bool
|
Whether to normalize Persian characters. Defaults to |
True
|
mask_urls
|
bool
|
Whether to mask URLs. Defaults to |
False
|
mask_emails
|
bool
|
Whether to mask email addresses. Defaults to |
False
|
mask_phones
|
bool
|
Whether to mask phone numbers. Defaults to |
False
|
mask_currencies
|
bool
|
Whether to mask currency symbols and amounts. Defaults to |
False
|
mask_all_numbers
|
bool
|
Whether to mask all numbers. Defaults to |
False
|
remove_emojis
|
bool
|
Whether to remove emojis. Defaults to |
False
|
url_mask
|
str | None
|
The mask to replace URLs with. Defaults to |
None
|
email_mask
|
str | None
|
The mask to replace email addresses with. Defaults to |
None
|
phone_mask
|
str | None
|
The mask to replace phone numbers with. Defaults to |
None
|
currency_mask
|
str | None
|
The mask to replace currency symbols and amounts with. Defaults to |
None
|
number_mask
|
str | None
|
The mask to replace numbers with. Defaults to |
None
|
clean_spacing
|
bool
|
Whether to clean up spacing issues. Defaults to |
True
|
remove_punctuation
|
bool
|
Whether to remove punctuation marks. Defaults to |
False
|
normalize_punctuation_spacing
|
bool
|
Whether to apply proper spacing around
punctuation marks. Defaults to |
False
|
keep_zwnj
|
bool
|
Whether to keep zero-width non-joiners when cleaning spacing instead of turning them
into spaces. Defaults to |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The normalized text. |
Source code in archipy/helpers/utils/string_utils.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
archipy.helpers.utils.string_utils.StringUtils.snake_to_camel_case
classmethod
¶
Converts snake_case to camelCase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text in snake_case format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text converted to camelCase format. |
Source code in archipy/helpers/utils/string_utils.py
archipy.helpers.utils.string_utils.StringUtils.camel_to_snake_case
classmethod
¶
Converts camelCase to snake_case.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The input text in camelCase format. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The text converted to snake_case format. |
Source code in archipy/helpers/utils/string_utils.py
options: show_root_toc_entry: false heading_level: 3
String Utils Constants¶
Constants used by string_utils for character sets, patterns, and limits.
Constants for string utility helpers.
archipy.helpers.utils.string_utils_constants.StringUtilsConstants ¶
Constants for string utility operations including translation tables and regex patterns.
Source code in archipy/helpers/utils/string_utils_constants.py
19 20 21 22 23 24 25 26 27 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 86 87 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | |
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.arabic_vowel_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_akoolad_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_alef_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_be_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_pe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_te_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_se_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_jim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_che_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_he_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_khe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_dal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_zal_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_re_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_ze_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_zhe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_sin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_shin_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_sad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_zad_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_ta_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_za_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_eyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_gheyn_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_fe_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_ghaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_kaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_gaf_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_lam_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_mim_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_nun_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_vav_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_ha_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.alphabet_ye_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table1
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table2
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table3
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table4
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table5
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table7
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table8
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table9
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.ascii_punctuation_patterns
class-attribute
instance-attribute
¶
ascii_punctuation_patterns: list[
tuple[Pattern[str], str]
] = compile_patterns(
[
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff]),|,(?=\\s*[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])",
"،",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff\\u061f])\\?",
"؟",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff\\u061b]);",
"؛",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])%",
"٪",
),
(
"(?<=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])_(?=[\\u0600-\\u06ff\\ufb50-\\ufdff\\ufe70-\\ufeff])",
"ـ",
),
]
)
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table12
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_translate_table13
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.character_refinement_patterns
class-attribute
instance-attribute
¶
character_refinement_patterns: list[
tuple[Pattern[str], str]
] = compile_patterns(
[(" +", " "), ("\\n\\n+", "\n"), (" ?\\.\\.\\.", " …")]
)
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_after
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_before
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_spacing_patterns
class-attribute
instance-attribute
¶
punctuation_spacing_patterns = compile_patterns(
[
(f" ([{punctuation_after}])", "\\1"),
(f"([{punctuation_before}]) ", "\\1"),
(
f"(?<![A-Za-z0-9])([{punctuation_after[:3]}])([^ {punctuation_after}"
+ "\\d])",
"\\1 \\2",
),
(
f"([{punctuation_after[3:]}])([^ {punctuation_after}])",
"\\1 \\2",
),
(
f"([^ {punctuation_before}])([{punctuation_before}])",
"\\1 \\2",
),
]
)
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_zero_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_one_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_two_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_three_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_four_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_five_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_six_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_seven_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_eight_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.number_nine_translate_table
class-attribute
instance-attribute
¶
archipy.helpers.utils.string_utils_constants.StringUtilsConstants.punctuation_persian_marks_to_space_translate_table
class-attribute
instance-attribute
¶
punctuation_persian_marks_to_space_translate_table = (
str.maketrans(
dict.fromkeys(".:!،؛؟»])}«[({-ـ٪!'\"#+/,?;%_", " ")
)
)
archipy.helpers.utils.string_utils_constants.compile_patterns ¶
Compile regex patterns with their replacement strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
patterns
|
list[tuple[str, str]]
|
List of tuples containing (pattern, replacement) pairs. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[Pattern[str], str]]
|
List of tuples containing (compiled_pattern, replacement) pairs. |
Source code in archipy/helpers/utils/string_utils_constants.py
options: show_root_toc_entry: false heading_level: 3
File Utils¶
Utilities for file operations including reading, writing, hashing, and type validation.
File system utility helpers.
archipy.helpers.utils.file_utils.FileUtils ¶
A utility class for handling file-related operations, such as creating secure links and validating file names.
Source code in archipy/helpers/utils/file_utils.py
18 19 20 21 22 23 24 25 26 27 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 86 87 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 | |
archipy.helpers.utils.file_utils.FileUtils.create_secure_link
classmethod
¶
create_secure_link(
path: str,
minutes: int | None = None,
file_config: FileConfig | None = None,
) -> str
Creates a secure link with expiration for file access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The file path to create a secure link for. |
required |
minutes
|
int | None
|
Number of minutes until link expiration.
Defaults to the config's |
None
|
file_config
|
FileConfig | None
|
Optional file configuration object. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A secure link with a hash and expiration timestamp. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the |
OutOfRangeError
|
If |
Source code in archipy/helpers/utils/file_utils.py
archipy.helpers.utils.file_utils.FileUtils.validate_file_name
classmethod
¶
Validates a file name based on allowed extensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
The file name to validate. |
required |
file_config
|
FileConfig | None
|
Optional file configuration object. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If |
Source code in archipy/helpers/utils/file_utils.py
options: show_root_toc_entry: false heading_level: 3
Error Utils¶
Deprecated wrappers (capture_exception, async_handle_fastapi_exception, get_fastapi_exception_responses)
that redirect to archipy.helpers.errors. They will be removed in 6.0.0.
Deprecated error handling helpers; use archipy.helpers.errors instead.
archipy.helpers.utils.error_utils.ErrorUtils ¶
Deprecated wrappers kept for BaseUtils callers; each method names its replacement in the warning.
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.error_utils.ErrorUtils.capture_exception
staticmethod
¶
Deprecated: use archipy.helpers.errors.capture_exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseException
|
The exception to capture and report. |
required |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.error_utils.ErrorUtils.async_handle_fastapi_exception
async
staticmethod
¶
Deprecated: use archipy.helpers.errors.fastapi.handle_fastapi_exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_request
|
Request
|
The incoming FastAPI request. |
required |
exception
|
BaseError
|
The exception to handle. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
JSONResponse |
JSONResponse
|
A JSON response containing the exception details. |
Source code in archipy/helpers/utils/error_utils.py
archipy.helpers.utils.error_utils.ErrorUtils.get_fastapi_exception_responses
staticmethod
¶
get_fastapi_exception_responses(
exceptions: list[type[BaseError]],
) -> dict[int, dict[str, object]]
Deprecated: use archipy.helpers.errors.fastapi.get_fastapi_exception_responses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exceptions
|
list[type[BaseError]]
|
A list of exception types to generate responses for. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, dict[str, object]]
|
dict[int, dict[str, object]]: A dictionary mapping HTTP status codes to their corresponding response schemas. |
Source code in archipy/helpers/utils/error_utils.py
options: show_root_toc_entry: false heading_level: 3
JWT Utils¶
Utilities for JWT generation, verification, and decoding with configurable signing algorithms and expiration.
Utility module for JWT token operations with enhanced security and datetime handling.
This module provides a robust JWT handling implementation with support for access and refresh tokens, cryptographic security, token validation, and comprehensive error handling.
archipy.helpers.utils.jwt_utils.JWTUtils ¶
Utility class for JWT token operations with enhanced security and datetime handling.
Source code in archipy/helpers/utils/jwt_utils.py
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 86 87 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | |
archipy.helpers.utils.jwt_utils.JWTUtils.create_token
classmethod
¶
create_token(
data: Mapping[str, object],
expires_in: int,
additional_claims: Mapping[str, object] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates a JWT token with enhanced security features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Mapping[str, object]
|
Base claims data to include in the token. |
required |
expires_in
|
int
|
Token expiration time in seconds. |
required |
additional_claims
|
Mapping[str, object] | None
|
Optional additional claims to include in the token. They
cannot redefine |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded JWT token. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If data is empty, the expiration is invalid, |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.create_access_token
classmethod
¶
create_access_token(
user_uuid: UUID,
additional_claims: Mapping[str, object] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates an access token for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_uuid
|
UUID
|
The user's UUID to include in the token. |
required |
additional_claims
|
Mapping[str, object] | None
|
Optional additional claims to include in the token. |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded access token. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.create_refresh_token
classmethod
¶
create_refresh_token(
user_uuid: UUID,
additional_claims: Mapping[str, object] | None = None,
auth_config: AuthConfig | None = None,
) -> str
Creates a refresh token for a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_uuid
|
UUID
|
The user's UUID to include in the token. |
required |
additional_claims
|
Mapping[str, object] | None
|
Optional additional claims to include in the token. |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The encoded refresh token. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.decode_token
classmethod
¶
decode_token(
token: str,
verify_type: str | None = None,
auth_config: AuthConfig | None = None,
) -> dict[str, object]
Decodes and verifies a JWT token with enhanced security checks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The JWT token to decode. |
required |
verify_type
|
str | None
|
Optional token type to verify (e.g., "access" or "refresh"). |
None
|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
dict[str, object]: The decoded token payload. |
Raises:
| Type | Description |
|---|---|
TokenExpiredError
|
If the token has expired. |
InvalidTokenError
|
If the token is invalid (e.g., invalid signature, audience, issuer, or type). |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.verify_access_token
classmethod
¶
Verifies an access token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The access token to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
dict[str, object]: The decoded access token payload. |
Raises:
| Type | Description |
|---|---|
InvalidTokenError
|
If the token is invalid or not an access token. |
TokenExpiredError
|
If the token has expired. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.verify_refresh_token
classmethod
¶
Verifies a refresh token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The refresh token to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
dict[str, object]: The decoded refresh token payload. |
Raises:
| Type | Description |
|---|---|
InvalidTokenError
|
If the token is invalid or not a refresh token. |
TokenExpiredError
|
If the token has expired. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.extract_user_uuid
staticmethod
¶
Extracts the user UUID from the token payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payload
|
Mapping[str, object]
|
The decoded token payload. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
UUID |
UUID
|
The user's UUID. |
Raises:
| Type | Description |
|---|---|
InvalidTokenError
|
If the user identifier is invalid or missing. |
Source code in archipy/helpers/utils/jwt_utils.py
archipy.helpers.utils.jwt_utils.JWTUtils.get_token_expiry
classmethod
¶
Gets the token expiry timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
str
|
The JWT token. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The token expiry timestamp in seconds. |
Raises:
| Type | Description |
|---|---|
InvalidTokenError
|
If the token is invalid. |
Source code in archipy/helpers/utils/jwt_utils.py
options: show_root_toc_entry: false heading_level: 3
Password Utils¶
Utilities for secure password hashing, verification, generation, and strength validation with timing-attack protection.
Password hashing and validation utilities.
archipy.helpers.utils.password_utils.PasswordUtils ¶
A utility class for handling password-related operations, such as hashing, verification, and validation.
Source code in archipy/helpers/utils/password_utils.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 86 87 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
archipy.helpers.utils.password_utils.PasswordUtils.hash_password
staticmethod
¶
Hashes a password using PBKDF2 with SHA256.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to hash. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A self-describing string in the format |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.password_utils.PasswordUtils.verify_password
staticmethod
¶
verify_password(
password: str,
stored_password: str,
auth_config: AuthConfig | None = None,
) -> bool
Verifies a password against a stored hash.
Both the current pbkdf2_sha256$... format and the legacy format (base64 of salt + hash, written by
earlier versions) are accepted. Legacy hashes carry no parameters, so they are tried with the configured
HASH_ITERATIONS and, if that differs, with the former default of 100000.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to verify. |
required |
stored_password
|
str
|
The stored password hash to compare against. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the password matches the stored hash, False otherwise. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.password_utils.PasswordUtils.validate_password
staticmethod
¶
Validates a password against the password policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
password
|
str
|
The password to validate. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidPasswordError
|
If the password does not meet the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.password_utils.PasswordUtils.generate_password
staticmethod
¶
Generates a random password that meets the policy requirements.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A randomly generated password that meets the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
archipy.helpers.utils.password_utils.PasswordUtils.validate_password_history
classmethod
¶
validate_password_history(
new_password: str,
password_history: list[str],
auth_config: AuthConfig | None = None,
lang: LanguageType | None = None,
) -> None
Validates a new password against the password history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_password
|
str
|
The new password to validate. |
required |
password_history
|
list[str]
|
A list of previous password hashes. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
lang
|
LanguageType
|
The language to use for error messages. Defaults to Persian. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidPasswordError
|
If the new password has been used recently or does not meet the policy requirements. |
Source code in archipy/helpers/utils/password_utils.py
options: show_root_toc_entry: false heading_level: 3
TOTP Utils¶
Utilities for TOTP (Time-based One-Time Password) generation, verification, and QR code URI construction.
Utility module for TOTP (Time-based One-Time Password) operations.
This module provides functionality for generating and verifying TOTP codes that are commonly used for multi-factor authentication.
archipy.helpers.utils.totp_utils.TOTPUtils ¶
Utility class for TOTP (Time-based One-Time Password) operations.
This class provides methods for generating and verifying TOTP codes, as well as generating secure secret keys for TOTP initialization.
Uses the following configuration parameters from AuthConfig: - TOTP_SECRET_KEY: Master secret key for generating TOTP secrets - TOTP_HASH_ALGORITHM: Hash algorithm used for TOTP generation (default: SHA1) - TOTP_LENGTH: Number of digits in generated TOTP codes - TOTP_TIME_STEP: Time step in seconds between TOTP code changes - TOTP_EXPIRES_IN: TOTP validity period in seconds - TOTP_VERIFICATION_WINDOW: Number of time steps to check before/after - SALT_LENGTH: Length of random bytes for secure key generation
Source code in archipy/helpers/utils/totp_utils.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
archipy.helpers.utils.totp_utils.TOTPUtils.generate_totp
classmethod
¶
Generates a TOTP code using the configured hash algorithm.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret
|
str | UUID
|
The secret key used to generate the TOTP code. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A tuple containing the generated TOTP code and its expiration time. The expiration |
datetime
|
is |
tuple[str, datetime]
|
stays valid for that whole period: this helper keeps no state, so callers must enforce single use and |
tuple[str, datetime]
|
attempt limits ( |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the secret is invalid or empty. |
Source code in archipy/helpers/utils/totp_utils.py
archipy.helpers.utils.totp_utils.TOTPUtils.verify_totp
classmethod
¶
verify_totp(
secret: str | UUID,
totp_code: str,
auth_config: AuthConfig | None = None,
replay_guard: Callable[[int], bool] | None = None,
) -> bool
Verifies a TOTP code against the provided secret.
This helper keeps no state. Two protections therefore belong to the caller: counting failed attempts
(TOTP_MAX_ATTEMPTS / TOTP_LOCKOUT_TIME are not enforced here) and rejecting a code that was already
used. For the second one pass replay_guard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secret
|
str | UUID
|
The secret key used to generate the TOTP code. |
required |
totp_code
|
str
|
The TOTP code to verify. |
required |
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
replay_guard
|
Callable[[int], bool] | None
|
Optional callback invoked with the matched time step (an |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
|
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the secret is invalid or empty. |
InvalidTokenError
|
If the TOTP code format is invalid. |
Source code in archipy/helpers/utils/totp_utils.py
archipy.helpers.utils.totp_utils.TOTPUtils.generate_secret_key_for_totp
staticmethod
¶
Generates a random secret key for TOTP initialization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
auth_config
|
AuthConfig | None
|
Optional auth configuration override. If not provided, uses the global config. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A base32-encoded secret key for TOTP initialization. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the TOTP_SECRET_KEY is not configured. |
InternalError
|
If there is an error generating the secret key. |
Source code in archipy/helpers/utils/totp_utils.py
options: show_root_toc_entry: false heading_level: 3
Keycloak Utils¶
The Keycloak authentication guards moved to the adapters layer; see
Keycloak. archipy.helpers.utils.keycloak_utils still resolves the old names
and emits a DeprecationWarning; the old module will be removed in 6.0.0.
OpenTelemetry Utils¶
Utilities for OpenTelemetry provider lifecycle, library instrumentation, status mapping, and gRPC client interceptors.
OpenTelemetry utilities for provider lifecycle, instrumentation, and status mapping.
archipy.helpers.utils.otel_utils.DURATION_HISTOGRAM_BUCKETS_S
module-attribute
¶
DURATION_HISTOGRAM_BUCKETS_S: tuple[float, ...] = (
0.005,
0.01,
0.025,
0.05,
0.075,
0.1,
0.25,
0.5,
0.75,
1.0,
2.5,
5.0,
7.5,
10.0,
)
archipy.helpers.utils.otel_utils.OTEL_FASTAPI_INSTALL_HINT
module-attribute
¶
archipy.helpers.utils.otel_utils.OTEL_GRPC_INSTALL_HINT
module-attribute
¶
archipy.helpers.utils.otel_utils.OTEL_INSTALL_HINT
module-attribute
¶
archipy.helpers.utils.otel_utils.OtelRuntime ¶
Idempotent OpenTelemetry provider management and helpers for one runtime.
Owns TracerProvider / MeterProvider / LoggerProvider references and
passes them explicitly to instrumentors. Globals are set once for third-party
interop (e.g. Temporal TracingInterceptor), but internal callers use
get_tracer / get_meter against owned or adopted providers.
Providers are built programmatically from BaseConfig.OTEL. The SDK's OTEL_*
autoconfiguration is not used; with OTEL.USE_STANDARD_ENV the standard variables fill in
the settings the config leaves unset. On init, ArchiPy installs the composite textmap
propagator named by OTEL.PROPAGATORS (W3C TraceContext + Baggage by default,
idempotent) so cross-service carriers stay locked to the intended ones.
Initialization is transactional: providers are published only after all enabled signals succeed. Pre-existing concrete global providers are adopted (not replaced) so ArchiPy and third-party libraries share context. Only ArchiPy-owned providers are shut down.
Source code in archipy/helpers/utils/_otel/runtime.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 | |
archipy.helpers.utils.otel_utils.OtelRuntime.duration_histogram_buckets
staticmethod
¶
Return the explicit bucket boundaries, in seconds, of the duration histograms ArchiPy records.
Reads OTEL.METRICS_DURATION_BUCKETS_S from the global config, and falls back to
DURATION_HISTOGRAM_BUCKETS_S while no global config is set.
Returns:
| Type | Description |
|---|---|
tuple[float, ...]
|
The bucket boundaries. |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.error_type_for_exception
staticmethod
¶
Return the error.type attribute value of a failure (BaseError.code or the exception class name).
archipy.helpers.utils.otel_utils.OtelRuntime.is_otel_enabled
staticmethod
¶
Return True when the OTel master switch is enabled.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
Application configuration. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.is_traces_enabled
staticmethod
¶
Return True when tracing should be active.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
Application configuration. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the master switch and |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.is_metrics_enabled
staticmethod
¶
Return True when metrics should be active.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
Application configuration. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the master switch and |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.is_logs_enabled
staticmethod
¶
Return True when log export should be active.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
Application configuration. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the master switch and |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.import_failed ¶
archipy.helpers.utils.otel_utils.OtelRuntime.get_tracer ¶
Return a tracer from the owned tracer provider (or a no-op tracer).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Instrumentation scope name. |
required |
Returns:
| Type | Description |
|---|---|
Tracer | _NoOpTracer
|
An OpenTelemetry |
Tracer | _NoOpTracer
|
|
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.get_meter ¶
Return a meter from the owned meter provider (or a no-op meter).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Instrumentation scope name. |
required |
Returns:
| Type | Description |
|---|---|
Meter | _NoOpMeter
|
An OpenTelemetry |
Meter | _NoOpMeter
|
|
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.tracer_provider ¶
archipy.helpers.utils.otel_utils.OtelRuntime.meter_provider ¶
archipy.helpers.utils.otel_utils.OtelRuntime.logger_provider ¶
archipy.helpers.utils.otel_utils.OtelRuntime.metrics_registry ¶
Return the Prometheus CollectorRegistry for pull/pushgateway metrics.
Returns:
| Type | Description |
|---|---|
CollectorRegistry | None
|
The registry when |
CollectorRegistry | None
|
and providers were built, otherwise |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.status_for_exception
staticmethod
¶
Map an exception to an OpenTelemetry Status, or None for UNSET.
BaseError with http_status below 500 leaves status UNSET (handled client error). All other exceptions
become StatusCode.ERROR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseException
|
The exception raised during a span. |
required |
Returns:
| Type | Description |
|---|---|
Status | None
|
An OpenTelemetry |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.metric_status_for_exception
staticmethod
¶
Map an exception to a metric status attribute value ("ok" or "error").
Aligns with status_for_exception: handled client BaseError values record as ok. Callers that catch
asyncio.CancelledError should record cancelled before invoking this helper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseException
|
The exception raised during the instrumented call. |
required |
Returns:
| Type | Description |
|---|---|
str
|
|
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.status_for_cancellation
staticmethod
¶
Return an ERROR status for asyncio task cancellation.
Returns:
| Type | Description |
|---|---|
Status
|
An OpenTelemetry |
archipy.helpers.utils.otel_utils.OtelRuntime.init_otel_if_needed ¶
Initialize OTel providers once (idempotent, thread-safe, fork-aware).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BaseConfig
|
Application configuration. |
required |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.force_flush ¶
Force-flush all known providers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout_millis
|
int
|
Maximum time to wait per provider. |
_DEFAULT_FLUSH_TIMEOUT_MS
|
Returns:
| Type | Description |
|---|---|
bool
|
True when every provider flushed successfully (or none exist). |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.shutdown ¶
Flush and shut down ArchiPy-owned providers; detach logging handler.
Adopted (borrowed) providers are left running. Idempotent.
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.configure_for_testing ¶
configure_for_testing(
span_exporter: SpanExporter | None = None,
metric_reader: MetricReader | None = None,
log_exporter: LogRecordExporter | None = None,
*,
service_name: str = "archipy-test",
) -> None
Swap in in-memory providers for BDD / unit tests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
span_exporter
|
SpanExporter | None
|
Optional span exporter (e.g. |
None
|
metric_reader
|
MetricReader | None
|
Optional metric reader (e.g. |
None
|
log_exporter
|
LogRecordExporter | None
|
Optional log exporter (e.g. |
None
|
service_name
|
str
|
Resource service name for the test providers. |
'archipy-test'
|
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.reset_for_testing ¶
Reset owned providers for the next BDD scenario.
Does not replace OTel globals (first-call-wins); subsequent scenarios
reuse configure_for_testing which overwrites class attributes and
rebuilds processors/readers on the existing global providers when possible.
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.grpc_client_interceptors ¶
Return sync gRPC client interceptors for outbound trace propagation.
Returns:
| Type | Description |
|---|---|
list[OpenTelemetryClientInterceptor]
|
A list containing the contrib client interceptor. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.async_grpc_client_interceptors ¶
Return async gRPC client interceptors for outbound trace propagation.
Returns:
| Type | Description |
|---|---|
list[ClientInterceptor]
|
A list of contrib aio client interceptors. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.resolve_metrics_endpoint ¶
Return the resolved OTLP metrics endpoint for Temporal / callers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
otel
|
OpentelemetryConfig
|
OpenTelemetry config section. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The resolved metrics endpoint URL. |
Source code in archipy/helpers/utils/_otel/runtime.py
archipy.helpers.utils.otel_utils.OtelRuntime.restore_redis_search_attributes ¶
Undo :meth:_guard_redis_search_attributes (used by tests that need the upstream behaviour).
archipy.helpers.utils.otel_utils.OtelUtils ¶
Idempotent OpenTelemetry provider management and helpers.
A facade over the default OtelRuntime, which owns the TracerProvider / MeterProvider /
LoggerProvider references and passes them explicitly to instrumentors. Globals are set once for third-party
interop (e.g. Temporal TracingInterceptor), but internal callers use get_tracer / get_meter against
owned or adopted providers. Everything process-wide (the OpenTelemetry globals, the propagator, the instrumented
libraries) is shared by every runtime; set_runtime swaps the object that owns the providers, for example to
give a test its own.
Providers are built programmatically from BaseConfig.OTEL. The SDK's OTEL_* autoconfiguration is not used;
with OTEL.USE_STANDARD_ENV the standard variables fill in the settings the config leaves unset. On init,
ArchiPy installs the composite textmap propagator named by OTEL.PROPAGATORS (W3C TraceContext + Baggage by
default, idempotent) so cross-service carriers stay locked to the intended ones.
Initialization is transactional: providers are published only after all enabled signals succeed. Pre-existing concrete global providers are adopted (not replaced) so ArchiPy and third-party libraries share context. Only ArchiPy-owned providers are shut down.
Source code in archipy/helpers/utils/otel_utils.py
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 86 87 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
archipy.helpers.utils.otel_utils.OtelUtils.runtime
classmethod
¶
archipy.helpers.utils.otel_utils.OtelUtils.set_runtime
classmethod
¶
Make runtime the one the facade delegates to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
runtime
|
OtelRuntime
|
The runtime to use from now on. |
required |
Returns:
| Type | Description |
|---|---|
OtelRuntime
|
The runtime that was in use before, so the caller can put it back. |
Source code in archipy/helpers/utils/otel_utils.py
archipy.helpers.utils.otel_utils.OtelUtils.duration_histogram_buckets
staticmethod
¶
Return the explicit bucket boundaries, in seconds, of the duration histograms ArchiPy records.
archipy.helpers.utils.otel_utils.OtelUtils.error_type_for_exception
staticmethod
¶
Return the error.type attribute value of a failure (BaseError.code or the exception class name).
Source code in archipy/helpers/utils/otel_utils.py
archipy.helpers.utils.otel_utils.OtelUtils.is_otel_enabled
staticmethod
¶
archipy.helpers.utils.otel_utils.OtelUtils.is_traces_enabled
staticmethod
¶
Return whether OpenTelemetry and its traces are enabled in config.
archipy.helpers.utils.otel_utils.OtelUtils.is_metrics_enabled
staticmethod
¶
Return whether OpenTelemetry and its metrics are enabled in config.
archipy.helpers.utils.otel_utils.OtelUtils.is_logs_enabled
staticmethod
¶
Return whether OpenTelemetry and its logs are enabled in config.
archipy.helpers.utils.otel_utils.OtelUtils.import_failed
classmethod
¶
Return whether OpenTelemetry could not be imported when initialization was attempted.
archipy.helpers.utils.otel_utils.OtelUtils.get_tracer
classmethod
¶
Return a tracer from the owned tracer provider (or a no-op tracer).
archipy.helpers.utils.otel_utils.OtelUtils.get_meter
classmethod
¶
Return a meter from the owned meter provider (or a no-op meter).
archipy.helpers.utils.otel_utils.OtelUtils.tracer_provider
classmethod
¶
archipy.helpers.utils.otel_utils.OtelUtils.meter_provider
classmethod
¶
archipy.helpers.utils.otel_utils.OtelUtils.logger_provider
classmethod
¶
archipy.helpers.utils.otel_utils.OtelUtils.metrics_registry
classmethod
¶
Return the Prometheus registry of the pull or Pushgateway metrics exporter, if any.
archipy.helpers.utils.otel_utils.OtelUtils.status_for_exception
staticmethod
¶
Map an exception to an OpenTelemetry Status, or None for UNSET.
archipy.helpers.utils.otel_utils.OtelUtils.metric_status_for_exception
staticmethod
¶
Map an exception to a metric status attribute value ("ok" or "error").
archipy.helpers.utils.otel_utils.OtelUtils.status_for_cancellation
staticmethod
¶
archipy.helpers.utils.otel_utils.OtelUtils.init_otel_if_needed
classmethod
¶
Build and publish the providers once per process, as config asks.
archipy.helpers.utils.otel_utils.OtelUtils.force_flush
classmethod
¶
Flush the owned providers; return whether every flush succeeded.
archipy.helpers.utils.otel_utils.OtelUtils.shutdown
classmethod
¶
archipy.helpers.utils.otel_utils.OtelUtils.configure_for_testing
classmethod
¶
configure_for_testing(
span_exporter: SpanExporter | None = None,
metric_reader: MetricReader | None = None,
log_exporter: LogRecordExporter | None = None,
*,
service_name: str = "archipy-test",
) -> None
Install in-memory providers for a test.
Source code in archipy/helpers/utils/otel_utils.py
archipy.helpers.utils.otel_utils.OtelUtils.reset_for_testing
classmethod
¶
archipy.helpers.utils.otel_utils.OtelUtils.grpc_client_interceptors
classmethod
¶
Return sync gRPC client interceptors for outbound trace propagation.
archipy.helpers.utils.otel_utils.OtelUtils.async_grpc_client_interceptors
classmethod
¶
Return async gRPC client interceptors for outbound trace propagation.
archipy.helpers.utils.otel_utils.OtelUtils.resolve_metrics_endpoint
classmethod
¶
Return the resolved OTLP metrics endpoint for Temporal / callers.
archipy.helpers.utils.otel_utils.OtelUtils.restore_redis_search_attributes
classmethod
¶
Undo the guard around the Redis instrumentor's search helper (for tests).
options: show_root_toc_entry: false heading_level: 3
Rate Limit Utils¶
Utilities for building rate-limit window DTOs and reading decorator metadata from gRPC servicer methods.
Shared rate-limit window helpers.
archipy.helpers.utils.rate_limit_utils.RateLimitUtils ¶
Utility class for building and reading rate-limit window metadata.
Source code in archipy/helpers/utils/rate_limit_utils.py
archipy.helpers.utils.rate_limit_utils.RateLimitUtils.ARCHIPY_RATE_LIMIT_WINDOWS_ATTR
class-attribute
instance-attribute
¶
archipy.helpers.utils.rate_limit_utils.RateLimitUtils.compute_rate_limit_window
classmethod
¶
compute_rate_limit_window(
calls_count: int = 1,
milliseconds: int = 0,
seconds: int = 0,
minutes: int = 0,
hours: int = 0,
days: int = 0,
) -> RateLimitWindowDTO
Build a validated rate-limit window from time unit parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calls_count
|
int
|
Maximum allowed requests within the window. |
1
|
milliseconds
|
int
|
Milliseconds component of the window. |
0
|
seconds
|
int
|
Seconds component of the window. |
0
|
minutes
|
int
|
Minutes component of the window. |
0
|
hours
|
int
|
Hours component of the window. |
0
|
days
|
int
|
Days component of the window. |
0
|
Returns:
| Type | Description |
|---|---|
RateLimitWindowDTO
|
A |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If |
Source code in archipy/helpers/utils/rate_limit_utils.py
archipy.helpers.utils.rate_limit_utils.RateLimitUtils.get_rate_limit_windows_from_callable
classmethod
¶
Return stacked rate-limit windows attached to a callable or bound method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
object
|
A function, bound method, or other callable inspected for decorator metadata. |
required |
Returns:
| Type | Description |
|---|---|
tuple[RateLimitWindowDTO, ...]
|
Tuple of |
Source code in archipy/helpers/utils/rate_limit_utils.py
options: show_root_toc_entry: false heading_level: 3