Skip to content

Instantly share code, notes, and snippets.

@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:

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 / 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()
@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 / 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 / Dockerfile
Created July 3, 2014 20:38
UWSGI Docker
# DOCKER-VERSION 0.6.1
# Borrowed from https://github.com/vvlad/dockerfiles-postgresql-9.3
#
# First run:
# $ docker build -t uwsgi -rm=true .
# $ ID=$(docker run -v "path/to/src:/app/src" -v "path/to/socks:/app/socks" -d uwsgi)
# $ docker wait $ID
# $ docker logs $ID
FROM ubuntu:12.04
#!/bin/sh
NAME="$(basename $0)"
ARCH="$(uname -sm)"
tail -n +6 $0 | tar -s "/$ARCH/$NAME/" -zx "$ARCH"
exec $0 $@
exit
(... tarball ...)
@shazow
shazow / gist:6dd544b9b17e69f4cd77
Last active August 29, 2015 14:16
Go pattern? Search buffered chan
// This is getting out of hand, probably a bad idea.
func (c *Client) searchBuffer(command string) (message *irc.Message, err error) {
buffer := make(chan *irc.Message, cap(c.buffer))
Fill:
for {
select {
case message = <-c.buffer:
if message.Command == command {
@shazow
shazow / gist:240045
Created November 21, 2009 07:41
Ported John Resig's Javascript pretty_date function to Python.
from datetime import datetime
import time
import math
def pretty_date(t):
"""
Take a datetime object or epoch time int, return a string representing
how long ago the date was.
Inspired by: http://ejohn.org/files/pretty.js
@shazow
shazow / gist:250137
Created December 6, 2009 08:59
Clever dumb object for faux proxy purposes.
class DumbObject(object):
def __init__(self, len=1):
self.len = len
def __call__(self, *args, **kw):
return self
def __getattr__(self, name):
return self