import glob
import re


def best_model_from_dir(basename):
    """ Return best saved model from basename. """
    models = glob.glob(basename + '*.index')
    best_model = None
    # get best model, if exists
    models_out = []
    for m in models:
        match = re.match(re.escape(basename) + '(1?[0-9]{4}).index', m)
        if match:
            models_out.append(int(match.groups()[0]))

    if models_out:
        acc = max(models_out)
        best_model = basename + str(acc)

    return best_model