<?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>Arunrocks &#187; Python</title> <atom:link href="http://www.arunrocks.com/blog/archives/category/technical/python/feed/" rel="self" type="application/rss+xml" /><link>http://www.arunrocks.com/blog</link> <description>Representing Anti-monotonistic Tendencies</description> <lastBuildDate>Sun, 15 Aug 2010 06:25:55 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.8.6</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <item><title>Mars Rover in Python and Haskell</title><link>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/</link> <comments>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/#comments</comments> <pubDate>Mon, 01 Feb 2010 07:02:52 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[Python]]></category> <category><![CDATA[Technical]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=234</guid> <description><![CDATA[Last week I tried to do something which I've been planning for quite sometime. Porting a Python program into Haskell.  I first solved the Mars Rover (used by Thoughtworks) in Python and later attempted to solve the same in Haskell.]]></description> <content:encoded><![CDATA[<p>Last week I tried to do something which I&#8217;ve been planning for quite sometime. Porting a Python program into Haskell. In case you didn&#8217;t know, <a
href="http://www.haskell.org/">Haskell</a> is a purely functional programming language that&#8217;s recently become a hot favourite. It has a lot of cutting edge ideas from the academic world esp laziness and strong typing. It has an interesting way to solve the &#8216;multi-CPU problem&#8217;.</p><p>Mars Rover is a famous programming problem used by <a
href="http://www.thoughtworks.com/">Thoughtworks</a> in their recruitments. I first solved the problem in Python and later attempted to solve the same in Haskell. I cannot say that I ported it from Python because the approach I&#8217;ve used is completely different.</p><h3>The Problem</h3><blockquote><p>A squad of robotic rovers are to be landed by NASA on a plateau on Mars.</p><p>This plateau, which is curiously rectangular, must be navigated by the rovers so that their on-board cameras can get a complete view of the surrounding terrain to send back to Earth.</p><p>A rover&#8217;s position and location is represented by a combination of x and y co-ordinates and a letter representing one of the four cardinal compass points. The plateau is divided up into a grid to simplify navigation. An example position might be 0, 0, N, which means the rover is in the bottom left corner and facing North.</p><p>In order to control a rover , NASA sends a simple string of letters. The possible letters are &#8216;L&#8217;, &#8216;R&#8217; and &#8216;M&#8217;. &#8216;L&#8217; and &#8216;R&#8217; makes the rover spin 90 degrees left or right respectively, without moving from its current spot. &#8216;M&#8217; means move forward one grid point, and maintain the same heading.</p><p>Assume that the square directly North from (x, y) is (x, y+1).</p><p>INPUT:</p><p>The first line of input is the upper-right coordinates of the plateau, the lower-left coordinates are assumed to be 0,0.</p><p>The rest of the input is information pertaining to the rovers that have been deployed. Each rover has two lines of input. The first line gives the rover&#8217;s position, and the second line is a series of instructions telling the rover how to explore the plateau.</p><p>The position is made up of two integers and a letter separated by spaces, corresponding to the x and y co-ordinates and the rover&#8217;s orientation.</p><p>Each rover will be finished sequentially, which means that the second rover won&#8217;t start to move until the first one has finished moving.</p><p>OUTPUT</p><p>The output for each rover should be its final co-ordinates and heading.</p><p>INPUT AND OUTPUT</p><p>Test Input:</p><p>5 5<br
/> 1 2 N<br
/> LMLMLMLMM<br
/> 3 3 E<br
/> MMRMMRMRRM<br
/> <br
/> Expected Output:<br
/> <br
/> 1 3 N<br
/> 5 1 E</p></blockquote><h3>The Python solution</h3><p>The Python solution is actually smaller than the problem itself. The readability isn&#8217;t that great, but it is quite extensible. In fact, adding a new instruction like <code>B(ackward)</code> would need just one additional line. You can also extend the four cardinal directions to eight with minimal changes to the code.</p> <script src="http://gist.github.com/291495.js?file=rover.py"></script> <h3>The Haskell solution</h3><p>I am a beginner in Haskell, so apologies for any bad coding practices. You might notice that rather than using Reflection as in the Python code, I have used Type-inference to invoke the correct function for each instruction. Yet again, this scales well while adding new instructions.</p> <script src="http://gist.github.com/291497.js?file=rover.hs"></script> <h3>Key learnings</h3><p>Since some of you might be interested in Haskell, I have tried to summarize my experience in Haskell programming</p><ol><li>There are no loop constructs. So everything must be done using recursion!</li><li>Haskell I/O is very hard. This is because of my little knowledge of Monads. In fact, I solved the logic pretty quickly. It took me a while to figure out the input parsing.</li><li>Type inference catches a lot of errors. This is quite handy but error messages are sometimes confusing</li><li>I could have used Abstract Data Types for directions but it would have made the code lengthier</li></ol><p>In short, programming in Haskell is a mind-bending exercise. Highly recommended!</p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2010/02/01/mars-rover-in-python-and-haskell/feed/</wfw:commentRss> <slash:comments>14</slash:comments> </item> <item><title>Managing Creative Assets &#8211; 2: Distributed Version Control Systems</title><link>http://www.arunrocks.com/blog/archives/2009/12/13/managing-creative-assets-2/</link> <comments>http://www.arunrocks.com/blog/archives/2009/12/13/managing-creative-assets-2/#comments</comments> <pubDate>Mon, 14 Dec 2009 02:31:29 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[Python]]></category> <category><![CDATA[Technical]]></category> <category><![CDATA[backup]]></category> <category><![CDATA[bazaar]]></category> <category><![CDATA[creativity]]></category> <category><![CDATA[distributed version control]]></category> <category><![CDATA[git]]></category> <category><![CDATA[hg]]></category> <category><![CDATA[lifehacks]]></category> <category><![CDATA[mercurial]]></category> <category><![CDATA[productivity]]></category> <category><![CDATA[version control]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=171</guid> <description><![CDATA[Managing Creative Assets is a multi-part series on how you can manage your creative works such as a novel or even blog post without impairing your creativity. It highlights the importance of using a version control system as an integral part of one&#8217;s creative workflow. Part 1 gives a good introduction to the series which [...]]]></description> <content:encoded><![CDATA[<p><em>Managing Creative Assets is a multi-part series on how you can manage your creative works such as a novel or even blog post without impairing your creativity. It highlights the importance of using a version control system as an integral part of one&#8217;s creative workflow. <a
href="/blog/archives/2009/12/13/managing-creative-assets-1/">Part 1</a> gives a good introduction to the series which is aimed at technology novices</em></p><h2>Who started the Fire?</h2><p>In April 7, 2005, Linus Torvalds wanted to use a version control system to replace the proprietary BitKeeper system for developing Linux Kernel. He absolutely hated CVS (the version control system in vogue then) with a passion. So, he did what he did best, he wrote his own. This resulted in the release of a version control system called git.</p><p>The development of git led to a sudden interest in distributed version control systems (DVCS). Though it was not the first of its kind (earlier open-source DVCS existed like Arch and Monotone), it was the first mainstream DVCS.</p><p>Today, one of the first choices you need to make while selecting a version control system is whether it is centralised or distributed. Let&#8217;s understand this from own unique stand-point i.e. for managing creative assets.</p><h3>Why I do not advice VSS, Subversion or a Central Version Control</h3><p>What is a Centralised Version Control (CVC)? The odds are that most of the version control systems that you might have heard of are Centralised for e.g. VSS (Microsoft Visual Sourcesafe), CVS, RCS, Clearcase or Subversion.</p><p>If you are planning to use a version control for personal use involving no or minimal collaboration with others, I would strongly recommend not to use a centralised version control system. You can skip to the next section, if you don&#8217;t want a detailed set of reasons on why I recommend non-centralised version control.</p><p>The reasons I would cite here are a mix of usability issues and technical limitations. The usability issues are subjective but I am sure many find them genuinely annoying. I am making an assumption that since this is for personal version control management, so your CVC server would probably be installed locally. The problems are:</p><ol><li><p><strong>Everything is stored inside repositories</strong>: Adding your project to a CVC effectively creates a duplicate layout of your project inside the CVC server. For e.g. if you created your subversion repository within <code>C:\svn</code>, all your projects will be kept inside this folder. You will have to maintain another filesystem inside this server using arcane commands.</p><p>In a distributed version control system, you simply manage the project directory inside your normal filesystem. All the version controlled files will still be inside the project directory. This is quite useful since your project directory can be moved to a different location easily and the version history will be completely intact.</p></li><li><p><strong>Server must be always running</strong>: If you have installed VSS or SVN locally, you must always remember to start the server. This can be configured to run as a service, but you will need admin privileges for this. This is not required in a distributed version control.</p></li><li><p><strong>Offline activity cannot be checked-in</strong>: This is an oft-quoted technical limitation. You need your svn server running to make any check-ins or check-outs, making it considerably less useful whenever you are offline. But this is less of an issue in our case, since I assume the svn server is installed locally.</p></li><li><p><strong>Remembering to checkout immediately after you import or check-in</strong>: Ever had the feeling that your files magically disappeared only to realise that you haven&#8217;t checked out? This is an irritating &#8216;feature&#8217; of CVS. The files appear and disappear as you check in and out. Even worse is that they are sometimes read-only and sometimes writeable. This is confusing and irritating from a usability standpoint.</p><p>Apparently, most people leave their relevant files checked out at all times to avoid this confusion. But that would defeat the purpose. In a distributed version control, the files are always present where you expect them to be. This leads to less confusion and a world of improvement in terms of usability.</p></li><li><p><strong>Weird layout</strong>: Ever seen a funny looking directory structure with <code>truck</code>, <code>branches</code> and <code>tags</code> directories? Then you might be looking at a project under SVN. Ever wondered which directory actually contains your code? The right answer is <code>trunk</code>. I am not sure, if this is  the most intuitive structure possible.</p></li><li><p><strong>Distributed Version Control is a superset</strong>: This should have been my first point, almost <a
href="http://bazaar-vcs.org/Workflows" title="Different kinds of workflow on Bazaar">every centralised workflow can be now supported</a> by Distributed Version Control. You can still upload (or &#8220;push&#8221;) stuff from your branches to the project&#8217;s central server.</p></li></ol><p>If you are still not convinced and still prefer centralised version control, check out <a
href="http://www.codinghorror.com/blog/archives/001093.html" title="Setting up svn and TortoiseSVN in Windows">the easiest way to setup subversion in Windows</a> written by Jeff Croft.</p><h3>Distributed Sounds Complex</h3><p>It is a common misconception that Distributed Version Control systems are difficult to use and hard to understand. In many ways, the concepts are simpler than traditional version control systems from a beginner&#8217;s perspective.</p><p>Assume that the files (say documents or images) related to your project are kept under a particular directory. This is called the Project folder. Traditionally, your project folder will be stored in a central server. Hence the name <em>centralised</em> version control.</p><p>Whenever you would need to use a particular file within this folder, you will need to check-out and once you have reached some logical point (say after adding a few paragraphs in your essay or sketching the torso of a toon) you would check-in.</p><p>These check-ins are like check-points. More check-points you add, the more finer undo history you will get. Fewer check-points will mean that there will be a lot of differences from one check-in to another making it less useful.</p><p>As you might have guessed, every time you need to check-in or check-out you will need to connect to the server. Hence, practically, you will need the server (installed on your machine or elsewhere) up and running at all times.</p><p>If someone else would like to work with you on the same project, they will need to connect to your server. If they would like to work on the same files that you are working on (a rare case), they would need to create a branch and work on the branch.</p><p>This collaborative scenario is slightly different when you are working with a DVCS.</p><h4>What About Distributed?</h4><p>In a distributed version control system, your friend would rather clone your entire project than branch it. After creating a clone, his copy will be identical to your repository in every way. It will have the entire version history intact.</p><p>He no longer needs to be connect to your repository, he can work independently. In fact, there is really no need for a server in DVCS. The repository is actually created within the project folder.</p><p>For instance, let&#8217;s take the initial scenario. You would like to add your project folder to version control. In a DVCS, the project folder is slightly modified to add some additional information (meta-data) which is typically hidden from the user. Hence, your project folder remains mostly intact and it doesn&#8217;t have to move into a server.</p><p>In short, the defining feature of DVCS is that there can be more than one &#8220;central&#8221; repository for the same project. In case, your repository gets nuked, the cloned repository with your friend is always available as a perfect clone. To quote:</p><blockquote><p>&#8220;Only wimps use tape backup: <em>real</em> men just upload their important stuff on ftp, and let the rest of the world mirror it ;)&#8221; &#8212; Linus Torvalds (1996-07-20)</p></blockquote><h3>Types of DVCS</h3><p>These are the popular open-source DVCS available:</p><ul><li><strong>Git</strong> &#8211; Very fast DVCS by Linus which runs on UNIX but has a weak port to Windows.</li><li><strong>Mercurial</strong> &#8211; Fast cross-platform DVCS by Matt Mackall of Selenic. Partly written in Python</li><li><strong>Bazaar</strong> &#8211; User friendly cross-platform DVCS by Canonical (of Ubuntu fame). Written in pure Python</li></ul><p>Selecting a DVCS, like most things, is a personal choice. So, you might want to read a more <a
href="http://www.infoq.com/articles/dvcs-guide" title="InfoQ's guide to Distributed Version Control Systems">detailed comparison</a> before making a choice. I would be explaining Mercurial in my <a
href="http://www.arunrocks.com/blog/archives/2009/12/15/managing-creative-assets-3-tortoisehg-tutorial/">next article</a> because it has a nice selection of all the desired features.</p><p><em>Do comment if you found DVCS more interesting or otherwise&#8230;</em></p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2009/12/13/managing-creative-assets-2/feed/</wfw:commentRss> <slash:comments>7</slash:comments> </item> <item><title>Managing Creative Assets &#8211; 1</title><link>http://www.arunrocks.com/blog/archives/2009/12/13/managing-creative-assets-1/</link> <comments>http://www.arunrocks.com/blog/archives/2009/12/13/managing-creative-assets-1/#comments</comments> <pubDate>Sun, 13 Dec 2009 14:24:02 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[Python]]></category> <category><![CDATA[Technical]]></category> <category><![CDATA[backup]]></category> <category><![CDATA[creativity]]></category> <category><![CDATA[distributed version control]]></category> <category><![CDATA[lifehacks]]></category> <category><![CDATA[productivity]]></category> <category><![CDATA[version control]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=159</guid> <description><![CDATA[Managing Creative Assets is a multi-part series on how you can manage your creative works such as a novel or even blog post without impairing your creativity. It highlights the importance of using a version control system as an integral part of one's creative workflow]]></description> <content:encoded><![CDATA[<p><em>Managing Creative Assets is a multi-part series on how you can manage your creative works such as a novel or even blog post without impairing your creativity. It highlights the importance of using a version control system as an integral part of one&#8217;s creative workflow</em></p><h2>Why Do We Need A Version Control system?</h2><p>Let me start off by saying that this is article is not for the techies. Despite what the title might tell you, this is an article about how to make computers help your creative process. How does a creative process work?</p><p>Most creative people follow the following simplified process <a
href="http://en.wikipedia.org/wiki/Creativity#Graham_Wallas" title="Wallas model">attributed to Graham Wallas</a> while thinking creatively:</p><ul><li><strong>Preparation</strong> (preparatory work on a problem that focuses the individual&#8217;s mind on the problem and explores the problem&#8217;s dimensions),</li><li><strong>Incubation</strong> (where the problem is internalized into the unconscious mind and nothing appears externally to be happening),</li><li><strong>Intimation</strong> (the creative person gets a &#8216;feeling&#8217; that a solution is on its way),</li><li><strong>Illumination</strong> or insight (where the creative idea bursts forth from its preconscious processing into conscious awareness); and</li><li><strong>Verification</strong> (where the idea is consciously verified, elaborated, and then applied).</li></ul><p>Obviously, this is an iterative process. Most writers would have a pile of crumpled paper sheets overflowing their waste baskets. Be prepared to reject a lot of ideas (even good ones) when you are involved in some creative process. Sometimes, against your earlier good judgement you would like to go back and retrieve an idea that you had discarded. You may have to rummage your waste basket for that page and if you are lucky, you might find it among the pile.</p><p>These days most of the creative works; be it a novel, a movie or even a comic is prepared on a computer. However, the process of throwing away drafts into the waste basket and later digging them out, is still the way we humans work. The various ways people achieve this in practise is:</p><ol><li><p><strong>Saving multiple version</strong>: This results in a whole mess of files grouped by their prefixes. Some prefer to suffix them with version numbers like <em>file-v0.5.doc</em>, <em>file-v1.0.doc</em> etc, while others prefer to use descriptive suffixes like <em>file-draft.doc</em>, <em>file-sentforreview.doc</em> etc. As anyone who might have used this system would have experienced, this quickly becomes unwieldy. For example what is the difference between <em>file-v0.5.doc</em> and <em>file-v1.0.doc</em>? How can I revert to the earliest version while correcting many of the typos I found in the latest version?</p></li><li><p><strong>Using Undo and Redo</strong>: This is a very simple system to understand and hence quite popular among artists. If something doesn&#8217;t feel right, keep pressing the Undo button till you are satisfied and then start over. There are many problems with this approach. Firstly, the timeline is linear. You cannot try two different approaches at the same time. Secondly, the Undo history is available only for a single session. Close the application and the undo history is forgotten.</p></li><li><p><strong>Use a version control system</strong>: This approach relies on an version control system that remembers every version you had ever saved (rather checked-in). This system is the focus of this article.</p></li></ol><p>To extend the analogy further, a version control system can give you a bottomless waste basket with the ability to show you what changes you made from one version to another. Version control systems are powerful enough to allow you to branch out into various versions simultaneously, which is often useful when you are collaborating with others.</p><p>In fact, the addition of a version control system makes a profound change to your creative process. You are no longer afraid to make mistakes. You can play around with your creations without the fear of what you had created so far. Most people are afraid to start from scratch, even though, it is often documented that subsequent creations become more refined and hence elegant due to the better understanding of the &#8216;problem&#8217; mentioned in the <strong>Preparation</strong> phase above. But be mindful of drifting in the opposite direction too, as in the case of the <a
href="http://en.wikipedia.org/wiki/Second-system_effect">Second System effect</a>.</p><p>In the <a
href="http://www.arunrocks.com/blog/archives/2009/12/13/managing-creative-assets-2/">next part</a>, we will be introduced to two kinds of version control systems &#8211; Centralized and Distributed; and which one is suited for certain scenarios.</p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2009/12/13/managing-creative-assets-1/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>PyCon India Talk 2009: Game Programming in Pyglet</title><link>http://www.arunrocks.com/blog/archives/2009/09/27/pycon-india-talk-2009-game-programming-in-pyglet/</link> <comments>http://www.arunrocks.com/blog/archives/2009/09/27/pycon-india-talk-2009-game-programming-in-pyglet/#comments</comments> <pubDate>Sun, 27 Sep 2009 13:07:59 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[Gaming]]></category> <category><![CDATA[General]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[featured]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=154</guid> <description><![CDATA[Thanks to everyone who enjoyed and commented on my talk today titled &#8216;Accelerate Your Game Development with Pyglet&#8217;. I am happy to see so much enthusiasm within the python community for game development. This was my first lightning talk (and the first one for the day as well) and though, I slightly overshot the timelimit, [...]]]></description> <content:encoded><![CDATA[<p>Thanks to everyone who enjoyed and commented on my talk today titled &#8216;Accelerate Your Game Development with Pyglet&#8217;. I am happy to see so much enthusiasm within the python community for game development. This was my first lightning talk (and the first one for the day as well) and though, I slightly overshot the timelimit, it was a great experience.</p><p>The talk was about creating a simple casual game using Pyglet called &#8216;FruitCatch&#8217;. The source code is really small and very readable. I also compared Pyglet with Pygame and why I prefer Pyglet (in certain situations:)). The demo showed the working game in the end.</p><p>You can download the <a
href="http://www.arunrocks.com/downloads/pyconindia2009/fruitcatch-0.1.tar.gz">game source code</a> and <a
href="http://www.arunrocks.com/downloads/pyconindia2009/Accelerate%20Your%20GameDevelopment%20with%20Pyglet.pdf">presentation slides (PDF)</a> here. I&#8217;ve shared this with the organisers as well, so it will be put up at the http://in.pycon.org/ site as well</p><p>UPDATE: The video of the <a
href="http://blip.tv/file/2690880">lightning talk</a> is now available and has been added below:</p><p><span
id="more-154"></span></p><p><embed
src="http://blip.tv/play/AYGlulYC" type="application/x-shockwave-flash" width="480" height="390" allowscriptaccess="always" allowfullscreen="true"></embed></p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2009/09/27/pycon-india-talk-2009-game-programming-in-pyglet/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Decoding Google&#8217;s First Tweet in Python</title><link>http://www.arunrocks.com/blog/archives/2009/02/28/decoding-googles-first-tweet-in-python/</link> <comments>http://www.arunrocks.com/blog/archives/2009/02/28/decoding-googles-first-tweet-in-python/#comments</comments> <pubDate>Sat, 28 Feb 2009 09:10:29 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Technical]]></category> <category><![CDATA[featured]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=140</guid> <description><![CDATA[Most of you must have read the news that Google finally jumped into the Twitter Bandwagon. In their trademark style, they have chosen to announce this in a cryptic way. Their first tweet was essentially this:I&#8217;m 01100110 01100101 01100101 01101100 01101001 01101110 01100111 00100000 01101100 01110101 01100011 01101011 01111001 00001010I will explain in [...]]]></description> <content:encoded><![CDATA[<p>Most of you must have read the news that <a
href="http://thenextweb.com/2009/02/26/googles-tweet-official-twitter-account/" title="The news about google's tweet">Google finally jumped into the Twitter Bandwagon</a>. In their trademark style, they have chosen to announce this in a cryptic way. Their <a
href="http://twitter.com/google/status/1251523388" title="Link to Google Twitter account">first tweet</a> was essentially this:</p><blockquote><p>I&#8217;m 01100110 01100101 01100101 01101100 01101001 01101110 01100111 00100000 01101100 01110101 01100011 01101011 01111001 00001010</p></blockquote><p>I will explain in this post how to crack this simple code with the help of some Python one-liners (Google&#8217;s favourite language). If you are a Google aspirant (who isn&#8217;t? ;) ), this might help you clear the interview. So pay attention.</p><p>To most people it is immediately obvious that it is a text encoded in binary. Since each binary word is 8 characters long, it is most probably written in the extended 8-bit ASCII code. In fact, it is and you can read this with a simple <a
href="http://www.cdrummond.qc.ca/cegep/informat/Professeurs/Alain/files/ascii.htm" title="ASCII chart">ASCII chart</a>.</p><p>But they have made it slightly difficult for you by writing in binary. Since most charts would provide you a lookup from decimal or hexadecimal numbers to ASCII representations only. So how do you convert from binary to decimal? It&#8217;s quite simple:</p><pre><code>decimal = lambda s: sum(int(j) * pow(2,i) for i,j in enumerate(reversed(s)))
</code></pre><p>This line defines a function <code>decimal</code> which works in a manner similar to how we would manually convert binary numbers into decimal. Each position is multiplied by increasing powers of two from the right. Then, these numbers are added together. for e.g. &#8216;1010&#8242; will be 1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 = 10.</p><p>Next, we split the binary part of the tweet string and apply the <code>decimal</code> function on each part</p><pre><code>tweet = "01100110 01100101 01100101 01101100 01101001 01101110 01100111 00100000 01101100 01110101 01100011 01101011 01111001 00001010"
print ''.join(chr(decimal(s)) for s in tweet.split())
</code></pre><p>The result is something that you might have already guessed seeing the first 2 words:</p><blockquote><p>&#8220;I&#8217;m feeling lucky\n&#8221;</p></blockquote><p>Hope you learnt some interesting python constructs. If there are other ways of decoding this in Python, please comment below.</p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2009/02/28/decoding-googles-first-tweet-in-python/feed/</wfw:commentRss> <slash:comments>13</slash:comments> </item> <item><title>Workaround to Easy Install PIL on Windows</title><link>http://www.arunrocks.com/blog/archives/2008/08/30/workaround-to-easy-install-pil-on-windows/</link> <comments>http://www.arunrocks.com/blog/archives/2008/08/30/workaround-to-easy-install-pil-on-windows/#comments</comments> <pubDate>Sat, 30 Aug 2008 17:43:54 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[Python]]></category> <category><![CDATA[django]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=133</guid> <description><![CDATA[This is a quick workaround for people who are using easy_install to install Python Imaging Library on Windows. Many people faced issues while doing this. I found a simple workaround for this.You must have tried the followingC:&#62; easy_install PIL
Searching for PIL
...
Finished processing dependencies for PIL
C:&#62; python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 [...]]]></description> <content:encoded><![CDATA[<p>This is a quick workaround for people who are using <a
href="http://peak.telecommunity.com/DevCenter/EasyInstall">easy_install</a> to install Python Imaging Library on Windows. Many people faced issues while doing this. I found a simple workaround for this.</p><p>You must have tried the following</p><pre><code>C:&gt; easy_install PIL
Searching for PIL
...
Finished processing dependencies for PIL
C:&gt; python
Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import Image
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ImportError: No module named Image
</code></pre><p>Now you will need to go to your site-packages directory (typically at C:\Python\Lib\site-packages) and change one line that starts with <code>./PIL-1.1.6-py2.5-win32.egg</code> to simply <code>./PIL</code> and change the sub-directory named similarly to <code>PIL</code></p><p>Now your imports should work :smile:</p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2008/08/30/workaround-to-easy-install-pil-on-windows/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Learning Pylons Through Tutorials</title><link>http://www.arunrocks.com/blog/archives/2008/06/26/learning-pylons-through-tutorials/</link> <comments>http://www.arunrocks.com/blog/archives/2008/06/26/learning-pylons-through-tutorials/#comments</comments> <pubDate>Thu, 26 Jun 2008 17:18:50 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[Python]]></category> <category><![CDATA[WordPress]]></category> <category><![CDATA[featured]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/?p=128</guid> <description><![CDATA[Web Development is now getting dominated by frameworks. After the initial hype of Rails, Python based frameworks are getting more popularity especially after the release of Google App Engine. Nobody seems to be interested in building websites using PHP, even if they are highly experienced in PHP. This could mean two things:There will be more [...]]]></description> <content:encoded><![CDATA[<p>Web Development is now getting dominated by frameworks. After the initial hype of Rails, Python based frameworks are getting more popularity especially after the release of Google App Engine. Nobody seems to be interested in building websites using PHP, even if they are highly experienced in PHP. This could mean two things:</p><ol><li>There will be more demand in Indian IT companies for PHP skills similar to Perl scripting skills these days  :mrgreen:</li><li>There will be soon be a viable competitor to the immensely popular Wordpress blogging platform from the Python/Ruby world</li></ol><p>I have nothing against Wordpress. In fact, it is one of the easiest tools to deploy. But with tools like cPanel, deployment could be no longer a deciding factor for blogging platforms.</p><p>Coming back to the topic of Python Web Frameworks, there is a multitude of options. I have used some of these framworks and found them to be &#8216;pythonic&#8217; in different ways:</p><ul><li><strong>Django</strong> Well documented. Tightly Coupled and &#8216;Batteries Included&#8217;</li><li><strong>Pylons</strong> Extremely flexibility to Plug and Play components. &#8216;Fun to Hack&#8217;</li><li><strong>web.py</strong> Great for beginners. Entire framework &#8216;fits in your head&#8217;</li></ul><p>Pylons looks most appealing to me. So far it looks like the underdog largely overshadowed by Django&#8217;s presence lacking the <a
href="http://www.jacobian.org/writing/2006/jan/27/why-django/">marketing</a> or <a
href="http://www.djangoproject.com/weblog/2006/aug/07/guidointerview/">love</a> it deserves. A few weeks back I would have complained about the lack of Pylons tutorials on the web. A google search brought up several links pointing to the wiki tutorial. As many have pointed out, the wiki tutorial is too long and complicated for beginners. It would easily put off a beginner. It took me quite some time to realise that the best place to start learning Pylons is the <a
href="http://bel-epa.com/pylonsdocs/">Pylons Documentation</a> itself and the best Pylons tutorial is the <a
href="http://bel-epa.com/pylonsdocs/tutorials/flickr_search_tutorial.html">Flickr Search</a>. Going through the documentation is almost feels like reading a book with every concept explained in detail.</p><p>Pylons currently seems to the having an edge over Django for enterprise application due to its well tested interface with SQLAlchemy. Django seems to be having a fairly recent <a
href="http://code.google.com/p/django-sqlalchemy/">branch</a> for SQLAlchemy integration. Pylons typically uses Python eggs and VirtualEnv for deployment which is might seem a little complex to beginners, but once you try it once it is actually quite convenient. Most of the installation can be done from the command-line and it will be nearly an independent sandbox for Pylons development. However it might take sometime to understand other aspects of using python eggs such as uninstallation and creation of new eggs. I haven&#8217;t had much success with making a portable version of my Pylons installation on Windows though :(</p><p>My favourite templating engine is Genshi because it is very designer friendly(which also means it works with the tools a Designer has, not just that it is easy for a Designer to learn). I can easily do all HTML designing directly on my Genshi templates because they are valid HTML or XML documents. Almost all of Genshi&#8217;s logic can be hidden away as attributes which is a great idea. In fact, the templates are even valid XML which makes the creation of valid HTML pages a much more natural experience. Almost all XML tools and even HTML tools like Tidy will work flawlessly on Genshi templates due to these reasons.</p><p>Once you get used to Genshi&#8217;s templates, you might even use them for static web site designing. I am sure it will save you a lot of time whenever those last minute &#8217;sidebar redesigns for every page&#8217; pops up.</p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2008/06/26/learning-pylons-through-tutorials/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>V-day Special</title><link>http://www.arunrocks.com/blog/archives/2008/02/17/v-day-special/</link> <comments>http://www.arunrocks.com/blog/archives/2008/02/17/v-day-special/#comments</comments> <pubDate>Sun, 17 Feb 2008 07:35:12 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[Python]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/archives/2008/02/17/v-day-special/</guid> <description><![CDATA[Here is a belated valentine&#8217;s day greeting from both of us.For all my readers, I recommend visiting the microsite I had made earlier. Now it is written entirely in web.py. Thanks to my new website host, A Small Orange, I can create and host python based applications. I agree that there is no need to [...]]]></description> <content:encoded><![CDATA[<p>Here is a belated valentine&#8217;s day greeting from both of us.</p><p><a
href="http://www.flickr.com/photos/arun_ravindran/2270952086/" title="Valentines Day by ArunClickClick, on Flickr"><img
src="http://farm3.static.flickr.com/2301/2270952086_c28e0af5be.jpg" width="500" height="333" alt="This photo is a macro taken by Arun" /></a></p><p>For all my readers, I recommend visiting the <a
href="http://www.arunrocks.com/wedding/">microsite</a> I had made earlier. Now it is written entirely in <a
href="http://webpy.org/">web.py</a>. Thanks to my new website host, <a
href="http://refer.asmallorange.com/15934">A Small Orange</a>, I can create and host python based applications. I agree that there is no need to design a new blog in python again, but it is a great way to learn a new (anti-) framework. Web.py applications are really small and easy to understand and <a
href="http://www.paulgraham.com/head.html">therefore, has less bugs</a>.</p><p>Guess what, <a
href="http://www.codinghorror.com/blog/archives/000624.html">Jeff Atwood owns an ASUS</a> laptop model (W3J) that is similar to <a
href="http://www.flickr.com/photos/arun_ravindran/2215166942/">mine</a> (F3J). He is full of praise about the decision. Considering I took at least 2 months (the standard duration for yours truly to zero into an buying decision) to arrive at the decision, it seems well worth now :).</p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2008/02/17/v-day-special/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Work Faster in Windows With Launchy and a few Python Scripts</title><link>http://www.arunrocks.com/blog/archives/2007/12/04/work-faster-in-windows-with-launchy-and-a-few-python-scripts/</link> <comments>http://www.arunrocks.com/blog/archives/2007/12/04/work-faster-in-windows-with-launchy-and-a-few-python-scripts/#comments</comments> <pubDate>Tue, 04 Dec 2007 05:39:23 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[launchy]]></category> <category><![CDATA[productivity]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/archives/2007/12/04/work-faster-in-windows-with-launchy-and-a-few-python-scripts/</guid> <description><![CDATA[Launchy is a great productivity tool and a cool way to impress your friends. You can launch any application by pressing a hotkey (say Alt+Space) and the first few letters of the application for eg: typing &#8216;gi&#8217; will display the GIMP icon and pressing Enter will launch GIMP. You can download Launchy from its website [...]]]></description> <content:encoded><![CDATA[<p><img
src="http://farm1.static.flickr.com/133/414218839_e89ef7791d_m_d.jpg" width="240" height="160" alt="Eating the Mouse" class="alignright" /></p><p><a
href="http://www.launchy.net/">Launchy</a> is a great productivity tool and a cool way to impress your friends. You can launch any application by pressing a hotkey (say Alt+Space) and the first few letters of the application for eg: typing &#8216;gi&#8217; will display the GIMP icon and pressing Enter will launch GIMP. You can <a
href="http://downloads.sourceforge.net/launchy/LaunchySetup125.exe?modtime=1177060449&amp;big_mirror=0">download Launchy</a> from its website and its <a
href="http://www.launchy.net/LaunchySetup199_1.exe">beta</a> is pretty stable (and gorgeous) on my XP laptop too.</p><p>Using Launchy gets pretty addictive and soon you will hate using the Start menu or even Explorer on Windows for opening applications or files. So I took the red pill and started automating the following things with a little help from Python:</p><ul><li>Launching Intranet applications inside Internet Explorer (even if Firefox is your default browser)</li><li>Bringing minimized or overlapped windows to the foreground</li></ul><h3>Some Necessary Evil</h3><p>Don&#8217;t get me wrong, I hate IE as much as you do. But the fact of life is that many web apps out there &#8220;Work best when viewed in IE&#8221; (TM). Even if you have launchy plugins to launch the web app, if your default browser is Firefox, it might show incorrectly. Here is the solution:</p><ol><li>Install <a
href="http://www.python.org/download/releases/">Python</a> and <a
href="http://sourceforge.net/project/platformdownload.php?group_id=78018">Pywin32</a></li><li>Copy the following script to the Utilities directory (it will be in the path where you installed Launchy) and save it with a <strong>.pyw</strong> extension not a <strong>.py</strong> extension</li></ol><p>Python:</p><pre><code>from win32com.client import Dispatch
ie = Dispatch("InternetExplorer.Application")
ie.Visible = True
ie.Navigate(r"http://intranetapp/home")
</code></pre><p>In the above code replace the URL <strong>http://intranetapp/home</strong> with the URL of your choice.</p><p>Finally, open Launchy, right-click and say &#8216;Rebuild Index&#8217;.</p><h3>No more Alt-Tabbing around</h3><p>If you are like me, you&#8217;ll have a lot of windows open at the same time. I have tried increasing the task bar height and grouping similar windows feature in XP to manage them. But I always wish I could invoke commonly used open applications like my chat window in just a few keystrokes. Launchy doesn&#8217;t index open programs by default, but with some python magic I can show you how to bring some commonly used windows to the foreground:</p><ol><li>As before, install <a
href="http://www.python.org/download/releases/">Python</a> and <a
href="http://sourceforge.net/project/platformdownload.php?group_id=78018">Pywin32</a></li><li>Copy the following script to the Utilities directory (it will be in the path where you installed Launchy) and save it with a <strong>.pyw</strong> extension not a <strong>.py</strong> extension</li></ol><p>Python:</p><pre><code>import sys
from win32gui import GetWindowText, EnumWindows, ShowWindow, SetForegroundWindow
from win32con import SW_RESTORE, SW_SHOW

TITLE_MATCH = "Microsoft Excel - Expenses.xls"

def listWindowsHandles():
    res = []
    def callback(hwnd, arg):
        res.append(hwnd)
    EnumWindows(callback, 0)
    return res

def listWindowsNamesAndHnd():
    return [(hwnd, GetWindowText(hwnd)) for hwnd in listWindowsHandles()]

def unminimizeWindow(a_hwnd):
    ShowWindow(a_hwnd, SW_RESTORE)
    SetForegroundWindow(a_hwnd)

def finder1():
    for hwnd, title in listWindowsNamesAndHnd():
        if TITLE_MATCH in title:
            unminimizeWindow(hwnd)

finder1()
</code></pre><p>In the above code change the string <strong>Microsoft Excel &#8211; Expenses.xls</strong> with the title the window you would like to summon.</p><p>Finally, open Launchy, right-click and say &#8216;Rebuild Index&#8217;.</p><p>This works even if the window was minimized.</p><p>I hope, finally you can throw your mouse away. Ah&#8230; What a bliss!</p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2007/12/04/work-faster-in-windows-with-launchy-and-a-few-python-scripts/feed/</wfw:commentRss> <slash:comments>14</slash:comments> </item> <item><title>Making Python Scripts Show Windows-friendly Errors/Stacktrace</title><link>http://www.arunrocks.com/blog/archives/2007/06/20/making-python-scripts-show-windows-friendly-errorsstacktrace/</link> <comments>http://www.arunrocks.com/blog/archives/2007/06/20/making-python-scripts-show-windows-friendly-errorsstacktrace/#comments</comments> <pubDate>Tue, 19 Jun 2007 19:33:41 +0000</pubDate> <dc:creator>Arun Bhai</dc:creator> <category><![CDATA[General]]></category> <category><![CDATA[Python]]></category> <category><![CDATA[Technical]]></category> <category><![CDATA[py2exe]]></category><guid
isPermaLink="false">http://www.arunrocks.com/blog/archives/2007/06/20/making-python-scripts-show-windows-friendly-errorsstacktrace/</guid> <description><![CDATA[Most of us love to distribute our python programs to others once you have finished coding a neat little script. For Windows users we package it using Py2exe or cx_freeze. However, many of the end-users will not be happy with a black command window popping up, say, when an error is thrown.Of course the alternative [...]]]></description> <content:encoded><![CDATA[<p>Most of us love to distribute our python programs to others once you have finished coding a neat little script. For Windows users we package it using <a
href="http://www.py2exe.org/index.cgi/">Py2exe</a> or <a
href="http://www.cxtools.net/default.aspx?nav=cxfrlb">cx_freeze</a>. However, many of the end-users will not be happy with a black command window popping up, say, when an error is thrown.</p><p>Of course the alternative is to write a full blown GUI application using <a
href="http://www.wxpython.org/">WXPython</a> or <a
href="http://pyfltk.sourceforge.net/">PyFLTK</a>. Even the latter, though quite lightweight, adds several megabytes to the distribution, when all you need is a simple message-box indicating an error or showing some informational text. Clearly, its an overkill for your throwaway python scripts.</p><p>This is the kind of problem I typically face and I have found a good solution. The answer is <a
href="http://python.net/crew/theller/ctypes/">ctypes library</a> which comes as a part of the standard distribution from Python 2.5 onwards. It simply calls the messagebox function from user32.dll (which is always present in a windows installation). With the main problem solved, what remained was to obtain the error text and stack trace.</p><p>Let&#8217;s see how the code looks like:</p><pre><code># Importing all the works for a native Win32 Message Box
from ctypes import c_int, WINFUNCTYPE, windll
from ctypes.wintypes import HWND, LPCSTR, UINT
prototype = WINFUNCTYPE(c_int, HWND, LPCSTR, LPCSTR, UINT)
paramflags = (1, "hwnd", 0), (1, "text", "Hi"), (1, "caption", None), (1, "flags", 0)
MessageBox = prototype(("MessageBoxA", windll.user32), paramflags)

# For printing the stack
import sys
import traceback
from time import sleep

def show_popup(text):
    print text
    MessageBox(text=text, caption="Sample App Says...")

def mainloop():
    raise "Uff!"

if __name__ == '__main__':
    try:
        mainloop()
    except:
        type, value, sys.last_traceback = sys.exc_info()
        lines = traceback.format_exception(type, value,sys.last_traceback)
        show_popup("Aiyooooo..... there has been an error!\n" +
            "Exception in user code:\n" +
            "".join(lines) +
            "===== Please mail a screenshot to arunvr@gmail.com ===="
            )
    finally:
        sleep(1) # show the console output for a second so that users can read it
</code></pre><p><em>EDIT:</em> This is how it looks like in PyMail, one of my scripts-that-grew-into-an-app ;)</p><p><img
src="http://i17.tinypic.com/61oz9d3.jpg" alt="Screenshot of a Python Stacktrace in a Messagebox" /></p> ]]></content:encoded> <wfw:commentRss>http://www.arunrocks.com/blog/archives/2007/06/20/making-python-scripts-show-windows-friendly-errorsstacktrace/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss><!--
This site's performance optimized by W3 Total Cache. Dramatically improve the speed and reliability of your blog!

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (user agent is rejected)
Database Caching 10/13 queries in 0.034 seconds using disk

Served from: zoe.asmallorange.com @ 2010-09-03 03:27:46 -->