nbimporter:将ipython笔记本作为模块导入

  • x8_137531
    了解作者
  • 8KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-07 04:55
    上传日期
nbimporter 将IPython Notebook作为模块导入(使用Jupyter v4)。 更新2019-06:我不建议再使用nbimporter 。 几年前创建此程序包时,我仍然相信从其他笔记本中导入功能对于开发和制作原型是个好主意。 到现在为止,我在数据科学领域已有多年的工作经验,并且仍在使用Jupyter,但是我再也不会从其他笔记本中导入了。 为什么? 其他笔记本电脑中的功能难以维护,并且妨碍了其他笔记本电脑的可重复性。 在不同项目中使用这些功能时,您将需要修复错误,进行性能改进或以更优雅的方式对其进行重写。 一旦您开始在另一个笔记本中修复某些内容,您可能会破坏它,并且您的分析不再具有可重复性。 很难在笔记本中测试功能。 建议为几乎所有功能创建一个单元测试。 在笔记本电脑中没有简单的方法可以做到这一点。 此外,您可能希望在功能强大的IDE中编辑可重复使用的代码,以防止
nbimporter-master.zip
  • nbimporter-master
  • .gitignore
    720B
  • setup.cfg
    38B
  • README.md
    4.1KB
  • .github
  • workflows
  • pythonpublish.yml
    865B
  • display_nb.py
    960B
  • LICENSE
    1.7KB
  • setup.py
    426B
  • nbimporter.py
    5KB
内容介绍
# nbimporter Import IPython Notebooks as modules (with Jupyter v4). ## Update 2019-06: I do not recommend any more to use *nbimporter*. When I created this package some years ago, I still believed that importing functions from other notebooks was a good idea for developing and prototyping. By now, I have some more years experience in doing data science, and I still use Jupyter, however I never import from other notebooks any more. ### Why? * **Functions in other notebooks are hard to maintain and hamper reproducibility of other notebooks.** As you use the functions in different projects, you'll want to fix bugs, make performance improvements or re-write it in a more elegant fashion. As soon as you start to fix something in the other notebook, you might break it and your analysis is not reproducible any more. * **It is hard to test functions within notebooks**. It is advisable to create a unit test for almost any function. There's no straightforward way to do so within a notebook. Moreover, you might want to edit re-usable code in a powerful IDE that will prevent you from making one or the other mistake. * **It is a hack, and doing it properly is *very* easy (see below).** Notebooks are not meant to be imported. This causes issues like [#4](https://github.com/grst/nbimporter/issues/4) and [#5](https://github.com/grst/nbimporter/issues/5). While it is probably totally possible to fix these, doing it properly doesn't cause such problems in the first place. ### I still want to re-use code (quickly!). What should I do instead? The **proper** way of doing it is to create a [python module](https://docs.python.org/3/tutorial/modules.html) that contains your functions. It is as simple as creating a file in the same directory as your notebooks, e.g. `utils.py` that contains your functions, classes, constants, etc: **`utils.py`** ``` def my_function(foo): return "Hello World!" ``` Congratulations! You created your first python module. Now you can import from it with a single line and zero external dependencies: **`notebook.ipynb`**: ``` In [1]: from utils import my_function In [2]: print(my_function()) Out [2]: "Hello World!" ``` By consequently putting re-usable code into modules, you can use powerful IDEs to develop and debug code, create unit tests, combine multiple modules into shareable packages. In brief, you'll write higher-quality code and re-use more of the code you've written, saving you a lot of time and trouble. **Another approach** is to use [jupytext](https://github.com/mwouts/jupytext) that allows you to save jupyter notebooks as plain python scripts from that you can import directly. However that alone does not prevent you from creating code debt. ### Can I still use *nbimporter*? Sure! If you still believe *nbimporter* fits your workflow best, the package still works. I won't actively maintain it any longer, but will accept (sensible) pull-requests. ## Origin This is a modified version of code originally from https://github.com/adrn/ipython/blob/master/examples/Notebook/Importing%20Notebooks.ipynb Check out that notebook for full documentation and to understand the background. ## Installation `pip install nbimporter` ## Usage **Notebook foo.ipynb:** In[1]: ```python def a(): print("Hello World!") ``` **Notebook bar.ipynb:** In[1]: ```python import nbimporter from foo import a #import from notebook a() ``` Out[1]: ``` Hello World! ``` ## Options Importing from a notebook is different from a module: because one typically keeps many computations and tests besides exportable defs, here we only run code which either defines a function or a class, or imports code from other modules and notebooks. This behaviour can be disabled by setting `nbimporter.options['only_defs'] = False`. Furthermore, in order to provide per-notebook initialisation, if a special function `__nbinit__()` is defined in the notebook, it will be executed the first time an import statement is. This behaviour can be disabled by setting `nbimporter.options['run_nbinit'] = False`. Finally, you can set the encoding of the notebooks with `nbimporter.options['encoding']`. The default is `'utf-8'`.
评论
    相关推荐
    • Python-PyTestReportPython单元测试框架
      PyTestReport - Python单元测试框架
    • UnitTestingProjectUASB:单元测试实践
      单元测试项目 单元测试实践 学生 卡洛斯·大卫·蒙泰拉诺·巴里加(Carlos David Montellano Barriga) 运作中 情感分析是计算语言学领域的一项任务,旨在确定给定文本的总体态度。 例如,我们希望有一个程序可以...
    • unit-testing-guidelines:单元测试最佳实践
      单元测试最佳实践 以下是帮助实现有效且可维护的单元测试的一系列约定和最佳实践。 大部分内容直接取自优秀的《一书 单元测试的 FIRST 属性 在较高级别上,旨在为您的单元测试实现以下目标。 ˚FAST:更快的测试...
    • idea单元测试jar包
      idea单元测试jar包,idea单元测试jar包,idea单元测试jar包
    • 智能设备项目的单元测试(VIDEO)
      MSDN Webcast 智能设备项目的单元测试 Video
    • java多线程单元测试
      java多线程单元测试一直以来都是挺头疼的一件事情,不能用普通的jUnit来测试java多线程,次资料提供了java多线程的一个框架(附加代码)让你测试java多线程不在头疼。呵呵
    • 如何使用VSTS写单元测试
      如何使用VSTS写单元测试如何使用VSTS写单元测试如何使用VSTS写单元测试如何使用VSTS写单元测试如何使用VSTS写单元测试如何使用VSTS写单元测试
    • 单元测试利器JUnit4
      单元测试利器JUnit4
    • 单元测试的记者
      本文介绍了一个用于可视化CppUnitLite单元测试框架的测试过程的应用程序。
    • jsunit2.2 javascript单元测试
      JsUnit is a unit testing framework for client-side JavaScript in the tradition of the XUnit frameworks