<?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>Thoughts from Mads Sülau Jørgensen &#187; Work</title> <atom:link href="http://madssj.com/blog/tag/work/feed/" rel="self" type="application/rss+xml" /><link>http://madssj.com/blog</link> <description>Various articles about programming and systems administration.</description> <lastBuildDate>Mon, 27 Jun 2011 08:54:43 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Figuring out largest/smallest/median filesizes</title><link>http://madssj.com/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/</link> <comments>http://madssj.com/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; &#8230; <a
href="http://madssj.com/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></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><p><span
id="more-189"></span></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://madssj.com/blog/2010/02/04/figuring-out-largestsmallestmedian-filesizes/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Keeping ssh connections alive</title><link>http://madssj.com/blog/2009/06/12/keeping-ssh-connections-alive/</link> <comments>http://madssj.com/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://madssj.com/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://madssj.com/blog/2009/05/15/python-http_head-method/</link> <comments>http://madssj.com/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 &#8230; <a
href="http://madssj.com/blog/2009/05/15/python-http_head-method/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></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://madssj.com/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://madssj.com/blog/2009/05/13/formatting-xml-with-xmllint/</link> <comments>http://madssj.com/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 &#8230; <a
href="http://madssj.com/blog/2009/05/13/formatting-xml-with-xmllint/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></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://madssj.com/blog/2009/05/13/formatting-xml-with-xmllint/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Very simple email sending method in python</title><link>http://madssj.com/blog/2009/05/11/very-simple-email-sending-method-in-python/</link> <comments>http://madssj.com/blog/2009/05/11/very-simple-email-sending-method-in-python/#comments</comments> <pubDate>Mon, 11 May 2009 17:30:24 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Uncategorized]]></category> <category><![CDATA[Email]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[SMTP]]></category> <category><![CDATA[Work]]></category> <guid
isPermaLink="false">http://swag.dk/blog/2009/05/11/very-simple-email-sending-method-in-python/</guid> <description><![CDATA[I needed to send an email, so I came up with this: &#160; def send_plain_mail&#40;subject, body, from_mail, to&#41;: import smtplib from email.MIMEText import MIMEText from email.Encoders import encode_quopri &#160; msg = MIMEText&#40;body, 'plain', 'iso-8859-1'&#41; &#160; msg&#91;'Subject'&#93; = subject msg&#91;'From'&#93; = &#8230; <a
href="http://madssj.com/blog/2009/05/11/very-simple-email-sending-method-in-python/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p>I needed to send an email, so I came up with this:</p><div
class="wp_syntax"><div
class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> send_plain_mail<span style="color: black;">&#40;</span>subject, body, from_mail, to<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">smtplib</span>
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">MIMEText</span> <span style="color: #ff7700;font-weight:bold;">import</span> MIMEText
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">Encoders</span> <span style="color: #ff7700;font-weight:bold;">import</span> encode_quopri
&nbsp;
    msg = MIMEText<span style="color: black;">&#40;</span>body, <span style="color: #483d8b;">'plain'</span>, <span style="color: #483d8b;">'iso-8859-1'</span><span style="color: black;">&#41;</span>
&nbsp;
    msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Subject'</span><span style="color: black;">&#93;</span> = subject
    msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'From'</span><span style="color: black;">&#93;</span> = from_mail
    msg<span style="color: black;">&#91;</span><span style="color: #483d8b;">'To'</span><span style="color: black;">&#93;</span> = to
&nbsp;
    s = <span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTP</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">sendmail</span><span style="color: black;">&#40;</span>from_mail, <span style="color: black;">&#91;</span>to<span style="color: black;">&#93;</span>, msg.<span style="color: black;">as_string</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    s.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div><p>Not rocket science, but it gets the job done.</p> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2009/05/11/very-simple-email-sending-method-in-python/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Using mercurial with eclipse to edit actionscript files on os x</title><link>http://madssj.com/blog/2009/03/11/using-mercurial-with-eclipse-to-edit-actionscript-files-on-os-x/</link> <comments>http://madssj.com/blog/2009/03/11/using-mercurial-with-eclipse-to-edit-actionscript-files-on-os-x/#comments</comments> <pubDate>Wed, 11 Mar 2009 09:38:33 +0000</pubDate> <dc:creator>Mads Sülau Jørgensen</dc:creator> <category><![CDATA[Actionscript]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[Work]]></category> <category><![CDATA[Mercurial]]></category> <guid
isPermaLink="false">http://swag.dk/blog/2009/03/11/using-mercurial-with-eclipse-to-edit-actionscript-files-on-os-x/</guid> <description><![CDATA[We have recently switched to use mercurial as our DVCS. We&#8217;re hosting our many repositories on bitbucket. I love it. When it comes to actionscript files, flash has always had odd newlines. For some reason, it has always used r &#8230; <a
href="http://madssj.com/blog/2009/03/11/using-mercurial-with-eclipse-to-edit-actionscript-files-on-os-x/">Continue reading <span
class="meta-nav">&#8594;</span></a>]]></description> <content:encoded><![CDATA[<p><a
href="http://www.konstellation.dk/">We</a> have recently switched to use <a
href="http://www.selenic.com/mercurial/wiki/">mercurial</a> as our DVCS. We&#8217;re hosting our many repositories on <a
href="http://bitbucket.org/">bitbucket</a>.</p><p>I love it.</p><p>When it comes to actionscript files, flash has always had odd newlines. For some reason, it has always used <code>r</code> as it&#8217;s newline. Mercurial, beeing a unix tool, likes it&#8217;s newlines to be <code>n</code>. So what better to do, than to make mercurial encode/decode the files for us.</p><p>To do that, we can simply add the following to our <code>.hgrc</code> file:</p><pre>
[encode]
*.as = perl -pe 's/r/n/g'
[decode]
*.as = perl -pe 's/r/n/g'
</pre><p>I don&#8217;t really know wether the decode part is necessary, but I like to keep it around (if someone should commit poison).</p> ]]></content:encoded> <wfw:commentRss>http://madssj.com/blog/2009/03/11/using-mercurial-with-eclipse-to-edit-actionscript-files-on-os-x/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> </channel> </rss>
