Team Ai
Apppublic

Fisharp/starcoder-playground

sourceHugging Faceupdated 3y agoView on Hugging Face
6likes
utils.py98 linesDownload Raw Back to src
1import os2from typing import List3from urllib.parse import urljoin4 5from settings import (6    DEFAULT_HUGGINGFACE_MODELS_API_BASE_URL,7    STATIC_PATH,8)9 10def masked(value: str, n_shown: int, length: int = None) -> str:11    """Returns a string with the first and last n_shown characters12    and the middle of the string replaced with '*'13 14    Args:15        value (str): The string to mask16        n_shown (int): The number of characters to show at the beginning and end of the string17        length (int, optional): The length of the string. If not given, it will be calculated as the length of the value. Defaults to None.18 19    Returns:20        str: The masked string21    """22    l = length or len(value)23    return value[0:n_shown] + '*'*(length-2*n_shown) + value[-n_shown:]24 25 26def ofuscated(value: str) -> str:27    """Returns a string with the first and last 4 characters28    and the middle of the string replaced with '*'29 30    Args:31        value (str): The string to mask32 33    Returns:34        str: The masked string35    """36    return masked(value, 4, len(value)//2)37 38 39def preview(label:str, value: str, ofuscate=False):40    """Print the variable name and its value in a nice way.41    If ofuscate is True, it will ofuscate the value42 43    Args:44        variable_name (str): The name of the variable to print45        ofuscate (bool, optional): If True, it will ofuscate the value. Defaults to False.46    """47    str_value = ofuscated(str(value)) if ofuscate else str(value)48    print(f"{label} = {str_value}")49 50def get_url_from_env_or_default_path(env_name: str, api_path: str) -> str:51    """Takes an url from the env variable (given the env name)52    or combines with urljoin the default models base url53    with the default path (given the path name)54 55    Args:56        env_name (str): The name of the environment variable to check57        api_path (str): The default path to use if the environment variable is not set58 59    Returns:60        str: The url to use61    """62    return os.environ.get(env_name) or urljoin(63        DEFAULT_HUGGINGFACE_MODELS_API_BASE_URL, api_path64    )65 66def get_file_as_string(file_name, path=STATIC_PATH) -> str:67    """Loads the content of a file given its name68    and returns all of its lines as a single string69    if a file path is given, it will be used70    instead of the default static path (from settings)71 72    Args:73        file_name (_type_): The name of the file to load.74        path (str, optional): The path to the file. Defaults to the current directory.75 76    Returns:77        str: The content of the file as a single string78    """79    with open(os.path.join(path, file_name), mode='r', encoding='UTF-8') as f:80        return f.read()81 82 83def get_sections(string: str, delimiter: str, up_to: int = None) -> List[str]:84    """Splits a string into sections given a delimiter85 86    Args:87        string (str): The string to split88        delimiter (str): The delimiter to use89        up_to (int, optional): The maximum number of sections to return.90                Defaults to None (which means all sections)91 92    Returns:93        List[str]: The list of sections (up to the given limit, if any provided)94    """95    return [section.strip()96            for section in string.split(delimiter)97            if (section and not section.isspace())][:up_to]98