Skip to content

Instantly share code, notes, and snippets.

@rhblind
rhblind / common-utils.py
Created February 7, 2018 09:06
Common utility functions to keep around
def flatten(sequence):
"""
Flatten an arbitrary nested sequence.
Example usage:
>> my_list = list(flatten(nested_lists))
:param sequence: A nested list or tuple.
:returns: A generator with all values in a flat structure.
"""
for i in sequence:
if isinstance(i, Sequence) and not isinstance(i, (str, bytes)):
#!/usr/bin/env bash
EXITED=$(docker ps -q -f status=exited)
DANGLING=$(docker images -q -f "dangling=true")
if [ "$1" == "--dry-run" ]
then
echo "==> Would stop containers:"
echo $EXITED
echo "==> And images:"
echo $DANGLING
@rhblind
rhblind / .vimrc
Last active August 29, 2015 14:20
My .vimrc file
" Global settings
set hidden
set nocompatible
filetype off
" Vundle settings
" See https://github.com/gmarik/Vundle.vim for info.
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'gmarik/Vundle.vim' " let Vundle manage Vundle
@rhblind
rhblind / middleware.py
Last active August 29, 2015 14:14
Django CORS Header setup
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.core.exceptions import ImproperlyConfigured
class SetRemoteAddrFromForwardedFor(object):
"""
Middleware that sets REMOTE_ADDR based on HTTP_X_FORWARDED_FOR, if the
latter is set. This is useful if you're sitting behind a reverse proxy that
@rhblind
rhblind / manage.py
Last active September 17, 2016 17:09
Django manage.py and runserver command for use with gevent-socketio
#!/usr/bin/env python
import os
import sys
from gevent import monkey
from psycogreen.gevent import patch_psycopg
# The threading module raises a KeyError if already loaded
# when monkey patching it.
if "threading" in sys.modules:
@rhblind
rhblind / .screenrc
Created March 24, 2013 21:47
My .screenrc
# some default settings
startup_message off
vbell off
msgwait 1
defutf8 on
compacthist on
# Monitor windows
defmonitor on
activity ""
@rhblind
rhblind / paginate.py
Last active December 13, 2015 23:59
Django pagination template tag.
# -*- coding: utf-8 -*-
from django import template
from django.utils.encoding import smart_unicode
register = template.Library()
class PaginateNode(template.Node):
@rhblind
rhblind / .bashrc
Last active December 13, 2015 22:29
My .bashrc file
# ~/.bashrc: executed by bash(1) for non-login shells
# if not running interactively, dont't do anything
[ -z "$PS1" ] && return
# timezone info
export TZ="Europe/Oslo"
# don't put duplicate lines in the history. See bash(1) for more options
# don't overwrite GNU Midnight Commander's setting of `ignorespace'.
@rhblind
rhblind / pg_killcon.sh
Last active September 29, 2021 03:20
Bash script to kill active connections on a PostgreSQL database
#!/bin/bash
#
# A simple script to kill all connections to
# a postgresql database.
#
function usage() {
cat << EOF
usage: $0 options