<?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>swag.dk</title>
	<atom:link href="http://swag.dk/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://swag.dk/blog</link>
	<description></description>
	<lastBuildDate>Thu, 04 Feb 2010 14:02:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Figuring out largest/smallest/median filesizes</title>
		<link>http://swag.dk/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/</link>
		<comments>http://swag.dk/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 14:00:52 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/</guid>
		<description><![CDATA[I had to get some statistics about file sizes today, but couldn&#8217;t really find a tool for the job, so naturally, I wrote one.


import os, sys, re
from os.path import join, getsize, exists
&#160;
def median&#40;numbers&#41;:
    s = sorted&#40;numbers&#41;
    l = len&#40;numbers&#41;
    if l % 2 == 0:
  [...]]]></description>
			<content:encoded><![CDATA[<p>I had to get some statistics about file sizes today, but couldn&#8217;t really find a tool for the job, so naturally, I wrote one.</p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">sys</span>, <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span> <span style="color: #ff7700;font-weight:bold;">import</span> join, getsize, exists
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> median<span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span>:
    s = <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span>
    l = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>numbers<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> l <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span> == <span style="color: #ff4500;">0</span>:
        a, b = s<span style="color: black;">&#91;</span>l / <span style="color: #ff4500;">2</span> - <span style="color: #ff4500;">1</span> : l / <span style="color: #ff4500;">2</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> a <span style="color: #66cc66;">!</span>= b:
            <span style="color: #ff7700;font-weight:bold;">return</span> a + b / <span style="color: #ff4500;">2.0</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> a
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> s<span style="color: black;">&#91;</span>l / <span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
&nbsp;
sizes = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
req_re = <span style="color: #008000;">None</span>
target = <span style="color: #483d8b;">'.'</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span>:
    target = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">3</span>:
    req_re = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> root, dirs, files <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">walk</span><span style="color: black;">&#40;</span>target<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">for</span> name <span style="color: #ff7700;font-weight:bold;">in</span> files:
        absp = join<span style="color: black;">&#40;</span>root, name<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> exists<span style="color: black;">&#40;</span>absp<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> req_re <span style="color: #ff7700;font-weight:bold;">or</span> req_re.<span style="color: black;">search</span><span style="color: black;">&#40;</span>absp<span style="color: black;">&#41;</span>:
                sizes.<span style="color: black;">append</span><span style="color: black;">&#40;</span>getsize<span style="color: black;">&#40;</span>absp<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
num = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span>
total = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Num files: %d&quot;</span> <span style="color: #66cc66;">%</span> num
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Average  : %0.2f KB&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>total / num<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024.0</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Median   : %0.2f KB&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>median<span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024.0</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Min      : %0.2f KB&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">min</span><span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024.0</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Max      : %0.2f KB&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #008000;">max</span><span style="color: black;">&#40;</span>sizes<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">1024.0</span><span style="color: black;">&#41;</span></pre></div></div>


<p>Usage should be self-explanatory.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AppleScript application to slow network</title>
		<link>http://swag.dk/blog/2009/09/15/applescript-application-to-slow-network/</link>
		<comments>http://swag.dk/blog/2009/09/15/applescript-application-to-slow-network/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 10:40:06 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=186</guid>
		<description><![CDATA[I keep doing the same ipfw commands over and over. Enough of that, here is my first applescript application every. Probably filled with bugs and other scary things, and I&#8217;m probably not the first one to do this, but I think I&#8217;m the first to stick the source out there.


property FLUSH_TEXT : &#34;Quit and flush&#34;
property [...]]]></description>
			<content:encoded><![CDATA[<p>I keep doing the same <code>ipfw</code> commands over and over. Enough of that, here is my first applescript application every. Probably filled with bugs and other scary things, and I&#8217;m probably not the first one to do this, but I think I&#8217;m the first to stick the source out there.</p>


<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #ff0033; font-weight: bold;">property</span> FLUSH_TEXT : <span style="color: #009900;">&quot;Quit and flush&quot;</span>
<span style="color: #ff0033; font-weight: bold;">property</span> SET_TEXT : <span style="color: #009900;">&quot;Set speed&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- be damn carefull what you input here, it will run as root</span>
<span style="color: #ff0033; font-weight: bold;">on</span> ipfwLimit<span style="color: #000000;">&#40;</span>bandwidth<span style="color: #000000;">&#41;</span>
	<span style="color: #ff0033; font-weight: bold;">my</span> ipfwFlush<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
	<span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;ipfw pipe 1 config bw &quot;</span> <span style="color: #000000;">&amp;</span> bandwidth <span style="color: #000000;">&amp;</span> <span style="color: #009900;">&quot;KB&quot;</span> <span style="color: #ff0033; font-weight: bold;">with</span> administrator privileges
	<span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;ipfw add 10 pipe 1 tcp from any 80 to me&quot;</span> <span style="color: #ff0033; font-weight: bold;">with</span> administrator privileges
	<span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;ipfw add 11 pipe 1 tcp from me to any 80&quot;</span> <span style="color: #ff0033; font-weight: bold;">with</span> administrator privileges
<span style="color: #ff0033; font-weight: bold;">end</span> ipfwLimit
&nbsp;
<span style="color: #808080; font-style: italic;">-- flush any ipfw rules</span>
<span style="color: #ff0033; font-weight: bold;">on</span> ipfwFlush<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #0066ff;">do shell script</span> <span style="color: #009900;">&quot;ipfw -f flush&quot;</span> <span style="color: #ff0033; font-weight: bold;">with</span> administrator privileges
<span style="color: #ff0033; font-weight: bold;">end</span> ipfwFlush
&nbsp;
<span style="color: #ff0033; font-weight: bold;">on</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> question <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">display dialog</span> <span style="color: #009900;">&quot;Control your http traffic speed&quot;</span> <span style="color: #0066ff;">buttons</span> <span style="color: #000000;">&#123;</span>FLUSH_TEXT, SET_TEXT<span style="color: #000000;">&#125;</span> default button <span style="color: #000000;">2</span>
	<span style="color: #ff0033; font-weight: bold;">set</span> answer <span style="color: #ff0033; font-weight: bold;">to</span> button returned <span style="color: #ff0033; font-weight: bold;">of</span> question
&nbsp;
	<span style="color: #ff0033; font-weight: bold;">if</span> answer <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">equal</span> <span style="color: #ff0033; font-weight: bold;">to</span> FLUSH_TEXT <span style="color: #ff0033; font-weight: bold;">then</span>
		<span style="color: #ff0033; font-weight: bold;">my</span> ipfwFlush<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
&nbsp;
	<span style="color: #ff0033; font-weight: bold;">if</span> answer <span style="color: #ff0033; font-weight: bold;">is</span> <span style="color: #ff0033;">equal</span> <span style="color: #ff0033; font-weight: bold;">to</span> SET_TEXT <span style="color: #ff0033; font-weight: bold;">then</span>
		<span style="color: #ff0033; font-weight: bold;">set</span> bandwidth_question <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">display dialog</span> <span style="color: #009900;">&quot;Enter bandwidth in KB/s (don't do something stupid like entering <span style="color: #000000; font-weight: bold;">\&quot;</span>; rm -rf /)&quot;</span> <span style="color: #0066ff;">default answer</span> <span style="color: #009900;">&quot;56&quot;</span>
		<span style="color: #ff0033; font-weight: bold;">set</span> bandwidth <span style="color: #ff0033; font-weight: bold;">to</span> <span style="color: #0066ff;">text</span> returned <span style="color: #ff0033; font-weight: bold;">of</span> bandwidth_question
&nbsp;
		<span style="color: #ff0033; font-weight: bold;">my</span> ipfwLimit<span style="color: #000000;">&#40;</span>bandwidth<span style="color: #000000;">&#41;</span>
&nbsp;
		<span style="color: #ff0033; font-weight: bold;">my</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">if</span>
<span style="color: #ff0033; font-weight: bold;">end</span> main
&nbsp;
<span style="color: #ff0033; font-weight: bold;">my</span> main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/09/15/applescript-application-to-slow-network/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building postgresql8x and psycopg2 for x86_64 and i386 on Snow Leopard (OS X 10.6)</title>
		<link>http://swag.dk/blog/2009/09/03/building-postgresql8x-x86_64-i386-snow-leopard/</link>
		<comments>http://swag.dk/blog/2009/09/03/building-postgresql8x-x86_64-i386-snow-leopard/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 09:42:30 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=171</guid>
		<description><![CDATA[I&#8217;ve recently installed Apple&#8217;s new 64 bit OS Snow Leopard, on my work computer. I use postgresql extensivly together with python, and usually use apple&#8217;s bundled python2.5 for working with django.

As the daredevil I am, I wanted to recompile all my macports to use the new 64 bit system, and therefore deleted them all, and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently installed Apple&#8217;s new 64 bit OS Snow Leopard, on my work computer. I use postgresql extensivly together with python, and usually use apple&#8217;s bundled python2.5 for working with django.</p>

<p>As the daredevil I am, I wanted to recompile all my macports to use the new 64 bit system, and therefore deleted them all, and made a fresh install of macports. After building the postgresql81 port, I was about to build the psycopg2 python postgresql driver for python 2.5, when it gave me a warning about not being able to find some symbols in the postgresql library it had linked to. I quickly realized that this might be an architecture problem, and sure enough, it turns out that python 2.5 is a i386/ppc and python 2.6 is x86_64/i386/ppc binary, as can be seen here:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> python<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python: Mach-O universal binary with <span style="color: #000000;">3</span> architectures
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> architecture x86_64<span style="color: #7a0874; font-weight: bold;">&#41;</span>:	Mach-O <span style="color: #000000;">64</span>-bit executable x86_64
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> architecture i386<span style="color: #7a0874; font-weight: bold;">&#41;</span>:	Mach-O executable i386
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> architecture ppc7400<span style="color: #7a0874; font-weight: bold;">&#41;</span>:	Mach-O executable ppc
$ <span style="color: #c20cb9; font-weight: bold;">file</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">which</span> python2.5<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python2.5: Mach-O universal binary with <span style="color: #000000;">2</span> architectures
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python2.5 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> architecture i386<span style="color: #7a0874; font-weight: bold;">&#41;</span>:	Mach-O executable i386
<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>python2.5 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> architecture ppc7400<span style="color: #7a0874; font-weight: bold;">&#41;</span>:	Mach-O executable ppc</pre></div></div>


<p>The solution seemed so simple. Recompile postgresql81 for both architectures, and let the linker figure out the rest.</p>

<p>Building the postgresql81 port as the +universal variant, does not work. It has something to do with the fact, that the linker (<code>ld</code>) does not know how to produce a binary for multiple architectures. After a good nights sleep, the solution was only a <a href="http://trac.macports.org/ticket/14619">trac ticket</a> away.</p>

<p>So, to build a i386 and x86_64 version of postgresql8x via macports, you have to patch the <code>Portfile</code>, which is located in <code>/opt/local/var/macports/sources/rsync.macports.org/release/ports/databases/postgresql81</code>.</p>

<p>That can be done like this &#8211; notice that the patch seem to place the files wrong, so we&#8217;re moving them as well:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">cd</span> <span style="color: #000000; font-weight: bold;">/</span>opt<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>macports<span style="color: #000000; font-weight: bold;">/</span>sources<span style="color: #000000; font-weight: bold;">/</span>rsync.macports.org<span style="color: #000000; font-weight: bold;">/</span>release<span style="color: #000000; font-weight: bold;">/</span>ports<span style="color: #000000; font-weight: bold;">/</span>databases<span style="color: #000000; font-weight: bold;">/</span>postgresql81
$ curl <span style="color: #660033;">-s</span> http:<span style="color: #000000; font-weight: bold;">//</span>trac.macports.org<span style="color: #000000; font-weight: bold;">/</span>raw-attachment<span style="color: #000000; font-weight: bold;">/</span>ticket<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">14619</span><span style="color: #000000; font-weight: bold;">/</span>combined_updated_universal.patch <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">patch</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mkdir</span> files<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> ld.sh files<span style="color: #000000; font-weight: bold;">/</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">mv</span> patch_pg_config_h files<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>


<p>Now you can go ahead and build the postgresql81 port with both architectures, like so:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> postgresql81 +universal</pre></div></div>


<p>And then, finally, we can build the psycopg2 extension for python:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>initd.org<span style="color: #000000; font-weight: bold;">/</span>pub<span style="color: #000000; font-weight: bold;">/</span>software<span style="color: #000000; font-weight: bold;">/</span>psycopg<span style="color: #000000; font-weight: bold;">/</span>psycopg2-2.0.12.tar.gz
$ <span style="color: #c20cb9; font-weight: bold;">tar</span> zxf psycopg2-2.0.12.tar.gz
$ <span style="color: #7a0874; font-weight: bold;">cd</span> psycopg2-2.0.12
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> python2.5 setup.py <span style="color: #c20cb9; font-weight: bold;">install</span>
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> python2.5 setup.py clean
$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> python2.6 setup.py <span style="color: #c20cb9; font-weight: bold;">install</span></pre></div></div>


<p>And you&#8217;re off.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/09/03/building-postgresql8x-x86_64-i386-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Managing multiple AWS identities</title>
		<link>http://swag.dk/blog/2009/07/22/managing-multiple-aws-identities/</link>
		<comments>http://swag.dk/blog/2009/07/22/managing-multiple-aws-identities/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 14:50:12 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=167</guid>
		<description><![CDATA[I&#8217;m running multiple different project on AWS which was so much of a pain to use, as I often find myself having to use the identity of project-a together with the official amazon ec2 tools.

To help myself manage the multiple identities, I wote a set of bash functions, called:


aws_load &#60;config-name&#62; &#8211; loads configuration from config-name
ec2ssh [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m running multiple different project on <a href="http://aws.amazon.com/">AWS</a> which was so much of a pain to use, as I often find myself having to use the identity of project-a together with the official amazon ec2 tools.</p>

<p>To help myself manage the multiple identities, I wote a set of bash functions, called:</p>

<ul>
<li><code>aws_load &lt;config-name&gt;</code> &#8211; loads configuration from config-name</li>
<li><code>ec2ssh &lt;instance-number-in-ec2din-list&gt;</code> &#8211; ssh&#8217;s into a given instance, with the root key</li>
<li><code>ec2scp</code> &#8211; a shorthand for scp -i &lt;keyfile&gt;
<!-- more --></li>
</ul>

<p>I keep the configuration files in the directory <code>~/amazon/conf/name.sh</code> and keypairs in <code>~/amazon/keypairs/</code> but that should be obvious to change.</p>

<p>To change or load an identity, one simply calls the function from a shell prompt like so:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mads<span style="color: #000000; font-weight: bold;">@</span>workmads ~ <span style="color: #000000; font-weight: bold;">%</span> aws_load some-identity
loaded certificate ...
loaded <span style="color: #000000; font-weight: bold;">/</span>Users<span style="color: #000000; font-weight: bold;">/</span>mads<span style="color: #000000; font-weight: bold;">/</span>amazon<span style="color: #000000; font-weight: bold;">/</span>conf<span style="color: #000000; font-weight: bold;">/</span>some-identity.sh <span style="color: #7a0874; font-weight: bold;">&#40;</span>...<span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>


<p>I hope someone finds this as useful as I do.</p>

<p>Functions (could be placed in <code>.bashrc</code> or <code>.zshrc</code>).</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> aws_load <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
		<span style="color: #007800;">ec2_configurations</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOME</span>/amazon/conf&quot;</span>
		<span style="color: #007800;">ec2_keys</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$HOME</span>/amazon/keypairs&quot;</span>
		<span style="color: #007800;">conf</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$ec2_configurations</span>/$1.sh&quot;</span>
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-x</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$conf</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
			<span style="color: #7a0874; font-weight: bold;">unset</span> AMAZON_ID AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_CERT EC2_PRIVATE_KEY EC2_CERT AWS_KEYPAIR_NAME
&nbsp;
			<span style="color: #7a0874; font-weight: bold;">source</span> <span style="color: #007800;">$conf</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$AWS_KEYPAIR_NAME</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
				<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_SSH_KEY</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$ec2_keys</span>/id_rsa_<span style="color: #007800;">${AWS_KEYPAIR_NAME}</span>-keypair&quot;</span>
			<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$AWS_CERT</span>&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
				<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">EC2_PRIVATE_KEY</span>=~<span style="color: #000000; font-weight: bold;">/</span>.ec2<span style="color: #000000; font-weight: bold;">/</span>pk-<span style="color: #007800;">$AWS_CERT</span>.pem
				<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">EC2_CERT</span>=~<span style="color: #000000; font-weight: bold;">/</span>.ec2<span style="color: #000000; font-weight: bold;">/</span>cert-<span style="color: #007800;">$AWS_CERT</span>.pem
&nbsp;
				<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;loaded certificate <span style="color: #007800;">$AWS_CERT</span>&quot;</span>
			<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
			<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;loaded <span style="color: #007800;">$conf</span> (<span style="color: #007800;">$AMAZON_ID</span>)&quot;</span>
		<span style="color: #000000; font-weight: bold;">else</span>
			<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;configuration <span style="color: #007800;">$conf</span> not found (or not executable)&quot;</span>
		<span style="color: #000000; font-weight: bold;">fi</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;usage: aws_load &lt;configuration name&gt;&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> ec2ssh <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-n</span> <span style="color: #ff0000;">&quot;$1&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
        <span style="color: #007800;">HOST</span>=<span style="color: #ff0000;">&quot;<span style="color: #780078;">`ec2din | awk '/i-/ {print $4}' | tail +$1 | head -n 1`</span>&quot;</span>
        <span style="color: #c20cb9; font-weight: bold;">ssh</span> <span style="color: #660033;">-i</span> <span style="color: #007800;">$AWS_SSH_KEY</span> <span style="color: #660033;">-l</span> root <span style="color: #800000;">${HOST}</span>
    <span style="color: #000000; font-weight: bold;">else</span>
        <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Please write a number&quot;</span>
    <span style="color: #000000; font-weight: bold;">fi</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> ec2scp <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #c20cb9; font-weight: bold;">scp</span> <span style="color: #660033;">-i</span> <span style="color: #007800;">$AWS_SSH_KEY</span> $<span style="color: #000000; font-weight: bold;">@</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>


<p>Configuration &#8220;file&#8221; template to be placed in <code>~/amazon/conf/&lt;config-name&gt;.sh</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AMAZON_ID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_ACCESS_KEY_ID</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_SECRET_ACCESS_KEY</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_CERT</span>=<span style="color: #ff0000;">&quot;&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">AWS_KEYPAIR_NAME</span>=<span style="color: #ff0000;">&quot;&quot;</span></pre></div></div>


<p>Happy identity switching.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/07/22/managing-multiple-aws-identities/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Detaching a running process on *nix (or how to make a process continue to run after logging out)</title>
		<link>http://swag.dk/blog/2009/07/16/detaching-a-process-on-nix/</link>
		<comments>http://swag.dk/blog/2009/07/16/detaching-a-process-on-nix/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 12:31:12 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=156</guid>
		<description><![CDATA[Today, I had to copy 70 GiB of data from a ext3 filesystem to a XFS filesystem. This involved a lot of small files. After a couple of hours of waiting, I thought it&#8217;d be best to just leave it running, and resume my activities the day after. But oh nooo, I forgot to run [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I had to copy 70 GiB of data from a ext3 filesystem to a XFS filesystem. This involved a lot of small files. After a couple of hours of waiting, I thought it&#8217;d be best to just leave it running, and resume my activities the day after. But <a href="http://www.youtube.com/watch?v=rks-Cr3Fr_k">oh nooo</a>, I forgot to run it in a <a href="http://www.gnu.org/software/screen/">screen</a>.<span id="more-156"></span></p>

<p>Detaching a running process from your shell, proved to be very easy in deed, using a shell command called <a href="http://www.google.com/search?q=disown+linux">disown</a>.</p>

<p>To disown a running process, start by suspending the process by pressing <code>^Z</code>, then send it to the background with the command <code>bg</code> <em>then</em> you can make it run until it&#8217;s done by using the <code>disown</code> command. To access the processes you&#8217;ve got suspended, you can use the <code>%</code> prefix (<code>%1</code> for the first, <code>%2</code> for the second etc.). To get a list of running processes you can use the <code>jobs</code> command.</p>

<p>Example:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mads<span style="color: #000000; font-weight: bold;">@</span>workmads ~ <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">120</span>
^Z
<span style="color: #c20cb9; font-weight: bold;">zsh</span>: suspended  <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">120</span>
mads<span style="color: #000000; font-weight: bold;">@</span>workmads ~ <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">bg</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>  + continued  <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">120</span>
mads<span style="color: #000000; font-weight: bold;">@</span>workmads ~ <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #7a0874; font-weight: bold;">disown</span> <span style="color: #000000; font-weight: bold;">%</span>1
&nbsp;
mads<span style="color: #000000; font-weight: bold;">@</span>workmads ~ <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">ps</span> xawu<span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span>
mads     <span style="color: #000000;">31725</span>   <span style="color: #000000;">0.0</span>  <span style="color: #000000;">0.0</span>    <span style="color: #000000;">75332</span>    <span style="color: #000000;">304</span> s003  S     <span style="color: #000000;">2</span>:25PM   <span style="color: #000000;">0</span>:<span style="color: #000000;">00.00</span> <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">120</span></pre></div></div>


<p>If you accedently didn&#8217;t background your process, you can tell it to resume by sending it a <code>SIGCONT</code> signal with <code>kill -SIGCONT &lt;pid&gt;</code>.</p>

<p>Of course, this is no replacement for <code>nohup</code> or <code>screen</code>, but I think it is a great supplement.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/07/16/detaching-a-process-on-nix/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Poormans cloudfront with EC2 and varnish</title>
		<link>http://swag.dk/blog/2009/06/30/poormans-cloudfront-with-ec2-and-varnish/</link>
		<comments>http://swag.dk/blog/2009/06/30/poormans-cloudfront-with-ec2-and-varnish/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 08:51:33 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Work]]></category>
		<category><![CDATA[amazon aws]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[cloudfront]]></category>
		<category><![CDATA[varnish]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=152</guid>
		<description><![CDATA[Recently (10-20 minutes ago), amazon couldfront (a cdn) stopped sending dns replies in europe:


% dig -t ns cloudfront.net

;  DiG 9.4.3-P1  -t ns cloudfront.net
;; global options:  printcmd
;; connection timed out; no servers could be reached


I was going to do a guide to set up a varnish to replace cloudfront temporarily (and did actually [...]]]></description>
			<content:encoded><![CDATA[<p>Recently (10-20 minutes ago), amazon couldfront (a cdn) stopped sending dns replies in europe:</p>

<pre>
% dig -t ns cloudfront.net

; <<>> DiG 9.4.3-P1 <<>> -t ns cloudfront.net
;; global options:  printcmd
;; connection timed out; no servers could be reached
</pre>

<p>I was going to do a guide to set up a varnish to replace cloudfront temporarily (and did actually set up the instance, and software &#8211; I might do the guide and ami anyway) when I realized, that I (as well as most other people) can just change the relevant url to point to the S3 bucket. Problem solved. That will, however, not be as fast as either cloudfront itself, or a varnish cached backend.</p>

<p>Should anyone be interested in how varnish is setup to handle failures from cloudfront, I&#8217;ll happily do an ami.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/06/30/poormans-cloudfront-with-ec2-and-varnish/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Django &#8211; sharing a memcached instance</title>
		<link>http://swag.dk/blog/2009/06/23/django-sharing-a-memcached-instance/</link>
		<comments>http://swag.dk/blog/2009/06/23/django-sharing-a-memcached-instance/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 11:23:00 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Django]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[prefix]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=135</guid>
		<description><![CDATA[Until recently I&#8217;ve been using the file:// django cache, but that has a &#8220;problem&#8221; when multiple users needs to manipulate the cache (think uid 80 writes a key, that uid 1000 wants to delete).

My problem with the memcached:// django cache provider has been, that it cannot handle being used on a shared memcached instance, because [...]]]></description>
			<content:encoded><![CDATA[<p>Until recently I&#8217;ve been using the <code>file://</code> django cache, but that has a &#8220;problem&#8221; when multiple users needs to manipulate the cache (think uid 80 writes a key, that uid 1000 wants to delete).</p>

<p>My problem with the <code>memcached://</code> django cache provider has been, that it cannot handle being used on a shared memcached instance, because of the danger of key collissions.</p>

<p><span id="more-135"></span>
If project A and project B would share a memcached instance, they basiclly share the same global namespace. So if they both write a key called <code>actor</code> there is no telling what will happen.</p>

<p>So I wrote a little cache backend for django, that uses the current <code>memcached</code> backend, but adds a pre-defined prefix to all keys.</p>

<p><strong>Usage:</strong>
Put the code somewhere inside your project in a file called <code>memcached&#95;key&#95;prefix.py</code>, and set your <code>CACHE&#95;BACKEND</code> to something like: <code>path.to.memcached&#95;key&#95;prefix:///127.0.0.1:11211/?key&#95;prefix=sewc&#95;&amp;foo=bar&amp;timeout=3600</code></p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;Memcached cache backend with key prefixing&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">cache</span>.<span style="color: black;">backends</span>.<span style="color: black;">base</span> <span style="color: #ff7700;font-weight:bold;">import</span> InvalidCacheBackendError
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">cache</span>.<span style="color: black;">backends</span>.<span style="color: black;">memcached</span> <span style="color: #ff7700;font-weight:bold;">import</span> CacheClass <span style="color: #ff7700;font-weight:bold;">as</span> MemcachedCacheClass
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">utils</span>.<span style="color: black;">encoding</span> <span style="color: #ff7700;font-weight:bold;">import</span> smart_unicode, smart_str
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CacheClass<span style="color: black;">&#40;</span>MemcachedCacheClass<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, server, params<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #008000;">self</span>._key_prefix = smart_str<span style="color: black;">&#40;</span>params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'key_prefix'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyError</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span> InvalidCacheBackendError<span style="color: black;">&#40;</span><span style="color: #483d8b;">'key_prefix not specified'</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span>server, params<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _get_key<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._key_prefix + smart_str<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> add<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, value, timeout=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>, value, timeout<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, default=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>, default<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, value, timeout=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>, value, timeout<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> delete<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">delete</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get_many<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, keys<span style="color: black;">&#41;</span>:
        keys = <span style="color: black;">&#91;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> key <span style="color: #ff7700;font-weight:bold;">in</span> keys<span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">get_many</span><span style="color: black;">&#40;</span>keys<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> incr<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, delta=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">incr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>, delta<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> decr<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, delta=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>CacheClass, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: black;">decr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._get_key<span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>, delta<span style="color: black;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/06/23/django-sharing-a-memcached-instance/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Keeping ssh connections alive</title>
		<link>http://swag.dk/blog/2009/06/12/keeping-ssh-connections-alive/</link>
		<comments>http://swag.dk/blog/2009/06/12/keeping-ssh-connections-alive/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 17:24:16 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[EC2]]></category>
		<category><![CDATA[SSH]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/2009/06/12/keeping-ssh-connections-alive/</guid>
		<description><![CDATA[I&#8217;ve got nothing more to say than:


mads@workmads ~ % cat .ssh/config 
ServerAliveInterval 60


Happy ssh&#8217;ing.
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got nothing more to say than:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mads<span style="color: #000000; font-weight: bold;">@</span>workmads ~ <span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> .ssh<span style="color: #000000; font-weight: bold;">/</span>config 
ServerAliveInterval <span style="color: #000000;">60</span></pre></div></div>


<p>Happy ssh&#8217;ing.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/06/12/keeping-ssh-connections-alive/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python http_head method</title>
		<link>http://swag.dk/blog/2009/05/15/python-http_head-method/</link>
		<comments>http://swag.dk/blog/2009/05/15/python-http_head-method/#comments</comments>
		<pubDate>Fri, 15 May 2009 10:30:28 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[HEAD]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/2009/05/15/python-http_head-method/</guid>
		<description><![CDATA[Seeing as there is no really easy way to do a HTTP HEAD request from python, I wrote up the following small method:

In advance I&#8217;d like to apologize for the method that assemblies the request path.

Update: Added handling of redirects.


def http_head&#40;url&#41;:
    import httplib
    import urlparse
&#160;
    redirects [...]]]></description>
			<content:encoded><![CDATA[<p>Seeing as there is no really easy way to do a HTTP <code>HEAD</code> request from python, I wrote up the following small method:</p>

<p>In advance I&#8217;d like to apologize for the method that assemblies the request path.</p>

<p><strong>Update:</strong> <em>Added handling of redirects.</em></p>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> http_head<span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">httplib</span>
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urlparse</span>
&nbsp;
    redirects = <span style="color: #ff4500;">0</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> redirects <span style="color: #66cc66;">&lt;</span> <span style="color: #ff4500;">10</span>:
        scheme, netloc, path, query, fragment = <span style="color: #dc143c;">urlparse</span>.<span style="color: black;">urlsplit</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> scheme == <span style="color: #483d8b;">'https'</span>:
            conn = <span style="color: #dc143c;">httplib</span>.<span style="color: black;">HTTPSConnection</span><span style="color: black;">&#40;</span>netloc<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            conn = <span style="color: #dc143c;">httplib</span>.<span style="color: black;">HTTPConnection</span><span style="color: black;">&#40;</span>netloc<span style="color: black;">&#41;</span>
&nbsp;
        conn.<span style="color: black;">request</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;HEAD&quot;</span>, <span style="color: #483d8b;">&quot;%s%s%s%s%s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>path, query <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #483d8b;">&quot;?&quot;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">&quot;&quot;</span>, query, 
                                             fragment <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #483d8b;">&quot;#&quot;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #483d8b;">&quot;&quot;</span>, fragment<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        res = conn.<span style="color: black;">getresponse</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> res.<span style="color: black;">status</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span><span style="color: #ff4500;">301</span>, <span style="color: #ff4500;">302</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> res.<span style="color: black;">getheader</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'location'</span><span style="color: black;">&#41;</span>:
            url = res.<span style="color: black;">getheader</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'location'</span><span style="color: black;">&#41;</span>
            redirects += <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> res.<span style="color: black;">status</span>, res.<span style="color: black;">reason</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/05/15/python-http_head-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Formatting xml with xmllint</title>
		<link>http://swag.dk/blog/2009/05/13/formatting-xml-with-xmllint/</link>
		<comments>http://swag.dk/blog/2009/05/13/formatting-xml-with-xmllint/#comments</comments>
		<pubDate>Wed, 13 May 2009 13:11:48 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/2009/05/13/formatting-xml-with-xmllint/</guid>
		<description><![CDATA[I keep forgetting how to format and indent xml from the command line. The tool xmllint does a fine job of doing just that, which has saved me numerous times whilst working with sports results. So. Much. Data.

Running


xmllint --format &#60;file&#62;


will re-format and re-indent the xml in the input file, and check it for various errors [...]]]></description>
			<content:encoded><![CDATA[<p>I keep forgetting how to format and indent xml from the command line. The tool <code>xmllint</code> does a fine job of doing just that, which has saved me numerous times whilst working with sports results. So. Much. Data.</p>

<p>Running</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">xmllint <span style="color: #660033;">--format</span> <span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>


<p>will re-format and re-indent the xml in the input file, and check it for various errors while doing it.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/05/13/formatting-xml-with-xmllint/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
