Skip to content

Instantly share code, notes, and snippets.

View andrewgodwin's full-sized avatar

Andrew Godwin andrewgodwin

View GitHub Profile
@andrewgodwin
andrewgodwin / test.py
Last active August 29, 2015 14:01
Function Membership Introspection
class MyClass(object):
def a(self):
pass
b = [a]
# Get the reference to a from the list
a = MyClass.b[0]
# How do I get the class a was defined on? Is it even possible?
# On Python 3, I can use __qualname__. This isn't around on Python 2.
from __future__ import unicode_literals
from django.core import validators
from django.db import models, migrations
from django.utils import timezone
class Migration(migrations.Migration):
dependencies = [
class X:
@staticmethod
def y(self):
return 3
z = [y]
>>> X.y
<function X.y at 0x7fc5e5ca90d0>
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
]
from collections import deque
def dependency_sort(initial, dependency_func):
"""
Generic dependency sorting algorithm; supply an initial list of IDs as
"initial", and a callable that takes an ID and returns a list of IDs as
"dependencies".
The resultant list is in order from leaf nodes (no dependencies) to stems.
"""

Small bag / wearing

  • Passport (+I-797B)
  • Hoodie
  • Bluetooth headphones
  • Surface
  • 3DS/Vita (+games for 3DS)
  • Nexus 9
  • Kindle
@andrewgodwin
andrewgodwin / profile-this.py
Created August 11, 2015 20:58
Handy profiler pattern
import cProfile, pstats, StringIO
pr = cProfile.Profile()
pr.enable()
# ... do something ...
pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
-- This presents an example of rendering templates in the database.
-- DO NOT DO THIS! It's a terrible idea, and just example for my talk
-- which you can see at https://speakerdeck.com/andrewgodwin/dubious-database-design
CREATE EXTENSION plpythonu;
-- Table to store template HTML by name
CREATE TABLE templates (
"name" text PRIMARY KEY,
"template" text
# Main Nginx router
www-router:
image: andrewgodwin/www-router
environment:
www.aeracode.org: aeracode-varnish
aeracode.org: redirect:www.aeracode.org
www.codingponies.com: codingponies
codingponies.com: redirect:www.codingponies.com
#!/usr/bin/python
import sys
import subprocess
import random
terms = [t.lower() for t in sys.argv[1:]]
if not terms:
print "No search terms supplied"
sys.exit(1)