from pathlib import Path


def extract_runtimes(path):
    """Extract total runtime from output files.
    
    Parameters
    ----------
    path: str
        Path to directory with output files

    Returns
    -------
    timings: dict
        Dictionary of timings with keys as <num_procs>_<num_host_procs>
    """
    wd = Path(path)
    datafiles = list(wd.glob('*_*.out'))

    timings = {}
    for df in datafiles:
        key = df.name[:-4] # Remove .out

        with df.open() as fin:
            lines = fin.readlines()

        time = None
        for l in lines:
            if 'Total Time' in l:
                time = float(l.split()[-1][:-2])

        if time:
            if key not in timings.keys():
                timings[key] = {}
            timings[key] = time
    
    return timings