Skip to content

Instantly share code, notes, and snippets.

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):
import socket
import select
import sys
clients = []
server = socket.socket()
server.bind(('', 8001))
server.listen(1)
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)
@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 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 / 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
#!/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):
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()
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)
##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