Skip to content

Instantly share code, notes, and snippets.

View mitchellrj's full-sized avatar

Richard Mitchell mitchellrj

View GitHub Profile
@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 / 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 / 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 / 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.
#! /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
#