Skip to content

Instantly share code, notes, and snippets.

View mitchellrj's full-sized avatar

Richard Mitchell mitchellrj

View GitHub Profile
#! /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
@mitchellrj
mitchellrj / flatten.py
Created October 12, 2011 14:44
Flatten in Python
def flatten(iterable):
if not isinstance(iterable, (list, tuple)):
yield iterable
else:
for i in iterable:
if isinstance(i, (list, tuple)):
for j in flatten(i):
yield j
else:
yield i
@mitchellrj
mitchellrj / tree.html
Created October 28, 2011 00:09
HTML & CSS vertical tree layout
<html>
<head>
<title>HTML &amp; CSS tree</title>
<!-- tree -->
<style type="text/css">
ul.tree {
overflow-x: auto;
white-space: nowrap;
}
@mitchellrj
mitchellrj / debug_login.py
Created November 15, 2011 14:59
Plone debug mode login
from AccessControl.SecurityManagement import newSecurityManager
from Testing.makerequest import makerequest
from zope.component.hooks import setSite
def get_manager(app):
for uid in app.acl_users.users.listUserIds():
user = app.acl_users.users.getUser(uid)
if 'Manager' in user.getRoles():
return user
@mitchellrj
mitchellrj / configure.zcml
Created November 25, 2011 14:58
z3c.form data manager for persistent dictionaries
<configure
xmlns="http://namespaces.zope.org/zope">
<adapter
factory=".datamanager.PersistentDictionaryField" />
</configure>