import glob
import re


def _find_weights(ckpt_path, paths=None, start=0):
    """ ckpt_path should be format string containing '{epoch}'

    >>> paths = ['abc.01.ckpt', 'abc.05.ckpt', 'abc.best.ckpt']
    >>> _find_weights('abc.{epoch}.ckpt', paths=paths)
    [(0, 'abc.01.ckpt'), (4, 'abc.05.ckpt')
    """
    if paths is None:
        paths = glob.glob(ckpt_path.format(epoch='*'))
    pat = ckpt_path.format(epoch='([0-9]+)')

    def get_epoch_path(path):
        m = re.match(pat, path)
        if m:
            return (int(m.groups()[0]) - 1 + start, path)

    epoch_paths = list(filter(lambda x: x, (get_epoch_path(path) for path in paths)))
    return epoch_paths