Skip to content

Data

get_path_for_key(key: Key, data_path: Path = DATA_PATH) -> Path

Gets the path to the file in data_path for the given key.

Parameters:

Name Type Description Default
key Key

The key to get the path for.

required
data_path Path

The path to the data directory.

DATA_PATH

Returns:

Type Description
Path

The path to the file in data_path for the given key.

Source code in aoc/data.py
10
11
12
13
14
15
16
17
18
19
20
def get_path_for_key(key: Key, data_path: Path = DATA_PATH) -> Path:
    """Gets the path to the file in `data_path` for the given `key`.

    Arguments:
        key: The key to get the path for.
        data_path: The path to the data directory.

    Returns:
        The path to the file in `data_path` for the given `key`.
    """
    return data_path / str(key.year) / str(key.day)

load_data(key: Key, data_path: Path = DATA_PATH, encoding: str = DEFAULT_ENCODING, errors: str = DEFAULT_ERRORS) -> str

Loads the data for the given key.

Parameters:

Name Type Description Default
key Key

The key to load the data for.

required
data_path Path

The path to the data directory.

DATA_PATH
encoding str

The encoding to use.

DEFAULT_ENCODING
errors str

The error handling of the encoding to use.

DEFAULT_ERRORS

Returns:

Type Description
str

The data for the given key.

Raises:

Type Description
DataNotFound

OSError occured.

Source code in aoc/data.py
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
def load_data(
    key: Key,
    data_path: Path = DATA_PATH,
    encoding: str = DEFAULT_ENCODING,
    errors: str = DEFAULT_ERRORS,
) -> str:
    """Loads the data for the given `key`.

    Arguments:
        key: The key to load the data for.
        data_path: The path to the data directory.
        encoding: The encoding to use.
        errors: The error handling of the encoding to use.

    Returns:
        The data for the given `key`.

    Raises:
        DataNotFound: [`OSError`][OSError] occured.
    """
    try:
        return get_path_for_key(key, data_path).read_text(encoding, errors)

    except OSError as origin:
        raise DataNotFound(key, data_path) from origin

dump_data(data: str, key: Key, data_path: Path = DATA_PATH, encoding: str = DEFAULT_ENCODING, errors: str = DEFAULT_ERRORS) -> None

Dumps the data for the given key.

Parameters:

Name Type Description Default
data str

The data to dump.

required
key Key

The key to dump the data for.

required
data_path Path

The path to the data directory.

DATA_PATH
encoding str

The encoding to use.

DEFAULT_ENCODING
errors str

The error handling of the encoding to use.

DEFAULT_ERRORS
Source code in aoc/data.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def dump_data(
    data: str,
    key: Key,
    data_path: Path = DATA_PATH,
    encoding: str = DEFAULT_ENCODING,
    errors: str = DEFAULT_ERRORS,
) -> None:
    """Dumps the `data` for the given `key`.

    Arguments:
        data: The data to dump.
        key: The key to dump the data for.
        data_path: The path to the data directory.
        encoding: The encoding to use.
        errors: The error handling of the encoding to use.
    """
    path = get_path_for_key(key, data_path)

    path.parent.mkdir(parents=True, exist_ok=True)

    path.write_text(data, encoding, errors)