Skip to content

Instantly share code, notes, and snippets.

View branch14's full-sized avatar

Phil Hofmann branch14

View GitHub Profile
@branch14
branch14 / json2csv.rb
Created November 13, 2013 10:29
quickly convert json (array of equally structured objects) into csv
#!/usr/bin/env ruby
require 'json'
require 'csv'
CSV do |csv|
data = JSON.parse(ARGF.read)
csv << data.first.keys.sort
data.each do |line|
csv << line.to_a.sort_by { |a| a.first }.map { |a| a.last }
@branch14
branch14 / isbn13to10.rb
Created June 9, 2013 19:18
script to convert isbn13 to isbn10
#!/usr/bin/env ruby
# returns isbn10 if the given string represents a isbn13
# returns the given string otherwise
def isbn13to10(line)
isbn13 = line.tr_s('- ', '')
return line unless isbn13.match(/\d{13}/)
isbn10 = isbn13[3,9]
checksum = 0
@branch14
branch14 / ruby-signaturepad-to-image.rb
Created December 11, 2012 14:15
convert json signatures captured by thomasjbradley's signature-pad to an image
# see https://github.com/thomasjbradley/signature-pad for more details
instructions = JSON.load(data).map { |h| "line #{h['mx']},#{h['my']} #{h['lx']},#{h['ly']}" } * ' '
system "convert -size 198x55 xc:transparent -stroke blue -draw '#{instructions}' signature.png"
@branch14
branch14 / merge_locales.rb
Created June 6, 2011 14:45
Interactively merge several YAML files into one
#!/usr/bin/env ruby
#
# synopsis
#
# ruby merge_locales.rb config/locales translations.yml
require 'yaml'
require 'rubygems'
require 'highline/import'
@branch14
branch14 / gist:743495
Created December 16, 2010 15:04
mplayer.rb
# control mplayer remotely via rack
#
# load & append files (mp3, ogg, etc) & lists (m3u)
# http://localhost:3000/mplayer/loadfile+http://www.archive.org/download/af002/03_Swiss_Jazz.mp3
# http://localhost:3000/mplayer/loadlist+http://www.archive.org/stream/af002
#
# playback
# http://localhost:3000/mplayer/pause
# http://localhost:3000/mplayer/stop
#
@branch14
branch14 / mass_translate.rb
Created October 26, 2010 11:00
mass translate active record entries
# coll = Product.find_all_by_kind('material') +
# Product.find_all_by_kind('materialgroup') +
# Product.find_all_by_kind('pricegroup') +
# Product.find_all_by_kind('component')
# mass_translate(coll, [:name, :description], :en, :de)
# mass_translate(coll, [:name, :description], :de, [:en, :fr]) { |v, l| "#{v} (#{l})" }
#
# coll = Product.find_all_by_kind('product')
# mass_translate(coll, :name, :de, [:en, :fr])
#
# monkey patch I18n to log used/missing translations
I18n.module_eval do
class << self
def my_logger
filename = File.join(RAILS_ROOT, %w(log translations.log))
@my_logger ||= Logger.new(File.open(filename, 'a'))
end
#!/usr/bin/env python
# (c) 2010 Phil Hofmann <pho@panter.ch>
import os
from gimpfu import *
def batch_make_textures(input_pattern, output_path, output_prefix, size):
if not os.path.exists(output_path):
os.makedirs(output_path)
num_files, file_names = pdb.file_glob(input_pattern, 1)
(defun haml-ify ()
"run html2haml on current buffer"
(interactive)
(setf filename buffer-file-name)
(setf newfilename (concat
(car (split-string filename "\\.")) ".html.haml"))
(save-buffer)
(shell-command (concat
"html2haml " filename " > " newfilename))
(kill-buffer (current-buffer))
class Polynomial
def initialize(coeffs)
raise ArgumentError.new("Need at least 2 coefficients.") if coeffs.size < 2
@coeffs = coeffs
end
def to_s
output = []
coeffs = @coeffs.dup