<?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>boriel.com &#187; Programming</title>
	<atom:link href="http://www.boriel.com/category/tag/informatica/programacion/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.boriel.com</link>
	<description>Hacks, science and personal rants</description>
	<lastBuildDate>Wed, 28 Dec 2011 22:50:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Breakpoint Wont be Hit in Visual Studio 2010</title>
		<link>http://www.boriel.com/2011/08/18/breakpoint-wont-be-hit-in-visual-studio-2010/?lang=en</link>
		<comments>http://www.boriel.com/2011/08/18/breakpoint-wont-be-hit-in-visual-studio-2010/?lang=en#comments</comments>
		<pubDate>Thu, 18 Aug 2011 23:25:36 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & tricks]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=1861</guid>
		<description><![CDATA[I&#8217;ve been working on a 3D VideoGame prototype these last months, using Visual C++ (Visual Studio 2010). With such work, some vacancies and some job affairs I havn&#8217;t had enough time for publishing something interesting in the blog&#8230; Visual Studio 2010 worked fine at the beginning, but one day, suddenly, breakpoints stopped working. A yellow [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a 3D VideoGame prototype these last months, using Visual C++ (Visual Studio 2010). With such work, some vacancies and some job affairs I havn&#8217;t had enough time for publishing something interesting in the blog&#8230; <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Visual Studio 2010 worked fine at the beginning, but one day, suddenly, breakpoints stopped working. A yellow exclamation mark symbol was shown over them and placing the mouse cursor over such symbols showed the message &#8220;Breakpoint won&#8217;t be hit&#8221;.</p>
<p>Searching on the internet for this problem I found out it was a common one in almost every version of Visual Studio, specially in 2008 and 2010. Most common causes for this error are:</p>
<ul>
<li>The project was compiled in <i>Release</i> mode, not in <i>Debug</i> one</li>
<li>Debug symbols are not found (or wrong path)</li>
<li>Incremental Debug linking option is not enabled</li>
<li>Not having installed the latest service packs and patches (some of them address this problem)</li>
<li>Corrupted installation: Reinstalling the application, restoring default configuration or even deleting Registry Keys and Local App configuration stored in the Windows User profile folders might fix this case</li>
</ul>
<p>The fact was none of the above worked for me, but found out that if I started Visual Studio with a different user account everything worked like a charm. <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_question.gif' alt=':?:' class='wp-smiley' />  After opening two projects (each generated with a different user) and compared differences on the .vproj XML files I found these extra lines added to the &#8220;faulty&#8221; one:</p>
<pre class="brush:xml">
<importgroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
  </importgroup>
</pre>
<p>Removing these lines from the .vproj file and recompiling made the breakpoints to work again! The case was these lines referenced User configurations that didn&#8217;t appear in my VS configuration panels or couldn&#8217;t change (some of them were <em>grayed</em>). These XML lines contain a conditional inclusion and the path is $(UserRootDir)\MicrosoftCpp.$(Platform). It happened that path was C:\Users\Boriel\AppData\Local\Microsoft\MSBuild\v4.0, which was not the Visual Studio configuration Folder. Deleting it fixed the issue (it&#8217;s regenerated the next time Visual Studio IDE is started).</p>
<p>To avoid even more troubles I not only removed that folder, but also the local configuration stored in my profile (C:\Users\Boriel\AppData\Local\Microsoft\VisualStudio\10.0) and related registry keys (HKEY_CURRENT_USER\Software\Microsoft\VSCommon\10.0). Afterwards I restarted the IDE and a splashscreen warning told me the application was being prepared to be used for the first time. And the problem was gone. <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<div id="tweetbutton1861" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2011%2F08%2F18%2Fbreakpoint-wont-be-hit-in-visual-studio-2010%2F%3Flang%3Den&amp;via=boriel&amp;text=%3Ci%3EBreakpoint%20Wont%20be%20Hit%3C%2Fi%3E%20in%20Visual%20Studio%202010%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2011%2F08%2F18%2Fbreakpoint-wont-be-hit-in-visual-studio-2010%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2011/08/18/breakpoint-wont-be-hit-in-visual-studio-2010/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ants</title>
		<link>http://www.boriel.com/2011/03/26/hormigas/?lang=en</link>
		<comments>http://www.boriel.com/2011/03/26/hormigas/?lang=en#comments</comments>
		<pubDate>Sat, 26 Mar 2011 15:42:55 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Divertimática]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=1774</guid>
		<description><![CDATA[This week&#8217;s math challenge of El País (Spanish video) is again about a graph. In this case, the graph is a cube (8 vertex, 12 edges) numbered as shown in the video. An ant starts walking from vertex #1 and changes it direction at random on each vertex (might even turn back from the same [...]]]></description>
			<content:encoded><![CDATA[<p>This week&#8217;s <a href="http://www.elpais.com/videos/sociedad/hormiga/amenazada/elpepusoc/20110325elpepusoc_1/Ves/">math challenge of El País</a> (Spanish video) is again about a graph. In this case, the graph is a cube (8 vertex, 12 edges) numbered as shown in the video.</p>
<p>An ant starts walking from vertex #1 and changes it direction at random on each vertex (might even turn back from the same edge it came from). Vertex #7 and #8 are poisoned. If the ant happens to walk into one of them it will die. The challenge consist in find out the probabilities of the ant dying or not and in which vertex (#7 or #8) when it does.<br />
<span id="more-1774"></span><br />
This program uses the previous one (which already defines a Graph class) to make a simple statistical simulation of the problem and give you an answer of what to expect. Obviously, the result by itself is not the entire solution: you also have to give a theoretical demonstration of such result <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<pre class="brush:py">#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:ts=4:et:sw=4:

from random import choice
from grafo import Graph, Vertex

class Point(Vertex):
    '''A vertex which can have a poison trap
    '''
    def __init__(self, *args):
        Vertex.__init__(self, *args)
        self.poisoned = False

    def __str__(self):
        tmp = '*' if self.poisoned else ''
        return '&lt;' + tmp + str(self.i) + '&gt;'

&nbsp;
class Cube(Graph):
    ''' A Graph derived class in which each vertex (Point)
    may contain a poison trap or not
    '''
    def add(self, node, *nodelist):
        vertex = Point(node, *nodelist)
        self.vList[node] = vertex

    def run(self, N):
        ''' Puts an ant on vertex #1 and let it run for N
        random steps. Stops when the ant enters a poisoned
        vertex or it has run N steps. Returns ant's last position.
        '''
        i = 0
        pos = 1 # Initial ant position
        while i &lt; N:
            i += 1
            pos = choice(self.vList[pos].nodeList)
            if self.vList[pos].venenoso:
                break

        return pos

&nbsp;
if __name__ == '__main__':
    G = Cube()
    G.add(1, 2, 4, 5)
    G.add(2, 1, 3, 6)
    G.add(3, 2, 4, 7)
    G.add(4, 1, 3, 8&#41;
    G.add(5, 1, 6, 8&#41;
    G.add(6, 2, 5, 7)
    G.add(7, 3, 6, 8&#41;
    G.add(8, 4, 5, 7)

    G.vList[7].poisoned = True
    G.vList[8].poisoned = True

    # Carry out simple statistics
    stats = [0, 0, 0] # Counters

    for i in xrange(1000000): # Total number of experiments (runs)
        pos = G.run(100) # make the ant to run 100 steps
        if pos &gt; 6: # Map 0..6 =&gt; 0, 7 =&gt; 1, 8 =&gt; 2
            pos -= 6 # 7 = 1, 8 = 2
        else:
            pos = 0

        stats[pos] += 1 # Increase corresponding counter

    tot = sum(stats) # Total #number of experiments (must be the same number as above)
    print stats # Absolute Frequencies
    print [100 * float(x) / tot for x in stats] # Relative frequencies (percentage)</pre>
<div id="tweetbutton1774" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2011%2F03%2F26%2Fhormigas%2F%3Flang%3Den&amp;via=boriel&amp;text=Ants%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2011%2F03%2F26%2Fhormigas%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2011/03/26/hormigas/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Hamiltonian Path</title>
		<link>http://www.boriel.com/2011/03/22/camino-hamiltoniano/?lang=en</link>
		<comments>http://www.boriel.com/2011/03/22/camino-hamiltoniano/?lang=en#comments</comments>
		<pubDate>Tue, 22 Mar 2011 23:25:17 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Divertimática]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=1738</guid>
		<description><![CDATA[Yesterday at midnight was the deadline of El País math challenge which consisted in finding the Hamiltonian path of a given graph (or to give a demonstration it hadn&#8217;t any as it was the case). A friend of mine told me a simple and elegant demonstration based on graph coloration, which is the one explained [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday at midnight was the deadline of <a href="http://www.elpais.com/articulo/cultura/solucion/hay/solucion/elpepusoc/20110322elpepicul_10/Tes">El País math challenge</a> which consisted in finding the Hamiltonian path of a given graph (or to give a demonstration it hadn&#8217;t any as it was the case). A friend of mine told me a simple and elegant demonstration based on graph coloration, which is the one explained in the video (the video is in Spanish but it&#8217;s my bet there are more demonstrations like this in English on the internet). If you can understand Spanish, I encourage you to watch the video. It&#8217;s really short, entertaining and easy to understand.<br />
<span id="more-1738"></span><br />
This tiny python program finds a  Hamiltonian path for a given graph and prints the solution (or None if it doesn&#8217;t exist, as it happens in this math challenge).</p>
<pre class="brush:python">#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:ts=4:et:sw=4

class Vertex(object):
    def __init__(self, node, *nodeList):
        self.i = node
        self.nodeList = list(nodeList)

    def __hash__(self):
        return self.i

    def reaches(self, vertex):
        ''' Can receive an integer or a Vertex
        '''
        if isinstance(vertex, int):
            return vertex in self.nodeList

        return self.reaches(vertex.i)

    def __str__(self):
        return '&lt; ' + str(self.i) + '&gt;'

    def __repr__(self):
        return self.__str__()

&nbsp;
class Graph(object):
    def __init__(self):
        self.vList = {}

    def add(self, node, *nodeList):
        vertex = Vertex(node, *nodeList)
        self.vList[node] = vertex

    def hamiltonian(self, current = None, pending = None, destiny = None):
        ''' Returns a list of nodes which represent
        a hamiltonian path, or None if not found
        '''
        if pending is None:
            pending = self.vList.values()

        result = None

        if current is None:
            for current in pending:
                result = self.hamiltonian(current, [x for x in pending if x is not current], current)
                if result is not None:
                    break
        else:
            if pending == []:
                if current.reaches(destiny):
                    return [current]
                else:
                    return None

            for x in [self.vList[v] for v in current.nodeList]:
                if x in pending:
                    result = self.hamiltonian(x, [y for y in pending if y is not x], destiny)
                    if result is not None:
                        result = [current] + result
                        break    

        return result

if __name__ == 'main':
    G = Graph()
    G.add(1, 2, 8, 11)
    G.add(2, 1, 6, 9)
    G.add(3, 6, 7, 9, 10)
    G.add(4, 5, 7, 10)
    G.add(5, 4, 8, 11)
    G.add(6, 2, 3, 8&#41;
    G.add(7, 3, 4, 8&#41;
    G.add(8, 1, 6, 7, 5)
    G.add(9, 2, 3, 11)
    G.add(10, 3, 4, 11)
    G.add(11, 1, 9, 10, 5)
    print G.hamiltonian()</pre>
<div id="tweetbutton1738" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2011%2F03%2F22%2Fcamino-hamiltoniano%2F%3Flang%3Den&amp;via=boriel&amp;text=Hamiltonian%20Path%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2011%2F03%2F22%2Fcamino-hamiltoniano%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2011/03/22/camino-hamiltoniano/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Properties in C++</title>
		<link>http://www.boriel.com/2010/07/07/properties-in-cpp/?lang=en</link>
		<comments>http://www.boriel.com/2010/07/07/properties-in-cpp/?lang=en#comments</comments>
		<pubDate>Wed, 07 Jul 2010 13:06:45 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=1362</guid>
		<description><![CDATA[In a previous post I wrote about Python&#8217;s elegance and some tips to make your code look even fancier and closer to the English language. I&#8217;m currently rather busy programming in C++, a language I have not used for 2 years, and the code certainly looks dirtier. Sometimes I want to make a class attribute [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous post I wrote about <a href="http://www.boriel.com/2010/03/03/python-with-elegance/">Python&#8217;s elegance</a> and some tips to make your code look even fancier and closer to the English language. I&#8217;m currently rather busy programming in C++,  a language I have not used for 2 years, and the code certainly looks dirtier. <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Sometimes I want to make a class attribute private, so in order to access it one has to implement a read method and (if needed) a write one. They&#8217;re called <i>getter</i> and <i>setter</i> respectively (I&#8217;m not very fond of private attributes, by the way). This usually happens when the external representation of the attribute value differs with the internal one.<br />
<span id="more-1362"></span></p>
<p>Both Microsoft Visual C++ and Borland Builder C++ implement <i>getters</i> and <i>setters</i> in their own way, but I prefer a standard method which can be used across every C++ standard compiler (including Intel C++ and G++, the GNU C++ compiler). Finally I worked out a solution and implemented this template, and it seems to work. Basically, it implements properties as classes:</p>
<pre class="brush:cpp">template &lt;typename T, class F> class Property
{
private:
	T data;
	F *_instance;
	T (F::*_getter)(void);
	T (F::*_setter)(const T &#038;);

public:
	Property(F *instance,
                T (F::*getter)(void),
                T (F::*setter)(const T &#038;) = NULL)
	{
		_instance = instance;
		_getter = getter;
		_setter = setter;
	}

	Property(F *instance,
                    T initialValue,
                    T (F::*getter)(void),
                    T (F::*setter)(const T &#038;) = NULL)
	{
		_instance = instance;
		_getter = getter;
		_setter = setter;
		data = initialValue;
	}

	Property()
	{
	}

	inline operator T()
                { return (_instance->*_getter)(); }

	inline T operator =(const T &#038;value)
                { return (_instance->*_setter)(value); }

	friend F;
};
</pre>
<p>You will usually put this template into an <i>include</i> file.</p>
<p>Now an example. It creates an &lt;int&gt; container (stored in an STL &lt;vector&gt;), with a <i>length</i> property. Reading this property returns vector.size() value, whilst setting a value to <i>length</i> will effectively resize the vector:</p>
<pre class="brush:cpp">
class IntVector {

private:
        int __setSize(const int &#038;newValue) {
            vectorData.resize(newValue);
            return vectorData.size();
        }

        int __getSize(void) {
            return vectorData.size();
        }

public:
        std::vector&lt;int> vectorData;

        Property&lt;int, IntVector> length;

        IntVector():
            length(this, &#038;IntVector::__getSize, &#038;IntVector::__setSize)
		{
		}
};

int main(int argc, char *argv[])
{
        IntVector myVector;

        myVector.length = 32; // Sets the vector size to 32 elements
        cout &lt;&lt; "Vector size: " &lt;&lt; myVector.length; 

        return EXIT_SUCCESS;
}</pre>
<p>Remember:</p>
<pre class="brush:cpp">
        myVector.length = 32; // Sets the vector size to 32 elements
</pre>
<p>effectively sets the vector length and</p>
<pre class="brush:cpp">
        variable = myVector.length; // Reads the vector size
</pre>
<p>read such length.</p>
<p>Note: This code is released with a <a href="http://creativecommons.org/about/cc0">CC0 (Public Domain)</a> license, so you can use it for whatever you want.</p>
<p>Read more:</p>
<ul>
<li><a href="http://www.codeguru.com/Cpp/Cpp/cpp_mfc/article.php/c4031">Implementing Properties in C++</a></li>
<li><a href="http://www.goingware.com/tips/member-pointers.html">Pointers to C++ Member Functions</a></li>
</ul>
<div id="tweetbutton1362" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2010%2F07%2F07%2Fproperties-in-cpp%2F%3Flang%3Den&amp;via=boriel&amp;text=Properties%20in%20C%2B%2B%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2010%2F07%2F07%2Fproperties-in-cpp%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2010/07/07/properties-in-cpp/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python with elegance</title>
		<link>http://www.boriel.com/2010/03/03/python-with-elegance/?lang=en</link>
		<comments>http://www.boriel.com/2010/03/03/python-with-elegance/?lang=en#comments</comments>
		<pubDate>Wed, 03 Mar 2010 18:50:27 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=1251</guid>
		<description><![CDATA[These last three weeks have been really stressful for me due to various reasons. Anyway, I managed to get some spare time to do something funny, like the self-replicating script posted in the previous entry. Sometimes, when we&#8217;re working on a problem, we need to denote a variable with an uknown value. C and SQL [...]]]></description>
			<content:encoded><![CDATA[<p>These last three weeks have been really stressful for me due to various reasons. Anyway, I managed to get some spare time to do something funny, like the self-replicating script posted in the previous entry.</p>
<p>Sometimes, when we&#8217;re working on a problem, we need to denote a variable with an <em>uknown</em> value. C and SQL uses <em>NULL</em> for this purpose, while Lisp uses <em>nil</em> and Python have <em>None</em>. Let&#8217;s look python closer: sometimes, <em>None</em> really means <em>none</em>. But there are some cases we need another semantic interpretation. For example, I like to work with the <em>unknown</em> value, which means something different to <em>none</em> (<em>None</em>) which I use for different a purpose. We can do this:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">Unknown</span><span class="hl-brackets">(</span><span class="hl-identifier">object</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-reserved">pass
</span><span class="hl-brackets">[</span><span class="hl-code">...</span><span class="hl-brackets">]
</span><span class="hl-reserved">if </span><span class="hl-identifier">b </span><span class="hl-reserved">is </span><span class="hl-identifier">Unknown</span><span class="hl-default">:
   </span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Var b is unknown</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>This works because in python, Classes are also objects. The <em>is</em> operator only returns True if two objects are the same instance. Even better, the <em>is</em> operator allows an interesting syntax when used with <em>not</em>:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">if </span><span class="hl-identifier">b </span><span class="hl-reserved">is not </span><span class="hl-identifier">Unknown</span><span class="hl-default">:
    </span><span class="hl-brackets">[</span><span class="hl-code">...</span><span class="hl-brackets">]</span></pre></div></div>
<p>This not only enhances (IMHO) code legibility, it also make comments useless: the code is the comment itself.</p>
<p>Finally, this idea can be also used to define your own <em>Infinitum</em> value:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">class </span><span class="hl-identifier">Inf</span><span class="hl-brackets">(</span><span class="hl-identifier">object</span><span class="hl-brackets">)</span><span class="hl-default">:
    </span><span class="hl-reserved">pass

try</span><span class="hl-default">:
   </span><span class="hl-identifier">b</span><span class="hl-default"> = </span><span class="hl-number">1</span><span class="hl-default"> / </span><span class="hl-number">0
</span><span class="hl-reserved">except </span><span class="hl-identifier">ZeroDivideError</span><span class="hl-default">:
   </span><span class="hl-identifier">b</span><span class="hl-default"> = </span><span class="hl-identifier">Inf

</span><span class="hl-reserved">if </span><span class="hl-identifier">b </span><span class="hl-reserved">is </span><span class="hl-identifier">Inf</span><span class="hl-default">:
   </span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">b is Inf</span><span class="hl-quotes">&quot;</span></pre></div></div>
<div id="tweetbutton1251" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2010%2F03%2F03%2Fpython-with-elegance%2F%3Flang%3Den&amp;via=boriel&amp;text=Python%20with%20elegance%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2010%2F03%2F03%2Fpython-with-elegance%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2010/03/03/python-with-elegance/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Self-replication</title>
		<link>http://www.boriel.com/2010/02/10/selfreplication/?lang=en</link>
		<comments>http://www.boriel.com/2010/02/10/selfreplication/?lang=en#comments</comments>
		<pubDate>Wed, 10 Feb 2010 21:40:06 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=1229</guid>
		<description><![CDATA[Reading an article linked from Barrapunto (a Spanish Slashdot-like site) entitled Reflections on Trusting Trust it came into my attention the paragraph about self-reproducing programs. The author of the article, Ken Thomson says: More precisely stated, the problem is to write a source program that, when compiled and executed, will produce as output an exact [...]]]></description>
			<content:encoded><![CDATA[<p>Reading an article linked from <a href="http://barrapunto.com/articles/10/02/09/1126247.shtml">Barrapunto</a> (a Spanish Slashdot-like site) entitled <a href="http://cm.bell-labs.com/who/ken/trust.html">Reflections on Trusting Trust</a> it came into my attention the paragraph about self-reproducing programs. The author of the article,  <a href="http://en.wikipedia.org/wiki/Ken_Thompson">Ken Thomson</a> says:</p>
<blockquote><p>More precisely stated, the problem is to write a source program that, when compiled and executed, will produce as output an exact copy of its source. If you have never done this, I urge you to try it on your own. The discovery of how to do it is a revelation that far surpasses any benefit obtained by being told how to do it.</p></blockquote>
<p>I&#8217;ve never tried to code such a thing, so I put my shoulder to the wheel and try this exercise in python. After a few minutes, I got this solution. It&#8217;s not the shortest (especially considering it reproduces the shebang and the coding lines), but it works. As Mr. Thomson says, it&#8217;s a very interesting exercise. Here&#8217;s the code:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/env python
# -*- coding: utf-8 -*-
</span><span class="hl-identifier">data</span><span class="hl-default"> = </span><span class="hl-brackets">[</span><span class="hl-quotes">'</span><span class="hl-string">#!/usr/bin/env python</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string"># -*- coding: utf-8 -*-</span><span class="hl-quotes">'</span><span class="hl-code">,
        </span><span class="hl-quotes">&quot;</span><span class="hl-string">for x in data[:2]:</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">    print x</span><span class="hl-quotes">&quot;</span><span class="hl-code">,
        </span><span class="hl-quotes">&quot;</span><span class="hl-string">print 'data = ', data</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">for x in data[2:]:</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">    print x</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">]
</span><span class="hl-reserved">for </span><span class="hl-identifier">x </span><span class="hl-reserved">in </span><span class="hl-identifier">data</span><span class="hl-brackets">[</span><span class="hl-code">:</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">:
    </span><span class="hl-reserved">print </span><span class="hl-identifier">x
</span><span class="hl-reserved">print </span><span class="hl-quotes">'</span><span class="hl-string">data = </span><span class="hl-quotes">'</span><span class="hl-default">, </span><span class="hl-identifier">data
</span><span class="hl-reserved">for </span><span class="hl-identifier">x </span><span class="hl-reserved">in </span><span class="hl-identifier">data</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-code">:</span><span class="hl-brackets">]</span><span class="hl-default">:
    </span><span class="hl-reserved">print </span><span class="hl-identifier">x</span></pre></div></div>
<p>Save it as prog1.py, and then execute <strong>python prog1.py</strong>.<br />
Even better, try <strong>python prog1.py &gt; prog2.py</strong> and then <strong>python prog2.py &gt; prog3.py</strong> and so on.</p>
<div id="tweetbutton1229" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2010%2F02%2F10%2Fselfreplication%2F%3Flang%3Den&amp;via=boriel&amp;text=Self-replication%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2010%2F02%2F10%2Fselfreplication%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2010/02/10/selfreplication/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>XHTML Strict 1.0 valid reCaptcha</title>
		<link>http://www.boriel.com/2009/07/29/xhtml-strict-valid-recaptcha/?lang=en</link>
		<comments>http://www.boriel.com/2009/07/29/xhtml-strict-valid-recaptcha/?lang=en#comments</comments>
		<pubDate>Wed, 29 Jul 2009 10:45:52 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Blogosphere]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=780</guid>
		<description><![CDATA[I&#8217;m currently working on a web which uses a contact form. To avoid Spam, I use reCaptcha which is a very good Captcha system (even better than the one I used for the blog comments). You can see it in action in the contact form of this blog. The problem with the default reCaptcha output [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on a web which uses a contact form. To avoid Spam, I use <a href="http://recatpcha.net">reCaptcha</a> which is a very good Captcha system (even better than <a href="http://www.boriel.com/plugins/captcha-plugin/">the one</a> I used for the blog comments). You can see it in action in the contact form of this blog.</p>
<p>The problem with the default reCaptcha output is that it does not validates XHTML 1.0 Strict because the use of iframes. But you can change the code at the recaptchalib.php file (available <a href="http://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest">here</a>). This problem persist at the current version (1.10 at this moment).</p>
<p>Replacing the code at lines #122 &#8211; #128 with:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-inlinetags">&lt;?php
    </span><span class="hl-reserved">return </span><span class="hl-quotes">'</span><span class="hl-string">&lt;script type=&quot;text/javascript&quot; src=&quot;</span><span class="hl-quotes">'</span><span class="hl-code">. </span><span class="hl-var">$server</span><span class="hl-code"> . </span><span class="hl-quotes">'</span><span class="hl-string">/challenge?k=</span><span class="hl-quotes">'</span><span class="hl-code"> . </span><span class="hl-var">$pubkey</span><span class="hl-code"> . </span><span class="hl-var">$errorpart</span><span class="hl-code"> . </span><span class="hl-quotes">'</span><span class="hl-string">&quot;&gt;&lt;/script&gt;
        &lt;noscript&gt;
            &lt;div id=&quot;no_js_recaptcha&quot;&gt;
                &lt;object type=&quot;text/html&quot; data=&quot;http://api.recaptcha.net/noscript?k=</span><span class="hl-quotes">'</span><span class="hl-code"> . </span><span class="hl-var">$pubkey</span><span class="hl-code"> . </span><span class="hl-quotes">'</span><span class="hl-string">&quot; width=&quot;500&quot; height=&quot;300&quot;&gt;
                &lt;!--[if IE]&gt;
                &lt;iframe width=&quot;500&quot; height=&quot;300&quot; frameborder=&quot;0&quot; src=&quot;http://api.recaptcha.net/noscript?k=</span><span class="hl-quotes">'</span><span class="hl-code"> . </span><span class="hl-var">$pubkey</span><span class="hl-code"> . </span><span class="hl-quotes">'</span><span class="hl-string">&quot;&gt;&lt;/iframe&gt;
                &lt; ![endif]--&gt;
                &lt;/object&gt;
            &lt;/div&gt;
        &lt;/noscript&gt;</span><span class="hl-quotes">'</span><span class="hl-code">;
</span><span class="hl-inlinetags">?&gt;</span></pre></div></div>
<p>will do the trick <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<div id="tweetbutton780" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2009%2F07%2F29%2Fxhtml-strict-valid-recaptcha%2F%3Flang%3Den&amp;via=boriel&amp;text=XHTML%20Strict%201.0%20valid%20reCaptcha%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2009%2F07%2F29%2Fxhtml-strict-valid-recaptcha%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2009/07/29/xhtml-strict-valid-recaptcha/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>JP08 &#8211; Parallel Computing Conferences 2008</title>
		<link>http://www.boriel.com/2008/09/28/jp08-jornadas-de-paralelismo-2008/?lang=en</link>
		<comments>http://www.boriel.com/2008/09/28/jp08-jornadas-de-paralelismo-2008/?lang=en#comments</comments>
		<pubDate>Sun, 28 Sep 2008 16:24:57 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Parallel Computing]]></category>
		<category><![CDATA[Pictures]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.boriel.com/?p=276</guid>
		<description><![CDATA[Last week I attended the National Parallel Computing Conferences 2008 (JP08), thanks to my university, which payed to us (a group of pre-PhD researchers) the trip and estance. I talked about OpenMP and some problemas with Dynamic Data structures. There were many other speeches both from my colleagues and from people of other universities. The [...]]]></description>
			<content:encoded><![CDATA[<p>
 Last week I attended the National Parallel Computing Conferences 2008 (<a href="http://jp08.uji.es">JP08</a>), thanks to <a href="http://www.ull.es">my university</a>, which payed to us (a group of pre-PhD researchers) the trip and estance. I talked about <a href="http://openmp.org/wp/">OpenMP</a> and some problemas with Dynamic Data structures. </p>
<p> There were many other speeches both from my colleagues and from people of other universities.<br />
 <a href="http://www.boriel.com/wp-images/archive/jp08/jp08-1.jpg"><img style="border: solid 1px #404040" title="Exposicion" src="http://www.boriel.com/wp-images/archive/jp08/_jp08-1.jpg" alt="Exposicion" width="320" height="240" /></a> </p>
<p> The conferences were hosted at <a href="http://www.uji.es/">Universidat Jaume I</a> located at Castellón de la Plana, Spain. There also is an <a href="http://picasaweb.google.com/XIX.JP08">official photo gallery</a>. Besides that, I also took tome photos. </p>
<div class="gallery">      <a href="http://www.boriel.com/blog/wp-content/plugins/iimage-gallery.php?idpost=276&amp;idg=1&amp;idi=1" class="gallery_item"><img src="http://www.boriel.com/blog/wp-content/thumb-cache/b739e87a7e2ac7fbc71d39eff75bc5d5.jpg" alt="Campus UJI" title="Campus UJI" border="0" /></a><a href="http://www.boriel.com/blog/wp-content/plugins/iimage-gallery.php?idpost=276&amp;idg=1&amp;idi=2" class="gallery_item"><img src="http://www.boriel.com/blog/wp-content/thumb-cache/3ebd48c1eec22f4620b76decf79b4295.jpg" alt="Parque Ribalta" title="Parque Ribalta" border="0" /></a><a href="http://www.boriel.com/blog/wp-content/plugins/iimage-gallery.php?idpost=276&amp;idg=1&amp;idi=3" class="gallery_item"><img src="http://www.boriel.com/blog/wp-content/thumb-cache/75d9ff5afef5d23da1a54faa67415336.jpg" alt="Me at Parque Ribalta" title="Me at Parque Ribalta" border="0" /></a><a href="http://www.boriel.com/blog/wp-content/plugins/iimage-gallery.php?idpost=276&amp;idg=1&amp;idi=4" class="gallery_item"><img src="http://www.boriel.com/blog/wp-content/thumb-cache/187331a7861129727d4a7f873c67611a.jpg" alt="Parque Ribalta" title="Parque Ribalta" border="0" /></a><a href="http://www.boriel.com/blog/wp-content/plugins/iimage-gallery.php?idpost=276&amp;idg=1&amp;idi=5" class="gallery_item"><img src="http://www.boriel.com/blog/wp-content/thumb-cache/0973204bd7e30b225b52f9eb73a17140.jpg" alt="Nice colleagues at dinner" title="Nice colleagues at dinner" border="0" /></a></div>
<p> It was a nice experience and I met very friendly people. <img src='http://www.boriel.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  More photos <a href="http://picasaweb.google.com/boriel/JP08?authkey=sNmiZ0otrdk#">here</a>. </p>
<div id="tweetbutton276" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2008%2F09%2F28%2Fjp08-jornadas-de-paralelismo-2008%2F%3Flang%3Den&amp;via=boriel&amp;text=JP08%20%26%238211%3B%20Parallel%20Computing%20Conferences%202008%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2008%2F09%2F28%2Fjp08-jornadas-de-paralelismo-2008%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2008/09/28/jp08-jornadas-de-paralelismo-2008/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>My own compiler written in Python</title>
		<link>http://www.boriel.com/2008/02/06/python-compiler-tool/?lang=en</link>
		<comments>http://www.boriel.com/2008/02/06/python-compiler-tool/?lang=en#comments</comments>
		<pubDate>Wed, 06 Feb 2008 12:05:32 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.boriel.com/2008/02/06/python-compiler-tool/</guid>
		<description><![CDATA[During these last months I&#8217;ve been developing, just for fun and in my *very* little spare time, my own compiler kit tool. More than a compiler, it&#8217;s a tool for building up compilers (there are others over there, like Lex &#038; Yacc, Flex &#038; Bison in its GNU flavour). Mine is called Bparser. I&#8217;ve put [...]]]></description>
			<content:encoded><![CDATA[<p>During these last months I&#8217;ve been developing, just for fun and in my <b>*very*</b> little spare time, my own compiler kit tool.<br />
More than a compiler, it&#8217;s a <b>tool for building up compilers</b> (there are others over there, like <a href="http://dinosaur.compilertools.net/">Lex &#038; Yacc</a>, Flex &#038; Bison in its GNU flavour). Mine is called <abbr title="Boriel Parser">Bparser</abbr>.</p>
<p>I&#8217;ve put it in my <a href="http://www.boriel.com/wiki/en/index.php/Compilers_and_Compiler_Tools">wiki</a>, just in case anybody want to give it a glance.</p>
<p>My tool is better than the LEX &#038; YACC couple in the sense it can parse LR(n) grammars whilst LEX/Yacc only parses <a href="http://en.wikipedia.org/wiki/LALR_parser">LALR</a> ones. It uses a Lookahead and mangle-likes estructures to take some decisions when choosing which grammar rule to use for reduction.</p>
<p>If it eventually cannot decide which rule should be used, it will start to <a href="http://en.wikipedia.org/wiki/Backtracking">backtrack</a> to find out the rule to apply.</p>
<p>This tool allows ambiguos grammars, so it is very similar to a <a href="http://en.wikipedia.org/wiki/GLR_parser">GLR</a> parser. BISON (GNU&#8217;s YACC) can also use the GLR algorithm, but it is less efficient and can take exponential space/time to parse some entries depending on the given grammar.</p>
<p><span id="more-119"></span></p>
<h2>The calculator example</h2>
<p>The calculator example is the <i>&#8220;Hello World&#8221;</i> of parsers generators. Below there is an calculator definition for mathematical expresions. Simply run the program and enter a mathematic expression like <i>1+3.2*5^2</i> and the program will output the result to the screen (with some explanation about rules used). The program will finish when you enter an empty input.</p>
<div class="hl-surround" style="height:280px;"><div class="hl-main"><pre><span class="hl-comment">#!/usr/bin/python
# -*- coding: utf-8 -*-

</span><span class="hl-reserved">import </span><span class="hl-identifier">sys

</span><span class="hl-reserved">from </span><span class="hl-identifier">bparser </span><span class="hl-reserved">import </span><span class="hl-identifier">Token
</span><span class="hl-reserved">from </span><span class="hl-identifier">bparser </span><span class="hl-reserved">import </span><span class="hl-identifier">Symbol
</span><span class="hl-reserved">from </span><span class="hl-identifier">bparser </span><span class="hl-reserved">import </span><span class="hl-identifier">Bparser

</span><span class="hl-reserved">class </span><span class="hl-identifier">Calc</span><span class="hl-brackets">(</span><span class="hl-identifier">Bparser</span><span class="hl-brackets">)</span><span class="hl-default">:
	</span><span class="hl-reserved">def </span><span class="hl-identifier">minus_factor</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = -</span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">term_mul_term</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> * </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">term_div_term</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> / </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">term_plus_term</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> + </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">term_minus_term</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> - </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value	
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">term_pow_term</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> ** </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">2</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">print_result</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-reserved">print </span><span class="hl-quotes">'</span><span class="hl-string">Result is</span><span class="hl-quotes">'</span><span class="hl-default">, </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol
	
	</span><span class="hl-reserved">def </span><span class="hl-identifier">lprp_action</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol

	</span><span class="hl-reserved">def </span><span class="hl-identifier">default_hook</span><span class="hl-brackets">(</span><span class="hl-identifier">self</span><span class="hl-code">, </span><span class="hl-identifier">symbol</span><span class="hl-code">, </span><span class="hl-identifier">l</span><span class="hl-brackets">)</span><span class="hl-default">:
		</span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value</span><span class="hl-default"> = </span><span class="hl-identifier">l</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-default">.</span><span class="hl-identifier">value
	
		</span><span class="hl-reserved">print </span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">name</span><span class="hl-default">, </span><span class="hl-quotes">'</span><span class="hl-string">--&gt;</span><span class="hl-quotes">'</span><span class="hl-default">, </span><span class="hl-brackets">[</span><span class="hl-identifier">x</span><span class="hl-code">.</span><span class="hl-identifier">name </span><span class="hl-reserved">for </span><span class="hl-identifier">x </span><span class="hl-reserved">in </span><span class="hl-identifier">l</span><span class="hl-brackets">]
		</span><span class="hl-reserved">print </span><span class="hl-quotes">'</span><span class="hl-string">ValueOf(%s) = </span><span class="hl-quotes">'</span><span class="hl-default"> % </span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">name</span><span class="hl-default">, </span><span class="hl-identifier">symbol</span><span class="hl-default">.</span><span class="hl-identifier">value 
	
		</span><span class="hl-reserved">return </span><span class="hl-identifier">symbol

calc</span><span class="hl-default"> = </span><span class="hl-identifier">Calc</span><span class="hl-brackets">()

</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">add_token</span><span class="hl-brackets">(</span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-string">[0-9]+(</span><span class="hl-special">\.</span><span class="hl-string">[0-9]+)?([eE][-+]?[0-9]+)?</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">NUMBER</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">add_tokens</span><span class="hl-brackets">(</span><span class="hl-code">{</span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\+</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">PLUS</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\-</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">MINUS</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\*</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">MUL</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">/</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">DIV</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-quotes">'</span><span class="hl-string">%</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">MOD</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\^</span><span class="hl-quotes">'</span><span class="hl-code">:</span><span class="hl-quotes">'</span><span class="hl-string">POW</span><span class="hl-quotes">'</span><span class="hl-code">}</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">add_tokens</span><span class="hl-brackets">(</span><span class="hl-code">{</span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\(</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">LP</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-identifier">r</span><span class="hl-quotes">'</span><span class="hl-special">\)</span><span class="hl-quotes">'</span><span class="hl-code">: </span><span class="hl-quotes">'</span><span class="hl-string">RP</span><span class="hl-quotes">'</span><span class="hl-code">}</span><span class="hl-brackets">) </span><span class="hl-comment"># Escape PARENTHESIS in Regular Expressions
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">add_token</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">[ </span><span class="hl-special">\t\n</span><span class="hl-string">]+</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">SEPARATOR</span><span class="hl-quotes">'</span><span class="hl-code">, </span><span class="hl-identifier">Token</span><span class="hl-code">.</span><span class="hl-identifier">skip</span><span class="hl-brackets">))

</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">%LEFT (PLUS, MINUS)</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">%LEFT (MUL, DIV)</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">%RIGHT (POW)</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">Program --&gt; expr .print_result</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">expr --&gt; term</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; term PLUS term .term_plus_term</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; term MINUS term .term_minus_term</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; term MUL term .term_mul_term</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; term DIV term .term_minus_term</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; term POW term .term_pow_term</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">term --&gt; value</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">value --&gt; LP expr RP .lprp_action</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">value --&gt; NUMBER</span><span class="hl-quotes">'</span><span class="hl-brackets">)
</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse_rule</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">value --&gt; MINUS NUMBER .minus_factor</span><span class="hl-quotes">'</span><span class="hl-brackets">)


</span><span class="hl-reserved">if </span><span class="hl-identifier">__name__</span><span class="hl-default"> == </span><span class="hl-quotes">'</span><span class="hl-string">__main__</span><span class="hl-quotes">'</span><span class="hl-default">:
	</span><span class="hl-reserved">while True</span><span class="hl-default">:
		</span><span class="hl-identifier">input</span><span class="hl-default"> = </span><span class="hl-identifier">sys</span><span class="hl-default">.</span><span class="hl-identifier">stdin</span><span class="hl-default">.</span><span class="hl-identifier">readline</span><span class="hl-brackets">()</span><span class="hl-default">.</span><span class="hl-identifier">strip</span><span class="hl-brackets">()
		</span><span class="hl-reserved">if </span><span class="hl-identifier">input</span><span class="hl-default"> == </span><span class="hl-quotes">''</span><span class="hl-default">: </span><span class="hl-reserved">break
	
		</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">set_buffer</span><span class="hl-brackets">(</span><span class="hl-identifier">input</span><span class="hl-brackets">)
		</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">verbose</span><span class="hl-default"> = </span><span class="hl-reserved">False
		</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">start</span><span class="hl-brackets">()
		</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">storage</span><span class="hl-default">.</span><span class="hl-identifier">pow_level</span><span class="hl-default"> = </span><span class="hl-brackets">[]
	
		</span><span class="hl-reserved">if </span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">parse</span><span class="hl-brackets">()</span><span class="hl-default">:
			</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">verbose</span><span class="hl-default"> = </span><span class="hl-reserved">True
			</span><span class="hl-identifier">calc</span><span class="hl-default">.</span><span class="hl-identifier">mimic</span><span class="hl-brackets">()
		</span><span class="hl-reserved">else</span><span class="hl-default">:
			</span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;</span><span class="hl-string">Syntax Error.</span><span class="hl-quotes">&quot;</span></pre></div></div>
<p>I&#8217;m using this tool in the <a href="http://www.speccy.org/zxbcompiler/">ZX Spectrum BASIC Compiler</a>, that will serve as a test-language for this tool.</p>
<div id="tweetbutton119" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2008%2F02%2F06%2Fpython-compiler-tool%2F%3Flang%3Den&amp;via=boriel&amp;text=My%20own%20compiler%20written%20in%20Python%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2008%2F02%2F06%2Fpython-compiler-tool%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2008/02/06/python-compiler-tool/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>libgmail throught HTTP(S) proxy</title>
		<link>http://www.boriel.com/2007/09/22/libgmail-proxy-https/?lang=en</link>
		<comments>http://www.boriel.com/2007/09/22/libgmail-proxy-https/?lang=en#comments</comments>
		<pubDate>Sat, 22 Sep 2007 19:57:47 +0000</pubDate>
		<dc:creator>Boriel</dc:creator>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.boriel.com/2007/09/22/libgmail-proxy-https/</guid>
		<description><![CDATA[libgmail is, in words of its author, a pure python binding to provide access to your gmail account. I&#8217;m using it and find it very useful. However, within my organization, I must connect to the internet via an HTTP/HTTPS proxy firewall. Direct internet connections are filtered, so I developed a hack to make it to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://libgmail.sourceforge.net/">libgmail</a> is, in words of its author, <i>a pure python binding</i> to provide access to your gmail account.</p>
<p>I&#8217;m using it and find it very useful. However, within my organization, I must connect to the internet via an HTTP/HTTPS proxy firewall. Direct internet connections are filtered, so I developed a hack to make it to work via an HTTP(S) proxied connection.</p>
<p>Basically I ripped the code from <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/456195">this ASPN snippet</a>, and derived the urllib proxied transport to create a new one.</p>
<p>To use it behind a proxy, you can copy the <a href="http://libgmail.sourceforge.net/">libgmail example</a>, and define the proxy this way:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">import </span><span class="hl-identifier">libgmail

libgmail</span><span class="hl-default">.</span><span class="hl-identifier">PROXY_URL</span><span class="hl-default"> = </span><span class="hl-quotes">'</span><span class="hl-string">www.myproxy.org:3128</span><span class="hl-quotes">'  </span><span class="hl-comment"># Define the proxy.

</span><span class="hl-identifier">ga</span><span class="hl-default"> = </span><span class="hl-identifier">libgmail</span><span class="hl-default">.</span><span class="hl-identifier">GmailAccount</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">google@gmail.com</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">mymailismypass</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
</span><span class="hl-identifier">ga</span><span class="hl-default">.</span><span class="hl-identifier">login</span><span class="hl-brackets">()
</span><span class="hl-identifier">folder</span><span class="hl-default"> = </span><span class="hl-identifier">ga</span><span class="hl-default">.</span><span class="hl-identifier">getMessagesByFolder</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">inbox</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-reserved">for </span><span class="hl-identifier">thread </span><span class="hl-reserved">in </span><span class="hl-identifier">folder</span><span class="hl-default">:
    </span><span class="hl-reserved">print </span><span class="hl-identifier">thread</span><span class="hl-default">.</span><span class="hl-identifier">id</span><span class="hl-default">, </span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">thread</span><span class="hl-brackets">)</span><span class="hl-default">, </span><span class="hl-identifier">thread</span><span class="hl-default">.</span><span class="hl-identifier">subject
    </span><span class="hl-reserved">for </span><span class="hl-identifier">msg </span><span class="hl-reserved">in </span><span class="hl-identifier">thread</span><span class="hl-default">:
        </span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;  &quot;</span><span class="hl-default">, </span><span class="hl-identifier">msg</span><span class="hl-default">.</span><span class="hl-identifier">id</span><span class="hl-default">, </span><span class="hl-identifier">msg</span><span class="hl-default">.</span><span class="hl-identifier">number</span><span class="hl-default">, </span><span class="hl-identifier">msg</span><span class="hl-default">.</span><span class="hl-identifier">subject
    </span><span class="hl-reserved">print </span><span class="hl-identifier">msg</span><span class="hl-default">.</span><span class="hl-identifier">source</span></pre></div></div>
<p>I patched the <tt>libgmail.py</tt> file and write the proxied transport in another one: <tt>gmail_transport.py</tt>. You will need to copy both the file <a href="http://www.boriel.com/files/libgmail/gmail_transport.py">gmail_transport.py</a> and the modified version of <a href="http://www.boriel.com/files/libgmail/libgmail.py">libgmail.py</a> (overwrite it)  into the same directory <tt>libgmail.py</tt> is installed. </p>
<p>There is also a <a href="http://www.boriel.com/files/libgmail/libgmail-0.1.6.2-proxy.diff">diff file</a> available, for libgmail-0.6.1.2.</p>
<p><b>Update (2007-10-06):</b> It&#8217;s now possible to use proxy authentication. To do it so, you must use the proxy host address on the previous example this way: <tt>user:password@myproxy.org:3128</tt> where <i>user</i> and <i>password</i> are your proxy user and password respectively. You&#8217;ll have to download again the file <a href="http://www.boriel.com/files/libgmail/gmail_transport.py">gmail_transport.py</a>.</p>
<p>Now, the example to use it behind a proxy requiring with user/passwd authentication is like this:</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-reserved">import </span><span class="hl-identifier">libgmail

</span><span class="hl-comment"># Connect from behind a proxy www.myproxy.org:3128 using
# proxy authentication user = 'john', password = 'proxy4321'
</span><span class="hl-identifier">libgmail</span><span class="hl-default">.</span><span class="hl-identifier">PROXY_URL</span><span class="hl-default"> = </span><span class="hl-quotes">'</span><span class="hl-string">john:proxy4321@www.myproxy.org:3128</span><span class="hl-quotes">'  </span><span class="hl-comment"># Define the proxy 

</span><span class="hl-identifier">ga</span><span class="hl-default"> = </span><span class="hl-identifier">libgmail</span><span class="hl-default">.</span><span class="hl-identifier">GmailAccount</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">google@gmail.com</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string">mymailismypass</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)
</span><span class="hl-identifier">ga</span><span class="hl-default">.</span><span class="hl-identifier">login</span><span class="hl-brackets">()
</span><span class="hl-identifier">folder</span><span class="hl-default"> = </span><span class="hl-identifier">ga</span><span class="hl-default">.</span><span class="hl-identifier">getMessagesByFolder</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">inbox</span><span class="hl-quotes">'</span><span class="hl-brackets">)

</span><span class="hl-reserved">for </span><span class="hl-identifier">thread </span><span class="hl-reserved">in </span><span class="hl-identifier">folder</span><span class="hl-default">:
    </span><span class="hl-reserved">print </span><span class="hl-identifier">thread</span><span class="hl-default">.</span><span class="hl-identifier">id</span><span class="hl-default">, </span><span class="hl-builtin">len</span><span class="hl-brackets">(</span><span class="hl-identifier">thread</span><span class="hl-brackets">)</span><span class="hl-default">, </span><span class="hl-identifier">thread</span><span class="hl-default">.</span><span class="hl-identifier">subject
    </span><span class="hl-reserved">for </span><span class="hl-identifier">msg </span><span class="hl-reserved">in </span><span class="hl-identifier">thread</span><span class="hl-default">:
        </span><span class="hl-reserved">print </span><span class="hl-quotes">&quot;  &quot;</span><span class="hl-default">, </span><span class="hl-identifier">msg</span><span class="hl-default">.</span><span class="hl-identifier">id</span><span class="hl-default">, </span><span class="hl-identifier">msg</span><span class="hl-default">.</span><span class="hl-identifier">number</span><span class="hl-default">, </span><span class="hl-identifier">msg</span><span class="hl-default">.</span><span class="hl-identifier">subject
    </span><span class="hl-reserved">print </span><span class="hl-identifier">msg</span><span class="hl-default">.</span><span class="hl-identifier">source</span></pre></div></div>
<div id="tweetbutton113" class="tw_button" style="float:right;margin-left:10px;"><a href="http://twitter.com/share?url=http%3A%2F%2Fwww.boriel.com%2F2007%2F09%2F22%2Flibgmail-proxy-https%2F%3Flang%3Den&amp;via=boriel&amp;text=libgmail%20throught%20HTTP%28S%29%20proxy%20-%20boriel.com&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fwww.boriel.com%2F2007%2F09%2F22%2Flibgmail-proxy-https%2F%3Flang%3Den" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://www.boriel.com/blog/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.boriel.com/2007/09/22/libgmail-proxy-https/?lang=en/feed/&#038;lang=en</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

