Skip to content

Instantly share code, notes, and snippets.

@dogweather
dogweather / controller2.py
Last active August 29, 2015 14:06
After some refactoring to make DRY, enable re-use, and keep similar abstractions together
import pytest
def product_index(request):
limit = parse_parameter(request, 'limit', int, default=10)
offset = parse_parameter(request, 'offset', int, default=0)
return limit, offset
def parse_parameter(request, name, klass, default=None):
try:
return klass(request.GET.get(name, default))
@dogweather
dogweather / ipython-output.py
Created October 13, 2014 20:09
Using ipython to learn about the String class
pluto:~ $ ipython
Python 2.7.8 (default, Aug 24 2014, 21:26:19)
Type "copyright", "credits" or "license" for more information.
IPython 2.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
@dogweather
dogweather / csv_test.rb
Created November 26, 2014 22:30
An automated test which verifies compliance with the LIVES standard
require 'test_helper'
describe 'csv' do
describe 'encoder' do
it 'should properly enclose quotes and commas' do
# From http://www.yelp.com/healthscores
#
# Field values that contain quotation marks or commas must be enclosed
# within quotation marks. In addition, each quotation mark in the field
# value must be preceded with a quotation mark. This is consistent with
@dogweather
dogweather / setup.py
Created December 14, 2014 23:12
Is this a reasonable setup.py for a 2.7.8 app?
#!/usr/bin/env python
from setuptools import setup, find_packages
import sys
if sys.argv[1] == 'test':
import multiprocessing, logging
from billiard import util
with open('requirements.txt') as f:
required = f.read().splitlines()
@dogweather
dogweather / python-testing-survey.md
Created January 29, 2015 08:06
Python Testing Options, Early 2015

Unittest

Doctest

@dogweather
dogweather / transcript.txt
Created March 2, 2015 21:15
Set up a Python development directory
pluto:~ $ mkdir project
pluto:~ $ cd !$
cd project
pluto:~/project $ pyvenv venv
pluto:~/project $ ls
venv/
pluto:~/project $ source venv/bin/activate
pluto:~/project $ which python
/Users/me/project/venv/bin/python
pluto:~/project $ python --version
@dogweather
dogweather / string.rb
Created March 18, 2015 21:51
Demonstrating ruby string extensions
# -*- coding: utf-8 -*-
require 'digest/md5'
require 'set'
class String
ARTICLES = Set.new [
'a','an','and','by','for','in','is','of','not','on','or','over','the','to','under', 'with']
@dogweather
dogweather / rsvp_controller.rb
Created May 11, 2015 23:02
An example of a "wizard" in Rails
require 'types'
class RsvpController < ApplicationController
include Wicked::Wizard
steps :get_visitor_kind, :get_gender, :choose_events, :confirmation, :thank_you, :cancel_visit
before_filter :get_user, :except => :sign_in
def show
case step
@dogweather
dogweather / show.rb
Created May 13, 2015 19:05
businesses_controller#show
def show
map_query = CGI.escape("#{@business.name} #{@business.address} #{@business.city} #{STATE} #{@business.zipcode}")
@google_map_url = "https://www.google.com/maps/embed/v1/place?key=#{API_KEY}&q=#{map_query}"
@html_title = "#{@business.name} – health score #{@business.score}/100 – #{@business.city}, OR – Eaternet"
@html_description = "Health inspection history for the #{@business.name} at #{@business.address} in #{@business.city}, #{state}"
@inspection = @business.latest_inspection
@previous_inspections = @business.inspections.select { |i| i != @inspection }
@swiftype_meta_info = swiftype_metadata_json(@business)
expires_in(60.minutes, public: true)
end
@dogweather
dogweather / show.html.erb
Created May 13, 2015 19:08
my show for a business
<div class="row">
<div class="col-sm-7">
<div class="page-header" itemscope itemtype="http://schema.org/Restaurant">
<h1 style="margin-bottom:0" itemprop="name">
<%= @business.name %>
</h1>
<address class="text-muted" itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
<span itemprop="streetAddress"><%= @business.address %></span><br>
<span itemprop="addressLocality"><%= @business.city %></span>, <span itemprop="addressRegion">OR</span> <span itemprop="postalCode"><%= @business.zipcode %></span>