Skip to content

Utilities

Floating point numbers

Module for handling floating point numbers in ATProtocol records.

ATProtocol doesn't support floating point numbers directly, so this module provides functions to convert floats to specially formatted strings and back again.

decode_floats_in_event(event)

Recursively decode all specially marked strings in an event back to floating point numbers.

Parameters:

Name Type Description Default
event dict[str, Any]

The event dictionary to process.

required

Returns:

Type Description
dict[str, Any]

A new dictionary with all encoded strings converted back to floating point numbers.

Source code in nebra/floats.py
def decode_floats_in_event(event: dict[str, Any]) -> dict[str, Any]:
    """Recursively decode all specially marked strings in an event back to floating point numbers.

    Parameters
    ----------
    event : dict[str, Any]
        The event dictionary to process.

    Returns
    -------
    dict[str, Any]
        A new dictionary with all encoded strings converted back to floating point numbers.
    """
    return _decode_floats_recursive(event)

encode_floats_in_event(event)

Recursively encode all floating point numbers in an event as specially marked strings.

Parameters:

Name Type Description Default
event dict[str, Any]

The event dictionary to process.

required

Returns:

Type Description
dict[str, Any]

A new dictionary with all floating point numbers encoded as strings.

Source code in nebra/floats.py
def encode_floats_in_event(event: dict[str, Any]) -> dict[str, Any]:
    """Recursively encode all floating point numbers in an event as specially marked strings.

    Parameters
    ----------
    event : dict[str, Any]
        The event dictionary to process.

    Returns
    -------
    dict[str, Any]
        A new dictionary with all floating point numbers encoded as strings.
    """
    return _encode_floats_recursive(event)

Time

These helpers can create fully AT Protocol-compliant timestamps.

get_atproto_utc_time()

Get a maximally ATProto-compatible UTC datetime string.

This function returns a datetime string in the format specified by the ATProtocol lexicon, which is "YYYY-MM-DDTHH:MM:SS.sssZ".

Returns:

Type Description
str

A datetime string in ATProtocol-compatible format.

See Also

https://atproto.com/specs/lexicon#datetime : ATProtocol datetime specification

Source code in nebra/time.py
def get_atproto_utc_time():
    """Get a maximally ATProto-compatible UTC datetime string.

    This function returns a datetime string in the format specified by the ATProtocol
    lexicon, which is "YYYY-MM-DDTHH:MM:SS.sssZ".

    Returns
    -------
    str
        A datetime string in ATProtocol-compatible format.

    See Also
    --------
    https://atproto.com/specs/lexicon#datetime : ATProtocol datetime specification
    """
    return datetime.now(UTC).strftime("%Y-%m-%dT%H:%M:%S.%fZ")