Skip to content

Instantly share code, notes, and snippets.

View ollieglass's full-sized avatar

Ollie Glass ollieglass

View GitHub Profile
ollieglass / ticket.rb
Created August 4, 2014 19:32
Imagine I have tickets that belong to events. As a developer, I only want to access tickets that belong to an event, and I want to safeguard against accessing all tickets (contrived example, but anyway). If event ids start at 0, and I set the default scope to an event_id of -1, then if I forget to set an event (which would select all tickets), I…
class Ticket < ActiveRecord::Base
belongs_to :event
default_scope where(event_id: -1)
ollieglass / tweet_text.js
Created August 30, 2014 20:10
Get the text from every tweet on the page
$(".tweet-text").map(function() { return $(this).text(); })
ollieglass /
Created April 9, 2012 16:39
Condiment graph
graph condiments {
ketchup -- mayonnaise -- tartar -- lemon
ketchup -- bbq
ollieglass / jqm_example.html
Created April 9, 2012 16:30
Pages and links with jQuery Mobile
<div data-role="page" id="first">
<div data-role="header">
<h1>Page Title1</h1>
</div><!-- /header -->
<div data-role="content">
<p>Page content goes here.</p>
<a href="#second">Go to second page</a>
</div><!-- /content -->
</div><!-- /page -->
ollieglass /
Created May 19, 2012 21:15
oauth_hook, imgur and requests error
import requests
import urlparse
from oauth_hook import OAuthHook
# consumer key + secret
# get access token, create client
client = requests.session(
ollieglass /
Created October 21, 2012 20:03
Simple network visualisation
import MySQLdb
db = MySQLdb.connect(host="localhost", port=3306, user="user", passwd="passwd", db="db")
cursor = db.cursor()
cursor.execute("SELECT id, referrer_id FROM users")
print "graph referrers {"
for row in cursor.fetchall():
print "%s -- %s" % (row[0], row[1])
ollieglass /
Created December 2, 2012 14:26
Pythonic Java FizzBuzz
/* Write a program that prints the numbers from 1 to 100.
But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”.
For numbers which are multiples of both three and five print “FizzBuzz”.
Pythonic Java -
public class FizzBuzz {
public static void main(String[] args) {
ollieglass / Object
Last active December 11, 2015 23:38
Transaction, Merchant and Category object model from my personal finance mini-hackathon
class Category(models.Model):
name = models.CharField(max_length=255)
class Merchant(models.Model):
name = models.CharField(max_length=255)
category = models.ForeignKey(Category, blank=True, null=True, db_index=True)
class Transaction(models.Model):
ollieglass / Capital One credit card CSV
Last active December 11, 2015 23:38
Capital One credit card CSV importer from my personal finance mini-hackathon
for filename in os.listdir(path):
reader = csv.reader(open(path + filename), dialect=csv.excel)
headers =
for row in reader:
m, created = Merchant.objects.get_or_create(name=row[3])
# lose the £ symbol and minus sign, cast to float
ollieglass / AJAX helper for categorising credit card transactions.js
Last active December 11, 2015 23:38
AJAX helper for categorising credit card transactions, from my personal finance mini-hackathon
var setCategory = function(transactionId, categoryId) {
type: "GET",
url: "/transaction/",
data: {
transactionId: transactionId,
categoryId: categoryId
}).done(function( msg ) {