token_container.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:basis 作者: vaitech 项目源码 文件源码
def load(cls, filepath):

        tmpdir = tempfile.mkdtemp()
        error = None

        logger.debug('extracting archive to: {}'.format(tmpdir))

        try:
            with tarfile.open(filepath, 'r:*') as ar:
                ar.extractall(tmpdir)

            payload = open(os.path.join(tmpdir, 'metadata'), 'rb').read()
            print(payload)
            meta = dill.loads(payload)

            instance = cls()

            for attr_name, attr_val in meta.items():
                setattr(instance, attr_name, attr_val)

            if os.path.exists(os.path.join(tmpdir, 'vectors')):
                logger.debug('loading word vectors')

                import h5py

                with h5py.File(os.path.join(tmpdir, 'vectors'), 'r') as h5:
                    setattr(instance, '_W', h5['vectors'][:])
            else:
                logger.debug('no word vectors found in archive')
        except Exception as e:
            logger.error('encountered error: {}'.format(e))
            error = e
        finally:
            logger.debug('cleaning up {}'.format(tmpdir))
            shutil.rmtree(tmpdir)
            if error is not None:
                raise error

        return instance
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号