<?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>~Nacho/blog</title>
	<atom:link href="http://criptonita.com/~nacho/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://criptonita.com/~nacho/blog</link>
	<description>the cutting edge of my mind!</description>
	<lastBuildDate>Fri, 22 Apr 2011 14:01:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Finding equilibrium</title>
		<link>http://criptonita.com/~nacho/blog/2011/04/22/finding-equilibrium/</link>
		<comments>http://criptonita.com/~nacho/blog/2011/04/22/finding-equilibrium/#comments</comments>
		<pubDate>Fri, 22 Apr 2011 12:41:26 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=462</guid>
		<description><![CDATA[Carlos shared a pretty cool link with me this morning. Codility is a platform to help recruiters and contractors to test developers before hiring them. Both the idea and the execution are beautiful. They&#8217;ve done a stunning job. As part of my visit to the page I gave a try to the demo test. As [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.carlosdf.com">Carlos</a> shared a pretty cool link with me this morning. <a href="http://www.codility.com">Codility</a> is a platform to help recruiters and contractors to test developers before hiring them. Both the idea and the execution are beautiful. They&#8217;ve done a stunning job. </p>
<p>As part of my visit to the page I gave a try to the demo test. As it was funny to solve it, I&#8217;m sharing my solution here. The problem was quite easy. It reads like this:</p>
<blockquote><p>Equilibrium index of a sequence is an index such that the sum of elements at lower indexes is equal to the sum of elements at higher indexes. For example, in a sequence A:</p>
<p>A[0]=-7 A[1]=1 A[2]=5 A[3]=2 A[4]=-4 A[5]=3 A[6]=0</p>
<p>3 is an equilibrium index, because:</p>
<p>A[0]+A[1]+A[2]=A[4]+A[5]+A[6]</p>
<p>6 is also an equilibrium index, because:</p>
<p>A[0]+A[1]+A[2]+A[3]+A[4]+A[5]=0</p>
<p>(sum of zero elements is zero) 7 is not an equilibrium index, because it is not a valid index of sequence A.</p>
<p>Assume the sum of zero elements is equal zero. Write a function int equi(int[] A); that given a sequence, returns its equilibrium index (<strong>any</strong>) or -1 if no equilibrium indexes exist. <strong>Assume that the sequence may be very long</strong>. </p></blockquote>
<p>They gave me 30 minutes to design, code and test my solution. Here it is (tests omitted):</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;">operator</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> equi<span style="color: black;">&#40;</span>A<span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">if</span> A <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>A<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> -<span style="color: #ff4500;">1</span>
  lsum = index = <span style="color: #ff4500;">0</span>
  rsum = <span style="color: #008000;">reduce</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">operator</span>.<span style="color: black;">add</span>, A<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
  <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> lsum == rsum:
      <span style="color: #ff7700;font-weight:bold;">return</span> index
    index += <span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> index == <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>A<span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">return</span> -<span style="color: #ff4500;">1</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
      lsum += A<span style="color: black;">&#91;</span>index-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
      rsum -= A<span style="color: black;">&#91;</span>index<span style="color: black;">&#93;</span></pre></div></div>

<p>It scores 100/100 and runs in linear time (that&#8217;s tricky because O(n<sup>2</sup>) algorithms will cause performance tests to fail) :)</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2011/04/22/finding-equilibrium/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Local public transportation in my pocket</title>
		<link>http://criptonita.com/~nacho/blog/2011/03/29/local-public-pocket/</link>
		<comments>http://criptonita.com/~nacho/blog/2011/03/29/local-public-pocket/#comments</comments>
		<pubDate>Tue, 29 Mar 2011 12:08:51 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=417</guid>
		<description><![CDATA[I&#8217;ve lately spent some time developing a webapp for mobile devices to interact with some of the data published by the Gijón City Council. More specifically, data about local public land transportation schedule and live arrivals. The way they are presenting that information for mobile devices at the moment is very very heavy and slow, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve lately spent some time developing a webapp for mobile devices to interact with some of the data published by <a href="http://www.gijon.es">the Gijón City Council</a>. More specifically, data about local public land transportation schedule and live arrivals. The way they are presenting that information for mobile devices at the moment is very very heavy and slow, so I thought it may be useful to do something simpler for personal usage.</p>
<p>Basically, it is a simple web service that intensively caches data (to avoid stressing the data origin with many requests) and a fancy AJAX-powered frontend with some CSS with mobile browsers in mind (works flawlessly on Android&#8217;s browser and Mobile Safari). Additionally, if you add it as a bookmark to your iPhone&#8217;s home screen it behaves like a native application (you know, splash screen, custom icon, taskbar and so on).</p>
<p>I&#8217;m now working on client-side caching using HTML5 caching for offline usage. This way the application will boot way faster. It&#8217;s almost done, but it still needs some debugging.</p>
<p>I don&#8217;t intend to make it public for now. However, if you find it useful feel free to drop me a line. Beta testers are always welcome (but unfortunately won&#8217;t be rewarded).</p>
<p>This is how it looks like at the moment. The source will be released soon.</p>
<p><strong>Update (23:26):</strong> Android screenshots provided by <a href="http://pozueco.net/">Javier Pozueco</a>. Thanks buddy!</p>
<p><a href="http://criptonita.com/~nacho/blog/wp-content/uploads/2011/03/c1.png"><img class="alignleft size-full wp-image-418" title="c1" src="http://criptonita.com/~nacho/blog/wp-content/uploads/2011/03/c1.png" alt="" width="320" height="480" /></a><a href="http://criptonita.com/~nacho/blog/wp-content/uploads/2011/03/c2.png"><img class="alignright size-full wp-image-419" title="c2" src="http://criptonita.com/~nacho/blog/wp-content/uploads/2011/03/c2.png" alt="" width="320" height="480" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a href="http://criptonita.com/~nacho/blog/wp-content/uploads/2011/03/c4.png"><img class="alignleft size-full wp-image-448" title="c4" src="http://criptonita.com/~nacho/blog/wp-content/uploads/2011/03/c4.png" alt="" width="320" height="480" /></a><a href="http://criptonita.com/~nacho/blog/wp-content/uploads/2011/03/c5.png"><img class="alignright size-full wp-image-449" title="c5" src="http://criptonita.com/~nacho/blog/wp-content/uploads/2011/03/c5.png" alt="" width="320" height="480" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a href="http://criptonita.com/~nacho/blog/wp-content/uploads/2011/03/c3.jpg"><img class="aligncenter size-full wp-image-420" title="c3" src="http://criptonita.com/~nacho/blog/wp-content/uploads/2011/03/c3.jpg" alt="" width="480" height="320" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2011/03/29/local-public-pocket/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Search term completion using a search tree</title>
		<link>http://criptonita.com/~nacho/blog/2011/03/24/term-completion-using-a-pythonic-search-tree/</link>
		<comments>http://criptonita.com/~nacho/blog/2011/03/24/term-completion-using-a-pythonic-search-tree/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 11:31:36 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=363</guid>
		<description><![CDATA[*lol* Nowadays it&#8217;s very usual to find websites offering hints while you&#8217;re typing on a search box. Google is a pretty good example of it. But, how could it be implemented? This feature could be implemented either in the client side or in the server side. If the word list is big (usually it is), [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter" title="Google search box completion" src="http://s4.hubimg.com/u/2092171_f520.jpg" alt="Google search box completion" width="520" height="378" /></p>
<p><strong>*lol*</strong></p>
<p>Nowadays it&#8217;s very usual to find websites offering hints while you&#8217;re typing on a search box. Google is a pretty good example of it. But, how could it be implemented?</p>
<p>This feature could be implemented either in the client side or in the server side. If the word list is big (usually it is), it&#8217;s recommended to keep the lookup logic in the server side to save some bytes while transferring the page to the client and also to save some computing power using server-side caches (cool when you plan to serve many requests).</p>
<p>Either way, there should be a data structure somewhere containing the word list and an algorithm to do the lookup. The simplest approach may be to use a list to store the words and issue something like this when you want to get a list of hints for a given prefix:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #008000;">filter</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> x: x.<span style="color: black;">startsWith</span><span style="color: black;">&#40;</span>prefix<span style="color: black;">&#41;</span>, word_list<span style="color: black;">&#41;</span></pre></div></div>

<p>That&#8217;s Python&#8217;s <em>filter</em>, but it works the same way the well-known Haskell&#8217;s first-order function <em>filter</em> does. It builds a new list with the elements of the original list (word_list) that match the predicate (the lambda function).</p>
<p>Although the results can (and should) be cached, the very first lookup (or when the cache expires) would be very inefficient because the entire list must be traversed and that operation will take linear time. Not bad, but when the size of the problem gets bigger (i.e. more and more words in the database) the lookup process may be too slow, especially whether you&#8217;re serving several users at the same time. If the list was sorted, the execution time could be improved a little bit by writing a more sophisticated algorithm, but let&#8217;s keep it that way for now.</p>
<p>Fortunately, there are better and faster ways to face the problem. If you don&#8217;t want to write code (usually the best choice) you may use some high-performance indexing engine such as Apache Lucene. But if you prefer the &#8216;do-it-yourself&#8217; way (for learning purposes), a search tree (more specifically, a <em>trie</em> or a prefix tree) is a good approach.</p>
<p>I&#8217;ve poorly benchmarked both alternatives (the list and the tree) and as expected the tree is pretty quicker generating hints. What I did was to feed both data structures with the content of an American English word list holding ~640k words (debian package <em>wamerican-insane</em>).</p>
<p>So, assuming four is a reasonable minimum prefix length, I measured the time it would take to get a list of words prefixed by <em>hous</em> (yes, just one, remember I said this was a poor benchmark? ;). Unsurprisingly, it took around 230 times longer for the list alternative to generate the hints (438.96 ms vs 1.92 ms). Wow.</p>
<p>My implementation of the tree is as follows. The API is quite straightforward, the &#8220;hot&#8221; methods are <em>put</em> and <em>get_hints</em>. I&#8217;ve stripped off the test suite for space reasons.</p>
<p>Usage example:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #66cc66;">&gt;&gt;&gt;</span> tree = HintSearchTree<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">put</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nacho&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">put</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nachos&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">put</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nachete&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">get_hints</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nach&quot;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'nachete'</span>, <span style="color: #483d8b;">'nacho'</span>, <span style="color: #483d8b;">'nachos'</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">get_hints</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nacho&quot;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'nacho'</span>, <span style="color: #483d8b;">'nachos'</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">delete</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nacho&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">get_hints</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nacho&quot;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'nachos'</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">count_words</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff4500;">2</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">get_hints</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;n&quot;</span><span style="color: black;">&#41;</span>
<span style="color: black;">&#91;</span><span style="color: #483d8b;">'nachete'</span>, <span style="color: #483d8b;">'nachos'</span><span style="color: black;">&#93;</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">is_indexed</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nachete&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">True</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">is_indexed</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;nach&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">False</span>
<span style="color: #66cc66;">&gt;&gt;&gt;</span> tree.<span style="color: black;">empty</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">False</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> HintSearchTreeNode<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
<span style="color: #ff7700;font-weight:bold;">class</span> HintSearchTreeNode<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, parent=<span style="color: #008000;">None</span>, terminal=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>._children = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
    <span style="color: #008000;">self</span>._terminal = terminal
    <span style="color: #008000;">self</span>._parent = parent
&nbsp;
  @<span style="color: #008000;">property</span>
  <span style="color: #ff7700;font-weight:bold;">def</span> children<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>._children
&nbsp;
  @<span style="color: #008000;">property</span>
  <span style="color: #ff7700;font-weight:bold;">def</span> terminal<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>._terminal
&nbsp;
  @terminal.<span style="color: black;">setter</span>
  <span style="color: #ff7700;font-weight:bold;">def</span> terminal<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, value<span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>._terminal = value
&nbsp;
  @<span style="color: #008000;">property</span>
  <span style="color: #ff7700;font-weight:bold;">def</span> parent<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>._parent
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> HintSearchTree<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
  <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #008000;">self</span>._root = HintSearchTreeNode<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> put<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, word<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Adds a word to the tree.&quot;&quot;&quot;</span>
    <span style="color: #808080; font-style: italic;"># TODO: Sanitize 'word'</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>word<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&amp;</span>gt<span style="color: #66cc66;">;</span> <span style="color: #ff4500;">0</span>:
      <span style="color: #008000;">self</span>._put<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._root, word<span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> count_words<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Retrieves the number of indexed words in the tree.&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._count_words<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._root<span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> is_indexed<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, word<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Returns True if 'word' is indexed.&quot;&quot;&quot;</span>
    node = <span style="color: #008000;">self</span>._find<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._root, word<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> node <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span> <span style="color: #ff7700;font-weight:bold;">and</span> node.<span style="color: black;">terminal</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">True</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> get_hints<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, prefix<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Returns a list of words prefixed by 'prefix'.&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._match_prefix<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._root, prefix<span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> delete<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, word<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Deletes 'word' (if exists) from the tree.&quot;&quot;&quot;</span>
    terminal = <span style="color: #008000;">self</span>._find<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._root, word<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> terminal <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span>:
      terminal.<span style="color: black;">terminal</span> = <span style="color: #008000;">False</span>
      <span style="color: #008000;">self</span>._prune<span style="color: black;">&#40;</span>terminal.<span style="color: black;">parent</span>, word<span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> empty<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Returns True if the tree contains no elements.&quot;&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._root.<span style="color: black;">children</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> _put<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, node, word, depth=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
    next_node = node.<span style="color: black;">children</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>word<span style="color: black;">&#91;</span>depth<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> next_node <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
      next_node = HintSearchTreeNode<span style="color: black;">&#40;</span>parent=node<span style="color: black;">&#41;</span>
      node.<span style="color: black;">children</span><span style="color: black;">&#91;</span>word<span style="color: black;">&#91;</span>depth<span style="color: black;">&#93;</span><span style="color: black;">&#93;</span> = next_node
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>word<span style="color: black;">&#41;</span>-<span style="color: #ff4500;">1</span> == depth:
      next_node.<span style="color: black;">terminal</span> = <span style="color: #008000;">True</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
      <span style="color: #008000;">self</span>._put<span style="color: black;">&#40;</span>next_node, word, depth+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> _count_words<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, node<span style="color: black;">&#41;</span>:
    words = <span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">if</span> node.<span style="color: black;">terminal</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">True</span> <span style="color: #ff7700;font-weight:bold;">else</span> <span style="color: #ff4500;">0</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> node.<span style="color: black;">children</span>:
      words += <span style="color: #008000;">self</span>._count_words<span style="color: black;">&#40;</span>node.<span style="color: black;">children</span><span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> words
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> _match_prefix<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, node, prefix<span style="color: black;">&#41;</span>:
    terminal = <span style="color: #008000;">self</span>._find<span style="color: black;">&#40;</span>node, prefix<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> terminal <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span>:
      <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._harvest_node<span style="color: black;">&#40;</span>terminal, prefix<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
      <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> _harvest_node<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, node, prefix, path=<span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>:
    hints = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> node.<span style="color: black;">terminal</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">True</span>:
      hints.<span style="color: black;">append</span><span style="color: black;">&#40;</span>prefix + path<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> node.<span style="color: black;">children</span>:
      hints.<span style="color: black;">extend</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._harvest_node<span style="color: black;">&#40;</span>node.<span style="color: black;">children</span><span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span>, prefix, path+k<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> hints
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> _find<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, node, word, depth=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> depth == <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>word<span style="color: black;">&#41;</span>:
      <span style="color: #ff7700;font-weight:bold;">return</span> node
    <span style="color: #ff7700;font-weight:bold;">else</span>:
      child = node.<span style="color: black;">children</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>word<span style="color: black;">&#91;</span>depth<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> child <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._find<span style="color: black;">&#40;</span>child, word, depth+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
      <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
&nbsp;
  <span style="color: #ff7700;font-weight:bold;">def</span> _prune<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, node, word<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>._count_words<span style="color: black;">&#40;</span>node.<span style="color: black;">children</span><span style="color: black;">&#91;</span>word<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span>:
      <span style="color: #ff7700;font-weight:bold;">del</span> node.<span style="color: black;">children</span><span style="color: black;">&#91;</span>word<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
      <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>node.<span style="color: black;">children</span><span style="color: black;">&#41;</span> == <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> node.<span style="color: black;">parent</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span> \
          <span style="color: #ff7700;font-weight:bold;">and</span> node.<span style="color: black;">terminal</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">True</span>:
        <span style="color: #008000;">self</span>._prune<span style="color: black;">&#40;</span>node.<span style="color: black;">parent</span>, word<span style="color: black;">&#91;</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The code is released in the public domain.</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2011/03/24/term-completion-using-a-pythonic-search-tree/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Some Perl to redirect HTTP requests</title>
		<link>http://criptonita.com/~nacho/blog/2011/02/24/some-perl-bits-to-redirect-http-requests/</link>
		<comments>http://criptonita.com/~nacho/blog/2011/02/24/some-perl-bits-to-redirect-http-requests/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 21:55:33 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Internet]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=325</guid>
		<description><![CDATA[After almost a year without publishing a single post, it seems this week I&#8217;m going to beat all my records. A week ago, I wanted to prank my brother for a while. Nothing sophisticated&#8230; just some Iptables rules, Tinyproxy and HTTP magic. To go ahead with my evil plans, I needed &#8220;something&#8221; able to redirect [...]]]></description>
			<content:encoded><![CDATA[<p>After almost a year without publishing a single post, it seems this week I&#8217;m going to beat all my records.</p>
<p>A week ago, I wanted to prank my brother for a while. Nothing sophisticated&#8230; just some Iptables rules, Tinyproxy and HTTP magic. To go ahead with my evil plans, I needed &#8220;something&#8221; able to redirect a HTTP request. Actually, there are several ways to do that: Apache redirects, Tornado, Netcat* and so on. These alternatives are fast, bulletproof and time-saving, but not fun.</p>
<p>As many of you probably know, I didn&#8217;t get a job yet. That necessary means that I&#8217;ve got plenty of free time to waste. So&#8230; what did I do? I wrote some Perl and today I&#8217;m publishing the source code just in case someone finds it useful somehow. Like the previous entry, it&#8217;s published in the public domain.</p>
<p>The script just collects connections, issues <em>301</em> back (Moved Permanently) and sets <em>Location</em> to the URI specified as a command line argument (option -u). It lacks some security checks (left as an exercise to the reader) but it does what it is supposed to do. You may likely spot some silly bugs as I haven&#8217;t spent much time reading it again. Reports are welcome!</p>
<p>For those wondering, the prank was a big success. I&#8217;m afraid I can&#8217;t spare any detail by now but it turns out my bro is still thinking that his computer has been cracked.</p>
<p>Example invocation:</p>
<blockquote><p>$ <strong>perl redir.pl -p 7070 -v -t 3 -u http://31337.pl</strong><br />
2011/02/24 21:41:54 Listening on port 7070<br />
2011/02/24 21:41:54 Redirecting HTTP requests to: &#8216;http://31337.pl&#8217;<br />
2011/02/24 21:41:54 3 thread(s) working under the hood<br />
&#8230;</p></blockquote>
<p>And finally the source code:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">use</span> warnings<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> threads<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> Thread<span style="color: #339933;">::</span><span style="color: #006600;">Queue</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> POSIX<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> IO<span style="color: #339933;">::</span><span style="color: #006600;">Socket</span><span style="color: #339933;">::</span><span style="color: #006600;">INET</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> HTTP<span style="color: #339933;">::</span><span style="color: #006600;">Request</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> HTTP<span style="color: #339933;">::</span><span style="color: #006600;">Status</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span>constants status_message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> Getopt<span style="color: #339933;">::</span><span style="color: #006600;">Long</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> DateTime<span style="color: #339933;">::</span><span style="color: #006600;">Format</span><span style="color: #339933;">::</span><span style="color: #006600;">HTTP</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Data<span style="color: #339933;">::</span><span style="color: #006600;">Validate</span><span style="color: #339933;">::</span><span style="color: #006600;">URI</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span>is_http_uri<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> Log<span style="color: #339933;">::</span><span style="color: #006600;">Log4perl</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">:</span>easy<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> constant MAX_THREADS <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> constant MAX_LEN_HEADERS_BUFFER <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">8</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">1024</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> constant DEFAULT_REDIRECT_URI <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">&quot;http://www.example.org&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> constant DEFAULT_PORT <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">use</span> constant DEFAULT_POOL_SIZE <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$redir_uri</span> <span style="color: #339933;">=</span> DEFAULT_REDIRECT_URI<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$server_port</span> <span style="color: #339933;">=</span> DEFAULT_PORT<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$thread_pool_size</span> <span style="color: #339933;">=</span> DEFAULT_POOL_SIZE<span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$verbose</span><span style="color: #339933;">;</span>
&nbsp;
GetOptions<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">'url=s'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\$redir_uri</span><span style="color: #339933;">,</span> 
           <span style="color: #ff0000;">'port=i'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\$server_port</span><span style="color: #339933;">,</span>
           <span style="color: #ff0000;">'threads=i'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\$thread_pool_size</span><span style="color: #339933;">,</span>
           <span style="color: #ff0000;">'verbose'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">\$verbose</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">exit</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Invalid redirect URI (e.g. http://www.example.org)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">unless</span> is_http_uri<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$redir_uri</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Invalid port (e.g. 8080)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #b1b100;">unless</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">$server_port</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$server_port</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">**</span><span style="color: #cc66cc;">16</span><span style="color: #339933;">;</span>
<span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Invalid pool size (should be in [1..&quot;</span><span style="color: #339933;">.</span>MAX_THREADS<span style="color: #339933;">.</span><span style="color: #ff0000;">&quot;])<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> 
            <span style="color: #b1b100;">unless</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">&lt;</span> <span style="color: #0000ff;">$thread_pool_size</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">$thread_pool_size</span> <span style="color: #339933;">&lt;=</span> MAX_THREADS<span style="color: #339933;">;</span>
&nbsp;
Log<span style="color: #339933;">::</span><span style="color: #006600;">Log4perl</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">easy_init</span><span style="color: #009900;">&#40;</span> level <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$verbose</span><span style="color: #339933;">?</span> <span style="color: #0000ff;">$DEBUG</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">$INFO</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$pending</span> <span style="color: #339933;">=</span> Thread<span style="color: #339933;">::</span><span style="color: #006600;">Queue</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$lsock</span> <span style="color: #339933;">=</span> IO<span style="color: #339933;">::</span><span style="color: #006600;">Socket</span><span style="color: #339933;">::</span><span style="color: #006600;">INET</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span> LocalPort <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">$server_port</span><span style="color: #339933;">,</span>
                                   Proto <span style="color: #339933;">=&gt;</span> <span style="color: #ff0000;">'tcp'</span><span style="color: #339933;">,</span>
                                   Listen <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
                                   Reuse <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Couldn't bind listening socket ($!)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span> 
&nbsp;
INFO<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Listening on port $server_port<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
INFO<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Redirecting HTTP requests to: '$redir_uri'<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">@workers</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">..</span><span style="color: #0000ff;">$thread_pool_size</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$thread</span> <span style="color: #339933;">=</span> threads<span style="color: #339933;">-&gt;</span><span style="color: #006600;">create</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;worker&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@workers</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$thread</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
DEBUG<span style="color: #009900;">&#40;</span><span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%d thread(s) working under the hood<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$#workers</span><span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Set a tidy shutdown just in case an external agent SIG{INT,TERM}s the process</span>
<span style="color: #0000ff;">$SIG</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'INT'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$SIG</span><span style="color: #009900;">&#123;</span><span style="color: #ff0000;">'TERM'</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># Dirty hack. threads-&gt;kill() does not wake up the thread :(</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">..</span><span style="color: #0000ff;">@workers</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #0000ff;">$pending</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">enqueue</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">@workers</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        DEBUG<span style="color: #009900;">&#40;</span><span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Worker %d terminated: %d clients served<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">tid</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">join</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$lsock</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
    <span style="color: #000066;">exit</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$csock</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$lsock</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">accept</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #b1b100;">next</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$pending</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">enqueue</span><span style="color: #009900;">&#40;</span>POSIX<span style="color: #339933;">::</span><span style="color: #006600;">dup</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">fileno</span> <span style="color: #0000ff;">$csock</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    DEBUG<span style="color: #009900;">&#40;</span><span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;New client enqueued: %s:%s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$csock</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">peerhost</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$csock</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">peerport</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$csock</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">sub</span> worker <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$clients_served</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$fd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$pending</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">dequeue</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;"># API promises thread safety :-)</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$fd</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066;">return</span> <span style="color: #0000ff;">$clients_served</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sock</span> <span style="color: #339933;">=</span> IO<span style="color: #339933;">::</span><span style="color: #006600;">Socket</span><span style="color: #339933;">::</span><span style="color: #006600;">INET</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new_from_fd</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$fd</span><span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;r+&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        DEBUG<span style="color: #009900;">&#40;</span><span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Dequeued client %s:%d by worker %d.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$sock</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">peerhost</span><span style="color: #339933;">,</span>
                            <span style="color: #0000ff;">$sock</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">peerport</span><span style="color: #339933;">,</span> threads<span style="color: #339933;">-&gt;</span><span style="color: #006600;">tid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$buf</span> <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;</span><span style="color: #0000ff;">$sock</span><span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;"># CAUTION: there isn't any self protection against very long lines</span>
            <span style="color: #b1b100;">last</span> <span style="color: #b1b100;">if</span> <span style="color: #009966; font-style: italic;">/^\r\n$/</span><span style="color: #339933;">;</span>
            <span style="color: #0000ff;">$buf</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">$_</span><span style="color: #339933;">;</span>
            <span style="color: #000066;">goto</span> BYE <span style="color: #b1b100;">if</span> <span style="color: #000066;">length</span> <span style="color: #0000ff;">$buf</span> <span style="color: #339933;">&gt;</span> MAX_LEN_HEADERS_BUFFER<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: #b1b100;">my</span> <span style="color: #0000ff;">$request</span> <span style="color: #339933;">=</span> HTTP<span style="color: #339933;">::</span><span style="color: #006600;">Request</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$buf</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            INFO<span style="color: #009900;">&#40;</span><span style="color: #000066;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;[%s] %s {%s}<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">method</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">uri</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">$sock</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">peerhost</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066;">printf</span> <span style="color: #0000ff;">$sock</span> <span style="color: #ff0000;">&quot;HTTP/1.1 %d %s<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> 
            HTTP_MOVED_PERMANENTLY<span style="color: #339933;">,</span> status_message<span style="color: #009900;">&#40;</span>HTTP_MOVED_PERMANENTLY<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">printf</span> <span style="color: #0000ff;">$sock</span> <span style="color: #ff0000;">&quot;Date: %s<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> DateTime<span style="color: #339933;">::</span><span style="color: #006600;">Format</span><span style="color: #339933;">::</span><span style="color: #006600;">HTTP</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">format_datetime</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">print</span> <span style="color: #0000ff;">$sock</span> <span style="color: #ff0000;">&quot;Location: $redir_uri<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">print</span> <span style="color: #0000ff;">$sock</span> <span style="color: #ff0000;">&quot;Server: Simple HTTP Redirection/0.1 ($^O)<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">print</span> <span style="color: #0000ff;">$sock</span> <span style="color: #ff0000;">&quot;Connection: close<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000066;">print</span> <span style="color: #0000ff;">$sock</span> <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><span style="color: #339933;">;</span>
&nbsp;
BYE<span style="color: #339933;">:</span>  
        <span style="color: #0000ff;">$clients_served</span><span style="color: #339933;">++;</span>
        <span style="color: #000066;">close</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$sock</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>(*) just an approach, may drop connections:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; 
 <span style="color: #000000; font-weight: bold;">do</span> <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;HTTP/1.1 301 Moved Permanently<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>Location: http://31337.pl<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> nc <span style="color: #660033;">-l</span> <span style="color: #000000;">7070</span>; 
<span style="color: #000000; font-weight: bold;">done</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2011/02/24/some-perl-bits-to-redirect-http-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reverse Polish Notation Evaluation in Python</title>
		<link>http://criptonita.com/~nacho/blog/2011/02/23/reverse-polish-notation-evaluation-in-python/</link>
		<comments>http://criptonita.com/~nacho/blog/2011/02/23/reverse-polish-notation-evaluation-in-python/#comments</comments>
		<pubDate>Wed, 23 Feb 2011 00:03:21 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=305</guid>
		<description><![CDATA[This introduction is followed by some Python code (function evaluate_postfix_expr) to evaluate expressions (only integers, but may be extended with ease) in Reverse Polish Notation (RPN). Some simple tests are also included in the bundle. I agree it&#8217;s a little useless, but I thought it might be useful for someone (CS students maybe?). If you [...]]]></description>
			<content:encoded><![CDATA[<p>This introduction is followed by some Python code (function evaluate_postfix_expr) to evaluate expressions (only integers, but may be extended with ease) in Reverse Polish Notation (RPN). Some simple tests are also included in the bundle.</p>
<p>I agree it&#8217;s a little useless, but I thought it might be useful for someone (CS students maybe?). If you want to examine the stack in each iteration you only have to turn debugging on. That can be accomplished by changing <em>logging.INFO</em> to <em>logging.DEBUG</em> (line 7).</p>
<p>Copy, distribute or do whatever you want with it. It&#8217;s released in the public domain.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
</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;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
&nbsp;
<span style="color: #dc143c;">logging</span>.<span style="color: black;">basicConfig</span><span style="color: black;">&#40;</span>level=<span style="color: #dc143c;">logging</span>.<span style="color: black;">INFO</span><span style="color: black;">&#41;</span>
&nbsp;
operators_table = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'+'</span>: <span style="color: #008000;">int</span>.<span style="color: #0000cd;">__add__</span>, 
             <span style="color: #483d8b;">'-'</span>: <span style="color: #008000;">int</span>.<span style="color: #0000cd;">__sub__</span>,
             <span style="color: #483d8b;">'*'</span>: <span style="color: #008000;">int</span>.<span style="color: #0000cd;">__mul__</span>,
             <span style="color: #483d8b;">'/'</span>: <span style="color: #008000;">int</span>.<span style="color: #0000cd;">__div__</span>,
             <span style="color: #483d8b;">'^'</span>: <span style="color: #008000;">int</span>.<span style="color: #0000cd;">__pow__</span><span style="color: black;">&#125;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ExpressionError<span style="color: black;">&#40;</span><span style="color: #008000;">Exception</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, message<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>._message = <span style="color: #483d8b;">&quot;Expression error: %s&quot;</span> <span style="color: #66cc66;">%</span> message
    <span style="color: #ff7700;font-weight:bold;">def</span> _get_message<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>._message
    message = <span style="color: #008000;">property</span><span style="color: black;">&#40;</span>_get_message<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> TestEvaluation<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> test_correct<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">666</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;666&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span>+<span style="color: #ff4500;">3</span>-<span style="color: #ff4500;">6</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;2 3 + 6 -&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">3</span>+<span style="color: #ff4500;">4</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;2 3 * 4 +&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">*</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span>+<span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;2 3 4 + *&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">4</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;3   3  *     3  *      3 *&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">7</span>/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">4</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;7 2 / 4 ^&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">**</span><span style="color: #ff4500;">4</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;2 3 ^ 4 ^&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span>+<span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>+<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>-<span style="color: #ff4500;">3</span>, evaluate_postfix_expr<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;5 1 2 + 4 * 3 - +&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_malformed<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;+&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;2 +&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;+ 2 2&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;2 2&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;2 2 + -&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertRaises</span><span style="color: black;">&#40;</span>ExpressionError, evaluate_postfix_expr, <span style="color: #483d8b;">&quot;a 2 -&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> evaluate_postfix_expr<span style="color: black;">&#40;</span>expr<span style="color: black;">&#41;</span>:
    atoms = <span style="color: #dc143c;">re</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\s</span>+&quot;</span>, expr<span style="color: black;">&#41;</span>
    stack = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span> 
    <span style="color: #ff7700;font-weight:bold;">for</span> atom <span style="color: #ff7700;font-weight:bold;">in</span> atoms:
        <span style="color: #ff7700;font-weight:bold;">if</span> atom <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: black;">&#91;</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>:
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                op2 = stack.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                op1 = stack.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IndexError</span>:
                <span style="color: #ff7700;font-weight:bold;">raise</span> ExpressionError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Too few operands (unbalanced)&quot;</span><span style="color: black;">&#41;</span>
            <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Calculating %d %s %d&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>op1, atom, op2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            atom = operators_table<span style="color: black;">&#91;</span>atom<span style="color: black;">&#93;</span><span style="color: black;">&#40;</span>op1, op2<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                atom = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>atom<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ValueError</span>:
                <span style="color: #ff7700;font-weight:bold;">raise</span> ExpressionError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Unable to parse '%s' as integer&quot;</span> <span style="color: #66cc66;">%</span> atom<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            stack.<span style="color: black;">append</span><span style="color: black;">&#40;</span>atom<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">MemoryError</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span> ExpressionError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Too long expression&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #dc143c;">logging</span>.<span style="color: black;">debug</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Pushed element %d. Stack status: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>atom, stack<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>stack<span style="color: black;">&#41;</span> == <span style="color: #ff4500;">1</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> stack.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">raise</span> ExpressionError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Too many operands (unbalanced)&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #dc143c;">unittest</span>.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2011/02/23/reverse-polish-notation-evaluation-in-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>E-commerce a la española: diesl.es</title>
		<link>http://criptonita.com/~nacho/blog/2010/04/06/opinion-diesl-com/</link>
		<comments>http://criptonita.com/~nacho/blog/2010/04/06/opinion-diesl-com/#comments</comments>
		<pubDate>Tue, 06 Apr 2010 09:01:16 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Life]]></category>
		<category><![CDATA[Shopping]]></category>
		<category><![CDATA[TV]]></category>
		<category><![CDATA[WTF]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=265</guid>
		<description><![CDATA[Si Google te ha traído aquí, te resumo lo que pone a continuación: NO COMPRES EN DIESL.ES Casi un año sin escribir en mi blog, principalmente por culpa del microblogging. Vamos allá. Es rara la vez que compro cosas que no son de primera necesidad en tiendas tradicionales. Gracias a las oportunidades que Internet brinda [...]]]></description>
			<content:encoded><![CDATA[<p><em>Si Google te ha traído aquí, te resumo lo que pone a continuación:<strong> NO COMPRES EN <a href="http://www.diesl.es">DIESL.ES</a></strong></em></p>
<p>Casi un año sin escribir en mi blog, principalmente por culpa del microblogging. Vamos allá.</p>
<p>Es rara la vez que compro cosas que no son de primera necesidad en tiendas tradicionales. Gracias a las oportunidades que Internet brinda al consumidor, es posible hoy en día hacerse con multitud de artículos a buen precio en tiendas en línea.</p>
<p>Para ser sinceros, no suelo comprar cosas en España. Acostumbro siempre a mercar en el Reino Unido, en Holanda (bromas aparte) o en los Estados Unidos. La calidad de los productos y el cumplimiento de los plazos de entrega es sencillamente impoluto. Por otro lado, la atención al cliente veloz e inteligente en la mayoría de los casos y la constante monitorización del estado del pedido hace que comprar en estas tiendas sea, en la mayoría de las ocasiones, una experiencia agradable. Pero, ¿qué pasa en España? Pues lo de siempre, aquí nos tomamos las cosas a pitorreo; el cliente: lo último de la lista. Os cuento.</p>
<p>Decidí comprar el martes pasado (30/03) un sintonizador TDT de alta definición. Lo quería para antes de la semana santa, por lo que necesitaba que el envío se realizara en, como mucho, 24 horas para que no se atragantara con las fiestas. Rebuscando por la Web, me encontré con la tienda online <a href="http://www.diesl.es">Diesl</a>. Me venía como anillo al dedo, ya que en sus <a href="http://www.diesl.es/epages/eb0383.sf/es_ES/?ObjectPath=/Shops/eb0383/Categories/TermsAndConditions">condiciones generales</a> ponía (y pone a día de hoy) lo siguiente:</p>
<blockquote>
<p style="line-height: 16px; resize: none; margin-top: 0px; margin-right: 0px; margin-bottom: 6px; margin-left: 0px;">Para todos los pedidos a Península y Baleares los plazos de entrega serán de 24 horas, siempre que el pedido se realice antes de las 14:00 (hora española) y esté disponible en stock.</p>
</blockquote>
<p style="line-height: 16px; resize: none; margin-top: 0px; margin-right: 0px; margin-bottom: 6px; margin-left: 0px;">Perfecto, sólo necesitaba que hubiera stock del producto que quería.</p>
<p style="line-height: 16px; resize: none; margin-top: 0px; margin-right: 0px; margin-bottom: 6px; margin-left: 0px;">
<p style="line-height: 16px; resize: none; margin-top: 0px; margin-right: 0px; margin-bottom: 6px; margin-left: 0px;">
<p style="line-height: 16px; resize: none; margin-top: 0px; margin-right: 0px; margin-bottom: 6px; margin-left: 0px;"><img class="aligncenter size-medium wp-image-266" title="Disponible" src="http://criptonita.com/~nacho/blog/wp-content/uploads/2010/04/Screen-shot-2010-04-06-at-10.45.31-300x224.png" alt="Disponible" width="329" height="245" /></p>
<p>Por si no se lee correctamente, decía claramente que el producto estaba disponible, y que el plazo de entrega era de <strong>1 día</strong>.</p>
<p>Formalizo y pago el pedido el martes 30/03 a las 11:59. Qué bien, iba a tener al día siguiente antes de las 14:00 en mi casa el aparato, mis futboleros familiares iban a poder ver el partido de Champions League en TPA HD. El pedido pasó automáticamente al estado <strong>pagado</strong>. Además se dan prisa en cobrar, en mi cuenta corriente ya se reflejaba el pago.</p>
<p>Miércoles 31/03 12:00 (casi  24 horas después). El pedido sigue en el mismo estado y, por supuesto, a la dirección de entrega no ha llegado nada. Decido mandarles un correo a la dirección de contacto que aparece en la página, sección &#8220;<a href="http://www.diesl.es/epages/eb0383.sf/es_ES/?ObjectPath=/Shops/eb0383/Categories/Imprint">Quienes somos</a>&#8220;.</p>
<p><img class="aligncenter size-full wp-image-268" title="Direccion diesl" src="http://criptonita.com/~nacho/blog/wp-content/uploads/2010/04/Screen-shot-2010-04-06-at-10.50.51.png" alt="Direccion diesl" width="289" height="182" />Vaya mala suerte:</p>
<blockquote><p>Delivery to the following recipient failed permanently:</p>
<p><span style="background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #fbee99; background-position: initial initial;">diesl</span> en <span style="background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #fbee99; background-position: initial initial;">diesl punto </span>com</p>
<p>Technical details of permanent failure:<br />
Google tried to deliver your message, but it was rejected by the recipient domain. We recommend contacting the other email provider for further information about the cause of this error. The error that the other server returned was: 550 550 sorry, <strong>no mailbox here by that name</strong>. (#5.7.17)</p></blockquote>
<p>Increíble, la única dirección de correo que encontré y no funciona.</p>
<p>Mirando en el correo electrónico de confirmación del pedido (más bien del pago, viendo lo visto), encuentro otra dirección de correo (tienda en diesl punto com) a la cual remito de nuevo mi correo, esta vez no hay rechazo.</p>
<blockquote><p>Hola,</p>
<p>Ayer ordené y pagué inmediatamente el pedido número 1-888-LETS-GET-RETARDED (ficción publicitaria) en su tienda.</p>
<p>El producto adquirido rezaba como disponible y entregable en 1 día. Según sus &#8220;condiciones generales&#8221; si un producto está disponible y el pedido se hace antes de las 14h (como así fue hecho) el plazo de entrega es de 24 horas. En la página correspondiente a la información del pedido no hay constancia de que haya sido ni siquiera enviado. Por supuesto que el producto aún no lo he recibido en la dirección de entrega facilitada.</p>
<p>La única razón por la que decidí comprar en su tienda fue por el plazo de entrega, para evitar tener que esperar hasta la semana que viene (por las fiestas). Ruego si no son capaces de cumplir los  plazos de entrega cancelen el pedido</p>
<p>Esperando respuesta,</p></blockquote>
<p>Y ya podía esperar, porque el correo lo mandé el 31/03 a las 14:34 y aún sigo esperando una respuesta a día de hoy.</p>
<p>El jueves 01/04 estuve fuera de casa y sin acceso a Internet. Cuando volví a mi domicilio el viernes por la tarde, el pedido había cambiado de estado: <strong>visto</strong>. Jajaja, ¿visto? ¡vaya bueno! ¿quién lo vió? por cierto, ¿mi correo lo vió alguien?</p>
<p>Pasan los días, hoy es 06/04 (es decir, ha pasado una semana) y sigue todo igual, en el mismo estado, el correo sin responder y yo sin mi compra (y sin mi dinero). A ver como acaba esta hazaña de comprar en España por Internet.</p>
<p>España, toros y olé. A tomar por el culo.</p>
<p><strong>Actualización (06/04 @ 23:15):</strong> Les he mandado un nuevo correo, a distintas direcciones. Permutando los nombres <em>diesl</em> y <em>tienda</em> con los dominios <em>diesl.es</em> y <em>diesl.com</em> incluso utilizando la dirección de gmail asociada al registro del dominio, como sugería Pablete en los <a href="http://criptonita.com/~nacho/blog/2010/04/06/opinion-diesl-com/#comments">comentarios</a><em>.<br />
</em></p>
<p><strong>Actualización (07/04 @ 10:00):</strong> Sin respuesta, de momento.</p>
<p><strong>Actualización (07/04 @ 12:15):</strong> Me llamaron por teléfono (¿no se darán cuenta de que si les mando un correo es porque quiero que me respondan por la misma vía?). Se disculpan achacando la culpa del retraso a un &#8220;fallo informático&#8221;. Joder, sin ánimo de desconfiar (o sí, ¡qué cojones! en el país de la picaresca y la mala fe como para no), pero esa excusa está alcanzando altos niveles de fama, ya es del calibre del &#8220;<em>es que no sabía&#8221;</em> o el &#8220;<em>es que nadie me dijo que me habían dicho que mi jefe dijo que&#8230;&#8221;.</em> El chaval muy majo, eso sí. Me aseguraron que mañana estará el producto en mi casa, con ocho días de retraso. Ver veremos. Sigo molesto de todos modos.</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2010/04/06/opinion-diesl-com/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>X Aniversario de AsturLiNUX</title>
		<link>http://criptonita.com/~nacho/blog/2009/04/13/x-aniversario-de-asturlinux/</link>
		<comments>http://criptonita.com/~nacho/blog/2009/04/13/x-aniversario-de-asturlinux/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 11:56:43 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[AsturLiNUX]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Spanish]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=248</guid>
		<description><![CDATA[Esta semana comienzan las actividades conmemorativas del décimo aniversario de la AsturLiNUX. Durante toda la semana hay programadas diversas actividades (charlas, talleres y conferencias) y como colofón, un gran acto central, el próximo sábado 18 de abril donde intervendrán Iván Frade (”Maemo; Software Libre Movil”) de Nokia y Rodrigo Salvador (”El Software Libre para la [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.asturlinux.org/aniversario/"><img class="aligncenter" title="X Aniversario de AsturLiNUX (banner)" src="http://www.asturlinux.org/aniversario/images/banner1xanivasttexto.png" alt="" width="493" height="141" /></a></p>
<p>Esta semana comienzan las actividades conmemorativas del décimo aniversario de la <a href="http://www.asturlinux.org">AsturLiNUX</a>.</p>
<p>Durante toda la semana hay programadas diversas actividades (charlas, talleres y conferencias) y como colofón, un gran acto central, el próximo <strong>sábado 18 de abril</strong> donde intervendrán Iván Frade (”Maemo; Software Libre Movil”) de Nokia y Rodrigo Salvador (”El Software Libre para la Gestión de la Red Educativa de la Junta de Andalucía”) de la Junta de Andalucía.</p>
<p>Además de estas ponencias, tendrá lugar una mesa redonda (“El Software Libre dentro y fuera de Asturias”) con una <a href="http://www.asturlinux.org/aniversario/?p=9">lista de participantes</a> muy interesante.</p>
<p>Más información en la <a href="http://www.asturlinux.org/aniversario/">página principal</a> y en el <a href="http://www.asturlinux.org/aniversario/?page_id=4">programa</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2009/04/13/x-aniversario-de-asturlinux/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Music spotted!</title>
		<link>http://criptonita.com/~nacho/blog/2009/01/29/music-spotted/</link>
		<comments>http://criptonita.com/~nacho/blog/2009/01/29/music-spotted/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 23:19:29 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Music]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=246</guid>
		<description><![CDATA[Doh! A new piece of software I couldn&#8217;t live without: Spotify.]]></description>
			<content:encoded><![CDATA[<p>Doh! A new piece of software I couldn&#8217;t live without: <a href="http://www.spotify.com">Spotify</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2009/01/29/music-spotted/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>DISTRO FAIL</title>
		<link>http://criptonita.com/~nacho/blog/2009/01/22/distro-fail/</link>
		<comments>http://criptonita.com/~nacho/blog/2009/01/22/distro-fail/#comments</comments>
		<pubDate>Wed, 21 Jan 2009 23:08:36 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[WTF]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=238</guid>
		<description><![CDATA[ ]]></description>
			<content:encoded><![CDATA[<p> </p>
<div id="attachment_240" class="wp-caption aligncenter" style="width: 551px"><a href="http://www.asturlinux.es"><img class="size-full wp-image-240" title="picture-21" src="http://criptonita.com/~nacho/blog/wp-content/uploads/2009/01/picture-21.png" alt="DISTRO FAIL" width="541" height="147" /></a><p class="wp-caption-text">DISTRO FAIL</p></div>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2009/01/22/distro-fail/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
		<item>
		<title>Free 1Password Licenses!</title>
		<link>http://criptonita.com/~nacho/blog/2008/12/28/free-1password-licenses/</link>
		<comments>http://criptonita.com/~nacho/blog/2008/12/28/free-1password-licenses/#comments</comments>
		<pubDate>Sun, 28 Dec 2008 12:42:50 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=232</guid>
		<description><![CDATA[1Password is a very powerful application for OSX to keep safe your passwords and identities. It works as a standalone application and as a series of plugins for several Internet browsers. It is capable of, among other things, autofill registration forms and save you a lot of time filling in the same personal information again [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://agilewebsolutions.com/products/1Password">1Password</a> is a very powerful application for OSX to keep safe your passwords and identities. It works as a standalone application and as a series of plugins for several Internet browsers. It is capable of, among other things, autofill registration forms and save you a lot of time filling in the same personal information again and again. To autofill a login form, just click the 1P browser menu bar icon, choose a previously recorded identity and voila, 1Password logins in <em>automagically </em>with no further interaction.</p>
<p>All the harvested information is stored in a different keychain unlocked by a master key placed in the login keychain (See &#8220;Keychain access.app&#8221; for more information).</p>
<p><a href="http://www.macheist.com/">Macheist</a>, in collaboration with Agile Web Solutions, is offering free licenses as a Christmas present <a href="http://givingtree.macheist.com/">here</a>. You only have to create and account and click in the links below the tree to get your free license.</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2008/12/28/free-1password-licenses/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Security? Privacy? Brain?</title>
		<link>http://criptonita.com/~nacho/blog/2008/12/23/security/</link>
		<comments>http://criptonita.com/~nacho/blog/2008/12/23/security/#comments</comments>
		<pubDate>Tue, 23 Dec 2008 17:16:51 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[Life]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=220</guid>
		<description><![CDATA[Yay! I&#8217;ve something cool to talk about. It seems my brother is not very good with numbers and managed to block his SIM card by mistyping the PIN code three times in a row. The card is quite old, he inherited it from me time ago and God knows where the proper documentation which came along [...]]]></description>
			<content:encoded><![CDATA[<p>Yay! I&#8217;ve something cool to talk about.</p>
<p>It seems my brother is not very good with numbers and managed to block his SIM card by mistyping the PIN code three times in a row. The card is quite old, he inherited it from me time ago and God knows where the proper documentation which came along with it was. In short, we were screwed without the PUK code.</p>
<p>When the card was bought, there wasn&#8217;t any active law demanding mobile operators to establish any link between the card and its owner. Given that, I told him it will be hard to get the PUK to unlock the card because most likely the customer service would suspect the card was stolen and its new evil owner was trying to use it. Anyway, why don&#8217;t give it a try?</p>
<p><em>Dialing 1485&#8230;</em></p>
<p>Me: Hello. My cellphone is locked, it is asking for a code in order to get itself unlocked.</p>
<p>Op: What code? PIN code?</p>
<p>Me: Well, no. It requests the PUK code.</p>
<p>Op: I see. Is it a prepaid card?</p>
<p>Me: It is.</p>
<p>Op: Ok, turn off the device, remove the battery, extract the SIM card and tell me the numbers printed on it.</p>
<p>Me: Well&#8230; the numbers are: 11111111111111.</p>
<p>Op: Ok, and your phone number is&#8230;?</p>
<p>Me: 622222222.</p>
<p>Op: Ok. Your PUK is: 333333333333333.</p>
<p>Me: Thanks.</p>
<p>Op: Thanks for calling. Have a nice day.</p>
<p>Thus anyone who knows your telephone number and wants to read you messages, copy your contact list or call for free only has to steal your phone. You may now start worrying even if the phone was off, the security codes don&#8217;t protect your privacy at all.</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2008/12/23/security/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cinema at home</title>
		<link>http://criptonita.com/~nacho/blog/2008/12/17/cinema-at-home/</link>
		<comments>http://criptonita.com/~nacho/blog/2008/12/17/cinema-at-home/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 12:23:01 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[Shopping]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=209</guid>
		<description><![CDATA[I&#8217;ve finally committed the crime :) I&#8217;ve been looking for a new monitor for a lot of time. Given I&#8217;ve decided to use my new Macbook as my unique desktop system it turned necessary to buy a new display to exploit in a better way the capacity of this marvelous operating system, MacOSX.  Starting with [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve finally committed the crime :)</p>
<p>I&#8217;ve been looking for a new monitor for a lot of time. Given I&#8217;ve decided to use my new Macbook as my unique desktop system it turned necessary to buy a new display to exploit in a better way the capacity of this marvelous operating system, MacOSX. </p>
<p>Starting with the idea of buying a simple LCD 20&#8243; monitor and after asking a bunch of anonymous multimedia and imaging amateurs for advice I ended up opening my scope and buying a gorgeous 23&#8243; FullHD Dell display.</p>
<p>The monitor arrived today. Connecting it to my macbook through VGA was easy as pie, it looks incredibly stunning at 1920&#215;1080. One of the main advantages of this display is its aspect ratio. Having an AR of 16:9 allows you to get rid of the annoying black parts on screen while watching 16:9 TV shows downloaded from the Internet. This way, you get an actual full screen :)</p>
<p>Take my advice and if you have in mind buying a new display evade old fashioned resolutions such as 1680&#215;1050 and enjoy the FullHD experience.</p>
<p>I&#8217;ve also bought a wired Apple keyboard&amp;mouse which have improved my desktop experience even more, having now a comfortable desktop setup to enjoy even more one of the things I do more often, spending uncountable hours in front of my beloved computer.</p>
<p>That&#8217;s all folks!</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2008/12/17/cinema-at-home/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Does Alabama want the bomb?</title>
		<link>http://criptonita.com/~nacho/blog/2008/10/30/does-alabama-want-the-bomb/</link>
		<comments>http://criptonita.com/~nacho/blog/2008/10/30/does-alabama-want-the-bomb/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 09:49:34 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=199</guid>
		<description><![CDATA[So Israel&#8217;s getting tense. Wants one in self defense. &#8220;The Lord&#8217;s our shepherd,&#8221; says the psalm, But just in case, we better get a bomb. Who&#8217;s next? Mwhahaha!]]></description>
			<content:encoded><![CDATA[<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/oRLON3ddZIw&#038;hl=es&#038;fs=1&#038;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/oRLON3ddZIw&#038;hl=es&#038;fs=1&#038;rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object></p>
<blockquote><p>So Israel&#8217;s getting tense.<br />
Wants one in self defense.<br />
&#8220;The Lord&#8217;s our shepherd,&#8221; says the psalm,<br />
But just in case, we better get a bomb.<br />
Who&#8217;s next?</p></blockquote>
<p>Mwhahaha!</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2008/10/30/does-alabama-want-the-bomb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Science Fail</title>
		<link>http://criptonita.com/~nacho/blog/2008/10/05/science-fail/</link>
		<comments>http://criptonita.com/~nacho/blog/2008/10/05/science-fail/#comments</comments>
		<pubDate>Sun, 05 Oct 2008 11:06:25 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Fun]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=197</guid>
		<description><![CDATA[Hahahahaha. Here.]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter" title="Science Fail" src="http://failblog.files.wordpress.com/2008/09/fail-owned-yahoo-science-mirror-fail1.jpg" alt="" width="500" height="180" /></p>
<p>Hahahahaha. <a href="http://failblog.org/2008/10/05/science-fail/">Here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2008/10/05/science-fail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ha-ha</title>
		<link>http://criptonita.com/~nacho/blog/2008/09/23/ha-ha/</link>
		<comments>http://criptonita.com/~nacho/blog/2008/09/23/ha-ha/#comments</comments>
		<pubDate>Tue, 23 Sep 2008 12:12:35 +0000</pubDate>
		<dc:creator>Nacho Barrientos</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[TV]]></category>

		<guid isPermaLink="false">http://criptonita.com/~nacho/blog/?p=191</guid>
		<description><![CDATA[I certainly love Boston Legal.]]></description>
			<content:encoded><![CDATA[<p><object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/S3gEf10ioYs&#038;hl=es&#038;fs=1"></param><param name="allowFullScreen" value="true"></param><embed src="http://www.youtube.com/v/S3gEf10ioYs&#038;hl=es&#038;fs=1" type="application/x-shockwave-flash" allowfullscreen="true" width="425" height="344"></embed></object></p>
<p>I certainly love Boston Legal.</p>
]]></content:encoded>
			<wfw:commentRss>http://criptonita.com/~nacho/blog/2008/09/23/ha-ha/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

