Skip to content

Instantly share code, notes, and snippets.

View rfong's full-sized avatar

rfong rfong

View GitHub Profile
@rfong
rfong / cprint.py
Created October 29, 2015 10:04
colorfulprint
from cStringIO import StringIO
output = StringIO()
class bcolors:
HEADER = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
@rfong
rfong / django_mixins.py
Last active February 22, 2017 19:27
useful django queryset mixins. import in settings to use.
from django.db import models
from django.db.models.query import QuerySet
class QuerySetExplainMixin:
"""
Dump SQL EXPLAIN for a Queryset.
Usage: `queryset.explain()`
"""
def explain(self, analyze=False, verbose=False):
/* Quick new angular app
* Accompany in template with:
<html ng-app="AppName"
ng-controller="AppCtrl">
*/
var app = angular.module('AppName', []);
app.config(['$interpolateProvider', '$httpProvider',
function($interpolateProvider, $httpProvider) {
// Change angular templating syntax from {{var}} to {[var]} so as not to
@rfong
rfong / django_retry_decorator.py
Last active September 6, 2019 09:00
django retry decorator for tests
import unittest
from django.utils.functional import wraps
def retry(tries=1, ignore_exception=None):
"""
Decorator to retry a test.
:param ignore_exception: Exception class to catch.
:param tries: number of times to try running the wrapped function.
Don't use if you need to do something with the return value.
@rfong
rfong / angular-util.js
Last active December 3, 2019 23:42
Globally available utility library in a JS namespace, in case you aren't allowed to use lodash for some reason
// Globally available utility library.
(function (window, document) {
'use strict';
// attach as a property of window
var util = window.util || (window.util = {});
function uniqify(list) {
var uniqified = {};
@rfong
rfong / video_edge_detection.py
Last active October 14, 2021 21:15
Simple live OpenCV transformation on image frames from webcam capture that renders edges as white on a black background.
import numpy as np
import cv2
def np_rotate_array(arr, shift):
"""Shift a numpy array to the right so that it wraps around."""
return np.concatenate([arr[shift:], arr[:shift]])
def main():
@rfong
rfong / slinky_video.py
Last active February 3, 2017 19:27
Turn yourself into a slinky by time-delaying successive chunks of a webcam capture with OpenCV! https://vimeo.com/183155228
import copy
import datetime
import numpy as np
from optparse import OptionParser
import time
import cv2
from matplotlib import pyplot as plt
from enum import Enum
from django.db import models
class Blips(Enum):
BLOOP = 1
BLIP_CHOICES = (
(SomeEnum.BLOOP, 'Bloop'),
)
#!/bin/bash
# remove exited containers:
docker ps --filter status=dead --filter status=exited -aq | xargs -r docker rm -v
# remove unused images:
docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi
# remove unused volumes:
find '/var/lib/docker/volumes/' -mindepth 1 -maxdepth 1 -type d | grep -vFf <(
@rfong
rfong / add-ssh-user.sh
Last active May 9, 2017 01:20
make an ssh user on an ubuntu mashine
sudo adduser $USER
#sudo usermod -aG sudo $USER # Give user sudo?
su - $USER
mkdir ~/.ssh
chmod 0700 ~/.ssh
touch ~/.ssh/authorized_keys
chmod 0600 ~/.ssh/authorized_keys
vim ~/.ssh/authorized_keys
chown -R $USER:$USER ~/.ssh