예제 #1
0
def download_exp_data_if_not_exist(
    exp_data_url: typing.AnyStr,
    exp_data_destination: typing.AnyStr,
) -> typing.AnyStr:
    """
    Downloads & extract config/weights for a model if the provided destination does not exist.

    The provided URL will be assumed to be a Google Drive download URL. The download will be
    skipped entirely if the destination folder already exists. This function will return the
    path to the existing folder, or to the newly created folder.

    Args:
        exp_data_url: the zip URL (under the `https://drive.google.com/file/d/ID` format).
        exp_data_destination: where to extract the model data.

    Returns:
        The path to the model data.
    """
    assert exp_data_url.startswith("https://drive.google.com/file/d/")
    gdrive_file_id = exp_data_url.split("/")[-1]
    output_data_path = os.path.join(exp_data_destination, gdrive_file_id)
    downloaded_zip_path = os.path.join(exp_data_destination,
                                       f"{gdrive_file_id}.zip")
    if os.path.isfile(downloaded_zip_path) and os.path.isdir(output_data_path):
        return output_data_path
    os.makedirs(output_data_path, exist_ok=True)
    zip_path = download_file_from_google_drive(gdrive_file_id,
                                               downloaded_zip_path)
    with zipfile.ZipFile(zip_path, "r") as zip_ref:
        zip_ref.extractall(output_data_path)
    return output_data_path
def remove_prefix(data: typing.AnyStr) -> typing.AnyStr:
    """Remove hexadecimal prefix from string ('0x', '0X')"""

    if isinstance(data, str):
        prefix = ('0x', '0X')
    else:
        prefix = (b'0x', b'0X')

    if data.startswith(prefix):
        return data[2:]
    return data
예제 #3
0
def convert_line(line: typing.AnyStr) -> typing.AnyStr:
    """
    Конвертирует одну строку

    :param line: строка для конвертаций
    :return: сконвертированная строка с требуемым отступом
    """
    line = line.replace('<', '&lt;') \
        .replace('>', '&gt;')

    for tag, value in single_tags.items():
        line = line.replace(tag, value)

    for tag, value in inline_tags.items():
        if line.startswith(tag):
            line = value.format(line[len(tag):].strip())

    return line