博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
module_loader.py
阅读量:6759 次
发布时间:2019-06-26

本文共 2884 字,大约阅读时间需要 9 分钟。

# few functions that make it possible to import functions# from jupyter notebooks as from modules;# source: http://jupyter-notebook.readthedocs.io/en/latest/examples/Notebook/Importing%20Notebooks.htmlimport io, os, sys, typesfrom IPython import get_ipythonfrom nbformat import readfrom IPython.core.interactiveshell import InteractiveShelldef find_notebook(fullname, path=None):    """find a notebook, given its fully qualified name and an optional path    This turns "foo.bar" into "foo/bar.ipynb"    and tries turning "Foo_Bar" into "Foo Bar" if Foo_Bar    does not exist.    """    name = fullname.rsplit('.', 1)[-1]    if not path:        path = ['']    for d in path:        nb_path = os.path.join(d, name + ".ipynb")        if os.path.isfile(nb_path):            return nb_path        # let import Notebook_Name find "Notebook Name.ipynb"        nb_path = nb_path.replace("_", " ")        if os.path.isfile(nb_path):            return nb_pathclass NotebookLoader(object):    """Module Loader for Jupyter Notebooks"""    def __init__(self, path=None):        self.shell = InteractiveShell.instance()        self.path = path    def load_module(self, fullname):        """import a notebook as a module"""        path = find_notebook(fullname, self.path)        #print ("importing Jupyter notebook from %s" % path)        # load the notebook object        with io.open(path, 'r', encoding='utf-8') as f:            nb = read(f, 4)        # create the module and add it to sys.modules        # if name in sys.modules:        #    return sys.modules[name]        mod = types.ModuleType(fullname)        mod.__file__ = path        mod.__loader__ = self        mod.__dict__['get_ipython'] = get_ipython        sys.modules[fullname] = mod        # extra work to ensure that magics that would affect the user_ns        # actually affect the notebook module's ns        save_user_ns = self.shell.user_ns        self.shell.user_ns = mod.__dict__        try:            for cell in nb.cells:                if cell.cell_type == 'code':                    # transform the input to executable Python                    code = self.shell.input_transformer_manager.transform_cell(cell.source)                    # run the code in themodule                    exec(code, mod.__dict__)        finally:            self.shell.user_ns = save_user_ns        return modclass NotebookFinder(object):    """Module finder that locates Jupyter Notebooks"""    def __init__(self):        self.loaders = {}    def find_module(self, fullname, path=None):        nb_path = find_notebook(fullname, path)        if not nb_path:            return        key = path        if path:            # lists aren't hashable            key = os.path.sep.join(path)        if key not in self.loaders:            self.loaders[key] = NotebookLoader(path)        return self.loaders[key]sys.meta_path.append(NotebookFinder())

转载地址:http://qefeo.baihongyu.com/

你可能感兴趣的文章
JsonCpp 判断 value 中是否有某个KEY
查看>>
Java基础11
查看>>
中国嵌入式工控机市场前景广阔
查看>>
Mysql 行列转换
查看>>
函数模板的使用说明
查看>>
【phpcms-v9】前台content模块中pc标签的调用说明
查看>>
iframe如何刷新的三种解决方案
查看>>
Unity通过Jar包调用Android
查看>>
vue-swiper的使用
查看>>
Ubuntu10.04上apache2: bad user name ${APACHE_RUN_USER}问题解决
查看>>
leetcode每日刷题计划-简单篇day5
查看>>
C语言学习-简介
查看>>
gradle下载及配置
查看>>
maven中央仓库、远程仓库地址
查看>>
PRD
查看>>
JAVASCRIPT 转换字符串杂记
查看>>
【无私分享:从入门到精通ASP.NET MVC】从0开始,一起搭框架、做项目(7.1) 模块管理,验证权限,展示模块列表...
查看>>
jquery鼠标悬停事件hover()
查看>>
hql查询语句 内存中的情况,fetch迫切查询关键字
查看>>
RabbitMQ安装,Windows下
查看>>