Skip to content

Instantly share code, notes, and snippets.

View wil's full-sized avatar

Wil Tan wil

View GitHub Profile
@wil
wil / summarize.py
Last active August 29, 2015 14:14 — forked from rodricios/summarize.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
pip install networkx distance pattern
In Flipboard's article[1], they kindly divulge their interpretation
of the summarization technique called LexRank[2].
@wil
wil / link_header.py
Last active December 20, 2015 20:29 — forked from mnot/link_header.py
HTTP Link header parsing routines originally adapted from [Mark Nottingham's gist](https://gist.github.com/mnot/210535) This version adds a `parse_link_value_by_rel` convenience function to address a common use case.
@wil
wil / libresolv_query.c
Created August 2, 2013 16:27
Demonstrates how to send a DNS query and parse response in C using libresolv.
/*
* Copyright (c) 2013 by Wil Tan <[email protected]>
*
* Based on dump_dns.c from the dnscap <https://www.dns-oarc.net/tools/dnscap>
* originally written by Paul Vixie.
*
* Copyright (c) 2007 by Internet Systems Consortium, Inc. ("ISC")
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@wil
wil / install-root-key.sh
Created July 10, 2013 14:17
downloads the public key from the "cloud" environment
#!/bin/sh
#
# install-root-key.sh
#
# This script downloads the public key from the "cloud" environment.
if [ ! -d /root/.ssh ]; then
mkdir -p /root/.ssh
chmod 700 /root/.ssh
fi
VERSION="0.9.9"
BUILD="betable1"
set -e -x
# Keep track of the original working directory.
OLDESTPWD="$PWD"
# Work in a temporary directory.
cd "$(mktemp -d)"
@wil
wil / convert_cache_setting.py
Created October 8, 2012 05:00
Django 1.3 CACHE_BACKEND settings backward compatibility
# This allows you to maintain your Django cache settings in Django 1.3 style i.e. settings.CACHE
# and convert it to the old settings.CACHE_BACKEND url so that you can run it under Django 1.2 or earlier
from django.core.exceptions import ImproperlyConfigured
CACHE_BACKEND_MAP = {
'django.core.cache.backends.db.DatabaseCache': 'db',
'django.core.cache.backends.dummy.DummyCache': 'dummy',
@wil
wil / fake_django_request
Created January 3, 2012 11:45
Fake django request useful for calling a view function
from django.http import QueryDict
class FakeRequest(object):
pass
request = FakeRequest()
request.GET = QueryDict('a=1&b=2')
request.META = {}
@wil
wil / django_sql_debug.py
Created January 3, 2012 10:51
Django Production SQL Debugging in the manage.py shell
# These are some commands to copy and paste into a manage.py shell session
# in order to find out what SQL queries are generated by a view
## turn on DEBUG so that ``connection.queries`` gets populated
from django.conf import settings
settings.DEBUG = True
from django.db import connection
## handy function for printing the queries
@wil
wil / find_forelse.py
Created October 24, 2011 11:49
Look for the for/else construct in python code
#!/usr/bin/env python
# Parses a .py file and look for for-else constructs
# Usage: find_forelse.py some_python_file.py
import sys
import ast
def check_file(filename):
with open(filename, "r") as f:
@wil
wil / gist:347596
Created March 29, 2010 08:25
Django Setting Expires Header in XHR Response
import time
from django.utils.http import http_date
AJAX_NEGATIVE_CHECK_EXPIRES = 60 # object is still available
AJAX_POSITIVE_CHECK_EXPIRES = 60*10 # if object is not available (or taken)
def check_ajax(request):
# do stuff here
timeout = AJAX_NEGATIVE_CHECK_EXPIRES if avail else AJAX_POSITIVE_CHECK_EXPIRES