Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
thomasballinger / parse.py
Created Feb 11, 2014
Adventures in doctesting
View parse.py
def operators_and_non_operators(s, operator_list):
"""returns the non-operators
>>> operators_and_non_operators('2*3+3-4', ['+', '-'])
(['+', '-'], ['2*3', '3', '4'])
"""
ops = []
non_ops = []
current_non_op = ''
for c in s:
View framework.py
"""
Example app:
from framework import server, add_route
def home():
return "<html>Hello!</html>"
add_route('/', home)
server.serve_forever()
View tomleahparser.py
import re
class Tokens():
def __init__(self, s):
self.tokens = tokenize(s)
self.current_i = 0
def peek(self):
return self.tokens[self.current_i]
def consume(self):
t = self.tokens[self.current_i]
View queens.py
import itertools
def brute():
for candidate in itertools.permutations(range(8)):
if (len(set(x - y for x, y in enumerate(candidate))) == 8 and
len(set(x + y for x, y in enumerate(candidate))) == 8):
yield candidate
def ordinals():
opposite_of_teenager = ["th", "st", "nd", "rd"] + ["th"] * 6
View Node.java
public class Node {
public Node left;
public Node right;
public int value;
public Node(int value, Node left, Node right){
this.value = value;
this.left = left;
View traversal.rb
require 'rspec'
require 'forwardable'
require 'pry'
require 'set'
class ListyThing
extend Forwardable
def_delegators :@data, :<<, :empty?, :concat
def initialize
@data = []
View Connect 4
##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
View combined.py
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)
View server.py
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()
View opt.py
#!/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):