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
import socket | |
import select | |
import sys | |
clients = [] | |
server = socket.socket() | |
server.bind(('', 8001)) | |
server.listen(1) |
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
""" | |
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 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
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
#!/usr/bin/env python | |
import sys | |
import urllib | |
import webbrowser | |
def get_opt_url(s, from_bpython=True): | |
""" Use json to post to github. """ | |
def fix(text): |
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 socket | |
# see wsgiref.simple_server for a real implementation of a WSGI server | |
def serve(app): | |
listener = socket.socket() | |
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
listener.bind(('', 8080)) | |
listener.listen(5) | |
while True: | |
s, addr = listener.accept() |
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 socket | |
def serve(): | |
listener = socket.socket() | |
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
listener.bind(('', 8080)) | |
listener.listen(5) | |
while True: | |
s, addr = listener.accept() | |
request = s.recv(10000) |
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
##Brian Chesley, Connect 4 | |
def run(): | |
board = [] | |
for i in range(0,6): | |
board.append([0]*7) | |
for row in board: | |
print row | |
turn = 1 | |
p1_wins = False |