Conversion Module¶
The rite.conversion module provides type conversion and format transformation utilities.
Overview¶
conversion ¶
Conversion Module¶
Comprehensive conversion utilities for types, formats, and units.
This module provides three main categories of conversions:
- Type Conversions (types submodule)
- Basic Python type conversions: int, float, str, bool, bytes
- Collection conversions: list, dict, set, tuple
-
Number parsing and percentage conversions
-
Format Conversions (formats submodule)
- JSON encoding/decoding
- Base64 encoding/decoding
- Hexadecimal encoding/decoding
-
URL encoding/decoding
-
Unit Conversions (units submodule)
- Temperature: Celsius, Fahrenheit, Kelvin
- Length: meters, feet, inches, miles, kilometers
- Weight: grams, kilograms, pounds, ounces
- Time: seconds, minutes, hours, days
Examples¶
from rite.conversion import ( ... types_to_bool, ... formats_json_encode, ... units_celsius_to_fahrenheit ... ) types_to_bool("yes") True formats_json_encode({"key": "value"}) '{"key": "value"}' units_celsius_to_fahrenheit(0) 32.0
Functions¶
units_celsius_to_fahrenheit ¶
Convert Celsius to Fahrenheit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
celsius
|
float
|
Temperature in Celsius. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Fahrenheit. |
Examples:
units_celsius_to_kelvin ¶
units_days_to_seconds ¶
units_fahrenheit_to_celsius ¶
Convert Fahrenheit to Celsius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fahrenheit
|
float
|
Temperature in Fahrenheit. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Celsius. |
Examples:
units_fahrenheit_to_kelvin ¶
units_feet_to_meters ¶
units_grams_to_kilograms ¶
units_grams_to_ounces ¶
units_grams_to_pounds ¶
units_hours_to_minutes ¶
units_hours_to_seconds ¶
units_inches_to_meters ¶
units_kelvin_to_celsius ¶
Convert Kelvin to Celsius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kelvin
|
float
|
Temperature in Kelvin. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Celsius. |
Examples:
units_kelvin_to_fahrenheit ¶
units_kilograms_to_grams ¶
units_kilometers_to_meters ¶
units_meters_to_feet ¶
units_meters_to_inches ¶
units_meters_to_kilometers ¶
units_meters_to_miles ¶
units_miles_to_meters ¶
units_minutes_to_hours ¶
units_minutes_to_seconds ¶
units_ounces_to_grams ¶
units_pounds_to_grams ¶
units_seconds_to_days ¶
units_seconds_to_hours ¶
units_seconds_to_minutes ¶
Modules¶
formats ¶
Format Conversion Utilities¶
Encoding and decoding utilities for various formats.
This submodule provides utilities for converting data between different formats: JSON, Base64, Hexadecimal, URL encoding, etc.
Examples¶
from rite.conversion.formats import ( ... formats_json_encode, ... formats_base64_encode, ... formats_hex_encode ... ) formats_json_encode({"key": "value"}) '{"key": "value"}' formats_base64_encode(b"hello") 'aGVsbG8=' formats_hex_encode(b"hello") '68656c6c6f'
Modules¶
formats_base64_decode ¶
Base64 Decoding¶
Convert base64 strings to bytes.
Examples¶
from rite.conversion.formats import formats_base64_decode formats_base64_decode('aGVsbG8=') b'hello' formats_base64_decode('invalid')
formats_base64_encode ¶
Base64 Encoding¶
Convert bytes to base64 string representation.
Examples¶
from rite.conversion.formats import formats_base64_encode formats_base64_encode(b"hello") 'aGVsbG8=' formats_base64_encode("hello") 'aGVsbG8='
formats_hex_decode ¶
Hexadecimal Decoding¶
Convert hexadecimal strings to bytes.
Examples¶
from rite.conversion.formats import formats_hex_decode formats_hex_decode('68656c6c6f') b'hello' formats_hex_decode('invalid')
Decode hexadecimal string to bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
Hexadecimal string (with or without spaces). |
required |
default
|
bytes | None
|
Value to return if decoding fails. |
None
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
Decoded bytes or default if decoding fails. |
Examples:
formats_hex_encode ¶
Hexadecimal Encoding¶
Convert bytes to hexadecimal string representation.
Examples¶
from rite.conversion.formats import formats_hex_encode formats_hex_encode(b"hello") '68656c6c6f' formats_hex_encode("hello") '68656c6c6f'
formats_json_decode ¶
JSON Decoding¶
Convert JSON strings to Python objects.
Examples¶
from rite.conversion.formats import formats_json_decode formats_json_decode('{"key": "value"}') {'key': 'value'} formats_json_decode('[1, 2, 3]') [1, 2, 3]
Convert JSON string to Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str | bytes
|
JSON string or bytes to decode. |
required |
default
|
Any
|
Value to return if decoding fails. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Decoded Python object or default if decoding fails. |
Examples:
formats_json_encode ¶
JSON Encoding¶
Convert Python objects to JSON strings.
Examples¶
from rite.conversion.formats import formats_json_encode formats_json_encode({"key": "value"}) '{"key": "value"}' formats_json_encode([1, 2, 3]) '[1, 2, 3]'
formats_json_encode(obj: Any, *, indent: int | None = None, sort_keys: bool = False, ensure_ascii: bool = True) -> str
Convert Python object to JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Python object to encode. |
required |
indent
|
int | None
|
Number of spaces for indentation (None for compact). |
None
|
sort_keys
|
bool
|
If True, sort dictionary keys. |
False
|
ensure_ascii
|
bool
|
If True, escape non-ASCII characters. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
JSON string representation. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If object is not JSON serializable. |
Examples:
formats_url_decode ¶
URL Decoding¶
Convert URL-encoded strings back to normal format.
Examples¶
from rite.conversion.formats import formats_url_decode formats_url_decode('hello+world') 'hello world' formats_url_decode('key%3Dvalue') 'key=value'
formats_url_encode ¶
URL Encoding¶
Convert strings to URL-safe format.
Examples¶
from rite.conversion.formats import formats_url_encode formats_url_encode("hello world") 'hello+world' formats_url_encode("key=value&foo=bar") 'key%3Dvalue%26foo%3Dbar'
formats_base64_decode ¶
Base64 Decoding¶
Convert base64 strings to bytes.
Examples¶
from rite.conversion.formats import formats_base64_decode formats_base64_decode('aGVsbG8=') b'hello' formats_base64_decode('invalid')
formats_base64_encode ¶
Base64 Encoding¶
Convert bytes to base64 string representation.
Examples¶
from rite.conversion.formats import formats_base64_encode formats_base64_encode(b"hello") 'aGVsbG8=' formats_base64_encode("hello") 'aGVsbG8='
formats_hex_decode ¶
Hexadecimal Decoding¶
Convert hexadecimal strings to bytes.
Examples¶
from rite.conversion.formats import formats_hex_decode formats_hex_decode('68656c6c6f') b'hello' formats_hex_decode('invalid')
Functions¶
formats_hex_decode ¶
Decode hexadecimal string to bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
Hexadecimal string (with or without spaces). |
required |
default
|
bytes | None
|
Value to return if decoding fails. |
None
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
Decoded bytes or default if decoding fails. |
Examples:
formats_hex_encode ¶
Hexadecimal Encoding¶
Convert bytes to hexadecimal string representation.
Examples¶
from rite.conversion.formats import formats_hex_encode formats_hex_encode(b"hello") '68656c6c6f' formats_hex_encode("hello") '68656c6c6f'
formats_json_decode ¶
JSON Decoding¶
Convert JSON strings to Python objects.
Examples¶
from rite.conversion.formats import formats_json_decode formats_json_decode('{"key": "value"}') {'key': 'value'} formats_json_decode('[1, 2, 3]') [1, 2, 3]
Functions¶
formats_json_decode ¶
Convert JSON string to Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str | bytes
|
JSON string or bytes to decode. |
required |
default
|
Any
|
Value to return if decoding fails. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Decoded Python object or default if decoding fails. |
Examples:
formats_json_encode ¶
JSON Encoding¶
Convert Python objects to JSON strings.
Examples¶
from rite.conversion.formats import formats_json_encode formats_json_encode({"key": "value"}) '{"key": "value"}' formats_json_encode([1, 2, 3]) '[1, 2, 3]'
Functions¶
formats_json_encode ¶
formats_json_encode(obj: Any, *, indent: int | None = None, sort_keys: bool = False, ensure_ascii: bool = True) -> str
Convert Python object to JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Python object to encode. |
required |
indent
|
int | None
|
Number of spaces for indentation (None for compact). |
None
|
sort_keys
|
bool
|
If True, sort dictionary keys. |
False
|
ensure_ascii
|
bool
|
If True, escape non-ASCII characters. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
JSON string representation. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If object is not JSON serializable. |
Examples:
formats_url_decode ¶
URL Decoding¶
Convert URL-encoded strings back to normal format.
Examples¶
from rite.conversion.formats import formats_url_decode formats_url_decode('hello+world') 'hello world' formats_url_decode('key%3Dvalue') 'key=value'
formats_url_encode ¶
URL Encoding¶
Convert strings to URL-safe format.
Examples¶
from rite.conversion.formats import formats_url_encode formats_url_encode("hello world") 'hello+world' formats_url_encode("key=value&foo=bar") 'key%3Dvalue%26foo%3Dbar'
is_protected_type ¶
Protected Type Check Module¶
Utilities for identifying protected scalar types.
Functions¶
is_protected_type ¶
Check if an object is a protected scalar type.
Protected types are simple scalar values that should not be converted to strings or bytes when strings_only mode is enabled.
obj: Object to check.
bool: True if obj is a protected type.
Example:¶
>>> is_protected_type(42)
True
>>> is_protected_type(None)
True
>>> is_protected_type("hello")
False
to_bool ¶
Boolean Conversion¶
Convert values to boolean representation.
This function converts various types of values to boolean, including strings like "yes", "no", "true", "false", numbers, etc.
Example
to_bool("yes") True to_bool("no") False to_bool(1) True to_bool(0) False
Functions¶
to_bool ¶
Convert a value to a boolean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (string, number, bool, or None) |
required |
Returns:
| Type | Description |
|---|---|
bool | None
|
Boolean representation or None if conversion fails |
Example
to_bool("yes") True to_bool(0) False to_bool(None) None
to_bytes ¶
Bytes Conversion Module¶
Provides utilities for converting various types to bytes.
Functions¶
to_bytes ¶
to_bytes(content: Any, *, encoding: str = 'utf-8', errors: str = 'strict', strings_only: bool = False) -> bytes | bytearray | Any
Convert content to a byte representation.
content: Content to convert to bytes.
encoding: Character encoding to use.
errors: Error handling scheme.
strings_only: If True, leave protected types unconverted.
bytes | bytearray | Any: Byte representation or original if protected.
Example:¶
>>> to_bytes("hello")
b'hello'
>>> to_bytes(42, strings_only=True)
42
>>> to_bytes(42, strings_only=False)
b'42'
Notes:¶
Behavior:
- bytes/bytearray: returned as-is
- memoryview: converted to bytes
- objects with __bytes__: bytes(obj)
- os.PathLike: os.fspath(obj) encoded with encoding
- str: encoded with encoding/errors
- everything else: str(obj).encode(encoding, errors)
If strings_only=True, protected types (None, numbers, dates, etc.)
are returned unmodified.
to_number ¶
Number Conversion¶
Parse a number from strings including those with units or suffixes like '20 m', '45.5 %', '100/ha'.
Functions¶
to_number ¶
Parse a number from messy strings like '20 m', '45.5 %', '100/ha'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (number, string, or None) |
required |
Returns:
| Type | Description |
|---|---|
float | None
|
Float representation or None if parsing fails |
Example
to_number("45.5 %") 45.5 to_number(100) 100.0 to_number("invalid") None
to_percentage ¶
Percentage Conversion¶
Accepts numbers, strings with %, or floats between 0-1 and converts to percentage value (0-100).
Functions¶
to_percentage ¶
Convert value to percentage (0-100).
Accepts 35, "35", "35%", 0.35 (interpreted as 35% if 0<=x<=1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (number, string, or None) |
required |
Returns:
| Type | Description |
|---|---|
float | None
|
Percentage value (0.0-100.0) or None if conversion fails |
Example
to_percentage(0.35) 35.0 to_percentage("35%") 35.0 to_percentage(150) 100.0
types ¶
Type Conversion Utilities¶
Basic Python type conversions.
This submodule provides utilities for converting between Python's built-in types: int, float, str, bool, bytes, list, dict, set, tuple.
Examples¶
from rite.conversion.types import ( ... types_to_int, ... types_to_bool, ... types_to_list ... ) types_to_int("42") 42 types_to_bool("yes") True types_to_list((1, 2, 3)) [1, 2, 3]
Modules¶
types_to_bool ¶
Boolean Conversion¶
Convert values to boolean representation.
Examples¶
from rite.conversion.types import types_to_bool types_to_bool("yes") True types_to_bool("no") False types_to_bool(1) True
Convert a value to a boolean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (string, number, bool, or None). |
required |
default
|
bool | None
|
Default value if conversion fails. |
None
|
Returns:
| Type | Description |
|---|---|
bool | None
|
Boolean representation or default/None if conversion fails. |
Examples:
types_to_bytes ¶
Bytes Conversion Module¶
Convert various types to bytes representation.
Examples¶
from rite.conversion.types import types_to_bytes types_to_bytes("hello") b'hello' types_to_bytes(42) b'42'
types_to_bytes(content: Any, *, encoding: str = 'utf-8', errors: str = 'strict', strings_only: bool = False) -> bytes | bytearray | Any
Convert content to a byte representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Any
|
Content to convert to bytes. |
required |
encoding
|
str
|
Character encoding to use. |
'utf-8'
|
errors
|
str
|
Error handling scheme. |
'strict'
|
strings_only
|
bool
|
If True, leave protected types unconverted. |
False
|
Returns:
| Type | Description |
|---|---|
bytes | bytearray | Any
|
Byte representation or original if protected. |
Examples:
>>> types_to_bytes("hello")
b'hello'
>>> types_to_bytes(42, strings_only=True)
42
>>> types_to_bytes(42, strings_only=False)
b'42'
>>> types_to_bytes(b"bytes")
b'bytes'
Notes
Behavior: - bytes/bytearray: returned as-is - memoryview: converted to bytes - objects with bytes: bytes(obj) - os.PathLike: os.fspath(obj) encoded - str: encoded with encoding/errors - everything else: str(obj).encode()
If strings_only=True, protected types (None, numbers) are returned unmodified.
types_to_dict ¶
Dictionary Conversion¶
Convert sequences and mappings to dictionary representation.
Examples¶
from rite.conversion.types import types_to_dict types_to_dict([("a", 1), ("b", 2)]) {'a': 1, 'b': 2} types_to_dict({"x": 10})
Convert a value to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (mapping, sequence of pairs, or list). |
required |
key_attr
|
str | None
|
Attribute name to use as key for object sequences. |
None
|
Returns:
| Type | Description |
|---|---|
dict[Any, Any]
|
Dictionary representation of the value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If conversion is not possible. |
Examples:
>>> types_to_dict([("a", 1), ("b", 2)])
{'a': 1, 'b': 2}
>>> types_to_dict({"x": 10, "y": 20})
{'x': 10, 'y': 20}
>>> types_to_dict([("x", 1)])
{'x': 1}
Notes
Objects with key_attr will use obj.key_attr as dictionary key.
types_to_float ¶
Float Conversion¶
Convert values to floating-point representation.
Examples¶
from rite.conversion.types import types_to_float types_to_float("3.14") 3.14 types_to_float(42) 42.0 types_to_float("invalid")
Convert a value to a float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (string, number, bool, or None). |
required |
default
|
float | None
|
Default value if conversion fails. |
None
|
Returns:
| Type | Description |
|---|---|
float | None
|
Float representation or default/None if conversion fails. |
Examples:
types_to_int ¶
Integer Conversion¶
Convert values to integer representation.
Examples¶
from rite.conversion.types import types_to_int types_to_int("42") 42 types_to_int(3.14) 3 types_to_int("invalid")
Convert a value to an integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (string, number, bool, or None). |
required |
default
|
int | None
|
Default value if conversion fails. |
None
|
Returns:
| Type | Description |
|---|---|
int | None
|
Integer representation or default/None if conversion fails. |
Examples:
types_to_list ¶
List Conversion¶
Convert values to list representation.
Examples¶
from rite.conversion.types import types_to_list types_to_list((1, 2, 3)) [1, 2, 3] types_to_list("hello") ['h', 'e', 'l', 'l', 'o'] types_to_list(42) [42]
types_to_number ¶
Number Conversion¶
Parse numbers from strings including those with units or suffixes.
Examples¶
from rite.conversion.types import types_to_number types_to_number("45.5 %") 45.5 types_to_number("20 m") 20.0
Parse a number from strings like '20 m', '45.5 %', '100/ha'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (number, string, or None). |
required |
default
|
float | None
|
Default value if parsing fails. |
None
|
Returns:
| Type | Description |
|---|---|
float | None
|
Float representation or default/None if parsing fails. |
Examples:
types_to_percentage ¶
Percentage Conversion¶
Convert values to percentage (0-100 range).
Examples¶
from rite.conversion.types import types_to_percentage types_to_percentage(0.35) 35.0 types_to_percentage("35%") 35.0
Convert value to percentage (0-100).
Accepts 35, "35", "35%", 0.35 (interpreted as 35% if 0<=x<=1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (number, string, or None). |
required |
default
|
float | None
|
Default value if conversion fails. |
None
|
clamp
|
bool
|
If True, clamp result to 0-100 range. |
True
|
Returns:
| Type | Description |
|---|---|
float | None
|
Percentage value (0.0-100.0) or default if conversion fails. |
Examples:
types_to_set ¶
Set Conversion¶
Convert iterables to set representation.
Examples¶
from rite.conversion.types import types_to_set types_to_set([1, 2, 2, 3]) {1, 2, 3} types_to_set("hello")
Convert a value to a set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert to set (iterable or single value). |
required |
Returns:
| Type | Description |
|---|---|
set[Any]
|
Set representation of the value. |
Examples:
>>> types_to_set([1, 2, 2, 3])
{1, 2, 3}
>>> types_to_set((1, 2, 3))
{1, 2, 3}
>>> types_to_set("hello")
{'h', 'e', 'l', 'o'}
>>> types_to_set(42)
{42}
Notes
Strings are treated as iterables of characters. Single values are wrapped in a set.
types_to_str ¶
String Conversion¶
Convert values to string representation.
Examples¶
from rite.conversion.types import types_to_str types_to_str(42) '42' types_to_str([1, 2, 3]) '[1, 2, 3]' types_to_str(None) ''
Convert a value to a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert to string. |
required |
none_as_empty
|
bool
|
If True, None converts to empty string. |
True
|
encoding
|
str
|
Encoding to use for bytes objects. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
str
|
String representation of the value. |
Examples:
types_to_tuple ¶
Tuple Conversion¶
Convert iterables to tuple representation.
Examples¶
from rite.conversion.types import types_to_tuple types_to_tuple([1, 2, 3]) (1, 2, 3) types_to_tuple("hello") ('h', 'e', 'l', 'l', 'o')
types_to_bool ¶
Boolean Conversion¶
Convert values to boolean representation.
Examples¶
from rite.conversion.types import types_to_bool types_to_bool("yes") True types_to_bool("no") False types_to_bool(1) True
Functions¶
types_to_bool ¶
Convert a value to a boolean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (string, number, bool, or None). |
required |
default
|
bool | None
|
Default value if conversion fails. |
None
|
Returns:
| Type | Description |
|---|---|
bool | None
|
Boolean representation or default/None if conversion fails. |
Examples:
types_to_bytes ¶
Bytes Conversion Module¶
Convert various types to bytes representation.
Examples¶
from rite.conversion.types import types_to_bytes types_to_bytes("hello") b'hello' types_to_bytes(42) b'42'
Functions¶
types_to_bytes ¶
types_to_bytes(content: Any, *, encoding: str = 'utf-8', errors: str = 'strict', strings_only: bool = False) -> bytes | bytearray | Any
Convert content to a byte representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Any
|
Content to convert to bytes. |
required |
encoding
|
str
|
Character encoding to use. |
'utf-8'
|
errors
|
str
|
Error handling scheme. |
'strict'
|
strings_only
|
bool
|
If True, leave protected types unconverted. |
False
|
Returns:
| Type | Description |
|---|---|
bytes | bytearray | Any
|
Byte representation or original if protected. |
Examples:
>>> types_to_bytes("hello")
b'hello'
>>> types_to_bytes(42, strings_only=True)
42
>>> types_to_bytes(42, strings_only=False)
b'42'
>>> types_to_bytes(b"bytes")
b'bytes'
Notes
Behavior: - bytes/bytearray: returned as-is - memoryview: converted to bytes - objects with bytes: bytes(obj) - os.PathLike: os.fspath(obj) encoded - str: encoded with encoding/errors - everything else: str(obj).encode()
If strings_only=True, protected types (None, numbers) are returned unmodified.
types_to_dict ¶
Dictionary Conversion¶
Convert sequences and mappings to dictionary representation.
Examples¶
from rite.conversion.types import types_to_dict types_to_dict([("a", 1), ("b", 2)]) {'a': 1, 'b': 2} types_to_dict({"x": 10})
Functions¶
types_to_dict ¶
Convert a value to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (mapping, sequence of pairs, or list). |
required |
key_attr
|
str | None
|
Attribute name to use as key for object sequences. |
None
|
Returns:
| Type | Description |
|---|---|
dict[Any, Any]
|
Dictionary representation of the value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If conversion is not possible. |
Examples:
>>> types_to_dict([("a", 1), ("b", 2)])
{'a': 1, 'b': 2}
>>> types_to_dict({"x": 10, "y": 20})
{'x': 10, 'y': 20}
>>> types_to_dict([("x", 1)])
{'x': 1}
Notes
Objects with key_attr will use obj.key_attr as dictionary key.
types_to_float ¶
Float Conversion¶
Convert values to floating-point representation.
Examples¶
from rite.conversion.types import types_to_float types_to_float("3.14") 3.14 types_to_float(42) 42.0 types_to_float("invalid")
Functions¶
types_to_float ¶
Convert a value to a float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (string, number, bool, or None). |
required |
default
|
float | None
|
Default value if conversion fails. |
None
|
Returns:
| Type | Description |
|---|---|
float | None
|
Float representation or default/None if conversion fails. |
Examples:
types_to_int ¶
Integer Conversion¶
Convert values to integer representation.
Examples¶
from rite.conversion.types import types_to_int types_to_int("42") 42 types_to_int(3.14) 3 types_to_int("invalid")
Functions¶
types_to_int ¶
Convert a value to an integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (string, number, bool, or None). |
required |
default
|
int | None
|
Default value if conversion fails. |
None
|
Returns:
| Type | Description |
|---|---|
int | None
|
Integer representation or default/None if conversion fails. |
Examples:
types_to_list ¶
List Conversion¶
Convert values to list representation.
Examples¶
from rite.conversion.types import types_to_list types_to_list((1, 2, 3)) [1, 2, 3] types_to_list("hello") ['h', 'e', 'l', 'l', 'o'] types_to_list(42) [42]
types_to_number ¶
Number Conversion¶
Parse numbers from strings including those with units or suffixes.
Examples¶
from rite.conversion.types import types_to_number types_to_number("45.5 %") 45.5 types_to_number("20 m") 20.0
Functions¶
types_to_number ¶
Parse a number from strings like '20 m', '45.5 %', '100/ha'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (number, string, or None). |
required |
default
|
float | None
|
Default value if parsing fails. |
None
|
Returns:
| Type | Description |
|---|---|
float | None
|
Float representation or default/None if parsing fails. |
Examples:
types_to_percentage ¶
Percentage Conversion¶
Convert values to percentage (0-100 range).
Examples¶
from rite.conversion.types import types_to_percentage types_to_percentage(0.35) 35.0 types_to_percentage("35%") 35.0
Functions¶
types_to_percentage ¶
Convert value to percentage (0-100).
Accepts 35, "35", "35%", 0.35 (interpreted as 35% if 0<=x<=1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (number, string, or None). |
required |
default
|
float | None
|
Default value if conversion fails. |
None
|
clamp
|
bool
|
If True, clamp result to 0-100 range. |
True
|
Returns:
| Type | Description |
|---|---|
float | None
|
Percentage value (0.0-100.0) or default if conversion fails. |
Examples:
types_to_set ¶
Set Conversion¶
Convert iterables to set representation.
Examples¶
from rite.conversion.types import types_to_set types_to_set([1, 2, 2, 3]) {1, 2, 3} types_to_set("hello")
Functions¶
types_to_set ¶
Convert a value to a set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert to set (iterable or single value). |
required |
Returns:
| Type | Description |
|---|---|
set[Any]
|
Set representation of the value. |
Examples:
>>> types_to_set([1, 2, 2, 3])
{1, 2, 3}
>>> types_to_set((1, 2, 3))
{1, 2, 3}
>>> types_to_set("hello")
{'h', 'e', 'l', 'o'}
>>> types_to_set(42)
{42}
Notes
Strings are treated as iterables of characters. Single values are wrapped in a set.
types_to_str ¶
String Conversion¶
Convert values to string representation.
Examples¶
from rite.conversion.types import types_to_str types_to_str(42) '42' types_to_str([1, 2, 3]) '[1, 2, 3]' types_to_str(None) ''
Functions¶
types_to_str ¶
Convert a value to a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert to string. |
required |
none_as_empty
|
bool
|
If True, None converts to empty string. |
True
|
encoding
|
str
|
Encoding to use for bytes objects. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
str
|
String representation of the value. |
Examples:
types_to_tuple ¶
Tuple Conversion¶
Convert iterables to tuple representation.
Examples¶
from rite.conversion.types import types_to_tuple types_to_tuple([1, 2, 3]) (1, 2, 3) types_to_tuple("hello") ('h', 'e', 'l', 'l', 'o')
units ¶
Unit Conversion Utilities¶
Physical unit conversions.
This submodule provides utilities for converting between different units of measurement: temperature, length, weight, time, etc.
Examples¶
from rite.conversion.units import ( ... units_celsius_to_fahrenheit, ... units_meters_to_feet, ... units_grams_to_pounds ... ) units_celsius_to_fahrenheit(0) 32.0 units_meters_to_feet(1) 3.28084 round(units_grams_to_pounds(453.592), 2) 1.0
Functions¶
units_celsius_to_fahrenheit ¶
Convert Celsius to Fahrenheit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
celsius
|
float
|
Temperature in Celsius. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Fahrenheit. |
Examples:
units_celsius_to_kelvin ¶
units_days_to_seconds ¶
units_fahrenheit_to_celsius ¶
Convert Fahrenheit to Celsius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fahrenheit
|
float
|
Temperature in Fahrenheit. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Celsius. |
Examples:
units_fahrenheit_to_kelvin ¶
units_feet_to_meters ¶
units_grams_to_kilograms ¶
units_grams_to_ounces ¶
units_grams_to_pounds ¶
units_hours_to_minutes ¶
units_hours_to_seconds ¶
units_inches_to_meters ¶
units_kelvin_to_celsius ¶
Convert Kelvin to Celsius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kelvin
|
float
|
Temperature in Kelvin. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Celsius. |
Examples:
units_kelvin_to_fahrenheit ¶
units_kilograms_to_grams ¶
units_kilometers_to_meters ¶
units_meters_to_feet ¶
units_meters_to_inches ¶
units_meters_to_kilometers ¶
units_meters_to_miles ¶
units_miles_to_meters ¶
units_minutes_to_hours ¶
units_minutes_to_seconds ¶
units_ounces_to_grams ¶
units_pounds_to_grams ¶
units_seconds_to_days ¶
units_seconds_to_hours ¶
units_seconds_to_minutes ¶
Modules¶
units_length ¶
Length Unit Conversion¶
Convert between meters, feet, inches, miles, and kilometers.
Examples¶
from rite.conversion.units import units_meters_to_feet units_meters_to_feet(1) 3.28084 units_meters_to_feet(10) 32.8084
units_temperature ¶
Temperature Unit Conversion¶
Convert between Celsius, Fahrenheit, and Kelvin.
Examples¶
from rite.conversion.units import units_celsius_to_fahrenheit units_celsius_to_fahrenheit(0) 32.0 units_celsius_to_fahrenheit(100) 212.0
Convert Celsius to Fahrenheit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
celsius
|
float
|
Temperature in Celsius. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Fahrenheit. |
Examples:
Convert Fahrenheit to Celsius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fahrenheit
|
float
|
Temperature in Fahrenheit. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Celsius. |
Examples:
Convert Kelvin to Celsius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kelvin
|
float
|
Temperature in Kelvin. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Celsius. |
Examples:
units_time ¶
Time Unit Conversion¶
Convert between seconds, minutes, hours, and days.
Examples¶
from rite.conversion.units import units_seconds_to_minutes units_seconds_to_minutes(60) 1.0 units_seconds_to_minutes(120) 2.0
units_weight ¶
Weight Unit Conversion¶
Convert between grams, kilograms, pounds, and ounces.
Examples¶
from rite.conversion.units import units_grams_to_kilograms units_grams_to_kilograms(1000) 1.0 units_grams_to_kilograms(500) 0.5
Submodules¶
Type Conversions¶
Convert between Python types.
Type Conversion Utilities¶
Basic Python type conversions.
This submodule provides utilities for converting between Python's built-in types: int, float, str, bool, bytes, list, dict, set, tuple.
Examples¶
from rite.conversion.types import ( ... types_to_int, ... types_to_bool, ... types_to_list ... ) types_to_int("42") 42 types_to_bool("yes") True types_to_list((1, 2, 3)) [1, 2, 3]
Modules¶
types_to_int ¶
Integer Conversion¶
Convert values to integer representation.
Examples¶
from rite.conversion.types import types_to_int types_to_int("42") 42 types_to_int(3.14) 3 types_to_int("invalid")
Functions¶
types_to_int ¶
Convert a value to an integer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (string, number, bool, or None). |
required |
default
|
int | None
|
Default value if conversion fails. |
None
|
Returns:
| Type | Description |
|---|---|
int | None
|
Integer representation or default/None if conversion fails. |
Examples:
types_to_float ¶
Float Conversion¶
Convert values to floating-point representation.
Examples¶
from rite.conversion.types import types_to_float types_to_float("3.14") 3.14 types_to_float(42) 42.0 types_to_float("invalid")
Functions¶
types_to_float ¶
Convert a value to a float.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (string, number, bool, or None). |
required |
default
|
float | None
|
Default value if conversion fails. |
None
|
Returns:
| Type | Description |
|---|---|
float | None
|
Float representation or default/None if conversion fails. |
Examples:
types_to_str ¶
String Conversion¶
Convert values to string representation.
Examples¶
from rite.conversion.types import types_to_str types_to_str(42) '42' types_to_str([1, 2, 3]) '[1, 2, 3]' types_to_str(None) ''
Functions¶
types_to_str ¶
Convert a value to a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert to string. |
required |
none_as_empty
|
bool
|
If True, None converts to empty string. |
True
|
encoding
|
str
|
Encoding to use for bytes objects. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
str
|
String representation of the value. |
Examples:
types_to_bool ¶
Boolean Conversion¶
Convert values to boolean representation.
Examples¶
from rite.conversion.types import types_to_bool types_to_bool("yes") True types_to_bool("no") False types_to_bool(1) True
Functions¶
types_to_bool ¶
Convert a value to a boolean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (string, number, bool, or None). |
required |
default
|
bool | None
|
Default value if conversion fails. |
None
|
Returns:
| Type | Description |
|---|---|
bool | None
|
Boolean representation or default/None if conversion fails. |
Examples:
types_to_list ¶
List Conversion¶
Convert values to list representation.
Examples¶
from rite.conversion.types import types_to_list types_to_list((1, 2, 3)) [1, 2, 3] types_to_list("hello") ['h', 'e', 'l', 'l', 'o'] types_to_list(42) [42]
types_to_dict ¶
Dictionary Conversion¶
Convert sequences and mappings to dictionary representation.
Examples¶
from rite.conversion.types import types_to_dict types_to_dict([("a", 1), ("b", 2)]) {'a': 1, 'b': 2} types_to_dict({"x": 10})
Functions¶
types_to_dict ¶
Convert a value to a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Value to convert (mapping, sequence of pairs, or list). |
required |
key_attr
|
str | None
|
Attribute name to use as key for object sequences. |
None
|
Returns:
| Type | Description |
|---|---|
dict[Any, Any]
|
Dictionary representation of the value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If conversion is not possible. |
Examples:
>>> types_to_dict([("a", 1), ("b", 2)])
{'a': 1, 'b': 2}
>>> types_to_dict({"x": 10, "y": 20})
{'x': 10, 'y': 20}
>>> types_to_dict([("x", 1)])
{'x': 1}
Notes
Objects with key_attr will use obj.key_attr as dictionary key.
types_to_bytes ¶
Bytes Conversion Module¶
Convert various types to bytes representation.
Examples¶
from rite.conversion.types import types_to_bytes types_to_bytes("hello") b'hello' types_to_bytes(42) b'42'
Functions¶
types_to_bytes ¶
types_to_bytes(content: Any, *, encoding: str = 'utf-8', errors: str = 'strict', strings_only: bool = False) -> bytes | bytearray | Any
Convert content to a byte representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
Any
|
Content to convert to bytes. |
required |
encoding
|
str
|
Character encoding to use. |
'utf-8'
|
errors
|
str
|
Error handling scheme. |
'strict'
|
strings_only
|
bool
|
If True, leave protected types unconverted. |
False
|
Returns:
| Type | Description |
|---|---|
bytes | bytearray | Any
|
Byte representation or original if protected. |
Examples:
>>> types_to_bytes("hello")
b'hello'
>>> types_to_bytes(42, strings_only=True)
42
>>> types_to_bytes(42, strings_only=False)
b'42'
>>> types_to_bytes(b"bytes")
b'bytes'
Notes
Behavior: - bytes/bytearray: returned as-is - memoryview: converted to bytes - objects with bytes: bytes(obj) - os.PathLike: os.fspath(obj) encoded - str: encoded with encoding/errors - everything else: str(obj).encode()
If strings_only=True, protected types (None, numbers) are returned unmodified.
Format Conversions¶
Convert between data formats.
Format Conversion Utilities¶
Encoding and decoding utilities for various formats.
This submodule provides utilities for converting data between different formats: JSON, Base64, Hexadecimal, URL encoding, etc.
Examples¶
from rite.conversion.formats import ( ... formats_json_encode, ... formats_base64_encode, ... formats_hex_encode ... ) formats_json_encode({"key": "value"}) '{"key": "value"}' formats_base64_encode(b"hello") 'aGVsbG8=' formats_hex_encode(b"hello") '68656c6c6f'
Modules¶
formats_base64_encode ¶
Base64 Encoding¶
Convert bytes to base64 string representation.
Examples¶
from rite.conversion.formats import formats_base64_encode formats_base64_encode(b"hello") 'aGVsbG8=' formats_base64_encode("hello") 'aGVsbG8='
formats_base64_decode ¶
Base64 Decoding¶
Convert base64 strings to bytes.
Examples¶
from rite.conversion.formats import formats_base64_decode formats_base64_decode('aGVsbG8=') b'hello' formats_base64_decode('invalid')
formats_hex_encode ¶
Hexadecimal Encoding¶
Convert bytes to hexadecimal string representation.
Examples¶
from rite.conversion.formats import formats_hex_encode formats_hex_encode(b"hello") '68656c6c6f' formats_hex_encode("hello") '68656c6c6f'
formats_hex_decode ¶
Hexadecimal Decoding¶
Convert hexadecimal strings to bytes.
Examples¶
from rite.conversion.formats import formats_hex_decode formats_hex_decode('68656c6c6f') b'hello' formats_hex_decode('invalid')
Functions¶
formats_hex_decode ¶
Decode hexadecimal string to bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
Hexadecimal string (with or without spaces). |
required |
default
|
bytes | None
|
Value to return if decoding fails. |
None
|
Returns:
| Type | Description |
|---|---|
bytes | None
|
Decoded bytes or default if decoding fails. |
Examples:
formats_url_encode ¶
URL Encoding¶
Convert strings to URL-safe format.
Examples¶
from rite.conversion.formats import formats_url_encode formats_url_encode("hello world") 'hello+world' formats_url_encode("key=value&foo=bar") 'key%3Dvalue%26foo%3Dbar'
formats_url_decode ¶
URL Decoding¶
Convert URL-encoded strings back to normal format.
Examples¶
from rite.conversion.formats import formats_url_decode formats_url_decode('hello+world') 'hello world' formats_url_decode('key%3Dvalue') 'key=value'
formats_json_encode ¶
JSON Encoding¶
Convert Python objects to JSON strings.
Examples¶
from rite.conversion.formats import formats_json_encode formats_json_encode({"key": "value"}) '{"key": "value"}' formats_json_encode([1, 2, 3]) '[1, 2, 3]'
Functions¶
formats_json_encode ¶
formats_json_encode(obj: Any, *, indent: int | None = None, sort_keys: bool = False, ensure_ascii: bool = True) -> str
Convert Python object to JSON string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Python object to encode. |
required |
indent
|
int | None
|
Number of spaces for indentation (None for compact). |
None
|
sort_keys
|
bool
|
If True, sort dictionary keys. |
False
|
ensure_ascii
|
bool
|
If True, escape non-ASCII characters. |
True
|
Returns:
| Type | Description |
|---|---|
str
|
JSON string representation. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If object is not JSON serializable. |
Examples:
formats_json_decode ¶
JSON Decoding¶
Convert JSON strings to Python objects.
Examples¶
from rite.conversion.formats import formats_json_decode formats_json_decode('{"key": "value"}') {'key': 'value'} formats_json_decode('[1, 2, 3]') [1, 2, 3]
Functions¶
formats_json_decode ¶
Convert JSON string to Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str | bytes
|
JSON string or bytes to decode. |
required |
default
|
Any
|
Value to return if decoding fails. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Decoded Python object or default if decoding fails. |
Examples:
Unit Conversions¶
Convert between units of measurement.
Unit Conversion Utilities¶
Physical unit conversions.
This submodule provides utilities for converting between different units of measurement: temperature, length, weight, time, etc.
Examples¶
from rite.conversion.units import ( ... units_celsius_to_fahrenheit, ... units_meters_to_feet, ... units_grams_to_pounds ... ) units_celsius_to_fahrenheit(0) 32.0 units_meters_to_feet(1) 3.28084 round(units_grams_to_pounds(453.592), 2) 1.0
Modules¶
units_temperature ¶
Temperature Unit Conversion¶
Convert between Celsius, Fahrenheit, and Kelvin.
Examples¶
from rite.conversion.units import units_celsius_to_fahrenheit units_celsius_to_fahrenheit(0) 32.0 units_celsius_to_fahrenheit(100) 212.0
Functions¶
units_celsius_to_fahrenheit ¶
Convert Celsius to Fahrenheit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
celsius
|
float
|
Temperature in Celsius. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Fahrenheit. |
Examples:
units_celsius_to_kelvin ¶
units_fahrenheit_to_celsius ¶
Convert Fahrenheit to Celsius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fahrenheit
|
float
|
Temperature in Fahrenheit. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Celsius. |
Examples:
units_fahrenheit_to_kelvin ¶
units_kelvin_to_celsius ¶
Convert Kelvin to Celsius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kelvin
|
float
|
Temperature in Kelvin. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Temperature in Celsius. |
Examples:
units_kelvin_to_fahrenheit ¶
units_length ¶
Length Unit Conversion¶
Convert between meters, feet, inches, miles, and kilometers.
Examples¶
from rite.conversion.units import units_meters_to_feet units_meters_to_feet(1) 3.28084 units_meters_to_feet(10) 32.8084
units_weight ¶
Weight Unit Conversion¶
Convert between grams, kilograms, pounds, and ounces.
Examples¶
from rite.conversion.units import units_grams_to_kilograms units_grams_to_kilograms(1000) 1.0 units_grams_to_kilograms(500) 0.5
units_time ¶
Time Unit Conversion¶
Convert between seconds, minutes, hours, and days.
Examples¶
from rite.conversion.units import units_seconds_to_minutes units_seconds_to_minutes(60) 1.0 units_seconds_to_minutes(120) 2.0