Skip to content

Instantly share code, notes, and snippets.

@yaniv-aknin
yaniv-aknin / omnidecorator.py
Last active December 12, 2015 04:38
When you read something like this, you go: "yuck!". When you write it, you're totally "dude, I'm so amazing!!1!".
def parse_with(parser):
def decor(func):
def __get__(self, instance, owner=None):
return self.__class__(instance, owner)
def __call__(self, *args, **kwargs):
params = parser.parse_args()
if self.instance:
return func(self.instance, params, *args, **kwargs)
return func(params, *args, **kwargs)
def __init__(self, instance=None, owner=None):
@yaniv-aknin
yaniv-aknin / gist:4701096
Last active December 12, 2015 02:48
Can you think of a better console text colouring library for shell?
#!sh
function newline() { printf "$@\n"; }
function color() {
output="\e[$1m"
shift
[ -n "$1" ] && output="$output$@\e[00m"
printf "$output"
}
function reset() { color "00"; }
@yaniv-aknin
yaniv-aknin / gist:3957499
Created October 26, 2012 07:49
Check what a process is blocking on using the CLI on Linux

Suppose I have blocking process blocking_process:

import os

os.system('rm -fr /tmp/pipe; mkfifo /tmp/pipe')
print len(open('/tmp/pipe').read())

On Linux, I might check what is it blocking on by doing:

$ strace -p $(pgrep -f blocking_process)

@yaniv-aknin
yaniv-aknin / gist:3720047
Created September 14, 2012 05:50
demonstration of how embedding IPython (pulling in SQLite) can trigger unwanted side effects atexit
$ pip freeze
ipython==0.13
readline==6.2.2
wsgiref==0.1.2
$ cat test.py
from IPython import embed ; embed()
import thread ; thread.start_new_thread(lambda: 5/0, ())
$ python test.py
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
Type "copyright", "credits" or "license" for more information.
@yaniv-aknin
yaniv-aknin / gist:3089763
Created July 11, 2012 11:26
Mixpanel client library with separate communications and URL creation code
#! /usr/bin/env python
#
# Mixpanel, Inc. -- http://mixpanel.com/
#
# Python API client library to consume mixpanel.com analytics data.
import hashlib
import urllib
import time
try:
@yaniv-aknin
yaniv-aknin / gist:3075120
Created July 9, 2012 08:35
odd coffeescript 1.3.3 behaviour with superflous indentation on first line
$ cat > ok.coffee
1
2
$ cat > bad.coffee
1
2
$ coffee -cs < ok.coffee
// Generated by CoffeeScript 1.3.3
(function() {
1;
@yaniv-aknin
yaniv-aknin / gist:3047862
Created July 4, 2012 15:19
Dirtys: shell function for a quick-and-dirty Redis instance
dirtys () {
if ! which redis-server > /dev/null
then
echo no redis, no dirtys
return 1
fi
redis-server - <<EOF
timeout 0
logfile stdout
databases 1
@yaniv-aknin
yaniv-aknin / requirements.txt
Created April 23, 2012 17:31
s3lint: a simple lint tool to update S3 content-type and cache-control headers
boto==2.3.0
gevent==0.13.7
greenlet==0.3.4
wsgiref==0.1.2
% heroku apps:create -s cedar restpaste
Creating restpaste... done, stack is cedar
http://restpaste.herokuapp.com/ | git@heroku.com:restpaste.git
Git remote heroku added
% heroku addons:remove shared-database:5mb
! WARNING: Potentially Destructive Action
! This command will affect the app: restpaste
! To proceed, type "restpaste" or re-run this command with --confirm restpaste
@yaniv-aknin
yaniv-aknin / heroku-dupdb.sh
Created April 14, 2012 18:18
heroku-dupdb: small utility to clone a database between two apps, useful for copying production into staging
#!/usr/bin/env bash
cprint () {
printf "%b" "\e[1;37m${1}\e[0m\n"
}
usage() {
cat << EOF
usage: $0 [OPTIONS] <src> <dst>
--confirm <dst> dont ask for confirmation before destroying and restoring into <dst>