import re


def list_checkpoints(dir_):
    """List all valid checkpoints in a given directory."""
    checkpoints = []
    for p in dir_.glob('checkpoint_*.tar'):
        numbers = re.findall(r'(\d+)', p.name)
        if len(numbers) == 0:
            continue
        assert len(numbers) == 1
        checkpoints.append((int(numbers[0]), p))
    return checkpoints