Skip to content

Instantly share code, notes, and snippets.

View jdunck's full-sized avatar

Jeremy Dunck jdunck

View GitHub Profile
@jdunck
jdunck / Find old-style (pre django 1.5) {% url %} tags
Last active August 29, 2015 14:10
Find old-style (pre django 1.5) {% url %} tags
Find: "\{\%\W*url +([-_a-zA-Z:0-9]+)\W"
Replace: "{% url '\1' "
(Don't forget you'll also need
{% load url from future %} if on 1.5.
----
If you'd like to whittle the tree down to just files that could possibly have old-style urls:
Find files in tree that have the {% url tag in them at all:

Keybase proof

I hereby claim:

  • I am jdunck on github.
  • I am jdunck (https://keybase.io/jdunck) on keybase.
  • I have a public key whose fingerprint is 8801 066B D85C 57BA 0D02 8BF9 A4EA DF1F 0080 A663

To claim this, I am signing this object:

@jdunck
jdunck / static_fallback_open.py
Last active August 13, 2019 14:17
Get the contents of a django static file, regardless of whether it has been collected
import posixpath
from django.contrib.staticfiles import finders
from django.contrib.staticfiles.storage import staticfiles_storage
def static_fallback_open(static_path, mode='r'):
writing = 'w' in mode or 'a' in mode
if writing or staticfiles_storage.exists(static_path):
return staticfiles_storage.open(static_path, mode)
@jdunck
jdunck / README
Created April 12, 2015 21:44
Migrating to django 1.7 migrations
Scenario:
You have a long-running branch to upgrade your existing codebase to django 1.7.
You've previously used south, and your existing deployment includes schema changes since the
base of the django-1.7 upgrade branch.
You need to re-create django (core) migrations periodically since you can't merge
south migrations into core migrations.
(Assumes commands are run from root of project and that apps are top-level directories Adjust pathing if that is not the case.)
@jdunck
jdunck / import_cleanup.py
Created April 23, 2015 17:44
List all stdlib packages
def get_stdlib_names():
import distutils.sysconfig as sysconfig
import os
import sys
std_lib = sysconfig.get_python_lib(standard_lib=True)
pathed_dirs = sorted(sys.path[:], key=lambda s: len(s), reverse=True)
prefixes = filter(lambda path: path.startswith(std_lib), pathed_dirs)
for top, dirs, files in os.walk(std_lib):
@jdunck
jdunck / fetch-rails-changelog.sh
Created July 21, 2015 22:44
Fetch all of rails' changelogs
for COMPONENT in actionmailer actionpack actionview activemodel activerecord activesupport; do
for VERSION in master 4-2-stable 4-1-stable 4-0-stable 3-2-stable 3-1-stable 3-0-stable; do
if [[ "$VERSION" == "3-0-stable" ]]; then
FN='CHANGELOG'
else
FN='CHANGELOG.md'
fi
curl "https://raw.githubusercontent.com/rails/rails/${VERSION}/${COMPONENT}/${FN}" > ${COMPONENT}-${VERSION}-CHANGELOG
done
@jdunck
jdunck / flatten_hash.rb
Created August 5, 2015 03:59
Flatten a Hash, preserving key namespacing
def flattened_hash(h, namespace = '', memo = {})
h.reduce(memo) { |memo, (key, value)|
if value.instance_of?(Hash)
memo.merge!(flattened_hash(value, "#{namespace}#{key}_", memo))
else
memo["#{namespace}#{key}"] = value
end
memo
}
end
@jdunck
jdunck / choices.py
Last active November 4, 2015 21:48
Better Choices for django
from collections import OrderedDict
class Choices(object):
"""
Just like raw tuple-choices you're used to passing to django's
choices kwarg, but better.
:param value_label_pairs: tuple-of-pairs, like raw choices= parameters.
:param attrs: optional attribute names to use instead of label.upper()
@jdunck
jdunck / README.md
Last active August 17, 2018 20:29
Python preamble ordering

It's been hard for me to remember what the required relative order of python preambles (that is, encoding markers, future imports, and module docstrings) should be.

This is an empirical approach, showing that the proper order is:

# -*- coding: utf8 -*-

"""døcst®îñg"""

from __future__ import unicode_literals
#!/bin/bash
if [[ $# != 1 ]]; then
echo usage: backup /path/to/backup/dest
echo "( src is assumed / )"
exit
fi
root_dir=$1
dest=${root_dir}/${HOSTNAME}
if [[ -e /proc ]] ; then