Skip to content

Instantly share code, notes, and snippets.

View vgel's full-sized avatar

Theia Vogel vgel

View GitHub Profile
@vgel
vgel / golfed-msgboard.py
Created July 23, 2012 12:47
A simple message-board-type site in golfed python. Expanded version below if you can't read golfed code or want comments.
import BaseHTTPServer as b,sys,os,json,urlparse
p=(json.loads(open(sys.argv[1]).read()) if os.path.exists(sys.argv[1]) else {})
n="<br/>"
h="""<form action=%s method="post">Post: <textarea name="a"></textarea><input type="submit" value="Post!"/></form>"""
y=lambda s:urlparse.parse_qs(s)['a'][0]
class F(b.BaseHTTPRequestHandler):
def do_GET(s):
s.send_response(200)
s.send_header("Content-type","text/html")
s.end_headers()
@vgel
vgel / a.py
Created July 20, 2012 19:55
simple platformer. run python a.py mapfile
import pygame as pg,sys
pg.init()
w=pg.display.set_mode((100,)*2)
o=list(open(sys.argv[1]).read().replace("\n",""))
px=py=50
j=0
k=pg.key.get_pressed
t=lambda x,y:o[x/5+y/5*20]
z=lambda i:min(95,max(0,i))
while True:
@vgel
vgel / gist:3098126
Created July 12, 2012 13:32
A autocomplete-like word suggester. Run with `python <file> "/usr/share/dict/words"` Enter words at command line and hit enter for suggestions
import sys
t={}
def w(o):
c=t
for l in o:
c[l]=c[l]if l in c else{}
c=c[l]
c[None]=None
g=lambda t,b,i:reduce(lambda i,l:i+[b]if l is None else g(t[l],b+l,i),t,i)
map(w,open(sys.argv[1]).read().split("\n"))
@vgel
vgel / fileserver.py
Created July 9, 2012 14:16
A very basic HTTP file server in 13 lines of python. Assumes all requests are GETs, and it vulnerable to directory traversal (Run it in ~ and localhost:8080/../../ will ls root), so don't use it online. Will correctly list files in directories.
import sys, os, socket
s = socket.socket()
s.bind((sys.argv[1], int(sys.argv[2])))
s.listen(5)
try:
while True:
conn, addr = s.accept()
path = os.path.join(os.getcwd(), "./"+conn.recv(4096).split("\n")[0].split(" ")[1])
conn.send((open(path).read() if os.path.isfile(path) else reduce(lambda x,s:x+"\n"+s+("/" if os.path.isdir(s) else ""),sorted(os.listdir(path)),"Directory "+path+" ls")) if os.path.exists(path) else '404: '+path)
conn.close()
@vgel
vgel / tinystackr3.py
Created July 8, 2012 23:59
Tiny stack-based language in python.
stack = []
compileStacks = []
words = {}
builtins = {}
lambdaType = type(lambda x: x) #cannot compare against the function type directly for some reason
def prn(o):
print(o)
def clr(l):