<?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>My Own Private Thermopylae &#187; Programming</title>
	<atom:link href="http://thermopylae.net/blog/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://thermopylae.net/blog</link>
	<description>Do I know what a rhetorical question is?</description>
	<lastBuildDate>Sat, 14 Mar 2009 18:53:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Login on behalf of a user in Django</title>
		<link>http://thermopylae.net/blog/2009/02/26/login-on-behalf-ofa-user-in-django/</link>
		<comments>http://thermopylae.net/blog/2009/02/26/login-on-behalf-ofa-user-in-django/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 18:57:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://thermopylae.net/blog/?p=107</guid>
		<description><![CDATA[There are several points during the pieces of a Django app&#8217;s lifecycle where you&#8217;d like to log in a user without going through the standard login views. These are situations when the user has authenticated their identity in some other way, for example: User just successfully signed up.  There&#8217;s no reason to ask them to [...]]]></description>
			<content:encoded><![CDATA[<p>There are several points during the pieces of a Django app&#8217;s lifecycle where you&#8217;d like to log in a user without going through the standard login views. These are situations when the user has authenticated their identity in some other way, for example:</p>
<ul>
<li>User just successfully signed up.  There&#8217;s no reason to ask them to re-enter the credentials they just set up.</li>
<li>User just reset their password.</li>
</ul>
<p>The official documentation isn&#8217;t super clear on how to do it, but it turns out its incredibly easy:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">contrib</span>.<span style="color: black;">auth</span> <span style="color: #ff7700;font-weight:bold;">import</span> login
&nbsp;
<span style="color: #dc143c;">user</span>.<span style="color: black;">backend</span> = <span style="color: #483d8b;">'django.contrib.auth.backends.ModelBackend'</span>
login<span style="color: black;">&#40;</span>request, <span style="color: #dc143c;">user</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The backend is used by the auth system later on in the process.</p>
]]></content:encoded>
			<wfw:commentRss>http://thermopylae.net/blog/2009/02/26/login-on-behalf-ofa-user-in-django/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Effective Java Strings</title>
		<link>http://thermopylae.net/blog/2008/10/06/effective-java-strings/</link>
		<comments>http://thermopylae.net/blog/2008/10/06/effective-java-strings/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 23:33:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://thermopylae.net/blog/?p=62</guid>
		<description><![CDATA[Strings are a critical part of the Java language and especially so in a web application. Here are a variety of pointers for dealing with Strings. Use StringUtils A lot of the common String operations that aren&#8217;t in the JDK library are available in Commons Lang StringUtils. Check out those methods and you&#8217;ll find your [...]]]></description>
			<content:encoded><![CDATA[<p>Strings are a critical part of the Java language and especially so in a web application. Here are a variety of pointers for dealing with Strings.</p>
<p><strong>Use StringUtils</strong></p>
<p>A lot of the common String operations that aren&#8217;t in the JDK library are available in <a class="external text" title="http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html" href="http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html">Commons Lang StringUtils</a>. Check out those methods and you&#8217;ll find your code looking much cleaner (and you&#8217;ll have to write less of it!). Anytime you&#8217;re about to write a piece of simple string manipulation code that you think should be in the library, check Commons first.</p>
<p><strong>Concatenating a List&lt;String&gt; or a String[]</strong></p>
<p>Check out <a class="external text" title="http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html#join(java.util.Collection,%20java.lang.String)" rel="nofollow" href="http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html#join%28java.util.Collection,%20java.lang.String%29">StringUtils.join()</a> to see if it&#8217;s already done for you! Also, see the discussion below about the different ways to concatenate strings.</p>
<p><strong>Case insensitive comparison</strong></p>
<p>Strings have a <code>.equalsIgnoreCase()</code> method that lets you do what you actually want rather than worrying about case conversions.</p>
<p><strong>Prefer StrBuilder to StringBuffer/StringBuilder</strong></p>
<p>Instead of the JDK StringBu* classes and the associated confusion explained below, try using <a class="external text" title="http://commons.apache.org/lang/apidocs/org/apache/commons/lang/text/StrBuilder.html" rel="nofollow" href="http://commons.apache.org/lang/apidocs/org/apache/commons/lang/text/StrBuilder.html">StrBuilder</a>. Apache Commons has created this very handy class that has a much more powerful and flexible API than StringBuffer. Give it a try and you won&#8217;t be able to go back to the regular StringBu* classes.</p>
<p><strong>StringBuffer, StringBuilder and + </strong></p>
<p>Strings in Java are immutable, so to append two Strings together the JVM must create another String. When you do that just once, the performance impact is negligible but if you do it many times (in a loop, or a more complicated piece of code) the cost of all of those intermediate Strings adds up. In order to deal with this problem, the StringBuffer and StringBuilder class were added to the Java core libraries. StringBuffer is threadsafe but slightly slower (because of the synchronization) and StringBuilder is not threadsafe but slightly faster. Both of the builder implementations have the con that they make the code harder to read.</p>
<p>The difference between using StringBu* and the &#8216;+&#8217; operator is generally trivial, except in loops where it can become a significant performance hit.</p>
<p>As a general guideline, you should use the &#8216;+&#8217; operator to concatenate simple Strings. The java compiler will actually convert code like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">a <span style="color: #339933;">=</span> b <span style="color: #339933;">+</span> c <span style="color: #339933;">+</span> d<span style="color: #339933;">;</span></pre></div></div>

<p>To look like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">a <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>d<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So you get the performance when the code is compiled, and easier readability while the code is still in source. As of 1.5, the compiler can NOT yet optimize away the &#8216;<code>+=</code>&#8216; operator.</p>
<p>This also means that if you use <code>+</code> in a loop, then you&#8217;re incurring a performance penalty for creating a new <code>StringBuilder</code> in every iteration. They&#8217;ll be garbage collected eventually, but its not a good habit to get into. Make sure to declare the buffer/builder outside of the loop:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">StringBuilder b <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> s <span style="color: #339933;">:</span> stringList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    b.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>s<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">return</span> b.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Again, most of the basic use cases where you&#8217;d want to do that are actually already implemented for you in <a class="external text" title="http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html" rel="nofollow" href="http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html">Commons Lang StringUtils</a>.</p>
<p><strong>Don&#8217;t optimize String concatenations!</strong></p>
<p>This may seem to go against the advice of the rest of this post, but bear with me. Don&#8217;t immediately go into your codebase fixing these issues everywhere they crop up. That&#8217;s just a waste of time. Use profiling to determine which loops are actually causing slowdowns and fix those. Just keep these tips in mind as you write new code and focus on core readability as a primary requirement.</p>
]]></content:encoded>
			<wfw:commentRss>http://thermopylae.net/blog/2008/10/06/effective-java-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Facebook API &#8211; setFBML() and profile_main</title>
		<link>http://thermopylae.net/blog/2008/09/10/facebook-api-setfbml-profile-main/</link>
		<comments>http://thermopylae.net/blog/2008/09/10/facebook-api-setfbml-profile-main/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 07:26:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://thermopylae.net/blog/?p=68</guid>
		<description><![CDATA[This is a stupid gotcha that took me WAAAAY too long to figure out. If you&#8217;re using the Facebook API with the new profile pages and attempting to set the FBML for a box on the wall and info pages it won&#8217;t work out of the box. You need to take whichever client library you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>This is a stupid gotcha that took me WAAAAY too long to figure out.</p>
<p>If you&#8217;re using the Facebook API with the new profile pages and attempting to set the FBML for a box on the wall and info pages it won&#8217;t work out of the box. You need to take whichever client library you&#8217;re using (PyFacebook, PHP, etc.) and make sure to point it at  <code>http://api.new.facebook.com/</code>. Only then will the profile_main parameter get properly parsed. It would be fabulous if Facebook was to make API differences like that explicit by say, oh, I don&#8217;t know, revving the protocol version?</p>
<p>In addition, if you&#8217;re using PyFacebook (at least up to r157) you should add the type parameter to facebook.profile.getFBML() in the __init__.py file for the library. Here&#8217;s <a href="http://code.google.com/p/pyfacebook/issues/detail?id=80">the open issue</a> in PyFacebook&#8217;s issue tracker.</p>
]]></content:encoded>
			<wfw:commentRss>http://thermopylae.net/blog/2008/09/10/facebook-api-setfbml-profile-main/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Effective Java Collections</title>
		<link>http://thermopylae.net/blog/2008/08/10/basic-tips-for-using-the-java-collections-framework/</link>
		<comments>http://thermopylae.net/blog/2008/08/10/basic-tips-for-using-the-java-collections-framework/#comments</comments>
		<pubDate>Sun, 10 Aug 2008 22:44:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://thermopylae.net/blog/?p=40</guid>
		<description><![CDATA[The Collections Framework is an awesome way to represent may common types of data using the mathematical basis for sets. Sun has a great tutorial that explains what the framework is and how to use it. This is meant to be a simple guide (in the traditions of Effective Java) on how to use the [...]]]></description>
			<content:encoded><![CDATA[<p>The Collections Framework is an awesome way to represent may common types of data using the mathematical basis for sets. Sun has a great tutorial that explains what the framework is and how to use it. This is meant to be a simple guide (in the traditions of <a href="http://www.amazon.com/gp/product/0321356683?tag=myownprivatet-20">Effective Java</a>) on how to use the framework correctly.</p>
<p>To make it clear at a glance, the good and bad code samples are marked in <span class="good">green</span> and <span class="bad">red</span> respectively.</p>
<p><strong>Collections have an <code>isEmpty()</code> method</strong></p>
<p>The Collections framework goes out of its way to make the code reflect what you&#8217;re actually thinking. The proper way to check if a collection is empty is not to check if its size is zero. Use the isEmpty() method!</p>
<p><strong>Avoid returning <code>null</code> to mean an empty collection</strong></p>
<p>Leaving the possibility of returning a <code>null</code> instead of an empty collection makes your caller have to write extra boilerplate code to deal with that case. Most of the time an empty collection has the same meaning that a <code>null</code> would. Trying to use null to represent an error in whatever was supposed to generate the collection bypasses Java&#8217;s preferred way for handling errors: Exceptions.</p>
<p><strong>Create an empty collection using <code>Collections.empty***()</code> methods</strong></p>
<p>If you need an empty collection that&#8217;s guaranteed to stay empty (for example in your test code) use the Collections utility class to create them:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">List</span> myEmptyList <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">emptyList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Set</span> myEmptySet <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">emptySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Map</span> myEmptyMap <span style="color: #339933;">=</span> <span style="color: #003399;">Collections</span>.<span style="color: #006633;">emptyMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There are also constants for <code>List.EMPTY_LIST</code>, <code>Set.EMPTY_SET</code>, <code>Map.EMPTY_MAP</code>, but they&#8217;re best avoided since they do not support generics.</p>
<p><strong>Iterate through collections using the foreach form when possible</strong></p>
<p>You want the code to match your thought process as much as possible. Sometimes you can&#8217;t use the new foreach syntax introduced in Java 5 because you need access to the loop counter but in most cases the best way to iterate through a collection looks like this:</p>
<pre class="good" lang="java">List foo;
for(String bar : foo) {
    System.out.println(bar);
}</pre>
<p>The same foreach loop also works for arrays, with the exact same syntax.</p>
<p><strong>Choosing which collection type to use</strong></p>
<p>When you create a new data structure, don&#8217;t think of whether it&#8217;s a hash table, tree, whatever. Think about what the relationship of what you&#8217;re storing is. Is it a Collection, a Set, a Map, or a List?</p>
<p>If you&#8217;re not clear on what the difference is between a Collection, Map, Set, and a List check out the Javadocs (<a href="http://java.sun.com/j2se/1.5.0/docs/guide/collections/reference.html">Sun&#8217;s Cliff&#8217;s notes</a>). Start with what the requirements are for the data structure. Does it need to be ordered? Does it need to be unique? etc. Those will tell you which interface you need to use, and then you can decide what the underlying implementation should be. What if you use the wrong type of map/set/list and performance sucks? Well, then changing that implementation involves one line of code. And no one else needs to know or worry about it.</p>
<p><strong>The left side is always an interface!</strong></p>
<p>Like the title says, what you&#8217;re assigning to should almost never be an implementation. As a corollary, you should never return an implementation instead of an interface.</p>
<p>Instead of:</p>
<pre class="bad" lang="java">ArrayList foo = new ArrayList();
HashMap bar = new LinkedHashMap();

public TreeMap foo(){
...
}</pre>
<p>do this:</p>
<pre class="good" lang="java">List foo = new ArrayList();
Map bar = new LinkedHashMap();

public SortedMap foo(){
...
}</pre>
<p>There might be a few exceptions to this rule, but they&#8217;re VERY rare. If you find yourself in one of those, stop and think really hard about whether you do actually need to break it. Better yet, ask the developer next to you about what she thinks.</p>
<p><strong>If you&#8217;re explicitly casting, chances are something is wrong. Use generics.</strong></p>
<p>Try to use generics on your collections. Knowing if foo is a list of Integers or Strings will make your client code much easier to read and will protect you from a lot of mistakes. Much more importantly, it will make it significantly easier for the next person who reads the code to understand what&#8217;s going on. If you find that you need a collection that needs to store multiple types of objects, then you should ask yourself whether you really have the right data structure and interface. Almost always the need to do that is a sign of a bad OO design.</p>
]]></content:encoded>
			<wfw:commentRss>http://thermopylae.net/blog/2008/08/10/basic-tips-for-using-the-java-collections-framework/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Playing with S3 in Python</title>
		<link>http://thermopylae.net/blog/2008/07/22/playing-with-s3-in-python/</link>
		<comments>http://thermopylae.net/blog/2008/07/22/playing-with-s3-in-python/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 22:31:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[S3]]></category>

		<guid isPermaLink="false">http://thermopylae.net/blog/?p=4</guid>
		<description><![CDATA[This is a brief guide to getting up and running against S3 in Python. You will need the following things: AWS Developer account (you can sign up for it on http://aws.amazon.com) Make sure you sign up for S3 (select Amazon Simple Storage Service in the sidebar on the left of the AWS page and then [...]]]></description>
			<content:encoded><![CDATA[<p>This is a brief guide to getting up and running against S3 in Python.</p>
<p>You will need the following things:</p>
<ol>
<li>AWS Developer account (you can sign up for it on <a href="http://aws.amazon.com">http://aws.amazon.com</a>)</li>
<li>Make sure you sign up for S3 (select Amazon Simple Storage Service in the sidebar on the left of the AWS page and then click &#8216;Sign Up For This Web Service&#8217; over on the right)</li>
<li>Python (I&#8217;m assuming you have it set up and working already)</li>
</ol>
<p>A great library to use for access to AWS is called boto and its homepage is here: <a href="http://code.google.com/p/boto">http://code.google.com/p/boto</a>. You can download the code directly and then run its setup.py to install it, or if you already have <a href="http://peak.telecommunity.com/DevCenter/EasyInstall">easy_install</a> you can make it do all of the work:</p>
<pre class="shell">C:boto-1.3a&gt;python setup.py install</pre>
<p>or</p>
<pre class="shell">C:&gt;python easy_install.py boto</pre>
<p>Either way, you can verify that boto is properly installed by trying to import it in python:</p>
<pre class="shell">C:&gt;python
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; import boto
&gt;&gt;&gt;</pre>
<p>Once boto is set up correctly get your AWS identifiers from the &#8220;Your Web Services Account&#8221; dropdown in the upper right of the AWS pages. You&#8217;ll need to use both the Access key and the Secret key when you make the connection.</p>
<p>At the highest level AWS stores data in &#8216;buckets&#8217;. These are unique across the entire service and each account is limited to a max of 100 of them. Within the bucket you create key/data pairs that look like filenames.</p>
<p>The code to do this is super simple. You&#8217;ll probably need to replace &#8216;examplebucket&#8217; with something else:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> boto.<span style="color: black;">s3</span>.<span style="color: black;">connection</span> <span style="color: #ff7700;font-weight:bold;">import</span> S3Connection
<span style="color: #ff7700;font-weight:bold;">from</span> boto.<span style="color: black;">s3</span>.<span style="color: black;">key</span> <span style="color: #ff7700;font-weight:bold;">import</span> Key
conn = S3Connection<span style="color: black;">&#40;</span><span style="color: #483d8b;">''</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
bucket = conn.<span style="color: black;">create_bucket</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'examplebucket'</span><span style="color: black;">&#41;</span>
k = Key<span style="color: black;">&#40;</span>bucket<span style="color: black;">&#41;</span>
k.<span style="color: black;">key</span> = <span style="color: #483d8b;">'foo'</span>
k.<span style="color: black;">set_contents_from_filename</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'foo.png'</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Doing this will upload the file foo.png to the bucket &#8216;examplebucket&#8217;. That would mean that the file is accessible at: http://s3.amazonaws.com/examplebucket/foo. If you attempt to go to the appropriate URL for that file you&#8217;ll get an access denied error back. By default new objects in the bucket aren&#8217;t publicly readable. Making the object readable takes advantage of a shortcut built into boto. In the boto.s3.acl module there is a list of &#8216;ready-to-use&#8217; modes:</p>
<pre class="shell">CannedACLStrings = ['private', 'public-read', 'public-read-write', 'authenticated-read']</pre>
<p>Now we just set the &#8216;public-read&#8217; ACL on our key and it will become accessible through the URL:</p>
<pre class="shell">k.set_acl('public-read')</pre>
<p>From here, check out the <a href="http://developer.amazonwebservices.com/connect/entry.jspa?externalID=123&#038;ref=latest-docs">official documentation</a> and tools such as <a href="https://addons.mozilla.org/en-US/firefox/addon/3247">S3Fox</a> that will make your S3 experience much easier.</p>
<p>You should also grab the bota documentation archive and save it locally to use as a reference.</p>
]]></content:encoded>
			<wfw:commentRss>http://thermopylae.net/blog/2008/07/22/playing-with-s3-in-python/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
