Skip to content

Commit

Permalink
Converted book notebooks to py3
Browse files Browse the repository at this point in the history
Converted using 2to3_nb.py: https://gist.github.com/takluyver/c8839593c615bb2f6e80

Interactive still seems to be broken.
  • Loading branch information
d-ming committed Oct 10, 2017
1 parent 817dadc commit 8add359
Show file tree
Hide file tree
Showing 31 changed files with 391 additions and 483 deletions.
80 changes: 80 additions & 0 deletions AR-book/2to3_nb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python3
"""
To run: python3 nb2to3.py notebook-or-directory
"""
# Authors: Thomas Kluyver, Fernando Perez
# See: https://gist.github.com/takluyver/c8839593c615bb2f6e80

import argparse
import pathlib
from nbformat import read, write

import lib2to3
from lib2to3.refactor import RefactoringTool, get_fixers_from_package


def refactor_notebook_inplace(rt, path):

def refactor_cell(src):
#print('\n***SRC***\n', src)
try:
tree = rt.refactor_string(src+'\n', str(path) + '/cell-%d' % i)
except (lib2to3.pgen2.parse.ParseError,
lib2to3.pgen2.tokenize.TokenError):
return src
else:
return str(tree)[:-1]


print("Refactoring:", path)
nb = read(str(path), as_version=4)

# Run 2to3 on code
for i, cell in enumerate(nb.cells, start=1):
if cell.cell_type == 'code':
if cell.execution_count in (' ', '*'):
cell.execution_count = None

if cell.source.startswith('%%'):
# For cell magics, try to refactor the body, in case it's
# valid python
head, source = cell.source.split('\n', 1)
cell.source = head + '\n' + refactor_cell(source)
else:
cell.source = refactor_cell(cell.source)


# Update notebook metadata
nb.metadata.kernelspec = {
'display_name': 'Python 3',
'name': 'python3',
'language': 'python',
}
if 'language_info' in nb.metadata:
nb.metadata.language_info.codemirror_mode = {
'name': 'ipython',
'version': 3,
}
nb.metadata.language_info.pygments_lexer = 'ipython3'
nb.metadata.language_info.pop('version', None)

write(nb, str(path))

def main(argv=None):
ap = argparse.ArgumentParser()
ap.add_argument('path', type=pathlib.Path,
help="Notebook or directory containing notebooks")

options = ap.parse_args(argv)

avail_fixes = set(get_fixers_from_package('lib2to3.fixes'))
rt = RefactoringTool(avail_fixes)

if options.path.is_dir():
for nb_path in options.path.rglob('*.ipynb'):
refactor_notebook_inplace(rt, nb_path)
else:
refactor_notebook_inplace(rt, options.path)

if __name__ == '__main__':
main()
221 changes: 25 additions & 196 deletions AR-book/AR-book-notebooks/Ch 1/Sam, Alex and Donald.ipynb

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions AR-book/AR-book-notebooks/Ch 2/Convex hull demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -190,21 +190,20 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
100 changes: 16 additions & 84 deletions AR-book/AR-book-notebooks/Ch 2/Convex_hulls_in_Python.ipynb

Large diffs are not rendered by default.

115 changes: 98 additions & 17 deletions AR-book/AR-book-notebooks/Ch 2/Example 12.ipynb

Large diffs are not rendered by default.

9 changes: 4 additions & 5 deletions AR-book/AR-book-notebooks/Ch 2/Linear mixing demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,20 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
18 changes: 11 additions & 7 deletions AR-book/AR-book-notebooks/Ch 2/NaCl_KOH demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import scipy as sp\n",
Expand Down Expand Up @@ -53,7 +55,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def plot_fn(c1c2_l=0.5, c2c3_l=0.5, m1m2_l=0.5):\n",
Expand Down Expand Up @@ -131,21 +135,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
Expand Down
22 changes: 14 additions & 8 deletions AR-book/AR-book-notebooks/Ch 2/Three beakers demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import scipy as sp\n",
Expand Down Expand Up @@ -60,7 +62,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"#combine concentrations for performance when plotting\n",
Expand All @@ -78,7 +82,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def plot_fn(V1=50, V2=50, V3=50,\n",
Expand Down Expand Up @@ -153,21 +159,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
Expand Down
9 changes: 4 additions & 5 deletions AR-book/AR-book-notebooks/Ch 3/Ch3 mixing game demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -184,21 +184,20 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
9 changes: 4 additions & 5 deletions AR-book/AR-book-notebooks/Ch 4/CSTR multiple solutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -166,21 +166,20 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
9 changes: 4 additions & 5 deletions AR-book/AR-book-notebooks/Ch 4/Constant alpha DSRs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -221,21 +221,20 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
" \n",
" if sp.fabs(n)<=tol: \n",
" found_pts += 1\n",
" print found_pts\n",
" print(found_pts)\n",
" \n",
" cstr_cs[k,:] = Ci\n",
" k = k + 1\n",
Expand All @@ -313,21 +313,20 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
9 changes: 4 additions & 5 deletions AR-book/AR-book-notebooks/Ch 4/PFR trajectory demo.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,20 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 8add359

Please sign in to comment.