<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Eldritch nomenclature, conjoured for arcane contraptions to execute unerringly.</description><title>Simon Engledew's Blog</title><generator>Tumblr (3.0; @engledew)</generator><link>http://blog.engledew.com/</link><item><title>Programatically get the version of an egg</title><description>&lt;p&gt;The documentation for pkg_resources does not make it 100% obvious how to get the current version of a Python egg. To do so:

&lt;code&gt;&lt;pre&gt;pkg_resources.get_distribution(egg_name).version&lt;/pre&gt;&lt;/code&gt;&lt;/p&gt;</description><link>http://blog.engledew.com/post/11350089557</link><guid>http://blog.engledew.com/post/11350089557</guid><pubDate>Wed, 12 Oct 2011 11:32:43 +0100</pubDate></item><item><title>Tee and exec at the same time</title><description>&lt;p&gt;Print a dot for every four lines of output, also logging to output.log.

&lt;pre&gt;&lt;code&gt;LOGFILE="output.log"
LOGPIPE="/tmp/$$.tmp"
trap "rm -f $LOGPIPE" EXIT
mknod "$LOGPIPE" p
sudo tee &amp;lt;"$LOGPIPE" -a "$LOGFILE" | awk '{ if (NR % 4 == 0) printf "."; system("") }' &amp;amp;
exec 1&amp;gt;&amp;gt;"$LOGPIPE" 2&amp;gt;&amp;amp;1&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;</description><link>http://blog.engledew.com/post/5445335743</link><guid>http://blog.engledew.com/post/5445335743</guid><pubDate>Fri, 13 May 2011 08:47:00 +0100</pubDate></item><item><title>Ruby lambda in a different scope</title><description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;To execute the lambda in your instance&amp;#8217;s scope, use the instance_eval method:&lt;/p&gt;

&lt;p&gt;&lt;script src="http://gist.github.com/518775.js?file=images_controller.rb"&gt;&lt;/script&gt;&lt;/p&gt;</description><link>http://blog.engledew.com/post/936516444</link><guid>http://blog.engledew.com/post/936516444</guid><pubDate>Wed, 11 Aug 2010 11:10:00 +0100</pubDate></item><item><title>Git Post Commit Hook to Play "Hallelujah" After a Commit</title><description>&lt;p&gt;Inspired by &lt;a href="http://collectiveidea.com/blog/archives/2010/08/03/happy-git-commits/"&gt;Brandon Keepers&lt;/a&gt;, what follows is a self contained script that will play a single Hallelujah after you commit.&lt;/p&gt;

&lt;p&gt;To make it work, place it in your repository&amp;#8217;s &lt;code&gt;.git/hooks&lt;/code&gt; directory as &lt;code&gt;post-commit&lt;/code&gt; and chmod it executable.&lt;/p&gt;

&lt;script src="http://gist.github.com/509672.js?file=post-commit"&gt;&lt;/script&gt;</description><link>http://blog.engledew.com/post/907550864</link><guid>http://blog.engledew.com/post/907550864</guid><pubDate>Thu, 05 Aug 2010 13:53:00 +0100</pubDate></item><item><title>NSThread a selector with multiple arguments</title><description>&lt;p&gt;One of the curious absences in the Mac SDK is the ability to invoke a thread that targets a method with multiple arguments.&lt;/p&gt;

&lt;p&gt;If you want to use &lt;code&gt;[+ NSThread detachNewThreadSelector:toTarget:withObject:]&lt;/code&gt; 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 a method like &lt;code&gt;[+ NSThread detachNewThreadSelector:toTarget:withObjects:]&lt;/code&gt; to exist.&lt;/p&gt;

&lt;p&gt;In order to get round this, I created the SEThreadInvocation object which blends a NSThread and a NSInvocation together.&lt;/p&gt;

&lt;script src="http://gist.github.com/451182.js?file=SEThreadInvocation.h"&gt;&lt;/script&gt;&lt;script src="http://gist.github.com/451182.js?file=SEThreadInvocation.m"&gt;&lt;/script&gt;&lt;p&gt;You can then call your method like so:&lt;/p&gt;

&lt;script src="http://gist.github.com/451182.js?file=example.m"&gt;&lt;/script&gt;</description><link>http://blog.engledew.com/post/730804661</link><guid>http://blog.engledew.com/post/730804661</guid><pubDate>Thu, 24 Jun 2010 10:03:00 +0100</pubDate></item><item><title>Duplicate NSManagedObject with NSFetchedResultsController</title><description>&lt;p&gt;While working on &lt;a href="http://www.wineledger.co.uk"&gt;WineLedger&lt;/a&gt; for the iPhone I spent a lot of time trying to work out why duplicate &amp;#8216;ghost&amp;#8217; entries would sometimes appear when merging two NSManagedObjectContexts:&lt;/p&gt;

