Skip to content

Instantly share code, notes, and snippets.

View thomasballinger's full-sized avatar

Tom Ballinger thomasballinger

View GitHub Profile
from typing import Callable
class A: pass
class B: pass
class C: pass
def composition(aToB: Callable[[A], B],
bToC: Callable[[B], C]) -> Callable[[A], C]:
"""Returns a callable that takes an A and returns a C"""
a simple remix file format
Files are always UTF-8
a syntax like
episode <episode query> of <podcast url>
play from <time> to <time> [at 2x speed] [at +12 dB]
example:
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@thomasballinger
thomasballinger / openedByPython.log
Created February 14, 2016 01:45
.py files opened by Python just to start up
tomb@localhost:~$ strace python myPythonScript.py 2>&1 >/dev/null | grep open | grep -v ENOENT | grep -o '".*"' | grep -v '\.pyc'
"/etc/ld.so.cache"
"/lib/x86_64-linux-gnu/libpthread.so.0"
"/lib/x86_64-linux-gnu/libc.so.6"
"/lib/x86_64-linux-gnu/libdl.so.2"
"/lib/x86_64-linux-gnu/libutil.so.1"
"/lib/x86_64-linux-gnu/libz.so.1"
"/lib/x86_64-linux-gnu/libm.so.6"
"/usr/lib/python2.7/site.py"
"/usr/lib/python2.7/os.py"
import tty
import termios
import sys
class Cbreak(object):
def __enter__(self):
self.original_stty = termios.tcgetattr(sys.stdin)
tty.setcbreak(sys.stdin, termios.TCSANOW)
#!/usr/bin/env python
import socket
listener = socket.socket()
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind(('', 8000))
listener.listen(5)
while True:
def preview(content):
import webbrowser
import tempfile
import time
with tempfile.NamedTemporaryFile(suffix='.html') as f:
f.write(content)
f.flush()
print f.name
webbrowser.open_new_tab('file://'+f.name)
time.sleep(1)
import re
def tokenize(s):
"""
>>> tokenize("(1.1 + 2 * 3)")
['(', '1.1', '+', '2', '*', '3', ')']
"""
return re.findall("[0123456789.a-zA-Z]+|[*+/\-()=]", s)
#!/usr/bin/env python2
import sys
import urllib2
import json
def do_gist_json(s):
""" Use json to post to github. """
gist_public = False
gist_url = 'https://api.github.com/gists'
import random
def write_data(filename, num_bytes=1000000):
with open(filename, 'w') as f:
for _ in range((num_bytes / 1000) + 1):
f.write(''.join(random.choice('ab') for _ in range(1000)))
def read_chunks(filename, num_bytes=1000):
with open(filename, 'r') as f:
while True: