module_loader.py
# 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.html import io, os, sys, types from IPython import get_ipython
from nbformat import read
from IPython.core.interactiveshell import InteractiveShell def 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_path class 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 mod class 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())
module_loader.py的更多相关文章
- python调用py中rar的路径问题。
1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...
- Python导入其他文件中的.py文件 即模块
import sys sys.path.append("路径") import .py文件
- import renumber.py in pymol
cp renumber.py /usr/local/lib/python2.7/dist-packages/pymol import renumber or run /path/to/renumber ...
- python gettitle.py
#!/usr/bin/env python # coding=utf-8 import threading import requests import Queue import sys import ...
- 解决 odoo.py: error: option --addons-path: The addons-path 'local-addons/' does not seem to a be a valid Addons Directory!
情况说明 odoo源文件路径-/odoo-dev/odoo/: 我的模块插件路径 ~/odoo-dev/local-addons/my-module 在my-module中创建了__init__.py ...
- caffe机器学习自带图片分类器classify.py实现输出预测结果的概率及caffe的web_demo例子运行实例
caffe机器学习环境搭建及python接口编译参见我的上一篇博客:机器学习caffe环境搭建--redhat7.1和caffe的python接口编译 1.运行caffe图片分类器python接口 还 ...
- 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优
libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...
- MySqlNDB使用自带的ndb_setup.py安装集群
在用Mysql做集群时,使用Mysql的NDB版本更易于集群的扩展,稳定和数据的实时性. 我们可以使用Mysql自带的工具进行集群安装与管理:ndb_setup.py.位于Mysql的安装目录bin下 ...
- 将做好的py文件打包成模块,供别人安装调用
现在要将写完的3个py文件,打包. 步骤: 1.新建一个文件夹setup(名字随便取),在setup文件夹下,再新建一个文件夹financeapi. 2.将上面4个py文件拷贝至financeapi文 ...
随机推荐
- Eclipse lombok java
Stablehttps://projectlombok.org/features/all Lombok介绍及使用方法 - holten - 博客园http://www.cnblogs.com/holt ...
- scp Permission denied
https://blog.csdn.net/xlgen157387/article/details/49818259
- Leaf——美团点评分布式ID生成系统 UUID & 类snowflake
Leaf——美团点评分布式ID生成系统 https://tech.meituan.com/MT_Leaf.html
- 虚拟机Ubuntu图形界面进入命令行快捷键
ctrl+alt+f2 https://jingyan.baidu.com/article/03b2f78c69e5c25ea337ae40.html https://www.zabbix.com/d ...
- 微信开发 提示 Redirect_uri(错误10003)
情景: 搭建完成一个网站,使用微信打开链接地址,结果报错1003 完整的错误信息: 出现这种情况一般有两种原因: 1.没有配置网页授权 我们可以根据微信的开发者文档http://mp.weixin. ...
- WPF中任务栏只显示主窗口
我们在用WPF开发的时候,常常会遇到在主窗口打开的情况下,去显示子窗口,而此时任务栏同时显示主窗口与子窗口.这样看起来很不美观.所以在弹出子窗口之前,设置它的几个相应属性,便不会出现这种问题了. // ...
- [官网]CREATE EXTENSION PostGreSQL 创建函数的方法
CREATE EXTENSION https://www.postgresql.org/docs/current/sql-createextension.html CREATE EXTENSION — ...
- mysql的主从复制
一.主从复制简介 二.主从复制原理 四.主从复制基本故障处理 五.延时从库 六.半同步复制 七.过滤复制 四.主从复制基本故障处理 SQL 线程为NO 一般情况下,因为主库一开始本来就有一些存在的库, ...
- Spring boot 全局配置文件application.properties
#更改Tomcat端口号 server.port=8090 #修改进入DispatcherServlet的规则为:*.htmlserver.servlet-path=*.html#这里要注意高版本的s ...
- Python——管道通信
管道:双向通信 2个进程之间相互通信 from multiprocessing import Pipe,Process def func(conn1,conn2): conn2.close() whi ...