Пример #1
0
def read_single_file(file_path: Optional[pathlib.Path]) -> Tuple[bytes, int]:
    """
    Given a path to a file, return the base64-encoded contents of the file and its original size.
    """
    if not file_path:
        return b"", 0

    check.check_true(file_path.is_file(),
                     'The file at "{}" could not be found'.format(file_path))

    content = file_path.read_bytes()

    return base64.b64encode(content), len(content)
def convert_notebook_to_python_script(notebook_path: str) -> str:
    check.check_true(
        notebook_path.endswith(".ipynb"), f"Notebook file {notebook_path} must has a suffix .ipynb"
    )
    processed_cells_path = f"{notebook_path[:-6]}__det__.py"

    with open(notebook_path, "r") as f1, open(processed_cells_path, "w") as f2:
        obj = json.load(f1)
        check.true("cells" in obj, f"Invalid notebook file {notebook_path}")
        for cell in obj["cells"]:
            if cell["cell_type"] == "code":
                lines = [line for line in cell["source"] if not line.lstrip().startswith("!")]
                f2.writelines(lines)
                f2.write("\n")
    return processed_cells_path