Skip to content

Instantly share code, notes, and snippets.

View bkeating's full-sized avatar
🤙
Gettin’ SUM & CALCin’ out

Benjamin Keating bkeating

🤙
Gettin’ SUM & CALCin’ out
View GitHub Profile
@mkhl
mkhl / py-appscript.py
Created November 24, 2008 08:34
Appscript ⁄ ScriptingBridge
#!/usr/bin/env python
# A tiny appscript example in Python.
import appscript
appscript.app("iTunes").current_track.artist.get()
@lincolnloop
lincolnloop / django.wsgi
Created March 13, 2009 03:22
Django mod_wsgi scripts
import os, sys
import site
# put virtualenv on pythonpath
site.addsitedir('/path/to/project/ve/lib/python2.5/site-packages')
# redirect prints to apache log
sys.stdout = sys.stderr
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
window.onload = function () {
if (! document.querySelectorAll) { // Quick & Dirty way to tell the good browsers from the bad ones.
alert("Please upgrade your browser to view this site.");
}
};
@m0n5t3r
m0n5t3r / gunicorn-upstart.conf.template
Created July 30, 2010 12:27
template for a gunicorn upstart job that can run several instances of a django application
# %(mysite)s - run %(mysite)s instances (default is the main production instance)
#
# This runs gunicorn-django for %(mysite)s; to install:
# * sudo ln -s <this file> /etc/init/%(mysite)s
# * sudo initctl reload-configuration
#
# it expects the following directory layout:
#
# /home/%(mysite)s/public_html
# \-env -> virtualenv
class QuerySetDoubleIteration(Exception):
"A QuerySet was iterated over twice, you probably want to list() it."
pass
# "Skinny" here means we use iterator by default, rather than
# ballooning in memory.
class SkinnyManager(Manager):
def get_query_set(self):
return SkinnyQuerySet(self.model, using=self._db)
def attach_foreignkey(objects, field, select_related=None):
"""
Shortcut method which handles a pythonic LEFT OUTER JOIN.
``attach_foreignkey(posts, Post.thread)``
"""
field = field.field
qs = field.rel.to.objects.filter(pk__in=distinct(getattr(o, field.column) for o in objects))
if select_related:
qs = qs.select_related(*select_related)
@isaacs
isaacs / node-and-npm-in-30-seconds.sh
Last active March 8, 2024 02:11
Use one of these techniques to install node and npm without having to sudo. Discussed in more detail at http://joyeur.com/2010/12/10/installing-node-and-npm/ Note: npm >=0.3 is *safer* when using sudo.
echo 'export PATH=$HOME/local/bin:$PATH' >> ~/.bashrc
. ~/.bashrc
mkdir ~/local
mkdir ~/node-latest-install
cd ~/node-latest-install
curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
./configure --prefix=~/local
make install # ok, fine, this step probably takes more than 30 seconds...
curl https://www.npmjs.org/install.sh | sh
"""
jQuery templates use constructs like:
{{if condition}} print something{{/if}}
This, of course, completely screws up Django templates,
because Django thinks {{ and }} mean something.
Wrap {% verbatim %} and {% endverbatim %} around those
blocks of jQuery templates and this will try its best
@kennethreitz
kennethreitz / 0_urllib2.py
Created May 16, 2011 00:17
urllib2 vs requests
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
gh_url = 'https://api.github.com'
req = urllib2.Request(gh_url)
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
@bitprophet
bitprophet / gist:1189894
Created September 2, 2011 20:56
Partial Cony settings file
def github(query, url):
url = "https://github.com/%s/issues" % url
# Direct to issue number
try:
redirect("%s/%s" % (url, int(query)))
except ValueError:
# New ticket
if query == "new":
redirect("%s/new" % url)
# Issue list