Skip to content

src.misc

Json2Obj

Json2Obj(data)

Converts a dictionary to an object.

Initializes the Json2Obj.

Parameters:

Name Type Description Default
data

The dictionary to convert.

required
Source code in src/misc.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def __init__(self, data):
    """Initializes the Json2Obj.

    Args:
        data: The dictionary to convert.
    """
    self.__dict__ = data
    for i in self.__dict__.keys():
        child = self.__dict__[i]
        if isinstance(child, dict):
            if len(child) > 0:
                self.__dict__[i] = Json2Obj(child)
        if isinstance(child, list):
            self.__dict__[i] = []
            for item in child:
                if isinstance(item, dict):
                    self.__dict__[i].append(Json2Obj(item))
                else:
                    self.__dict__[i].append(item)

generate_player_names

generate_player_names(n: int) -> list[str]

Generates a list of random player names.

Parameters:

Name Type Description Default
n int

The number of names to generate.

required

Returns:

Type Description
list[str]

A list of random names.

Source code in src/misc.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def generate_player_names(n: int) -> list[str]:
    """Generates a list of random player names.

    Args:
        n: The number of names to generate.

    Returns:
        A list of random names.
    """
    fkr = Faker()
    names: set[str] = set()
    while len(names) < n:
        names.add(fkr.name())
    return list(names)

timeit

timeit(method)

Decorator to measure execution time of a function.

Parameters:

Name Type Description Default
method

The function to measure.

required

Returns:

Type Description

The decorated function.

Source code in src/misc.py
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
73
74
75
def timeit(method):
    """Decorator to measure execution time of a function.

    Args:
        method: The function to measure.

    Returns:
        The decorated function.
    """

    def timed(*args, **kw):
        sys.stdout.write("\rt({}) ...".format(method.__name__))
        sys.stdout.flush()

        ts = time.time()
        result = method(*args, **kw)
        te = time.time()
        if "log_time" in kw:
            name = kw.get("log_name", method.__name__.upper())
            kw["log_time"][name] = int((te - ts) * 1000)
        else:
            # print('%r  %2.2f ms' % (method.__name__, (te - ts) * 1000))
            sys.stdout.write(
                "\rt({}) = {:.2f} ms\n".format(method.__name__, (te - ts) * 1000)
            )
            sys.stdout.flush()
        return result

    return timed