Skip to content

Formats

The rite.conversion.formats submodule exposes helpers for converting between common data encodings such as Base64, hex, URL, and JSON.

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

Functions

formats_base64_decode(s: str, default: bytes | None = None) -> bytes | None

Decode base64 string to bytes.

Parameters:

Name Type Description Default
s str

Base64 encoded string.

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_base64_decode('aGVsbG8=')
b'hello'
>>> formats_base64_decode('invalid')
>>> formats_base64_decode('invalid', b'')
b''
>>> formats_base64_decode('5LiW55WM')
b'\xe4\xb8\x96\xe7\x95\x8c'

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

Functions

formats_base64_encode(data: bytes | str, encoding: str = 'utf-8') -> str

Encode bytes to base64 string.

Parameters:

Name Type Description Default
data bytes | str

Data to encode (bytes or string).

required
encoding str

String encoding if data is string.

'utf-8'

Returns:

Type Description
str

Base64 encoded string.

Examples:

>>> formats_base64_encode(b"hello")
'aGVsbG8='
>>> formats_base64_encode("hello")
'aGVsbG8='
>>> formats_base64_encode("世界")
'5LiW55WM'

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(s: str, default: bytes | None = None) -> bytes | None

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_decode('68656c6c6f')
b'hello'
>>> formats_hex_decode('68 65 6c 6c 6f')
b'hello'
>>> formats_hex_decode('invalid')
>>> formats_hex_decode('invalid', b'')
b''

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'

Functions

formats_hex_encode(data: bytes | str, encoding: str = 'utf-8') -> str

Encode bytes to hexadecimal string.

Parameters:

Name Type Description Default
data bytes | str

Data to encode (bytes or string).

required
encoding str

String encoding if data is string.

'utf-8'

Returns:

Type Description
str

Hexadecimal string (lowercase).

Examples:

>>> formats_hex_encode(b"hello")
'68656c6c6f'
>>> formats_hex_encode("hello")
'68656c6c6f'
>>> formats_hex_encode(b"\x00\xff")
'00ff'

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(s: str | bytes, default: Any = None) -> Any

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_decode('{"key": "value"}')
{'key': 'value'}
>>> formats_json_decode('[1, 2, 3]')
[1, 2, 3]
>>> formats_json_decode('invalid')
>>> formats_json_decode('invalid', {})
{}
>>> formats_json_decode(b'{"a": 1}')
{'a': 1}

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(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_encode({"b": 2, "a": 1})
'{"b": 2, "a": 1}'
>>> formats_json_encode({"b": 2, "a": 1}, sort_keys=True)
'{"a": 1, "b": 2}'
>>> formats_json_encode([1, 2, 3], indent=2)
'[\n  1,\n  2,\n  3\n]'

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'

Functions

formats_url_decode(s: str, encoding: str = 'utf-8') -> str

Decode URL-encoded string.

Parameters:

Name Type Description Default
s str

URL-encoded string.

required
encoding str

Character encoding to use.

'utf-8'

Returns:

Type Description
str

Decoded string.

Examples:

>>> formats_url_decode('hello+world')
'hello world'
>>> formats_url_decode('key%3Dvalue')
'key=value'
>>> formats_url_decode('a%2Fb%2Fc')
'a/b/c'

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'

Functions

formats_url_encode(s: str, safe: str = '', encoding: str = 'utf-8') -> str

Encode string for use in URLs.

Parameters:

Name Type Description Default
s str

String to encode.

required
safe str

Characters that should not be encoded.

''
encoding str

Character encoding to use.

'utf-8'

Returns:

Type Description
str

URL-encoded string.

Examples:

>>> formats_url_encode("hello world")
'hello+world'
>>> formats_url_encode("key=value")
'key%3Dvalue'
>>> formats_url_encode("a/b/c", safe="/")
'a/b/c'

options: show_root_heading: true show_source: false heading_level: 2