October 2011
1 post
Programatically get the version of an egg
The documentation for pkg_resources does not make it 100% obvious how to get the current version of a Python egg. To do so: pkg_resources.get_distribution(egg_name).version
Oct 12th
May 2011
1 post
Tee and exec at the same time
Print a dot for every four lines of output, also logging to output.log. LOGFILE="output.log" LOGPIPE="/tmp/$$.tmp" trap "rm -f $LOGPIPE" EXIT mknod "$LOGPIPE" p sudo tee <"$LOGPIPE" -a "$LOGFILE" | awk '{ if (NR % 4 == 0) printf "."; system("") }' & exec 1>>"$LOGPIPE" 2>&1
May 13th
August 2010
2 posts
Ruby lambda in a different scope
When you create a ruby lambda it is bound the the scope it was defined in. Therefore, if you create it in the class scope, you cannot access instance variables or protected instance methods. To execute the lambda in your instance’s scope, use the instance_eval method:
Aug 11th
Git Post Commit Hook to Play "Hallelujah" After a...
Inspired by Brandon Keepers, what follows is a self contained script that will play a single Hallelujah after you commit. To make it work, place it in your repository’s .git/hooks directory as post-commit and chmod it executable.
Aug 5th
June 2010
1 post
NSThread a selector with multiple arguments
One of the curious absences in the Mac SDK is the ability to invoke a thread that targets a method with multiple arguments. If you want to use [+ NSThread detachNewThreadSelector:toTarget:withObject:] you either have to refactor your function to a) take a collection like an NSArray or NSDictionary or b) call an intermediary function which then makes the call on that thread. You would half-expect...
Jun 24th
April 2010
1 post
Duplicate NSManagedObject with...
While working on WineLedger for the iPhone I spent a lot of time trying to work out why duplicate ‘ghost’ entries would sometimes appear when merging two NSManagedObjectContexts: As I inserted a new entry, two copies would appear: One would remain stuck, while the second would correctly update when you edited it. As soon as the app was restarted the ghost entry would miraculously...
Apr 30th
February 2010
1 post
Running Resque / Rake Tasks with Monit
script/consumer: #!/usr/bin/env ruby def pidfile @pidfile ||= File.expand_path(File.join('..', '..', 'tmp', 'pids', 'consumer.pid'), __FILE__) end case ARGV.first when 'start' then require 'rubygems' require 'rake' ENV['QUEUE'] ||= '*' ENV['RAILS_ENV'] ||= 'production' load(File.expand_path(File.join('..', '..', 'Rakefile'), __FILE__)) ...
Feb 3rd
September 2009
2 posts
1 tag
Reinstalling git on Snow Leopard
If you are reinstalling git for Snow Leopard, don’t forget to nuke MacPorts. Leaving an old version on your machine will cause the git compile process to spit out various incorrect architecture errors: ld: warning: in /opt/local/lib/libz.dylib, file is not of required architecture ld: warning: in /opt/local/lib/libiconv.dylib, file is not of required architecture Remove MacPorts with the...
Sep 2nd
1 tag
Reinstalling All Native Gems on Snow Leopard
After installing Snow Leopard all of my native code gems broke horribly, complaining that they were now on the wrong architecture. To re-install every gem automatically, I used: sudo gem list | awk '{print $1}' | xargs sudo gem install
Sep 2nd
July 2009
5 posts
1 tag
Flyweighting in Python Redux
I just expanded the Flyweighted Object class to support keyword arguments: import weakref import cPickle class FlyweightedObject(object): _pool = weakref.WeakValueDictionary() def __new__(klass, *args, **kwargs): if not hasattr(klass.__init__, 'im_func'): raise 'cannot flyweight an object which has no python constructor' arguments = {} constructor =...
Jul 23rd
1 tag
Flyweighting in Python
If you are dealing with large static datasets in Python it can be useful to flyweight your objects. With flyweighting, every time you construct a new object you check to see if it already exists. If so, the original object will be returned instead of constructing a duplicate. Recently I wrote a little bit of code to achieve this in the general case: import weakref class...
Jul 9th
1 tag
Haml 2.2 Compile Error In Ugly Production Mode
If you were previously using the Haml syntax: %p= 'string ', method, ' string' you will need to change it to: %p= ['string ', method, ' string'] to avoid compilation errors in production mode.
Jul 9th
1 tag
Python Slots
I just got clued in by Elf Sternberg’s Blog to a really useful feature in Python that I have never heard about before, slots: class Foo(object): __slots__ = ['x'] def __init__(self, n): self.x = n From the Python reference manual: “By default, instances of both old and new-style classes have a dictionary for attribute storage. This wastes space for objects having very few instance...
Jul 7th
4 notes
1 tag
JavaScript String format
Here’s a useful snippet of JavaScript I use for inserting arguments into a format string: String.format = function() { var replacements = arguments; return arguments[0].replace(/\{(\d+)\}/gm, function(string, match) { return replacements[parseInt(match) + 1]; }); } And here it is in action: String.format('http://www.google.com/search?q={0}', escape(searchTerm))
Jul 2nd
June 2009
1 post
1 tag
Converting ASCII Into Unicode In Python
To convert the unicodeString José into Jose in python: import unicodedata unicodedata.normalize('NFKD', unicodeString).encode('ASCii', 'ignore')
Jun 15th
May 2009
3 posts
1 tag
IO Error From raw_input() in Jython
When I try to use raw_input on my Ubuntu machine in Jython 2.2.1 on Java 1.6.0_07 I get an empty IOError when executing a python file, but not when using a python shell: -- raw_input.py -- raw_input() >> jython raw_input.py Traceback (innermost last): File "raw_input.py", line 1, in ? IOError: -- console -- >>> raw_input() hello 'hello' To get round this I just use...
May 20th
1 tag
Greedy and Reluctant Qualifiers
A common gotcha when using regular expressions occurs when using the default (greedy) qualifiers + ? and *. These qualifiers will attempt to make the longest match they possibly can. The regular expression: /'(.*)'/ will successfully match and group the word there in hello 'there', but will actually run on to the last single quote in the string hello 'there' how are 'you', matching there'...
May 15th
1 tag
Making Java Processes Play Nice
Looking around, it seems that there is no easy way to stop Java from eating all your system resources when running a particularly heavy-going task. Thankfully my lovely colleague Ben made me aware of a helpful UNIX command called nice. By prefixing nice to any command you can ask the scheduler to be a bit more kind, running the process at a slightly lower priority to ensure it doesn’t starve...
May 14th
April 2009
1 post
1 tag
Progressive Enhancement Using Comments
Most methods of progressive enhancement involve setting various DOM elements to display:none and then making them visible from JavaScript, or building new document nodes and inserting them. In the former case you end up with lots of brittle snippets of code for traversing the DOM and toggling elements. In the later case you often end up having to write the same markup twice: Once in your web...
Apr 28th
March 2009
3 posts
1 tag
MySQL Remote Database Transfers
One-liner to remote copy a MySQL database over SSH: mysqldump [db] | ssh -C [host] 'mysql [db]' For anything more complicated there is also taps.
Mar 9th
1 tag
Reflection and Introspection Over Modules and...
Someone Twitter me if I’m missing something, but I couldn’t find a core way of doing reflection over packages in Python. In this particular case, I wanted a way to load all the modules in a certain package (a directory with an __init__.py) and automatically add them into a running Twisted service. To get this working, I created a small module called reflection: import os import sys import...
Mar 6th
1 tag
Killing Python: Exiting Without Using SystemExit
Usually in python you exit a script programmatically by raising SystemExit or calling sys.exit. Both methods send an exception hurtling up the call stack, allowing every level of your program to execute finally statements and exit cleanly. This behaviour changes if you throw SystemExit in a multithreaded application: it kills the calling thread instead. If the calling thread is not the main...
Mar 5th
February 2009
2 posts
1 tag
Creating Static Methods In Python
Static methods can be a little confusing if you come to Python from other languages in which they are first class citizens. To create a static method you need to pass an existing method through staticmethod(): class Person: people = {} def __init__(self, name): self.name = name Person.people[self.name] = self def find_by_name(name): return Person.people.get(name) find_by_name...
Feb 13th
1 tag
Rails 2.2 Templates
TemplateHandlers have been significantly overhauled in Rails 2.2, and these changes are not backwards-compatible with Rails 2.1. Instead of being responsible for rendering a template, TemplateHandlers should now provide a string of Ruby that will be eval’ed by ActionView further along the rendering chain. So, where previously a TemplateHandler might have declared a render method: class...
Feb 1st
January 2009
1 post
1 tag
Setting The Default Java Virtual Machine On Ubuntu
Select the default Ubuntu JVM with update-alternatives: sudo update-alternatives --config java
Jan 8th
December 2008
4 posts
1 tag
Monit And My Sql
Usually Monit will be happy to watch MySQL with the following configuration: check process mysqld with pidfile "/var/run/mysqld/mysqld.pid" group database start program = "/etc/init.d/mysql start" stop program = "/etc/init.d/mysql stop" if cpu > 60% for 2 cycles then alert if cpu > 80% for 10 cycles then restart if failed port 3306 protocol mysql then restart If you...
Dec 15th
1 tag
Dynamically Generated Classes
To create a class with a dynamic name in Ruby, create a new Class object and then set it as a constant within Object: Object.const_set('ChunkyBacon', Class.new) => ChunkyBacon >> ChunkyBacon.new => #<ChunkyBacon:0x5ef740> You can use this to good effect with ActiveResource, creating arbitrary restful resources as you go: Object.const_set(model,...
Dec 14th
1 tag
Absolute Paths From Relative Paths In Bash
There doesn’t seem to be a nice, cross-platform way of deriving the absolute path from a relative one in Bash. (`readline -f .` works in Linux, but doesn’t seem to work in OS X). Here is a small program written in Python which does exactly that: #!/usr/bin/env python import os, sys if len(sys.argv) < 2: print 'usage: %s <PATHS>' % sys.argv[0] raise SystemExit(1) directory =...
Dec 11th
1 tag
Custom 404 Pages With Passenger
On failing to match a route, Rails 2.1.2 appears to rescue the ActionController::RoutingError with its stock template (/action_controller/templates/rescues/layout.erb) and then returns an upstream 404 error code — even if you’ve defined a custom rescue_from in ActionController: rescue_from ActionController::RoutingError, :with => :render_404 With your Mongrel/Thin and Apache/Nginx combo this...
Dec 11th
November 2008
2 posts
1 tag
Custom Error Pages With Nginx and Thin
If you are using Nginx to proxy to another web server such as Thin or Mongrel, chances are you have had trouble getting your custom error pages to return the correct status codes. If your application redirects to a cached error page Nginx will serve it as a normal file and will (correctly) tell the browser that everything was successful. Not quite the intended behaviour. To fix this, add...
Nov 21st
1 tag
Memoization In Javascript
function memoize(f) { return function () { var args = Array.prototype.slice.call(arguments); f.memoized = f.memoized || {}; return (args in f.memoized) ? f.memoized[args] : f.memoized[args] = f.apply(this, args); }; } http://blog.thejit.org/
Nov 5th
October 2008
3 posts
1 tag
Ruby Process Lock and Graceful Failovers
If you are running several thin or mongrel instances it can be useful to designate one as master and the others as slaves — say for a backup task that only really needs to run once per machine. A nice way of doing it is with a process lock. When a server starts it tries to grab a file lock and write its process ID. The first server to do so becomes the master process. You can tell if the owner...
Oct 29th
1 tag
Link Like DHTML Elements And Disabling Text...
When turning a non-anchor element into an onclickable using JavaScript, you may have noticed that browsers will still allow users to select text ranges. Disable the behaviour using this: element.onselectstart = function(){return false}; element.unselectable = "on"; element.style.MozUserSelect = "none";
Oct 10th
1 tag
Mac OS X: Tar Without Resource Forks & File...
You can stop Tar from archiving the ._ files that Mac OS uses to mark up its file system with an undocumented environment variable: export COPYFILE_DISABLE=true Thanks Norman Walsh
Oct 8th
June 2008
1 post
1 tag
Downed Remote Sources In Gem
To remove a broken remote source from your Ruby Gem environment, edit your ~/.gemrc file.
Jun 19th