extended_data.primitives.strings

This module provides utilities for string manipulation and validation.

It includes functions to sanitize keys, truncate messages, manipulate character cases, validate URLs, convert camelCase to TitleCase, and convert string representations of truth to boolean values.

Module Contents

Functions

bytes_to_string

Convert bytes, memoryview, bytearray, or another object to a string.

sanitize_key

Sanitizes a key by replacing non-alphanumeric characters with a delimiter.

truncate

Truncates a message to a maximum length, appending an ender if truncated.

lower_first_char

Converts the first character of a string to lowercase.

upper_first_char

Converts the first character of a string to uppercase.

is_url

Checks if the given string is a valid URL using urlparse.

titleize_name

Converts a camelCase name to a TitleCase name.

API

extended_data.primitives.strings.bytes_to_string(value: object) str

Convert bytes, memoryview, bytearray, or another object to a string.

Bytes-like values are decoded as UTF-8. Strings are returned unchanged and all other objects use their standard string representation.

Args: value: The value to convert.

Returns: The string representation of the input.

Raises: UnicodeDecodeError: If the bytes or bytearray cannot be decoded into a valid UTF-8 string.

extended_data.primitives.strings.sanitize_key(key: str, delim: str = '_') str

Sanitizes a key by replacing non-alphanumeric characters with a delimiter.

Args: key (str): The key to sanitize. delim (str): The delimiter to replace non-alphanumeric characters with. Defaults to “_”.

Returns: str: The sanitized key.

extended_data.primitives.strings.truncate(msg: str, max_length: int, ender: str = '...') str

Truncates a message to a maximum length, appending an ender if truncated.

Args: msg (str): The message to truncate. max_length (int): The maximum length of the truncated message. ender (str): The string to append to the truncated message. Defaults to “…”.

Returns: str: The truncated message.

extended_data.primitives.strings.lower_first_char(inp: str) str

Converts the first character of a string to lowercase.

Args: inp (str): The input string.

Returns: str: The string with the first character in lowercase.

extended_data.primitives.strings.upper_first_char(inp: str) str

Converts the first character of a string to uppercase.

Args: inp (str): The input string.

Returns: str: The string with the first character in uppercase.

extended_data.primitives.strings.is_url(url: str) bool

Checks if the given string is a valid URL using urlparse.

Args: url (str): The string to check.

Returns: bool: True if the string is a valid URL, False otherwise.

extended_data.primitives.strings.titleize_name(name: str) str

Converts a camelCase name to a TitleCase name.

Args: name (str): The camelCase name.

Returns: str: The TitleCase name.