Skip to content

Instantly share code, notes, and snippets.

View ollieglass's full-sized avatar

Ollie Glass ollieglass

View GitHub Profile
@ollieglass
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)
end
@ollieglass
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
ollieglass / condiments.dot
Created April 9, 2012 16:39
Condiment graph
graph condiments {
ketchup -- mayonnaise -- tartar -- lemon
ketchup -- bbq
}
@ollieglass
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
ollieglass / imgur.py
Created May 19, 2012 21:15
oauth_hook, imgur and requests error
import requests
import urlparse
from oauth_hook import OAuthHook
# consumer key + secret
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
# get access token, create client
client = requests.session(
@ollieglass
ollieglass / vis.py
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
ollieglass / FizzBuzz.java
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 - https://gist.github.com/1725650
*/
public class FizzBuzz {
public static void main(String[] args) {
@ollieglass
ollieglass / Object model.py
Last active December 11, 2015 23:38
Transaction, Merchant and Category object model from my personal finance mini-hackathon http://ollieglass.com/2013/01/30/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
ollieglass / Capital One credit card CSV importer.py
Last active December 11, 2015 23:38
Capital One credit card CSV importer from my personal finance mini-hackathon http://ollieglass.com/2013/01/30/personal-finance-mini-hackathon
for filename in os.listdir(path):
reader = csv.reader(open(path + filename), dialect=csv.excel)
headers = reader.next()
for row in reader:
m, created = Merchant.objects.get_or_create(name=row[3])
# lose the £ symbol and minus sign, cast to float
@ollieglass
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 http://ollieglass.com/2013/01/30/personal-finance-mini-hackathon
var setCategory = function(transactionId, categoryId) {
$.ajax({
type: "GET",
url: "/transaction/",
data: {
transactionId: transactionId,
categoryId: categoryId
}
}).done(function( msg ) {
console.log(msg);