├── .gitignore ├── .pylintrc ├── .travis.yml ├── LICENSE ├── README.md ├── codecov.yml ├── docs ├── Makefile ├── conf.py └── index.rst ├── ipynb ├── __init__.py ├── fs │ ├── __init__.py │ ├── defs │ │ └── __init__.py │ ├── finder.py │ └── full │ │ └── __init__.py ├── setup │ └── __init__.py └── utils.py ├── setup.py └── tests ├── bogus_ipynb.ipynb ├── mixed_ipynb ├── __init__.py └── foo.ipynb ├── older_nbformat.ipynb ├── pure_ipynb ├── __init__.ipynb └── foo.ipynb ├── r_notebook.ipynb ├── test_defs.py └── test_full.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Django stuff: 51 | *.log 52 | 53 | # Sphinx documentation 54 | docs/_build/ 55 | 56 | # PyBuilder 57 | target/ 58 | 59 | # Emacs 60 | .#* 61 | 62 | # Notebook Checkpoints 63 | .ipynb_checkpoints/ 64 | -------------------------------------------------------------------------------- /.pylintrc: -------------------------------------------------------------------------------- 1 | [MASTER] 2 | # Ignore git and virtualenv-related folders. 3 | ignore=.git,lib 4 | 5 | [MESSAGES CONTROL] 6 | disable=all 7 | 8 | # Explicitly whitelist the lint checks we want to have 9 | # Prefer things that catch errors and what not, and not stylistic choices 10 | enable=missing-docstring,empty-docstring,unneeded-not,singleton-comparison,misplaced-comparison-constant,unidiomatic-typecheck,consider-using-enumerate,consider-iterating-dictionary,bad-classmethod-argument,bad-mcs-method-argument,bad-mcs-classmethod-argument,too-many-lines,multiple-statements,superfluous-parens,multiple-imports,ungrouped-imports,syntax-error,init-is-generator,return-in-init,function-redefined,not-in-loop,return-outside-function,yield-outside-function,nonexistent-operator,duplicate-argument-name,abstract-class-instantiated,bad-reversed-sequence,too-many-star-expressions,invalid-star-assignment-target,star-needs-assignment-target,nonlocal-and-global,continue-in-finally,nonlocal-without-binding,method-hidden,access-member-before-definition,no-method-argument,no-self-argument,invalid-slots-object,assigning-non-slot,invalid-slots,inherit-non-class,inconsistent-mro,duplicate-bases,non-iterator-returned,unexpected-special-method-signature,invalid-length-returned,import-error,used-before-assignment,undefined-variable,undefined-all-variable,unbalanced-tuple-unpacking,unpacking-non-sequence,bad-except-order,raising-bad-type,bad-exception-context,misplaced-bare-raise,raising-non-exception,notimplemented-raised,catching-non-exception,bad-super-call,no-member,not-callable,assignment-from-no-return,no-value-for-parameter,too-many-function-args,unexpected-keyword-arg,redundant-keyword-arg,missing-kwoa,invalid-sequence-index,invalid-slice-index,assignment-from-none,not-context-manager,invalid-unary-operand-type,unsupported-binary-operation,repeated-keyword,not-an-iterable,not-a-mapping,unsupported-membership-test,unsubscriptable-object,logging-unsupported-format,logging-format-truncated,logging-too-many-args,logging-too-few-args,bad-format-character,truncated-format-string,mixed-format-string,format-needs-mapping,missing-format-string-key,too-many-format-args,too-few-format-args,bad-str-strip-call,yield-inside-async-function,not-async-context-manager,fatal,astroid-error,parse-error,method-check-failed,bad-inline-option,useless-suppression,deprecated-pragma,unreachable,dangerous-default-value,pointless-statement,expression-not-assigned,unnecessary-pass,unnecessary-lambda,duplicate-key,useless-else-on-loop,eval-used,confusing-with-statement,using-constant-test,lost-exception,assert-on-tuple,bad-staticmethod-argument,protected-access,arguments-differ,signature-differs,abstract-method,super-init-not-called,no-init,non-parent-init-called,unnecessary-semicolon,bad-indentation,mixed-indentation,wildcard-import,deprecated-module,reimported,import-self,misplaced-future,global-variable-undefined,global-variable-not-assigned,global-at-module-level,unused-import,unused-variable,unused-wildcard-import,redefined-outer-name,redefined-builtin,redefine-in-handler,undefined-loop-variable,cell-var-from-loop,duplicate-except,broad-except,bare-except,binary-op-exception,logging-not-lazy,logging-format-interpolation,bad-format-string-key,bad-format-string,missing-format-argument-key,format-combined-specification,missing-format-attribute,invalid-format-index,anomalous-backslash-in-string,anomalous-unicode-escape-in-string,bad-open-mode,redundant-unittest-assert,deprecated-method 11 | 12 | [REPORTS] 13 | # Do not print a summary report 14 | reports=no 15 | 16 | [FORMAT] 17 | # One of the few stylistic things we try to check. 18 | # Modules shouldn't be *too large* - should be broken up 19 | # Feel free to bump this number if you disagree 20 | max-module-lines=2000 21 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "3.6-dev" 4 | - 3.5 5 | - 3.4 6 | install: 7 | - pip install pytest pytest-cov --upgrade 8 | - pip install codecov --upgrade 9 | - pip install pylint --upgrade 10 | - pip install -e . 11 | script: 12 | - py.test --cov=./ 13 | - pylint ipynb 14 | after_success: 15 | - codecov 16 | matrix: 17 | allow_failures: 18 | - python: 3.6-dev 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2016, Yuvi Panda 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ipynb 2 | 3 | [![Build Status](https://travis-ci.org/yuvipanda/ipynb.svg?branch=master)](https://travis-ci.org/yuvipanda/ipynb) 4 | 5 | A python package providing an easy way to explicitly import [Jupyter Notebooks](https://github.com/jupyter/notebook) files (`.ipynb`) the same way you would import regular `.py` files. 6 | 7 | ## Installation ## 8 | 9 | You can install ipynb with: 10 | 11 | ```bash 12 | pip install ipynb 13 | ``` 14 | 15 | ## Importing a notebook ## 16 | 17 | ### Full import ### 18 | 19 | You can do a 'full' import - this has the same semantics of importing a .py file. All the code in the .ipynb file is executed, and classes/functions/variables in the top level are available for use. 20 | 21 | If you have a notebook file named `server.ipynb`, you can import it via: 22 | 23 | ```python 24 | import ipynb.fs.full.server 25 | ``` 26 | 27 | You can use the `from ... import ..` too. 28 | 29 | ```python 30 | from ipynb.fs.full.server import X, Y, X 31 | ``` 32 | 33 | ### Definitions only import ### 34 | 35 | Sometimes your notebook has been used as a way to run an analysis or other computation, and you only want to import the functions / classes defined in it - and not the extra statements you have in there. This can be accomplished via `ipynb.fs.defs`. 36 | 37 | If you have a notebook file named `server.ipynb`, and do: 38 | 39 | ```python 40 | import ipynb.fs.defs.server 41 | ``` 42 | 43 | It'll only execute and make available the following parts of the code in `server.ipynb`: 44 | - `class` definitions 45 | - `def` function definitions 46 | - `import` statements 47 | - Assignment statements where the variables being assigned to are ALL_CAPS. These are assumed to be constants. 48 | 49 | This skips most computational work and brings in your definitions only, making it easy to reuse functions / classes across similar analyses. 50 | 51 | ### Relative Imports ### 52 | 53 | You can also easily do relative imports, both for full notebooks or for definitions only. This works inside notebooks too. 54 | 55 | If you have a notebook called `notebook1.ipynb` in the same dir as your current notebook, you can import it with: 56 | 57 | ```python 58 | import ipynb.fs # Boilerplate required 59 | 60 | # Do a full import 61 | from .full.notebook1 import foo 62 | 63 | # Do a definitions-only import 64 | from .defs.notebook1 import bar 65 | ``` 66 | 67 | This works transitively nicely - other code can import your notebook that's using relative imports and it'll all work well. 68 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | coverage: 2 | status: 3 | project: 4 | default: 5 | target: auto 6 | threshold: 10 7 | patch: 8 | default: 9 | target: 0% 10 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Makefile for Sphinx documentation 2 | # 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | PAPER = 8 | BUILDDIR = _build 9 | 10 | # Internal variables. 11 | PAPEROPT_a4 = -D latex_paper_size=a4 12 | PAPEROPT_letter = -D latex_paper_size=letter 13 | ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 14 | # the i18n builder cannot share the environment and doctrees with the others 15 | I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . 16 | 17 | .PHONY: help 18 | help: 19 | @echo "Please use \`make ' where is one of" 20 | @echo " html to make standalone HTML files" 21 | @echo " dirhtml to make HTML files named index.html in directories" 22 | @echo " singlehtml to make a single large HTML file" 23 | @echo " pickle to make pickle files" 24 | @echo " json to make JSON files" 25 | @echo " htmlhelp to make HTML files and a HTML help project" 26 | @echo " qthelp to make HTML files and a qthelp project" 27 | @echo " applehelp to make an Apple Help Book" 28 | @echo " devhelp to make HTML files and a Devhelp project" 29 | @echo " epub to make an epub" 30 | @echo " epub3 to make an epub3" 31 | @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" 32 | @echo " latexpdf to make LaTeX files and run them through pdflatex" 33 | @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" 34 | @echo " text to make text files" 35 | @echo " man to make manual pages" 36 | @echo " texinfo to make Texinfo files" 37 | @echo " info to make Texinfo files and run them through makeinfo" 38 | @echo " gettext to make PO message catalogs" 39 | @echo " changes to make an overview of all changed/added/deprecated items" 40 | @echo " xml to make Docutils-native XML files" 41 | @echo " pseudoxml to make pseudoxml-XML files for display purposes" 42 | @echo " linkcheck to check all external links for integrity" 43 | @echo " doctest to run all doctests embedded in the documentation (if enabled)" 44 | @echo " coverage to run coverage check of the documentation (if enabled)" 45 | @echo " dummy to check syntax errors of document sources" 46 | 47 | .PHONY: clean 48 | clean: 49 | rm -rf $(BUILDDIR)/* 50 | 51 | .PHONY: html 52 | html: 53 | $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html 54 | @echo 55 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." 56 | 57 | .PHONY: dirhtml 58 | dirhtml: 59 | $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml 60 | @echo 61 | @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." 62 | 63 | .PHONY: singlehtml 64 | singlehtml: 65 | $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml 66 | @echo 67 | @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." 68 | 69 | .PHONY: pickle 70 | pickle: 71 | $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle 72 | @echo 73 | @echo "Build finished; now you can process the pickle files." 74 | 75 | .PHONY: json 76 | json: 77 | $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json 78 | @echo 79 | @echo "Build finished; now you can process the JSON files." 80 | 81 | .PHONY: htmlhelp 82 | htmlhelp: 83 | $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp 84 | @echo 85 | @echo "Build finished; now you can run HTML Help Workshop with the" \ 86 | ".hhp project file in $(BUILDDIR)/htmlhelp." 87 | 88 | .PHONY: qthelp 89 | qthelp: 90 | $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp 91 | @echo 92 | @echo "Build finished; now you can run "qcollectiongenerator" with the" \ 93 | ".qhcp project file in $(BUILDDIR)/qthelp, like this:" 94 | @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/ipynb.qhcp" 95 | @echo "To view the help file:" 96 | @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/ipynb.qhc" 97 | 98 | .PHONY: applehelp 99 | applehelp: 100 | $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp 101 | @echo 102 | @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." 103 | @echo "N.B. You won't be able to view it unless you put it in" \ 104 | "~/Library/Documentation/Help or install it in your application" \ 105 | "bundle." 106 | 107 | .PHONY: devhelp 108 | devhelp: 109 | $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp 110 | @echo 111 | @echo "Build finished." 112 | @echo "To view the help file:" 113 | @echo "# mkdir -p $$HOME/.local/share/devhelp/ipynb" 114 | @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/ipynb" 115 | @echo "# devhelp" 116 | 117 | .PHONY: epub 118 | epub: 119 | $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub 120 | @echo 121 | @echo "Build finished. The epub file is in $(BUILDDIR)/epub." 122 | 123 | .PHONY: epub3 124 | epub3: 125 | $(SPHINXBUILD) -b epub3 $(ALLSPHINXOPTS) $(BUILDDIR)/epub3 126 | @echo 127 | @echo "Build finished. The epub3 file is in $(BUILDDIR)/epub3." 128 | 129 | .PHONY: latex 130 | latex: 131 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 132 | @echo 133 | @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." 134 | @echo "Run \`make' in that directory to run these through (pdf)latex" \ 135 | "(use \`make latexpdf' here to do that automatically)." 136 | 137 | .PHONY: latexpdf 138 | latexpdf: 139 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 140 | @echo "Running LaTeX files through pdflatex..." 141 | $(MAKE) -C $(BUILDDIR)/latex all-pdf 142 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 143 | 144 | .PHONY: latexpdfja 145 | latexpdfja: 146 | $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex 147 | @echo "Running LaTeX files through platex and dvipdfmx..." 148 | $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja 149 | @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." 150 | 151 | .PHONY: text 152 | text: 153 | $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text 154 | @echo 155 | @echo "Build finished. The text files are in $(BUILDDIR)/text." 156 | 157 | .PHONY: man 158 | man: 159 | $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man 160 | @echo 161 | @echo "Build finished. The manual pages are in $(BUILDDIR)/man." 162 | 163 | .PHONY: texinfo 164 | texinfo: 165 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 166 | @echo 167 | @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." 168 | @echo "Run \`make' in that directory to run these through makeinfo" \ 169 | "(use \`make info' here to do that automatically)." 170 | 171 | .PHONY: info 172 | info: 173 | $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo 174 | @echo "Running Texinfo files through makeinfo..." 175 | make -C $(BUILDDIR)/texinfo info 176 | @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." 177 | 178 | .PHONY: gettext 179 | gettext: 180 | $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale 181 | @echo 182 | @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." 183 | 184 | .PHONY: changes 185 | changes: 186 | $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes 187 | @echo 188 | @echo "The overview file is in $(BUILDDIR)/changes." 189 | 190 | .PHONY: linkcheck 191 | linkcheck: 192 | $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck 193 | @echo 194 | @echo "Link check complete; look for any errors in the above output " \ 195 | "or in $(BUILDDIR)/linkcheck/output.txt." 196 | 197 | .PHONY: doctest 198 | doctest: 199 | $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest 200 | @echo "Testing of doctests in the sources finished, look at the " \ 201 | "results in $(BUILDDIR)/doctest/output.txt." 202 | 203 | .PHONY: coverage 204 | coverage: 205 | $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage 206 | @echo "Testing of coverage in the sources finished, look at the " \ 207 | "results in $(BUILDDIR)/coverage/python.txt." 208 | 209 | .PHONY: xml 210 | xml: 211 | $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml 212 | @echo 213 | @echo "Build finished. The XML files are in $(BUILDDIR)/xml." 214 | 215 | .PHONY: pseudoxml 216 | pseudoxml: 217 | $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml 218 | @echo 219 | @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." 220 | 221 | .PHONY: dummy 222 | dummy: 223 | $(SPHINXBUILD) -b dummy $(ALLSPHINXOPTS) $(BUILDDIR)/dummy 224 | @echo 225 | @echo "Build finished. Dummy builder generates no files." 226 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # 4 | # ipynb documentation build configuration file, created by 5 | # sphinx-quickstart on Sun Nov 6 09:25:24 2016. 6 | # 7 | # This file is execfile()d with the current directory set to its 8 | # containing dir. 9 | # 10 | # Note that not all possible configuration values are present in this 11 | # autogenerated file. 12 | # 13 | # All configuration values have a default; values that are commented out 14 | # serve to show the default. 15 | 16 | # If extensions (or modules to document with autodoc) are in another directory, 17 | # add these directories to sys.path here. If the directory is relative to the 18 | # documentation root, use os.path.abspath to make it absolute, like shown here. 19 | # 20 | # import os 21 | # import sys 22 | # sys.path.insert(0, os.path.abspath('.')) 23 | 24 | # -- General configuration ------------------------------------------------ 25 | 26 | # If your documentation needs a minimal Sphinx version, state it here. 27 | # 28 | # needs_sphinx = '1.0' 29 | 30 | # Add any Sphinx extension module names here, as strings. They can be 31 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 32 | # ones. 33 | extensions = [ 34 | 'sphinx.ext.intersphinx', 35 | 'sphinx.ext.viewcode', 36 | ] 37 | 38 | # Add any paths that contain templates here, relative to this directory. 39 | templates_path = ['_templates'] 40 | 41 | # The suffix(es) of source filenames. 42 | # You can specify multiple suffix as a list of string: 43 | # 44 | # source_suffix = ['.rst', '.md'] 45 | source_suffix = '.rst' 46 | 47 | # The encoding of source files. 48 | # 49 | # source_encoding = 'utf-8-sig' 50 | 51 | # The master toctree document. 52 | master_doc = 'index' 53 | 54 | # General information about the project. 55 | project = 'ipynb' 56 | copyright = '2016, Yuvi Panda' 57 | author = 'Yuvi Panda' 58 | 59 | # The version info for the project you're documenting, acts as replacement for 60 | # |version| and |release|, also used in various other places throughout the 61 | # built documents. 62 | # 63 | # The short X.Y version. 64 | version = '0.1' 65 | # The full version, including alpha/beta/rc tags. 66 | release = version 67 | 68 | # The language for content autogenerated by Sphinx. Refer to documentation 69 | # for a list of supported languages. 70 | # 71 | # This is also used if you do content translation via gettext catalogs. 72 | # Usually you set "language" from the command line for these cases. 73 | language = None 74 | 75 | # There are two options for replacing |today|: either, you set today to some 76 | # non-false value, then it is used: 77 | # 78 | # today = '' 79 | # 80 | # Else, today_fmt is used as the format for a strftime call. 81 | # 82 | # today_fmt = '%B %d, %Y' 83 | 84 | # List of patterns, relative to source directory, that match files and 85 | # directories to ignore when looking for source files. 86 | # This patterns also effect to html_static_path and html_extra_path 87 | exclude_patterns = [] 88 | 89 | # The reST default role (used for this markup: `text`) to use for all 90 | # documents. 91 | # 92 | # default_role = None 93 | 94 | # If true, '()' will be appended to :func: etc. cross-reference text. 95 | # 96 | # add_function_parentheses = True 97 | 98 | # If true, the current module name will be prepended to all description 99 | # unit titles (such as .. function::). 100 | # 101 | # add_module_names = True 102 | 103 | # If true, sectionauthor and moduleauthor directives will be shown in the 104 | # output. They are ignored by default. 105 | # 106 | # show_authors = False 107 | 108 | # The name of the Pygments (syntax highlighting) style to use. 109 | pygments_style = 'sphinx' 110 | 111 | # A list of ignored prefixes for module index sorting. 112 | # modindex_common_prefix = [] 113 | 114 | # If true, keep warnings as "system message" paragraphs in the built documents. 115 | # keep_warnings = False 116 | 117 | # If true, `todo` and `todoList` produce output, else they produce nothing. 118 | todo_include_todos = False 119 | 120 | 121 | # -- Options for HTML output ---------------------------------------------- 122 | 123 | # The theme to use for HTML and HTML Help pages. See the documentation for 124 | # a list of builtin themes. 125 | # 126 | import os 127 | 128 | ON_RTD = os.environ.get('READTHEDOCS', None) == 'True' 129 | 130 | if ON_RTD: 131 | tags.add('rtd') 132 | else: 133 | import sphinx_rtd_theme 134 | html_theme = "sphinx_rtd_theme" 135 | html_theme_path = [sphinx_rtd_theme.get_html_theme_path()] 136 | 137 | 138 | # Theme options are theme-specific and customize the look and feel of a theme 139 | # further. For a list of options available for each theme, see the 140 | # documentation. 141 | # 142 | # html_theme_options = {} 143 | 144 | # Add any paths that contain custom themes here, relative to this directory. 145 | # html_theme_path = [] 146 | 147 | # The name for this set of Sphinx documents. 148 | # " v documentation" by default. 149 | # 150 | # html_title = 'ipynb v0.0.1' 151 | 152 | # A shorter title for the navigation bar. Default is the same as html_title. 153 | # 154 | # html_short_title = None 155 | 156 | # The name of an image file (relative to this directory) to place at the top 157 | # of the sidebar. 158 | # 159 | # html_logo = None 160 | 161 | # The name of an image file (relative to this directory) to use as a favicon of 162 | # the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 163 | # pixels large. 164 | # 165 | # html_favicon = None 166 | 167 | # Add any paths that contain custom static files (such as style sheets) here, 168 | # relative to this directory. They are copied after the builtin static files, 169 | # so a file named "default.css" will overwrite the builtin "default.css". 170 | html_static_path = ['_static'] 171 | 172 | # Add any extra paths that contain custom files (such as robots.txt or 173 | # .htaccess) here, relative to this directory. These files are copied 174 | # directly to the root of the documentation. 175 | # 176 | # html_extra_path = [] 177 | 178 | # If not None, a 'Last updated on:' timestamp is inserted at every page 179 | # bottom, using the given strftime format. 180 | # The empty string is equivalent to '%b %d, %Y'. 181 | # 182 | # html_last_updated_fmt = None 183 | 184 | # If true, SmartyPants will be used to convert quotes and dashes to 185 | # typographically correct entities. 186 | # 187 | # html_use_smartypants = True 188 | 189 | # Custom sidebar templates, maps document names to template names. 190 | # 191 | # html_sidebars = {} 192 | 193 | # Additional templates that should be rendered to pages, maps page names to 194 | # template names. 195 | # 196 | # html_additional_pages = {} 197 | 198 | # If false, no module index is generated. 199 | # 200 | # html_domain_indices = True 201 | 202 | # If false, no index is generated. 203 | # 204 | # html_use_index = True 205 | 206 | # If true, the index is split into individual pages for each letter. 207 | # 208 | # html_split_index = False 209 | 210 | # If true, links to the reST sources are added to the pages. 211 | # 212 | # html_show_sourcelink = True 213 | 214 | # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. 215 | # 216 | # html_show_sphinx = True 217 | 218 | # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. 219 | # 220 | # html_show_copyright = True 221 | 222 | # If true, an OpenSearch description file will be output, and all pages will 223 | # contain a tag referring to it. The value of this option must be the 224 | # base URL from which the finished HTML is served. 225 | # 226 | # html_use_opensearch = '' 227 | 228 | # This is the file name suffix for HTML files (e.g. ".xhtml"). 229 | # html_file_suffix = None 230 | 231 | # Language to be used for generating the HTML full-text search index. 232 | # Sphinx supports the following languages: 233 | # 'da', 'de', 'en', 'es', 'fi', 'fr', 'h', 'it', 'ja' 234 | # 'nl', 'no', 'pt', 'ro', 'r', 'sv', 'tr', 'zh' 235 | # 236 | # html_search_language = 'en' 237 | 238 | # A dictionary with options for the search language support, empty by default. 239 | # 'ja' uses this config value. 240 | # 'zh' user can custom change `jieba` dictionary path. 241 | # 242 | # html_search_options = {'type': 'default'} 243 | 244 | # The name of a javascript file (relative to the configuration directory) that 245 | # implements a search results scorer. If empty, the default will be used. 246 | # 247 | # html_search_scorer = 'scorer.js' 248 | 249 | # Output file base name for HTML help builder. 250 | htmlhelp_basename = 'ipynbdoc' 251 | 252 | # -- Options for LaTeX output --------------------------------------------- 253 | 254 | latex_elements = { 255 | # The paper size ('letterpaper' or 'a4paper'). 256 | # 257 | # 'papersize': 'letterpaper', 258 | 259 | # The font size ('10pt', '11pt' or '12pt'). 260 | # 261 | # 'pointsize': '10pt', 262 | 263 | # Additional stuff for the LaTeX preamble. 264 | # 265 | # 'preamble': '', 266 | 267 | # Latex figure (float) alignment 268 | # 269 | # 'figure_align': 'htbp', 270 | } 271 | 272 | # Grouping the document tree into LaTeX files. List of tuples 273 | # (source start file, target name, title, 274 | # author, documentclass [howto, manual, or own class]). 275 | latex_documents = [ 276 | (master_doc, 'ipynb.tex', 'ipynb Documentation', 277 | 'Yuvi Panda', 'manual'), 278 | ] 279 | 280 | # The name of an image file (relative to this directory) to place at the top of 281 | # the title page. 282 | # 283 | # latex_logo = None 284 | 285 | # For "manual" documents, if this is true, then toplevel headings are parts, 286 | # not chapters. 287 | # 288 | # latex_use_parts = False 289 | 290 | # If true, show page references after internal links. 291 | # 292 | # latex_show_pagerefs = False 293 | 294 | # If true, show URL addresses after external links. 295 | # 296 | # latex_show_urls = False 297 | 298 | # Documents to append as an appendix to all manuals. 299 | # 300 | # latex_appendices = [] 301 | 302 | # It false, will not define \strong, \code, itleref, \crossref ... but only 303 | # \sphinxstrong, ..., \sphinxtitleref, ... To help avoid clash with user added 304 | # packages. 305 | # 306 | # latex_keep_old_macro_names = True 307 | 308 | # If false, no module index is generated. 309 | # 310 | # latex_domain_indices = True 311 | 312 | 313 | # -- Options for manual page output --------------------------------------- 314 | 315 | # One entry per manual page. List of tuples 316 | # (source start file, name, description, authors, manual section). 317 | man_pages = [ 318 | (master_doc, 'ipynb', 'ipynb Documentation', 319 | [author], 1) 320 | ] 321 | 322 | # If true, show URL addresses after external links. 323 | # 324 | # man_show_urls = False 325 | 326 | 327 | # -- Options for Texinfo output ------------------------------------------- 328 | 329 | # Grouping the document tree into Texinfo files. List of tuples 330 | # (source start file, target name, title, author, 331 | # dir menu entry, description, category) 332 | texinfo_documents = [ 333 | (master_doc, 'ipynb', 'ipynb Documentation', 334 | author, 'ipynb', 'One line description of project.', 335 | 'Miscellaneous'), 336 | ] 337 | 338 | # Documents to append as an appendix to all manuals. 339 | # 340 | # texinfo_appendices = [] 341 | 342 | # If false, no module index is generated. 343 | # 344 | # texinfo_domain_indices = True 345 | 346 | # How to display URL addresses: 'footnote', 'no', or 'inline'. 347 | # 348 | # texinfo_show_urls = 'footnote' 349 | 350 | # If true, do not generate a @detailmenu in the "Top" node's menu. 351 | # 352 | # texinfo_no_detailmenu = False 353 | 354 | 355 | # Example configuration for intersphinx: refer to the Python standard library. 356 | intersphinx_mapping = {'https://docs.python.org/3/': None} 357 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. ipynb documentation master file, created by 2 | sphinx-quickstart on Sun Nov 6 09:25:24 2016. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to ipynb's documentation! 7 | ================================= 8 | 9 | This module allow you to import ``ipynb`` as is they were classical python 10 | modules. Simply prepend ``ipynb.fs.full`` to the regular import. 11 | 12 | The `Zen of Python `_ say it well: 13 | Explicit is better than implicit. Thus wile import-hook are useful they lack 14 | explicitness. This module is meant to fix this by allowing you to explicitly 15 | requiring notebook files. 16 | 17 | This module does install an import hook, though it will only try to load 18 | packages that explicitly start with ``ipynb.fs.``. 19 | 20 | This is still highly work in progress, any feedback and improvement welcomed. 21 | 22 | The source code for this package can be found 23 | `on GitHub `_. 24 | 25 | 26 | 27 | Installation 28 | ============ 29 | 30 | To install this package you can use `pip ` 31 | that should be available with your python distribution. Use the following at 32 | command prompt: 33 | 34 | .. code:: 35 | 36 | $ pip install ipynb --upgrade 37 | 38 | Make sure to use a recent version of pip! 39 | 40 | ``ipynb`` requires python 3.4 or above to work. It is technically possible to 41 | have it work on older python versions but might require quite some work. We 42 | would welcome your contributions. 43 | 44 | 45 | Developer install 46 | ----------------- 47 | 48 | If you are developing the ``ipynb`` package, we suggest you do a developer install. 49 | After cloning the source code, from the root of the newly clone directory issue a: 50 | 51 | .. code:: 52 | 53 | $ pip install -e . 54 | 55 | 56 | How does ``ipynb`` work 57 | ======================= 58 | 59 | The ``ipynb`` package setup an `importhook 60 | `_ which will automatically 61 | make available as a python module any ``.ipynb`` files as long as the import 62 | starts with ``ipynb.fs.``. 63 | 64 | 65 | Limitation 66 | ========== 67 | 68 | Although ``ipynb`` files are often connected to IPython kernel, ``ipynb`` does 69 | not (yet?) support many of IPython syntactic features like ``!shell command`` 70 | as well as line magics (``%magic``) and cell magics (``%%cell_magic``). While 71 | the former should be pretty easy to emulate, the two later one requires the 72 | code to be ran from with the main namespace of IPython so are unavailable. 73 | 74 | 75 | Import only definitions 76 | ======================= 77 | 78 | If you would like to import only class & function definitions from a notebook 79 | (and not the top level statements), you can use ``ipynb.fs.defs`` instead of 80 | ``ipynb.fs.full``. Full uppercase variable assignment will get evaluated as well. 81 | 82 | Relative imports 83 | ================ 84 | 85 | If you want to import notebooks from other notebooks relatively, you can easily 86 | do that with the following: 87 | 88 | .. code-block:: python 89 | import ipynb.fs 90 | from .full.notebook1 import foo 91 | from .defs.notebook2 import bar 92 | 93 | This will do the imports from other notebooks relative to the path of the notebook 94 | in which the importing is happening. The `import ipynb.fs` is boilerplate that is 95 | required for this feature to work properly. 96 | 97 | Releasing a package that contains notebook files 98 | ================================================ 99 | 100 | You might have the need to release a python package with some modules written 101 | as ``.ipynb`` files, but you do not want to require the ``ipynb`` package 102 | for your users. 103 | 104 | If you are using `setuptools`, you can import `ipynb.setup.find_packages`, 105 | which will convert ``.ipynb`` files to python files on before building an 106 | source distribution or a wheel. This allows others to use your package without 107 | needing to have the ``ipynb`` package installed. 108 | 109 | .. code-block:: python 110 | :caption: setup.py 111 | 112 | from ipynb.setup import find_packages 113 | from setuptools import setup 114 | 115 | setup( 116 | name='trombulator', 117 | version='4.8.15162342', 118 | description='Trombulate with class using trombulator', 119 | url='http://tronbula.tor/', 120 | author='Rick Sanchez', 121 | author_email='morty@lubalubadub.dub', 122 | license='BSD', 123 | packages=find_packages(), 124 | python_requires='>=3.4' 125 | ) 126 | 127 | 128 | 129 | 130 | Contents: 131 | 132 | .. toctree:: 133 | :maxdepth: 2 134 | 135 | 136 | 137 | Indices and tables 138 | ================== 139 | 140 | * :ref:`genindex` 141 | * :ref:`modindex` 142 | * :ref:`search` 143 | 144 | -------------------------------------------------------------------------------- /ipynb/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ipython/ipynb/f82fe63596bff08c7e98743264b4a2a9dcf3b73f/ipynb/__init__.py -------------------------------------------------------------------------------- /ipynb/fs/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Module to enable relative & absolute import of .ipynb files. 3 | 4 | This file in particular is useful to enable relative imports in 5 | interactive notebook usage. None of the code executes outside 6 | of an interactive notebook environment. 7 | 8 | Relative imports require two things: 9 | - `__package__` is set 10 | - The module that is `__package__` is already imported 11 | 12 | So we set __package__ in the notebook's namespace to the __package__ 13 | of this variable. This will allow users to do imports like: 14 | 15 | ``` 16 | from .full.notebook1 import foo 17 | from .defs.notebook2 import bar 18 | ``` 19 | 20 | and they would work everywhere just fine. 21 | """ 22 | try: 23 | import IPython 24 | ip = IPython.get_ipython() 25 | 26 | if ip is not None: 27 | if ip.user_ns['__package__'] is None: 28 | ip.user_ns['__package__'] = __package__ 29 | except ImportError: 30 | # If we don't have IPython installed at all, let it go. 31 | # We aren't running in a jupyter notebook then. 32 | pass 33 | -------------------------------------------------------------------------------- /ipynb/fs/defs/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | FileSystem based importer for ipynb / .py files that only imports function / class definitions. 3 | """ 4 | import sys 5 | import ast 6 | import json 7 | 8 | from importlib.machinery import SourceFileLoader 9 | from ipynb.fs.finder import FSFinder 10 | 11 | from ipynb.utils import code_from_ipynb, validate_nb, filter_ast 12 | 13 | 14 | 15 | class FilteredLoader(SourceFileLoader): 16 | """ 17 | A notebook loader that loads only a subset of the code in an .ipynb file 18 | 19 | It executes and imports only the following top level items: 20 | - imports 21 | - function definitions 22 | - class definitions 23 | - top level assignments where all the targets on the LHS are all caps 24 | 25 | If it isn't an .ipynb file, it's treated the same as a .py file. 26 | """ 27 | def get_code(self, fullname): 28 | if self.path.endswith('.ipynb'): 29 | with open(self.path) as f: 30 | try: 31 | nb = json.load(f) 32 | except ValueError: 33 | # This is if it isn't a valid JSON file at all 34 | raise ImportError('Could not import {path} for {fn}: not a valid ipynb file'.format( 35 | path=self.path, 36 | fn=fullname 37 | )) 38 | if not validate_nb(nb): 39 | # This is when it isn't the appropriate 40 | # nbformet version or language 41 | raise ImportError('Could not import {path} for {fn}: incorrect version or language'.format( 42 | path=self.path, 43 | fn=fullname 44 | )) 45 | return self.source_to_code( 46 | filter_ast(ast.parse(code_from_ipynb(nb))), 47 | self.path 48 | ) 49 | else: 50 | return super().get_code(fullname) 51 | 52 | sys.meta_path.append(FSFinder(__package__, FilteredLoader)) 53 | -------------------------------------------------------------------------------- /ipynb/fs/finder.py: -------------------------------------------------------------------------------- 1 | """ 2 | Contains the finder for use with filesystems. 3 | """ 4 | import sys 5 | import os 6 | 7 | from importlib.abc import MetaPathFinder 8 | from importlib.machinery import ModuleSpec 9 | 10 | 11 | class FSFinder(MetaPathFinder): 12 | """ 13 | Finder for ipynb/py files from the filesystem. 14 | 15 | Only tries to load modules that are under ipynb.fs. 16 | Tries to treat .ipynb and .py files exactly the same as much as possible. 17 | 18 | The loader_class passed in to the constructor is used to do actual loading 19 | """ 20 | def __init__(self, package_prefix, loader_class): 21 | self.loader_class = loader_class 22 | self.package_prefix = package_prefix 23 | 24 | def _get_paths(self, fullname): 25 | """ 26 | Generate ordered list of paths we should look for fullname module in 27 | """ 28 | real_path = os.path.join(*fullname[len(self.package_prefix):].split('.')) 29 | for base_path in sys.path: 30 | if base_path == '': 31 | # Empty string means process's cwd 32 | base_path = os.getcwd() 33 | path = os.path.join(base_path, real_path) 34 | yield path + '.ipynb' 35 | yield path + '.py' 36 | yield os.path.join(path, '__init__.ipynb') 37 | yield os.path.join(path, '__init__.py') 38 | 39 | def find_spec(self, fullname, path, target=None): 40 | """ 41 | Claims modules that are under ipynb.fs 42 | """ 43 | if fullname.startswith(self.package_prefix): 44 | for path in self._get_paths(fullname): 45 | if os.path.exists(path): 46 | return ModuleSpec( 47 | name=fullname, 48 | loader=self.loader_class(fullname, path), 49 | origin=path, 50 | is_package=(path.endswith('__init__.ipynb') or path.endswith('__init__.py')), 51 | ) 52 | -------------------------------------------------------------------------------- /ipynb/fs/full/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | FileSystem based importer for ipynb / .py files. 3 | 4 | If you prefix module names with 'ipynb.fs', it'll try to treat them exactly 5 | the same way as .py files. All the output is ignored, and all the code is imported 6 | as if the cells were linearly written to be in a flat file. 7 | """ 8 | import sys 9 | import json 10 | from importlib.machinery import SourceFileLoader 11 | 12 | from ipynb.utils import code_from_ipynb, validate_nb 13 | from ipynb.fs.finder import FSFinder 14 | 15 | 16 | class FullLoader(SourceFileLoader): 17 | """ 18 | A notebook loader that loads code from a .ipynb file 19 | 20 | It picks out all the code from a .ipynb file and executes it 21 | into the module. 22 | 23 | If it isn't an .ipnb file, it's treated the same as a .py file 24 | """ 25 | def get_code(self, fullname): 26 | if self.path.endswith('.ipynb'): 27 | with open(self.path) as f: 28 | try: 29 | nb = json.load(f) 30 | except ValueError: 31 | # This is when it isn't a valid json file 32 | raise ImportError('Could not import {path} for {fn}: not a valid ipynb file'.format( 33 | path=self.path, 34 | fn=fullname 35 | )) 36 | if not validate_nb(nb): 37 | # This is when it isn't the appropriate 38 | # nbformet version or language 39 | raise ImportError('Could not import {path} for {fn}: incorrect version or language'.format( 40 | path=self.path, 41 | fn=fullname 42 | )) 43 | return self.source_to_code(code_from_ipynb(nb), self.path) 44 | else: 45 | return super().get_code(fullname) 46 | 47 | 48 | sys.meta_path.append(FSFinder(__package__, FullLoader)) 49 | -------------------------------------------------------------------------------- /ipynb/setup/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Provides helpers to produce python packages sourced from .ipynb files 3 | """ 4 | from setuptools import PackageFinder 5 | import os 6 | import glob 7 | import json 8 | from ..utils import code_from_ipynb 9 | 10 | 11 | class IPynbPackageFinder(PackageFinder): 12 | """ 13 | A setuptools PackageFinder that provides .py files for .ipynb files when packaged 14 | """ 15 | @staticmethod 16 | def _looks_like_package(path): 17 | like_package = os.path.isfile(os.path.join(path, '__init__.py')) 18 | like_package = like_package or os.path.isfile(os.path.join(path, '__init__.ipynb')) 19 | if like_package: 20 | ipynbs = glob.glob(os.path.join(path,'*.ipynb')) 21 | for ipynb in ipynbs: 22 | with open(ipynb) as notebook: 23 | data = json.load(notebook) 24 | with open(ipynb.replace('ipynb','py'), 'w') as pyfile: 25 | pyfile.write(code_from_ipynb(data, markdown=True)) 26 | 27 | return like_package 28 | 29 | find_packages = IPynbPackageFinder.find 30 | -------------------------------------------------------------------------------- /ipynb/utils.py: -------------------------------------------------------------------------------- 1 | """ 2 | Utility functions for dealing with manipulating notebooks. 3 | 4 | Try to not put too many things here, nor to re-implement nbformat. 5 | """ 6 | import ast 7 | 8 | 9 | PREAMBLE=\ 10 | """ 11 | ############################################################################## 12 | # This source has been auto generated from an IPython/Jupyter notebook file. # 13 | # Please modify the origin file # 14 | ############################################################################## 15 | """ 16 | 17 | ALLOWED_NODES = set([ 18 | ast.ClassDef, 19 | ast.FunctionDef, 20 | ast.Import, 21 | ast.ImportFrom 22 | ]) 23 | 24 | 25 | def validate_nb(nb): 26 | """ 27 | Validate that given notebook JSON is importable 28 | 29 | - Check for nbformat == 4 30 | - Check that language is python 31 | 32 | Do not re-implement nbformat here :D 33 | """ 34 | if nb['nbformat'] != 4: 35 | return False 36 | 37 | language_name = (nb.get('metadata', {}) 38 | .get('kernelspec', {}) 39 | .get('language', '').lower()) 40 | return language_name == 'python' 41 | 42 | 43 | def filter_ast(module_ast): 44 | """ 45 | Filters a given module ast, removing non-whitelisted nodes 46 | 47 | It allows only the following top level items: 48 | - imports 49 | - function definitions 50 | - class definitions 51 | - top level assignments where all the targets on the LHS are all caps 52 | """ 53 | def node_predicate(node): 54 | """ 55 | Return true if given node is whitelisted 56 | """ 57 | for an in ALLOWED_NODES: 58 | if isinstance(node, an): 59 | return True 60 | 61 | # Recurse through Assign node LHS targets when an id is not specified, 62 | # otherwise check that the id is uppercase 63 | if isinstance(node, ast.Assign): 64 | return all([node_predicate(t) for t in node.targets if not hasattr(t, 'id')]) \ 65 | and all([t.id.isupper() for t in node.targets if hasattr(t, 'id')]) 66 | 67 | return False 68 | 69 | module_ast.body = [n for n in module_ast.body if node_predicate(n)] 70 | return module_ast 71 | 72 | def code_from_ipynb(nb, markdown=False): 73 | """ 74 | Get the code for a given notebook 75 | 76 | nb is passed in as a dictionary that's a parsed ipynb file 77 | """ 78 | code = PREAMBLE 79 | for cell in nb['cells']: 80 | if cell['cell_type'] == 'code': 81 | # transform the input to executable Python 82 | code += ''.join(cell['source']) 83 | if cell['cell_type'] == 'markdown': 84 | code += '\n# ' + '# '.join(cell['source']) 85 | # We want a blank newline after each cell's output. 86 | # And the last line of source doesn't have a newline usually. 87 | code += '\n\n' 88 | return code 89 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | 3 | setup( 4 | name='ipynb', 5 | version='0.5.1', 6 | description='Package / Module importer for importing code from Jupyter Notebook files (.ipynb)', 7 | url='http://github.com/yuvipanda/ipynb', 8 | author='Yuvi Panda', 9 | author_email='yuvipanda@gmail.com', 10 | license='BSD', 11 | packages=find_packages(), 12 | python_requires='>=3.4' 13 | ) 14 | -------------------------------------------------------------------------------- /tests/bogus_ipynb.ipynb: -------------------------------------------------------------------------------- 1 | # This is not a real ipynb file! 2 | # THIS IS BOGUS BWAHAHA 3 | -------------------------------------------------------------------------------- /tests/mixed_ipynb/__init__.py: -------------------------------------------------------------------------------- 1 | from .foo import RAWR 2 | 3 | __all__ = [RAWR] 4 | -------------------------------------------------------------------------------- /tests/mixed_ipynb/foo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This has a mixture of code definitions (classes, etc), variable assignments and markdown.\n", 8 | "\n", 9 | "It has multiple lines!" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": true 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "def foo():\n", 21 | " return 'foo'" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "bar = 'hi'" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 1, 38 | "metadata": { 39 | "collapsed": true 40 | }, 41 | "outputs": [], 42 | "source": [ 43 | "WAT = 'boo'" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "metadata": { 50 | "collapsed": true 51 | }, 52 | "outputs": [], 53 | "source": [ 54 | "class RAWR:\n", 55 | " def rawr(self):\n", 56 | " return 'rawr'" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "metadata": { 63 | "collapsed": false, 64 | "history": [ 65 | { 66 | "code": "r = RAWR()\nprint(r.rawr())", 67 | "response": { 68 | "content": { 69 | "name": "stdout", 70 | "text": "rawr\n" 71 | }, 72 | "metadata": {}, 73 | "msg_type": "stream", 74 | "version": "5.0" 75 | }, 76 | "timestamp": "2016-11-13T16:21:19.538091" 77 | } 78 | ] 79 | }, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "rawr\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "# This will be not picked up by the defs only importer\n", 91 | "r = RAWR()\n", 92 | "print(r.rawr())" 93 | ] 94 | } 95 | ], 96 | "metadata": { 97 | "kernelspec": { 98 | "display_name": "Python 3", 99 | "language": "python", 100 | "name": "python3" 101 | }, 102 | "language_info": { 103 | "codemirror_mode": { 104 | "name": "ipython", 105 | "version": 3 106 | }, 107 | "file_extension": ".py", 108 | "mimetype": "text/x-python", 109 | "name": "python", 110 | "nbconvert_exporter": "python", 111 | "pygments_lexer": "ipython3", 112 | "version": "3.5.2+" 113 | } 114 | }, 115 | "nbformat": 4, 116 | "nbformat_minor": 1 117 | } 118 | -------------------------------------------------------------------------------- /tests/older_nbformat.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "metadata": { 3 | "name": "", 4 | "signature": "sha256:441fa273fba6403e4fa6ef51f4397b1a0ca9ddec43fe8fba217b854e2c7b0377" 5 | }, 6 | "nbformat": 3, 7 | "nbformat_minor": 0, 8 | "worksheets": [ 9 | { 10 | "cells": [ 11 | { 12 | "cell_type": "heading", 13 | "level": 1, 14 | "metadata": {}, 15 | "source": [ 16 | "Basic Output" 17 | ] 18 | }, 19 | { 20 | "cell_type": "markdown", 21 | "metadata": {}, 22 | "source": [ 23 | "When a cell is run, it can generate *output*. In IPython, the definition of output is quite general; it can be text, images, LaTeX, HTML or JSON. All output is displayed below the code that generated it, in the *output area*.\n", 24 | "\n", 25 | "This Notebook describes the basics of output and shows how the `stdout/stderr` streams are handled." 26 | ] 27 | }, 28 | { 29 | "cell_type": "heading", 30 | "level": 2, 31 | "metadata": {}, 32 | "source": [ 33 | "Displayhook" 34 | ] 35 | }, 36 | { 37 | "cell_type": "markdown", 38 | "metadata": {}, 39 | "source": [ 40 | "When a Python object is returned by an expression, Python's `displayhook` mechanism is triggered. In IPython, this results in an output prompt, such as `Out[2]`. These objects are then available under the variables:\n", 41 | "\n", 42 | "* `_` (last output)\n", 43 | "* `__` (second to last output)\n", 44 | "* `_N` (`Out[N]`)" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "collapsed": false, 50 | "input": [ 51 | "import numpy as np\n", 52 | "import sys" 53 | ], 54 | "language": "python", 55 | "metadata": {}, 56 | "outputs": [], 57 | "prompt_number": 1 58 | }, 59 | { 60 | "cell_type": "code", 61 | "collapsed": false, 62 | "input": [ 63 | "np.random.rand(10)" 64 | ], 65 | "language": "python", 66 | "metadata": {}, 67 | "outputs": [ 68 | { 69 | "metadata": {}, 70 | "output_type": "pyout", 71 | "prompt_number": 2, 72 | "text": [ 73 | "array([ 0.95903549, 0.20840774, 0.89732074, 0.72494962, 0.30424358,\n", 74 | " 0.03881097, 0.72698477, 0.92148251, 0.96582423, 0.95202918])" 75 | ] 76 | } 77 | ], 78 | "prompt_number": 2 79 | }, 80 | { 81 | "cell_type": "code", 82 | "collapsed": false, 83 | "input": [ 84 | "np.sin(_)" 85 | ], 86 | "language": "python", 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "metadata": {}, 91 | "output_type": "pyout", 92 | "prompt_number": 3, 93 | "text": [ 94 | "array([ 0.81863802, 0.20690236, 0.78165864, 0.66309773, 0.29957159,\n", 95 | " 0.03880123, 0.66461974, 0.79649888, 0.82251797, 0.81459417])" 96 | ] 97 | } 98 | ], 99 | "prompt_number": 3 100 | }, 101 | { 102 | "cell_type": "heading", 103 | "level": 2, 104 | "metadata": {}, 105 | "source": [ 106 | "sys.stdout and sys.stderr" 107 | ] 108 | }, 109 | { 110 | "cell_type": "markdown", 111 | "metadata": {}, 112 | "source": [ 113 | "The stdout and stderr streams are displayed as text in the output area." 114 | ] 115 | }, 116 | { 117 | "cell_type": "code", 118 | "collapsed": false, 119 | "input": [ 120 | "print(\"hi, stdout\")" 121 | ], 122 | "language": "python", 123 | "metadata": {}, 124 | "outputs": [ 125 | { 126 | "output_type": "stream", 127 | "stream": "stdout", 128 | "text": [ 129 | "hi, stdout\n" 130 | ] 131 | } 132 | ], 133 | "prompt_number": 4 134 | }, 135 | { 136 | "cell_type": "code", 137 | "collapsed": false, 138 | "input": [ 139 | "from __future__ import print_function\n", 140 | "print('hi, stderr', file=sys.stderr)" 141 | ], 142 | "language": "python", 143 | "metadata": {}, 144 | "outputs": [ 145 | { 146 | "output_type": "stream", 147 | "stream": "stderr", 148 | "text": [ 149 | "hi, stderr\n" 150 | ] 151 | } 152 | ], 153 | "prompt_number": 5 154 | }, 155 | { 156 | "cell_type": "heading", 157 | "level": 2, 158 | "metadata": {}, 159 | "source": [ 160 | "Output is asynchronous" 161 | ] 162 | }, 163 | { 164 | "cell_type": "markdown", 165 | "metadata": {}, 166 | "source": [ 167 | "All output is displayed asynchronously as it is generated in the Kernel. If you execute the next cell, you will see the output one piece at a time, not all at the end." 168 | ] 169 | }, 170 | { 171 | "cell_type": "code", 172 | "collapsed": false, 173 | "input": [ 174 | "import time, sys\n", 175 | "for i in range(8):\n", 176 | " print(i)\n", 177 | " time.sleep(0.5)" 178 | ], 179 | "language": "python", 180 | "metadata": {}, 181 | "outputs": [ 182 | { 183 | "output_type": "stream", 184 | "stream": "stdout", 185 | "text": [ 186 | "0\n", 187 | "1" 188 | ] 189 | }, 190 | { 191 | "output_type": "stream", 192 | "stream": "stdout", 193 | "text": [ 194 | "\n", 195 | "2" 196 | ] 197 | }, 198 | { 199 | "output_type": "stream", 200 | "stream": "stdout", 201 | "text": [ 202 | "\n", 203 | "3" 204 | ] 205 | }, 206 | { 207 | "output_type": "stream", 208 | "stream": "stdout", 209 | "text": [ 210 | "\n", 211 | "4" 212 | ] 213 | }, 214 | { 215 | "output_type": "stream", 216 | "stream": "stdout", 217 | "text": [ 218 | "\n", 219 | "5" 220 | ] 221 | }, 222 | { 223 | "output_type": "stream", 224 | "stream": "stdout", 225 | "text": [ 226 | "\n", 227 | "6" 228 | ] 229 | }, 230 | { 231 | "output_type": "stream", 232 | "stream": "stdout", 233 | "text": [ 234 | "\n", 235 | "7" 236 | ] 237 | }, 238 | { 239 | "output_type": "stream", 240 | "stream": "stdout", 241 | "text": [ 242 | "\n" 243 | ] 244 | } 245 | ], 246 | "prompt_number": 6 247 | }, 248 | { 249 | "cell_type": "heading", 250 | "level": 2, 251 | "metadata": {}, 252 | "source": [ 253 | "Large outputs" 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": {}, 259 | "source": [ 260 | "To better handle large outputs, the output area can be collapsed. Run the following cell and then single- or double- click on the active area to the left of the output:" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "collapsed": false, 266 | "input": [ 267 | "for i in range(50):\n", 268 | " print(i)" 269 | ], 270 | "language": "python", 271 | "metadata": {}, 272 | "outputs": [ 273 | { 274 | "output_type": "stream", 275 | "stream": "stdout", 276 | "text": [ 277 | "0\n", 278 | "1\n", 279 | "2\n", 280 | "3\n", 281 | "4\n", 282 | "5\n", 283 | "6\n", 284 | "7\n", 285 | "8\n", 286 | "9\n", 287 | "10\n", 288 | "11\n", 289 | "12\n", 290 | "13\n", 291 | "14\n", 292 | "15\n", 293 | "16\n", 294 | "17\n", 295 | "18\n", 296 | "19\n", 297 | "20\n", 298 | "21\n", 299 | "22\n", 300 | "23\n", 301 | "24\n", 302 | "25\n", 303 | "26\n", 304 | "27\n", 305 | "28\n", 306 | "29\n", 307 | "30\n", 308 | "31\n", 309 | "32\n", 310 | "33\n", 311 | "34\n", 312 | "35\n", 313 | "36\n", 314 | "37\n", 315 | "38\n", 316 | "39\n", 317 | "40\n", 318 | "41\n", 319 | "42\n", 320 | "43\n", 321 | "44\n", 322 | "45\n", 323 | "46\n", 324 | "47\n", 325 | "48\n", 326 | "49\n" 327 | ] 328 | } 329 | ], 330 | "prompt_number": 7 331 | }, 332 | { 333 | "cell_type": "markdown", 334 | "metadata": {}, 335 | "source": [ 336 | "Beyond a certain point, output will scroll automatically:" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "collapsed": false, 342 | "input": [ 343 | "for i in range(500):\n", 344 | " print(2**i - 1)" 345 | ], 346 | "language": "python", 347 | "metadata": {}, 348 | "outputs": [ 349 | { 350 | "output_type": "stream", 351 | "stream": "stdout", 352 | "text": [ 353 | "0\n", 354 | "1\n", 355 | "3\n", 356 | "7\n", 357 | "15\n", 358 | "31\n", 359 | "63\n", 360 | "127\n", 361 | "255\n", 362 | "511\n", 363 | "1023\n", 364 | "2047\n", 365 | "4095\n", 366 | "8191\n", 367 | "16383\n", 368 | "32767\n", 369 | "65535\n", 370 | "131071\n", 371 | "262143\n", 372 | "524287\n", 373 | "1048575\n", 374 | "2097151\n", 375 | "4194303\n", 376 | "8388607\n", 377 | "16777215\n", 378 | "33554431\n", 379 | "67108863\n", 380 | "134217727\n", 381 | "268435455\n", 382 | "536870911\n", 383 | "1073741823\n", 384 | "2147483647\n", 385 | "4294967295\n", 386 | "8589934591\n", 387 | "17179869183\n", 388 | "34359738367\n", 389 | "68719476735\n", 390 | "137438953471\n", 391 | "274877906943\n", 392 | "549755813887\n", 393 | "1099511627775\n", 394 | "2199023255551\n", 395 | "4398046511103\n", 396 | "8796093022207\n", 397 | "17592186044415\n", 398 | "35184372088831\n", 399 | "70368744177663\n", 400 | "140737488355327\n", 401 | "281474976710655\n", 402 | "562949953421311\n", 403 | "1125899906842623\n", 404 | "2251799813685247\n", 405 | "4503599627370495\n", 406 | "9007199254740991\n", 407 | "18014398509481983\n", 408 | "36028797018963967\n", 409 | "72057594037927935\n", 410 | "144115188075855871\n", 411 | "288230376151711743\n", 412 | "576460752303423487\n", 413 | "1152921504606846975\n", 414 | "2305843009213693951\n", 415 | "4611686018427387903\n", 416 | "9223372036854775807\n", 417 | "18446744073709551615\n", 418 | "36893488147419103231\n", 419 | "73786976294838206463\n", 420 | "147573952589676412927\n", 421 | "295147905179352825855\n", 422 | "590295810358705651711\n", 423 | "1180591620717411303423\n", 424 | "2361183241434822606847\n", 425 | "4722366482869645213695\n", 426 | "9444732965739290427391\n", 427 | "18889465931478580854783\n", 428 | "37778931862957161709567\n", 429 | "75557863725914323419135\n", 430 | "151115727451828646838271\n", 431 | "302231454903657293676543\n", 432 | "604462909807314587353087\n", 433 | "1208925819614629174706175\n", 434 | "2417851639229258349412351\n", 435 | "4835703278458516698824703\n", 436 | "9671406556917033397649407\n", 437 | "19342813113834066795298815\n", 438 | "38685626227668133590597631\n", 439 | "77371252455336267181195263\n", 440 | "154742504910672534362390527\n", 441 | "309485009821345068724781055\n", 442 | "618970019642690137449562111\n", 443 | "1237940039285380274899124223\n", 444 | "2475880078570760549798248447\n", 445 | "4951760157141521099596496895\n", 446 | "9903520314283042199192993791\n", 447 | "19807040628566084398385987583\n", 448 | "39614081257132168796771975167\n", 449 | "79228162514264337593543950335\n", 450 | "158456325028528675187087900671\n", 451 | "316912650057057350374175801343\n", 452 | "633825300114114700748351602687\n", 453 | "1267650600228229401496703205375\n", 454 | "2535301200456458802993406410751\n", 455 | "5070602400912917605986812821503\n", 456 | "10141204801825835211973625643007\n", 457 | "20282409603651670423947251286015\n", 458 | "40564819207303340847894502572031\n", 459 | "81129638414606681695789005144063\n", 460 | "162259276829213363391578010288127\n", 461 | "324518553658426726783156020576255\n", 462 | "649037107316853453566312041152511\n", 463 | "1298074214633706907132624082305023\n", 464 | "2596148429267413814265248164610047\n", 465 | "5192296858534827628530496329220095\n", 466 | "10384593717069655257060992658440191\n", 467 | "20769187434139310514121985316880383\n", 468 | "41538374868278621028243970633760767\n", 469 | "83076749736557242056487941267521535\n", 470 | "166153499473114484112975882535043071\n", 471 | "332306998946228968225951765070086143\n", 472 | "664613997892457936451903530140172287\n", 473 | "1329227995784915872903807060280344575\n", 474 | "2658455991569831745807614120560689151\n", 475 | "5316911983139663491615228241121378303\n", 476 | "10633823966279326983230456482242756607\n", 477 | "21267647932558653966460912964485513215\n", 478 | "42535295865117307932921825928971026431\n", 479 | "85070591730234615865843651857942052863\n", 480 | "170141183460469231731687303715884105727\n", 481 | "340282366920938463463374607431768211455\n", 482 | "680564733841876926926749214863536422911\n", 483 | "1361129467683753853853498429727072845823\n", 484 | "2722258935367507707706996859454145691647\n", 485 | "5444517870735015415413993718908291383295\n", 486 | "10889035741470030830827987437816582766591\n", 487 | "21778071482940061661655974875633165533183\n", 488 | "43556142965880123323311949751266331066367\n", 489 | "87112285931760246646623899502532662132735\n", 490 | "174224571863520493293247799005065324265471\n", 491 | "348449143727040986586495598010130648530943\n", 492 | "696898287454081973172991196020261297061887\n", 493 | "1393796574908163946345982392040522594123775\n", 494 | "2787593149816327892691964784081045188247551\n", 495 | "5575186299632655785383929568162090376495103\n", 496 | "11150372599265311570767859136324180752990207\n", 497 | "22300745198530623141535718272648361505980415\n", 498 | "44601490397061246283071436545296723011960831\n", 499 | "89202980794122492566142873090593446023921663\n", 500 | "178405961588244985132285746181186892047843327\n", 501 | "356811923176489970264571492362373784095686655\n", 502 | "713623846352979940529142984724747568191373311\n", 503 | "1427247692705959881058285969449495136382746623\n", 504 | "2854495385411919762116571938898990272765493247\n", 505 | "5708990770823839524233143877797980545530986495\n", 506 | "11417981541647679048466287755595961091061972991\n", 507 | "22835963083295358096932575511191922182123945983\n", 508 | "45671926166590716193865151022383844364247891967\n", 509 | "91343852333181432387730302044767688728495783935\n", 510 | "182687704666362864775460604089535377456991567871\n", 511 | "365375409332725729550921208179070754913983135743\n", 512 | "730750818665451459101842416358141509827966271487\n", 513 | "1461501637330902918203684832716283019655932542975\n", 514 | "2923003274661805836407369665432566039311865085951\n", 515 | "5846006549323611672814739330865132078623730171903\n", 516 | "11692013098647223345629478661730264157247460343807\n", 517 | "23384026197294446691258957323460528314494920687615\n", 518 | "46768052394588893382517914646921056628989841375231\n", 519 | "93536104789177786765035829293842113257979682750463\n", 520 | "187072209578355573530071658587684226515959365500927\n", 521 | "374144419156711147060143317175368453031918731001855\n", 522 | "748288838313422294120286634350736906063837462003711\n", 523 | "1496577676626844588240573268701473812127674924007423\n", 524 | "2993155353253689176481146537402947624255349848014847\n", 525 | "5986310706507378352962293074805895248510699696029695\n", 526 | "11972621413014756705924586149611790497021399392059391\n", 527 | "23945242826029513411849172299223580994042798784118783\n", 528 | "47890485652059026823698344598447161988085597568237567\n", 529 | "95780971304118053647396689196894323976171195136475135\n", 530 | "191561942608236107294793378393788647952342390272950271\n", 531 | "383123885216472214589586756787577295904684780545900543\n", 532 | "766247770432944429179173513575154591809369561091801087\n", 533 | "1532495540865888858358347027150309183618739122183602175\n", 534 | "3064991081731777716716694054300618367237478244367204351\n", 535 | "6129982163463555433433388108601236734474956488734408703\n", 536 | "12259964326927110866866776217202473468949912977468817407\n", 537 | "24519928653854221733733552434404946937899825954937634815\n", 538 | "49039857307708443467467104868809893875799651909875269631\n", 539 | "98079714615416886934934209737619787751599303819750539263\n", 540 | "196159429230833773869868419475239575503198607639501078527\n", 541 | "392318858461667547739736838950479151006397215279002157055\n", 542 | "784637716923335095479473677900958302012794430558004314111\n", 543 | "1569275433846670190958947355801916604025588861116008628223\n", 544 | "3138550867693340381917894711603833208051177722232017256447\n", 545 | "6277101735386680763835789423207666416102355444464034512895\n", 546 | "12554203470773361527671578846415332832204710888928069025791\n", 547 | "25108406941546723055343157692830665664409421777856138051583\n", 548 | "50216813883093446110686315385661331328818843555712276103167\n", 549 | "100433627766186892221372630771322662657637687111424552206335\n", 550 | "200867255532373784442745261542645325315275374222849104412671\n", 551 | "401734511064747568885490523085290650630550748445698208825343\n", 552 | "803469022129495137770981046170581301261101496891396417650687\n", 553 | "1606938044258990275541962092341162602522202993782792835301375\n", 554 | "3213876088517980551083924184682325205044405987565585670602751\n", 555 | "6427752177035961102167848369364650410088811975131171341205503\n", 556 | "12855504354071922204335696738729300820177623950262342682411007\n", 557 | "25711008708143844408671393477458601640355247900524685364822015\n", 558 | "51422017416287688817342786954917203280710495801049370729644031\n", 559 | "102844034832575377634685573909834406561420991602098741459288063\n", 560 | "205688069665150755269371147819668813122841983204197482918576127\n", 561 | "411376139330301510538742295639337626245683966408394965837152255\n", 562 | "822752278660603021077484591278675252491367932816789931674304511\n", 563 | "1645504557321206042154969182557350504982735865633579863348609023\n", 564 | "3291009114642412084309938365114701009965471731267159726697218047\n", 565 | "6582018229284824168619876730229402019930943462534319453394436095\n", 566 | "13164036458569648337239753460458804039861886925068638906788872191\n", 567 | "26328072917139296674479506920917608079723773850137277813577744383\n", 568 | "52656145834278593348959013841835216159447547700274555627155488767\n", 569 | "105312291668557186697918027683670432318895095400549111254310977535\n", 570 | "210624583337114373395836055367340864637790190801098222508621955071\n", 571 | "421249166674228746791672110734681729275580381602196445017243910143\n", 572 | "842498333348457493583344221469363458551160763204392890034487820287\n", 573 | "1684996666696914987166688442938726917102321526408785780068975640575\n", 574 | "3369993333393829974333376885877453834204643052817571560137951281151\n", 575 | "6739986666787659948666753771754907668409286105635143120275902562303\n", 576 | "13479973333575319897333507543509815336818572211270286240551805124607\n", 577 | "26959946667150639794667015087019630673637144422540572481103610249215\n", 578 | "53919893334301279589334030174039261347274288845081144962207220498431\n", 579 | "107839786668602559178668060348078522694548577690162289924414440996863\n", 580 | "215679573337205118357336120696157045389097155380324579848828881993727\n", 581 | "431359146674410236714672241392314090778194310760649159697657763987455\n", 582 | "862718293348820473429344482784628181556388621521298319395315527974911\n", 583 | "1725436586697640946858688965569256363112777243042596638790631055949823\n", 584 | "3450873173395281893717377931138512726225554486085193277581262111899647\n", 585 | "6901746346790563787434755862277025452451108972170386555162524223799295\n", 586 | "13803492693581127574869511724554050904902217944340773110325048447598591\n", 587 | "27606985387162255149739023449108101809804435888681546220650096895197183\n", 588 | "55213970774324510299478046898216203619608871777363092441300193790394367\n", 589 | "110427941548649020598956093796432407239217743554726184882600387580788735\n", 590 | "220855883097298041197912187592864814478435487109452369765200775161577471\n", 591 | "441711766194596082395824375185729628956870974218904739530401550323154943\n", 592 | "883423532389192164791648750371459257913741948437809479060803100646309887\n", 593 | "1766847064778384329583297500742918515827483896875618958121606201292619775\n", 594 | "3533694129556768659166595001485837031654967793751237916243212402585239551\n", 595 | "7067388259113537318333190002971674063309935587502475832486424805170479103\n", 596 | "14134776518227074636666380005943348126619871175004951664972849610340958207\n", 597 | "28269553036454149273332760011886696253239742350009903329945699220681916415\n", 598 | "56539106072908298546665520023773392506479484700019806659891398441363832831\n", 599 | "113078212145816597093331040047546785012958969400039613319782796882727665663\n", 600 | "226156424291633194186662080095093570025917938800079226639565593765455331327\n", 601 | "452312848583266388373324160190187140051835877600158453279131187530910662655\n", 602 | "904625697166532776746648320380374280103671755200316906558262375061821325311\n", 603 | "1809251394333065553493296640760748560207343510400633813116524750123642650623\n", 604 | "3618502788666131106986593281521497120414687020801267626233049500247285301247\n", 605 | "7237005577332262213973186563042994240829374041602535252466099000494570602495\n", 606 | "14474011154664524427946373126085988481658748083205070504932198000989141204991\n", 607 | "28948022309329048855892746252171976963317496166410141009864396001978282409983\n", 608 | "57896044618658097711785492504343953926634992332820282019728792003956564819967\n", 609 | "115792089237316195423570985008687907853269984665640564039457584007913129639935\n", 610 | "231584178474632390847141970017375815706539969331281128078915168015826259279871\n", 611 | "463168356949264781694283940034751631413079938662562256157830336031652518559743\n", 612 | "926336713898529563388567880069503262826159877325124512315660672063305037119487\n", 613 | "1852673427797059126777135760139006525652319754650249024631321344126610074238975\n", 614 | "3705346855594118253554271520278013051304639509300498049262642688253220148477951\n", 615 | "7410693711188236507108543040556026102609279018600996098525285376506440296955903\n", 616 | "14821387422376473014217086081112052205218558037201992197050570753012880593911807\n", 617 | "29642774844752946028434172162224104410437116074403984394101141506025761187823615\n", 618 | "59285549689505892056868344324448208820874232148807968788202283012051522375647231\n", 619 | "118571099379011784113736688648896417641748464297615937576404566024103044751294463\n", 620 | "237142198758023568227473377297792835283496928595231875152809132048206089502588927\n", 621 | "474284397516047136454946754595585670566993857190463750305618264096412179005177855\n", 622 | "948568795032094272909893509191171341133987714380927500611236528192824358010355711\n", 623 | "1897137590064188545819787018382342682267975428761855001222473056385648716020711423\n", 624 | "3794275180128377091639574036764685364535950857523710002444946112771297432041422847\n", 625 | "7588550360256754183279148073529370729071901715047420004889892225542594864082845695\n", 626 | "15177100720513508366558296147058741458143803430094840009779784451085189728165691391\n", 627 | "30354201441027016733116592294117482916287606860189680019559568902170379456331382783\n", 628 | "60708402882054033466233184588234965832575213720379360039119137804340758912662765567\n", 629 | "121416805764108066932466369176469931665150427440758720078238275608681517825325531135\n", 630 | "242833611528216133864932738352939863330300854881517440156476551217363035650651062271\n", 631 | "485667223056432267729865476705879726660601709763034880312953102434726071301302124543\n", 632 | "971334446112864535459730953411759453321203419526069760625906204869452142602604249087\n", 633 | "1942668892225729070919461906823518906642406839052139521251812409738904285205208498175\n", 634 | "3885337784451458141838923813647037813284813678104279042503624819477808570410416996351\n", 635 | "7770675568902916283677847627294075626569627356208558085007249638955617140820833992703\n", 636 | "15541351137805832567355695254588151253139254712417116170014499277911234281641667985407\n", 637 | "31082702275611665134711390509176302506278509424834232340028998555822468563283335970815\n", 638 | "62165404551223330269422781018352605012557018849668464680057997111644937126566671941631\n", 639 | "124330809102446660538845562036705210025114037699336929360115994223289874253133343883263\n", 640 | "248661618204893321077691124073410420050228075398673858720231988446579748506266687766527\n", 641 | "497323236409786642155382248146820840100456150797347717440463976893159497012533375533055\n", 642 | "994646472819573284310764496293641680200912301594695434880927953786318994025066751066111\n", 643 | "1989292945639146568621528992587283360401824603189390869761855907572637988050133502132223\n", 644 | "3978585891278293137243057985174566720803649206378781739523711815145275976100267004264447\n", 645 | "7957171782556586274486115970349133441607298412757563479047423630290551952200534008528895\n", 646 | "15914343565113172548972231940698266883214596825515126958094847260581103904401068017057791\n", 647 | "31828687130226345097944463881396533766429193651030253916189694521162207808802136034115583\n", 648 | "63657374260452690195888927762793067532858387302060507832379389042324415617604272068231167\n", 649 | "127314748520905380391777855525586135065716774604121015664758778084648831235208544136462335\n", 650 | "254629497041810760783555711051172270131433549208242031329517556169297662470417088272924671\n", 651 | "509258994083621521567111422102344540262867098416484062659035112338595324940834176545849343\n", 652 | "1018517988167243043134222844204689080525734196832968125318070224677190649881668353091698687\n", 653 | "2037035976334486086268445688409378161051468393665936250636140449354381299763336706183397375\n", 654 | "4074071952668972172536891376818756322102936787331872501272280898708762599526673412366794751\n", 655 | "8148143905337944345073782753637512644205873574663745002544561797417525199053346824733589503\n", 656 | "16296287810675888690147565507275025288411747149327490005089123594835050398106693649467179007\n", 657 | "32592575621351777380295131014550050576823494298654980010178247189670100796213387298934358015\n", 658 | "65185151242703554760590262029100101153646988597309960020356494379340201592426774597868716031\n", 659 | "130370302485407109521180524058200202307293977194619920040712988758680403184853549195737432063\n", 660 | "260740604970814219042361048116400404614587954389239840081425977517360806369707098391474864127\n", 661 | "521481209941628438084722096232800809229175908778479680162851955034721612739414196782949728255\n", 662 | "1042962419883256876169444192465601618458351817556959360325703910069443225478828393565899456511\n", 663 | "2085924839766513752338888384931203236916703635113918720651407820138886450957656787131798913023\n", 664 | "4171849679533027504677776769862406473833407270227837441302815640277772901915313574263597826047\n", 665 | "8343699359066055009355553539724812947666814540455674882605631280555545803830627148527195652095\n", 666 | "16687398718132110018711107079449625895333629080911349765211262561111091607661254297054391304191\n", 667 | "33374797436264220037422214158899251790667258161822699530422525122222183215322508594108782608383\n", 668 | "66749594872528440074844428317798503581334516323645399060845050244444366430645017188217565216767\n", 669 | "133499189745056880149688856635597007162669032647290798121690100488888732861290034376435130433535\n", 670 | "266998379490113760299377713271194014325338065294581596243380200977777465722580068752870260867071\n", 671 | "533996758980227520598755426542388028650676130589163192486760401955554931445160137505740521734143\n", 672 | "1067993517960455041197510853084776057301352261178326384973520803911109862890320275011481043468287\n", 673 | "2135987035920910082395021706169552114602704522356652769947041607822219725780640550022962086936575\n", 674 | "4271974071841820164790043412339104229205409044713305539894083215644439451561281100045924173873151\n", 675 | "8543948143683640329580086824678208458410818089426611079788166431288878903122562200091848347746303\n", 676 | "17087896287367280659160173649356416916821636178853222159576332862577757806245124400183696695492607\n", 677 | "34175792574734561318320347298712833833643272357706444319152665725155515612490248800367393390985215\n", 678 | "68351585149469122636640694597425667667286544715412888638305331450311031224980497600734786781970431\n", 679 | "136703170298938245273281389194851335334573089430825777276610662900622062449960995201469573563940863\n", 680 | "273406340597876490546562778389702670669146178861651554553221325801244124899921990402939147127881727\n", 681 | "546812681195752981093125556779405341338292357723303109106442651602488249799843980805878294255763455\n", 682 | "1093625362391505962186251113558810682676584715446606218212885303204976499599687961611756588511526911\n", 683 | "2187250724783011924372502227117621365353169430893212436425770606409952999199375923223513177023053823\n", 684 | "4374501449566023848745004454235242730706338861786424872851541212819905998398751846447026354046107647\n", 685 | "8749002899132047697490008908470485461412677723572849745703082425639811996797503692894052708092215295\n", 686 | "17498005798264095394980017816940970922825355447145699491406164851279623993595007385788105416184430591\n", 687 | "34996011596528190789960035633881941845650710894291398982812329702559247987190014771576210832368861183\n", 688 | "69992023193056381579920071267763883691301421788582797965624659405118495974380029543152421664737722367\n", 689 | "139984046386112763159840142535527767382602843577165595931249318810236991948760059086304843329475444735\n", 690 | "279968092772225526319680285071055534765205687154331191862498637620473983897520118172609686658950889471\n", 691 | "559936185544451052639360570142111069530411374308662383724997275240947967795040236345219373317901778943\n", 692 | "1119872371088902105278721140284222139060822748617324767449994550481895935590080472690438746635803557887\n", 693 | "2239744742177804210557442280568444278121645497234649534899989100963791871180160945380877493271607115775\n", 694 | "4479489484355608421114884561136888556243290994469299069799978201927583742360321890761754986543214231551\n", 695 | "8958978968711216842229769122273777112486581988938598139599956403855167484720643781523509973086428463103\n", 696 | "17917957937422433684459538244547554224973163977877196279199912807710334969441287563047019946172856926207\n", 697 | "35835915874844867368919076489095108449946327955754392558399825615420669938882575126094039892345713852415\n", 698 | "71671831749689734737838152978190216899892655911508785116799651230841339877765150252188079784691427704831\n", 699 | "143343663499379469475676305956380433799785311823017570233599302461682679755530300504376159569382855409663\n", 700 | "286687326998758938951352611912760867599570623646035140467198604923365359511060601008752319138765710819327\n", 701 | "573374653997517877902705223825521735199141247292070280934397209846730719022121202017504638277531421638655\n", 702 | "1146749307995035755805410447651043470398282494584140561868794419693461438044242404035009276555062843277311\n", 703 | "2293498615990071511610820895302086940796564989168281123737588839386922876088484808070018553110125686554623\n", 704 | "4586997231980143023221641790604173881593129978336562247475177678773845752176969616140037106220251373109247\n", 705 | "9173994463960286046443283581208347763186259956673124494950355357547691504353939232280074212440502746218495\n", 706 | "18347988927920572092886567162416695526372519913346248989900710715095383008707878464560148424881005492436991\n", 707 | "36695977855841144185773134324833391052745039826692497979801421430190766017415756929120296849762010984873983\n", 708 | "73391955711682288371546268649666782105490079653384995959602842860381532034831513858240593699524021969747967\n", 709 | "146783911423364576743092537299333564210980159306769991919205685720763064069663027716481187399048043939495935\n", 710 | "293567822846729153486185074598667128421960318613539983838411371441526128139326055432962374798096087878991871\n", 711 | "587135645693458306972370149197334256843920637227079967676822742883052256278652110865924749596192175757983743\n", 712 | "1174271291386916613944740298394668513687841274454159935353645485766104512557304221731849499192384351515967487\n", 713 | "2348542582773833227889480596789337027375682548908319870707290971532209025114608443463698998384768703031934975\n", 714 | "4697085165547666455778961193578674054751365097816639741414581943064418050229216886927397996769537406063869951\n", 715 | "9394170331095332911557922387157348109502730195633279482829163886128836100458433773854795993539074812127739903\n", 716 | "18788340662190665823115844774314696219005460391266558965658327772257672200916867547709591987078149624255479807\n", 717 | "37576681324381331646231689548629392438010920782533117931316655544515344401833735095419183974156299248510959615\n", 718 | "75153362648762663292463379097258784876021841565066235862633311089030688803667470190838367948312598497021919231\n", 719 | "150306725297525326584926758194517569752043683130132471725266622178061377607334940381676735896625196994043838463\n", 720 | "300613450595050653169853516389035139504087366260264943450533244356122755214669880763353471793250393988087676927\n", 721 | "601226901190101306339707032778070279008174732520529886901066488712245510429339761526706943586500787976175353855\n", 722 | "1202453802380202612679414065556140558016349465041059773802132977424491020858679523053413887173001575952350707711\n", 723 | "2404907604760405225358828131112281116032698930082119547604265954848982041717359046106827774346003151904701415423\n", 724 | "4809815209520810450717656262224562232065397860164239095208531909697964083434718092213655548692006303809402830847\n", 725 | "9619630419041620901435312524449124464130795720328478190417063819395928166869436184427311097384012607618805661695\n", 726 | "19239260838083241802870625048898248928261591440656956380834127638791856333738872368854622194768025215237611323391\n", 727 | "38478521676166483605741250097796497856523182881313912761668255277583712667477744737709244389536050430475222646783\n", 728 | "76957043352332967211482500195592995713046365762627825523336510555167425334955489475418488779072100860950445293567\n", 729 | "153914086704665934422965000391185991426092731525255651046673021110334850669910978950836977558144201721900890587135\n", 730 | "307828173409331868845930000782371982852185463050511302093346042220669701339821957901673955116288403443801781174271\n", 731 | "615656346818663737691860001564743965704370926101022604186692084441339402679643915803347910232576806887603562348543\n", 732 | "1231312693637327475383720003129487931408741852202045208373384168882678805359287831606695820465153613775207124697087\n", 733 | "2462625387274654950767440006258975862817483704404090416746768337765357610718575663213391640930307227550414249394175\n", 734 | "4925250774549309901534880012517951725634967408808180833493536675530715221437151326426783281860614455100828498788351\n", 735 | "9850501549098619803069760025035903451269934817616361666987073351061430442874302652853566563721228910201656997576703\n", 736 | "19701003098197239606139520050071806902539869635232723333974146702122860885748605305707133127442457820403313995153407\n", 737 | "39402006196394479212279040100143613805079739270465446667948293404245721771497210611414266254884915640806627990306815\n", 738 | "78804012392788958424558080200287227610159478540930893335896586808491443542994421222828532509769831281613255980613631\n", 739 | "157608024785577916849116160400574455220318957081861786671793173616982887085988842445657065019539662563226511961227263\n", 740 | "315216049571155833698232320801148910440637914163723573343586347233965774171977684891314130039079325126453023922454527\n", 741 | "630432099142311667396464641602297820881275828327447146687172694467931548343955369782628260078158650252906047844909055\n", 742 | "1260864198284623334792929283204595641762551656654894293374345388935863096687910739565256520156317300505812095689818111\n", 743 | "2521728396569246669585858566409191283525103313309788586748690777871726193375821479130513040312634601011624191379636223\n", 744 | "5043456793138493339171717132818382567050206626619577173497381555743452386751642958261026080625269202023248382759272447\n", 745 | "10086913586276986678343434265636765134100413253239154346994763111486904773503285916522052161250538404046496765518544895\n", 746 | "20173827172553973356686868531273530268200826506478308693989526222973809547006571833044104322501076808092993531037089791\n", 747 | "40347654345107946713373737062547060536401653012956617387979052445947619094013143666088208645002153616185987062074179583\n", 748 | "80695308690215893426747474125094121072803306025913234775958104891895238188026287332176417290004307232371974124148359167\n", 749 | "161390617380431786853494948250188242145606612051826469551916209783790476376052574664352834580008614464743948248296718335\n", 750 | "322781234760863573706989896500376484291213224103652939103832419567580952752105149328705669160017228929487896496593436671\n", 751 | "645562469521727147413979793000752968582426448207305878207664839135161905504210298657411338320034457858975792993186873343\n", 752 | "1291124939043454294827959586001505937164852896414611756415329678270323811008420597314822676640068915717951585986373746687\n", 753 | "2582249878086908589655919172003011874329705792829223512830659356540647622016841194629645353280137831435903171972747493375\n", 754 | "5164499756173817179311838344006023748659411585658447025661318713081295244033682389259290706560275662871806343945494986751\n", 755 | "10328999512347634358623676688012047497318823171316894051322637426162590488067364778518581413120551325743612687890989973503\n", 756 | "20657999024695268717247353376024094994637646342633788102645274852325180976134729557037162826241102651487225375781979947007\n", 757 | "41315998049390537434494706752048189989275292685267576205290549704650361952269459114074325652482205302974450751563959894015\n", 758 | "82631996098781074868989413504096379978550585370535152410581099409300723904538918228148651304964410605948901503127919788031\n", 759 | "165263992197562149737978827008192759957101170741070304821162198818601447809077836456297302609928821211897803006255839576063\n", 760 | "330527984395124299475957654016385519914202341482140609642324397637202895618155672912594605219857642423795606012511679152127\n", 761 | "661055968790248598951915308032771039828404682964281219284648795274405791236311345825189210439715284847591212025023358304255\n", 762 | "1322111937580497197903830616065542079656809365928562438569297590548811582472622691650378420879430569695182424050046716608511\n", 763 | "2644223875160994395807661232131084159313618731857124877138595181097623164945245383300756841758861139390364848100093433217023\n", 764 | "5288447750321988791615322464262168318627237463714249754277190362195246329890490766601513683517722278780729696200186866434047\n", 765 | "10576895500643977583230644928524336637254474927428499508554380724390492659780981533203027367035444557561459392400373732868095\n", 766 | "21153791001287955166461289857048673274508949854856999017108761448780985319561963066406054734070889115122918784800747465736191\n", 767 | "42307582002575910332922579714097346549017899709713998034217522897561970639123926132812109468141778230245837569601494931472383\n", 768 | "84615164005151820665845159428194693098035799419427996068435045795123941278247852265624218936283556460491675139202989862944767\n", 769 | "169230328010303641331690318856389386196071598838855992136870091590247882556495704531248437872567112920983350278405979725889535\n", 770 | "338460656020607282663380637712778772392143197677711984273740183180495765112991409062496875745134225841966700556811959451779071\n", 771 | "676921312041214565326761275425557544784286395355423968547480366360991530225982818124993751490268451683933401113623918903558143\n", 772 | "1353842624082429130653522550851115089568572790710847937094960732721983060451965636249987502980536903367866802227247837807116287\n", 773 | "2707685248164858261307045101702230179137145581421695874189921465443966120903931272499975005961073806735733604454495675614232575\n", 774 | "5415370496329716522614090203404460358274291162843391748379842930887932241807862544999950011922147613471467208908991351228465151\n", 775 | "10830740992659433045228180406808920716548582325686783496759685861775864483615725089999900023844295226942934417817982702456930303\n", 776 | "21661481985318866090456360813617841433097164651373566993519371723551728967231450179999800047688590453885868835635965404913860607\n", 777 | "43322963970637732180912721627235682866194329302747133987038743447103457934462900359999600095377180907771737671271930809827721215\n", 778 | "86645927941275464361825443254471365732388658605494267974077486894206915868925800719999200190754361815543475342543861619655442431\n", 779 | "173291855882550928723650886508942731464777317210988535948154973788413831737851601439998400381508723631086950685087723239310884863\n", 780 | "346583711765101857447301773017885462929554634421977071896309947576827663475703202879996800763017447262173901370175446478621769727\n", 781 | "693167423530203714894603546035770925859109268843954143792619895153655326951406405759993601526034894524347802740350892957243539455\n", 782 | "1386334847060407429789207092071541851718218537687908287585239790307310653902812811519987203052069789048695605480701785914487078911\n", 783 | "2772669694120814859578414184143083703436437075375816575170479580614621307805625623039974406104139578097391210961403571828974157823\n", 784 | "5545339388241629719156828368286167406872874150751633150340959161229242615611251246079948812208279156194782421922807143657948315647\n", 785 | "11090678776483259438313656736572334813745748301503266300681918322458485231222502492159897624416558312389564843845614287315896631295\n", 786 | "22181357552966518876627313473144669627491496603006532601363836644916970462445004984319795248833116624779129687691228574631793262591\n", 787 | "44362715105933037753254626946289339254982993206013065202727673289833940924890009968639590497666233249558259375382457149263586525183\n", 788 | "88725430211866075506509253892578678509965986412026130405455346579667881849780019937279180995332466499116518750764914298527173050367\n", 789 | "177450860423732151013018507785157357019931972824052260810910693159335763699560039874558361990664932998233037501529828597054346100735\n", 790 | "354901720847464302026037015570314714039863945648104521621821386318671527399120079749116723981329865996466075003059657194108692201471\n", 791 | "709803441694928604052074031140629428079727891296209043243642772637343054798240159498233447962659731992932150006119314388217384402943\n", 792 | "1419606883389857208104148062281258856159455782592418086487285545274686109596480318996466895925319463985864300012238628776434768805887\n", 793 | "2839213766779714416208296124562517712318911565184836172974571090549372219192960637992933791850638927971728600024477257552869537611775\n", 794 | "5678427533559428832416592249125035424637823130369672345949142181098744438385921275985867583701277855943457200048954515105739075223551\n", 795 | "11356855067118857664833184498250070849275646260739344691898284362197488876771842551971735167402555711886914400097909030211478150447103\n", 796 | "22713710134237715329666368996500141698551292521478689383796568724394977753543685103943470334805111423773828800195818060422956300894207\n", 797 | "45427420268475430659332737993000283397102585042957378767593137448789955507087370207886940669610222847547657600391636120845912601788415\n", 798 | "90854840536950861318665475986000566794205170085914757535186274897579911014174740415773881339220445695095315200783272241691825203576831\n", 799 | "181709681073901722637330951972001133588410340171829515070372549795159822028349480831547762678440891390190630401566544483383650407153663\n", 800 | "363419362147803445274661903944002267176820680343659030140745099590319644056698961663095525356881782780381260803133088966767300814307327\n", 801 | "726838724295606890549323807888004534353641360687318060281490199180639288113397923326191050713763565560762521606266177933534601628614655\n", 802 | "1453677448591213781098647615776009068707282721374636120562980398361278576226795846652382101427527131121525043212532355867069203257229311\n", 803 | "2907354897182427562197295231552018137414565442749272241125960796722557152453591693304764202855054262243050086425064711734138406514458623\n", 804 | "5814709794364855124394590463104036274829130885498544482251921593445114304907183386609528405710108524486100172850129423468276813028917247\n", 805 | "11629419588729710248789180926208072549658261770997088964503843186890228609814366773219056811420217048972200345700258846936553626057834495\n", 806 | "23258839177459420497578361852416145099316523541994177929007686373780457219628733546438113622840434097944400691400517693873107252115668991\n", 807 | "46517678354918840995156723704832290198633047083988355858015372747560914439257467092876227245680868195888801382801035387746214504231337983\n", 808 | "93035356709837681990313447409664580397266094167976711716030745495121828878514934185752454491361736391777602765602070775492429008462675967\n", 809 | "186070713419675363980626894819329160794532188335953423432061490990243657757029868371504908982723472783555205531204141550984858016925351935\n", 810 | "372141426839350727961253789638658321589064376671906846864122981980487315514059736743009817965446945567110411062408283101969716033850703871\n", 811 | "744282853678701455922507579277316643178128753343813693728245963960974631028119473486019635930893891134220822124816566203939432067701407743\n", 812 | "1488565707357402911845015158554633286356257506687627387456491927921949262056238946972039271861787782268441644249633132407878864135402815487\n", 813 | "2977131414714805823690030317109266572712515013375254774912983855843898524112477893944078543723575564536883288499266264815757728270805630975\n", 814 | "5954262829429611647380060634218533145425030026750509549825967711687797048224955787888157087447151129073766576998532529631515456541611261951\n", 815 | "11908525658859223294760121268437066290850060053501019099651935423375594096449911575776314174894302258147533153997065059263030913083222523903\n", 816 | "23817051317718446589520242536874132581700120107002038199303870846751188192899823151552628349788604516295066307994130118526061826166445047807\n", 817 | "47634102635436893179040485073748265163400240214004076398607741693502376385799646303105256699577209032590132615988260237052123652332890095615\n", 818 | "95268205270873786358080970147496530326800480428008152797215483387004752771599292606210513399154418065180265231976520474104247304665780191231\n", 819 | "190536410541747572716161940294993060653600960856016305594430966774009505543198585212421026798308836130360530463953040948208494609331560382463\n", 820 | "381072821083495145432323880589986121307201921712032611188861933548019011086397170424842053596617672260721060927906081896416989218663120764927\n", 821 | "762145642166990290864647761179972242614403843424065222377723867096038022172794340849684107193235344521442121855812163792833978437326241529855\n", 822 | "1524291284333980581729295522359944485228807686848130444755447734192076044345588681699368214386470689042884243711624327585667956874652483059711\n", 823 | "3048582568667961163458591044719888970457615373696260889510895468384152088691177363398736428772941378085768487423248655171335913749304966119423\n", 824 | "6097165137335922326917182089439777940915230747392521779021790936768304177382354726797472857545882756171536974846497310342671827498609932238847\n", 825 | "12194330274671844653834364178879555881830461494785043558043581873536608354764709453594945715091765512343073949692994620685343654997219864477695\n", 826 | "24388660549343689307668728357759111763660922989570087116087163747073216709529418907189891430183531024686147899385989241370687309994439728955391\n", 827 | "48777321098687378615337456715518223527321845979140174232174327494146433419058837814379782860367062049372295798771978482741374619988879457910783\n", 828 | "97554642197374757230674913431036447054643691958280348464348654988292866838117675628759565720734124098744591597543956965482749239977758915821567\n", 829 | "195109284394749514461349826862072894109287383916560696928697309976585733676235351257519131441468248197489183195087913930965498479955517831643135\n", 830 | "390218568789499028922699653724145788218574767833121393857394619953171467352470702515038262882936496394978366390175827861930996959911035663286271\n", 831 | "780437137578998057845399307448291576437149535666242787714789239906342934704941405030076525765872992789956732780351655723861993919822071326572543\n", 832 | "1560874275157996115690798614896583152874299071332485575429578479812685869409882810060153051531745985579913465560703311447723987839644142653145087\n", 833 | "3121748550315992231381597229793166305748598142664971150859156959625371738819765620120306103063491971159826931121406622895447975679288285306290175\n", 834 | "6243497100631984462763194459586332611497196285329942301718313919250743477639531240240612206126983942319653862242813245790895951358576570612580351\n", 835 | "12486994201263968925526388919172665222994392570659884603436627838501486955279062480481224412253967884639307724485626491581791902717153141225160703\n", 836 | "24973988402527937851052777838345330445988785141319769206873255677002973910558124960962448824507935769278615448971252983163583805434306282450321407\n", 837 | "49947976805055875702105555676690660891977570282639538413746511354005947821116249921924897649015871538557230897942505966327167610868612564900642815\n", 838 | "99895953610111751404211111353381321783955140565279076827493022708011895642232499843849795298031743077114461795885011932654335221737225129801285631\n", 839 | "199791907220223502808422222706762643567910281130558153654986045416023791284464999687699590596063486154228923591770023865308670443474450259602571263\n", 840 | "399583814440447005616844445413525287135820562261116307309972090832047582568929999375399181192126972308457847183540047730617340886948900519205142527\n", 841 | "799167628880894011233688890827050574271641124522232614619944181664095165137859998750798362384253944616915694367080095461234681773897801038410285055\n", 842 | "1598335257761788022467377781654101148543282249044465229239888363328190330275719997501596724768507889233831388734160190922469363547795602076820570111\n", 843 | "3196670515523576044934755563308202297086564498088930458479776726656380660551439995003193449537015778467662777468320381844938727095591204153641140223\n", 844 | "6393341031047152089869511126616404594173128996177860916959553453312761321102879990006386899074031556935325554936640763689877454191182408307282280447\n", 845 | "12786682062094304179739022253232809188346257992355721833919106906625522642205759980012773798148063113870651109873281527379754908382364816614564560895\n", 846 | "25573364124188608359478044506465618376692515984711443667838213813251045284411519960025547596296126227741302219746563054759509816764729633229129121791\n", 847 | "51146728248377216718956089012931236753385031969422887335676427626502090568823039920051095192592252455482604439493126109519019633529459266458258243583\n", 848 | "102293456496754433437912178025862473506770063938845774671352855253004181137646079840102190385184504910965208878986252219038039267058918532916516487167\n", 849 | "204586912993508866875824356051724947013540127877691549342705710506008362275292159680204380770369009821930417757972504438076078534117837065833032974335\n", 850 | "409173825987017733751648712103449894027080255755383098685411421012016724550584319360408761540738019643860835515945008876152157068235674131666065948671\n", 851 | "818347651974035467503297424206899788054160511510766197370822842024033449101168638720817523081476039287721671031890017752304314136471348263332131897343\n", 852 | "1636695303948070935006594848413799576108321023021532394741645684048066898202337277441635046162952078575443342063780035504608628272942696526664263794687\n" 853 | ] 854 | } 855 | ], 856 | "prompt_number": 8 857 | }, 858 | { 859 | "cell_type": "heading", 860 | "level": 2, 861 | "metadata": {}, 862 | "source": [ 863 | "Capturing output with %%capture" 864 | ] 865 | }, 866 | { 867 | "cell_type": "markdown", 868 | "metadata": {}, 869 | "source": [ 870 | "IPython has a [cell magic](Cell%20Magics.ipynb), `%%capture`, which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable." 871 | ] 872 | }, 873 | { 874 | "cell_type": "code", 875 | "collapsed": false, 876 | "input": [ 877 | "from __future__ import print_function\n", 878 | "import sys" 879 | ], 880 | "language": "python", 881 | "metadata": {}, 882 | "outputs": [], 883 | "prompt_number": 9 884 | }, 885 | { 886 | "cell_type": "markdown", 887 | "metadata": {}, 888 | "source": [ 889 | "By default, `%%capture` discards these streams. This is a simple way to suppress unwanted output." 890 | ] 891 | }, 892 | { 893 | "cell_type": "code", 894 | "collapsed": false, 895 | "input": [ 896 | "%%capture\n", 897 | "print('hi, stdout')\n", 898 | "print('hi, stderr', file=sys.stderr)" 899 | ], 900 | "language": "python", 901 | "metadata": {}, 902 | "outputs": [], 903 | "prompt_number": 10 904 | }, 905 | { 906 | "cell_type": "markdown", 907 | "metadata": {}, 908 | "source": [ 909 | "If you specify a name, then stdout/stderr will be stored in an object in your namespace." 910 | ] 911 | }, 912 | { 913 | "cell_type": "code", 914 | "collapsed": false, 915 | "input": [ 916 | "%%capture captured\n", 917 | "print('hi, stdout')\n", 918 | "print('hi, stderr', file=sys.stderr)" 919 | ], 920 | "language": "python", 921 | "metadata": {}, 922 | "outputs": [], 923 | "prompt_number": 11 924 | }, 925 | { 926 | "cell_type": "code", 927 | "collapsed": false, 928 | "input": [ 929 | "captured" 930 | ], 931 | "language": "python", 932 | "metadata": {}, 933 | "outputs": [ 934 | { 935 | "metadata": {}, 936 | "output_type": "pyout", 937 | "prompt_number": 12, 938 | "text": [ 939 | "" 940 | ] 941 | } 942 | ], 943 | "prompt_number": 12 944 | }, 945 | { 946 | "cell_type": "markdown", 947 | "metadata": {}, 948 | "source": [ 949 | "Calling the object writes the output to stdout/stderr as appropriate." 950 | ] 951 | }, 952 | { 953 | "cell_type": "code", 954 | "collapsed": false, 955 | "input": [ 956 | "captured()" 957 | ], 958 | "language": "python", 959 | "metadata": {}, 960 | "outputs": [ 961 | { 962 | "output_type": "stream", 963 | "stream": "stdout", 964 | "text": [ 965 | "hi, stdout\n" 966 | ] 967 | }, 968 | { 969 | "output_type": "stream", 970 | "stream": "stderr", 971 | "text": [ 972 | "hi, stderr\n" 973 | ] 974 | } 975 | ], 976 | "prompt_number": 13 977 | }, 978 | { 979 | "cell_type": "code", 980 | "collapsed": false, 981 | "input": [ 982 | "captured.stdout" 983 | ], 984 | "language": "python", 985 | "metadata": {}, 986 | "outputs": [ 987 | { 988 | "metadata": {}, 989 | "output_type": "pyout", 990 | "prompt_number": 14, 991 | "text": [ 992 | "'hi, stdout\\n'" 993 | ] 994 | } 995 | ], 996 | "prompt_number": 14 997 | }, 998 | { 999 | "cell_type": "code", 1000 | "collapsed": false, 1001 | "input": [ 1002 | "captured.stderr" 1003 | ], 1004 | "language": "python", 1005 | "metadata": {}, 1006 | "outputs": [ 1007 | { 1008 | "metadata": {}, 1009 | "output_type": "pyout", 1010 | "prompt_number": 15, 1011 | "text": [ 1012 | "'hi, stderr\\n'" 1013 | ] 1014 | } 1015 | ], 1016 | "prompt_number": 15 1017 | }, 1018 | { 1019 | "cell_type": "markdown", 1020 | "metadata": {}, 1021 | "source": [ 1022 | "`%%capture` grabs all output types, not just stdout/stderr, so you can do plots and use IPython's display system inside `%%capture`" 1023 | ] 1024 | }, 1025 | { 1026 | "cell_type": "code", 1027 | "collapsed": false, 1028 | "input": [ 1029 | "%matplotlib inline\n", 1030 | "import matplotlib.pyplot as plt\n", 1031 | "import numpy as np" 1032 | ], 1033 | "language": "python", 1034 | "metadata": {}, 1035 | "outputs": [], 1036 | "prompt_number": 16 1037 | }, 1038 | { 1039 | "cell_type": "code", 1040 | "collapsed": false, 1041 | "input": [ 1042 | "%%capture wontshutup\n", 1043 | "\n", 1044 | "print(\"setting up X\")\n", 1045 | "x = np.linspace(0,5,1000)\n", 1046 | "print(\"step 2: constructing y-data\")\n", 1047 | "y = np.sin(x)\n", 1048 | "print(\"step 3: display info about y\")\n", 1049 | "plt.plot(x,y)\n", 1050 | "print(\"okay, I'm done now\")" 1051 | ], 1052 | "language": "python", 1053 | "metadata": {}, 1054 | "outputs": [], 1055 | "prompt_number": 17 1056 | }, 1057 | { 1058 | "cell_type": "code", 1059 | "collapsed": false, 1060 | "input": [ 1061 | "wontshutup()" 1062 | ], 1063 | "language": "python", 1064 | "metadata": {}, 1065 | "outputs": [ 1066 | { 1067 | "output_type": "stream", 1068 | "stream": "stdout", 1069 | "text": [ 1070 | "setting up X\n", 1071 | "step 2: constructing y-data\n", 1072 | "step 3: display info about y\n", 1073 | "okay, I'm done now\n" 1074 | ] 1075 | }, 1076 | { 1077 | "metadata": {}, 1078 | "output_type": "display_data", 1079 | "png": "iVBORw0KGgoAAAANSUhEUgAAAXoAAAEACAYAAAC9Gb03AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xt8z3X/x/HHmOqHcupq2HaZy7A5j0nJNKcUWVI/h1Rz\nqKSEUnT8oStSOlxqV4WrRK5rDv1ySCMp39KYoVXE9UPlalsskgiF+f7+eIewscP3u/fn+/k+77fb\n98by2b7P28qr917vU4jX6/UiIiKuVc52ABER8S8VehERl1OhFxFxORV6ERGXU6EXEXE5FXoREZcr\ndaEfNGgQYWFhNG3atNBnhg8fTv369WnevDlZWVmlfUsRESmGUhf6gQMHsmzZskL/PC0tje3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+2bduydetWIiMjmTFjhu1IVqSnpzN79mxWrlxJXFwccXFxLFu2\nzHYsK3bu3EnHjh1p0aIFbdq0oUePHnTq1Ml2LEcI5tZvXl4eCQkJJ/+7uP7667nmmmsKfV4bpkRE\nXE5T1yIiLqdCLyLicir0IiIup0IvIuJyKvQiIi6nQi8i4nIq9CIiLqdCLyLicv8PoaUAhzXYTeQA\nAAAASUVORK5CYII=\n", 1080 | "text": [ 1081 | "" 1082 | ] 1083 | } 1084 | ], 1085 | "prompt_number": 18 1086 | }, 1087 | { 1088 | "cell_type": "markdown", 1089 | "metadata": {}, 1090 | "source": [ 1091 | "And you can selectively disable capturing stdout, stderr or rich display, by passing `--no-stdout`, `--no-stderr` and `--no-display`" 1092 | ] 1093 | }, 1094 | { 1095 | "cell_type": "code", 1096 | "collapsed": false, 1097 | "input": [ 1098 | "%%capture cap --no-stderr\n", 1099 | "print('hi, stdout')\n", 1100 | "print(\"hello, stderr\", file=sys.stderr)" 1101 | ], 1102 | "language": "python", 1103 | "metadata": {}, 1104 | "outputs": [ 1105 | { 1106 | "output_type": "stream", 1107 | "stream": "stderr", 1108 | "text": [ 1109 | "hello, stderr\n" 1110 | ] 1111 | } 1112 | ], 1113 | "prompt_number": 19 1114 | }, 1115 | { 1116 | "cell_type": "code", 1117 | "collapsed": false, 1118 | "input": [ 1119 | "cap.stdout" 1120 | ], 1121 | "language": "python", 1122 | "metadata": {}, 1123 | "outputs": [ 1124 | { 1125 | "metadata": {}, 1126 | "output_type": "pyout", 1127 | "prompt_number": 20, 1128 | "text": [ 1129 | "'hi, stdout\\n'" 1130 | ] 1131 | } 1132 | ], 1133 | "prompt_number": 20 1134 | }, 1135 | { 1136 | "cell_type": "code", 1137 | "collapsed": false, 1138 | "input": [ 1139 | "cap.stderr" 1140 | ], 1141 | "language": "python", 1142 | "metadata": {}, 1143 | "outputs": [ 1144 | { 1145 | "metadata": {}, 1146 | "output_type": "pyout", 1147 | "prompt_number": 21, 1148 | "text": [ 1149 | "''" 1150 | ] 1151 | } 1152 | ], 1153 | "prompt_number": 21 1154 | }, 1155 | { 1156 | "cell_type": "code", 1157 | "collapsed": false, 1158 | "input": [ 1159 | "cap.outputs" 1160 | ], 1161 | "language": "python", 1162 | "metadata": {}, 1163 | "outputs": [ 1164 | { 1165 | "metadata": {}, 1166 | "output_type": "pyout", 1167 | "prompt_number": 22, 1168 | "text": [ 1169 | "[]" 1170 | ] 1171 | } 1172 | ], 1173 | "prompt_number": 22 1174 | } 1175 | ], 1176 | "metadata": {} 1177 | } 1178 | ] 1179 | } -------------------------------------------------------------------------------- /tests/pure_ipynb/__init__.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 2, 6 | "metadata": { 7 | "collapsed": false, 8 | "history": [ 9 | { 10 | "code": "from .foo import RAWR\n\n__all__ = [RAWR]", 11 | "response": { 12 | "content": { 13 | "ename": "SystemError", 14 | "evalue": "Parent module '' not loaded, cannot perform relative import", 15 | "traceback": [ 16 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 17 | "\u001b[0;31mSystemError\u001b[0m Traceback (most recent call last)", 18 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mfoo\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mRAWR\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0m__all__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mRAWR\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 19 | "\u001b[0;31mSystemError\u001b[0m: Parent module '' not loaded, cannot perform relative import" 20 | ] 21 | }, 22 | "metadata": {}, 23 | "msg_type": "error", 24 | "version": "5.0" 25 | }, 26 | "timestamp": "2016-11-13T17:01:10.441036" 27 | }, 28 | { 29 | "code": "from .foo import RAWR\n\n__all__ = [RAWR]", 30 | "response": { 31 | "content": { 32 | "ename": "SystemError", 33 | "evalue": "Parent module '' not loaded, cannot perform relative import", 34 | "traceback": [ 35 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 36 | "\u001b[0;31mSystemError\u001b[0m Traceback (most recent call last)", 37 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0;34m.\u001b[0m\u001b[0mfoo\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mRAWR\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0m__all__\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mRAWR\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 38 | "\u001b[0;31mSystemError\u001b[0m: Parent module '' not loaded, cannot perform relative import" 39 | ] 40 | }, 41 | "metadata": {}, 42 | "msg_type": "error", 43 | "version": "5.0" 44 | }, 45 | "timestamp": "2016-11-13T17:07:52.236213" 46 | }, 47 | { 48 | "code": "if __package__ is None:\nfrom .foo import RAWR\n\n__all__ = [RAWR]", 49 | "response": { 50 | "content": { 51 | "ename": "IndentationError", 52 | "evalue": "expected an indented block (, line 2)", 53 | "traceback": [ 54 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m from .foo import RAWR\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" 55 | ] 56 | }, 57 | "metadata": {}, 58 | "msg_type": "error", 59 | "version": "5.0" 60 | }, 61 | "timestamp": "2016-11-13T17:08:03.881435" 62 | } 63 | ] 64 | }, 65 | "outputs": [ 66 | { 67 | "ename": "IndentationError", 68 | "evalue": "expected an indented block (, line 2)", 69 | "output_type": "error", 70 | "traceback": [ 71 | "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m from .foo import RAWR\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m expected an indented block\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "if __package__ is None:\n", 77 | " __package__ = ''\n", 78 | "from .foo import RAWR\n", 79 | "\n", 80 | "__all__ = [RAWR]" 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": null, 86 | "metadata": { 87 | "collapsed": true 88 | }, 89 | "outputs": [], 90 | "source": [] 91 | } 92 | ], 93 | "metadata": { 94 | "kernelspec": { 95 | "display_name": "Python 3", 96 | "language": "python", 97 | "name": "python3" 98 | }, 99 | "language_info": { 100 | "codemirror_mode": { 101 | "name": "ipython", 102 | "version": 3 103 | }, 104 | "file_extension": ".py", 105 | "mimetype": "text/x-python", 106 | "name": "python", 107 | "nbconvert_exporter": "python", 108 | "pygments_lexer": "ipython3", 109 | "version": "3.5.2+" 110 | } 111 | }, 112 | "nbformat": 4, 113 | "nbformat_minor": 1 114 | } 115 | -------------------------------------------------------------------------------- /tests/pure_ipynb/foo.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "This has a mixture of code definitions (classes, etc), variable assignments and markdown.\n", 8 | "\n", 9 | "It has multiple lines!" 10 | ] 11 | }, 12 | { 13 | "cell_type": "code", 14 | "execution_count": 1, 15 | "metadata": { 16 | "collapsed": true 17 | }, 18 | "outputs": [], 19 | "source": [ 20 | "def foo():\n", 21 | " return 'foo'" 22 | ] 23 | }, 24 | { 25 | "cell_type": "code", 26 | "execution_count": 2, 27 | "metadata": { 28 | "collapsed": true 29 | }, 30 | "outputs": [], 31 | "source": [ 32 | "bar = 'hi'" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": 1, 38 | "metadata": { 39 | "collapsed": true 40 | }, 41 | "outputs": [], 42 | "source": [ 43 | "WAT = 'boo'" 44 | ] 45 | }, 46 | { 47 | "cell_type": "code", 48 | "execution_count": 2, 49 | "metadata": { 50 | "collapsed": true 51 | }, 52 | "outputs": [], 53 | "source": [ 54 | "class RAWR:\n", 55 | " def rawr(self):\n", 56 | " return 'rawr'" 57 | ] 58 | }, 59 | { 60 | "cell_type": "code", 61 | "execution_count": 3, 62 | "metadata": { 63 | "collapsed": false, 64 | "history": [ 65 | { 66 | "code": "r = RAWR()\nprint(r.rawr())", 67 | "response": { 68 | "content": { 69 | "name": "stdout", 70 | "text": "rawr\n" 71 | }, 72 | "metadata": {}, 73 | "msg_type": "stream", 74 | "version": "5.0" 75 | }, 76 | "timestamp": "2016-11-13T16:21:19.538091" 77 | } 78 | ] 79 | }, 80 | "outputs": [ 81 | { 82 | "name": "stdout", 83 | "output_type": "stream", 84 | "text": [ 85 | "rawr\n" 86 | ] 87 | } 88 | ], 89 | "source": [ 90 | "# This will be not picked up by the defs only importer\n", 91 | "r = RAWR()\n", 92 | "print(r.rawr())" 93 | ] 94 | } 95 | ], 96 | "metadata": { 97 | "kernelspec": { 98 | "display_name": "Python 3", 99 | "language": "python", 100 | "name": "python3" 101 | }, 102 | "language_info": { 103 | "codemirror_mode": { 104 | "name": "ipython", 105 | "version": 3 106 | }, 107 | "file_extension": ".py", 108 | "mimetype": "text/x-python", 109 | "name": "python", 110 | "nbconvert_exporter": "python", 111 | "pygments_lexer": "ipython3", 112 | "version": "3.5.2+" 113 | } 114 | }, 115 | "nbformat": 4, 116 | "nbformat_minor": 1 117 | } 118 | -------------------------------------------------------------------------------- /tests/r_notebook.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": false 8 | }, 9 | "outputs": [ 10 | { 11 | "name": "stdout", 12 | "output_type": "stream", 13 | "text": [ 14 | "[1] \"Hello world\"\n" 15 | ] 16 | } 17 | ], 18 | "source": [ 19 | "print(\"Hello world\")" 20 | ] 21 | } 22 | ], 23 | "metadata": { 24 | "kernelspec": { 25 | "display_name": "R", 26 | "language": "R", 27 | "name": "ir" 28 | }, 29 | "language_info": { 30 | "codemirror_mode": "r", 31 | "file_extension": ".r", 32 | "mimetype": "text/x-r-source", 33 | "name": "R", 34 | "pygments_lexer": "r", 35 | "version": "3.3.2" 36 | } 37 | }, 38 | "nbformat": 4, 39 | "nbformat_minor": 0 40 | } 41 | -------------------------------------------------------------------------------- /tests/test_defs.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import importlib 3 | 4 | 5 | @pytest.fixture( 6 | scope='module', 7 | params=[ 8 | 'ipynb.fs.defs.pure_ipynb.foo', 9 | 'ipynb.fs.defs.mixed_ipynb.foo' 10 | ] 11 | ) 12 | def foo(request): 13 | return importlib.import_module(request.param) 14 | 15 | @pytest.fixture( 16 | scope='module', 17 | params=[ 18 | 'ipynb.fs.defs.pure_ipynb', 19 | 'ipynb.fs.defs.mixed_ipynb' 20 | ] 21 | ) 22 | def init(request): 23 | return importlib.import_module(request.param) 24 | 25 | def test_execute(foo): 26 | assert foo.foo() == 'foo' 27 | rawr = foo.RAWR() 28 | assert rawr.rawr() == 'rawr' 29 | 30 | def test_no_execute(foo): 31 | assert not hasattr(foo, 'bar') 32 | assert not hasattr(foo, 'r') 33 | 34 | def test_allcaps_execute(foo): 35 | assert foo.WAT == 'boo' 36 | 37 | def test_all(init): 38 | r = init.RAWR() 39 | assert r.rawr() == 'rawr' 40 | 41 | def test_bogus_ipynb(): 42 | with pytest.raises(ImportError): 43 | import ipynb.fs.defs.bogus_ipynb as bogus_ipynb 44 | 45 | def test_r_notebook(): 46 | with pytest.raises(ImportError): 47 | import ipynb.fs.defs.r_notebook 48 | 49 | def test_nbformat_2(): 50 | with pytest.raises(ImportError): 51 | import ipynb.fs.defs.older_nbformat 52 | -------------------------------------------------------------------------------- /tests/test_full.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import importlib 3 | 4 | 5 | @pytest.fixture( 6 | scope='module', 7 | params=[ 8 | 'ipynb.fs.full.pure_ipynb.foo', 9 | 'ipynb.fs.full.mixed_ipynb.foo' 10 | ] 11 | ) 12 | def foo(request): 13 | return importlib.import_module(request.param) 14 | 15 | @pytest.fixture( 16 | scope='module', 17 | params=[ 18 | 'ipynb.fs.defs.pure_ipynb', 19 | 'ipynb.fs.defs.mixed_ipynb' 20 | ] 21 | ) 22 | def init(request): 23 | return importlib.import_module(request.param) 24 | 25 | def test_execute(foo): 26 | assert foo.foo() == 'foo' 27 | assert foo.bar == 'hi' 28 | assert foo.r.rawr() == 'rawr' 29 | 30 | def test_allcaps_execute(foo): 31 | assert foo.WAT == 'boo' 32 | 33 | def test_all(init): 34 | r = init.RAWR() 35 | assert r.rawr() == 'rawr' 36 | 37 | def test_bogus_ipynb(): 38 | with pytest.raises(ImportError): 39 | import ipynb.fs.full.bogus_ipynb as bogus_ipynb 40 | 41 | def test_r_notebook(): 42 | with pytest.raises(ImportError): 43 | import ipynb.fs.full.r_notebook 44 | 45 | def test_nbformat_2(): 46 | with pytest.raises(ImportError): 47 | import ipynb.fs.full.older_nbformat 48 | --------------------------------------------------------------------------------