#!/usr/bin/env python """ simple example script for running and testing notebooks. Usage: `ipnbdoctest.py foo.ipynb [bar.ipynb [...]]` Each cell is submitted to the kernel, and the outputs are compared with those stored in the notebook. """ from __future__ import print_function import os,sys,time import base64 import re from difflib import unified_diff as diff from collections import defaultdict try: from queue import Empty except ImportError: print('Python 3.x is needed to run this script.') sys.exit(77) import imp try: imp.find_module('IPython') except: print('IPython is needed to run this script.') sys.exit(77) try: from IPython.kernel import KernelManager except ImportError: from IPython.zmq.blockingkernelmanager import BlockingKernelManager as KernelManager # Until Debian ships IPython 3.0, we stick to the v3 format. from IPython.nbformat import v3 as nbformat def compare_png(a64, b64): """compare two b64 PNGs (incomplete)""" try: import Image except ImportError: pass adata = base64.decodestring(a64) bdata = base64.decodestring(b64) return True def sanitize(s): """sanitize a string for comparison. fix universal newlines, strip trailing newlines, and normalize likely random values (memory addresses and UUIDs) """ if not isinstance(s, str): return s # normalize newline: s = s.replace('\r\n', '\n') # ignore trailing newlines (but not space) s = s.rstrip('\n') # remove hex addresses: s = re.sub(r'at 0x[a-f0-9]+', 'object', s) # normalize UUIDs: s = re.sub(r'[a-f0-9]{8}(\-[a-f0-9]{4}){3}\-[a-f0-9]{12}', 'U-U-I-D', s) # SVG generated by graphviz may put note at different positions # depending on the graphviz build. Let's just strip anything that # look like a position. s = re.sub(r'