Text Module¶
The rite.text module provides comprehensive text processing utilities including case conversions, slug generation, text analysis, validation, manipulation, and search operations.
Overview¶
text ¶
Text Processing Module¶
This module provides text processing utilities similar to Python's str, string, and textwrap modules.
The text module includes functions for: - Case conversions (snake_case, camelCase, PascalCase, kebab-case, etc.) - Slug generation for URL-friendly strings - Text analysis (character frequency, word count, etc.) - String sanitization and cleaning - Text validation (email, numeric, alpha, alphanumeric) - Text manipulation (truncate, pad, wrap) - Text search (contains, starts_with, ends_with, find, count) - Morse code encoding/decoding - Random string generation
Example
from rite.text import to_snake_case, slugify to_snake_case("Hello World") 'hello_world' slugify("Hello World!") 'hello-world'
Functions¶
add_slug_prefix ¶
Add a prefix to a slug.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
prefix
|
str
|
The prefix to add. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
The slug with the prefix added. |
Example
add_slug_prefix("world", "hello") 'hello-world' add_slug_prefix("world", "hello", delimiter="_") 'hello_world'
add_slug_suffix ¶
Add a suffix to a slug.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
suffix
|
str
|
The suffix to add. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
The slug with the suffix added. |
Example
add_slug_suffix("hello", "world") 'hello-world' add_slug_suffix("hello", "world", delimiter="_") 'hello_world'
clean ¶
Remove extra whitespace from text.
text: The input text to clean.
str: The cleaned text with normalized whitespace.
unique_slug ¶
Generate a unique slug by appending an incremental number.
If the slug already exists in the set of existing slugs, appends an incremental number (starting from 1) until a unique slug is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
existing_slugs
|
set[str] | list[str]
|
A set or list of existing slugs to check against. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
A unique slug with an incremental number if needed. |
Example
unique_slug("hello-world", {"hello-world"}) 'hello-world-1' unique_slug("hello-world", {"hello-world", "hello-world-1"}) 'hello-world-2' unique_slug("hello-world", []) 'hello-world'
Modules¶
analysis ¶
Modules¶
average_word_length ¶
Average Word Length¶
Calculate average word length in text.
Calculate the average word length in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text string |
required |
Returns:
| Type | Description |
|---|---|
float
|
Average length of words |
Raises:
| Type | Description |
|---|---|
ValueError
|
If text contains no words |
Example
average_word_length("hello world") 5.0
char_frequency ¶
Character Frequency Analysis¶
Count character frequency in text.
is_palindrome ¶
Palindrome Checker¶
Check if text is a palindrome.
Check if text is a palindrome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text string |
required |
ignore_case
|
bool
|
Whether to ignore case differences |
True
|
ignore_spaces
|
bool
|
Whether to ignore spaces and non-alphanumeric |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if text is a palindrome, False otherwise |
Example
is_palindrome("A man a plan a canal Panama") True
longest_word ¶
Longest Word Finder¶
Find the longest word in text.
shortest_word ¶
Shortest Word Finder¶
Find the shortest word in text.
case ¶
Case Conversion Module¶
Functions for converting text between different case formats.
Modules¶
case_to_acronym ¶
case_to_alphabet_position ¶
case_to_alternate_character_deletion ¶
case_to_alternate_uppercase_lowercase ¶
case_to_diagonal_text ¶
case_to_first_letter_of_each_word ¶
case_to_leet_speak ¶
case_to_pig_latin ¶
case_to_random ¶
case_to_slug ¶
case_to_spongebob_meme ¶
case_to_spongebob_mocking ¶
case_to_swap ¶
case_to_upper_consonant ¶
case_to_upper_first_letter ¶
case_to_upper_vowel ¶
case_to_upside_down ¶
case_to_zalgo_text ¶
to_camel_case ¶
Camel Case Conversion¶
Convert text to camelCase format.
Convert text to camelCase.
Converts the input text to camel case where the first word is lowercase and subsequent words are capitalized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to camelCase. |
Example
to_camel_case("hello world") 'helloWorld' to_camel_case("Hello World") 'helloWorld'
to_constant_case ¶
Constant Case Conversion¶
Convert text to CONSTANT_CASE format.
Convert text to CONSTANT_CASE.
Converts the input text to constant case (screaming snake case) by replacing spaces and special characters with underscores and converting to uppercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to CONSTANT_CASE. |
Example
to_constant_case("Hello World") 'HELLO_WORLD' to_constant_case("helloWorld") 'HELLO_WORLD'
to_dot_case ¶
Dot Case Conversion¶
Convert text to dot.case format.
Convert text to dot.case.
Converts the input text to dot case by replacing spaces and special characters with dots and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to dot.case. |
Example
to_dot_case("Hello World") 'hello.world' to_dot_case("helloWorld") 'hello.world'
to_kebab_case ¶
Kebab Case Conversion¶
Convert text to kebab-case format.
Convert text to kebab-case.
Converts the input text to kebab case by replacing spaces and special characters with hyphens and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to kebab-case. |
Example
to_kebab_case("Hello World") 'hello-world' to_kebab_case("helloWorld") 'hello-world'
to_pascal_case ¶
Pascal Case Conversion¶
Convert text to PascalCase format.
Convert text to PascalCase.
Converts the input text to Pascal case where all words are capitalized and joined without separators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to PascalCase. |
Example
to_pascal_case("hello world") 'HelloWorld' to_pascal_case("hello_world") 'HelloWorld'
to_path_case ¶
Path Case Conversion¶
Convert text to path/case format.
Convert text to path/case.
Converts the input text to path case by replacing spaces and special characters with forward slashes and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to path/case. |
Example
to_path_case("Hello World") 'hello/world' to_path_case("helloWorld") 'hello/world'
to_sentence_case ¶
Sentence Case Conversion¶
Convert text to Sentence case format.
Convert text to Sentence case.
Converts the input text to sentence case where only the first letter of the first word is capitalized and the rest is lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to Sentence case. |
Example
to_sentence_case("hello world") 'Hello world' to_sentence_case("HELLO WORLD") 'Hello world'
to_snake_case ¶
Snake Case Conversion¶
Convert text to snake_case format.
Convert text to snake_case.
Converts the input text to snake case by replacing spaces and special characters with underscores and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to snake_case. |
Example
to_snake_case("Hello World") 'hello_world' to_snake_case("helloWorld") 'hello_world'
to_title_case ¶
Title Case Conversion¶
Convert text to Title Case format.
Convert text to Title Case.
Converts the input text to title case where the first letter of each word is capitalized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to Title Case. |
Example
to_title_case("hello world") 'Hello World' to_title_case("HELLO WORLD") 'Hello World'
char_frequency ¶
Character Frequency Analysis¶
Count character frequency in text.
converters ¶
Rite - String Converters Module¶
This module provides utilities for converting strings between different formats.
Functions¶
convert_string_to_binary ¶
convert_string_to_bool ¶
String to Boolean Converter¶
Convert a string representation of a boolean value to a boolean.
convert_string_to_datetime ¶
String to Datetime Converter¶
Convert a string to a timezone-aware datetime object (UTC).
Parses a timestamp like '2024-12-11 11:42:34.049271+00' or ISO-ish strings. Returns None on blank or invalid input.
Supports ISO 8601 format with optional timezone. Naive datetimes are converted to UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
The string to convert. |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
A timezone-aware datetime object in UTC, or None. |
convert_string_to_decimal ¶
String to Decimal Converter¶
Convert a string to a Decimal, quantized to length decimal places.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
The input string (or None). |
required |
length
|
int
|
Number of decimal places to quantize to. Default is 3. |
3
|
Returns:
| Type | Description |
|---|---|
Decimal | None
|
Decimal quantized to |
convert_string_to_float ¶
Convert a string representation of a float value to a float.
convert_string_to_int ¶
Convert a string representation of an integer value to an integer.
Modules¶
case_to_abbreviation ¶
Abbreviation Case Converter¶
Convert text to abbreviation format.
Converts text to its abbreviation.
Example: 'As Soon As Possible' -> 'ASAP'
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to abbreviate |
required |
Returns:
| Type | Description |
|---|---|
str
|
The abbreviation of the text |
Example
to_abbreviation_case("As Soon As Possible") 'ASAP'
case_to_ascii_value ¶
ASCII Value Converter¶
Convert characters to ASCII values.
case_to_braille_transcription ¶
Braille Transcription Converter¶
Transcribe text into simulated Braille.
Transcribes text into simulated Braille.
Note: This is a highly simplified and symbolic representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to transcribe |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text transcribed into simulated Braille |
Example
to_braille_transcription_case("Hello") 'Braille: Hello'
case_to_double_every_second_word ¶
Double Every Second Word Converter¶
Double every second word in text.
Doubles every second word in the text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to process |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text with every second word doubled |
Example
to_double_every_second_word_case("Hello world program") 'Hello Hello world world program program'
case_to_emoji ¶
Emoji Converter¶
Replace words with corresponding emojis.
Replace certain words with corresponding emojis.
Note: This is a limited implementation; only a few words are replaced.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text with words replaced by emojis |
Example
to_emoji_case("I love my dog") 'I ❤️ my 🐶'
case_to_nato_phonetic_alphabet ¶
NATO Phonetic Alphabet Converter¶
Translate text to NATO phonetic alphabet.
Translates each letter to its corresponding NATO phonetic alphabet word.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text in NATO phonetic alphabet |
Example
to_nato_phonetic_alphabet_case("AB") 'Alpha Bravo'
case_to_numeric_words_to_numbers ¶
Numeric Words to Numbers Converter¶
Convert numeric words to numeral equivalents.
Converts numeric words to their numeral equivalents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text with numeric words converted to numbers |
Example
to_numeric_words_to_numbers_case("one two three") '1 2 3'
case_to_numeronym ¶
Numeronym Converter¶
Convert words to numeronym format.
case_to_rainbow ¶
Rainbow Case Converter¶
Assign color codes to letters.
Assigns a different color code to each letter.
Note: Actual color cannot be represented in plain text; using symbolic representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text with symbolic color codes |
Example
to_rainbow_case("Hello") '🟥H🟧e🟨l🟩l🟦o'
case_to_substitute_numbers_with_words ¶
Substitute Numbers with Words Converter¶
Replace numbers with word equivalents.
Replaces numbers in the text with their word equivalents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to process |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text with numbers replaced by words |
Example
to_substitute_numbers_with_words_case("I have 2 dogs and 3 cats") 'I have two dogs and three cats'
case_to_vowel_concatenation ¶
Vowel Concatenation Converter¶
Concatenate all vowels from text.
case_to_vowel_removal ¶
Vowel Removal Converter¶
Remove all vowels from text.
converter_string_to_binary ¶
Rite - String - String to Binary Converter Module¶
Provides functionality to convert strings to binary values.
converter_string_to_bool ¶
converter_string_to_datetime ¶
Rite - String - String to Datetime Converter Module¶
Provides functionality to convert strings to datetime values.
String to Datetime Converter¶
Convert a string to a timezone-aware datetime object (UTC).
Parses a timestamp like '2024-12-11 11:42:34.049271+00' or ISO-ish strings. Returns None on blank or invalid input.
Supports ISO 8601 format with optional timezone. Naive datetimes are converted to UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
The string to convert. |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
A timezone-aware datetime object in UTC, or None. |
converter_string_to_decimal ¶
Rite - String - String to Decimal Converter Module¶
Provides functionality to convert strings to decimal values.
String to Decimal Converter¶
Convert a string to a Decimal, quantized to length decimal places.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str | None
|
The input string (or None). |
required |
length
|
int
|
Number of decimal places to quantize to. Default is 3. |
3
|
Returns:
| Type | Description |
|---|---|
Decimal | None
|
Decimal quantized to |
converter_string_to_float ¶
converter_string_to_int ¶
string_clean ¶
String Cleaning¶
Clean and normalize string values.
is_palindrome ¶
Palindrome Checker¶
Check if text is a palindrome.
Functions¶
is_palindrome ¶
Check if text is a palindrome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text string |
required |
ignore_case
|
bool
|
Whether to ignore case differences |
True
|
ignore_spaces
|
bool
|
Whether to ignore spaces and non-alphanumeric |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if text is a palindrome, False otherwise |
Example
is_palindrome("A man a plan a canal Panama") True
longest_word ¶
Longest Word Finder¶
Find the longest word in text.
manipulation ¶
Manipulation Module¶
Text manipulation utilities.
This submodule provides utilities for manipulating text including truncation, padding, and wrapping.
Examples¶
from rite.text.manipulation import text_truncate, text_pad_left text_truncate("Hello World", 8) 'Hello...' text_pad_left("5", 3, "0") '005'
Modules¶
text_pad_left ¶
Text Pad Left¶
Pad text on the left.
Examples¶
from rite.text.manipulation import text_pad_left text_pad_left("5", 3, "0") '005'
text_pad_right ¶
Text Pad Right¶
Pad text on the right.
Examples¶
from rite.text.manipulation import text_pad_right text_pad_right("Hi", 5) 'Hi '
text_truncate ¶
Text Truncate¶
Truncate text to maximum length.
Examples¶
from rite.text.manipulation import text_truncate text_truncate("Hello World", 5) 'Hello...'
Truncate text to maximum length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to truncate. |
required |
max_length
|
int
|
Maximum length (including suffix). |
required |
suffix
|
str
|
Suffix to add when truncated. |
'...'
|
Returns:
| Type | Description |
|---|---|
str
|
Truncated text with suffix if needed. |
Examples:
Notes
Suffix length is included in max_length.
text_wrap ¶
Text Wrap¶
Wrap text to specified width.
Examples¶
from rite.text.manipulation import text_wrap text_wrap("Hello World", 5) ['Hello', 'World']
Wrap text to specified width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to wrap. |
required |
width
|
int
|
Maximum line width. |
70
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of wrapped lines. |
Examples:
>>> text_wrap("Hello World", 5)
['Hello', 'World']
>>> text_wrap("A long sentence", 10)
['A long', 'sentence']
Notes
Uses textwrap.wrap() from stdlib.
morse ¶
Modules¶
morse_decode ¶
Morse Code Decoder¶
Convert Morse code to text.
Convert Morse code to text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morse_code
|
str
|
Morse code string to decode |
required |
separator
|
str
|
Character separating Morse code symbols |
' '
|
Returns:
| Type | Description |
|---|---|
str
|
Decoded text string |
Raises:
| Type | Description |
|---|---|
ValueError
|
If invalid Morse code sequence encountered |
Example
morse_decode(".... . .-.. .-.. ---") 'HELLO'
morse_encode ¶
Morse Code Encoder¶
Convert text to Morse code.
morse_decode ¶
Morse Code Decoder¶
Convert Morse code to text.
Functions¶
morse_decode ¶
Convert Morse code to text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morse_code
|
str
|
Morse code string to decode |
required |
separator
|
str
|
Character separating Morse code symbols |
' '
|
Returns:
| Type | Description |
|---|---|
str
|
Decoded text string |
Raises:
| Type | Description |
|---|---|
ValueError
|
If invalid Morse code sequence encountered |
Example
morse_decode(".... . .-.. .-.. ---") 'HELLO'
morse_encode ¶
Morse Code Encoder¶
Convert text to Morse code.
random ¶
Modules¶
random_alphanumeric ¶
Random Alphanumeric Generator¶
Generate random alphanumeric strings.
random_alphanumeric(length: int = 16, include_lowercase: bool = True, include_uppercase: bool = True) -> str
Generate a random alphanumeric string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
Length of the random string |
16
|
include_lowercase
|
bool
|
Include lowercase letters |
True
|
include_uppercase
|
bool
|
Include uppercase letters |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Random alphanumeric string |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both include_lowercase and include_uppercase are False |
Example
len(random_alphanumeric(10)) 10
random_hex ¶
Random Hexadecimal Generator¶
Generate random hexadecimal strings.
random_string ¶
Random String Generator¶
Generate secure random strings.
random_alphanumeric ¶
Random Alphanumeric Generator¶
Generate random alphanumeric strings.
Functions¶
random_alphanumeric ¶
random_alphanumeric(length: int = 16, include_lowercase: bool = True, include_uppercase: bool = True) -> str
Generate a random alphanumeric string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
Length of the random string |
16
|
include_lowercase
|
bool
|
Include lowercase letters |
True
|
include_uppercase
|
bool
|
Include uppercase letters |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Random alphanumeric string |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both include_lowercase and include_uppercase are False |
Example
len(random_alphanumeric(10)) 10
random_hex ¶
Random Hexadecimal Generator¶
Generate random hexadecimal strings.
random_string ¶
Random String Generator¶
Generate secure random strings.
sanitize ¶
Functions¶
clean ¶
Remove extra whitespace from text.
text: The input text to clean.
str: The cleaned text with normalized whitespace.
sanitize ¶
Sanitize text to create safe ASCII identifiers.
text: The input text to sanitize.
replacement: The character to replace non-alphanumeric characters.
str: The sanitized text with only ASCII alphanumeric characters.
Modules¶
sanitize_clean ¶
search ¶
Search Module¶
Text search utilities.
This submodule provides utilities for searching within text including contains, starts_with, ends_with, find, and count operations.
Examples¶
from rite.text.search import text_contains, text_find text_contains("Hello World", "World") True text_find("Hello World", "World") 6
Modules¶
text_contains ¶
Text Contains¶
Check if text contains a substring.
Examples¶
from rite.text.search import text_contains text_contains("Hello World", "World") True
text_count ¶
Text Count¶
Count substring occurrences in text.
Examples¶
from rite.text.search import text_count text_count("Hello Hello World", "Hello") 2
Count substring occurrences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to search in. |
required |
substring
|
str
|
Substring to count. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of non-overlapping occurrences. |
Examples:
Notes
Uses str.count() which counts non-overlapping occurrences.
text_ends_with ¶
Text Ends With¶
Check if text ends with suffix.
Examples¶
from rite.text.search import text_ends_with text_ends_with("Hello World", "World") True
text_find ¶
Text Find¶
Find substring position in text.
Examples¶
from rite.text.search import text_find text_find("Hello World", "World") 6
Find substring position in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to search in. |
required |
substring
|
str
|
Substring to find. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Index of substring or -1 if not found. |
Examples:
Notes
Uses str.find() method. Returns -1 if not found.
text_starts_with ¶
Text Starts With¶
Check if text starts with prefix.
Examples¶
from rite.text.search import text_starts_with text_starts_with("Hello World", "Hello") True
slug ¶
Slug Generation Module¶
Functions for generating URL-friendly slugs.
Functions¶
add_slug_prefix ¶
Add a prefix to a slug.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
prefix
|
str
|
The prefix to add. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
The slug with the prefix added. |
Example
add_slug_prefix("world", "hello") 'hello-world' add_slug_prefix("world", "hello", delimiter="_") 'hello_world'
add_slug_suffix ¶
Add a suffix to a slug.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
suffix
|
str
|
The suffix to add. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
The slug with the suffix added. |
Example
add_slug_suffix("hello", "world") 'hello-world' add_slug_suffix("hello", "world", delimiter="_") 'hello_world'
is_valid_slug ¶
Validate if a string is a valid slug.
A valid slug contains only lowercase letters, numbers, and delimiters, and does not start or end with a delimiter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The string to validate. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the string is a valid slug, False otherwise. |
Example
is_valid_slug("hello-world") True is_valid_slug("Hello-World") False is_valid_slug("-hello-world") False is_valid_slug("hello--world") False
unique_slug ¶
Generate a unique slug by appending an incremental number.
If the slug already exists in the set of existing slugs, appends an incremental number (starting from 1) until a unique slug is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
existing_slugs
|
set[str] | list[str]
|
A set or list of existing slugs to check against. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
A unique slug with an incremental number if needed. |
Example
unique_slug("hello-world", {"hello-world"}) 'hello-world-1' unique_slug("hello-world", {"hello-world", "hello-world-1"}) 'hello-world-2' unique_slug("hello-world", []) 'hello-world'
Modules¶
slug_add_prefix ¶
Add Slug Prefix Function¶
Add a prefix to a slug.
Add a prefix to a slug.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
prefix
|
str
|
The prefix to add. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
The slug with the prefix added. |
Example
add_slug_prefix("world", "hello") 'hello-world' add_slug_prefix("world", "hello", delimiter="_") 'hello_world'
slug_add_suffix ¶
Add Slug Suffix Function¶
Add a suffix to a slug.
Add a suffix to a slug.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
suffix
|
str
|
The suffix to add. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
The slug with the suffix added. |
Example
add_slug_suffix("hello", "world") 'hello-world' add_slug_suffix("hello", "world", delimiter="_") 'hello_world'
slug_is_valid ¶
Is Valid Slug Function¶
Validate slug format.
Validate if a string is a valid slug.
A valid slug contains only lowercase letters, numbers, and delimiters, and does not start or end with a delimiter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The string to validate. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the string is a valid slug, False otherwise. |
Example
is_valid_slug("hello-world") True is_valid_slug("Hello-World") False is_valid_slug("-hello-world") False is_valid_slug("hello--world") False
slug_unique ¶
Unique Slug Function¶
Generate unique slugs with incremental numbers.
Generate a unique slug by appending an incremental number.
If the slug already exists in the set of existing slugs, appends an incremental number (starting from 1) until a unique slug is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
existing_slugs
|
set[str] | list[str]
|
A set or list of existing slugs to check against. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
A unique slug with an incremental number if needed. |
Example
unique_slug("hello-world", {"hello-world"}) 'hello-world-1' unique_slug("hello-world", {"hello-world", "hello-world-1"}) 'hello-world-2' unique_slug("hello-world", []) 'hello-world'
slugify ¶
Slugify Function¶
Generate URL-friendly slugs from text.
slugify(text: str, delimiter: str = '-', max_length: int | None = None, lowercase: bool = True, custom_replacements: dict[str, str] | None = None) -> str
Generate a URL-friendly slug from text.
Converts the input text to a URL-friendly slug by: - Normalizing Unicode characters - Applying custom character replacements (optional) - Converting to lowercase (optional) - Replacing spaces and special characters with delimiters - Truncating to maximum length (optional)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert into a slug. |
required |
delimiter
|
str
|
The delimiter to use for separating words (default: "-"). |
'-'
|
max_length
|
int | None
|
Maximum length of the slug. If None, no limit is applied. |
None
|
lowercase
|
bool
|
Convert slug to lowercase if True (default: True). |
True
|
custom_replacements
|
dict[str, str] | None
|
Dictionary of custom character replacements. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A URL-friendly slug. |
Example
slugify("Hello World!") 'hello-world' slugify("Café au Lait", delimiter="_") 'cafe_au_lait' slugify("Hello World", max_length=8) 'hello-wo' slugify("Hello & World", custom_replacements={"&": "and"}) 'hello-and-world'
slugify ¶
Slugify Function¶
Generate URL-friendly slugs from text.
Functions¶
slugify ¶
slugify(text: str, delimiter: str = '-', max_length: int | None = None, lowercase: bool = True, custom_replacements: dict[str, str] | None = None) -> str
Generate a URL-friendly slug from text.
Converts the input text to a URL-friendly slug by: - Normalizing Unicode characters - Applying custom character replacements (optional) - Converting to lowercase (optional) - Replacing spaces and special characters with delimiters - Truncating to maximum length (optional)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert into a slug. |
required |
delimiter
|
str
|
The delimiter to use for separating words (default: "-"). |
'-'
|
max_length
|
int | None
|
Maximum length of the slug. If None, no limit is applied. |
None
|
lowercase
|
bool
|
Convert slug to lowercase if True (default: True). |
True
|
custom_replacements
|
dict[str, str] | None
|
Dictionary of custom character replacements. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A URL-friendly slug. |
Example
slugify("Hello World!") 'hello-world' slugify("Café au Lait", delimiter="_") 'cafe_au_lait' slugify("Hello World", max_length=8) 'hello-wo' slugify("Hello & World", custom_replacements={"&": "and"}) 'hello-and-world'
text_contains ¶
Text Contains¶
Check if text contains a substring.
Examples¶
from rite.text.search import text_contains text_contains("Hello World", "World") True
text_count ¶
Text Count¶
Count substring occurrences in text.
Examples¶
from rite.text.search import text_count text_count("Hello Hello World", "Hello") 2
Functions¶
text_count ¶
Count substring occurrences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to search in. |
required |
substring
|
str
|
Substring to count. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of non-overlapping occurrences. |
Examples:
Notes
Uses str.count() which counts non-overlapping occurrences.
text_ends_with ¶
Text Ends With¶
Check if text ends with suffix.
Examples¶
from rite.text.search import text_ends_with text_ends_with("Hello World", "World") True
text_find ¶
Text Find¶
Find substring position in text.
Examples¶
from rite.text.search import text_find text_find("Hello World", "World") 6
Functions¶
text_find ¶
Find substring position in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to search in. |
required |
substring
|
str
|
Substring to find. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Index of substring or -1 if not found. |
Examples:
Notes
Uses str.find() method. Returns -1 if not found.
text_is_alpha ¶
Text Is Alpha¶
Check if text is alphabetic.
Examples¶
from rite.text.validation import text_is_alpha text_is_alpha("Hello") True
text_is_alphanumeric ¶
Text Is Alphanumeric¶
Check if text is alphanumeric.
Examples¶
from rite.text.validation import text_is_alphanumeric text_is_alphanumeric("Hello123") True
text_is_email ¶
Text Is Email¶
Validate email address format.
Examples¶
from rite.text.validation import text_is_email text_is_email("user@example.com") True
Functions¶
text_is_email ¶
Check if text is valid email format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid email format, False otherwise. |
Examples:
>>> text_is_email("[email protected]")
True
>>> text_is_email("invalid.email")
False
Notes
Basic email validation using regex. Not RFC-compliant, for simple checks only.
text_is_numeric ¶
Text Is Numeric¶
Check if text is numeric.
Examples¶
from rite.text.validation import text_is_numeric text_is_numeric("123") True
Functions¶
text_is_numeric ¶
Check if text contains only numeric characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if numeric, False otherwise. |
Examples:
Notes
Uses str.isnumeric() method. Returns False for floats with decimals.
text_pad_left ¶
Text Pad Left¶
Pad text on the left.
Examples¶
from rite.text.manipulation import text_pad_left text_pad_left("5", 3, "0") '005'
text_pad_right ¶
Text Pad Right¶
Pad text on the right.
Examples¶
from rite.text.manipulation import text_pad_right text_pad_right("Hi", 5) 'Hi '
text_starts_with ¶
Text Starts With¶
Check if text starts with prefix.
Examples¶
from rite.text.search import text_starts_with text_starts_with("Hello World", "Hello") True
text_truncate ¶
Text Truncate¶
Truncate text to maximum length.
Examples¶
from rite.text.manipulation import text_truncate text_truncate("Hello World", 5) 'Hello...'
Functions¶
text_truncate ¶
Truncate text to maximum length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to truncate. |
required |
max_length
|
int
|
Maximum length (including suffix). |
required |
suffix
|
str
|
Suffix to add when truncated. |
'...'
|
Returns:
| Type | Description |
|---|---|
str
|
Truncated text with suffix if needed. |
Examples:
Notes
Suffix length is included in max_length.
text_wrap ¶
Text Wrap¶
Wrap text to specified width.
Examples¶
from rite.text.manipulation import text_wrap text_wrap("Hello World", 5) ['Hello', 'World']
Functions¶
text_wrap ¶
Wrap text to specified width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to wrap. |
required |
width
|
int
|
Maximum line width. |
70
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of wrapped lines. |
Examples:
>>> text_wrap("Hello World", 5)
['Hello', 'World']
>>> text_wrap("A long sentence", 10)
['A long', 'sentence']
Notes
Uses textwrap.wrap() from stdlib.
to_camel_case ¶
Camel Case Conversion¶
Convert text to camelCase format.
Functions¶
to_camel_case ¶
Convert text to camelCase.
Converts the input text to camel case where the first word is lowercase and subsequent words are capitalized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to camelCase. |
Example
to_camel_case("hello world") 'helloWorld' to_camel_case("Hello World") 'helloWorld'
to_constant_case ¶
Constant Case Conversion¶
Convert text to CONSTANT_CASE format.
Functions¶
to_constant_case ¶
Convert text to CONSTANT_CASE.
Converts the input text to constant case (screaming snake case) by replacing spaces and special characters with underscores and converting to uppercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to CONSTANT_CASE. |
Example
to_constant_case("Hello World") 'HELLO_WORLD' to_constant_case("helloWorld") 'HELLO_WORLD'
to_dot_case ¶
Dot Case Conversion¶
Convert text to dot.case format.
Functions¶
to_dot_case ¶
Convert text to dot.case.
Converts the input text to dot case by replacing spaces and special characters with dots and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to dot.case. |
Example
to_dot_case("Hello World") 'hello.world' to_dot_case("helloWorld") 'hello.world'
to_kebab_case ¶
Kebab Case Conversion¶
Convert text to kebab-case format.
Functions¶
to_kebab_case ¶
Convert text to kebab-case.
Converts the input text to kebab case by replacing spaces and special characters with hyphens and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to kebab-case. |
Example
to_kebab_case("Hello World") 'hello-world' to_kebab_case("helloWorld") 'hello-world'
to_pascal_case ¶
Pascal Case Conversion¶
Convert text to PascalCase format.
Functions¶
to_pascal_case ¶
Convert text to PascalCase.
Converts the input text to Pascal case where all words are capitalized and joined without separators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to PascalCase. |
Example
to_pascal_case("hello world") 'HelloWorld' to_pascal_case("hello_world") 'HelloWorld'
to_path_case ¶
Path Case Conversion¶
Convert text to path/case format.
Functions¶
to_path_case ¶
Convert text to path/case.
Converts the input text to path case by replacing spaces and special characters with forward slashes and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to path/case. |
Example
to_path_case("Hello World") 'hello/world' to_path_case("helloWorld") 'hello/world'
to_sentence_case ¶
Sentence Case Conversion¶
Convert text to Sentence case format.
Functions¶
to_sentence_case ¶
Convert text to Sentence case.
Converts the input text to sentence case where only the first letter of the first word is capitalized and the rest is lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to Sentence case. |
Example
to_sentence_case("hello world") 'Hello world' to_sentence_case("HELLO WORLD") 'Hello world'
to_snake_case ¶
Snake Case Conversion¶
Convert text to snake_case format.
Functions¶
to_snake_case ¶
Convert text to snake_case.
Converts the input text to snake case by replacing spaces and special characters with underscores and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to snake_case. |
Example
to_snake_case("Hello World") 'hello_world' to_snake_case("helloWorld") 'hello_world'
to_title_case ¶
Title Case Conversion¶
Convert text to Title Case format.
Functions¶
to_title_case ¶
Convert text to Title Case.
Converts the input text to title case where the first letter of each word is capitalized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to Title Case. |
Example
to_title_case("hello world") 'Hello World' to_title_case("HELLO WORLD") 'Hello World'
validation ¶
Validation Module¶
Text validation utilities.
This submodule provides utilities for validating text formats and content types.
Examples¶
from rite.text.validation import text_is_email, text_is_numeric text_is_email("user@example.com") True text_is_numeric("123") True
Modules¶
text_is_alpha ¶
Text Is Alpha¶
Check if text is alphabetic.
Examples¶
from rite.text.validation import text_is_alpha text_is_alpha("Hello") True
text_is_alphanumeric ¶
Text Is Alphanumeric¶
Check if text is alphanumeric.
Examples¶
from rite.text.validation import text_is_alphanumeric text_is_alphanumeric("Hello123") True
text_is_email ¶
Text Is Email¶
Validate email address format.
Examples¶
from rite.text.validation import text_is_email text_is_email("user@example.com") True
Check if text is valid email format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid email format, False otherwise. |
Examples:
>>> text_is_email("[email protected]")
True
>>> text_is_email("invalid.email")
False
Notes
Basic email validation using regex. Not RFC-compliant, for simple checks only.
text_is_numeric ¶
Text Is Numeric¶
Check if text is numeric.
Examples¶
from rite.text.validation import text_is_numeric text_is_numeric("123") True
Check if text contains only numeric characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if numeric, False otherwise. |
Examples:
Notes
Uses str.isnumeric() method. Returns False for floats with decimals.
Submodules¶
Case Conversions¶
Transform text between different naming conventions and styles.
Case Conversion Module¶
Functions for converting text between different case formats.
Modules¶
to_snake_case ¶
Snake Case Conversion¶
Convert text to snake_case format.
Functions¶
to_snake_case ¶
Convert text to snake_case.
Converts the input text to snake case by replacing spaces and special characters with underscores and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to snake_case. |
Example
to_snake_case("Hello World") 'hello_world' to_snake_case("helloWorld") 'hello_world'
to_camel_case ¶
Camel Case Conversion¶
Convert text to camelCase format.
Functions¶
to_camel_case ¶
Convert text to camelCase.
Converts the input text to camel case where the first word is lowercase and subsequent words are capitalized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to camelCase. |
Example
to_camel_case("hello world") 'helloWorld' to_camel_case("Hello World") 'helloWorld'
to_pascal_case ¶
Pascal Case Conversion¶
Convert text to PascalCase format.
Functions¶
to_pascal_case ¶
Convert text to PascalCase.
Converts the input text to Pascal case where all words are capitalized and joined without separators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to PascalCase. |
Example
to_pascal_case("hello world") 'HelloWorld' to_pascal_case("hello_world") 'HelloWorld'
to_kebab_case ¶
Kebab Case Conversion¶
Convert text to kebab-case format.
Functions¶
to_kebab_case ¶
Convert text to kebab-case.
Converts the input text to kebab case by replacing spaces and special characters with hyphens and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to kebab-case. |
Example
to_kebab_case("Hello World") 'hello-world' to_kebab_case("helloWorld") 'hello-world'
to_constant_case ¶
Constant Case Conversion¶
Convert text to CONSTANT_CASE format.
Functions¶
to_constant_case ¶
Convert text to CONSTANT_CASE.
Converts the input text to constant case (screaming snake case) by replacing spaces and special characters with underscores and converting to uppercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to CONSTANT_CASE. |
Example
to_constant_case("Hello World") 'HELLO_WORLD' to_constant_case("helloWorld") 'HELLO_WORLD'
to_dot_case ¶
Dot Case Conversion¶
Convert text to dot.case format.
Functions¶
to_dot_case ¶
Convert text to dot.case.
Converts the input text to dot case by replacing spaces and special characters with dots and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to dot.case. |
Example
to_dot_case("Hello World") 'hello.world' to_dot_case("helloWorld") 'hello.world'
to_path_case ¶
Path Case Conversion¶
Convert text to path/case format.
Functions¶
to_path_case ¶
Convert text to path/case.
Converts the input text to path case by replacing spaces and special characters with forward slashes and converting to lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to path/case. |
Example
to_path_case("Hello World") 'hello/world' to_path_case("helloWorld") 'hello/world'
to_title_case ¶
Title Case Conversion¶
Convert text to Title Case format.
Functions¶
to_title_case ¶
Convert text to Title Case.
Converts the input text to title case where the first letter of each word is capitalized.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to Title Case. |
Example
to_title_case("hello world") 'Hello World' to_title_case("HELLO WORLD") 'Hello World'
to_sentence_case ¶
Sentence Case Conversion¶
Convert text to Sentence case format.
Functions¶
to_sentence_case ¶
Convert text to Sentence case.
Converts the input text to sentence case where only the first letter of the first word is capitalized and the rest is lowercase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The text converted to Sentence case. |
Example
to_sentence_case("hello world") 'Hello world' to_sentence_case("HELLO WORLD") 'Hello world'
Slug Generation¶
Create URL-friendly slugs from text.
Slug Generation Module¶
Functions for generating URL-friendly slugs.
Modules¶
slugify ¶
Slugify Function¶
Generate URL-friendly slugs from text.
Functions¶
slugify ¶
slugify(text: str, delimiter: str = '-', max_length: int | None = None, lowercase: bool = True, custom_replacements: dict[str, str] | None = None) -> str
Generate a URL-friendly slug from text.
Converts the input text to a URL-friendly slug by: - Normalizing Unicode characters - Applying custom character replacements (optional) - Converting to lowercase (optional) - Replacing spaces and special characters with delimiters - Truncating to maximum length (optional)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to convert into a slug. |
required |
delimiter
|
str
|
The delimiter to use for separating words (default: "-"). |
'-'
|
max_length
|
int | None
|
Maximum length of the slug. If None, no limit is applied. |
None
|
lowercase
|
bool
|
Convert slug to lowercase if True (default: True). |
True
|
custom_replacements
|
dict[str, str] | None
|
Dictionary of custom character replacements. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A URL-friendly slug. |
Example
slugify("Hello World!") 'hello-world' slugify("Café au Lait", delimiter="_") 'cafe_au_lait' slugify("Hello World", max_length=8) 'hello-wo' slugify("Hello & World", custom_replacements={"&": "and"}) 'hello-and-world'
slug_unique ¶
Unique Slug Function¶
Generate unique slugs with incremental numbers.
Functions¶
unique_slug ¶
Generate a unique slug by appending an incremental number.
If the slug already exists in the set of existing slugs, appends an incremental number (starting from 1) until a unique slug is found.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
existing_slugs
|
set[str] | list[str]
|
A set or list of existing slugs to check against. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
A unique slug with an incremental number if needed. |
Example
unique_slug("hello-world", {"hello-world"}) 'hello-world-1' unique_slug("hello-world", {"hello-world", "hello-world-1"}) 'hello-world-2' unique_slug("hello-world", []) 'hello-world'
slug_add_prefix ¶
Add Slug Prefix Function¶
Add a prefix to a slug.
Functions¶
add_slug_prefix ¶
Add a prefix to a slug.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
prefix
|
str
|
The prefix to add. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
The slug with the prefix added. |
Example
add_slug_prefix("world", "hello") 'hello-world' add_slug_prefix("world", "hello", delimiter="_") 'hello_world'
slug_add_suffix ¶
Add Slug Suffix Function¶
Add a suffix to a slug.
Functions¶
add_slug_suffix ¶
Add a suffix to a slug.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The original slug. |
required |
suffix
|
str
|
The suffix to add. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
str
|
The slug with the suffix added. |
Example
add_slug_suffix("hello", "world") 'hello-world' add_slug_suffix("hello", "world", delimiter="_") 'hello_world'
slug_is_valid ¶
Is Valid Slug Function¶
Validate slug format.
Functions¶
is_valid_slug ¶
Validate if a string is a valid slug.
A valid slug contains only lowercase letters, numbers, and delimiters, and does not start or end with a delimiter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
slug
|
str
|
The string to validate. |
required |
delimiter
|
str
|
The delimiter used in the slug (default: "-"). |
'-'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the string is a valid slug, False otherwise. |
Example
is_valid_slug("hello-world") True is_valid_slug("Hello-World") False is_valid_slug("-hello-world") False is_valid_slug("hello--world") False
Text Analysis¶
Analyze text content and structure.
Modules¶
char_frequency ¶
Character Frequency Analysis¶
Count character frequency in text.
word_count ¶
is_palindrome ¶
Palindrome Checker¶
Check if text is a palindrome.
Functions¶
is_palindrome ¶
Check if text is a palindrome.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text string |
required |
ignore_case
|
bool
|
Whether to ignore case differences |
True
|
ignore_spaces
|
bool
|
Whether to ignore spaces and non-alphanumeric |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if text is a palindrome, False otherwise |
Example
is_palindrome("A man a plan a canal Panama") True
longest_word ¶
Longest Word Finder¶
Find the longest word in text.
shortest_word ¶
Shortest Word Finder¶
Find the shortest word in text.
average_word_length ¶
Average Word Length¶
Calculate average word length in text.
Functions¶
average_word_length ¶
Calculate the average word length in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Input text string |
required |
Returns:
| Type | Description |
|---|---|
float
|
Average length of words |
Raises:
| Type | Description |
|---|---|
ValueError
|
If text contains no words |
Example
average_word_length("hello world") 5.0
Text Validation¶
Validate text format and content.
Validation Module¶
Text validation utilities.
This submodule provides utilities for validating text formats and content types.
Examples¶
from rite.text.validation import text_is_email, text_is_numeric text_is_email("user@example.com") True text_is_numeric("123") True
Modules¶
text_is_email ¶
Text Is Email¶
Validate email address format.
Examples¶
from rite.text.validation import text_is_email text_is_email("user@example.com") True
Functions¶
text_is_email ¶
Check if text is valid email format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid email format, False otherwise. |
Examples:
>>> text_is_email("[email protected]")
True
>>> text_is_email("invalid.email")
False
Notes
Basic email validation using regex. Not RFC-compliant, for simple checks only.
text_is_numeric ¶
Text Is Numeric¶
Check if text is numeric.
Examples¶
from rite.text.validation import text_is_numeric text_is_numeric("123") True
Functions¶
text_is_numeric ¶
Check if text contains only numeric characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
String to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if numeric, False otherwise. |
Examples:
Notes
Uses str.isnumeric() method. Returns False for floats with decimals.
text_is_alpha ¶
Text Is Alpha¶
Check if text is alphabetic.
Examples¶
from rite.text.validation import text_is_alpha text_is_alpha("Hello") True
text_is_alphanumeric ¶
Text Is Alphanumeric¶
Check if text is alphanumeric.
Examples¶
from rite.text.validation import text_is_alphanumeric text_is_alphanumeric("Hello123") True
Text Manipulation¶
Manipulate and transform text.
Manipulation Module¶
Text manipulation utilities.
This submodule provides utilities for manipulating text including truncation, padding, and wrapping.
Examples¶
from rite.text.manipulation import text_truncate, text_pad_left text_truncate("Hello World", 8) 'Hello...' text_pad_left("5", 3, "0") '005'
Modules¶
text_truncate ¶
Text Truncate¶
Truncate text to maximum length.
Examples¶
from rite.text.manipulation import text_truncate text_truncate("Hello World", 5) 'Hello...'
Functions¶
text_truncate ¶
Truncate text to maximum length.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to truncate. |
required |
max_length
|
int
|
Maximum length (including suffix). |
required |
suffix
|
str
|
Suffix to add when truncated. |
'...'
|
Returns:
| Type | Description |
|---|---|
str
|
Truncated text with suffix if needed. |
Examples:
Notes
Suffix length is included in max_length.
text_pad_left ¶
Text Pad Left¶
Pad text on the left.
Examples¶
from rite.text.manipulation import text_pad_left text_pad_left("5", 3, "0") '005'
text_pad_right ¶
Text Pad Right¶
Pad text on the right.
Examples¶
from rite.text.manipulation import text_pad_right text_pad_right("Hi", 5) 'Hi '
text_wrap ¶
Text Wrap¶
Wrap text to specified width.
Examples¶
from rite.text.manipulation import text_wrap text_wrap("Hello World", 5) ['Hello', 'World']
Functions¶
text_wrap ¶
Wrap text to specified width.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to wrap. |
required |
width
|
int
|
Maximum line width. |
70
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of wrapped lines. |
Examples:
>>> text_wrap("Hello World", 5)
['Hello', 'World']
>>> text_wrap("A long sentence", 10)
['A long', 'sentence']
Notes
Uses textwrap.wrap() from stdlib.
Text Search¶
Search and find patterns in text.
Search Module¶
Text search utilities.
This submodule provides utilities for searching within text including contains, starts_with, ends_with, find, and count operations.
Examples¶
from rite.text.search import text_contains, text_find text_contains("Hello World", "World") True text_find("Hello World", "World") 6
Modules¶
text_contains ¶
Text Contains¶
Check if text contains a substring.
Examples¶
from rite.text.search import text_contains text_contains("Hello World", "World") True
text_starts_with ¶
Text Starts With¶
Check if text starts with prefix.
Examples¶
from rite.text.search import text_starts_with text_starts_with("Hello World", "Hello") True
text_ends_with ¶
Text Ends With¶
Check if text ends with suffix.
Examples¶
from rite.text.search import text_ends_with text_ends_with("Hello World", "World") True
text_find ¶
Text Find¶
Find substring position in text.
Examples¶
from rite.text.search import text_find text_find("Hello World", "World") 6
Functions¶
text_find ¶
Find substring position in text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to search in. |
required |
substring
|
str
|
Substring to find. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Index of substring or -1 if not found. |
Examples:
Notes
Uses str.find() method. Returns -1 if not found.
text_count ¶
Text Count¶
Count substring occurrences in text.
Examples¶
from rite.text.search import text_count text_count("Hello Hello World", "Hello") 2
Functions¶
text_count ¶
Count substring occurrences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to search in. |
required |
substring
|
str
|
Substring to count. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of non-overlapping occurrences. |
Examples:
Notes
Uses str.count() which counts non-overlapping occurrences.
Sanitization¶
Clean and sanitize text content.
Functions¶
sanitize ¶
Sanitize text to create safe ASCII identifiers.
text: The input text to sanitize.
replacement: The character to replace non-alphanumeric characters.
str: The sanitized text with only ASCII alphanumeric characters.
clean ¶
Remove extra whitespace from text.
text: The input text to clean.
str: The cleaned text with normalized whitespace.
Morse Code¶
Encode and decode Morse code.
Modules¶
morse_encode ¶
Morse Code Encoder¶
Convert text to Morse code.
morse_decode ¶
Morse Code Decoder¶
Convert Morse code to text.
Functions¶
morse_decode ¶
Convert Morse code to text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
morse_code
|
str
|
Morse code string to decode |
required |
separator
|
str
|
Character separating Morse code symbols |
' '
|
Returns:
| Type | Description |
|---|---|
str
|
Decoded text string |
Raises:
| Type | Description |
|---|---|
ValueError
|
If invalid Morse code sequence encountered |
Example
morse_decode(".... . .-.. .-.. ---") 'HELLO'
Random Generation¶
Generate random strings.
Modules¶
random_string ¶
Random String Generator¶
Generate secure random strings.
random_hex ¶
Random Hexadecimal Generator¶
Generate random hexadecimal strings.
random_alphanumeric ¶
Random Alphanumeric Generator¶
Generate random alphanumeric strings.
Functions¶
random_alphanumeric ¶
random_alphanumeric(length: int = 16, include_lowercase: bool = True, include_uppercase: bool = True) -> str
Generate a random alphanumeric string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
Length of the random string |
16
|
include_lowercase
|
bool
|
Include lowercase letters |
True
|
include_uppercase
|
bool
|
Include uppercase letters |
True
|
Returns:
| Type | Description |
|---|---|
str
|
Random alphanumeric string |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both include_lowercase and include_uppercase are False |
Example
len(random_alphanumeric(10)) 10
Examples¶
Basic Usage¶
from rite.text import to_snake_case, slugify, text_truncate
# Case conversion
text = "HelloWorld"
snake = to_snake_case(text) # "hello_world"
# Slug generation
title = "Hello World! This is a test."
slug = slugify(title) # "hello-world-this-is-a-test"
# Text manipulation
long_text = "This is a very long text that needs truncation"
short = text_truncate(long_text, 20) # "This is a very l..."
Advanced Examples¶
from rite.text import (
text_is_email,
text_pad_left,
char_frequency,
text_contains
)
# Validation
email = "[email protected]"
is_valid = text_is_email(email) # True
# Padding
number = "42"
padded = text_pad_left(number, 5, "0") # "00042"
# Analysis
text = "hello world"
freq = char_frequency(text) # {'h': 1, 'e': 1, 'l': 3, ...}
# Search
haystack = "The quick brown fox"
found = text_contains(haystack, "quick") # True