Skip to content

Instantly share code, notes, and snippets.

class FeedManager(models.Manager):
def to_json(self):
l = []
for feed in self.get_query_set():
l.append(feed.dump())
return dumps(l)
class Feed(models.Model):
owners = models.ManyToManyField(User, related_name='owned_feeds')
def slugify(input):
allowed = "abcdefghijklmnopqrstuvwxyz0123456789"
output = ""
for letter in input.lower():
if letter not in allowed:
letter = '-'
output = output + letter
return output
@jacobh
jacobh / run.sh
Created April 9, 2012 22:49
Super Simple Python CI
PID_FILE="/tmp/gunicorn_photokarma"
echo "pulling changes"
git pull
echo "checking for new pip deps"
pip install -r requirements.txt
echo "collecting static files"
python manage.py collectstatic --noinput
@jacobh
jacobh / index.html
Created April 20, 2012 16:21
instagram-twitter-stream
<html>
<head>
<title>Progressive Conference | Instagram Feed</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<section id="feed">
</section>
@jacobh
jacobh / model.py
Created April 26, 2012 07:22
paginating markdown
class Session(models.Model):
course = models.ForeignKey(Course, related_name='sessions')
name = models.CharField(max_length=100)
content = models.TextField()
def pages(self):
def add_h1(string):
return "# %s" % string
return map(add_h1, re.split(r'\n#[^#]', self.content))
@jacobh
jacobh / pretty-python.py
Created May 1, 2012 20:10
OOPD 110 test 2
months = []
for month in range(12):
weight = -1
while weight < 0:
weight = float(raw_input('input weight for month #%d: ' % (month+1)))
months.append(weight)
average = reduce(lambda x,y:x+y, months) / 12
print 'average weight is %f' % average
@jacobh
jacobh / gist:2785812
Created May 25, 2012 04:43
Django ajax csrf coffeescript
$.ajaxSetup(
{
beforeSend: (xhr, settings) ->
getCookie = (name) ->
cookieValue = null
if (document.cookie and document.cookie) isnt ''
cookies = document.cookie.split(';')
for cookie in cookies
console.log cookie
if cookie.substring(0, name.length+1) is name+'='
@jacobh
jacobh / gist:2842966
Created May 31, 2012 12:01
Twitter Bootstrap Form Mixin for Django Templates
{% for field in form %}
<div class="control-group {% if field.errors %}error{% endif %}">
<label class="control-label" for="{{ field.html_name }}">{{ field.label }}</label>
<div class="controls">
{{ field }}
{% if field.errors %}
<span class="help-inline">{{ field.errors|first }}</span>
{% endif %}
{% if field.help_text %}
<p class="help-block">{{ field.help_text }}</p>
@jacobh
jacobh / scraper.py
Created June 3, 2012 15:55
LoL Champion info scraper
from bs4 import BeautifulSoup
import requests
import re
import json
base_url = 'http://na.leagueoflegends.com/'
get_page = lambda x: BeautifulSoup(requests.get(base_url + x).text)
# step one, get a list of all champion urls
from base64 import b64encode
from hashlib import sha256
from random import random
def random_uppercase_string():
raw = b64encode( sha256( str(random() ) ).digest() ).upper()
string = ''
for letter in raw:
if letter.isalpha():