Skip to content

Instantly share code, notes, and snippets.

@mtigas
mtigas / 1-s3up-private.py
Created August 24, 2012 20:04
Uploader and "signed" url generator for semi-secure private/encrypted S3 uploads.
#!/usr/bin/env python
# coding=utf-8
#
# s3up-private.py
# (c)2012, Mike Tigas
# http://mike.tig.as/
#
# Uploads files into S3 with a "private" ACL and with "encrypt_key" enabled.
# Generates a time-limited URL that can access the given uploaded file.
#
<!--somewhere in the headers or in a site-wide JS file or right before you need a retina snippet -->
<script>var SR=SR||{};SR.RETINA=((window.devicePixelRatio !== undefined)&&(window.devicePixelRatio >= 2));</script>
@mtigas
mtigas / 01-renderers.py
Created June 30, 2012 07:25
django-medusa renderer.py example files (these are the ones that power my blog)
# mt3/renderers.py
from django_medusa.renderers import StaticSiteRenderer
class HomeRenderer(StaticSiteRenderer):
def get_paths(self):
return frozenset([
"/",
"/about/",
"/colophon/",
"/contact/",
@mtigas
mtigas / gist:2597650
Created May 4, 2012 20:54
"git log grepping" for the number of commits that contain certain words.
$ git log --grep="fuck" --format=oneline -i | wc -l
8
$ git log --grep="shit" --format=oneline -i | wc -l
3
$ git log --grep="derp" --format=oneline -i | wc -l
24
$ git log --grep=" ie " --format=oneline -i | wc -l
class TrackingCheck(object):
"""
Allows implementing the HTTP header-based "Do Not Track" mechanism described in
http://tools.ietf.org/html/draft-mayer-do-not-track-00
"""
def process_request(self, request):
# DNT: 1
optout = request.META.get("HTTP_DNT", False) or request.META.get("HTTP_X_DNT", "0")
if optout.strip() == "1":
setattr(request, "tracking_opt_out", True)
"""
forked from django.contrib.webdesign.lorem_ipsum
https://github.com/django/django/blob/master/django/contrib/webdesign/lorem_ipsum.py
import instagram
for p in instagram.paragraphs(6):
print p+"\n"
@mtigas
mtigas / genetic1.py
Created April 2, 2012 22:19
genetic algorithms are fun
#!/usr/bin/env pypy
"""
See https://news.ycombinator.com/item?id=3789904
Google Cache'd version of article: https://webcache.googleusercontent.com/search?q=cache:JBfk95WBF2kJ:www.generation5.org/content/2003/gahelloworld.asp+&cd=1&hl=en&ct=clnk&gl=us
Port of C++ code in that: https://webcache.googleusercontent.com/search?q=cache:MurkRHQVoO0J:www.generation5.org/content/2003/data/gahelloworld.cpp&hl=en&gl=us&strip=1
"""
import random
import string
@mtigas
mtigas / gist:2220161
Created March 27, 2012 20:55
Serving retina <img> tags in Django at the Spokesman-Review
<!--
Where {% jsmin %}{% endjsmin %} is a django template tag that
uses a Python port of Crockford's JSMin:
* http://www.crockford.com/javascript/jsmin.html
* https://gist.github.com/1b085c39b85347255557
Where {% mogrify %} is a custom template tag that generates a
URL with a resize command in the filename (semi-compatible with
ImageMagick mogrify) that is caught by a 404-handling script
running on the media server. A special hashed key is generated
@mtigas
mtigas / gist:1961114
Created March 2, 2012 20:36
lambda assisted elections magic
# GIVEN:
# `counties` is a dict with each key being the slug of a county,
# containing a sub-dict with keys including vote counts and stuff.
# each candidate's vote count is stored in a key that is the candidate's
# name as a slug
# find vote leader for each county and store it in a 'leader' key for that county
for county_slug in counties.iterkeys():
vals = [(candidate, counties[county_slug][candidate]) for candidate in CANDIDATES]
vals.sort(key=lambda c: c[1], reverse=True)
@mtigas
mtigas / readme.markdown
Created February 14, 2012 03:46
how to get your own fork of django and run the testcases so you can be a good citizen and help make django better or something

tl;dr how to hack on django and run the test suite

fork your own copy of django first, then change the git@github.com:mtigas/django.git bit to your repo's private URL.

cd ~/Code
virtualenv --no-site-packages django
cd django
echo "export PIP_RESPECT_VIRTUALENV=true" >> bin/activate
echo "export PYTHONPATH=\"\$VIRTUAL_ENV/test_src:\$VIRTUAL_ENV/repo\"" >> bin/activate

echo "export DJANGO_SETTINGS_MODULE="settings"" >> bin/activate