<?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>Inspirated &#187; Flag 42</title>
	<atom:link href="http://inspirated.com/tag/flag-42/feed" rel="self" type="application/rss+xml" />
	<link>http://inspirated.com</link>
	<description>krkhan&#039;s blog</description>
	<lastBuildDate>Thu, 05 Apr 2012 16:19:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Using Boyer-Moore-Horspool algorithm on file streams in Python</title>
		<link>http://inspirated.com/2010/06/19/using-boyer-moore-horspool-algorithm-on-file-streams-in-python</link>
		<comments>http://inspirated.com/2010/06/19/using-boyer-moore-horspool-algorithm-on-file-streams-in-python#comments</comments>
		<pubDate>Fri, 18 Jun 2010 23:53:36 +0000</pubDate>
		<dc:creator>krkhan</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Boyer-Moore-Horspool]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[File Handling]]></category>
		<category><![CDATA[Flag 42]]></category>
		<category><![CDATA[GSoC]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Search]]></category>
		<category><![CDATA[Unicode]]></category>
		<category><![CDATA[UTF-8]]></category>

		<guid isPermaLink="false">http://inspirated.com/?p=294</guid>
		<description><![CDATA[Horspool&#8217;s algorithm is a simple and efficient string-searching algorithm which trades space for time and performs better as length of search string is increased. Another (perhaps overlooked) advantage of this algorithm is its ability to search through stream files without requiring random access. As I was working on Launchpad for my SoC project I required [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore%E2%80%93Horspool_algorithm">Horspool&#8217;s algorithm</a> is a simple and efficient string-searching algorithm which trades space for time and performs better as length of search string is increased. Another (perhaps overlooked) advantage of this algorithm is its ability to search through stream files without requiring random access. As I was working on Launchpad for my SoC project I required this particular stream-handling attribute as the file descriptors opened by <code>urllib2</code> didn&#8217;t support <code>seek()</code>ing. Modifying the example code from Wiki page a little, I was able to <code>read()</code> only the required bytes sequentially:</p>
<p style="text-align: center"><a href="http://inspirated.com/uploads/horspool.zip">horspool.py</a></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">locale</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">urllib2</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> boyermoore_horspool<span style="color: black;">&#40;</span>fd, needle<span style="color: black;">&#41;</span>:
    nlen = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>needle<span style="color: black;">&#41;</span>
    nlast = nlen - <span style="color: #ff4500;">1</span>
&nbsp;
    skip = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">256</span><span style="color: black;">&#41;</span>:
        skip.<span style="color: black;">append</span><span style="color: black;">&#40;</span>nlen<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>nlast<span style="color: black;">&#41;</span>:
        skip<span style="color: black;">&#91;</span><span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>needle<span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> = nlast - k
    skip = <span style="color: #008000;">tuple</span><span style="color: black;">&#40;</span>skip<span style="color: black;">&#41;</span>
&nbsp;
    pos = <span style="color: #ff4500;">0</span>
    consumed = <span style="color: #ff4500;">0</span>
    haystack = <span style="color: #dc143c;">bytes</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
        more = nlen - <span style="color: black;">&#40;</span>consumed - pos<span style="color: black;">&#41;</span>
        morebytes = fd.<span style="color: black;">read</span><span style="color: black;">&#40;</span>more<span style="color: black;">&#41;</span>
        haystack = haystack<span style="color: black;">&#91;</span>more:<span style="color: black;">&#93;</span> + morebytes
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>morebytes<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> more:
            <span style="color: #ff7700;font-weight:bold;">return</span> -<span style="color: #ff4500;">1</span>
        consumed = consumed + more
&nbsp;
        i = nlast
        <span style="color: #ff7700;font-weight:bold;">while</span> i <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> haystack<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> == needle<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span>:
            i = i - <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> i == -<span style="color: #ff4500;">1</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> pos
&nbsp;
        pos = pos + skip<span style="color: black;">&#91;</span><span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>haystack<span style="color: black;">&#91;</span>nlast<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> -<span style="color: #ff4500;">1</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;">&lt;</span> <span style="color: #ff4500;">3</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Usage: horspool.py &lt;url&gt; &lt;search text&gt;&quot;</span>
        <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
    url = <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>
    needle = <span style="color: #dc143c;">sys</span>.<span style="color: black;">argv</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>
    needle = needle.<span style="color: black;">decode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'string_escape'</span><span style="color: black;">&#41;</span>
&nbsp;
    fd = <span style="color: #dc143c;">urllib2</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
    offset = boyermoore_horspool<span style="color: black;">&#40;</span>fd, needle<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">hex</span><span style="color: black;">&#40;</span>offset<span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'::'</span>, offset
    fd.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Now comes the fun part:</p>
<ul>
<li>The code can search through any URL without downloading it completely, stopping at the first match. For example, the following command will download only the first few bytes of the provided URL:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>horspool.py http:<span style="color: #000000; font-weight: bold;">//</span>www.gutenberg.org<span style="color: #000000; font-weight: bold;">/</span>files<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">132</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">132</span>.txt <span style="color: #ff0000;">&quot;The Art of War&quot;</span></pre></div></div>

<blockquote><p>0x1d :: 29</p></blockquote>
</li>
<li>Unicode searches work perfectly as well. Although the matching takes place according to the character encoding of the terminal used. That&#8217;s to say, since I&#8217;m using a UTF-8 terminal the &#8220;bytes&#8221; searched were assumed to be UTF-8 encoded as well:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>horspool.py http:<span style="color: #000000; font-weight: bold;">//</span>www.gutenberg.org<span style="color: #000000; font-weight: bold;">/</span>files<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">29011</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">29011</span>-<span style="color: #000000;">0</span>.txt <span style="color: #ff0000;">&quot;Σημείωση: Ο Πίνακας περιεχομένων&quot;</span></pre></div></div>

<blockquote><p>0x44f :: 1103</p></blockquote>
</li>
<li>Same goes for multi-line searches:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ .<span style="color: #000000; font-weight: bold;">/</span>horspool.py http:<span style="color: #000000; font-weight: bold;">//</span>www.gutenberg.org<span style="color: #000000; font-weight: bold;">/</span>files<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">29011</span><span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">29011</span>-<span style="color: #000000;">0</span>.txt <span style="color: #ff0000;">&quot;διευκόλυνση<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>του αναγνώστη&quot;</span></pre></div></div>

<blockquote><p>0x4b5 :: 1205</p></blockquote>
</li>
</ul>
<div class="shr-publisher-294"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><hr />
<p><small><a href="http://inspirated.com/2010/06/19/using-boyer-moore-horspool-algorithm-on-file-streams-in-python">Permalink</a> |
<a href="http://inspirated.com/2010/06/19/using-boyer-moore-horspool-algorithm-on-file-streams-in-python#comments">One comment</a>
<br/>
Post tags: <a href="http://inspirated.com/tag/algorithms" rel="tag">Algorithms</a>, <a href="http://inspirated.com/tag/boyer-moore-horspool" rel="tag">Boyer-Moore-Horspool</a>, <a href="http://inspirated.com/tag/code" rel="tag">Code</a>, <a href="http://inspirated.com/tag/file-handling" rel="tag">File Handling</a>, <a href="http://inspirated.com/tag/flag-42" rel="tag">Flag 42</a>, <a href="http://inspirated.com/tag/gsoc" rel="tag">GSoC</a>, <a href="http://inspirated.com/tag/python" rel="tag">Python</a>, <a href="http://inspirated.com/tag/search" rel="tag">Search</a>, <a href="http://inspirated.com/tag/unicode" rel="tag">Unicode</a>, <a href="http://inspirated.com/tag/utf-8" rel="tag">UTF-8</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://inspirated.com/2010/06/19/using-boyer-moore-horspool-algorithm-on-file-streams-in-python/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>HOWTO: Query WordPress posts in CMS Made Simple</title>
		<link>http://inspirated.com/2010/05/16/howto-query-wordpress-posts-in-cms-made-simple</link>
		<comments>http://inspirated.com/2010/05/16/howto-query-wordpress-posts-in-cms-made-simple#comments</comments>
		<pubDate>Sun, 16 May 2010 10:53:34 +0000</pubDate>
		<dc:creator>krkhan</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[CMS Made Simple]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Flag 42]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[Workaround]]></category>

		<guid isPermaLink="false">http://inspirated.com/?p=279</guid>
		<description><![CDATA[While I run my blog at inspirated.com, I aggregate posts related to coding at the subdomain code.inspirated.com. The websites are run through WordPress and CMS Made Simple respectively. For the latter, I needed to find a way of fetching blog posts from the main site for linking. Initially, I used tag feeds for this purpose. [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>While I run my blog at <code>inspirated.com</code>, I aggregate posts related to coding at the subdomain <a href="http://code.inspirated.com/"><code>code.inspirated.com</code></a>. The websites are run through WordPress and CMS Made Simple respectively.</p>
<p>For the latter, I needed to find a way of fetching blog posts from the main site for linking. Initially, I used tag feeds for this purpose. That is, I used the RSS module for CMS-MS and fetched the feed for a particular tag (e.g., <a href="http://inspirated.com/tag/code/feed">inspirated.com/tag/code/feed</a>). This worked well for a while until the feeds became large and I noticed that only the most recent 10 posts were showing up in the listings.</p>
<p>Digging around, I found <a href="http://codex.wordpress.org/Function_Reference/query_posts">this</a> piece of documentation which explains how one can use custom queries for collecting WordPress posts from a blog. There was a catch however as the code for doing so could only be run globally. In other words, if I tried running the code mentioned on the page inside a User Defined Tag in CMS-MS I would get strange errors.</p>
<p>The solution was to run the code in a separate PHP file. Here&#8217;s how:</p>
<ol>
<li>Create a file named <code>wp.php</code> in your CMS-MS folder with the following code:

<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="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/path/to/wordpress/wp-blog-header.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// edit the path in the line above to point to your wp-blog-header.php</span>
&nbsp;
<span style="color: #000088;">$tag</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'--tag'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'--tag'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'code'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'--count'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'--count'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'-1'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$after</span> <span style="color: #339933;">=</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'--after'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'--after'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'1970-01-01'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> filter_where<span style="color: #009900;">&#40;</span><span style="color: #000088;">$where</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$after</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$where</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; AND post_date &gt;= '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$after</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$where</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'posts_where'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'filter_where'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
query_posts<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'tag='</span><span style="color: #339933;">.</span><span style="color: #000088;">$tag</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'&amp;posts_per_page='</span><span style="color: #339933;">.</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;ul&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> the_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/a&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot; (&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> the_time<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'F jS, Y'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;li&gt;No posts found&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/ul&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
wp_reset_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</li>
<li>Add a User Defined Tag in CMS-MS with the following code:

<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'whoami | php -q /path/to/wp.php'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// edit the path in the line above to point to the wp.php created in previous step</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// whoami command piped for no reason because my script wasn't</span>
<span style="color: #666666; font-style: italic;">// producing any output without it</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tag'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$path</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' --tag='</span><span style="color: #339933;">.</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'tag'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'count'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$path</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' --count='</span><span style="color: #339933;">.</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'count'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'after'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$path</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">' --after='</span><span style="color: #339933;">.</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'after'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> `<span style="color: #000088;">$path</span>`<span style="color: #339933;">;</span></pre></td></tr></table></div>

</li>
<li>Use the tag in any CMS-MS page with any of the following combinations:
<ul>
<li>List all posts with the tag <code>code</code>:

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{wp_posts_with_tag tag=&quot;code&quot;}</pre></div></div>

</li>
<li>List 10 posts with the tag <code>code</code>:

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{wp_posts_with_tag tag=&quot;code&quot; count=&quot;10&quot;}</pre></div></div>

</li>
<li>List all posts with the tag <code>code</code> after May 1st, 2009:

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">{wp_posts_with_tag tag=&quot;code&quot; after=&quot;2009-05-01&quot;}</pre></div></div>

</li>
</ul>
</li>
</ol>
<p>Custom queries are very powerful once you get them working. Anyone planning on using them should take a look at the <a href="http://codex.wordpress.org/Function_Reference/query_posts">function reference</a> for getting to grips with the flexibility they offer.</p>
<div class="shr-publisher-279"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><hr />
<p><small><a href="http://inspirated.com/2010/05/16/howto-query-wordpress-posts-in-cms-made-simple">Permalink</a> |
<a href="http://inspirated.com/2010/05/16/howto-query-wordpress-posts-in-cms-made-simple#comments">2 comments</a>
<br/>
Post tags: <a href="http://inspirated.com/tag/cms-made-simple" rel="tag">CMS Made Simple</a>, <a href="http://inspirated.com/tag/code" rel="tag">Code</a>, <a href="http://inspirated.com/tag/flag-42" rel="tag">Flag 42</a>, <a href="http://inspirated.com/tag/php" rel="tag">PHP</a>, <a href="http://inspirated.com/tag/wordpress" rel="tag">WordPress</a>, <a href="http://inspirated.com/tag/workaround" rel="tag">Workaround</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://inspirated.com/2010/05/16/howto-query-wordpress-posts-in-cms-made-simple/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Faking User-Agent with PyS60</title>
		<link>http://inspirated.com/2010/02/07/faking-user-agent-with-pys60</link>
		<comments>http://inspirated.com/2010/02/07/faking-user-agent-with-pys60#comments</comments>
		<pubDate>Sat, 06 Feb 2010 19:00:25 +0000</pubDate>
		<dc:creator>krkhan</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Flag 42]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Nokia]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Proxy]]></category>
		<category><![CDATA[PyS60]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Series 60]]></category>
		<category><![CDATA[Symbian]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[User-Agent]]></category>

		<guid isPermaLink="false">http://inspirated.com/?p=262</guid>
		<description><![CDATA[&#8220;Anyone who slaps a &#8216;this page is best viewed with Browser X&#8217; label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network.&#8221; &#8212; Tim Berners-Lee in Technology Review, [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><blockquote><p>&#8220;Anyone who slaps a &#8216;this page is best viewed with Browser X&#8217; label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network.&#8221; &#8212; <em>Tim Berners-Lee</em> in Technology Review, July 1996</p></blockquote>
<p>People never learn. Slapping such labels is one thing, they even go as far as adopting brain-dead practices of checking user-agent strings and refusing service to any browser not originating from Redmond. For example, Opera Mobile &#8212; the sexiest mobile application on planet &#8212; works astonishingly well for Javascript websites. Nevertheless, when trying to browse my university&#8217;s academic management portal on it I am presented with a big ugly <em>&#8220;We&#8217;re dumb, you need to open this page with Internet Explorer 6.0 or later because it uses JAVASCRIPTXX0RZ.&#8221;</em> Even though Opera does allow spoofing of user-agent strings, the fake strings still contained &#8220;Symbian&#8221; as the operating system which still resulted in incompatibility errors.</p>
<p>As ever, Python came to the rescue. Firing up the Twisted framework, I created a simple HTTP proxy which modifies the user-agent string on the fly. Peaches:</p>
<p style="text-align: center"><img src="http://inspirated.com/uploads/useragentproxy-0.1-1.jpg" alt="Opera Mobile User Agent Spoofed" /><br />
<img src="http://inspirated.com/uploads/useragentproxy-0.1-2.jpg" alt="User-Agent Proxy Screeshot" /></p>
<p>The script is still very quirky and is the farthest thing from what you&#8217;d call a stable solution. You can download the inital release <a href="http://inspirated.com/uploads/useragentproxy-0.1.zip">here</a>. The zip file contains the tiny proxy server script as well as Twisted and Zope dependencies. Good luck with trying to counter retards who&#8217;re doing everything they can to avoid compatibility. Yes, even 14 years after Sir Tim&#8217;s veracious proclamation.</p>
<div class="shr-publisher-262"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><hr />
<p><small><a href="http://inspirated.com/2010/02/07/faking-user-agent-with-pys60">Permalink</a> |
<a href="http://inspirated.com/2010/02/07/faking-user-agent-with-pys60#comments">One comment</a>
<br/>
Post tags: <a href="http://inspirated.com/tag/code" rel="tag">Code</a>, <a href="http://inspirated.com/tag/flag-42" rel="tag">Flag 42</a>, <a href="http://inspirated.com/tag/internet" rel="tag">Internet</a>, <a href="http://inspirated.com/tag/nokia" rel="tag">Nokia</a>, <a href="http://inspirated.com/tag/open-source" rel="tag">Open Source</a>, <a href="http://inspirated.com/tag/proxy" rel="tag">Proxy</a>, <a href="http://inspirated.com/tag/pys60" rel="tag">PyS60</a>, <a href="http://inspirated.com/tag/python" rel="tag">Python</a>, <a href="http://inspirated.com/tag/series-60" rel="tag">Series 60</a>, <a href="http://inspirated.com/tag/symbian" rel="tag">Symbian</a>, <a href="http://inspirated.com/tag/technology" rel="tag">Technology</a>, <a href="http://inspirated.com/tag/user-agent" rel="tag">User-Agent</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://inspirated.com/2010/02/07/faking-user-agent-with-pys60/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>(GUI, Mathematical Equations, Scientific Plotting) = (GTK+, LaTeX, Matplotlib)</title>
		<link>http://inspirated.com/2010/01/06/gui-mathematical-equations-scientific-plotting-gtk-latex-matplotlib</link>
		<comments>http://inspirated.com/2010/01/06/gui-mathematical-equations-scientific-plotting-gtk-latex-matplotlib#comments</comments>
		<pubDate>Wed, 06 Jan 2010 05:16:17 +0000</pubDate>
		<dc:creator>krkhan</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Equation]]></category>
		<category><![CDATA[Flag 42]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[GTK+]]></category>
		<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[Matplotlib]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Plot]]></category>
		<category><![CDATA[PyGTK]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[TeX]]></category>

		<guid isPermaLink="false">http://inspirated.com/?p=259</guid>
		<description><![CDATA[GTK+ needs no introduction. LaTeX is the first thing that pops in anyone&#8217;s mind if mathematical equations&#8217; typesetting is under consideration. Matplotlib &#8212; while not as well-known as the former two &#8212; is the super easy and elegant solution for scientific plotting on *nix platforms. For an application demo, I required all three. Past experience [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>GTK+ needs no introduction. LaTeX is the first thing that pops in anyone&#8217;s mind if mathematical equations&#8217; typesetting is under consideration. Matplotlib &#8212; while not as well-known as the former two &#8212; is the super easy and elegant solution for scientific plotting on *nix platforms.</p>
<p>For an application demo, I required all three. Past experience has taught me that the most straightforward way of &#8220;gluing&#8221; things together is Python. GTK+ therefore = PyGTK. Next up was LaTeX, and a previous solution of mine for <a href="http://inspirated.com/2009/09/10/howto-use-latex-mathematical-expressions-in-pygtk">embedding LaTeX in PyGTK</a> came to the rescue. The final requirement of Matplotlib was fulfilled without any hassle since the library was already written in Python.</p>
<p>The collective result was pretty:</p>
<p style="text-align: center"><a href="http://inspirated.com/uploads/radareq-0.1.tar.gz">radareq-0.1.tar.gz</a></p>
<p style="text-align: center; font-size: x-small"><a href="http://inspirated.com/uploads/gtk-latex-matplotlib.png"><img src="http://inspirated.com/uploads/gtk-latex-matplotlib-thumb.jpg" alt="Screenshot of GTK+ with LaTeX and Matplotlib" width="400" height="316" /></a><br />
(Click on the image for larger version.)</p>
<p>The linked tarball contains the Python scripts for the application. For everything to run smoothly, LaTeX and Matplotlib packages need to be installed on your system. If you encounter any issues running the code, feel free to flame your distribution for the apparent lack of sanity regarding package management.</p>
<div class="shr-publisher-259"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><hr />
<p><small><a href="http://inspirated.com/2010/01/06/gui-mathematical-equations-scientific-plotting-gtk-latex-matplotlib">Permalink</a> |
<a href="http://inspirated.com/2010/01/06/gui-mathematical-equations-scientific-plotting-gtk-latex-matplotlib#comments">2 comments</a>
<br/>
Post tags: <a href="http://inspirated.com/tag/code" rel="tag">Code</a>, <a href="http://inspirated.com/tag/equation" rel="tag">Equation</a>, <a href="http://inspirated.com/tag/flag-42" rel="tag">Flag 42</a>, <a href="http://inspirated.com/tag/graphics" rel="tag">Graphics</a>, <a href="http://inspirated.com/tag/gtk" rel="tag">GTK+</a>, <a href="http://inspirated.com/tag/latex" rel="tag">LaTeX</a>, <a href="http://inspirated.com/tag/matplotlib" rel="tag">Matplotlib</a>, <a href="http://inspirated.com/tag/open-source" rel="tag">Open Source</a>, <a href="http://inspirated.com/tag/plot" rel="tag">Plot</a>, <a href="http://inspirated.com/tag/pygtk" rel="tag">PyGTK</a>, <a href="http://inspirated.com/tag/python" rel="tag">Python</a>, <a href="http://inspirated.com/tag/tex" rel="tag">TeX</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://inspirated.com/2010/01/06/gui-mathematical-equations-scientific-plotting-gtk-latex-matplotlib/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>HOWTO: Use PyS60&#8242;s Bluetooth Console on Fedora/Ubuntu/Debian Linux</title>
		<link>http://inspirated.com/2009/10/31/howto-use-pys60s-bluetooth-console-on-fedoraubuntudebian-linux</link>
		<comments>http://inspirated.com/2009/10/31/howto-use-pys60s-bluetooth-console-on-fedoraubuntudebian-linux#comments</comments>
		<pubDate>Sat, 31 Oct 2009 18:03:50 +0000</pubDate>
		<dc:creator>krkhan</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Bluetooth]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Debian]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Fedora]]></category>
		<category><![CDATA[Flag 42]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Nokia]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PyS60]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Series 60]]></category>
		<category><![CDATA[Symbian]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://inspirated.com/?p=250</guid>
		<description><![CDATA[While developing PyS60 apps is one of the most fun things you could do with your Nokia phone, debugging them isn&#8217;t as zippy as one would hope for in a Py development environment. To make up for that, PyS60 gives the developers an option for directly connecting to the interpreter through Bluetooth. Doesn&#8217;t sound very [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>While developing PyS60 apps is one of the most fun things you could do with your Nokia phone, debugging them isn&#8217;t as zippy as one would hope for in a Py development environment. To make up for that, PyS60 gives the developers an option for directly connecting to the interpreter through Bluetooth. Doesn&#8217;t sound very appealing? How about this: You connect your laptop with the cellphone, jump in at some place in the code while your app is executing and then use lappy&#8217;s big keyboard for exploiting different code and values in the interpreter. Sounds better?</p>
<p>To accomplish this on a Linux distro, you will need the following packages installed on your system:</p>
<table align="center" summary="Required packages" cellspacing="3" cellpadding="3">
<tr>
<th align="center" style="font-size: small; border: 1px dotted">Name</th>
<th align="center" style="font-size: small; border: 1px dotted">Links</th>
</tr>
<tr>
<td align="center" style="font-size: small; border: 1px dotted"><code>gnome-bluetooth</code></td>
<td align="center" style="font-size: small; border: 1px dotted">
<ul>
<li><a href="https://admin.fedoraproject.org/pkgdb/packages/name/gnome-bluetooth">Fedora</a></li>
<li><a href="http://packages.ubuntu.com/karmic/gnome-bluetooth">Ubuntu</a></li>
<li><a href="http://packages.debian.org/lenny/gnome-bluetooth">Debian</a></li>
</ul>
</td>
</tr>
<tr>
<td align="center" style="font-size: small; border: 1px dotted"><code>uucp</code>/<code>cu</code></td>
<td align="center" style="font-size: small; border: 1px dotted">
<ul>
<li><a href="https://admin.fedoraproject.org/pkgdb/packages/name/uucp">Fedora</a></li>
<li><a href="http://packages.ubuntu.com/karmic/cu">Ubuntu</a></li>
<li><a href="http://packages.debian.org/lenny/cu">Debian</a></li>
</ul>
</td>
</tr>
</table>
<p>After making sure that both are present on your system, <a href="http://wiki.opensource.nokia.com/projects/Installing_PyS60">install PyS60</a> on your phone if you haven&#8217;t already done so.</p>
<p>Now the fun part:</p>
<ol>
<li>Switch on Bluetooth in the cellphone.
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-mobile-1.jpg" alt="PyS60 Bluetooth HOWTO, Mobile screenshot #1" /></p>
</li>
<li>Launch <code>bluetooth-properties</code> and click on &#8220;Setup New Device&#8221;.
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-pc-1.jpg" alt="PyS60 Bluetooth HOWTO, PC screenshot #1" /></p>
</li>
<li>Select your cellphone.
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-pc-2.jpg" alt="PyS60 Bluetooth HOWTO, PC screenshot #2" /></p>
</li>
<li>You will be shown a pin.
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-pc-3.jpg" alt="PyS60 Bluetooth HOWTO, PC screenshot #3" /></p>
</li>
<li>Enter the pin when queried on the cellphone.
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-mobile-2.jpg" alt="PyS60 Bluetooth HOWTO, Mobile screenshot #2" /></p>
</li>
<li>The phone should be successfully paired.
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-pc-4.jpg" alt="PyS60 Bluetooth HOWTO, PC screenshot #4" /></p>
</li>
<li>Authorize your Linux system to make automatic connections to the phone.
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-mobile-3.jpg" alt="PyS60 Bluetooth HOWTO, Mobile screenshot #3" /></p>
</li>
<li>As <code>root</code>, run <a href="http://inspirated.com/uploads/rfcomm-listen.sh">this</a> shell script:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>orthanc ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># ./rfcomm-listen.sh</span></pre></div></div>

<blockquote><p>Serial Port service registered<br />
Waiting for connection on channel 2
</p></blockquote>
</li>
<li>Launch PyS60 interpreter and select &#8220;Bluetooth Console&#8221; from the application menu.
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-mobile-4.jpg" alt="PyS60 Bluetooth HOWTO, Mobile screenshot #4" /></p>
</li>
<li>Select your Linux machine.
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-mobile-5.jpg" alt="PyS60 Bluetooth HOWTO, Mobile screenshot #5" /></p>
<p>The command you ran in previous step should have <strong>new</strong> output:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>orthanc ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># ./rfcomm-listen.sh</span></pre></div></div>

<blockquote><p>Serial Port service registered<br />
Waiting for connection on channel 2<br />
<strong>Connection from 00:17:4B:B6:35:31 to /dev/rfcomm0<br />
Press CTRL-C for hangup</strong>
</p></blockquote>
<p>The cellphone screen should be showing something like this:</p>
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-mobile-6.jpg" alt="PyS60 Bluetooth HOWTO, Mobile screenshot #6" /></p>
</li>
<li>As <code>root</code> again, open a new terminal and run:

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#91;</span>root<span style="color: #000000; font-weight: bold;">@</span>orthanc ~<span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #666666; font-style: italic;"># cu -l /dev/rfcomm0</span></pre></div></div>

<blockquote><p>Connected.</p></blockquote>
</li>
<li>Hit Enter till prompt (<code>>>></code>) appears, then type:<br />
<blockquote><p>&gt;&gt;&gt; import appuifw<br />
&gt;&gt;&gt; appuifw.query(u&#39;Hello World&#39;, &#39;text&#39;)</p></blockquote>
</li>
<li>Viola, you should have an input box on the mobile screen:
<p style="text-align: center; font-size: x-small"><img src="http://inspirated.com/uploads/pys60-rfcomm-mobile-7.jpg" alt="PyS60 Bluetooth HOWTO, Mobile screenshot #7" /></p>
</li>
<li>Enter any text and press the OK key. It should be show up in the terminal you were using to type in code:<br />
<blockquote><p>&gt;&gt;&gt; import appuifw<br />
&gt;&gt;&gt; appuifw.query(u&#39;Hello World&#39;, &#39;text&#39;)<br />
<strong>u&#8217;Finally&#8217;</strong></p></blockquote>
</li>
<li>Exit the interpreter by typing <code>CTRL+D</code> on an empty line:<br />
<blockquote><p>&gt;&gt;&gt; import appuifw<br />
&gt;&gt;&gt; appuifw.query(u&#39;Hello World&#39;, &#39;text&#39;)<br />
u&#8217;Finally&#8217;<br />
&gt;&gt;&gt;<br />
<strong>Interactive interpreter finished.<br />
cu: Got hangup signal</strong></p>
<p><strong>Disconnected.</strong>
</p></blockquote>
</li>
</ol>
<p>Pat yourself on the back. Now, you can use your Bluetooth console to <code>import</code> your modules, execute some stuff and then jump in the middle to test some extra lines or values. In fact, I found it to be a pretty darned good way of learning about PyS60&#8242;s API. Res secundae!</p>
<div class="shr-publisher-250"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><hr />
<p><small><a href="http://inspirated.com/2009/10/31/howto-use-pys60s-bluetooth-console-on-fedoraubuntudebian-linux">Permalink</a> |
<a href="http://inspirated.com/2009/10/31/howto-use-pys60s-bluetooth-console-on-fedoraubuntudebian-linux#comments">4 comments</a>
<br/>
Post tags: <a href="http://inspirated.com/tag/bluetooth" rel="tag">Bluetooth</a>, <a href="http://inspirated.com/tag/code" rel="tag">Code</a>, <a href="http://inspirated.com/tag/debian" rel="tag">Debian</a>, <a href="http://inspirated.com/tag/debugging" rel="tag">Debugging</a>, <a href="http://inspirated.com/tag/fedora" rel="tag">Fedora</a>, <a href="http://inspirated.com/tag/flag-42" rel="tag">Flag 42</a>, <a href="http://inspirated.com/tag/linux" rel="tag">Linux</a>, <a href="http://inspirated.com/tag/mobile" rel="tag">Mobile</a>, <a href="http://inspirated.com/tag/nokia" rel="tag">Nokia</a>, <a href="http://inspirated.com/tag/open-source" rel="tag">Open Source</a>, <a href="http://inspirated.com/tag/pys60" rel="tag">PyS60</a>, <a href="http://inspirated.com/tag/python" rel="tag">Python</a>, <a href="http://inspirated.com/tag/series-60" rel="tag">Series 60</a>, <a href="http://inspirated.com/tag/symbian" rel="tag">Symbian</a>, <a href="http://inspirated.com/tag/technology" rel="tag">Technology</a>, <a href="http://inspirated.com/tag/tutorial" rel="tag">Tutorial</a>, <a href="http://inspirated.com/tag/ubuntu" rel="tag">Ubuntu</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://inspirated.com/2009/10/31/howto-use-pys60s-bluetooth-console-on-fedoraubuntudebian-linux/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>HOWTO: Use LaTeX mathematical expressions in PyGTK</title>
		<link>http://inspirated.com/2009/09/10/howto-use-latex-mathematical-expressions-in-pygtk</link>
		<comments>http://inspirated.com/2009/09/10/howto-use-latex-mathematical-expressions-in-pygtk#comments</comments>
		<pubDate>Thu, 10 Sep 2009 17:04:19 +0000</pubDate>
		<dc:creator>krkhan</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Equation]]></category>
		<category><![CDATA[Flag 42]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[GTK+]]></category>
		<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PyGTK]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[TeX]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://inspirated.com/?p=243</guid>
		<description><![CDATA[I had never really laid my hands on LaTeX until I required it in one of the helper applications for my graduation project. Unfortunately, the requirement wasn&#8217;t as simple as producing some documents as I had to embed mathematical expressions on the fly in my PyGTK apps. Googling around for the solution, I found GtkMathView [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I had never really laid my hands on LaTeX until I required it in one of the helper applications for my graduation project. Unfortunately, the requirement wasn&#8217;t as simple as producing some documents as I had to embed mathematical expressions on the fly in my PyGTK apps. Googling around for the solution, I found <a href="http://helm.cs.unibo.it/mml-widget/">GtkMathView</a> which accomplished something similar to this albeit using MathML. However, my luck ran out on me again as the widget lacked Python bindings. The other solution was to generate transparent PNGs on the fly and include them as <code>GtkImage</code>s. This worked rather well, as the final code allowed easy modifications to the generated expressions.</p>
<p>Requirements for the code were:</p>
<ul>
<li><a href="http://www.latex-project.org/">LaTeX</a></li>
<li><a href="http://sourceforge.net/projects/dvipng/">dvipng</a></li>
<li><a href="http://code.google.com/p/latexmath2png/">latexmath2png</a></li>
</ul>
<p>Final results:</p>
<div style="text-align: center"><img src="http://inspirated.com/uploads/gtktex.jpg" alt="LaTeX in PyGTK" /></div>
<p>And the simple code behind it:</p>
<div style="text-align: center"><a href="http://inspirated.com/uploads/gtktex.zip">gtktex.py</a></div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #483d8b;">&quot;&quot;&quot;An example demonstrating usage of latexmath2png module for embedding math
equations in PyGTK
&nbsp;
Author: Kamran Riaz Khan &lt;krkhan@inspirated.com&gt;
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> gtk
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> latexmath2png
&nbsp;
pre = <span style="color: #483d8b;">'gtktex_'</span>
eqs = <span style="color: black;">&#91;</span>
	r<span style="color: #483d8b;">'$<span style="color: #000099; font-weight: bold;">\a</span>lpha_i &gt; <span style="color: #000099; font-weight: bold;">\b</span>eta_i$'</span>,
	r<span style="color: #483d8b;">'$<span style="color: #000099; font-weight: bold;">\s</span>um_{i=0}^<span style="color: #000099; font-weight: bold;">\i</span>nfty x_i$'</span>,
	r<span style="color: #483d8b;">'$<span style="color: #000099; font-weight: bold;">\l</span>eft(<span style="color: #000099; font-weight: bold;">\f</span>rac{5 - <span style="color: #000099; font-weight: bold;">\f</span>rac{1}{x}}{4}<span style="color: #000099; font-weight: bold;">\r</span>ight)$'</span>,
	r<span style="color: #483d8b;">'$s(t) = <span style="color: #000099; font-weight: bold;">\m</span>athcal{A}<span style="color: #000099; font-weight: bold;">\s</span>in(2 <span style="color: #000099; font-weight: bold;">\o</span>mega t)$'</span>,
	r<span style="color: #483d8b;">'$<span style="color: #000099; font-weight: bold;">\s</span>um_{n=1}^<span style="color: #000099; font-weight: bold;">\i</span>nfty<span style="color: #000099; font-weight: bold;">\f</span>rac{-e^{i<span style="color: #000099; font-weight: bold;">\p</span>i}}{2^n}$'</span>
	<span style="color: black;">&#93;</span>
latexmath2png.<span style="color: black;">math2png</span><span style="color: black;">&#40;</span>eqs, <span style="color: #dc143c;">os</span>.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, prefix = pre<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> window_destroy<span style="color: black;">&#40;</span>widget<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>eqs<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
		<span style="color: #dc143c;">os</span>.<span style="color: black;">unlink</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'%s%d.png'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>pre, i + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	gtk.<span style="color: black;">main_quit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
window = gtk.<span style="color: black;">Window</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
window.<span style="color: black;">set_border_width</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
window.<span style="color: black;">set_title</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'LaTeX Equations in GTK'</span><span style="color: black;">&#41;</span>
window.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'destroy'</span>, window_destroy<span style="color: black;">&#41;</span>
vbox = gtk.<span style="color: black;">VBox</span><span style="color: black;">&#40;</span>spacing = <span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>
window.<span style="color: black;">add</span><span style="color: black;">&#40;</span>vbox<span style="color: black;">&#41;</span>
&nbsp;
images = <span style="color: black;">&#91;</span><span style="color: #008000;">None</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">*</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>eqs<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>eqs<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
	images<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span> = gtk.<span style="color: black;">image_new_from_file</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'%s%d.png'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>pre, i + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
	vbox.<span style="color: black;">pack_start</span><span style="color: black;">&#40;</span>images<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
window.<span style="color: black;">show_all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
gtk.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<div class="shr-publisher-243"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><hr />
<p><small><a href="http://inspirated.com/2009/09/10/howto-use-latex-mathematical-expressions-in-pygtk">Permalink</a> |
<a href="http://inspirated.com/2009/09/10/howto-use-latex-mathematical-expressions-in-pygtk#comments">2 comments</a>
<br/>
Post tags: <a href="http://inspirated.com/tag/code" rel="tag">Code</a>, <a href="http://inspirated.com/tag/equation" rel="tag">Equation</a>, <a href="http://inspirated.com/tag/flag-42" rel="tag">Flag 42</a>, <a href="http://inspirated.com/tag/graphics" rel="tag">Graphics</a>, <a href="http://inspirated.com/tag/gtk" rel="tag">GTK+</a>, <a href="http://inspirated.com/tag/latex" rel="tag">LaTeX</a>, <a href="http://inspirated.com/tag/open-source" rel="tag">Open Source</a>, <a href="http://inspirated.com/tag/pygtk" rel="tag">PyGTK</a>, <a href="http://inspirated.com/tag/python" rel="tag">Python</a>, <a href="http://inspirated.com/tag/tex" rel="tag">TeX</a>, <a href="http://inspirated.com/tag/tutorial" rel="tag">Tutorial</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://inspirated.com/2009/09/10/howto-use-latex-mathematical-expressions-in-pygtk/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>HOWTO: Add grid lines to a GtkDrawingArea in PyGTK</title>
		<link>http://inspirated.com/2009/08/30/howto-add-grid-lines-to-a-gtkdrawingarea-in-pygtk</link>
		<comments>http://inspirated.com/2009/08/30/howto-add-grid-lines-to-a-gtkdrawingarea-in-pygtk#comments</comments>
		<pubDate>Sun, 30 Aug 2009 13:29:49 +0000</pubDate>
		<dc:creator>krkhan</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Flag 42]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[GTK+]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[PyGTK]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://inspirated.com/?p=242</guid>
		<description><![CDATA[Recently, I needed to create some visualizations in a drawing area for one of my PyGTK apps. The PyGTK tutorial had an excellent writeup complete with a working example for immediate help. Unfortunately, my requirements were of a &#8220;grid&#8221; view on which other objects would be drawn. Naturally, I hoped for some variable on the [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Recently, I needed to create some visualizations in a drawing area for one of my PyGTK apps. The PyGTK tutorial had an excellent <a href="http://pygtk.org/pygtk2tutorial/sec-DrawingMethods.html">writeup</a> complete with a working example for immediate help. Unfortunately, my requirements were of a &#8220;grid&#8221; view on which other objects would be drawn. Naturally, I hoped for some variable on the <code>GtkDrawingArea</code> which I would set to true and have the grid lines drawn automatically. But since I couldn&#8217;t find any such magic setting in the API, I had to draw the lines manually.</p>
<p>Consequently, I could use either of the following approaches:</p>
<ul>
<li>Loop horizontally and vertically while <code>draw_line()</code>ing.</li>
<li>Draw a 10&#215;10 box as a tile and repeat it over the background.</li>
</ul>
<p>The first approach would&#8217;ve worked faster but involved writing more code. The second was uglier &#8212; especially for larger images &#8212; but resulted in me having to add only a few lines to the example:</p>
<div style="text-align: center"><a href="http://inspirated.com/uploads/drawingareagrid.zip">drawingareagrid.py</a></div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">        xpm_data = <span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;10 10 2 1&quot;</span>,
            <span style="color: #483d8b;">&quot; 	c #EEEEEE&quot;</span>,
            <span style="color: #483d8b;">&quot;.	c #FFFFFF&quot;</span>,
            <span style="color: #483d8b;">&quot;          &quot;</span>,
            <span style="color: #483d8b;">&quot; ........ &quot;</span>,
            <span style="color: #483d8b;">&quot; ........ &quot;</span>,
            <span style="color: #483d8b;">&quot; ........ &quot;</span>,
            <span style="color: #483d8b;">&quot; ........ &quot;</span>,
            <span style="color: #483d8b;">&quot; ........ &quot;</span>,
            <span style="color: #483d8b;">&quot; ........ &quot;</span>,
            <span style="color: #483d8b;">&quot; ........ &quot;</span>,
            <span style="color: #483d8b;">&quot; ........ &quot;</span>,
            <span style="color: #483d8b;">&quot;          &quot;</span><span style="color: black;">&#93;</span>
        tile_pixmap, tile_mask = gtk.<span style="color: black;">gdk</span>.<span style="color: black;">pixmap_create_from_xpm_d</span><span style="color: black;">&#40;</span>
            area.<span style="color: black;">window</span>, <span style="color: #008000;">None</span>, xpm_data<span style="color: black;">&#41;</span>
        tile_gc = area.<span style="color: black;">window</span>.<span style="color: black;">new_gc</span><span style="color: black;">&#40;</span>fill = gtk.<span style="color: black;">gdk</span>.<span style="color: black;">TILED</span>,
            tile = tile_pixmap<span style="color: black;">&#41;</span>
        width = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">hruler</span>.<span style="color: black;">get_range</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        height = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">vruler</span>.<span style="color: black;">get_range</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        area.<span style="color: black;">window</span>.<span style="color: black;">draw_rectangle</span><span style="color: black;">&#40;</span>tile_gc, <span style="color: #008000;">True</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, width, height<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Turning this:</p>
<div style="text-align: center"><img src="http://inspirated.com/uploads/drawingareaexample.jpg" alt="GtkDrawingArea Example" /></div>
<p>Into this:</p>
<div style="text-align: center"><img src="http://inspirated.com/uploads/drawingareagridexample.jpg" alt="GtkDrawingArea Grid Example" /></div>
<p>Note that the 10&#215;10 tiles&#8217; borders match prettily with the markers on top and left rulers. There will definitely be some more efficient method for achieving this but for the time being tiling serves my needs perfectly.</p>
<div class="shr-publisher-242"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><hr />
<p><small><a href="http://inspirated.com/2009/08/30/howto-add-grid-lines-to-a-gtkdrawingarea-in-pygtk">Permalink</a> |
<a href="http://inspirated.com/2009/08/30/howto-add-grid-lines-to-a-gtkdrawingarea-in-pygtk#comments">One comment</a>
<br/>
Post tags: <a href="http://inspirated.com/tag/code" rel="tag">Code</a>, <a href="http://inspirated.com/tag/flag-42" rel="tag">Flag 42</a>, <a href="http://inspirated.com/tag/graphics" rel="tag">Graphics</a>, <a href="http://inspirated.com/tag/gtk" rel="tag">GTK+</a>, <a href="http://inspirated.com/tag/open-source" rel="tag">Open Source</a>, <a href="http://inspirated.com/tag/pygtk" rel="tag">PyGTK</a>, <a href="http://inspirated.com/tag/python" rel="tag">Python</a>, <a href="http://inspirated.com/tag/tutorial" rel="tag">Tutorial</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://inspirated.com/2009/08/30/howto-add-grid-lines-to-a-gtkdrawingarea-in-pygtk/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing a minimal shell in Python</title>
		<link>http://inspirated.com/2009/08/01/writing-a-minimal-shell-in-python</link>
		<comments>http://inspirated.com/2009/08/01/writing-a-minimal-shell-in-python#comments</comments>
		<pubDate>Fri, 31 Jul 2009 20:48:10 +0000</pubDate>
		<dc:creator>krkhan</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Flag 42]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Rants]]></category>
		<category><![CDATA[Shell]]></category>
		<category><![CDATA[shlex]]></category>
		<category><![CDATA[subprocess]]></category>

		<guid isPermaLink="false">http://inspirated.com/?p=236</guid>
		<description><![CDATA[Being a Unix geek has its own little benefits. For example, when a friend wants you to code a shell for his Operating Systems assignment, you can extort a lunch out of him for the services. Moreover, if you can somehow get him to convince his teacher for accepting submissions in Python, you can have [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Being a Unix geek has its own little benefits. For example, when a friend wants you to code a shell for his Operating Systems assignment, you can extort a lunch out of him for the services. Moreover, if you can somehow get him to convince his teacher for accepting submissions in Python, you can have sex with the code itself.</p>
<p>The bare-essentials shell was required to have the following:</p>
<ul>
<li>Support for internal commands such as <code>cd</code> or <code>ls</code> which would be implemented using system calls.</li>
<li>Support for launching external programs with arguments provided to the shell &#8212; optionally putting them to background with an ampersand at the end of the arguments.</li>
</ul>
<p>That is it. No fancy intput/output stream redirection, no jobs, no pipes. Just a simple launcher. A quick doze of reflective programming resulted in a class that checked the given commands against member functions (e.g., <code>Shell.on_cd</code> is queried for &#8220;<code>cd path</code>&#8220;) and called them to process the input. In case of unrecognized commands, the default handler is called which in turn uses the <code>subprocess</code> module and attempts to launch the command as an external program. Support for escaped or quoted arguments wasn&#8217;t required (the assignment recommended tokenizing strings at spaces), but the godsent module &#8230; :</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">shlex</span>
<span style="color: #808080; font-style: italic;"># ...</span>
args = <span style="color: #dc143c;">shlex</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span>command<span style="color: black;">&#41;</span></pre></div></div>

<p>&#8230; allowed me to support proper argument parsing with only half a line of code anyway. Similarly, connecting to the standard input/output of the child program wasn&#8217;t in the requirements, but the simple call &#8230; :</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">subprocess</span>
<span style="color: #808080; font-style: italic;"># ...</span>
proc = <span style="color: #dc143c;">subprocess</span>.<span style="color: black;">Popen</span><span style="color: black;">&#40;</span>args,
	executable = args<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>,
	stdin = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdin</span>,
	stdout = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span>,
	stderr = <span style="color: #dc143c;">sys</span>.<span style="color: black;">stderr</span><span style="color: black;">&#41;</span></pre></div></div>

<p>&#8230; did the trick perfectly; allowing proper executions of commands such as &#8220;<code>cat -</code>&#8220;.</p>
<p>The complete code is downloadable <a href="http://inspirated.com/uploads/mshell.zip">here</a>. Unfortunately, coding was the easy part. Getting the guy to pay for the extorted lunch wasn&#8217;t and as of this writing, I&#8217;m still deadlocked on that front.</p>
<div class="shr-publisher-236"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><hr />
<p><small><a href="http://inspirated.com/2009/08/01/writing-a-minimal-shell-in-python">Permalink</a> |
<a href="http://inspirated.com/2009/08/01/writing-a-minimal-shell-in-python#comments">No comment</a>
<br/>
Post tags: <a href="http://inspirated.com/tag/code" rel="tag">Code</a>, <a href="http://inspirated.com/tag/flag-42" rel="tag">Flag 42</a>, <a href="http://inspirated.com/tag/python" rel="tag">Python</a>, <a href="http://inspirated.com/tag/rants" rel="tag">Rants</a>, <a href="http://inspirated.com/tag/shell" rel="tag">Shell</a>, <a href="http://inspirated.com/tag/shlex" rel="tag">shlex</a>, <a href="http://inspirated.com/tag/subprocess" rel="tag">subprocess</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://inspirated.com/2009/08/01/writing-a-minimal-shell-in-python/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>User-defined iterators in Python</title>
		<link>http://inspirated.com/2009/05/14/user-defined-iterators-in-python</link>
		<comments>http://inspirated.com/2009/05/14/user-defined-iterators-in-python#comments</comments>
		<pubDate>Thu, 14 May 2009 00:19:19 +0000</pubDate>
		<dc:creator>krkhan</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Classes]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Flag 42]]></category>
		<category><![CDATA[Iterators]]></category>
		<category><![CDATA[Object Oriented Programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Sex]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.inspirated.com/wordpress/?p=219</guid>
		<description><![CDATA[Iterable classes are one of the features which make Python code more readable. Simply put, they let you iterate over a container a la: 1 2 for s in &#40;&#34;Spam&#34;, &#34;Eggs&#34;&#41;: print s Here, s iterates over the tuple printing the words one by one.: Spam Eggs Now comes the interesting part: How do I [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Iterable classes are one of the features which make Python code more readable. Simply put, they let you iterate over a container a la:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> s <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Spam&quot;</span>, <span style="color: #483d8b;">&quot;Eggs&quot;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">print</span> s</pre></td></tr></table></div>

<p>Here, <code>s</code> iterates over the tuple printing the words one by one.:</p>
<blockquote><p>Spam<br />
Eggs</p></blockquote>
<p>Now comes the interesting part: How do I make my own classes iterable? The official <a href="http://docs.python.org/tutorial/classes.html#iterators">Python Tutorial</a> gives a working example for how to do 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
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Reverse:
	<span style="color: #483d8b;">&quot;Iterator for looping over a sequence backwards&quot;</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, data<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">data</span> = data
		<span style="color: #008000;">self</span>.<span style="color: black;">index</span> = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__iter__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> next<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">index</span> == <span style="color: #ff4500;">0</span>:
			<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">StopIteration</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">index</span> = <span style="color: #008000;">self</span>.<span style="color: black;">index</span> - <span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>.<span style="color: black;">index</span><span style="color: black;">&#93;</span>
&nbsp;
value = Reverse<span style="color: black;">&#40;</span><span style="color: #483d8b;">'spam'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> char <span style="color: #ff7700;font-weight:bold;">in</span> value:
		<span style="color: #ff7700;font-weight:bold;">print</span> char</pre></td></tr></table></div>

<p>Output:</p>
<blockquote><p>m<br />
a<br />
p<br />
s</p></blockquote>
<p>The example appeared perfectly fine to a beginner like me. However, since I&#8217;m just kinda twisted in the head, I added a new line in the <code>for</code> loop:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>16
17
18
19
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">value = Reverse<span style="color: black;">&#40;</span><span style="color: #483d8b;">'spam'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> char <span style="color: #ff7700;font-weight:bold;">in</span> value:
	<span style="color: #ff7700;font-weight:bold;">if</span> char <span style="color: #ff7700;font-weight:bold;">in</span> value:
		<span style="color: #ff7700;font-weight:bold;">print</span> char</pre></td></tr></table></div>

<p>Which resulted in the (quite unexpected) output:</p>
<blockquote><p>&nbsp;</p></blockquote>
<p>That&#8217;s it. Nothing. Even though the code should make perfect sense and does work in case of built-in types. For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">tup = <span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Spam&quot;</span>, <span style="color: #483d8b;">&quot;Eggs&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> s <span style="color: #ff7700;font-weight:bold;">in</span> tup:
	<span style="color: #ff7700;font-weight:bold;">if</span> s <span style="color: #ff7700;font-weight:bold;">in</span> tup: 
		<span style="color: #ff7700;font-weight:bold;">print</span> s</pre></td></tr></table></div>

<p>So daisy-ly gives:</p>
<blockquote><p>Spam<br />
Eggs</p></blockquote>
<p>The culprit in case of tutorial&#8217;s example for user-defined iterators? After toying around the code sample a little, here&#8217;s what I pinned down:</p>
<ul>
<li>On the nested lines where another iterator is required, the <code>Reverse</code> class is supposed to return <em>instance</em> of an iterator which would define the <code>next()</code> method for returning successive values.</li>
<li>Since the <code>Reverse</code> class returns only it<code>self</code> in this scenario, the <code>self.index</code> variable is shared among iterators of the <code>Reverse('spam')</code>.</li>
<li>As a result, <code>Reverse.next()</code> raises the <code>StopIteration</code> in the nested condition.</li>
</ul>
<hr />
<p>Once I understood the underlying problem, some further head-scratching and a can of malted drink resulted in the solutions:</p>
<ul>
<li>Return a <code>copy</code> for the iterative functions instead of the instance it<code>self</code>:

<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
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">copy</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Reverse:
	<span style="color: #483d8b;">&quot;Iterator for looping over a sequence backwards&quot;</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, data<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">data</span> = data
		<span style="color: #008000;">self</span>.<span style="color: black;">index</span> = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>data<span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__iter__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">copy</span>.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> next<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">index</span> == <span style="color: #ff4500;">0</span>:
			<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">StopIteration</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">index</span> = <span style="color: #008000;">self</span>.<span style="color: black;">index</span> - <span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>.<span style="color: black;">index</span><span style="color: black;">&#93;</span>
&nbsp;
value = Reverse<span style="color: black;">&#40;</span><span style="color: #483d8b;">'spam'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> char <span style="color: #ff7700;font-weight:bold;">in</span> value:
	<span style="color: #ff7700;font-weight:bold;">if</span> char <span style="color: #ff7700;font-weight:bold;">in</span> value:
		<span style="color: #ff7700;font-weight:bold;">print</span> char</pre></td></tr></table></div>

<p><em>Pro:</em> Less strain on the programmer, only a couple of extra lines of code are needed.<br />
<em>Con:</em> <code>copy</code>ing the instance can be expensive in case of larger containers.</p>
</li>
<li>Use another class:

<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" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Reverse:
	<span style="color: #483d8b;">&quot;Iterator for looping over a sequence backwards&quot;</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, data<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">data</span> = data
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__iter__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> ReverseIter<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ReverseIter:
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, inst<span style="color: black;">&#41;</span>:
		<span style="color: #008000;">self</span>.<span style="color: black;">inst</span> = inst
		<span style="color: #008000;">self</span>.<span style="color: black;">index</span> = <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">inst</span>.<span style="color: black;">data</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> next<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">index</span> == <span style="color: #ff4500;">0</span>:
			<span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">StopIteration</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">index</span> = <span style="color: #008000;">self</span>.<span style="color: black;">index</span> - <span style="color: #ff4500;">1</span>
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">inst</span>.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>.<span style="color: black;">index</span><span style="color: black;">&#93;</span>
&nbsp;
value = Reverse<span style="color: black;">&#40;</span><span style="color: #483d8b;">'spam'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> char <span style="color: #ff7700;font-weight:bold;">in</span> value:
	<span style="color: #ff7700;font-weight:bold;">if</span> char <span style="color: #ff7700;font-weight:bold;">in</span> value:
		<span style="color: #ff7700;font-weight:bold;">print</span> char</pre></td></tr></table></div>

<p><em>Pro:</em> Since the whole container is not copied, only the <em>index</em> is unique among iterators &#8212; less burden on the memory.<br />
<em>Con:</em> Not everyone likes defining new classes.</p>
</li>
</ul>
<p>Both solutions worked equally well and resulted in the same output (the expected one this time):</p>
<blockquote><p>m<br />
a<br />
p<br />
s
</p></blockquote>
<p>The choice of either solution is solely dependent on the programmer&#8217;s preference. As a side note, after equating Python programming with carnal activities in few of my previous posts, I&#8217;m gonna take it to the next step and finally tag this post accordingly.</p>
<div class="shr-publisher-219"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><hr />
<p><small><a href="http://inspirated.com/2009/05/14/user-defined-iterators-in-python">Permalink</a> |
<a href="http://inspirated.com/2009/05/14/user-defined-iterators-in-python#comments">2 comments</a>
<br/>
Post tags: <a href="http://inspirated.com/tag/classes" rel="tag">Classes</a>, <a href="http://inspirated.com/tag/code" rel="tag">Code</a>, <a href="http://inspirated.com/tag/example" rel="tag">Example</a>, <a href="http://inspirated.com/tag/flag-42" rel="tag">Flag 42</a>, <a href="http://inspirated.com/tag/iterators" rel="tag">Iterators</a>, <a href="http://inspirated.com/tag/object-oriented-programming" rel="tag">Object Oriented Programming</a>, <a href="http://inspirated.com/tag/oop" rel="tag">OOP</a>, <a href="http://inspirated.com/tag/open-source" rel="tag">Open Source</a>, <a href="http://inspirated.com/tag/python" rel="tag">Python</a>, <a href="http://inspirated.com/tag/sex" rel="tag">Sex</a>, <a href="http://inspirated.com/tag/technology" rel="tag">Technology</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://inspirated.com/2009/05/14/user-defined-iterators-in-python/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>&#8220;All methods in Python are effectively virtual&#8221;</title>
		<link>http://inspirated.com/2009/05/03/all-methods-in-python-are-effectively-virtual</link>
		<comments>http://inspirated.com/2009/05/03/all-methods-in-python-are-effectively-virtual#comments</comments>
		<pubDate>Sun, 03 May 2009 15:07:20 +0000</pubDate>
		<dc:creator>krkhan</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Dive Into Python]]></category>
		<category><![CDATA[Example]]></category>
		<category><![CDATA[Flag 42]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[Method]]></category>
		<category><![CDATA[Object Oriented Programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Polymorphism]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Virtual]]></category>

		<guid isPermaLink="false">http://www.inspirated.com/wordpress/?p=213</guid>
		<description><![CDATA[Dive Into Python really is one of the best programming books I have ever laid my hands on. Short, concise and to-the-point. The somewhat unorthodox approach of presenting an alien-looking program at the start of each chapter and then gradually building towards making it comprehensible is extraordinarily captivating. With that said, here&#8217;s an excerpt from [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><a href="http://diveintopython.org/">Dive Into Python</a> really is one of the best programming books I have ever laid my hands on. Short, concise and to-the-point. The somewhat unorthodox approach of presenting an alien-looking program at the start of each chapter and then gradually building towards making it comprehensible is extraordinarily captivating. With that said, here&#8217;s an <a href="http://diveintopython.org/object_oriented_framework/userdict.html">excerpt from the chapter introducing</a> Python&#8217;s object orientation framework:</p>
<blockquote><p>Guido, the original author of Python, explains method overriding this way: &#8220;Derived classes may override methods of their base classes. Because methods have no special privileges when calling other methods of the same object, a method of a base class that calls another method defined in the same base class, may in fact end up calling a method of a derived class that overrides it. (<em>For C++ programmers: all methods in Python are effectively virtual.</em>)&#8221; If that doesn&#8217;t make sense to you (it confuses the hell out of me), feel free to ignore it. I just thought I&#8217;d pass it along.</p></blockquote>
<p>If you were able to comprehend the full meaning of that paragraph in a single go, you most definitely are one of the following:</p>
<ul>
<li>Guido van Rossum himself</li>
<li>Donald Ervin Knuth</li>
<li>Pinocchio</li>
</ul>
<p>Neither of which happens to be my identity, so it took me around three rereads to grasp the idea. It brought back memories of an interesting question that I used to ask students while I was working as a teacher&#8217;s assistant for the C++ course: &#8220;What is a virtual function?&#8221; The answer <em>always</em> involved pointers and polymorphism; completely ignoring any impact virtual functions would be having on inheritance in referential/non-pointer scenarios. (Considering that most of the C++ books never attempt to portray the difference either, I didn&#8217;t blame the students much.) Confused again? Here&#8217;s some more food for thought: Python does not even have pointers, so what do these perpetually virtual functions <em>really</em> entail in its universe? Let&#8217;s make everything peachy with a nice example.</p>
<p>Consider a <code>Base</code> class in C++ which defines three functions:</p>
<ul>
<li><code>hello()</code></li>
<li><code>hello_non_virtual()</code></li>
<li><code>hello_virtual()</code></li>
</ul>
<p>The first function, i.e., <code>hello()</code> calls the latter two (<code>hello_non_virtual()</code> and <code>hello_virtual()</code>). Now, we inherit a <code>Derived</code> class from the <code>Base</code>, and override the functions:</p>
<ul>
<li><code>hello_non_virtual()</code></li>
<li><code>hello_virtual()</code></li>
</ul>
<p>Note that the <code>hello()</code> function is <strong>not</strong> defined in the <code>Derived</code> class. Now, what happens when someone calls <code>Derived::hello()</code>? The answer:</p>
<p style="text-align:center"><img src="http://www.inspirated.com/uploads/virtual-method.jpg" alt="Mechanism of virtual function invocation" /></p>
<p>Since <code>Derived::hello()</code> does not exist, <code>Base::hello()</code> is called instead. Which, in turn, calls <code>hello_non_virtual()</code> and <code>hello_virtual()</code>. For the non-virtual function call, the <code>Base::hello_non_virtual()</code> function is executed. For the virtual function call, the overridden <code>Derived::hello_virtual()</code> is called instead.</p>
<p>Here&#8217;s the test code for C++:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Base <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">void</span> hello<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Hello called from Base&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
&nbsp;
		hello_non_virtual<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		hello_virtual<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">void</span> hello_non_virtual<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Hello called from non-virtual Base function&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> hello_virtual<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Hello called from virtual Base function&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Derived <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> Base <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">void</span> hello_non_virtual<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Hello called from non-virtual Derived function&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">void</span> hello_virtual<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span><span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;Hello called from virtual Derived function&quot;</span><span style="color: #000080;">&lt;&lt;</span>endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Derived d<span style="color: #008080;">;</span>
&nbsp;
	d.<span style="color: #007788;">hello</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>And its output:</p>
<blockquote><p>Hello called from Base<br />
Hello called from non-virtual Base function<br />
Hello called from virtual Derived function</p></blockquote>
<p>Similarly, a Python program to illustrate the statement <em>&#8220;all methods in Python are effectively virtual&#8221;</em>:</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
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Base:
	<span style="color: #ff7700;font-weight:bold;">def</span> hello<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Hello called from Base&quot;</span>
&nbsp;
		<span style="color: #008000;">self</span>.<span style="color: black;">hello_virtual</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> hello_virtual<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Hello called from virtual Base function&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Derived<span style="color: black;">&#40;</span>Base<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">def</span> hello_virtual<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Hello called from virtual Derived function&quot;</span>
&nbsp;
d = Derived<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
d.<span style="color: black;">hello</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Output:</p>
<blockquote><p>Hello called from Base<br />
Hello called from virtual Derived function</p></blockquote>
<p>I hope this clears up the <em>always-virtual</em> concept for other Python newcomers as well. As far as my experience with the language itself is concerned, Python is sex; simple as that. Mere two days after picking up my first Python book for reading, I have fallen in love with its elegance, simplicity and overall highly addictive nature.</p>
<div class="shr-publisher-213"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><hr />
<p><small><a href="http://inspirated.com/2009/05/03/all-methods-in-python-are-effectively-virtual">Permalink</a> |
<a href="http://inspirated.com/2009/05/03/all-methods-in-python-are-effectively-virtual#comments">One comment</a>
<br/>
Post tags: <a href="http://inspirated.com/tag/c" rel="tag">C++</a>, <a href="http://inspirated.com/tag/class" rel="tag">Class</a>, <a href="http://inspirated.com/tag/code" rel="tag">Code</a>, <a href="http://inspirated.com/tag/dive-into-python" rel="tag">Dive Into Python</a>, <a href="http://inspirated.com/tag/example" rel="tag">Example</a>, <a href="http://inspirated.com/tag/flag-42" rel="tag">Flag 42</a>, <a href="http://inspirated.com/tag/function" rel="tag">Function</a>, <a href="http://inspirated.com/tag/method" rel="tag">Method</a>, <a href="http://inspirated.com/tag/object-oriented-programming" rel="tag">Object Oriented Programming</a>, <a href="http://inspirated.com/tag/oop" rel="tag">OOP</a>, <a href="http://inspirated.com/tag/open-source" rel="tag">Open Source</a>, <a href="http://inspirated.com/tag/polymorphism" rel="tag">Polymorphism</a>, <a href="http://inspirated.com/tag/python" rel="tag">Python</a>, <a href="http://inspirated.com/tag/virtual" rel="tag">Virtual</a><br/>
</small></p>]]></content:encoded>
			<wfw:commentRss>http://inspirated.com/2009/05/03/all-methods-in-python-are-effectively-virtual/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.758 seconds -->