&lt;img src="http://media.tumblr.com/tumblr_l1oocxbgOY1qacsvs.png"/&gt;&lt;p&gt;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 vanish. To add to the confusion, this behaviour would only occur when I used the camera to take a photo via UIImagePickerController.&lt;/p&gt;

&lt;p&gt;After a lot of trial and error I finally discovered that the main view was being unloaded from memory as soon as you took a picture. Using the camera causes a huge memory spike as the phone stores the high resolution image.&lt;/p&gt;

&lt;p&gt;This shouldn&amp;#8217;t be a problem, but as the view loads back &lt;i&gt;into&lt;/i&gt; memory the NSFetchedResultsController does a fetch. This initial fetch loads the full set of results (including the freshly inserted object), and then the delegate methods proceed to perform a nice slide animation when the two contexts are merged, inserting the object into the UITableView again.&lt;/p&gt;

&lt;p&gt;The fix for this problem is to insert a guard to check to see if the NSFetchedResultsController already has some managed objects before it does a fetch on viewDidLoad.&lt;/p&gt;

&lt;code&gt;&lt;pre&gt;
if (self.fetchedResultsController.fetchedObjects == nil)
{
    NSError * error = nil;
	
    if (![self.fetchedResultsController performFetch:&amp;amp;error])
    {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    }
}
&lt;/pre&gt;&lt;/code&gt;</description><link>http://blog.engledew.com/post/560601132</link><guid>http://blog.engledew.com/post/560601132</guid><pubDate>Fri, 30 Apr 2010 11:04:00 +0100</pubDate></item><item><title>Running Resque / Rake Tasks with Monit</title><description>&lt;p&gt;script/consumer:

