Skip to content

Instantly share code, notes, and snippets.

View longfin's full-sized avatar

Swen Mun longfin

View GitHub Profile
(defn split [seq delimiter]
(letfn [(check-non-delimiter [val] (not (= val delimiter)))]
(cond
(empty? seq)
seq
(every? check-non-delimiter seq)
(list seq)
:default
(cons
(take-while check-non-delimiter seq)
@longfin
longfin / gist:1422196
Created December 2, 2011 07:29
딱히 의도한건 아니지만 메모삼아...
from collections import deque, Iterable
def flatten(l):
"""
>>> [i for i in flatten([1, [2,3], [5, [6,7]]])]
[1, 2, 3, 5, 6, 7]
"""
queue = deque(l)
while len(queue) > 0:
p = queue.popleft()
@longfin
longfin / gist:1426133
Created December 3, 2011 05:11
요런 식으로 sqlalchemy의 joinedload, subqueryload를 바꿔치기함
from functools import wraps
def __extend_keys(l):
ret = [[]]
for p in l:
for r in list(ret):
if isinstance(p, list):
ret.remove(r)
for e in p:
nr = list(r)
@longfin
longfin / gist:1441283
Created December 7, 2011 03:18
Class Factory?
TAG_TYPES = ["location", "category"]
class Tag(db.Model, BaseEntityMixIn,
IgnoreDeletionMixIn):
__tablename__ = "tag"
name = db.Column(db.Unicode(255), index=True)
type = db.Column(db.Enum(*TAG_TYPES, native_enum=False), index=True)
__mapper_args__ = {'polymorphic_on': type}
g = globals()
@longfin
longfin / gist:1455788
Created December 10, 2011 18:04
kmp search
"""
http://en.wikipedia.org/wiki/Knuth%E2%80%93Morris%E2%80%93Pratt_algorithm
"""
def kmp_make_table(word):
table = len(word) * [0]
pos = 2
cnd = 0
table[0] = -1
table[1] = 0
@longfin
longfin / gist:1606034
Created January 13, 2012 13:11
very simple wsgi application
def app(environ, start_response):
response_body = 'Hello World!'
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
@longfin
longfin / gist:1606137
Created January 13, 2012 13:29
very simple wsgi application(via class)
class App(object):
def __init__(self, environ, start_response):
self.environ = environ
self.start_response = start_response
def __iter__(self):
status = '200 OK'
response_body = "Hello World!"
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]
@longfin
longfin / gist:1606870
Created January 13, 2012 15:06
wsgiref.simple_server example...
from wsgiref.simple_server import make_server
httpd = make_server('', 8000, app)
httpd.serve_forever()
@longfin
longfin / gist:1615142
Created January 15, 2012 09:00
environ dump wsgi app
def dump_environ_app(environ, start_response):
response_body = "\n".join(["{0}: {1}".format(k, environ[k]) for k in environ.keys()])
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
@longfin
longfin / gist:1616103
Created January 15, 2012 15:00
MethodRewriteMiddleware
from werkzeug import url_decode
class MethodRewriteMiddleware(object):
"""
app = MethodRewriteMiddleware(app)
"""
def __init__(self, app, input_name = '__method__'):
self.app = app
self.input_name = input_name