Skip to content

Names

NAME_EXAMPLE = 'YearYYYYDayDD' module-attribute

The example of the name format.

get_key_by_name(name: str) -> Key

Gets the key representing the problem by the name of the solution type.

Note

The name must match the format of NAME_EXAMPLE.

Parameters:

Name Type Description Default
name str

The name of the solution type.

required

Returns:

Type Description
Key

The key representing the problem.

Raises:

Type Description
TypeError

The name does not match the expected format.

ValueError

The year or the day is not valid.

InternalError

The pattern was matched but year or day group is not set.

Source code in aoc/names.py
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
73
74
75
76
77
def get_key_by_name(name: str) -> Key:
    """Gets the key representing the problem by the name of the solution type.

    Note:
        The `name` must match the format of [`NAME_EXAMPLE`][aoc.names.NAME_EXAMPLE].

    Arguments:
        name: The name of the solution type.

    Returns:
        The key representing the problem.

    Raises:
        TypeError: The `name` does not match the expected format.
        ValueError: The year or the day is not valid.
        InternalError: The pattern was matched but `year` or `day` group is not set.
    """
    match = NAME.match(name)

    if match is None:
        raise TypeError(invalid_name(name))

    year_option = match.group(YEAR)

    if year_option is None:
        raise InternalError  # TODO: message?

    year_value = int(year_option)

    day_option = match.group(DAY)

    if day_option is None:
        raise InternalError  # TODO: message?

    day_value = int(day_option)

    try:
        year = Year(year_value)

    except ValueError as invalid_year:
        raise TypeError(invalid_name_with_reason(name, invalid_year)) from invalid_year

    try:
        day = Day(day_value)

    except ValueError as invalid_day:
        raise TypeError(invalid_name_with_reason(name, invalid_day)) from invalid_day

    return Key(year, day)

get_name_by_key(key: Key) -> str

The inverse of get_key_by_name.

Parameters:

Name Type Description Default
key Key

The key to create the name for.

required

Returns:

Type Description
str

The name created.

Source code in aoc/names.py
84
85
86
87
88
89
90
91
92
93
def get_name_by_key(key: Key) -> str:
    """The inverse of [`get_key_by_name`][aoc.names.get_key_by_name].

    Arguments:
        key: The key to create the name for.

    Returns:
        The name created.
    """
    return create_name(key.year.value, key.day.value)