<?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 &#187; Python</title>
	<atom:link href="http://swag.dk/blog/category/programming/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://swag.dk/blog</link>
	<description></description>
	<lastBuildDate>Wed, 21 Jul 2010 12:34:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<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 &#8230; <a href="http://swag.dk/blog/2009/07/22/managing-multiple-aws-identities/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></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>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[Update: Some Curious User brought to my attention, that a ticket has been opened which, when implemented, will add a setting for a cache prefix. It will also allow other cache key manipulations. Until recently I&#8217;ve been using the file:// &#8230; <a href="http://swag.dk/blog/2009/06/23/django-sharing-a-memcached-instance/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong>
Some <a href="http://swag.dk/blog/2009/06/23/django-sharing-a-memcached-instance/comment-page-1/#comment-812">Curious User</a> brought to my attention, that <a href="http://code.djangoproject.com/ticket/13795">a ticket</a> has been opened which, when implemented, will add a setting for a cache prefix. It will also allow other cache key manipulations.</p>

<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>6</slash:comments>
		</item>
		<item>
		<title>Django compatible PyAMF test client</title>
		<link>http://swag.dk/blog/2009/04/21/django-compatible-pyamf-test-client/</link>
		<comments>http://swag.dk/blog/2009/04/21/django-compatible-pyamf-test-client/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 13:18:19 +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[flash]]></category>
		<category><![CDATA[Client]]></category>
		<category><![CDATA[PyAMF]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=92</guid>
		<description><![CDATA[While working on a project using PyAMF today, i was about to write a unittest of a service method, when I realized that it would be harder than necessary to make unittests of the service. The cause of this is, &#8230; <a href="http://swag.dk/blog/2009/04/21/django-compatible-pyamf-test-client/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While working on a project using <a href="http://pyamf.org/">PyAMF</a> today, i was about to write a unittest of a service method, when I realized that it would be harder than necessary to make unittests of the service. The cause of this is, that there was no test client wrapper for the django test client for pyamf, which means, that to do a unittest of the gateway, you&#8217;ll have to start a django server, and run a unittest elsewhere.</p>

<p>Seeing as how there are a lot of benefits to using djangos own test suite (fixtures and automatic database generation to name a few), i set out to write a little test client for PyAMF to utilize django&#8217;s test client, so it would be possible to write a proper test suite.</p>

<p>This turned out great, and is now <a href="http://pyamf.org/ticket/508">ticket 508</a> over at PyAMFs trac. Look at the <a href="http://pyamf.org/attachment/ticket/508/client.py">client.py</a> for the code. At some point, it will be integrated into PyAMF mainline as <code>p.r.c.django.TestClient</code> (or something like that).</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2009/04/21/django-compatible-pyamf-test-client/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mercurial trac commit hook</title>
		<link>http://swag.dk/blog/2008/10/13/mercurial-trac-commit-hook/</link>
		<comments>http://swag.dk/blog/2008/10/13/mercurial-trac-commit-hook/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 15:03:50 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Ramblings]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/?p=56</guid>
		<description><![CDATA[Having searched a lot around google, it does not seem that anyone has published their trac commit hooks for mercurial. Since I had to use just that, I&#8217;ve cooked up a little hook which is based on the hook from &#8230; <a href="http://swag.dk/blog/2008/10/13/mercurial-trac-commit-hook/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Having searched a lot around google, it does not seem that anyone has published their <a href="http://trac.edgewall.org/">trac</a> commit hooks for <a href="http://www.selenic.com/mercurial/">mercurial</a>. Since I had to use just that, I&#8217;ve cooked up a little hook which is based on the hook from the <code>timingandestimationplugin</code>. I&#8217;ve created it as a changegroup hook, and it&#8217;s probably filled with bugs, but it seems to work and it catches the <code>fixes #42</code> and such.</p>

<p>To use the hook you must place it somewhere inside your <code>PYTHONPATH</code> and tell mercurial to use it (I placed it in a module called trachook &#8212; don&#8217;t call your module trac):</p>

<pre linenumbers="1">
[hooks]
changegroup = python:trachook.hook
</pre>

<p>And tell the hook where to find your trac installation:</p>

<pre linenumbers="1">
[trac-hook]
root = /path/to/trac
url = http://url/to/trac
</pre>

<p>Grab your own copy of the <a href="http://www.bitbucket.org/madssj/mercurial-trac-hook/src/">source</a>, and happy coding. And a big thank you to <a href="http://noehr.org/">Jesper Nøhr</a>.</p>

<p>Also, if you need a place to host your mercurial repository but don&#8217;t wan&#8217;t to set it up yourself, check out <a href="http://www.bitbucket.org/">bitbucket</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2008/10/13/mercurial-trac-commit-hook/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Fun with mod_macro and django</title>
		<link>http://swag.dk/blog/2008/03/19/fun-with-mod_macro/</link>
		<comments>http://swag.dk/blog/2008/03/19/fun-with-mod_macro/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 19:36:06 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[coniuro aps]]></category>
		<category><![CDATA[mod_macro]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/2008/03/19/fun-with-mod_macro/</guid>
		<description><![CDATA[At work we yestoday decided to update our internal url structure for our client test sites, issue management systems and such arround a bit. Seeing as we decided on purchasing a genuine signed ssl wildcard certificate, we needed to change &#8230; <a href="http://swag.dk/blog/2008/03/19/fun-with-mod_macro/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://www.coniuro.dk/" title="Coniuro ApS - we code">work</a> we yestoday decided to update our internal url structure for our client test sites, issue management systems and such arround a bit. Seeing as we decided on purchasing a genuine signed ssl wildcard certificate, we needed to change our url&#8217;s a bit.</p>

<p><span id="more-12"></span></p>

<p>We used to have a url schema consisting of the following components:</p>

<pre><code>    [dev.|test.]&lt;project-name&gt;.&lt;client-name&gt;.companyname.tld
</code></pre>

<p>That made for very long url&#8217;s, and furthermore, the url&#8217;s would not be supported by a <code>*.companyname.tld</code>. Based on this infomation, and wanting to create a more generelized, nicer url schema, we choose to cross over to the following schema:</p>

<pre><code>   [dev-|test-]&lt;project-name&gt;.companyname.tld
</code></pre>

<p>Which besides from supporting a wildcard ssl certificate just fine, just, well, looks nicer.</p>

<p>Either way, me beeing in charge of the contents in <code>/etc/</code>, and getting a little sick of growing apache configuration files, I chooose to write some macros to configure our project sites.</p>

<p>We generally have 2 types of project sites. <a href="http://www.djangoproject.com/" title="Django - a python web framework for very rapid development, love it!">django</a> based sites, and PHP based sites.</p>

<p>A typical django apache configuration consists of a VirtualHost and some mod_python settings inside a Location block. Lot&#8217;s of configuration, very little actual diffrence between the configration projects in between.</p>

<p>An example of a django based application running inside a apache VirtualHost, could look like this:</p>

<pre><code>    &lt;VirtualHost *:80&gt;
        ServerName test-project.company.tld
        &lt;Location /&gt;
            SetHandler python-program
            PythonHandler django.core.handlers.modpython
            PythonPath "['/path/to/project', '/other/path/to/inject']"
            SetEnv DJANGO_SETTINGS_MODULE project.settings
        &lt;/Location&gt;
        &lt;Location /media/&gt;
            SetHandler none
        &lt;/Location&gt;
        Alias /media/admin/ /path/to/django/contrib/admin/media/
        Alias /media/ /path/to/project/media/
    &lt;/VirtualHost&gt;
</code></pre>

<p>And the on top of that comes SSL configuration, auth and such. 25 lines of configuration per django site. Now, I really wanted to acomplish two things.</p>

<ul>
<li>Make the configuration easier to maintain</li>
<li>Enable other users to setup sites without knowing the depths of django and <code>mod_python</code></li>
</ul>

<p>Now, apache configuration, meet <a href="http://www.cri.ensmp.fr/~coelho/mod_macro/" title="mod_macro - a configuration macro module for apache2">mod_macro</a>. The solution to my problem was very simple. Create a macro that handles all the django configuration, given 4 parameters. A server name (i.e. the url), the parent directory of the django site and the module name of the site.</p>

<p>So i started building my macro.</p>

<pre><code>    &lt;Macro DjangoSite $servername $root $module&gt;
        &lt;VirtualHost *:80&gt;
            ServerName $servername
            &lt;Location /&gt;
                SetHandler python-program
                PythonHandler django.core.handlers.modpython
                PythonPath "['/common/path/to/inject', '$root']"
                SetEnv DJANGO_SETTINGS_MODULE $modulename.settings
            &lt;/Location&gt;
            &lt;Location /media/&gt;
                SetHandler none
            &lt;/Location&gt;
            Alias /media/admin/ /path/to/django/contrib/admin/media/
            Alias /media/ $root/$module/media/
        &lt;/VirtualHost&gt;
    &lt;/Macro&gt;
</code></pre>

<p><code>mod_macro</code> is really simple, it will do a longest-match search and replace on the macro&#8217;s content. So, if we had a django site in <code>/path/to/djangosite/testsite/</code> the above macro could be used as:</p>

<pre><code>    Use DjangoSite www.example.org /path/to/djangosite/ testsite
</code></pre>

<p>Which would expand to:</p>

<pre><code>    &lt;VirtualHost *:80&gt;
        ServerName www.example.org
        &lt;Location /&gt;
            SetHandler python-program
            PythonHandler django.core.handlers.modpython
            PythonPath "['/common/path/to/inject', '/path/to/djangosite/']"
            SetEnv DJANGO_SETTINGS_MODULE testsite.settings
        &lt;/Location&gt;
        &lt;Location /media/&gt;
            SetHandler none
        &lt;/Location&gt;
        Alias /media/admin/ /path/to/django/contrib/admin/media/
        Alias /media/ /path/to/djangosite/testsite/media/
    &lt;/VirtualHost&gt;
</code></pre>

<p>Hope you find this infomation a little useful. I actually like looking at our apache configuration file now.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2008/03/19/fun-with-mod_macro/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Asynchronous socket server in python</title>
		<link>http://swag.dk/blog/2008/02/21/asynchronous-socket-server-in-python/</link>
		<comments>http://swag.dk/blog/2008/02/21/asynchronous-socket-server-in-python/#comments</comments>
		<pubDate>Thu, 21 Feb 2008 14:39:52 +0000</pubDate>
		<dc:creator>Mads Sülau Jørgensen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[asynchat]]></category>
		<category><![CDATA[asyncore]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[socket]]></category>

		<guid isPermaLink="false">http://swag.dk/blog/2008/02/21/asynchronous-socket-server-in-python/</guid>
		<description><![CDATA[The code for a very simple asynchronous socket server written in python utilizing the asynchat module. It&#8217;s all in good fun. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 &#8230; <a href="http://swag.dk/blog/2008/02/21/asynchronous-socket-server-in-python/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>The code for a very simple asynchronous socket server written in python utilizing the asynchat module. It&#8217;s all in good fun.</p>

<p><span id="more-8"></span></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">asyncore</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">asynchat</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">socket</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Lobby<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><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><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">clients</span> = <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> leave<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, client<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">clients</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>client<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> join<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, client<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">clients</span>.<span style="color: black;">add</span><span style="color: black;">&#40;</span>client<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> send_to_all<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, data<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> client <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">clients</span>:
            client.<span style="color: black;">push</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Client<span style="color: black;">&#40;</span><span style="color: #dc143c;">asynchat</span>.<span style="color: black;">async_chat</span><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>, conn, lobby<span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">asynchat</span>.<span style="color: black;">async_chat</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, conn=conn<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">in_buffer</span> = <span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">set_terminator</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span> = lobby
        <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> collect_incoming_data<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, data<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">in_buffer</span> += data
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> found_terminator<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">in_buffer</span>.<span style="color: black;">rstrip</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == <span style="color: #483d8b;">&quot;QUIT&quot;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span>.<span style="color: black;">leave</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">close_when_done</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span>.<span style="color: black;">send_to_all</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">in_buffer</span> + <span style="color: #008000;">self</span>.<span style="color: black;">terminator</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">in_buffer</span> = <span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Server<span style="color: black;">&#40;</span><span style="color: #dc143c;">asynchat</span>.<span style="color: black;">async_chat</span><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><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">asynchat</span>.<span style="color: black;">async_chat</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">create_socket</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">socket</span>.<span style="color: black;">AF_INET</span>, <span style="color: #dc143c;">socket</span>.<span style="color: black;">SOCK_STREAM</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">set_reuse_addr</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">bind</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;127.0.0.1&quot;</span>, <span style="color: #ff4500;">12345</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">listen</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span> = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> set_lobby<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, lobby<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span> = lobby
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> handle_accept<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        sock, addr = <span style="color: #008000;">self</span>.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        client = Client<span style="color: black;">&#40;</span>sock, <span style="color: #008000;">self</span>.<span style="color: black;">lobby</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    lobby = Lobby<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    server = Server<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    server.<span style="color: black;">set_lobby</span><span style="color: black;">&#40;</span>lobby<span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">asyncore</span>.<span style="color: black;">loop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>


<p>Booting the server is a matter of <code>python echoserver.py</code>.</p>

<p>After that, telnet can be used to induce messages, as such:
<pre>
$ telnet localhost 12345
foo
foo</pre>
The same will be true for any amount of clients. This is so simple. Love it.</p>
]]></content:encoded>
			<wfw:commentRss>http://swag.dk/blog/2008/02/21/asynchronous-socket-server-in-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
