Skip to content

Instantly share code, notes, and snippets.

View schlamar's full-sized avatar

Marc Schlaich schlamar

View GitHub Profile
@schlamar
schlamar / gist:6317614
Created August 23, 2013 10:00
SQLAlchemy: count number of executed statements
import sys
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
import model
if sys.version_info >= (2, 7):
import unittest as unittest2
@schlamar
schlamar / example.py
Last active December 19, 2015 19:49
Tornado's IOStream on top of tulip's Transport/Protocol API.
import re
import tulip
import tulipiostream
loop = tulip.get_event_loop()
LENGTH_PATTERN = re.compile(b'Content-Length: (\d*)\\r\\n')
@schlamar
schlamar / bench_torn.py
Created July 11, 2013 09:30
Tulip vs. Tornado benchmark.
import timeit
import tornloop
import tulip
loop = tornloop.TornadoLoop()
tulip.set_event_loop(loop)
@schlamar
schlamar / gist:5966805
Created July 10, 2013 14:33
tulip.coroutine vs. tulip.task
import timeit
import tulip
loop = tulip.get_event_loop()
def async_func(value):
p = tulip.Future()
@schlamar
schlamar / gist:5965809
Last active December 19, 2015 13:59
Some helpers for PEP 3156 Futures.
def link(f, other):
'''Resolve other with the current Future.'''
def on_done(f):
e = f.exception()
if e:
other.set_exception(e)
else:
other.set_result(f.result())
@schlamar
schlamar / gist:5458717
Last active December 16, 2015 15:49
Deap function composition as alternative to eval lamba expression in lambdify.
def _lookup(pos):
def composition(*args):
return args[pos]
return composition
def _apply(func, *funcs):
def composition(*args):
return func(*(f(*args) for f in funcs))
return composition
@schlamar
schlamar / gist:5373110
Created April 12, 2013 16:05
Another serialization benchmark, OSX Python 2.7.3
import cPickle
import json
import ujson
import msgpack
d = {
'words': """
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Mauris adipiscing adipiscing placerat.
Vestibulum augue augue,
@schlamar
schlamar / remconsole.py
Created April 11, 2013 13:31
Fork of `rfoo.utils.rconsole` to run with other RPC implementations.
"""
remconsole.py
A Python console you can embed in a program and attach to remotely.
To spawn a Python console in a script do the following in any scope
of any module:
import remconsole
remconsole.spawn_server()
@schlamar
schlamar / gist:5319691
Created April 5, 2013 14:28
Insertion sort.
from operator import lt
def insertion_sort(data, cmp_func=lt):
for i in xrange(1, len(data)):
if cmp_func(data[i], data[i - 1]):
el = data.pop(i)
j = i - 1
while j > 0 and cmp_func(el, data[j - 1]):
@schlamar
schlamar / stpip.py
Last active December 15, 2015 17:59
Run pip within Sublime Text.
import sys
import os
def _packages_path():
t = os.path.join(os.path.dirname(__file__), '..')
return os.path.abspath(t)
SITE_PACKAGES = os.path.join(_packages_path(), 'Lib', 'site-packages')
_PACKAGING_MODS = os.path.join(_packages_path(), 'Lib', 'packaging')