Skip to content

Instantly share code, notes, and snippets.

View stephenemslie's full-sized avatar

Stephen stephenemslie

  • Freelance
  • London
View GitHub Profile

Keybase proof

I hereby claim:

  • I am stephenemslie on github.
  • I am stephenemslie (https://keybase.io/stephenemslie) on keybase.
  • I have a public key ASBlYoClNgkGUA0RW4_B3_KDyv84UKNYp57qkH7-BZzDDgo

To claim this, I am signing this object:

@stephenemslie
stephenemslie / alembic_fk_deferrable.py
Created November 13, 2014 15:26
e.g. DBURI=postgres://user:pass@host:port/name python alembic_fk_deferrable.py
import os
import sqlalchemy as sa
engine = sa.create_engine(os.environ['DBURI'])
meta = sa.MetaData(bind=engine, reflect=True)
tmpl = """
op.drop_constraint('{name}', '{source}')
op.create_foreign_key('{name}', '{source}', '{referent}', ['{local_cols}'], ['{remote_cols}'], deferrable=True)
"""
@stephenemslie
stephenemslie / boot2docker-dns.js
Last active August 29, 2015 14:06
A little dns server to resolve `localdocker` to the ip address of your boot2docker VM.
// This needs to listen on port 53, so run with sudo node boot2docker-dns.js
var exec = require('child_process').exec;
var jack = require('dnsjack').createServer();
jack.route('localdocker', function(domain, callback){
exec('sudo -u $SUDO_USER boot2docker ip', function(err, stdout, stderr){
callback(null, stdout);
});
});
jack.listen();
import serial
import socket
import argparse
import sys
NEWLINE = "\r\n"
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument("com_port")
@stephenemslie
stephenemslie / admin.py
Last active December 16, 2015 08:19
Export to csv action for users in the Django admin.
import csv
from django.contrib import admin
from django.http import HttpResponse
class UserAdmin(ModelAdmin):
actions = ["export_as_csv"]
def export_as_csv(self, request, queryset):
response = HttpResponse(content_type='text/csv')
@stephenemslie
stephenemslie / resolvermatch.py
Created February 3, 2011 12:35
Very simple but useful Django context processor to generate a ResolverMatch object from request.path and add this to the context. Handy in checking for active urls or namespaces.
"""
resolved_pattern Django context processor: insert the current url's ResolverMatch object into context.
Example use: Set class=active to a menu item, based on the namespace of the currently matched url.
<li {% if resolved.namespace == "home" %}class="active"{% endif %}> home </li>
or more specifically:
<li {% if resolved.view_name == "contact" %}class="active"{% endif %}> contact </li>