File size: 1,655 Bytes
e01a839
 
 
 
 
 
fadbdc4
 
 
 
 
 
 
 
 
 
e01a839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
49
50
51
52
53
54
55
56
import os
from typing import List

from settings import STATIC_PATH


def is_google_colab():
    """Check if the environment is Google Colab."""
    try:
        from google.colab import drive

        return True
    except ImportError:
        return False


def get_file_as_string(file_name, path=STATIC_PATH) -> str:
    """Loads the content of a file given its name
    and returns all of its lines as a single string
    if a file path is given, it will be used
    instead of the default static path (from settings)

    Args:
        file_name (_type_): The name of the file to load.
        path (str, optional): The path to the file. Defaults to the current directory.

    Returns:
        str: The content of the file as a single string
    """
    with open(os.path.join(path, file_name), mode="r", encoding="UTF-8") as f:
        return f.read()


def get_sections(string: str, delimiter: str, up_to: int = None) -> List[str]:
    """Splits a string into sections given a delimiter

    Args:
        string (str): The string to split
        delimiter (str): The delimiter to use
        up_to (int, optional): The maximum number of sections to return.
                Defaults to None (which means all sections)

    Returns:
        List[str]: The list of sections (up to the given limit, if any provided)
    """
    return [
        section.strip()
        for section in string.split(delimiter)
        if (section and not section.isspace())
    ][:up_to]


def get_workers(safety: int = 4) -> int:
    """Return the number of cores available on the current system, minus a safety margin."""
    return max(1, os.cpu_count() - safety)