Skip to content

Instantly share code, notes, and snippets.

@ghtdak
Last active August 29, 2015 14:06
Show Gist options
  • Save ghtdak/426a70eb309a9c1d9a1c to your computer and use it in GitHub Desktop.
Save ghtdak/426a70eb309a9c1d9a1c to your computer and use it in GitHub Desktop.
IPython Notebook Git revision control with Pre-commit hook, Netcat and Python Daemon
#!/usr/bin/python
import socket
import itertools as it
from IPython.nbformat.current import reads, writes
def processIPython(strin):
json_in = reads(strin, 'json')
for sheet in json_in.worksheets:
for cell in sheet.cells:
if "outputs" in cell:
cell.outputs = []
if "prompt_number" in cell:
cell.prompt_number = None
data = writes(json_in, 'json')
return data
TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024 # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
while True:
try:
conn, addr = s.accept()
print 'Connection address:', addr
ghtBuffer = []
while 1:
data = conn.recv(BUFFER_SIZE)
if not data: break
ghtBuffer.append(data)
data = "".join(ghtBuffer)
data = processIPython(data)
conn.send(data) # echo
conn.close()
except:
print "weird exception"
#!/bin/bash
# This is my all time favorite script
nc 127.0.0.1 5005
@ghtdak
Copy link
Author

ghtdak commented Sep 17, 2014

Python is slow as a filter. The script is invoked multiple times by git tools like tig, git diff, etc.

Launching a tiny python daemon and using Netcat to twist the ends together works well.

Good info from: stackoverflow though make sure to use 'None instead of '' in the Json dict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment