Skip to content

Instantly share code, notes, and snippets.

#!/bin/bash
# Record work @ current time with message
if [ ! -n "$1" ]
then
echo "give a message!"
exit
fi
@minism
minism / color.py
Created January 10, 2012 18:39
python terminal colors
# Terminal color definitions
class fg:
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
@minism
minism / middleware.py
Created February 13, 2012 22:45
maintenance mode middleware
from django import http
from mainsite import settings
class MaintenanceMiddleware(object):
"""Serve a temporary redirect to a maintenance url in maintenance mode"""
def process_request(self, request):
if getattr(settings, 'MAINTENANCE_MODE', False) == True and hasattr(settings, 'MAINTENANCE_URL'):
return http.HttpResponseRedirect(settings.MAINTENANCE_URL)
return None
@minism
minism / gist:1928314
Created February 28, 2012 01:10
Git merge theirs
# in case branchA is not our current branch
git checkout branchA
# make merge commit but without conflicts!!
# the contents of 'ours' will be discarded later
git merge -s ours branchB
# make temporary branch to merged commit
git branch branchTEMP
@minism
minism / gist:1954294
Created March 2, 2012 00:35
modes.py
def rotate(seq, n):
return seq[n:] + seq[:n]
diatonic = [2, 2, 1, 2, 2, 2, 1]
ionian = rotate(diatonic, 0)
dorian = rotate(diatonic, 1)
phrygian = rotate(diatonic, 2)
lydian = rotate(diatonic, 3)
mixolydian = rotate(diatonic, 4)
@minism
minism / gist:2832403
Created May 30, 2012 01:39
Fix CKEditor not letting you paste something before typing
editor.on('paste', function(e) {
if (e.editor.getData() == "")
e.editor.setData(e.data.html)
})
@minism
minism / binary.lua
Created September 5, 2012 22:31
lua binary data packing
-- Fast functions for working with binary data
return {
decode_uint8 = function(str, ofs)
ofs = ofs or 0
return string.byte(str, ofs + 1)
end,
decode_uint16 = function(str, ofs)
ofs = ofs or 0
local a, b = string.byte(str, ofs + 1, ofs + 2)
local binary = require 'shared.binary'
-- Datagram format
--
-- uint32 seq Sequence number of the packet
-- uint32 ack Last acknowledged sequence number of remote channel
-- uint16 fragment_ofs Offset of fragment
-- uint16 fragment_len Length of fragment, zero if single message
-- uint8 cmd Command (see command.lua)
--- binary data Arbitrary data
class NestedLazyModelField(serializers.ModelSerializer):
"""
Use this class for defining a nested relationship based on a funciton
instead of an attribute.
For example, 'get_profile' method of user
"""
def field_to_native(self, obj, field_name):
obj = getattr(obj, self.source or field_name)
@minism
minism / resources.py
Created November 8, 2012 20:26
tastypie ModelResource that respects 'blank' attribute on Model fields
class BaseModelResource(ModelResource):
@classmethod
def get_fields(cls, fields=None, excludes=None):
"""
Unfortunately we must override this method because tastypie ignores 'blank' attribute
on model fields.
Here we invoke an insane workaround hack due to metaclass inheritance issues:
http://stackoverflow.com/questions/12757468/invoking-super-in-classmethod-called-from-metaclass-new
"""