Skip to content

Instantly share code, notes, and snippets.

View asmallteapot's full-sized avatar
😴
😴

Ellen Teapot asmallteapot

😴
😴
View GitHub Profile
class Article(models.Model):
@models.permalink
def get_absolute_url(self):
return reverse('article_permalink', kwargs={
'year': self.published_at.year,
'month': self.published_at.month,
'day': self.published_at.day,
'slug': self.slug,
})
@asmallteapot
asmallteapot / selmon.rb
Created September 11, 2011 18:25
Check status of the Tampa Crosstown Expressway’s reversible express lanes in Ruby. Tampa–Hillsborough Expressway Authority doesn’t offer an API, so it uses Nokogiri to scrap the official website’s status display.
#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
require 'nokogiri'
doc = Nokogiri::HTML(open('http://www.tampa-xway.com/'))
img_src = doc.css('img.closures').first['src']
if img_src.match /West/
puts 'Westbound'
@asmallteapot
asmallteapot / init_nginx.sh
Created January 18, 2012 15:20
Init scripts for Nginx and uWSGI on Ubuntu 11.10
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
@asmallteapot
asmallteapot / gist:1664575
Created January 23, 2012 18:07
Using C99 for clever and non-fragile UITableView configuration. Allows implicit defaults. Copypasta’d from a screencap tweeted by @jonsterling: http://twitpic.com/80uyfp
typedef enum {
kAccomplishmentTitleSection = 0,
kAccomplishmentCategorySection,
kNumberOfSections
} NESTemplateEditTableSections;
static NSString * const kCellIdentifiers[] = {
[kAccomplishmentTitleSection] = @"AccomplishmentTitleCell",
[kAccomplishmentCategorySection] = @"AccomplishmentCategoryCell"
};
@asmallteapot
asmallteapot / api.py
Created January 25, 2012 16:04
How to make a TastyPie API with nested resources.
class ArticleResource(ModelResource):
class Meta:
authentication = Authentication()
authorization = DjangoAuthorization()
queryset = Article.objects.all()
class CategoryResource(ModelResource):
articles = fields.ToManyField('locations.api.Articles', 'articles', full=True)
@asmallteapot
asmallteapot / opening_hours_validation.py
Created January 31, 2012 21:56
[Django] Validate that each day of the week is used in up to one set of opening hours. Includes user–friendly error messages.
from django.core.exceptions import ValidationError
WEEKDAYS = (
(1, 'Monday'),
(2, 'Tuesday'),
(3, 'Wednesday'),
(4, 'Thursday'),
(5, 'Friday'),
(6, 'Saturday'),
(7, 'Sunday'),
@asmallteapot
asmallteapot / gist:1747711
Created February 5, 2012 19:59
Less-hacky approach for configuring table views that are mostly static, but have some sections/cells that are conditionally visible.
// in -viewDidLoad
self.cellOrder = [NSMutableArray arrayWithObjects:@"Address", @"Hours", @"Call", @"Get Directions", nil];
if (foo) {
[self.cellOrder removeObject:@"Hours"];
}
if (!self.location.phoneNumber) {
[self.cellOrder removeObject:@"Call"];
}
@asmallteapot
asmallteapot / currentlyOpen.m
Created February 8, 2012 18:04
I love^W**HATE** date programming on iOS.
- (BOOL)currentlyOpen {
int weekday = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:[NSDate date]] weekday];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(weekdayFrom <= %d OR weekdayTo >= %d) AND ANY location.resourceUri like %@", weekday, weekday, self.resourceUri];
Hours *hours = [Hours findFirstWithPredicate:predicate];
int currentHour = [[[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]] hour];
if ([hours.hourFrom intValue] <= currentHour && [hours.hourTo intValue] >= currentHour) {
return YES;
} else {
return NO;
}
@asmallteapot
asmallteapot / example.conf
Created February 27, 2012 19:42
Upstart configuration for uWSGI serving a Django project to a socket. I’m using this with Nginx on Ubuntu 11.10 in production for a smallish RESTful API.
#!upstart
description "uWSGI server for example.com"
start on startup
stop on shutdown
script
exec sudo -u www-data /usr/bin/uwsgi --plugins http,python \
--chmod-socket --socket /tmp/example.sock \
--wsgi-file /srv/www/example.com/example/config.wsgi \
@asmallteapot
asmallteapot / markdown.rb
Created February 28, 2012 12:55
Markdown + syntax highlighting. Can’t remember where I stole this from. Probably one of the GitHub folk.
gem 'redcarpet'
gem 'albino' # requires pgyments
gem 'nokogiri'
def markdown(text)
options = [
:autolink, :fenced_code, :filter_html, :gh_blockcode, :hard_wrap
:no_intraemphasis, :strikethrough, :tables, :xhtml
]