<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Matthijs de Jonge &#187; Tech</title>
	<atom:link href="http://matthijsdejonge.nl/category/tech/feed/" rel="self" type="application/rss+xml" />
	<link>http://matthijsdejonge.nl</link>
	<description>de persoonlijke website van matthijs de jonge</description>
	<lastBuildDate>Sat, 12 Jun 2010 21:46:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language></language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>When cleverness obscures your intentions</title>
		<link>http://matthijsdejonge.nl/2008/12/16/when-cleverness-obscures-your-intentions/</link>
		<comments>http://matthijsdejonge.nl/2008/12/16/when-cleverness-obscures-your-intentions/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 08:38:22 +0000</pubDate>
		<dc:creator>matthijs</dc:creator>
				<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://tech.rommelhok.com/?p=31</guid>
		<description><![CDATA[Generally speaking, writing code as a series of clever oneliners is a terrible idea. Elegant though they may seem, oneliners often (*) obscure the thing the code is actually trying to accomplish. Conventional wisdom therefore has it that oneliners should be avoided as they make it unnecessarily difficult for future maintainers to make changes to [...]]]></description>
			<content:encoded><![CDATA[<p>Generally speaking, writing code as a series of clever oneliners is a terrible idea. Elegant though they may seem, oneliners often (*) obscure the thing the code is actually trying to accomplish. Conventional wisdom therefore has it that oneliners should be avoided as they make it unnecessarily difficult for future maintainers to make changes to the code.</p>
<p>However, clever oneliners also make life difficult for the developer of the code <em>while the code is being written</em>. The reason is, again, that they obscure the thing the code is actually trying to accomplish. Consider this real life example:</p>
<pre>
my $unprocessedRecords = [$data-&gt;{'records'}] unless (ref($data-&gt;{'records'}) eq 'ARRAY');
</pre>
<p>What <em>should</em> happen here is that a reference to the contents of a data element called &#8220;records&#8221; goes into a variable called $unprocessedRecords. Owing to a quirk of the mechanism that created $data, however, $data-&gt;{&#8217;records&#8217;} may be either a reference to an array of records or to a single record, which is a hash. The developer, correctly, decided to make life easier for himself by ensuring $unprocessedRecords would always be a reference to an array, and since the developer in question (not me, though it could easily have been), wrote this oneliner to accomplish this. </p>
<p>What happened, therefore, was that several steps further on in the code, $unprocessedRecords turned out to contain no records at all and it took the developer almost half an hour worth of debugging for him to find and correct this mistake.</p>
<p>Now there is, of course, no shame, in producing bugs and taking a long time to find them. However, this particular bug (only if $data contained exactly one record would $unprocessedRecords contain records) would have easily been avoided had the developer written the code to explicitly match his intentions, like so:</p>
<pre>
my $unprocessedRecords = $data-&gt;{'records'};
$unprocessedRecords = [$unprocessedRecords] unless (ref($unprocessedRecords) eq 'ARRAY');
</pre>
<p>The lesson here is that your code should always match your intentions, even if it takes up more vertical space.</p>
<p>(*) except when they <a href="/2008/02/04/functional-programming-in-perl/">don&#8217;t</a></p>
]]></content:encoded>
			<wfw:commentRss>http://matthijsdejonge.nl/2008/12/16/when-cleverness-obscures-your-intentions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Safari, MediaWiki and NSURLErrorDomain: -1015</title>
		<link>http://matthijsdejonge.nl/2008/08/04/safari-mediawiki-and-nsurlerrordomain-1015/</link>
		<comments>http://matthijsdejonge.nl/2008/08/04/safari-mediawiki-and-nsurlerrordomain-1015/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 20:18:58 +0000</pubDate>
		<dc:creator>matthijs</dc:creator>
				<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://tech.rommelhok.com/2008/08/04/safari-mediawiki-and-nsurlerrordomain-1015/</guid>
		<description><![CDATA[For reasons that don&#8217;t matter in the least I needed, yes, really and truly needed, to open a mediawiki-driven site in Safari and when I tried to do so, I was greeted by an unsightly error calling itself &#8220;NSURLErrorDomain: -1015&#8243; that complained &#8220;cannot decode raw data&#8221;.
Ouch.
Searching Google, I found nothing useful except for this thread [...]]]></description>
			<content:encoded><![CDATA[<p>For reasons that don&#8217;t matter in the least I needed, yes, really and truly needed, to open a <a href="http://www.mediawiki.org">mediawiki</a>-driven site in Safari and when I tried to do so, I was greeted by an unsightly error calling itself &#8220;NSURLErrorDomain: -1015&#8243; that complained &#8220;cannot decode raw data&#8221;.</p>
<p>Ouch.</p>
<p>Searching Google, I found nothing useful except for <a href="http://discussions.apple.com/thread.jspa?messageID=7685774">this</a> thread on the Apple support forums where amongst the dozens of rightfully irate screamers there were two that suggested gzip compression on the web server was to blame (well, not really to blame, of course. every single browser in the world is absolutely fine with gzip compression except for Safari, so by rights it&#8217;s Safari that&#8217;s to blame and not the compression, but you get the point). And luckily the mediawiki site in question was running on my own server, so I could disable the offending gzip compression quite easily. Of course I couldn&#8217;t remember ever having turned <em>on</em> gzip compression, but after some grepping I managed to unearth the following foul, foul line in in includes/DefaultSettings.php:</p>
<pre>
$wgDisableOutputCompression = false;
</pre>
<p>Added a line to LocalSettings.php that explicitly set it to &#8220;true&#8221; and presto, Safari displayed the website without any problems at all and I was once again the happy and fulfilled little tinkerer I usually am.</p>
<p>The question, of course, is why Apple thinks it can pass off a browser as &#8220;just working&#8221; that chokes so violently on something as common as server side gzip encoding. <em>Especially</em> in the mobile browsing market that they&#8217;re so interested in acquiring a substantial share of and where megabytes transferred are quite literally worth their weight in gold.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthijsdejonge.nl/2008/08/04/safari-mediawiki-and-nsurlerrordomain-1015/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Forcing MySQL to output UTF-8 in Python</title>
		<link>http://matthijsdejonge.nl/2008/07/31/forcing-mysql-to-output-utf-8-in-python/</link>
		<comments>http://matthijsdejonge.nl/2008/07/31/forcing-mysql-to-output-utf-8-in-python/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 13:10:48 +0000</pubDate>
		<dc:creator>matthijs</dc:creator>
				<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://tech.rommelhok.com/2008/07/31/forcing-mysql-to-output-utf-8-in-python/</guid>
		<description><![CDATA[No matter how you&#8217;ve defined your tables (as containing UTF-8 data only, for example), the MySQLdb module that takes care of interacting with a MySQL database from Python simply refuses to return your data as proper UTF-8. Since it took me nearly an hour (and most of my hair) to figure out how to force [...]]]></description>
			<content:encoded><![CDATA[<p>No matter how you&#8217;ve defined your tables (as containing UTF-8 data only, for example), the MySQLdb module that takes care of interacting with a MySQL database from Python simply refuses to return your data as proper UTF-8. Since it took me nearly an hour (and most of my hair) to figure out how to force it to do so anyway and I care enough to have others avoid the same fate, here&#8217;s what I learned:</p>
<p>Make the connection:</p>
<pre>
  connection = MySQLdb.connect(host = dbhost, user=dbuser, passwd=dbpass, db=dbname)
</pre>
<p>And before you do anything else, run a query that forces MySQL to output UTF-8 from now on:</p>
<pre>
  cursor = connection.cursor(MySQLdb.cursors.DictCursor) # or some other cursor type
  cursor.execute('SET character_set_results="utf8"')
</pre>
<p>Once you&#8217;ve done this, all your subsequent queries will return your data in proper UTF-8 encoding.</p>
<p>The fact that you have to run a query suggests that it&#8217;s not Python or MySQLdb that&#8217;s to blame, by the way. Not that I particularly care, of course. I just want it to work.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthijsdejonge.nl/2008/07/31/forcing-mysql-to-output-utf-8-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamically loading modules and classes in Python</title>
		<link>http://matthijsdejonge.nl/2008/07/29/dynamically-loading-modules-and-classes-in-python/</link>
		<comments>http://matthijsdejonge.nl/2008/07/29/dynamically-loading-modules-and-classes-in-python/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 16:11:14 +0000</pubDate>
		<dc:creator>matthijs</dc:creator>
				<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://tech.rommelhok.com/2008/07/29/dynamically-loading-modules-and-classes-in-python/</guid>
		<description><![CDATA[For some reason (a database abstraction layer that would call a specific engine to do the actual work, if you must know) I needed to find a way to dynamically load modules and classes in Python. In other words, I needed code like this:

  import db
  dbObj = db.DB(type='mysql', someparameters)

to return an instantiation [...]]]></description>
			<content:encoded><![CDATA[<p>For some reason (a database abstraction layer that would call a specific engine to do the actual work, if you must know) I needed to find a way to dynamically load modules and classes in Python. In other words, I needed code like this:</p>
<pre>
  import db
  dbObj = db.DB(type='mysql', someparameters)
</pre>
<p>to return an instantiation of the DB class in the package db.engines.mysql.</p>
<p>After reading through the documentation and some Googling, I figured out how to do it. Let&#8217;s say we have the following files:</p>
<p>In db/__init__.py :</p>
<pre>
  class DB(object):
    __init__(self, dbtype, config):
      module_name = 'db.engines.' + dbtype
      # before python 2.5, __import__ had no named parameters. the non-empty list (fromlist) ensures we
      # actually get the mysql engine object, not the db object
      module_obj = __import__(module_name, globals(), locals(), [1])
      class_obj = getattr(module_obj, 'DBEngine')
      dbobject = class_obj(config)
      self.dbobject = dbobject
      # do whatever else you need to do to initialize a generic DB object
</pre>
<p>And in db/engines/mysql.py :</p>
<pre>
  class DBEngine(object):

      def __init__(self, config):
        # do whatever you need to do to initialize a mysql db engine object
        pass
</pre>
<p>In other words, you need to use the __import__ function to dynamically load the module you&#8217;re after (in this case db.engines.mysql.py) and then the getattr function to dynamically load a specific class from that module. Easy as pie, actually.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthijsdejonge.nl/2008/07/29/dynamically-loading-modules-and-classes-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Posterous Is Wicked Cool</title>
		<link>http://matthijsdejonge.nl/2008/06/29/posterous-is-wicked-cool/</link>
		<comments>http://matthijsdejonge.nl/2008/06/29/posterous-is-wicked-cool/#comments</comments>
		<pubDate>Sun, 29 Jun 2008 11:27:25 +0000</pubDate>
		<dc:creator>matthijs</dc:creator>
				<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://tech.rommelhok.com/2008/06/29/posterous-is-wicked-cool/</guid>
		<description><![CDATA[Strangely enough, there is actually some substance to today&#8217;s hype in web 2.0 land (microblogging service Posterous). This substance is not in what it offers (try Tumbler or  for other, more established microblogging services) but in how it offers it. Creating a blog in Posterous is as simple as sending an email to post@posterous.com. [...]]]></description>
			<content:encoded><![CDATA[<p>Strangely enough, there is actually some substance to today&#8217;s hype in web 2.0 land (microblogging service <a href="http://posterous.com">Posterous</a>). This substance is not in <em>what</em> it offers (try <a href="http://tumblr.com">Tumbler</a> or <a href="http://twitter.com"></a> for other, more established microblogging services) but in <em>how</em> it offers it. Creating a blog in Posterous is as simple as sending an email to <a href="mailto:post@posterous.com">post@posterous.com</a>. Your message will automatically appear on http://somenamethatlookslikeyouremailaddres.posterous.com. The subject line of your message will be the title.</p>
<p>That&#8217;s it.</p>
<p>To really make it work well, however, you&#8217;ll want to register at the site. This allows you to set a decent sub domain name (the automatically generated ones are less than fantastic &#8211; mine was http://matthijs_auiyk.posterous.com/ , for example) but, even better, it also allows you to receive replies to your blog posts in your mail. If you reply to these, the results will automatically turn into new blog posts. This way you can have a conversation online using a tool you&#8217;re already using anyway (your email program). </p>
<p>An idea so simple it is, even though I personally have no need for a service like this, genius.</p>
]]></content:encoded>
			<wfw:commentRss>http://matthijsdejonge.nl/2008/06/29/posterous-is-wicked-cool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->