Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
thomasballinger / display.py
Created April 3, 2014 13:23
simple recursive tree display
tree = [1, [2, [10, 3, 4], [11, 5, 6]], [12, 7,8]]
def display(t, indent=0):
if isinstance(t, list):
print ' '*indent, t[0]
display(t[1], indent+4)
display(t[2], indent+4)
else:
print ' '*indent, t
import select
import socket
class EchoServer(object):
def __init__(self, reactor):
self.sock = socket.socket()
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind(('', 8000))
self.sock.listen(2)
@thomasballinger
thomasballinger / getsourcelinesbug.py
Created April 14, 2014 17:08
Possible bug in inspect.getsourcelines
"""
inspect.getsourcelines incorrectly guesses what lines correspond
to the function foo
see getblock in inspect.py
once it finds a lambda, def or class it finishes it then stops
so get getsourcelines returns only the first two noop decorator
lines of bar, while normal behavior is to return all decorators
as it does for foo
"""
import termios, fcntl, sys, os, select, socket, threading, time
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = oldterm[:]
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
class LazyLength(object):
def __init__(self, data):
self.data = data
def __iter__(self):
len = 0
for x in self.data:
yield x
len += 1
self.len = len
def __len__(self):
tom-mba:~ tomb$ ipython
Python 2.7.5 (default, Nov 26 2013, 10:28:53)
Type "copyright", "credits" or "license" for more information.
IPython 2.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
import code
import re
import select
import signal
import socket
import sys
from curtsies import input
from curtsies.fmtfuncs import green, blue
from subprocess import Popen, PIPE
from curtsies.fmtfuncs import *
import code
import traceback
class Interp(code.InteractiveInterpreter):
def showtraceback(self):
output_lines = []
lines = ['asdf', 'asdfa.py', 'asdfasdf']
import sys
from pygments.style import Style
from pygments.token import *
from pygments.formatter import Formatter
from curtsies.bpythonparse import parse
default_colors = {
'keyword': 'y',
'name': 'c',
'comment': 'b',
""" Evidence the += method is bad:
In [4]: def plusequals():
...: s = ''
...: for x in 'asdf'*1000:
...: s += x
In [5]: plusequals()
In [6]: def joinmethod():
...: l = 'asdf'*1000