Skip to content

Instantly share code, notes, and snippets.

View jdunck's full-sized avatar

Jeremy Dunck jdunck

View GitHub Profile
@jdunck
jdunck / example_usage.py
Last active December 2, 2015 00:05
Coerce writes to bytes, while avoiding str.encode's common UnicodeDecodeError
import sys, unicode_io
sys.stdout = unicode_io.IOWrapper(sys.stdout)
@jdunck
jdunck / chunked_bulk_create.py
Last active January 17, 2017 23:23
Chunked bulk_create under mysql
import math
from itertools import izip_longest
from django.db import connections
def get_cursor(alias='default'):
"""
Get a raw cursor out of django's DB connection
"""
wrapped_conn = connections[alias]
#!/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
@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
@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 / 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 / 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 / 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 / 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 / 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)