Skip to content

Instantly share code, notes, and snippets.

View alecthomas's full-sized avatar
😀

Alec Thomas alecthomas

😀
View GitHub Profile
import logging
import gevent
from thrift.server.TServer import TServer
# XXX Hackish, but should be safe: monkey patch gevent socket support into
# Thrift. Overall I think this is cleaner than reimplementing all of TSocket.
from thrift.transport import TSocket; TSocket.socket = gevent.socket
from thrift.transport.TTransport import TTransportException
$ curl -d 'hello world' http://pushyrobot.appspot.com/push/swapoff.org/m6I02zmlA
<pre>Traceback (most recent call last):
File &quot;/base/python_lib/versions/1/google/appengine/ext/webapp/__init__.py&quot;, line 512, in __call__
handler.post(*groups)
File &quot;/base/data/home/apps/pushyrobot/1.340741887312755514/pushy/receive.py&quot;, line 19, in post
if self._pushy.handle_push(self.request.path, self.request.body):
File &quot;/base/data/home/apps/pushyrobot/1.340741887312755514/pushy/pushy.py&quot;, line 23, in handle_push
self._reply_to_wave(wave_id, wavelet_id, message)
File &quot;/base/data/home/apps/pushyrobot/1.340741887312755514/pushy/pushy.py&quot;, line 27, in _reply_to_wave
type TaskNode struct {
children []*Node
}
func (self *Task) AddChild(node *TaskNode) {
self.children = append(self.children, node)
}
func (self *Task) ChildAt(index int) *TaskNode {
return self.children[index]
@alecthomas
alecthomas / gist:4053506
Created November 11, 2012 02:54
SublimeLinter module for Go (golang) based on https://github.com/SublimeLinter/SublimeLinter/pull/78/files
# -*- coding: utf-8 -*-
# golang.py - sublimelint package for checking golang files
import glob
import os
import re
import subprocess
from base_linter import BaseLinter, INPUT_METHOD_FILE
@alecthomas
alecthomas / gist:4103124
Created November 18, 2012 02:46
Flask + Injector - A dependency-injected web framework.
from injector import Module, inject, singleton
from flask import Flask, Request, jsonify
from flask.ext.cache import Cache
from flask.ext.injector import Builder, route, decorator
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy import Column, String
"""
@alecthomas
alecthomas / gist:4497119
Created January 9, 2013 21:28
*Very* hacky YAML + Salt structure validation.
import itertools
import functools
import fnmatch
import os
from StringIO import StringIO
import yaml
from yaml.parser import ParserError
import voluptuous as V
@alecthomas
alecthomas / console_log_call_stack.js
Created March 22, 2013 20:24
Javascript console.log() with collapsed call stack
if (typeof console !== undefined) {
console.logJack = console.log;
console.log = function(){
console.groupCollapsed.apply(console, arguments);
console.logJack(new Error().stack);
console.groupEnd();
};
}
@alecthomas
alecthomas / gist:5408999
Created April 18, 2013 00:51
Django: Users joined by hour
models.User.objects.order_by('date_joined').extra({'day_joined': 'date(date_joined)', 'hour_joined': 'hour(date_joined)'}).values('day_joined', 'hour_joined').annotate(joined_count=Count('id'))
@alecthomas
alecthomas / gist:5758156
Created June 11, 2013 16:02
Example of Injector assisted injection using @provides.
from injector import *
class Session(object):
def __init__(self, key, site):
self.key = key
self.site = site
SessionKey = Key('SessionKey')
@alecthomas
alecthomas / gist:5779354
Last active December 18, 2015 11:59
Monkey patching context manager
from contextlib import contextmanager
@contextmanager
def patch(owner, attr, value):
"""Monkey patch context manager.
with patch(os, 'open', myopen):
...
"""
old = getattr(owner, attr)