Skip to content

Instantly share code, notes, and snippets.

View anderser's full-sized avatar

Anders Eriksen anderser

View GitHub Profile
# Depends on the OS X "say" command
import time, datetime, subprocess, math, sys
def say(s):
subprocess.call(['say', str(s)])
def seconds_until(dt):
return time.mktime(dt.timetuple()) - time.time()
"Memcached cache backend"
from django.core.cache.backends import memcached
from django.utils.encoding import smart_unicode, smart_str
MIN_COMPRESS_LEN = 150000
class CacheClass(memcached.CacheClass):
def add(self, key, value, timeout=None, min_compress_len=MIN_COMPRESS_LEN):
if isinstance(value, unicode):
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<style type="text/css">
body{font:12px/1.231 arial,helvetica,clean,sans-serif;}
</style>
@anderser
anderser / grabber.py
Created July 3, 2011 17:52
Simple python script for use in Django to grab webpage to PDF using wkhtmltodf
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import subprocess
import logging
from django.conf import settings
logger = logging.getLogger('pagetracker.grabber')
def grab_page(url, outfile):
@anderser
anderser / fusiontimeline.html
Created August 12, 2011 20:50
Use Google Fusion Tables to power ProPublica's Timelinesetter timelines
<!DOCTYPE html>
<html>
<head>
<link href="http://propublica.github.com/timeline-setter/public/stylesheets/timeline-setter.css" rel="stylesheet" />
<script src="http://propublica.github.com/timeline-setter/public/javascripts/vendor/jquery-min.js"></script>
<script src="http://propublica.github.com/timeline-setter/public/javascripts/vendor/underscore-min.js"></script>
<script src="http://propublica.github.com/timeline-setter/public/javascripts/timeline-setter.js"></script>
<script src="../js/date.js"></script>
<style>
#timeline_setter .TS-item_label a {
@anderser
anderser / sheet.py
Created October 10, 2011 21:02
Merge multiple excel sheets in one workbook into single sheet workbook
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xlrd
import xlwt
from optparse import OptionParser
import datetime
"""
Merges excel files with multiple sheets with identical header lines into
@anderser
anderser / urls.py
Created January 24, 2012 10:28
Using cache decorator in urls.py to keep the app DRY
from django.conf.urls.defaults import *
from timelineit.views import *
from django.views.decorators.cache import cache_control,never_cache
urlpatterns = patterns('timelineit.views',
url(r'^desk/(?P<slug>[-\w\d]+)',
never_cache(timeline_preview),
name="timeline_preview"),
@anderser
anderser / gettag.py
Created January 26, 2012 12:01
Simple python method to retrieve title tag of external page/url - or other tags..
#!/usr/bin/python
# -*- coding: utf-8 -*-
import re
import requests
def get_page_tag(url, title_re=re.compile(r'<title>(.*?)</title>', re.UNICODE )):
"""
Retrieves the title tag from a given url (or actually any tag if you want..)
@anderser
anderser / models.py
Created March 23, 2012 17:15
Save image locally from submitted url in field image_url in Django model
from django.core.files.temp import NamedTemporaryFile
def save_image_from_url(self):
"""
Save remote images from url to image field.
Requires python-requests
"""
r = requests.get(self.image_url)
if r.status_code == 200:
@anderser
anderser / api.py
Created March 26, 2012 14:31
Cached tastypie model resource
class CachedModelResource(ModelResource):
def create_response(self, *args, **kwargs):
resp = super(CachedModelResource, self).create_response(*args, **kwargs)
resp['Cache-Control'] = "max-age=600"
return resp