Skip to content

Instantly share code, notes, and snippets.

Avatar
🌴
On sabbatical

Willem van Bergen wvanbergen

🌴
On sabbatical
View GitHub Profile
@wvanbergen
wvanbergen / gist:5017
Created Aug 12, 2008 — forked from dbussink/gist:4461
Add the current git branch in pink to your bash prompt.
View gist:5017
# colors for ls, etc.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \[\1\]/'
}
if [[ ${EUID} == 0 ]] ; then
PS1='\[\033[01;31m\]\u@\h \[\033[01;34m\]\w\[\033[01;35m\]$(parse_git_branch)\[\033[01;34m\] \$ \[\033[00m\]'
else
PS1='\[\033[01;37m\]\w\[\033[00;35m\]$(parse_git_branch)\[\033[00m\] \$ '
fi
@wvanbergen
wvanbergen / ParseTree.eval_static_tree.rb
Created Oct 3, 2009
A method to evaluate static parts of a ParseTree syntax tree.
View ParseTree.eval_static_tree.rb
class ParseTree
# Evaluates a static part of the tree that ParseTree.translate returns.
# You can pas a block that will be used to get a value that is not static.
# A simple example:
#
# >> code = '{ :array => ["str", 123, 4.5], :dynamic => method_call }'
# >> tree = ParseTree.translate(code)
# => [:hash, [:lit, :array], [:array, [:str, "str"], [:lit, 123], [:lit, 4.5]],
# [:lit, :dynamic], [:vcall, :method_call]]
@wvanbergen
wvanbergen / gist:204134
Created Oct 7, 2009
Audio-enabled, profanity-enabled unit testing
View gist:204134
module Swearing
SWEAR_WORDS = ['dammit', 'fuck', 'son of a bitch', 'motherfucker', 'oh no', 'prick']
SWEAR_VOICE = 'Alex'
def self.swear!
`say -v #{SWEAR_VOICE} #{SWEAR_WORDS[rand(SWEAR_WORDS.length)]} &`
end
end
class Test::Unit::Error
View 1.8.7 (ruby-1.8.7-p299): ruby 1.8.7 (2010-06-23 patchlevel 299) [i686-darwin10.4.0]
---------------------------------------------
ChunkyPNG (0.8.0) decoding benchmark (n=50)
---------------------------------------------
Rehearsal ---------------------------------------------------------
From PNG stream 16.050000 0.110000 16.160000 ( 16.747523)
From RGBA pixelstream 0.090000 0.030000 0.120000 ( 0.122060)
From RGB pixelstream 0.860000 0.030000 0.890000 ( 0.901062)
----------------------------------------------- total: 17.170000sec
@wvanbergen
wvanbergen / bmp_import.rb
Created Aug 18, 2010
Create PNG from 24 bit BMP file
View bmp_import.rb
require 'rubygems'
require 'chunky_png'
# get the data
data = StringIO.new(File.read('filename.bmp'))
bmp_file_header = data.read(14).unpack('a2VxxV')
bmp_info_header = data.read(40).unpack('V3v2V6')
raise "Not a BMP file" unless bmp_file_header[0] == "BM"
@wvanbergen
wvanbergen / convert_innodb_to_per_table_idb.rb
Created Aug 22, 2010
Convert InnoDB tables to use a separate idb files instead of the shared, giant ibdata1.
View convert_innodb_to_per_table_idb.rb
#!/usr/bin/env ruby
# Converts an InnoDB database that uses a shared InnoDB tablespace file (e.g. ibdata1) to use
# a separate file per table.
#
# - First change your my.cnf file to use separate files by adding "innodb_file_per_table" to
# the [mysqld] section. (This is safe: MySQL will revert to the giant file if it cannot find
# the per table file.)
# - Restart the MySQL server
# - cd to the directory of your database (cd /opt/mysql/data/mydb)
View bucket_intervals.rb
# settings
@bucket_count = 1000 # Must be integer > 0
@min_value = 10 ** -4 # Must be > 0.0
@max_value = 10 ** +2 # Must be > @min_value
# precalculate some stuff
@min_value_log = Math.log(@min_value)
@max_value_log = Math.log(@max_value)
@log_delta = @max_value_log - @min_value_log
@bucket_size = @log_delta / @bucket_count.to_f
View Shell invocation
~ $ request-log-analyzer production.log # Analyze production.log
--format custom_file_format.rb # Use custom file format
--reject account_type free # Ignore requests from free accounts
--select locale en-US # Only look at en-US locale requests
--output HTML --file report.html # Generate an HTML report as report.html
View Encoding speed
---------------------------------------------
ChunkyPNG (0.10.0) encoding benchmark (n=10)
---------------------------------------------
Autodetect (indexed) 0.860000 0.000000 0.860000 ( 0.856220)
:no_compression 0.490000 0.000000 0.490000 ( 0.488724)
:fast_rgba 0.140000 0.000000 0.140000 ( 0.143066)
:fast_rgb 0.140000 0.000000 0.140000 ( 0.143170)
:good_compression 0.470000 0.010000 0.480000 ( 0.476883)
:best_compression 1.880000 0.000000 1.880000 ( 1.877189)
@wvanbergen
wvanbergen / Decoding speed
Created Oct 5, 2010
Chunky PNG performance: same benchmarks on Ruby 1.8.7, but added 'require "oily_png"' for a speed boost
View Decoding speed
---------------------------------------------
ChunkyPNG (0.10.2) decoding benchmark (n=20)
---------------------------------------------
ChunkyPNG OilyPNG
PNG - no filtering ( 1.056832) ( 0.078345)
PNG - UP filtering ( 3.327986) ( 0.078387)
PNG - PAETH filtering ( 7.499367) ( 0.089143)
From RGBA pixelstream ( 0.032571) ( 0.035790)