Spaces:
Sleeping
Sleeping
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) | |