<?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>Josh on the Web &#187; Python</title>
	<atom:link href="http://joshduck.com/blog/category/programming/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://joshduck.com/blog</link>
	<description>It's a blog about the web, by Josh. Geddit?</description>
	<lastBuildDate>Thu, 29 Jul 2010 11:58:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting started with Python and Django in 23 frustrating steps</title>
		<link>http://joshduck.com/blog/2010/02/27/getting-started-with-python-and-django-in-23-frustrating-steps/</link>
		<comments>http://joshduck.com/blog/2010/02/27/getting-started-with-python-and-django-in-23-frustrating-steps/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 00:52:19 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=70</guid>
		<description><![CDATA[Should I or shouldn&#8217;t I? Should I ditch my well-worn PHP and the frameworks I know so well to go with this new-fangled (silent-d)Jango thingy I hear the cool kids talking about? It&#8217;s a big decision, as this project is going to be the big one (you know, the one that will change the world [...]]]></description>
			<content:encoded><![CDATA[<p>Should I or shouldn&#8217;t I? Should I ditch my well-worn PHP and the frameworks I know so well to go with this new-fangled (silent-d)Jango thingy I hear the cool kids talking about? It&#8217;s a big decision, as this project is going to be the big one (you know, the one that will change the world and all that).</p>
<p>I&#8217;ve worked with PHP for over six years. It has it&#8217;s warts (and how) but it&#8217;s very much a known quantity at this point. On the other hand, when I have used Python it&#8217;s been a much more pleasant experience. The fact that this is a personal project makes the decision easier: let&#8217;s ditch old mate LAMPhp go with LAMPy. It&#8217;s almost dinner time and the latter sounds like it&#8217;d go well with an ale anyway. So, starting with a brand new dev box, where do I begin?<span id="more-70"></span></p>
<ol>
<li> Go to the Python website<br />
Of course. So P-Y-T-H-O-N-DOT-C-O-M, Enter. Er. Not exactly what I was looking for (and not what you should be looking for if your boss is anywhere nearby). Let&#8217;s try that again. Google to the rescue. Apparently I want DOT-O-R-G.</li>
<li>Choose a binary<br />
OK, so I know from listening to the fine folks over at proggit that there is an attempted Python 2 to 3 migration happening at the moment. There is no point investing my time in an outdated version so I&#8217;ll grab the 3.1 download.</li>
<li>Install Python<br />
C:\Python31. Hmm. Python, I know that you have a high opinion of yourself but you&#8217;re not that important to me just yet. You can go play nice in Program Files with all the other programs.</li>
<li>Download Django<br />
Now it&#8217;s Django&#8217;s turn. There&#8217;s the installer. Python 2.3 or higher, you say. Well logically 3 is higher than 2.3, but doesn&#8217;t Python 3 have problems with it&#8217;s younger siblings? Let me just check with a hit of the old Google&#8230; yup, as I thought. We&#8217;ll have to go with the older version.</li>
<li>Install Python<br />
Python 2! It&#8217;s not quite as fun when you know your downloading an outdated model.</li>
<li>Install Django<br />
So I&#8217;ve downloaded Django and now I&#8217;ve got an archive full of Python files. How do I install them?</li>
<li>Read the instructions<br />
But I hate doing that!</li>
<li>Run setup.py<br />
So I have to run the included setup.py file? If I launch the file from Explorer it prints something then immediately exits. Let&#8217;s open up the command line, cd to the correct location and run it again.</li>
<li>Run setup.py install<br />
OK, I forgot the &#8220;install&#8221; argument. You could have just told my with a popup; slept after printing the error; or heaven forbid, prompted me for an action. At least it&#8217;s working&#8230; can&#8217;t copy!? What do you mean?</li>
<li>Run setup.py install as an administrator<br />
Ah, of course you can&#8217;t copy to my Program Files. Time to launch a new command window as an administrator, renavigate to the archive and install.</li>
<li>Start a Django project<br />
You have to love the feeling you get when something works first time.</li>
<li>Define database settings<br />
Edit my project settings, easy enough.</li>
<li>Run the Django server<br />
Error: MySQLdb blah blah blah. What does that mean? Wait, Python does come with MySQL extensions right? Right!? It&#8217;s not like it&#8217;s one of the the most popular RDBMS on the planet.</li>
<li>Google it<br />
Hi Google. Yes, I&#8217;m back again. Do you know which module I want? Wow. That&#8217;s a lot of results. Are these the same? I&#8217;m looking for MySQLdb, not MySQL-Python. Ah, I see. They&#8217;re the same.</li>
<li>Go to SourceForge<br />
Why is this on SourceForge? Who&#8217;s distributing it? Ah, screw it. There is a big download button. I&#8217;m sure it&#8217;s safe. I mean, the button is green. Green is good. Red is bad. I didn&#8217;t get this far without learning a few things.</li>
<li>These are not the files you are looking for<br />
What do you mean they are the C files? I want a binary installer dammit. I&#8217;m a Windows user. I&#8217;m like a ten year old who still has training wheels on his bike; I don&#8217;t know anything about make files and building binaries.</li>
<li>Get the binary<br />
At least I know where the binaries can be downloaded&#8230; don&#8217;t I? Why do the binaries stop at Python 2.5 on the SourceForge page?</li>
<li>Google it!<br />
Yes, Google, I know I was just here. Just tell me where I can get a binary. Please stop laughing at me.</li>
<li>Download a binary<br />
There are a bunch of unofficial installers. Which one do I choose? bobs-super-awesome-mysql-python-for-2.6.exe looks reputable.</li>
<li>Install MySQLdb<br />
Great, a proper installer this time. It looks a bit &#8220;Windows 95&#8243; but beggers can&#8217;t be choosers. At least with an installer this should be eas&#8230; Hmmm, it&#8217;s frozen.</li>
<li>Think back<br />
Wait, the other installer didn&#8217;t like running in user mode either. Perhaps if I run as root. Ah ha. That&#8217;s done it.</li>
<li>Install Python<br />
How silly of me. It should be obvious that all the C extensions only offer win32 versions. What exactly is the point of having a 64 bit binary then? Let&#8217;s reinstall Python. Joy of joys.</li>
<li>Enjoy<br />
Success at last. Time to revel in my new found smugness as a Pythonista:	&#8220;If the implementation is easy to explain, it may be a good idea.&#8221; You tell &#8216;em Guido.</li>
</ol>
<h2>The Takeaways</h2>
<p>Sarcasm is fun! Fixing things is hard. Despite the tone of what I&#8217;ve said above there are quite a few frustrating  hurdles that face those new to Python and Django.</p>
<ul>
<li>If the .com variant of your website is a port site then you have problems. Developers might know to go to the right site but think of clueless managers.</li>
<li>The Python download page should spell out the benefits and drawbacks to each binary download. Be realistic. It&#8217;s better that the developer gets the version they need first time rather than having to come back later on for seconds, or thirds.</li>
<li>It&#8217;s not the 90&#8217;s any more. It&#8217;s not acceptable to expect to be  installed to C:\ root.</li>
<li>I didn&#8217;t mention it in the article but Python doesn&#8217;t add itself to my PATH. Having to dig around in my computer settings to be able to easily run Python from the command line is a pain.</li>
<li>Win64 has existed for a long time now. Shouldn&#8217;t it be supported by common modules?</li>
<li>Installers should be aware of restrictions introduced by Vista&#8217;s UAC model. This is basic stuff. Hanging on install without displaying errors is not acceptable.</li>
<li>The MySQL module should not be a second-class citizen maintained by some third party. If you want to poach PHP developers then you have to speak their language.</li>
<li>Setup tools could be a lot friendlier by prompting the user for an operation if an argument is not provided.</li>
<li>The Django download page should make it clear that Python 3 is not supported. Dropping backwards compatibility is not actually that common in other languages.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/02/27/getting-started-with-python-and-django-in-23-frustrating-steps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>A First Look at Python</title>
		<link>http://joshduck.com/blog/2008/04/11/a-first-look-at-python/</link>
		<comments>http://joshduck.com/blog/2008/04/11/a-first-look-at-python/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 11:47:08 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/2008/04/11/a-first-look-at-python/</guid>
		<description><![CDATA[So I&#8217;ve been looking at and using Python recently. I thought I&#8217;d share some of my thoughts for those who haven&#8217;t had a chance to play with the language yet. I&#8217;ll try to avoid a preachy OMG-I&#8217;ve-just-discovered-the-best-thing-ever post, or to simply write another Python tutorial. I&#8217;ll look at the good and bad points of the [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been looking at and using Python recently. I thought I&#8217;d share some of my thoughts for those who haven&#8217;t had a chance to play with the language yet. I&#8217;ll try to avoid a preachy OMG-I&#8217;ve-just-discovered-the-best-thing-ever post, or to simply write another Python tutorial. I&#8217;ll look at the good and bad points of the language.I first looked at Python a month or two ago. The guy and girls over at <a href="http://reddit.com/r/programming">programming.reddit.com</a> push it as the language to end all languages, so I decided to grab a copy of the (free!) <a href="http://www.diveintopython.org/">Dive Into Python</a> book. I started putting together a smallish personal project, but with no external pressure it petered out. When a discussion came up at work (a PHP shop) on how to quickly write a reliable server daemon I pushed the idea of Python. It took a little convincing, but the results speak for themselves.</p>
<p><span id="more-13"></span><br />
<h2>Picking an Editor</h2>
<p>I believe that the editor can make a huge difference to how you perceive a new language. What might be a massively frustrating time-sapping bug in one could be pointed out and corrected be another. Python doesn&#8217;t have any kind of officially recommended editor, and it can take a few hours to really get the feel of an IDE. Picking an editor when you don&#8217;t even know a language is even worse.</p>
<p>Dive Into Python suggested using Activestate&#8217;s Pythonwin. Unfortunately this advice seems to be very dated; the IDE was functional, but basic. I appreciated the in-built debugger and auto complete, but the editor itself seemed a little too rudimentary.</p>
<p>I eventually switched over to <a href="http://www.openkomodo.com/">Open Komodo</a>, also by ActiveState. The IDE is very good and does a lot of hand-holding that newbies appreciate, like looking after whitespace issues, providing auto-complete and picking up syntax errors. It does have a few drawbacks: it&#8217;s an editor, not an IDE, so you will need to run and debug your code outside the editor. It&#8217;s missing a few basic features, like a function list, but the problems are fairly minor. It is built on Firefox&#8217;s XUL platform so perhaps we&#8217;ll see more extensions becoming available in the near future.</p>
<h2>Interactive Shell</h2>
<p>The number one tool for making Python easy for beginners would have to be the interactive shell. There have been countless times when I&#8217;ve just jumped over to the shell to test how certain Python functions work. Even copying and playing with examples in the Dive Into Python book helps you better absorb the information. In other languages testing functionality would mean I&#8217;d need to create a new file, add my code, save it to a temporary location and then execute it. The interactive shell actively encourages me to experiment with how object behave, rather than adopting a cargo-cult mentality of just using what has worked in the past.</p>
<h2>The White Space Issue</h2>
<p>Python&#8217;s use of white space seems to be a big issue amongst those that are not familiar with the language. It was one of the objections that I faced when proposing it at work. It tends to put Python in the &#8220;weird language&#8221; basket, which is unfortunate. After using the language I&#8217;d say that the white space issue is largely irrelevant.</p>
<p>It does have some negative effects. It means you&#8217;ll have to watch out for tab/space issues, but a good IDE should do that for you. It also make refactoring a little bit more difficult; I like to sometimes comment out a conditional statements, but that is not possible in Python. Sometimes I also like to indent my code for readability, for example if I&#8217;m printing out HTML I&#8217;ll indent some child elements to indicate that they are related to previous lines. Again, that&#8217;s not possible. The biggest issue I see is that there is nothing stopping you from accidentally breaking the flow of your code. If you took a Python code file and removed the whitespace you can loose meaning:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">list</span>:
<span style="color: #ff7700;font-weight:bold;">if</span> item.<span style="color: black;">available</span>:
item.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
item.<span style="color: black;">check_stock</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It&#8217;s impossible to tell where the statements should be. Are we calling <em>check_stock </em>on every item, or just the available ones? Sure, this is contrived, but I can see something like this happening.</p>
<p>The advantages of the white space convention become very obvious very quickly. Python code is very compact. Not &#8220;what-the-hell-is-this-Perl-code-doing&#8221; compact, but actual readable compact non-ugly code. I&#8217;ve heard some people describe it as &#8220;prose&#8221;. That is going a bit far, but it is very neat and easy to read.</p>
<h2>Lists, Dicts and Tuples</h2>
<p>Python makes working with data sets incredibly easy. It has made me realise how much of my programming is actually just munging sets. Something I&#8217;d envisage as the driving part of a module can be converted from a nest of loops and conditional statements into a single line.</p>
<p>Python list comprehension is the magic that makes this happen. What makes it even better is that the code is just as readable, perhaps even more so, than the verbose multi-line version. Python&#8217;s syntax makes list invocations feel like a natural extension of for loops, meaning it is a great way to get programmers stuck in the imperative mindset on board.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Hmmm</span>
new_list = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> old_list:
    <span style="color: #ff7700;font-weight:bold;">if</span> item <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span> == <span style="color: #ff4500;">0</span>:
    new_list.<span style="color: black;">append</span><span style="color: black;">&#40;</span>item <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#Yay, list comprehensions sort it all out</span>
new_list = <span style="color: black;">&#91;</span>item <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">2</span> <span style="color: #ff7700;font-weight:bold;">for</span> item <span style="color: #ff7700;font-weight:bold;">in</span> old_list <span style="color: #ff7700;font-weight:bold;">if</span> item <span style="color: #66cc66;">%</span> <span style="color: #ff4500;">2</span> == <span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span></pre></div></div>

<p>I&#8217;ve used map and filter functions to do the same thing in the past, but the lambda functions feel like they are out of place when transforming a list.</p>
<h2>Syntactic Sugar</h2>
<p>People love to repeatedly trot out one or two new features in the blog posts they write when they&#8217;ve just discovered a language. I&#8217;m no different. However I usually end up look at these contrived examples with a skeptical &#8220;but how often do you really use that?&#8221; So I&#8217;ll do you a favour and share some features that will become second nature to you in Python.</p>
<p>Tuple (and list) unpacking is a really neat feature. It makes a lot of code very concise. In this example the <em>range</em> function returns a list of [0, 1, 2]. The values are unpacked and assigned to the three variables a, b and c.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">a, b, c = <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This gives you the cool feature of multiple return values in a way that fits into the language and doesn&#8217;t feel bolted on. Even better, it does it without needing to implement some one-off syntax to achieve it. You can use the same unpacking feature anywhere in your code. And yes, you will use it.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">data = <span style="color: black;">&#91;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span>, <span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span>, <span style="color: black;">&#40;</span><span style="color: #ff4500;">4</span>, <span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: black;">&#91;</span>x + y <span style="color: #ff7700;font-weight:bold;">for</span> x, y <span style="color: #ff7700;font-weight:bold;">in</span> data<span style="color: black;">&#93;</span>
<span style="color: #808080; font-style: italic;">#x and y are automatically unpacked</span></pre></div></div>

<p>Another neat feature is the way Python treats everything as an object. This means the following code is perfectly valid.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;!&quot;</span> <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">5</span>
<span style="color: #483d8b;">&quot;Hello world&quot;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #483d8b;">&quot;, &quot;</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>values<span style="color: black;">&#41;</span></pre></div></div>

<p>Note that join works on the string variable, not a list as you may expect. This is not as widely used as the tuple unpacking, but does have it&#8217;s place with string munging.</p>
<h2>Modules</h2>
<p>One of the (few) compliments given to PHP is that it has modules for pretty much anything. From my experience, albeit limited, with Python I&#8217;d have to say that it deserves the same accolades. The project I have been working on has made use of threads, queues, HTTP servers and clients, config and command line parsers as well as sub processes (including writing to <em>stdin </em>and reading <em>stdout</em>). Everything I&#8217;ve wanted has been available as a pre-packaged module. (OK, I lie, I wanted a HTML generator. I ended up downloading <a href="http://markup.sourceforge.net/">markup.py</a> and was up and running in a couple of minutes). Each of the pre-packaged modules seems well written too. Their components can be set up and used with little or no boilerplate and does what I need with very few exceptions.</p>
<p>The actual module system is a welcome sight for a PHP programmer. It&#8217;s a lot better than the &#8220;throw everything into the global namespace&#8221; method I&#8217;ve grown used to. It also means you can be sure that your required modules actually exist at runtime, instead of facing the prospect of your program failing mid-execution with &#8220;function X does not exist&#8221;</p>
<h2>Documentation</h2>
<p>PHP has spoilt me with its fantastic documentation. Python documentation is adequate, but it could be better. The API reference shows the functions and classes within a module, but could benefit code examples, gotchas and so on. It would also be great to find more links between related parts of the Python documentation. Usually I&#8217;ll need to supplement the Python documentation with a Google search for more information. PHP&#8217;s comment section is great in this regard. If someone posts to the documentation then they usually have something worth saying.</p>
<h2>It&#8217;s Guido</h2>
<p>One cools thing I&#8217;ve noticed when searching for Python examples is that <a href="http://www.python.org/~guido/">Guido van Rossum&#8217;s</a> name keeps appearing all over the place. I don&#8217;t know much about Guido, but it&#8217;s cool to see a language creator being so heavily involved in his creation. It is a kind of vote of confidence that makes me feel the language is worth learning.</p>
<h2>Am I a Convert?</h2>
<p>So, Python&#8217;s a great language, but am I a convert? Is this the end of PHP for me? Well, no. It&#8217;s a bit soon to be making that call. I&#8217;ve only used the language for a few weeks, and the first couple of weeks in a new project are always the most productive.</p>
<p>There are still quite a few things I like about PHP too: its documentation, easy integration with Apache and the new OO features are making it much more bearable. My knowledge and experience with PHP is not something I want to throw away on a whim. I know PHP&#8217;s strengths and weaknesses. I know exactly how far I can push it before things go bad. That knowledge is not something to underestimate. At this stage Python is still an unknown. I have no idea how will it perform in a web environment or how it will handle itself under a large load.</p>
<p>So, Python is a language that I can choose to use when appropriate. I can honestly say it&#8217;s been enjoyable so far and I&#8217;ll look forward to learning more.</p>
<h2>So You Want More?</h2>
<p>If you want to learn Python right now then check out the free <a href="http://www.diveintopython.org/">Dive Into Python</a> book.</p>
<p>If you are looking for something more lightweight, then the <a href="http://www.poromenos.org/tutorials/python">Python in Ten Minutes</a> tutorial is a good one.</p>
<p>Unfortunately I can&#8217;t find anything that fills in the gaps between these two resources. If you do know of something then please let me know by leaving a comment below.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2008/04/11/a-first-look-at-python/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
