Skip to content

Instantly share code, notes, and snippets.

View mitchellrj's full-sized avatar

Richard Mitchell mitchellrj

View GitHub Profile
@mitchellrj
mitchellrj / keybase.md
Created July 8, 2014 11:14
keybase.md

Keybase proof

I hereby claim:

  • I am mitchellrj on github.
  • I am mitchell (https://keybase.io/mitchell) on keybase.
  • I have a public key whose fingerprint is 7F06 1B87 5590 ED92 5FE4 DCD3 27AA 9799 D267 327E

To claim this, I am signing this object:

@mitchellrj
mitchellrj / gist:12acfd703bc75beb0236
Last active August 29, 2015 14:04
Python multiple inheritance and super
>>> class MetaMeta(object):
... def __init__(self):
... print ('metameta')
...
>>> class Meta(MetaMeta):
... def __init__(self):
... print ('meta1')
...
>>> class Meta2(MetaMeta):
... def __init__(self):
@mitchellrj
mitchellrj / base.py
Created July 25, 2014 08:21
Tastypie - make fields read only for object update but not for creation
from tastypie.resources import ModelResource as BaseModelResource
class ReadOnlyFieldsAfterAddMeta(BaseModelResource.__metaclass__):
def __new__(cls, name, bases, attrs):
new_class = super(ReadOnlyFieldsAfterAddMeta, cls).__new__(cls, name, bases, attrs)
read_only_fields_after_add = getattr(new_class._meta, 'read_only_after_add', ())
for field_name in read_only_fields_after_add:
@mitchellrj
mitchellrj / watch-and-revert.sh
Last active August 29, 2015 14:07
Script to watch a file for modifications and revert it immediately
#!/bin/bash
#
# Usage: watch-and-revert.sh filename
#
BACKUP="$(dirname $1)/.backup-$(basename $1)"
function cleanup {
rm $BACKUP
}
trap cleanup EXIT
cat $1 > $BACKUP
#! /usr/bin/env python
#
# Copyright 2011 Richard Mitchell
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
@mitchellrj
mitchellrj / constrained_insert.py
Created July 26, 2011 16:41
Constrained list insertion in Python.
def constrained_insert(list_, item, before=None, after=None, strict=True):
""" Given a list and an item to be inserted into that list, attempts
to insert the item into the list in such a way that it is before
any items listed in the 'before' argument and after any of those
listed in the 'after' argument. If this is not possible, raises
a RuntimeError.
If any of the values in before or after do not appear in the
list, ValueError will be raised, unless 'strict' is set to
False.
@mitchellrj
mitchellrj / update_wordpress.sh
Created August 10, 2011 00:10
Automated Wordpress updates from the command line
#!/bin/bash
wp_dir=$1
backup_dir=$2
latest_version=`echo -e "HEAD /latest.tar.gz HTTP/1.1\nHost: wordpress.org\n\n" | \
nc -i 1 wordpress.org 80 | \
grep 'Content-Disposition:' | \
sed 's:.*filename=[A-Za-z-]*\([0-9\.]*\)\.tar\.gz.*:\1:'`
current_version=`grep '$wp_version = ' ${wp_dir}/wp-includes/version.php | \
sed "s:.*['\"]\([0-9\.]*\)['\"].*:\1:"`
@mitchellrj
mitchellrj / workflow_tools.py
Created August 17, 2011 15:00
Set workflow state or retrieve a list of transitions required to get from one state to another
#@memoize
def get_wf_transitions_for(workflow, from_state, to_state):
exit_state_maps = {}
for state in workflow.states.objectValues():
for t in state.getTransitions():
exit_state_maps.setdefault(t, [])
exit_state_maps[t].append(state.getId())
transition_maps = {}
for transition in workflow.transitions.objectValues():
@mitchellrj
mitchellrj / difftools.py
Created August 25, 2011 10:04
Dictionary & list difference. Two-way and three-way.
from copy import deepcopy
def two_way_list_diff(left, right):
""" Compares two lists, returns a tuple of two lists detailing
which items have been added to the second list and removed
from the first list.
Example
-------
@mitchellrj
mitchellrj / wrap.py
Created September 26, 2011 14:15
Wrap text in Python
def wrap_text(text, limit):
#s = map(str.split, text.split('\n'))
#i = 0
#for n in range(len(s)):
# while i < len(s[n]) - 1:
# if (len(s[n][i]) + len(s[n][i+1])) <= limit:
# s[n][i] += ' ' + s[n][i+1]
# s[n] = s[n][:i+1] + s[n][i+2:]
# elif len(s[n][i+1]) > limit:
# i += 1