def save_cached_object(object, filename, directory='Cache/'):
    """Save cached objects in pickel format using joblib compression.
       If a previous cached file exists, then get its modified date and append it to filename and archive it
    """
    if filename[-4:] != '.pkl':
        filename = filename+'.pkl'
    try:
        modified_date = time.strptime(time.ctime(os.path.getmtime(directory+filename)), '%a %b %d %H:%M:%S %Y')
        modified_date = datetime.fromtimestamp(time.mktime(modified_date)).strftime('%m-%d-%y_%H%M')
        archived_file = directory+'Archive/'+filename[:len(filename)-4]+'--'+modified_date+'.pkl'
        os.rename(directory+filename,archived_file)
        log.info('Cached object already exists with given filename, archiving old object to: '+ archived_file)
    except WindowsError:
        pass
    joblib.dump(object, directory+filename, compress=9)
    log.info('New object cached to: '+directory+filename)
dataset/ETHPy150Open theusual/kaggle-seeclickfix-ensemble/Bryan/data_io.py/save_cached_object/original