Skip to content

Instantly share code, notes, and snippets.

@dogweather
dogweather / define-a-function.py
Last active December 31, 2015 06:59
Define a function
def percentage_of_whole(part1, part2):
return float(part1) / (part1 + part2) * 100
@dogweather
dogweather / define-a-function.rb
Created December 13, 2013 20:33
Define a function
def percentage_of_whole(part1, part2)
part1.to_f / (part1 + part2) * 100
end
@dogweather
dogweather / regex-byte-search.rb
Last active December 31, 2015 09:39
Search for bytes with a regex
# Repair strings which won't encode into UTF-8
text.gsub(/\x92/n, "'")
text.gsub(/\x97/n, ", ")
@dogweather
dogweather / cmd-line-args-py.txt
Created January 1, 2014 08:02
The current best practice for parsing command line arguments with Python
optparse is deprecated.
use argparse.
@dogweather
dogweather / number_parser.rb
Created January 2, 2014 07:48
A simple English language number parser. How's the coding style? How would I extend this for a broader range of natural language number expressions?
#
# Parse textual numbers into actual numbers
#
class NumberParser
ATOMS = {
'zero' => 0,
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
@dogweather
dogweather / number_parser_spec.rb
Created January 2, 2014 07:50
A passing spec for the simple number parser.
# -*- coding: utf-8 -*-
require 'spec_helper'
describe Parsers::NumberParser do
before(:each) do
@p = Parsers::NumberParser.new
end
require 'rspec'
describe "Behaviour" do
it "should pass" do
true.should eq true
end
it "should fail" do
true.should eq false
end
@dogweather
dogweather / Gemfile
Created April 21, 2014 17:46
Rails 4.1.0 + SASS + SASS-Rails
source 'https://rubygems.org'
ruby '2.1.1'
gem 'rails', '4.1.0'
gem 'sass-rails', '~> 4.0.3'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'turbolinks'
@dogweather
dogweather / better_csv_spec.rb
Created May 7, 2014 19:50
A first pass at a CSV API which provides header-based access plus error handling
describe BetterCsv do
it 'has a nice API' do
parser = BetterCsv.parse('info.csv', headers: :first_row)
parser.each_row do |parse_result|
next if parse_result.error?
puts "Found the name and phone #: #{parse_result.name}, #{parse_result.telephone}"
end
end
end
@dogweather
dogweather / controller1.py
Last active August 29, 2015 14:06
The controller code as originally written, with a small test suite
import pytest
def product_index(request):
limit = request.GET.get('limit')
offset = request.GET.get('offset')
if limit and limit.isdigit():
limit = int(limit)
else:
limit = 10