Skip to content

Instantly share code, notes, and snippets.

View nottrobin's full-sized avatar

Robin Winslow nottrobin

View GitHub Profile
@nottrobin
nottrobin / check-django-headers.sh
Created February 18, 2016 10:30
What caching headers should look like when updated with Django
$ curl -I cn.ubuntu.com
...
Date: Fri, 12 Feb 2016 22:48:38 GMT
Expires: Fri, 12 Feb 2016 22:53:35 GMT
Last-Modified: Fri, 12 Feb 2016 22:48:35 GMT
Cache-Control: max-age=300
@nottrobin
nottrobin / django-templateview-add-headers.py
Created February 18, 2016 10:29
How to add headers in your custom TemplateView
class OurTemplateView(TemplateView):
def render_to_response(self, context, **response_kwargs):
# Get response from parent TemplateView class
response = super(CmsTemplateFinder, self).render_to_response(
context, **response_kwargs
)
# Add Cache-Control and Expires headers
patch_response_headers(response, cache_timeout=300)
@nottrobin
nottrobin / extending-templateview.py
Created February 18, 2016 10:28
Example of how to extend django.views.generic.base.TemplateView
from django.views.generic.base import TemplateView
class OurTemplateView(TemplateView):
# Setup our custom template data
@nottrobin
nottrobin / wsgi.py
Last active September 3, 2018 02:59
Simple WSGI application server
from wsgiref.simple_server import make_server
def application(env, start_response):
"""
A basic WSGI application
"""
http_status = '200 OK'
response_headers = [('Content-Type', 'text/html')]
@nottrobin
nottrobin / working-directory.py
Created January 26, 2016 01:42
Change directory / working directory context processor for python
import os
from contextlib import contextmanager
@contextmanager
def working_directory(path):
"""
A context manager which changes the working directory to the given
path, and then changes it back to its previous value on exit.
Usage:
@nottrobin
nottrobin / grey-invert.sh
Last active January 23, 2016 14:25
An imagemagick script to convert a PDF to a greyscaled & inverted version (for a unix-based system)
#!/usr/bin/env bash
##
# Usage:
# ./grey-invert.sh my-doc.pdf
#
# This will:
# - Convert to images and store in a /tmp directory
# - Greyscale images
# - Invert images
@nottrobin
nottrobin / juju-reset.py
Created November 25, 2015 23:20
Destroy all machines and services in a juju environment, without destroying the environment
#!/usr/bin/env python
import yaml
import subprocess
status_command = "juju status"
status_output = subprocess.check_output(status_command.split())
status = yaml.load(status_output)
@nottrobin
nottrobin / hypothesis-test-example.py
Created October 7, 2015 11:54
An example of how Hypothesis can generate test cases
from hypothesis.strategies import lists, floats
@given(lists(floats()))
def test_average(float_list):
ave = reduce(lambda x, y: x + y, float_list) / len(float_list)
assert average(float_list) == ave
@nottrobin
nottrobin / kind-test.py
Created October 7, 2015 11:54
An example of how we choose really easy text cases
def test_average():
assert my_average([2, 4]) == 3
@nottrobin
nottrobin / find-duplicate-filenames.sh
Created October 7, 2015 00:27
Recursively find duplicate filenames in a directory tree
#!/bin/sh
dirname=Pictures
find $dirname -type f | sed 's_.*/__' | sort| uniq -d|
while read fileName
do
find $dirname -type f | grep "$fileName"
done