<?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>Fellinghaug Blog &#187; python</title>
	<atom:link href="http://asbjorn.fellinghaug.com/blog/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://asbjorn.fellinghaug.com/blog</link>
	<description>&#62;&#62;&#62; from fellinghaug import asbjorn; asbjorn.play()</description>
	<lastBuildDate>Thu, 19 Nov 2009 21:22:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating Python extensions in C</title>
		<link>http://asbjorn.fellinghaug.com/blog/2009/11/creating-python-extensions-in-c/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2009/11/creating-python-extensions-in-c/#comments</comments>
		<pubDate>Thu, 19 Nov 2009 21:22:01 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[C extension]]></category>
		<category><![CDATA[fortran]]></category>
		<category><![CDATA[HPC]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=210</guid>
		<description><![CDATA[Hi there!
In my current work I&#8217;m working on optimizing some parallel software, and basically tries to make programs run faster. Within this work there are focus areas such as I/O and memory utilization, which are key areas when trying to optimize software. Generally when people think of highly optimized software, they think of C/C++ and [...]]]></description>
			<content:encoded><![CDATA[<p>Hi there!</p>
<p>In my current work I&#8217;m working on optimizing some parallel software, and basically tries to make programs run faster. Within this work there are focus areas such as I/O and memory utilization, which are key areas when trying to optimize software. Generally when people think of highly optimized software, they think of C/C++ and possible assembly.</p>
<p>Python is very simple in its syntax, and its speed is not at all bad. From a HPC (<em>High Performance Computing</em>) perspective Python may not look that interesting, but combining Python&#8217;s simplicity with C&#8217;s speed, you&#8217;ll get the best from both worlds.</p>
<p>Since many of the core modules, and Python itself is written in C, it is possible to further extend Python in C. Even the official Python documentation (<a title="Python official documentation" href="http://docs.python.org">http://docs.python.org</a>) have a whole section covering the Python C API. By downloading the Python development files (and of course the GNU compiler; <em>apt-get install build-essential</em>), then you&#8217;re ready to create C extensions for Python. The overall goal for your C extensions should be to only do the very compute intensive tasks, and keep the other stuff in Python. A key observation here is to identify which parts of your program you may need to create a C extension for &#8211; and for this to be found you can use a profiler. Included in Python (<em>&#8220;batteries included&#8221;</em>) there are a couple of profilers:</p>
<ul>
<li>import profile</li>
<li>import cProfile</li>
<li>import hotshot</li>
</ul>
<p>I will not cover  these python profilers here, but rather inspire you to try them out. They might save you a lot of work and give you valuable knowledge of your software. Do some reading of python profiling <a title="Python profiling" href="http://docs.python.org/library/profile.html">here</a>.</p>
<p>Now lets create a very simple (and extremely stupid) python extension in C. We want it to contain two methods:</p>
<ol>
<li>a method which allows you to run shell commands by passing a string</li>
<li>a method which returns some text based on your input</li>
</ol>
<p>To create this extension we follow these steps: a) decide the python module name b) create the C file with the same name as the python module name c) start programming C! Let&#8217;s name our module for &#8220;cool&#8221; (not the best name)..</p>
<p>This is our <strong>cool.c</strong> file:</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;Python.h&gt;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
 * Takes a string argument, which is the shell command, and runs it.
 * Returns the return code of the system(cmd) call.
 * */</span>
<span style="color: #993333;">static</span> PyObject <span style="color: #339933;">*</span>cool_command<span style="color: #009900;">&#40;</span>PyObject <span style="color: #339933;">*</span>self<span style="color: #339933;">,</span> PyObject <span style="color: #339933;">*</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">const</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>cmd;
    <span style="color: #993333;">int</span> retval;
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>PyArg_ParseTuple<span style="color: #009900;">&#40;</span>args<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;s&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>cmd<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
&nbsp;
    retval <span style="color: #339933;">=</span> system<span style="color: #009900;">&#40;</span> cmd <span style="color: #009900;">&#41;</span>;
    <span style="color: #b1b100;">return</span> Py_BuildValue<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;i&quot;</span><span style="color: #339933;">,</span> retval<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/*
 * Takes a string argument, and assembles it into a new python string
 * object and returns it.
 * */</span>
<span style="color: #993333;">static</span> PyObject <span style="color: #339933;">*</span>cool_greet<span style="color: #009900;">&#40;</span>PyObject <span style="color: #339933;">*</span>self<span style="color: #339933;">,</span> PyObject <span style="color: #339933;">*</span>args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>input;
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>resp <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;Hi there: &quot;</span>;
&nbsp;
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>PyArg_ParseTuple<span style="color: #009900;">&#40;</span>args<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;s&quot;</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>input<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
&nbsp;
    <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>retstr <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span> malloc<span style="color: #009900;">&#40;</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span>strlen<span style="color: #009900;">&#40;</span>input<span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>strlen<span style="color: #009900;">&#40;</span>resp<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#41;</span>; <span style="color: #666666; font-style: italic;">// +1 &amp;lt;- null termination</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>retstr <span style="color: #339933;">==</span> <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">NULL</span>;
&nbsp;
    PyObject <span style="color: #339933;">*</span>retString;
&nbsp;
    strcpy<span style="color: #009900;">&#40;</span> retstr<span style="color: #339933;">,</span> resp <span style="color: #009900;">&#41;</span>;
    strcat<span style="color: #009900;">&#40;</span>retstr<span style="color: #339933;">,</span> input<span style="color: #009900;">&#41;</span>;
&nbsp;
    retString <span style="color: #339933;">=</span> Py_BuildValue<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;s&quot;</span><span style="color: #339933;">,</span> retstr<span style="color: #009900;">&#41;</span>;
    Py_INCREF<span style="color: #009900;">&#40;</span>retString<span style="color: #009900;">&#41;</span>;
    free<span style="color: #009900;">&#40;</span>retstr<span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #b1b100;">return</span> retString;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* an array of PyMethodDef structure. A PyMethodDef structure is
used to describe a method of an extension type. */</span>
<span style="color: #993333;">static</span> PyMethodDef CoolMethods<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;command&quot;</span><span style="color: #339933;">,</span> cool_command<span style="color: #339933;">,</span> METH_VARARGS<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Execute a shell command.&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #ff0000;">&quot;greet&quot;</span><span style="color: #339933;">,</span> cool_greet<span style="color: #339933;">,</span> METH_VARARGS<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;Returns a greeting to the caller.&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span><span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #339933;">,</span> <span style="color:#800080;">0</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">NULL</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>;
&nbsp;
PyMODINIT_FUNC initcool<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> Py_InitModule<span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;cool&quot;</span><span style="color: #339933;">,</span> CoolMethods <span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This C extension file is built up with the following key sections:</p>
<ul>
<li>Inclusion of headers.</li>
<li>static PyObject methods which constitutes the visible python functions of our extension module.</li>
<li>An array of PyMethodDef structures. A PyMethodDef structure describes a method of an extension type.</li>
<li>A initialization method which is named &#8220;<em>init&lt;modulename&gt;</em>&#8220;. This method has the sole purpose of calling the Py_InitModule function which takes the name and the PyMethodDef array.</li>
</ul>
<p>So there you go, this is our Python extension implemented in C with the help of the Python development headers. Now we&#8217;ll have a look at our Distutils script (<em>setup.py</em>) that will assist us with the compilation and creation of the actual python module.</p>
<p>This is our <strong>setup.py</strong> file:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">distutils</span>.<span style="color: black;">core</span> <span style="color: #ff7700;font-weight:bold;">import</span> setup, Extension
&nbsp;
module1 = Extension<span style="color: black;">&#40;</span><span style="color: #483d8b;">'cool'</span>,
        sources = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'cool.c'</span><span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span>
&nbsp;
setup <span style="color: black;">&#40;</span> name = <span style="color: #483d8b;">'CoolPackage'</span>,
        version = <span style="color: #483d8b;">'0.1'</span>,
        description = <span style="color: #483d8b;">'A descriptive and informal C extension to Python'</span>,
        ext_modules = <span style="color: black;">&#91;</span>module1<span style="color: black;">&#93;</span> <span style="color: black;">&#41;</span></pre></div></div>

<p>jada..</p>
<p>This file is much self-explained. We have basically one method named &#8220;setup&#8221; which takes a bunch of arguments. These arguments is the package name, version, description and a set of <em>Extension</em> objects. These <em>Extension</em> objects describes our C extension files. By simply running this command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">python setup.py build</pre></div></div>

<p>When the compilation is done, you&#8217;ll have a build folder in the same directory as the setup.py. Go into the &#8220;<em>build/lib.linux-i686-2.6/</em>&#8220;. Then type &#8220;python&#8221; or &#8220;ipython&#8221;, and then &#8220;import cool&#8221;. Now you actually have loaded the &#8220;cool&#8221; module, and you may call &#8220;cool.greet(&#8217;Alex&#8217;)&#8221; and/or &#8220;cool.command(&#8217;ls /&#8217;)&#8221; and the actual computing happens in the C world instead of the Python world.</p>
<p>Now, keep in mind that this C extension isn&#8217;t actual doing anything useful. But, given that you have some algorithm or other problem to solve, and the time is of the essence, then utilizing the power which lays in this C extensions can give you significant time savings.</p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2009/11/creating-python-extensions-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python 3.1 is now released</title>
		<link>http://asbjorn.fellinghaug.com/blog/2009/03/python-31-is-now-released/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2009/03/python-31-is-now-released/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 19:19:22 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[python 3000 v3.1]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=217</guid>
		<description><![CDATA[Since the very important and big release of Python v3.0 (also knowed as &#8220;Python 3000&#8243;) in december, there has been some minor bugfixes and further improvements. Now, in Python v3.1, we may get to feel these bugfixes and enhancements.
Of important changes we may highlight such as:

The IO module have been reimplemented in C for gaining [...]]]></description>
			<content:encoded><![CDATA[<p>Since the very important and big release of Python v3.0 (also knowed as &#8220;Python 3000&#8243;) in december, there has been some minor bugfixes and further improvements. Now, in Python v3.1, we may get to feel these bugfixes and enhancements.</p>
<div id="attachment_146" class="wp-caption alignnone" style="width: 310px"><a href="http://asbjorn.fellinghaug.com/blog/wp-content/uploads/2008/12/python-logo-master-v3-tm.png"><img class="size-medium wp-image-146" title="python logo" src="http://asbjorn.fellinghaug.com/blog/wp-content/uploads/2008/12/python-logo-master-v3-tm-300x101.png" alt="Python Logo" width="300" height="101" /></a><p class="wp-caption-text">Python Logo</p></div>
<p>Of important changes we may highlight such as:</p>
<ul>
<li>The IO module have been reimplemented in C for gaining even more speed</li>
<li>Decoding of utf-8, utf-16 and latin1 is know from 2x to 4x times faster than before</li>
<li>int and str comparisons is know faster</li>
</ul>
<p>So, for all Python v3.0 people out there, I would suggest upgrading to this lastest version. Visit this site: <a title="Python v3.1" href="http://www.python.org/download/releases/3.1/">http://www.python.org/download/releases/3.1/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2009/03/python-31-is-now-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Concerning my master thesis</title>
		<link>http://asbjorn.fellinghaug.com/blog/2009/01/concerning-my-master-thesis/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2009/01/concerning-my-master-thesis/#comments</comments>
		<pubDate>Fri, 30 Jan 2009 14:36:37 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[lucene]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[bigram]]></category>
		<category><![CDATA[bigram index]]></category>
		<category><![CDATA[fellinghaug]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[master]]></category>
		<category><![CDATA[master thesis]]></category>
		<category><![CDATA[search engine]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=181</guid>
		<description><![CDATA[Hi everyone.
I just want to inform that I&#8217;ve taken some further steps to describe and provide my master thesis. I have written a page (http://asbjorn.fellinghaug.com/blog/master-thesis/) who&#8217;s goal is to summeraize and further describe the overall goals and design of my master thesis.
I will also &#8211; in time &#8211; further work on the bigram index, as [...]]]></description>
			<content:encoded><![CDATA[<p>Hi everyone.</p>
<p>I just want to inform that I&#8217;ve taken some further steps to describe and provide my master thesis. I have written a page (<a title="Master thesis" href="http://asbjorn.fellinghaug.com/blog/master-thesis/">http://asbjorn.fellinghaug.com/blog/master-thesis/</a>) who&#8217;s goal is to summeraize and further describe the overall goals and design of my master thesis.</p>
<p>I will also &#8211; in time &#8211; further work on the <strong>bigram index</strong>, as I want to see its full working potential one a more <em>real-life</em> collection. In the beginning I will use the dumps provided by the wonderful Wikipedia foundation. These dumps are several gigabytes with pure text (and some metadata). I realize that the content of each wikipedia article may not fully reflect typical websites on the internet, but it is a start. The next step I&#8217;ve made myself is to find a sufficiently large website, and then index all the data on it. Then, to check how the bigram index performs on it.</p>
<p>I will most likely keep further developments in the Java programming language, as it is the language which Apache Lucene is written in. However, I&#8217;m also quite interessted in writing a Python analyzer for the PyLucene package (Python port of Lucene).</p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2009/01/concerning-my-master-thesis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>IPython &#8211; enhanced python interpreter</title>
		<link>http://asbjorn.fellinghaug.com/blog/2008/11/ipython-enhanced-python-interpreter/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2008/11/ipython-enhanced-python-interpreter/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 14:23:31 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[interactive python]]></category>
		<category><![CDATA[ipython]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=130</guid>
		<description><![CDATA[Hi everyone.
A couple of times now I&#8217;ve been amazed over how many people who is still unaware of the IPython. From the IPython webpage, a very short summary of what IPython is &#8220;Enhanced interactive Python shell&#8221;. The python programming language is surrounded its interpreter, which facilitates dynamic typing and execution. This feature sufficiently increases productivity [...]]]></description>
			<content:encoded><![CDATA[<p>Hi everyone.</p>
<p>A couple of times now I&#8217;ve been amazed over how many people who is still unaware of the IPython. From the IPython webpage, a very short summary of what IPython is &#8220;Enhanced interactive Python shell&#8221;. The python programming language is surrounded its interpreter, which facilitates dynamic typing and execution. This feature sufficiently increases productivity as there is no problem to test and try code snippets on-the-fly.</p>
<p>The IPython is a further extension of the standard python interpreter, as IPython provides more features in the python shell, such as auto-completion of imported modules, syntax highlightning, colors, and a variouse other usefull commands and features.</p>
<p>IPython is highly flexible in terms of providing the user with the possibility to extend the python shell even further with custom commands (<em>called magic commands</em>). There is also an even tigther integration between the python interpreter and the underlying shell, such as bash,csh, etc. It is for instance much simpler to list files and folders, by just typing &#8220;ls&#8221; directly into the shell. Even commands such as &#8220;mkdir&#8221;, &#8220;mv&#8221;, &#8220;rm&#8221; is builtin, and its trivial to further extend the shell command vocabulary with more complex commands. We&#8217;ll show an example for howto extend with custom commands below.</p>
<p>As every flexible software, IPython comes with a main configuration file ($HOME/.ipython/ipythonrc). If we wanted a custom command, such as &#8220;chmod &lt;mod&gt; &lt;file&gt;&#8221; (chmod 755 myfile.py), we could add this to the &#8220;ipythonrc&#8221; file:</p>
<div class="code"><code><br />
# my custom chmod alias. By typing '&gt;&gt;&gt; chmod 755 myprog.py' or<br />
# '&gt;&gt;&gt; chmod a+rx myprog.py' IPython will execute this<br />
# statement as a shell command.<br />
alias chmod chmod %s %s<br />
</code></div>
<p>Also, debugging lists (tuples, dictionaries, etc) is more readable within the IPython, as it wraps all such print statements inside the &#8220;pprint&#8221; (<em>pretty-print</em>) module, and therefore a comprehensible representation will find place.</p>
<p>So, if you often find yourself in the python interpreter, I would highly recommend you spending a half an hour to get to known IPython. I promise you &#8211; it will save you a lot of headaches in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2008/11/ipython-enhanced-python-interpreter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New python magazine</title>
		<link>http://asbjorn.fellinghaug.com/blog/2008/10/new-python-magazine/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2008/10/new-python-magazine/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 14:26:27 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[magazine]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=125</guid>
		<description><![CDATA[Finally!
Its time for the monthly edition of the Python Magazine, which is a highly interesting and technial magazine regarding the python programming language.
Everyone who is in to Python should subscribe to this magazine, as it covers many &#8220;hot&#8221; topics, as well as presenting many howto tutorials for everyday challenges. From cutting edge web applications and [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;">Finally!</p>
<p style="text-align: left;">Its time for the monthly edition of the <a title="Python magazine" href="http://pymag.phparch.com/ "><strong>Python Magazine</strong></a>, which is a highly interesting and technial magazine regarding the python programming language.</p>
<div id="attachment_126" class="wp-caption alignnone" style="width: 377px"><a href="http://asbjorn.fellinghaug.com/blog/wp-content/uploads/2008/10/pymag_blog2.png"><img class="size-medium wp-image-126" title="pymag_blog2" src="http://asbjorn.fellinghaug.com/blog/wp-content/uploads/2008/10/pymag_blog2-271x300.png" alt="python magazine" width="367" height="405" /></a><p class="wp-caption-text">python magazine</p></div>
<p>Everyone who is in to Python should subscribe to this magazine, as it covers many &#8220;<em>hot</em>&#8221; topics, as well as presenting many howto tutorials for everyday challenges. From cutting edge web applications and frameworks, to desktop applications and backbone server implementations.</p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2008/10/new-python-magazine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vim og Python</title>
		<link>http://asbjorn.fellinghaug.com/blog/2008/10/vim-og-python/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2008/10/vim-og-python/#comments</comments>
		<pubDate>Fri, 17 Oct 2008 07:12:36 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[VIM]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=108</guid>
		<description><![CDATA[Hepp hepp!

VIM (Vi IMproved) er en tekst editor som bygger på den svært gamle og anerkjente editoren VI. VIM (heretter kalt Vim) er en svært avansert teksteditor som har fra første stund lagt til rette for å være svært konfigurerbar. Tekst editoren har sitt eget &#8220;skript&#8221; språk der enkle trivielle operasjoner er representert ved små [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;">Hepp hepp!</p>
<p style="text-align: center;">
<p style="text-align: left;"><a title="VIM tekst editor" href="http://www.vim.org">VIM (Vi IMproved)</a> er en tekst editor som bygger på den svært gamle og anerkjente editoren VI. VIM (heretter kalt Vim) er en svært avansert teksteditor som har fra første stund lagt til rette for å være svært konfigurerbar. Tekst editoren har sitt eget &#8220;skript&#8221; språk der enkle trivielle operasjoner er representert ved små kommandoer, som i sammenheng kan utføre mange spesialiserte operasjoner (alt etter hva ønsket er).</p>
<p style="text-align: left;">
<p style="text-align: left;">Vim har hatt et rykte på seg for å være en vanskelig editor, da den krever en litt spesiell tankegang i begynnelsen. Dog, etter å ha kommet over den første milepælen på læringskurven, så er verden forståelig igjen. Vim har forskjellige &#8220;modus&#8221; der man kan gjøre forskjellige ting. For eksempel har man skrivemodus som tilrettelegger for at det man skriver på tastaturet blir skrevet til dokumentet. Man har også et kommandomodus, der man skriver kommandoer som Vim skal utføre på dokumentet. Typiske kommandoer kan være &#8220;slett linjen, slett tegn, finn tekst, finn og erstatt tekst, flytt tekst, kopier, klipp ut, lim inn, etc..&#8221;.</p>
<p style="text-align: left;">
<p style="text-align: left;">Nå detter jeg litt ut her, men kan tipse om <a title="Vim beskrevet på norsk" href="http://www.vim.org/6k/features.no.txt">dette dokumentet</a> (på norsk) som beskriver Vim på en mye bedre måte. Siden Vim er så fleksibel og modulær som den er, så har mange aktive brukere skrevet &#8220;plugins&#8221; som er små spesialiserte programsnutter til Vim. Disse programsnuttene er gjerne direktet myntet mot en spesifikk oppgave; fargelegge spesielle ord, tegn, sørge for korrekt formatering gitt en spesiell filtype, etc. For de av dere som kjenner programmeringsspråket <a title="Python programming language" href="http://www.python.org">Python</a>, så trenger jeg sikkert ikke å si at det kreves litt av din IDE (<em>Integrated Development Environment</em>) for å sørge for riktig indent for blokker med kode, samt korrekt syntax, etc. Med andre ord vil en bra IDE hindre unødvendige og slurvete feil i Python kode, derfor kreves det en bra IDE for å effektivt skrive rask og bra kode.</p>
<p style="text-align: left;">
<p>Nå kommer det som sikkert ingen overraskelse at Vim fungerer utmerket som en IDE, selv om den aldri var ment for å fungere som det. Dette er en av de flotte egenskapene til Vim: den er skrevet såpass modulært at man faktisk kan sette på byggeklosser for å spesialisere den innenfor et felt. Det finnes mange python &#8220;plugins&#8221; til Vim, slik som fargelegging av Python kode (se bilde under/til siden). Andre automatiske oppgaver man gjerne vil delegere til &#8220;plugins&#8221; er at når man trykker &#8220;enter&#8221; for linjeskift så skal man holde seg på samme innrykksnivå. Dette fordi at Python ikke benytter slike klammeparanteres {} som f.eks C++ og Java.<img class="size-medium wp-image-110 alignright" style="border: 1px solid black;" title="vim2_python" src="http://asbjorn.fellinghaug.com/blog/wp-content/uploads/2008/10/vim2_python-300x214.png" alt="" width="426" height="305" /></p>
<p>Automatisk feilkorrigering er også ting man gjerne vil ta høyde for i en IDE, og det finnes det gode løsninger for i Vim. Deriblant finnes det &#8220;plugins&#8221; som forsikrer seg om at det er riktig syntaks på slike logiske uttrykk som if-elif-else krever, samt at det bestandig er en kolon helt til høyre for slike uttrykk, og at alle strenger startes og avsluttes av fnutter, slikt som &#8217;streng1&#8242;, &#8220;streng2&#8243;, &#8220;&#8221;"streng3&#8243;&#8221;". En annen faktor som har i større grad uttrykket styrken til en IDE er dens muligheter til å tilby <em>auto-completion</em> av kode. Ta for eksempel følgende lille kodesnutt:</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">sys</span>.<span style="color: #dc143c;">platform</span> == <span style="color: #483d8b;">'linux2'</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;you'r surely using Linux, right?&quot;</span></pre></div></div>

<p>så, når jeg så skriver &#8217;sys.&#8217; og vil gjerne ha opp hvilke funksjoner/klasser/variabler som kan aksesseres fra modulen &#8217;sys&#8217;, så vil jeg gjerne at IDE&#8217;en skal presentere en liste over disse alternativene.</p>
<p>Vim har innebygd en <em>auto-completion</em> feature, men denne baserer seg på alle enkeltord som allerede er skrevet i dokumentet. Så, gitt kodesnutten ovenfor så kan man taste &#8220;CTRL+n&#8221; for å få opp en liste med alternativer. <strong>Men</strong>, denne vil ikke funke for vårt problem, da vi gjerne vil ha alle funksjoner/klasser/variabler som ligger under modulen &#8220;sys&#8221;. Frykt ikke &#8211; det fins råd. I denne <a title="Python with a modular ide vim" href="http://blog.sontek.net/2008/05/11/python-with-a-modular-ide-vim/">blogposten</a> er det beskrevet hvordan man med enkle funksjoner kan muliggjøre en mer avansert <em>auto-completion</em> for Python. Merk at denne featuren krever at Vim er kompilert med Python støtte (de fleste Linux distroer har dette). Så, dersom du tar å editerer din $HOME/.vimrc fil, og legger til følgende:</p>
<div class="code"><code><br />
" enables python completion<br />
autocmd FileType python set omnifunc=pythoncomplete#Complete<br />
" maps CTRL+SPACE to autocompletion function<br />
inoremap &lt;Nul&gt; &lt;C-x&gt;&lt;C-o&gt;<br />
</code></div>
<p>Nå kan man enkelt få til den ønskede funksjonaliteten ved å skrive &#8220;sys.&#8221; også taste CTRL+SPACE. Da vil Vim editoren legge til et ekstra vindu helt øverst med forklarende tekst for den aktuelle funksjonene/klassen/variabelen, samt at man ved hjelp av piltastene kan bevege seg nedover i listen.</p>
<p><strong>Merk</strong> at dette er bare en av flere nyttige funksjoner for Vim som en IDE for Python. Vil på det sterkeste anbefale deg å lese hele denne blogposten* dersom du befinner deg i en situasjon der du gjerne vil benytte Vim til å kode Python. Legg også merke til at Vim kjører på mange plattformer: Linux, Mac, Windows. Og, den trenger ikke å kjøre i et terminalvindu, som man ofte ser på skjermbildene. Det finnes en GUI versjon (kalt gVim).</p>
<p>* <a href="http://blog.sontek.net/2008/05/11/python-with-a-modular-ide-vim/">http://blog.sontek.net/2008/05/11/python-with-a-modular-ide-vim/</a></p>
<p><a href="http://asbjorn.fellinghaug.com/blog/wp-content/uploads/2008/10/vim.png"><img class="alignnone size-medium wp-image-111" title="vim" src="http://asbjorn.fellinghaug.com/blog/wp-content/uploads/2008/10/vim-300x218.png" alt="" width="508" height="368" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2008/10/vim-og-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Finally the Python 2.6 is released</title>
		<link>http://asbjorn.fellinghaug.com/blog/2008/10/finally-the-python-26-is-released/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2008/10/finally-the-python-26-is-released/#comments</comments>
		<pubDate>Fri, 03 Oct 2008 10:38:41 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[2.6]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[v.2.6]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=94</guid>
		<description><![CDATA[Hi everyone!
2. October the python developmeant team released the Python v2.6. At http://planet.python.org there is a whole lot of other blogs listing all the new cool features, so I won&#8217;t use any space outlining them here.
However, I will encourage everyone to have a look at the documentation, as there is some valuable key features in [...]]]></description>
			<content:encoded><![CDATA[<p>Hi everyone!</p>
<p>2. October the python developmeant team released the Python v2.6. At http://planet.python.org there is a whole lot of other blogs listing all the new cool features, so I won&#8217;t use any space outlining them here.</p>
<p>However, I will encourage everyone to have a look at the documentation, as there is some valuable key features in this release which will in the long run revolutionize the way we program in Python.</p>
<p>In time I will cover some of these features.</p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2008/10/finally-the-python-26-is-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My master thesis</title>
		<link>http://asbjorn.fellinghaug.com/blog/2008/06/my-master-thesis/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2008/06/my-master-thesis/#comments</comments>
		<pubDate>Fri, 27 Jun 2008 09:12:43 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[lucene]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[school]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[master]]></category>
		<category><![CDATA[thesis]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=36</guid>
		<description><![CDATA[Since my master thesis is now delivered, I will dedicate some time to clean up the code and thoroughly document it. When I&#8217;m finished and the code is clean, I will make it freely available to the Apache Lucene community.
I will also make my master thesis freely available for download (see my &#8220;master thesis&#8221; page [...]]]></description>
			<content:encoded><![CDATA[<p>Since my master thesis is now delivered, I will dedicate some time to clean up the code and thoroughly document it. When I&#8217;m finished and the code is clean, I will make it freely available to the Apache Lucene community.</p>
<p>I will also make my master thesis freely available for download (see my <a href="http://asbjorn.fellinghaug.com/blog/master-thesis/">&#8220;master thesis&#8221;</a> page for more info), so documentation regarding the code is somewhat covered by the thesis. Also, the abstract goals for the code (since the code reflects the experiment) is outlined in the thesis, in addition to a presentation regarding the results and observations made.</p>
<p>Since I&#8217;m a huge fan of Python, I also thought of experiment with the performance of python and my bigram index. I would love to further enhance and maybe introduce some new improvements and such.. In time, I will create a project page for the &#8220;Bigram index&#8221; beneath my future django/turbogears website <a title="Asbjørn Alexander Fellinghaug" href="http://asbjorn.fellinghaug.com">http://asbjorn.fellinghaug.com/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2008/06/my-master-thesis/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Twitter from commandline</title>
		<link>http://asbjorn.fellinghaug.com/blog/2008/05/twitter-from-commandline/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2008/05/twitter-from-commandline/#comments</comments>
		<pubDate>Mon, 05 May 2008 19:26:44 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[Funny]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[lucene]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=24</guid>
		<description><![CDATA[I&#8217;ve been taken by the Twitter storm these days.. Damn, I should focus a hole lot more on my master report. Well, this took me only one little hour, so it&#8217;s not that waste of time..    So, I guess you have heard about the new &#8220;facebook&#8221; called Twitter? Well, its this new [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been taken by the Twitter storm these days.. Damn, I should focus a hole lot more on my master report. Well, this took me only one little hour, so it&#8217;s not that waste of time.. <img src='http://asbjorn.fellinghaug.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   So, I guess you have heard about the new &#8220;facebook&#8221; called <a title="Twitter" href="http://www.twitter.com">Twitter</a>? Well, its this new web community thing were people can write their current status for what they are doing in the world.. And, of course, one can follow friends and pay attention to were / what they are doing.. Now, after some time I found it rather heavy to enter the twitter webpage, login, and then post a new twitter message for each time I want to update my status. So, as a python fan I am, I created myself a little python script to capture this problem. It relies on the <a title="Python Twitter" href="http://code.google.com/p/python-twitter/">python-twitter</a> module available at the Google Code pages. So, lets have a look at the code. I have named this file &#8220;update.py&#8221;, however feel free to rename it.</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
</pre></td><td class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> twitter
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
&nbsp;
USERNAME=<span style="color: #483d8b;">&quot;&quot;</span>
PASSWORD=<span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> postNewMessage<span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span>:
    api = twitter.<span style="color: black;">Api</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    api = twitter.<span style="color: black;">Api</span><span style="color: black;">&#40;</span>username=<span style="color: #483d8b;">&quot;&quot;</span>, password=<span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>msg, <span style="color: #008000;">list</span><span style="color: black;">&#41;</span>:
        msg = <span style="color: #483d8b;">&quot; &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span>
    msg = <span style="color: #008000;">unicode</span><span style="color: black;">&#40;</span>msg, <span style="color: #483d8b;">&quot;utf-8&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">140</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;ERROR: Message can't be over 140 chars.&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        api.<span style="color: black;">PostUpdate</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;OK. Was %i chars in msg.&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">Exception</span>, e:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;FUck..&quot;</span>
&nbsp;
    api.<span style="color: black;">ClearCredentials</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <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>:
        t = <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>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>t<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>t<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">10</span>:
            <span style="color: #808080; font-style: italic;"># writes ./update &quot;hi there mate&quot;</span>
            postNewMessage<span style="color: black;">&#40;</span>t<span style="color: black;">&#91;</span>0<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #808080; font-style: italic;"># writes ./update hello world</span>
            postNewMessage<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;">1</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;fuck&quot;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2008/05/twitter-from-commandline/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Performing disitributed operation with python</title>
		<link>http://asbjorn.fellinghaug.com/blog/2008/05/performing-disitributed-operation-with-python/</link>
		<comments>http://asbjorn.fellinghaug.com/blog/2008/05/performing-disitributed-operation-with-python/#comments</comments>
		<pubDate>Thu, 01 May 2008 13:26:49 +0000</pubDate>
		<dc:creator>Asbjørn Alexander Fellinghaug</dc:creator>
				<category><![CDATA[distributed]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://asbjorn.fellinghaug.com/blog/?p=18</guid>
		<description><![CDATA[Hi.
In the worst writing rush in my master thesis I found it quite relaxing to enjoy some other reading than research papers and such. What I wanted to do is to create a master robot to control a set of other robots, whereas a robot is just a simple program running on a host. Pretty [...]]]></description>
			<content:encoded><![CDATA[<p>Hi.</p>
<p>In the worst writing rush in my master thesis I found it quite relaxing to enjoy some other reading than research papers and such. What I wanted to do is to create a master robot to control a set of other robots, whereas a robot is just a simple program running on a host. Pretty much the same thing that goes on in Botnets, but this is <strong>not</strong> a botnet though. My goal is to create a crawling system to crawl URIs, and download and parse the documents retrieved into plain text. Then, to create a <em><a title="Forward index in search engines" href="http://en.wikipedia.org/wiki/Index_%28search_engine%29">forward index</a> </em>in a efficient data structure. When this is done, I would make my not-yet-constructed indexer index those documents and construct an inverted index.</p>
<p>Now, this project is not meant to be any &#8220;enterprise&#8221; software or such, only a hobby or shall I say interest of my.</p>
<p>However, the core in such a crawling system is that is needs a way to communicate with each other. So, I&#8217;ve explored some distributed approaches performed in Python, and come up with some alternatives:</p>
<ul>
<li>Pyro: <strong>Py</strong>thon <strong>R</strong>emote <strong>O</strong>bject is very much the same as the good old CORBA.</li>
<li>XML-RPC: Has the advantage of beeing extreme simplistic. However, there are some overhead of using XML to transport <em>remote-procedure-calls</em>. I would guess that it is quite application dependent (do you need speed or simplicity?)</li>
<li>SOAP: Doh.. SOAP is a very heavy protocol widely used in web services. It is also very simple, given the appropriate tools, but a little deprecated in light of the other approaches.</li>
<li>Sockets: Using pure TCP/UDP sockets (low-level) is significantly fast in contrast to the above approaches. However, it requires some more programming and hence more time. There is a tradeoff here somewhere..</li>
</ul>
<p>We will here illustrate a simple &#8220;client-server&#8221; example based on the <strong>Pyro</strong> module. Our goal is to add two integers and return the result. Lets first consider the server:</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
</pre></td><td class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Pyro.<span style="color: black;">core</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Pyro.<span style="color: black;">naming</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MathServer<span style="color: black;">&#40;</span>Pyro.<span style="color: black;">core</span>.<span style="color: black;">ObjBase</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> add<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, a, b<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> a+b
&nbsp;
daemon=Pyro.<span style="color: black;">core</span>.<span style="color: black;">Daemon</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
ns=Pyro.<span style="color: black;">naming</span>.<span style="color: black;">NameServerLocator</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">getNS</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
daemon.<span style="color: black;">useNameServer</span><span style="color: black;">&#40;</span>ns<span style="color: black;">&#41;</span>
uri=daemon.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>MathServer<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,<span style="color: #483d8b;">&quot;mathserver&quot;</span><span style="color: black;">&#41;</span>
daemon.<span style="color: black;">requestLoop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&lt;</span>/code<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>/div<span style="color: #66cc66;">&gt;</span>
So, basically what we do here <span style="color: #ff7700;font-weight:bold;">is</span> to create a simple Class <span style="color: black;">&#40;</span>MathServer<span style="color: black;">&#41;</span> which enherits <span style="color: #ff7700;font-weight:bold;">from</span> Pyro.<span style="color: black;">core</span>.<span style="color: black;">ObjBase</span>. <span style="color: black;">Next</span> we simply create our methods as <span style="color: #ff7700;font-weight:bold;">for</span> a standard <span style="color: #ff7700;font-weight:bold;">class</span>. <span style="color: black;">Later</span> on we <span style="color: #483d8b;">&quot;start&quot;</span> the server, <span style="color: #ff7700;font-weight:bold;">and</span> connects an instance of MathServer to our Pyro server <span style="color: #ff7700;font-weight:bold;">and</span> awaits incoming connections. <span style="color: black;">Lets</span> now consider the Pyro client:
<span style="color: #66cc66;">&lt;</span>div <span style="color: #ff7700;font-weight:bold;">class</span>=<span style="color: #483d8b;">&quot;code&quot;</span><span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>code<span style="color: #66cc66;">&gt;</span>
<span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Pyro.<span style="color: black;">core</span>
mathserver = Pyro.<span style="color: black;">core</span>.<span style="color: black;">getProxyForURI</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;PYRONAME://mathserver&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> mathserver.<span style="color: black;">add</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1000</span>, <span style="color: #ff4500;">2500</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># will return 3500.</span>
<span style="color: #ff7700;font-weight:bold;">print</span> mathserver.<span style="color: black;">add</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;hello &quot;</span>, <span style="color: #483d8b;">&quot;world&quot;</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># will return &quot;hello world&quot;</span></pre></td></tr></table></div>

<p>In our client code we take advantage of the simple name service which is shipped with Pyro. Basically what happens is that we instruct our client to lookup the Pyro name server and then connect to the provided controller named &#8220;mathserver&#8221; (recall from the server code?). </p>
<p>One important note is that to be able to use the name server that is shipped with Pyro, it will need to be started. In Ubuntu Gutsy the name server is disabled by default, so changing the value &#8220;ENABLED=0&#8243; to 1 in file /etc/default/pyro-nsd, and then &#8220;/etc/init.d/pyro-nsd start&#8221; will do the trick.</p>
]]></content:encoded>
			<wfw:commentRss>http://asbjorn.fellinghaug.com/blog/2008/05/performing-disitributed-operation-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
