def bash_out_to_time_and_memory(infile):
    """Parses output file `out` of command run like this:
          /usr/bin/time -v foo &> out
    returns a dictionary of time and memory used"""
    stats = {}

    with open(infile) as f:
        in_time_lines = False
        for line in f:
            if not in_time_lines:
                if line.startswith("\tCommand being timed:"):
                    in_time_lines = True
            elif line.startswith("\tElapsed (wall clock) time (h:mm:ss or m:ss): "):
                time_fields = line.rstrip().split()[-1].split(":")
                if not 2 <= len(time_fields) <= 3:
                    raise Exception(f"Error getting time from this line: {line}")
                time_in_seconds = float(time_fields[-1]) + 60 * float(time_fields[-2])
                if len(time_fields) == 3:
                    time_in_seconds += 60 * 60 * float(time_fields[0])
                stats["wall_clock_time"] = time_in_seconds
            elif line.startswith("\tUser time (seconds): "):
                stats["user_time"] = float(line.rstrip().split()[-1])
            elif line.startswith("\tSystem time (seconds): "):
                stats["system_time"] = float(line.rstrip().split()[-1])
            elif line.startswith("\tMaximum resident set size (kbytes): "):
                stats["ram"] = float(line.rstrip().split()[-1])

    return stats