<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Blaginations</title>
	<atom:link href="http://kasterma.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://kasterma.wordpress.com</link>
	<description>An additional opportunity to be hopelessly wrong</description>
	<lastBuildDate>Mon, 12 Sep 2011 21:30:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='kasterma.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Blaginations</title>
		<link>http://kasterma.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://kasterma.wordpress.com/osd.xml" title="Blaginations" />
	<atom:link rel='hub' href='http://kasterma.wordpress.com/?pushpress=hub'/>
		<item>
		<title>php functions to help you get out of unicode hell.</title>
		<link>http://kasterma.wordpress.com/2011/09/12/php-functions-to-help-you-get-out-of-unicode-hell/</link>
		<comments>http://kasterma.wordpress.com/2011/09/12/php-functions-to-help-you-get-out-of-unicode-hell/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 21:30:31 +0000</pubDate>
		<dc:creator>kasterma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kasterma.wordpress.com/?p=745</guid>
		<description><![CDATA[Here is an example of use<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=745&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><pre class="brush: php;">
function bpad ($instr) {
  return substr(&quot;000000&quot;, 0, 8 - strlen($instr)) . $instr;
}

function dpad ($instr) {
  return substr(&quot;      &quot;, 0, 3 - strlen($instr)) . $instr;
}

function show($instr) {
  for ($i = 0; $i &lt; strlen($instr); $i++) {
    $let = $instr[$i];                                                          
    $no = ord($let);
    echo $i . &quot; : &quot; . $let . &quot; / &quot; . dpad($no) . &quot; / &quot; . bpad(decbin($no)) . &quot; / &quot; . dechex($no).  &quot;\n&quot;;
  }
}
</pre></p>
<p>Here is an example of use</p>
<p><pre class="brush: php;">
php &gt; $var = &quot;T&quot; . pack(&quot;cc&quot;, 195, 171) . &quot;st&quot;;
php &gt; show($var);
0 : T /  84 / 01010100 / 54
1 : � / 195 / 11000011 / c3
2 : � / 171 / 10101011 / ab
3 : s / 115 / 01110011 / 73
4 : t / 116 / 01110100 / 74
php &gt; show (utf8_decode($var));
0 : T /  84 / 01010100 / 54
1 : � / 235 / 11101011 / eb
2 : s / 115 / 01110011 / 73
3 : t / 116 / 01110100 / 74
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kasterma.wordpress.com/745/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kasterma.wordpress.com/745/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kasterma.wordpress.com/745/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kasterma.wordpress.com/745/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kasterma.wordpress.com/745/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kasterma.wordpress.com/745/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kasterma.wordpress.com/745/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kasterma.wordpress.com/745/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kasterma.wordpress.com/745/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kasterma.wordpress.com/745/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kasterma.wordpress.com/745/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kasterma.wordpress.com/745/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kasterma.wordpress.com/745/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kasterma.wordpress.com/745/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=745&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kasterma.wordpress.com/2011/09/12/php-functions-to-help-you-get-out-of-unicode-hell/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Bart Kastermans</media:title>
		</media:content>
	</item>
		<item>
		<title>Computing the height of a Tkinter.Label on given text.</title>
		<link>http://kasterma.wordpress.com/2011/09/06/computing-the-height-of-a-tkinter-label-on-given-text/</link>
		<comments>http://kasterma.wordpress.com/2011/09/06/computing-the-height-of-a-tkinter-label-on-given-text/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 22:08:53 +0000</pubDate>
		<dc:creator>kasterma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kasterma.wordpress.com/?p=741</guid>
		<description><![CDATA[This is a first solution of code that does this. It doesn&#8217;t yet take care of the fact a Label takes whitespace into account, but it does look like a good start. The real work is in lin.count (), the other code is just enough to exercise lin.count ().<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=741&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is a first solution of code that does this.  It doesn&#8217;t yet take care of the fact a Label takes whitespace into account, but it does look like a good start.  The real work is in lin.count (), the other code is just enough to exercise lin.count ().</p>
<p><span id="more-741"></span></p>
<p><pre class="brush: python;">
import Tkinter as Tk
import tkFont
import random
import sys

def genstr (j):
    rno = random.randint(4,50)
    ret_val = str(j) + &quot;:&quot;
    for i in range (0, rno):
        ret_val += &quot;hello&quot; + str(i)
    return ret_val

def gendata (lh):
    ret_val = []
    for i in range(0,lh):
        ret_val.append (genstr (i))
    return ret_val

data = gendata (100)

root = Tk.Tk()
font = tkFont.Font(family='times', size=13)

class lines:
    def __init__ (self):
        self.lastct = 1   # remember where the cutoff was last work from there

    def count (self, text, cutoff = 400):
        global font
        no_lines = 1
        start_idx = 0
        idx = self.lastct

        while True:
            if idx &gt; len (text):
                idx = len (text)

            # shrink from guessed value
            while font.measure (text[start_idx:idx - 1]) &gt; cutoff:
                if idx &lt;= start_idx:
                    print &quot;error&quot;
                    sys.exit ()
                else:
                    idx -= 1
                    self.lastct = idx - start_idx # adjust since was too big

            # increase from guessed value (note: if first shrunk then done)
            while (idx &lt; len (text)
                   and font.measure (text[start_idx:idx]) &lt; cutoff):
                idx += 1
                self.lastct = idx - start_idx     # adjust since was too small

            # next line has been determined
            print &quot;*&quot; + text[start_idx:idx-1] + &quot;*&quot;
            if idx == len(text) and font.measure (text[start_idx:]) &lt; cutoff:
                return no_lines
            elif idx == len(text):
                return no_lines + 1
            else:
                no_lines += 1
                start_idx = idx - 1
                idx = start_idx + self.lastct

lin = lines()

for i in range(0,len(data)):
    lin.count(data[i], 450)

for i in range(0,min(len(data),10)):
    l = Tk.Label(root)
    l.pack()
    l['text'] = data[i]
    print i
    no = lin.count (data[i], 450)
    print &quot;computed lines&quot;, no
    l['width'] = 50
    l['justify'] = Tk.LEFT
    l['anchor'] = 'w'
    l['wraplength'] = 450
    l['padx']=10
    l['pady'] = 5
    l['height'] = no
    l['font'] = font
    if i % 2 == 0:
        l['background'] = 'grey80'
    else:
        l['background'] = 'grey70'

root.mainloop()

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kasterma.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kasterma.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kasterma.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kasterma.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kasterma.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kasterma.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kasterma.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kasterma.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kasterma.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kasterma.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kasterma.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kasterma.wordpress.com/741/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kasterma.wordpress.com/741/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kasterma.wordpress.com/741/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=741&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kasterma.wordpress.com/2011/09/06/computing-the-height-of-a-tkinter-label-on-given-text/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Bart Kastermans</media:title>
		</media:content>
	</item>
		<item>
		<title>Some fun with the Stanford GraphBase.</title>
		<link>http://kasterma.wordpress.com/2011/09/01/some-fun-with-the-stanford-graphbase/</link>
		<comments>http://kasterma.wordpress.com/2011/09/01/some-fun-with-the-stanford-graphbase/#comments</comments>
		<pubDate>Fri, 02 Sep 2011 01:30:05 +0000</pubDate>
		<dc:creator>kasterma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kasterma.wordpress.com/?p=735</guid>
		<description><![CDATA[Fascinating conclusion, in this graph of 5 letter English words connecting two when they differ by a single letter, even though it only contains 5757/26^5 = 4.8 e-4 of the complete list of words there is a component of size 4493. (the second largest is size 24, and the maximum degree is 25).<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=735&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Fascinating conclusion, in this graph of 5 letter English words connecting two when they differ by a single letter, even though it only contains 5757/26^5 = 4.8 e-4 of the complete list of words there is a component of size 4493. (the second largest is size 24, and the maximum degree is 25).</p>
<p><span id="more-735"></span></p>
<p><pre class="brush: python;">
import time
import copy

data = map (lambda x: x.strip(), open('sgb-words.txt').readlines())
print len (data)

def d (w1, w2):
    count = 0
    for idx in range(0,5):
        if w1[idx] != w2[idx]:
            count += 1
    return count

print &quot;creating graph&quot;
t0 = time.clock ()
graph = [[a,b] for a in data for b in data if d(a,b) ==1 and a &lt; b]
t1 = time.clock ()
print &quot;took &quot; + str (t1 - t0) + &quot; seconds.&quot;

#t0 = time.clock ()
#graph2 = []
#for i in range (0, len(data)):
#    for j in range(0,len(data)):
#        if d(data[i],data[j]) == 1 and i &lt; j:
#            graph2.append ([i,j])
#t1 = time.clock ()
#print &quot;took &quot; + str (t1 - t0) + &quot; seconds.&quot;

def deg ():
    maxd = 0
    maxw = &quot;&quot;
    for idx in range (0,len(data)):
        count = 0
        if idx % 100 == 0:
            print idx, maxd, maxw
        for idx2 in range (0,len(data)):
            if d(data[idx],data[idx2]) == 1:
                count += 1
        if count &gt; maxd:
            maxd = count
            maxw = [data[idx]]
        elif count == maxd:
            maxw.append (data[idx])
    return maxd, maxw

def nbd (w):
    nbdv = []
    for idx in range(0,len(data)):
        if d(w,data[idx]) == 1:
            nbdv.append(data[idx])
    return nbdv

def gnbd (w, graph):
    N = [x[1] for x in graph if x[0] == w]
    N.extend ([ x[0] for x in graph if x[1] == w])
    return N

def union (x,y):
    ret_val = copy.copy(x)
    ret_val.extend (y)
    return ret_val

def unique (x):
    ret_val = []
    for idx in range(0,len(x)):
        if not x[idx] in x[0:idx]:
            ret_val.append (x[idx])
    return ret_val

def collect (f, x, remlist):
    if remlist == []:
        return x
    else:
        return collect (f, f(x, remlist[0]), remlist [1:])

def collect2 (f, x, remlist):
    ret_val = copy.copy(x)
    for idx in range (0, len (remlist)):
        ret_val.extend (remlist[idx])
    return ret_val

def next (reached, graph):
    N = unique (collect2 (union, [], map (lambda x: gnbd(x, graph), reached)))
    return N

#print deg()
# (25, ['cores', 'bares'])
#print &quot;Nbd of cores is:&quot;
#print nbd('cores')
#print &quot;Nbd of bares is:&quot;
#print nbd('bares')

def sub (l1, l2):
    ret_val = []
    for idx in range(0,len(l1)):
        if l1[idx] not in l2:
            ret_val.append (l1[idx])
    return ret_val

def component (graph, start):
    reached = [start]  # start with just the first node
    new = [start]
    t0 = time.clock ()
    while True:
        next_bd = next (new, graph)
        new = sub (next_bd,reached)
        reached = unique (union (reached, new))
        if new == []:
            print &quot;DONE:&quot;, len (reached)
            return reached
        else:
            print len (reached), time.clock() - t0

def components (graph, vertices):
    rem = vertices
    components = []
    while rem != []:
        print rem [0]
        new_component = component (graph, rem[0])
        rem = sub (rem, new_component)
        components.append (new_component)
        print &quot;still to do: &quot; + str (len(rem))
        print &quot;components: &quot;, map (len, components)
    return components

print components (graph, data)

</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kasterma.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kasterma.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kasterma.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kasterma.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kasterma.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kasterma.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kasterma.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kasterma.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kasterma.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kasterma.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kasterma.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kasterma.wordpress.com/735/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kasterma.wordpress.com/735/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kasterma.wordpress.com/735/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=735&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kasterma.wordpress.com/2011/09/01/some-fun-with-the-stanford-graphbase/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Bart Kastermans</media:title>
		</media:content>
	</item>
		<item>
		<title>AIDE on Ubuntu.</title>
		<link>http://kasterma.wordpress.com/2011/08/02/aide-on-ubuntu/</link>
		<comments>http://kasterma.wordpress.com/2011/08/02/aide-on-ubuntu/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 17:19:43 +0000</pubDate>
		<dc:creator>kasterma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kasterma.wordpress.com/?p=730</guid>
		<description><![CDATA[I had a link to a linux.com weekend project lying around for quite a while. I finally decided to play some with aide (advanced intrusion detection environment). I set up a config file as suggested in the article, but then got stuck on an error that was not immediately very descriptive to me. After some [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=730&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had a link to a <a href="http://www.linux.com/learn/tutorials/386908-weekend-project-intrusion-detection-on-linux-with-aide">linux.com weekend project</a> lying around for quite a while.  I finally decided to play some with aide (advanced intrusion detection environment).  I set up a config file as suggested in the article, but then got stuck on an error that was not immediately very descriptive to me.  After some searching I found the thread <a href="http://ubuntuforums.org/showthread.php?t=1096333">Aide will not work</a> on the ubuntu forums.  It didn&#8217;t give me the immediate solution, but did make me think in the right direction.  I had forgotten to add database file specifications to the configuration file (see my post in the tread mentioned above).  Now aide is doing its first run, and I can work on figuring out what the &#8220;best&#8221; configuration is for me over the coming days (I should at least get a good view of which parts of my system change regularly and which do not).</p>
<p>UPDATE: when running aide my system became rather slow, so I would recommend running it with nice: sudo nice -n 20 aide &#8211;init &#8211;config=aide.kasterma.conf</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kasterma.wordpress.com/730/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kasterma.wordpress.com/730/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kasterma.wordpress.com/730/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kasterma.wordpress.com/730/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kasterma.wordpress.com/730/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kasterma.wordpress.com/730/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kasterma.wordpress.com/730/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kasterma.wordpress.com/730/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kasterma.wordpress.com/730/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kasterma.wordpress.com/730/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kasterma.wordpress.com/730/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kasterma.wordpress.com/730/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kasterma.wordpress.com/730/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kasterma.wordpress.com/730/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=730&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kasterma.wordpress.com/2011/08/02/aide-on-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Bart Kastermans</media:title>
		</media:content>
	</item>
		<item>
		<title>My computer lying to me.</title>
		<link>http://kasterma.wordpress.com/2011/06/03/my-computer-lying-to-me/</link>
		<comments>http://kasterma.wordpress.com/2011/06/03/my-computer-lying-to-me/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 12:47:06 +0000</pubDate>
		<dc:creator>kasterma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kasterma.wordpress.com/?p=713</guid>
		<description><![CDATA[The following might show that I don&#8217;t use windows much, but it just took me a while to figure out it was lying to me. The location is in fact C:\Users\bart\Documents\<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=713&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The following might show that I don&#8217;t use windows much, but it just took me a while to figure out it was lying to me.<br />
<div id="attachment_714" class="wp-caption aligncenter" style="width: 310px"><a href="http://kasterma.files.wordpress.com/2011/06/capture.png"><img src="http://kasterma.files.wordpress.com/2011/06/capture.png?w=300&#038;h=66" alt="total lie" title="total lie" width="300" height="66" class="size-medium wp-image-714" /></a><p class="wp-caption-text">Total LIE!</p></div></p>
<p>The location is in fact C:\Users\bart\Documents\</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kasterma.wordpress.com/713/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kasterma.wordpress.com/713/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kasterma.wordpress.com/713/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kasterma.wordpress.com/713/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kasterma.wordpress.com/713/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kasterma.wordpress.com/713/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kasterma.wordpress.com/713/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kasterma.wordpress.com/713/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kasterma.wordpress.com/713/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kasterma.wordpress.com/713/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kasterma.wordpress.com/713/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kasterma.wordpress.com/713/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kasterma.wordpress.com/713/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kasterma.wordpress.com/713/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=713&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kasterma.wordpress.com/2011/06/03/my-computer-lying-to-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Bart Kastermans</media:title>
		</media:content>

		<media:content url="http://kasterma.files.wordpress.com/2011/06/capture.png?w=300" medium="image">
			<media:title type="html">total lie</media:title>
		</media:content>
	</item>
		<item>
		<title>Why the graph is linear.</title>
		<link>http://kasterma.wordpress.com/2011/05/11/why-the-graph-is-linear/</link>
		<comments>http://kasterma.wordpress.com/2011/05/11/why-the-graph-is-linear/#comments</comments>
		<pubDate>Wed, 11 May 2011 18:28:18 +0000</pubDate>
		<dc:creator>kasterma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kasterma.wordpress.com/?p=708</guid>
		<description><![CDATA[In my previous post I indicated that I didn&#8217;t quite know why the graph was so linear as I expected something to happen when pages had to be swapped more. I think I have an answer, the linux scheduler (a description) schedules threads for a certain time quantum. According to this question on a &#8220;random&#8221; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=708&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my previous post I indicated that I didn&#8217;t quite know why the graph was so linear as I expected something to happen when pages had to be swapped more.  I think I have an answer, the linux scheduler (<a href="http://oreilly.com/catalog/linuxkernel/chapter/ch10.html">a description</a>) schedules threads for a certain time quantum.  According to <a href="http://fixunix.com/linux/7623-why-time-quantum-has-not-changed-much.html">this question on a &#8220;random&#8221; webpage</a> that quantum is about 100ms.  A processor running at about 2.7 GHz, and a page size of 4 KB, has about 72,000 ticks to deal with a byte on a memory page if it is to finish the page in its quantum &#8230; that seems plenty.  I.e., from this it seems that in swapping out, and then later swapping in a thread it is unlikely to need to same page; and the linearity really is thread administration overhead.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kasterma.wordpress.com/708/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kasterma.wordpress.com/708/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kasterma.wordpress.com/708/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kasterma.wordpress.com/708/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kasterma.wordpress.com/708/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kasterma.wordpress.com/708/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kasterma.wordpress.com/708/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kasterma.wordpress.com/708/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kasterma.wordpress.com/708/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kasterma.wordpress.com/708/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kasterma.wordpress.com/708/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kasterma.wordpress.com/708/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kasterma.wordpress.com/708/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kasterma.wordpress.com/708/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=708&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kasterma.wordpress.com/2011/05/11/why-the-graph-is-linear/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Bart Kastermans</media:title>
		</media:content>
	</item>
		<item>
		<title>Parallelizing merge sort doesn&#8217;t help, or does it?</title>
		<link>http://kasterma.wordpress.com/2011/05/11/parallelizing-merge-sort-doesnt-help-or-does-it/</link>
		<comments>http://kasterma.wordpress.com/2011/05/11/parallelizing-merge-sort-doesnt-help-or-does-it/#comments</comments>
		<pubDate>Wed, 11 May 2011 17:58:04 +0000</pubDate>
		<dc:creator>kasterma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kasterma.wordpress.com/?p=700</guid>
		<description><![CDATA[Merge sort not being a computationally heavy algorithm (lots of moving of data, but not computing much with it) you might expect not much improvement in parallelizing it. I did the experiment and found that on my 8 core processor dividing the work into 4 to 8 threads does speed things up considerably (about a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=700&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Merge sort not being a computationally heavy algorithm (lots of moving of data, but not computing much with it) you might expect not much improvement in parallelizing it.  I did the experiment and found that on my 8 core processor dividing the work into 4 to 8 threads does speed things up considerably (about a factor 3).  I believe this to be the case because it allows for more efficient use of the memory bus and the cache.</p>
<p>Then adding more and more threads leads to significant slowdown.  </p>
<p><a href="http://kasterma.files.wordpress.com/2011/05/thread_data.png"><img src="http://kasterma.files.wordpress.com/2011/05/thread_data.png?w=300&#038;h=175" alt="Thread Data" title="Thread Data" width="300" height="175" class="aligncenter size-medium wp-image-701" /></a></p>
<p>The linear slowdown as shown in the graph continues at least up to 750 threads.  I was expecting an additional slowdown (an increase in slope of the line) when there were so many threads their pages of memory would get swapped out in between them being active (4 KB per page, 8192 KB cache size, at least 5 pages per thread [for the 3 different parts of the array it is sorting, code, and stack], means this would happen before 410 threads).  I am not quite sure yet why that does not happen.</p>
<p>Code is again available in my <a href="https://github.com/kasterma/merge-sort">github repository</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kasterma.wordpress.com/700/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kasterma.wordpress.com/700/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kasterma.wordpress.com/700/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kasterma.wordpress.com/700/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kasterma.wordpress.com/700/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kasterma.wordpress.com/700/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kasterma.wordpress.com/700/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kasterma.wordpress.com/700/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kasterma.wordpress.com/700/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kasterma.wordpress.com/700/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kasterma.wordpress.com/700/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kasterma.wordpress.com/700/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kasterma.wordpress.com/700/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kasterma.wordpress.com/700/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=700&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kasterma.wordpress.com/2011/05/11/parallelizing-merge-sort-doesnt-help-or-does-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Bart Kastermans</media:title>
		</media:content>

		<media:content url="http://kasterma.files.wordpress.com/2011/05/thread_data.png?w=300" medium="image">
			<media:title type="html">Thread Data</media:title>
		</media:content>
	</item>
		<item>
		<title>Timing n VS n*log(n).</title>
		<link>http://kasterma.wordpress.com/2011/05/10/timing-n-vs-nlogn/</link>
		<comments>http://kasterma.wordpress.com/2011/05/10/timing-n-vs-nlogn/#comments</comments>
		<pubDate>Tue, 10 May 2011 22:11:33 +0000</pubDate>
		<dc:creator>kasterma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kasterma.wordpress.com/?p=690</guid>
		<description><![CDATA[Standard merge sort is , just playing around with some code I did some timing up to the size that all work sorting an array of integers fits in memory. When I drew the results initially I was convinced I had made a mistake, but this really was just b/c I had never quite realized [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=690&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Standard merge sort is <img src='http://s0.wp.com/latex.php?latex=O%28n%2Alog%28n%29%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='O(n*log(n))' title='O(n*log(n))' class='latex' />, just playing around with some code I did some timing up to the size that all work sorting an array of integers fits in memory.  When I drew the results initially I was convinced I had made a mistake, but this really was just b/c I had never quite realized how &#8220;linear&#8221; <img src='http://s0.wp.com/latex.php?latex=n+%2A+log%28n%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n * log(n)' title='n * log(n)' class='latex' /> looks at these sizes.  Here is the graph (red dots are the measured time, blue is a line fitted with the next to last measured value, green is <img src='http://s0.wp.com/latex.php?latex=n+%2A+log%28n%29&amp;bg=ffffff&amp;fg=1c1c1c&amp;s=0' alt='n * log(n)' title='n * log(n)' class='latex' /> fitted with the next to last measured value).</p>
<p><a href="http://kasterma.files.wordpress.com/2011/05/graph_merge_timing.png"><img src="http://kasterma.files.wordpress.com/2011/05/graph_merge_timing.png?w=300&#038;h=226" alt="Merge timing data graphed." title="graph_merge_timing" width="300" height="226" class="aligncenter size-medium wp-image-694" /></a></p>
<p>Note that I used to next to last value measured since the last value is a bit of an outlier (I was watching the system monitor, and for some reason the process switched to another core).  All code is available in the <a href="https://github.com/kasterma/merge-sort">github repository</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kasterma.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kasterma.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kasterma.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kasterma.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kasterma.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kasterma.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kasterma.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kasterma.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kasterma.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kasterma.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kasterma.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kasterma.wordpress.com/690/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kasterma.wordpress.com/690/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kasterma.wordpress.com/690/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=690&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kasterma.wordpress.com/2011/05/10/timing-n-vs-nlogn/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Bart Kastermans</media:title>
		</media:content>

		<media:content url="http://kasterma.files.wordpress.com/2011/05/graph_merge_timing.png?w=300" medium="image">
			<media:title type="html">graph_merge_timing</media:title>
		</media:content>
	</item>
		<item>
		<title>Using TRAMP on Windows; first steps.</title>
		<link>http://kasterma.wordpress.com/2011/05/09/using-tramp-on-windows-first-steps/</link>
		<comments>http://kasterma.wordpress.com/2011/05/09/using-tramp-on-windows-first-steps/#comments</comments>
		<pubDate>Mon, 09 May 2011 18:22:40 +0000</pubDate>
		<dc:creator>kasterma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kasterma.wordpress.com/?p=685</guid>
		<description><![CDATA[I decided to give TRAMP a spin on windows. In order to get it initially working I needed to take the following steps: download plink. add the location of plink to the windows path (on the page how to set the windows path there is a description, that didn&#8217;t quite work for me but got [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=685&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I decided to give <a href="http://www.gnu.org/software/tramp/">TRAMP</a> a spin on windows.  In order to get it initially working I needed to take the following steps:</p>
<ul>
<li>
  download <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html">plink</a>.
</li>
<li>
  add the location of plink to the windows path (on the page <a href="http://geekswithblogs.net/renso/archive/2009/10/21/how-to-set-the-windows-path-in-windows-7.aspx">how to set the windows path</a> there is a description, that didn&#8217;t quite work for me but got me close enough).
</li>
</ul>
<p>After doing this I now have a local emacs editing a remote file.  It remains to be seen if it is quick enough (although I am connected over a very fast network, so I do not expect problems).  The big advantage is that the local emacs has much better default color scheme than the default of emacs over putty.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kasterma.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kasterma.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kasterma.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kasterma.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kasterma.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kasterma.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kasterma.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kasterma.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kasterma.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kasterma.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kasterma.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kasterma.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kasterma.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kasterma.wordpress.com/685/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=685&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kasterma.wordpress.com/2011/05/09/using-tramp-on-windows-first-steps/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Bart Kastermans</media:title>
		</media:content>
	</item>
		<item>
		<title>Wolfram&#124;Alpha has an API</title>
		<link>http://kasterma.wordpress.com/2011/04/08/wolframalpha-has-an-api/</link>
		<comments>http://kasterma.wordpress.com/2011/04/08/wolframalpha-has-an-api/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 14:56:15 +0000</pubDate>
		<dc:creator>kasterma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://kasterma.wordpress.com/?p=680</guid>
		<description><![CDATA[The other day I was playing around a little with Wolfram&#124;Alpha and found that it has an API. I was looking at population histograms at the time, so my first use of it was to get a bit more of a picture of which countries were had a histogram very different from the one in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=680&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The other day I was playing around a little with Wolfram|Alpha and found that it has an API.  I was looking at population histograms at the time, so my first use of it was to get a bit more of a picture of which countries were had a histogram very different from the one in the Netherlands (where I took as a marker the fact of whether the ago group 0-4 was the largest of the population).  The result can be seen in the image <a href="http://maps.google.com/maps/api/staticmap?center=europe&amp;zoom=1&amp;size=640x640&amp;&amp;markers=color:green|Swaziland|Liberia|RepublicCongo|Lesotho|Tajikistan|Mauritania|EastTimor|Togo|Tonga|FrenchGuiana|Libya|Panama|CentralAfricanRepublic|EquatorialGuinea|GuineaBissau|Belize|SaintVincentGrenadines|Namibia|Micronesia|PapuaNewGuinea|Djibouti|SaoTomePrincipe|Reunion|Nicaragua|SolomonIslands|Vanuatu|Israel|Jordan|Gambia|Eritrea|WesternSahara|Honduras|Botswana|Paraguay|Comoros&amp;sensor=false">Google Maps Representation</a> (clearly google maps misinterpreted at least two country names and drew the markers for those in the US).</p>
<p>The code for getting the string &#8216;Swaziland|Liberia|RepublicCongo|Lesotho|Tajikistan|Mauritania|EastTimor|Togo|T\<br />
onga|FrenchGuiana|Libya|Panama|CentralAfricanRepublic|EquatorialGuinea|GuineaBi\<br />
ssau|Belize|SaintVincentGrenadines|Namibia|Micronesia|PapuaNewGuinea|Djibouti|S\<br />
aoTomePrincipe|Reunion|Nicaragua|SolomonIslands|Vanuatu|Israel|Jordan|Gambia|Er\<br />
itrea|WesternSahara|Honduras|Botswana|Paraguay|Comoros&#8217; consisting of all countries where the 0-4 age group is the biggest is mostly given below.  I am not giving all of it b/c I think I need to keep my key private, and I don&#8217;t want to be the cause of tops of queries to their API (if you want you can come up with your own complete list of countrys; hint Wolfram|Alpha).</p>
<p>First the script that queries Wolfram|Alpha and saves the info to a file.</p>
<p><pre class="brush: python;">
&quot;&quot;&quot; wa.py

Setting up some basic code to get queries answered by Wolfram|Alpha.

Bart Kastermans, www.bartk.nl
&quot;&quot;&quot;

import time
import pickle
import urllib
import BeautifulSoup

APPID=&quot;GET-YOUR-OWN&quot;
URL=&quot;http://api.wolframalpha.com/v2/query?input=%(query)s&amp;appid=&quot; + \
    APPID  + \
    &quot;&amp;podstate=AgeDistributionGrid:AgeDistributionData__Show details&quot;

ALL_COUNTRIES = [&quot;China&quot;, &quot;India&quot;, &quot;United States&quot;, &quot;Indonesia&quot;, &quot;Brazil&quot;, &quot;Pakistan&quot;, &quot;Bangladesh&quot;, &quot;Nigeria&quot;, &quot;Russia&quot;, &quot;Japan&quot;, &quot;Mexico&quot;, &quot;Philippines&quot;, &quot;Vietnam&quot;, &quot;Ethiopia&quot;, &quot;Germany&quot;, &quot;Egypt&quot;, &quot;Turkey&quot;, &quot;Iran&quot;, &quot;Thailand&quot;, &quot;France&quot;, &quot;Democratic Republic of the Congo&quot;, &quot;United Kingdom&quot;, &quot;Italy&quot;, &quot;Myanmar&quot;, &quot;South Africa&quot;]

for country in ALL_COUNTRIES:
    query = urllib.quote (&quot;age distribution &quot; + country)
    url_query = URL % {'query':query}

    print &quot;current query:&quot;, url_query

    page = urllib.urlopen (url_query)
    cont = page.readlines ()

    # extract the info we want (apologies for magic constants)
    cont_parsed = BeautifulSoup.BeautifulStoneSoup (''.join (cont))
    data = cont_parsed.findAll (scanner='Data')
    if len (data) &gt; 1:
        result = cont_parsed.findAll (scanner='Data')[1].plaintext
    else:
        result = None

    countryinfo [country] = result

    print &quot;current result:&quot;  # give some feedback something is happening
    print result
    print &quot;Just got information for country:&quot;, country

    time.sleep (5)   # spread the queries over time

# save the information to file
COUNTRY_FILE = open (&quot;country_info.txt&quot;, &quot;w&quot;)
PICKLER = pickle.Pickler (COUNTRY_FILE)
PICKLER.dump (countryinfo)
COUNTRY_FILE.close ()
</pre></p>
<p>Then the script that does the parsing</p>
<p><pre class="brush: python;">

import pickle

def convert (x):
    &quot;&quot;&quot; anything that does not convert is set to 0 &quot;&quot;&quot;
    try:
        return int (x)
    except:
        return 0

def parse_info (inp):
    &quot;&quot;&quot; certainly presumes the input is of right form &quot;&quot;&quot;
    example2 = str(inp).split (&quot;\n&quot;)
    example3 = example2 [1:-2]
    example4 = map (lambda x: x.split (&quot;|&quot;), example3)
    example5 = map (lambda x: [x[0], x[3].split (&quot; &quot;)[1]], example4)
    example6 = map (lambda x: [x[0], convert (x[1])], example5)
    return example6

COUNTRY_FILE = open (&quot;country_info.txt&quot;)
UNPICKLER = pickle.Unpickler (COUNTRY_FILE)
country_info = UNPICKLER.load ()
COUNTRY_FILE.close ()

# remove coutries with no info, &quot;parse&quot; the ones with
for country in country_info.keys ():
    if country_info [country] == None:
        del country_info [country]
    else:
        country_info [country] = parse_info (country_info [country])

def country_color (country):
    &quot;&quot;&quot; green means the 0-4 group is largest, red otherwise &quot;&quot;&quot;
    val_least_key = country [0][1]
    color = &quot;green&quot;

    for item in country:
        if item [1] &gt; val_least_key:
            color = &quot;red&quot;

    return color

# assign colors
colors = {}
for country in country_info.keys ():
    colors [country] = country_color (country_info [country])

# create the strings to paste into google staticmap
red = []
green = []
for country in colors.keys ():
    if colors [country] == 'red':
        red.append (country)
    else:
        green.append (country)

red_str = &quot;|&quot;.join (red)
green_str = &quot;|&quot;.join (green)
</pre></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kasterma.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kasterma.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kasterma.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kasterma.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kasterma.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kasterma.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kasterma.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kasterma.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kasterma.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kasterma.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kasterma.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kasterma.wordpress.com/680/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kasterma.wordpress.com/680/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kasterma.wordpress.com/680/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kasterma.wordpress.com&amp;blog=3824912&amp;post=680&amp;subd=kasterma&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kasterma.wordpress.com/2011/04/08/wolframalpha-has-an-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">Bart Kastermans</media:title>
		</media:content>
	</item>
	</channel>
</rss>
