This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from curtsies.fmtfuncs import * | |
import code | |
import traceback | |
class Interp(code.InteractiveInterpreter): | |
def showtraceback(self): | |
output_lines = [] | |
lines = ['asdf', 'asdfa.py', 'asdfasdf'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" 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 |