&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;#!/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__))

    Process.detach(consumer = Process.fork do
      begin
        Rake::Task['environment'].invoke
        Rake::Task['resque:work'].invoke
      rescue Exception =&amp;gt; e
        $stdout.puts([e, *e.backtrace].join($/))
      end
    end)

    %x(echo "#{consumer}" &amp;gt; #{pidfile})

  when 'stop' then
    %x(kill `cat #{pidfile}`)
    %x(rm #{pidfile})
    
  else
    
    puts 'usage: script/consumer start|stop'
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;</description><link>http://blog.engledew.com/post/368557156</link><guid>http://blog.engledew.com/post/368557156</guid><pubDate>Wed, 03 Feb 2010 10:11:00 +0000</pubDate></item><item><title>Reinstalling git on Snow Leopard</title><description>&lt;p&gt;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:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;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&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Remove MacPorts with the terrifyingly brutal:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;sudo rm -rf \
    /opt/local \
    /etc/manpaths.d/macports \
    /etc/paths.d/macports \
    /Applications/DarwinPorts \
    /Applications/MacPorts \
    /Library/LaunchDaemons/org.macports.* \
    /Library/Receipts/DarwinPorts*.pkg \
    /Library/Receipts/MacPorts*.pkg \
    /Library/StartupItems/DarwinPortsStartup \
    /Library/Tcl/darwinports1.0 \
    /Library/Tcl/macports1.0&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Once you have removed MacPorts, the compile process should be as easy as:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;make configure 
./configure --prefix=/usr/local
make prefix=/usr/local all
sudo make prefix=/usr/local install

git config --global user.name "[user.name]" 
git config --global user.email "[user.email]"&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And when you are done, don’t forget this little gem for adding pretty colours to your git output:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;git config --global color.ui "auto"&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://blog.engledew.com/post/344118357</link><guid>http://blog.engledew.com/post/344118357</guid><pubDate>Wed, 02 Sep 2009 21:50:00 +0100</pubDate><category>admin</category></item><item><title>Reinstalling All Native Gems on Snow Leopard</title><description>&lt;p&gt;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:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;sudo gem list | awk '{print $1}' | xargs sudo gem install&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://blog.engledew.com/post/344118347</link><guid>http://blog.engledew.com/post/344118347</guid><pubDate>Wed, 02 Sep 2009 21:38:00 +0100</pubDate><category>rails</category></item><item><title>Flyweighting in Python Redux</title><description>&lt;p&gt;I just expanded the Flyweighted Object class to support keyword arguments:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;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 = klass.__init__.im_func
        arguments_missing = constructor.func_code.co_argcount - len(args) - 1

        if arguments_missing &amp;gt; 0:
            args += constructor.func_defaults[-arguments_missing:]

        varnames = constructor.func_code.co_varnames[1:]

        for i in range(len(varnames)):
            varname = varnames[i]
            arguments[varname] = kwargs.get(varname, args[i])

        key = cPickle.dumps((klass, arguments))
        instance = klass._pool.get(key, None)

        if instance is None:
            instance = object.__new__(klass)
            klass._pool[key] = instance

        return instance

    def __getnewargs__(self):
        if hasattr(self.__class__.__init__, 'im_func'):
            constructor = self.__class__.__init__.im_func
            return tuple(getattr(self, attr) for attr in constructor.func_code.co_varnames[1:])
        return tuple()&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://blog.engledew.com/post/344118340</link><guid>http://blog.engledew.com/post/344118340</guid><pubDate>Fri, 24 Jul 2009 01:56:00 +0100</pubDate><category>python</category></item><item><title>Flyweighting in Python</title><description>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Recently I wrote a little bit of code to achieve this in the general case:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;import weakref

class FlyweightedObject(object):
    _pool = weakref.WeakValueDictionary()
    
    def __new__(klass, *args):
        if hasattr(klass.__init__, 'im_func'):
            constructor = klass.__init__.im_func
            arguments_missing = constructor.func_code.co_argcount - len(args) - 1
            if arguments_missing &amp;gt; 0:
                args += constructor.func_defaults[-arguments_missing:]

        key = (klass,) + args
        instance = klass._pool.get(key, None)

        if instance is None:
            instance = object.__new__(klass)
            klass._pool[key] = instance
            
        return instance&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now when you inherit &lt;code&gt;FlyweightedObject&lt;/code&gt; you get the flyweighting thrown in for free:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;class Person(FlyweightedObject):
    def __init__(self, age, name = 'Simon'):
        self.age = age
        self.name = name

f = Person(1)
g = Person(1, 'Simon')

print id(f) == id(g)
# =&amp;gt; True&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;One thing to watch out for is changing attributes after the object has been constructed. This will lead to the flyweight pool keys and the objects themselves going out of sync:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;f = Person(1, 'Dave')
f.name = 'Simon'  

g = Person(1, 'Simon')

print id(f) == id(g)
# =&amp;gt; False&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;One last thing. To get pickle working with flyweighted objects you’ll have to create a &lt;code&gt;__getnewargs__&lt;/code&gt; method which returns the tuple that will be passed to &lt;code&gt;__new__&lt;/code&gt; on unpickling:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;class Person(FlyweightedObject):
    def __init__(self, age, name = 'Simon'):
        self.name = name
        self.age = age

    def __getnewargs__(self):
        return self.age, self.name&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This can also be automated as long as the instance variables are named correctly:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;def __getnewargs__(self):
    if hasattr(self.__class__.__init__, 'im_func'):
        constructor = self.__class__.__init__.im_func
        return tuple(getattr(self, attr) for attr in constructor.func_code.co_varnames[1:])
    return tuple()&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://blog.engledew.com/post/344118333</link><guid>http://blog.engledew.com/post/344118333</guid><pubDate>Fri, 10 Jul 2009 02:55:00 +0100</pubDate><category>python</category></item><item><title>Haml 2.2 Compile Error In Ugly Production Mode</title><description>&lt;p&gt;If you were previously using the Haml syntax:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;%p= 'string ', method, ' string'&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;you will need to change it to:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;%p= ['string ', method, ' string']&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;to avoid compilation errors in production mode.&lt;/p&gt;</description><link>http://blog.engledew.com/post/344118323</link><guid>http://blog.engledew.com/post/344118323</guid><pubDate>Thu, 09 Jul 2009 12:09:00 +0100</pubDate><category>rails</category></item><item><title>Python Slots</title><description>&lt;p&gt;I just got clued in by &lt;a href="http://www.elfsternberg.com/2009/07/06/python-what-the-hell-is-a-slot/comment-page-1/#comment-335"&gt;Elf Sternberg’s Blog&lt;/a&gt; to a really useful feature in Python that I have never heard about before, slots:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;class Foo(object):
    __slots__ = ['x']
    def __init__(self, n):
        self.x = n&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;From the &lt;a href="http://www.network-theory.co.uk/docs/pylang/__slots__.html"&gt;Python reference manual&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;“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 variables. The space consumption can become acute when creating large numbers of instances.&lt;br/&gt;&lt;br/&gt;
The default can be overridden by defining &lt;var&gt;&lt;i&gt;slots&lt;/i&gt;&lt;/var&gt; in a new-style class definition. The &lt;var&gt;&lt;i&gt;slots&lt;/i&gt;&lt;/var&gt; declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because &lt;var&gt;&lt;i&gt;dict&lt;/i&gt;&lt;/var&gt; is not created for each instance.&amp;#8221;&lt;/blockquote&gt;
&lt;p&gt;At work we have a plans engine that loads millions of little Python objects into memory, so this is a great little optimisation for us.&lt;/p&gt;
&lt;p&gt;Finally, to get a set of every slot attribute in the object hierarcy, you can add this method to your class:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;def inherited_slots(self):
        return set(slot for klass in self.__class__.__mro__ if hasattr(klass, '__slots__') for slot in klass.__slots__)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://blog.engledew.com/post/344118314</link><guid>http://blog.engledew.com/post/344118314</guid><pubDate>Tue, 07 Jul 2009 10:42:00 +0100</pubDate><category>python</category></item><item><title>JavaScript String format</title><description>&lt;p&gt;Here’s a useful snippet of JavaScript I use for inserting arguments into a format string:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;String.format = function()
{
  var replacements = arguments;
  return arguments[0].replace(/\{(\d+)\}/gm, function(string, match) {
    return replacements[parseInt(match) + 1];
  });
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And here it is in action:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;String.format('http://www.google.com/search?q={0}', escape(searchTerm))&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://blog.engledew.com/post/344118305</link><guid>http://blog.engledew.com/post/344118305</guid><pubDate>Thu, 02 Jul 2009 13:11:00 +0100</pubDate><category>javascript</category></item><item><title>Converting ASCII Into Unicode In Python</title><description>&lt;p&gt;To convert the &lt;var&gt;unicodeString&lt;/var&gt; José into Jose in python:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;import unicodedata

unicodedata.normalize('NFKD', unicodeString).encode('ASCii', 'ignore')&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://blog.engledew.com/post/344116691</link><guid>http://blog.engledew.com/post/344116691</guid><pubDate>Mon, 15 Jun 2009 16:08:00 +0100</pubDate><category>python</category></item><item><title>IO Error From raw_input() in Jython</title><description>&lt;p&gt;When I try to use &lt;var&gt;raw_input&lt;/var&gt; on my Ubuntu machine in Jython 2.2.1 on Java 1.6.0_07 I get an empty &lt;var&gt;IOError&lt;/var&gt; when executing a python file, but not when using a python shell:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;-- raw_input.py --
raw_input()

&amp;gt;&amp;gt; jython raw_input.py 
Traceback (innermost last):
  File "raw_input.py", line 1, in ?
IOError:

-- console --
&amp;gt;&amp;gt;&amp;gt; raw_input()
hello
'hello'&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To get round this I just use &lt;var&gt;sys.stdin.readline().strip()&lt;/var&gt; to read a line from stdin and remove the trailing newline character.&lt;/p&gt;</description><link>http://blog.engledew.com/post/344116683</link><guid>http://blog.engledew.com/post/344116683</guid><pubDate>Wed, 20 May 2009 12:54:00 +0100</pubDate><category>python</category></item><item><title>Greedy and Reluctant Qualifiers</title><description>&lt;p&gt;A common gotcha when using regular expressions occurs when using the default (greedy) qualifiers +&amp;#160;? and *. These qualifiers will attempt to make the longest match they possibly can.&lt;/p&gt;
&lt;p&gt;The regular expression:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;/'(.*)'/&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;will successfully match and group the word &lt;var&gt;there&lt;/var&gt; in &lt;code&gt;hello 'there'&lt;/code&gt;, but will actually run on to the last single quote in the string &lt;code&gt;hello 'there' how are 'you'&lt;/code&gt;, matching &lt;code&gt;there' how are 'you&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;One solution is to restrict the set of characters you are searching for with the greedy qualifier, thus ensuring the match will finish before hitting the terminating character:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;/'([^']*)'/&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This works, but the more readable option is to turn the greedy qualifier into a &lt;em&gt;reluctant&lt;/em&gt; one:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;/'(.*?)'/&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;By adding a&amp;#160;? to the qualifier the expression will try to match the minimum string that satisfies the expression.&lt;/p&gt;</description><link>http://blog.engledew.com/post/344116675</link><guid>http://blog.engledew.com/post/344116675</guid><pubDate>Fri, 15 May 2009 08:52:00 +0100</pubDate><category>tricks</category></item><item><title>Making Java Processes Play Nice</title><description>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Thankfully my lovely colleague Ben made me aware of a helpful &lt;span class="caps"&gt;UNIX&lt;/span&gt; command called &lt;code&gt;nice&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;By prefixing &lt;var&gt;nice&lt;/var&gt; 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 other resources of &lt;span class="caps"&gt;CPU&lt;/span&gt; time:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;nice java ExpensiveTask&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description><link>http://blog.engledew.com/post/344116669</link><guid>http://blog.engledew.com/post/344116669</guid><pubDate>Thu, 14 May 2009 14:50:06 +0100</pubDate><category>tricks</category></item><item><title>Progressive Enhancement Using Comments</title><description>&lt;p&gt;Most methods of progressive enhancement involve setting various &lt;span class="caps"&gt;DOM&lt;/span&gt; elements to display:none and then making them visible from JavaScript, or building new document nodes and inserting them.&lt;/p&gt;
&lt;p&gt;In the former case you end up with lots of brittle snippets of code for traversing the &lt;span class="caps"&gt;DOM&lt;/span&gt; and toggling elements. In the later case you often end up having to write the same markup twice: Once in your web application, and once in the JavaScript which enhances your code.&lt;/p&gt;
&lt;p&gt;I recently read a &lt;a href="http://james.padolsey.com/javascript/introducing-jshtml/"&gt;blog post by James Padolsey&lt;/a&gt; that suggested creating comment nodes with &lt;span class="caps"&gt;HTML&lt;/span&gt; inside and then promoting their contents to real nodes within their parent elements. Neat.&lt;/p&gt;
&lt;p&gt;The comments for this article on Hacker News quickly moved to performance however: As you can’t natively scour the &lt;span class="caps"&gt;DOM&lt;/span&gt; for comments, it is necessary to iterate over every &lt;span class="caps"&gt;DOM&lt;/span&gt; element from JavaScript – checking its type and manipulating it if applicable. An expensive business.&lt;/p&gt;
&lt;p&gt;If performance really is an issue, but you like the idea of baking your &lt;span class="caps"&gt;HTML&lt;/span&gt; directly into the page as comments, you can replace the &lt;span class="caps"&gt;DOM&lt;/span&gt; traversal with an atomic regular expression replace and an innerHTML assignment. Both of these operations occur in the underlying &lt;span class="caps"&gt;DOM&lt;/span&gt; implementation and are run at native speeds:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code class="javascript"&gt;document.body.innerHTML = document.body.innerHTML.replace(/(?:&amp;lt;!--\[enhance\]&amp;gt;)|(?:&amp;lt;!\[enhance\]--&amp;gt;)/g, '');&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Wrapped up into a tidy rails helper, you can use this to create blocked out elements which will only render if the user has JavaScript enabled:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;def enhancement(&amp;amp;block)
  concat("&amp;lt;!--[enhance]&amp;gt;#{capture(&amp;amp;block)}&amp;lt;![enhance]--&amp;gt;")
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;&amp;lt;% enhancement do %&amp;gt;You have JavaScript enabled&amp;lt;% end %&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Obviously it’s not quite as flexible as the &lt;span class="caps"&gt;DOM&lt;/span&gt;-based solution — especially as James seems to be branching out into a fully-blown templating system called &lt;span class="caps"&gt;JSHTML&lt;/span&gt; — but I think it works as a speedy way to encapsulate JavaScript-only functionality.&lt;/p&gt;</description><link>http://blog.engledew.com/post/344116656</link><guid>http://blog.engledew.com/post/344116656</guid><pubDate>Tue, 28 Apr 2009 14:46:00 +0100</pubDate><category>javascript</category></item><item><title>MySQL Remote Database Transfers</title><description>&lt;p&gt;One-liner to remote copy a MySQL database over &lt;span class="caps"&gt;SSH&lt;/span&gt;:&lt;br/&gt;&lt;/p&gt;&lt;div class="code"&gt;&lt;pre&gt;&lt;code&gt;mysqldump [db] | ssh -C [host] 'mysql [db]'&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For anything more complicated there is also &lt;a href="http://adam.blog.heroku.com/past/2009/2/11/taps_for_easy_database_transfers/"&gt;taps&lt;/a&gt;.&lt;/p&gt;</description><link>http://blog.engledew.com/post/344116649</link><guid>http://blog.engledew.com/post/344116649</guid><pubDate>Mon, 09 Mar 2009 13:53:00 +0000</pubDate><category>admin</category></item></channel></rss>

