Skip to content

Utils

enum_converter(enum_name)

Return a callable that can be used in attr converters to convert values to enums

Parameters:

Name Type Description Default
enum_name str

The name of the enum the value has

required

Returns:

Type Description
Callable

A callable converter

Source code in src/bungio/utils.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def enum_converter(enum_name: str) -> Callable:
    """
    Return a callable that can be used in attr converters to convert values to enums

    Args:
        enum_name: The name of the enum the value has

    Returns:
        A callable converter
    """

    def converter(
        value: int | str | list[int | str],
    ) -> BaseEnum | UnknownEnumValue | list[BaseEnum | UnknownEnumValue]:
        if isinstance(value, list):
            return [converter(v) for v in value]

        elif isinstance(value, BaseEnum | UnknownEnumValue | type(MISSING)):
            return value

        imp = importlib.import_module("bungio.models")
        enum_class = getattr(imp, enum_name)
        try:
            return enum_class(value)
        except ValueError:
            return UnknownEnumValue(value=value, enum=enum_class)

    return converter

get_now_with_tz()

Returns the timezone aware current datetime

Returns:

Type Description
datetime

Timezone aware datetime

Source code in src/bungio/utils.py
10
11
12
13
14
15
16
17
18
def get_now_with_tz() -> datetime.datetime:
    """
    Returns the timezone aware current datetime

    Returns:
        Timezone aware datetime
    """

    return datetime.datetime.now(tz=datetime.timezone.utc)

split_list(to_split, chunk_size)

Yield successive n-sized chunks from list

Parameters:

Name Type Description Default
to_split list

The list to split

required
chunk_size int

How long the chunks should be

required

Returns:

Type Description
None

A generator which returns the chunks

Source code in src/bungio/utils.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def split_list(to_split: list, chunk_size: int) -> Generator[list, None, None]:
    """
    Yield successive n-sized chunks from list

    Args:
        to_split: The list to split
        chunk_size: How long the chunks should be

    Returns:
        A generator which returns the chunks
    """

    for i in range(0, len(to_split), chunk_size):
        yield to_split[i : i + chunk_size]