Skip to content

pysmo.tools.utils #

Pysmo's little helpers.

Functions:

average_datetimes #

average_datetimes(
    datetimes: Sequence[datetime],
) -> datetime

Average a sequence of datetimes.

Parameters:

Returns:

  • datetime

    Datetime representing average of all datetimes.

Raises:

  • ValueError

    If an empty sequence is provides as input.

Source code in pysmo/tools/utils.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def average_datetimes(datetimes: Sequence[datetime]) -> datetime:
    """Average a sequence of datetimes.

    Parameters:
        datetimes: Datetimes to average.

    Returns:
        Datetime representing average of all datetimes.

    Raises:
        ValueError: If an empty sequence is provides as input.
    """
    if len(datetimes) == 0:
        raise ValueError("Cannot average empty sequence of datetimes.")
    reference_time = datetimes[0]
    seconds = sum((i - reference_time).total_seconds() for i in datetimes[1:])
    return reference_time + timedelta(seconds=seconds / len(datetimes))

uuid_shortener #

uuid_shortener(
    uuids: Sequence[UUID], min_length: int = 4
) -> dict[str, UUID]

Shorten a sequence of UUIDs to their shortest unique representation.

Parameters:

  • uuids (Sequence[UUID]) –

    UUIDs to shorten.

  • min_length (int, default: 4 ) –

    Minimum length of the shortened UUID strings.

Returns:

  • dict[str, UUID]

    Dictionary mapping shortened UUID strings to their original UUID objects.

Raises:

  • ValueError

    If an empty sequence is provided as input.

  • ValueError

    If min_length is not between 1 and 36.

Examples:

>>> import uuid
>>> from pysmo.tools.utils import uuid_shortener
>>>
>>> uuids = [uuid.uuid4() for _ in range(100)]
>>> for k, v in sorted(uuid_shortener(uuids).items()):
...     print(f"{k} -> {v}")
...
01d7 -> 01d74256-3860-4ab6-96a4-02f23ae8cc93
03c7 -> 03c72ba8-d605-4770-8a63-f881ffd0f9d5
080a -> 080aadfb-e7c9-4b26-9141-25c63a9bedd4
0e51 -> 0e51f30d-c6a7-4e39-84b0-32ccd7c524a5
...
>>>
Source code in pysmo/tools/utils.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def uuid_shortener(uuids: Sequence[UUID], min_length: int = 4) -> dict[str, UUID]:
    """Shorten a sequence of UUIDs to their shortest unique representation.

    Parameters:
        uuids: UUIDs to shorten.
        min_length: Minimum length of the shortened UUID strings.

    Returns:
        Dictionary mapping shortened UUID strings to their original UUID objects.

    Raises:
        ValueError: If an empty sequence is provided as input.
        ValueError: If `min_length` is not between 1 and 36.

    Examples:
        ```python
        >>> import uuid
        >>> from pysmo.tools.utils import uuid_shortener
        >>>
        >>> uuids = [uuid.uuid4() for _ in range(100)]
        >>> for k, v in sorted(uuid_shortener(uuids).items()):
        ...     print(f"{k} -> {v}")
        ...
        01d7 -> 01d74256-3860-4ab6-96a4-02f23ae8cc93
        03c7 -> 03c72ba8-d605-4770-8a63-f881ffd0f9d5
        080a -> 080aadfb-e7c9-4b26-9141-25c63a9bedd4
        0e51 -> 0e51f30d-c6a7-4e39-84b0-32ccd7c524a5
        ...
        >>>
        ```
    """
    UUID_LENGTH = 36

    if len(uuids) == 0:
        raise ValueError("Cannot shorten empty sequence of UUIDs.")

    if not (1 <= min_length <= UUID_LENGTH):
        raise ValueError(f"min_length must be between 1 and {UUID_LENGTH}.")

    for length in range(min_length, UUID_LENGTH + 1):
        shortened = [u[:length] for u in map(str, uuids)]
        if len(set(shortened)) == len(uuids):
            return {u: uuid for u, uuid in zip(shortened, uuids)}

    # Fallback to full UUIDs if necessary (like that's going to happen...)
    return {str(u): u for u in uuids}