Skip to content

Instantly share code, notes, and snippets.

@shazow
shazow / Dockerfile
Created July 3, 2014 20:30
PostgreSQL Dockerfile
# DOCKER-VERSION 0.6.1
# Borrowed from https://github.com/vvlad/dockerfiles-postgresql-9.3
#
# First run:
# $ docker build -t postgresql -rm=true .
# $ ID=$(docker run -e "CREATE_DB=foo" -v "path/to/socks:/var/run/postgresql" -d postgresql)
# $ docker wait $ID
# $ docker logs $ID
FROM ubuntu:12.04
@shazow
shazow / metaclass_woes.py
Created June 18, 2014 04:38
Python Metaclass woes.
storage = {}
class RegistryMeta(type):
def __init__(cls, name, bases, attrs):
super(RegistryMeta, cls).__init__(name, bases, attrs)
id = getattr(cls, 'id', None)
if not id:
return
@shazow
shazow / gist:11339093
Last active August 29, 2015 14:00 — forked from dahu/gist:11338846
autocmd CursorMoved,CursorMovedI * hi FOW ctermfg=black ctermbg=NONE guifg=#444444 guibg=NONE
call matchadd('FOW', '\%(.*\n\)\{1,20\}\ze\%(.*\n\)\{3\}.*\%#')
call matchadd('FOW', '.*\%#.*\%(\n.*\)\{3\}\zs\%(.*\n\)\{1,20\}')
@shazow
shazow / nightmare.py
Created April 24, 2014 22:21
A "higher-level" socket module for Python. Inspired by the lovely urllib2.
s = socket2.build_opener(socket2.IPv4Resolver(host), port);
s.addsecurewrap = socket.wrap_socket
conn = s.open()

Keybase proof

I hereby claim:

  • I am shazow on github.
  • I am shazow (https://keybase.io/shazow) on keybase.
  • I have a public key whose fingerprint is 9FCE A980 CCFD 3C13 E11E 88A9 3506 87D1 7E81 FD68

To claim this, I am signing this object:

@shazow
shazow / _thumbnail.py
Last active August 29, 2015 13:57
Thumbnailing helpers.
from math import ceil
def resize_dimensions(x, y, max_width=None, max_height=None, min_side=None, max_side=None):
if not any([max_width, max_height, min_side, max_side]):
return x, y
original_x, original_y = x, y
priority_width = True
if max_width and x > max_width:
@shazow
shazow / gist:8036248
Last active December 31, 2015 19:49
Human helpers (unstdlib.py candidates)
import re
from unstdlib import html
_Default = object()
RE_HUMAN_URL = re.compile('^(\w*://)?(www\.)?(.+)/?$')
def human_url(s, max_length=None):
@shazow
shazow / gist:7091224
Created October 21, 2013 21:21
Disabled backend for dogpile.cache, useful for testing.
from dogpile.cache.api import CacheBackend, NO_VALUE
class DisabledBackend(CacheBackend):
def __init__(self, arguments):
pass
def get(self, key):
return NO_VALUE
def set(self, key, value):
@shazow
shazow / cache_key_generator.py
Last active December 24, 2015 17:49
Cache key generator, can be used for things like dogpile.cache
"""
Based on code of dogpile.cache.util.function_key_generator(...) but adds support for kw.
Related dogpile.cache bug: https://bitbucket.org/zzzeek/dogpile.cache/issue/43/kw-support-in-function_key_generator
Usage:
from dogpile.cache import make_region
my_region = make_region(
function_key_generator=make_key_generator,
@shazow
shazow / gist:6428419
Created September 3, 2013 19:25
Caching with Go
// Without caching, or with a memoization wrapper:
func (c *Context) FooApi() (*Result, error) {
return c.apiClient.someGoogleApiQuery.Do()
}
func (c *Context) BarApi(someArg string) (*Result, error) {
return c.apiClient.someGoogleApiQuery.Filter(someArg).Do()
}