Skip to content

Instantly share code, notes, and snippets.

View takluyver's full-sized avatar

Thomas Kluyver takluyver

View GitHub Profile
@takluyver
takluyver / make_nb.py
Created July 21, 2016 13:09
Make a notebook from a script
"""Create a notebook containing code from a script.
Run as: python make_nb.py my_script.py
"""
import sys
import nbformat
from nbformat.v4 import new_notebook, new_code_cell
nb = new_notebook()
@takluyver
takluyver / 2to3_nb.py
Last active November 5, 2021 02:45
Run 2to3 on IPython notebooks
#!/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
@takluyver
takluyver / h5py_strings.py
Created October 15, 2020 08:59
Read HDF5 strings as str with h5py 2.x and 3.0
"""Read HDF5 strings to Python str
h5py 3.0 changed the APIs for storing and reading strings.
This shows how to read strings as Python 3 str if you need
to support both old and new h5py.
"""
import h5py
import numpy as np
f = h5py.File('foo.h5', 'r')
@takluyver
takluyver / GPL_EXCEPTION.txt
Created July 13, 2014 03:31
PyQt GPL exception
Riverbank GPL Exception version 1.1
===================================
Additional rights granted beyond the GPL (the "Exception").
As a special exception to the terms and conditions of version 2.0 of the GPL,
Riverbank hereby grants you the rights described below, provided you agree to
the terms and conditions in this Exception, including its obligations and
restrictions on use.
@takluyver
takluyver / asyncio_magic.py
Created January 25, 2016 11:53
Cell magic for 'await' expressions in IPython
# (c) Thomas Kluyver, 2016
# Use it under the MIT license
# This is fairly experimental. Use at your own risk.
import ast
from ast import Call, Attribute, Name, Load
import asyncio as asyncio_mod
#import astpp
from IPython.utils.text import indent
@takluyver
takluyver / README.md
Created September 6, 2014 21:44
Flatten notebooks for git diff

Copy nbflatten.py to somewhere on $PATH. Then, in the root of a git repository, run these commands:

echo "*.ipynb diff=ipynb" >> .gitattributes 
git config diff.ipynb.textconv nbflatten.py

When you change a notebook and run git diff, you'll see the diff of flattened, simplified notebooks, rather than the full JSON. This does lose some information (metadata, non-text output), but it makes it easier to see simple changes in the notebook.

This doesn't help with merging conflicting changes in notebooks. For that, see nbdiff.org.

@takluyver
takluyver / cache_format_options.py
Created August 27, 2019 11:26
Karabo data cache format experimentation
import json
import h5py
import numpy as np
def read_json(path):
with open(path) as f:
loaded_data = json.load(f)
res = {}
for info in loaded_data:
@takluyver
takluyver / numpy_array_sum0.npy
Last active June 7, 2019 16:47
Bizarre numpy sum behaviour
@takluyver
takluyver / VDS issue.ipynb
Created April 9, 2019 14:21
HDF5 virtual dataset access issue
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@takluyver
takluyver / example
Created May 13, 2016 19:17
Debug on interrupt
$ python3 interrupt-debug.py
0
1
2
3
^C--Return--
> /home/takluyver/scratch/interrupt-debug.py(6)handle_sigint()->None
-> pdb.set_trace()
(Pdb) p a
4