<?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>Dabrorius blog</title>
	<atom:link href="http://dabrorius.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://dabrorius.com</link>
	<description>Python, PHP, web development, Ruby, Java, software...</description>
	<lastBuildDate>Wed, 04 May 2011 08:53:27 +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>Java play! framework PubSubHubbub subscription confirmation problem</title>
		<link>http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/</link>
		<comments>http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/#comments</comments>
		<pubDate>Wed, 04 May 2011 08:53:14 +0000</pubDate>
		<dc:creator>dabrorius</dc:creator>
				<category><![CDATA[Errors]]></category>

		<guid isPermaLink="false">http://dabrorius.com/?p=181</guid>
		<description><![CDATA[I have lost hours and hours on this one. My subscription confirmations just didn&#8217;t work, I have scanned the network traffic with wireshark and my app just didn&#8217;t send a response to confirmation request.
To solve this problem all you need to do is open application.conf ande set play.pool value to 2 or higher. Default is [...]]]></description>
			<content:encoded><![CDATA[<p>I have lost hours and hours on this one. My subscription confirmations just didn&#8217;t work, I have scanned the network traffic with wireshark and my app just didn&#8217;t send a response to confirmation request.</p>
<p>To solve this problem all you need to do is open application.conf ande set <strong>play.pool</strong> value to 2 or higher. Default is one and server can&#8217;t responde to a request until the first one is finished so confirmation fails.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<div class="d">
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/&amp;title=Java+play%21+framework+PubSubHubbub+subscription+confirmation+problem" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/&amp;title=Java+play%21+framework+PubSubHubbub+subscription+confirmation+problem" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Java+play%21+framework+PubSubHubbub+subscription+confirmation+problem&amp;url=http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/&amp;title=Java+play%21+framework+PubSubHubbub+subscription+confirmation+problem" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/&amp;title=Java+play%21+framework+PubSubHubbub+subscription+confirmation+problem" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/&amp;title=Java+play%21+framework+PubSubHubbub+subscription+confirmation+problem" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/&amp;title=Java+play%21+framework+PubSubHubbub+subscription+confirmation+problem" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Java+play%21+framework+PubSubHubbub+subscription+confirmation+problem+@+http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://dabrorius.com/errors/java-play-framework-pubsubhubbub-subscription-confirmation-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Basic string manipulation in ruby</title>
		<link>http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/</link>
		<comments>http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/#comments</comments>
		<pubDate>Wed, 23 Mar 2011 23:25:15 +0000</pubDate>
		<dc:creator>dabrorius</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://dabrorius.com/?p=153</guid>
		<description><![CDATA[Split string to an array
One common task that you probably want to learn how to do is spliting a string into an array (like explode function in PHP). You can do this by calling &#8220;split&#8221; method. By default it will split a string on whitespace, but you can pass it a delimiter. Delimiter can be [...]]]></description>
			<content:encoded><![CDATA[<h2>Split string to an array</h2>
<p>One common task that you probably want to learn how to do is spliting a string into an array (like explode function in PHP). You can do this by calling &#8220;split&#8221; method. By default it will split a string on whitespace, but you can pass it a delimiter. Delimiter can be a string or regular expression.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">animals = <span style="color:#996600;">'dog wolf cat lynx tiger'</span>
animals.<span style="color:#CC0066; font-weight:bold;">split</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;dog&quot;, &quot;wolf&quot;, &quot;cat&quot;, &quot;lynx&quot;, &quot;tiger&quot;] </span>
&nbsp;
<span style="color:#996600;">&quot;6534-234-23-45&quot;</span>.<span style="color:#CC0066; font-weight:bold;">split</span> <span style="color:#996600;">&quot;-&quot;</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;6534&quot;, &quot;234&quot;, &quot;23&quot;, &quot;45&quot;] </span>
&nbsp;
calc = <span style="color:#996600;">&quot;45 + 36 - 25 * 14&quot;</span>
calc.<span style="color:#CC0066; font-weight:bold;">split</span> <span style="color:#006600; font-weight:bold;">/</span>\s<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">-+*</span><span style="color:#006600; font-weight:bold;">&#93;</span>\s<span style="color:#006600; font-weight:bold;">*/</span> <span style="color:#008000; font-style:italic;"># =&gt; [&quot;45&quot;, &quot;36&quot;, &quot;25&quot;, &quot;14&quot;]</span></pre></td></tr></table></div>

<p><span id="more-153"></span></p>
<h2>Substitute part of a string</h2>
<p>We can do this in two ways. First one is by calling &#8220;gsub&#8221; method upon a string, and this is quite a usual way. Second one may seem strange at first but it&#8217;s quite nice actually. You can substitute a substring like this: my_string["string_to_substitute"] = &#8220;replacement_string&#8221;. Take a look at the example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">my_statement = <span style="color:#996600;">&quot;I like dogs&quot;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">print</span> my_statement.<span style="color:#CC0066; font-weight:bold;">gsub</span> <span style="color:#996600;">&quot;dogs&quot;</span>,<span style="color:#996600;">&quot;cats&quot;</span> <span style="color:#008000; font-style:italic;">#  =&gt; &quot;I like cats&quot; </span>
<span style="color:#CC0066; font-weight:bold;">print</span> my_statement <span style="color:#008000; font-style:italic;"># =&gt; &quot;I like dogs&quot;</span>
&nbsp;
my_statement<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;dogs&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#996600;">&quot;cats&quot;</span> <span style="color:#008000; font-style:italic;">#  =&gt; &quot;I like cats&quot; </span>
<span style="color:#CC0066; font-weight:bold;">print</span> my_statement <span style="color:#008000; font-style:italic;"># =&gt; &quot;I like cats&quot;</span></pre></td></tr></table></div>

<p>Notice that in first case we didn&#8217;t make changes upon a string, &#8220;gsub&#8221; method just returned a new version of a string and left &#8220;my_statment&#8221; as it is. If you want to make changes upon a string you could use &#8220;gsub!&#8221; method.<br />
This is a ruby convention, so every time you see a method that ends with &#8220;!&#8221; you should be careful because it will change the object it is called upon.</p>
<h2>String concatenation</h2>
<p>You can &#8220;glue&#8221; one string on another simply with a <strong>+</strong> or a <strong><<</strong> operator.<br />
If you want to insert a string in a string, simply use &#8220;insert&#8221; method, it takes 2 parameters, first is position where to insert the string and second is the string that you want to insert.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">part1 = <span style="color:#996600;">&quot;walk&quot;</span>
part2 = <span style="color:#996600;">&quot;dog&quot;</span>
result = part1 <span style="color:#006600; font-weight:bold;">+</span> part2 <span style="color:#008000; font-style:italic;"># =&gt; walkdog</span>
result.<span style="color:#9900CC;">insert</span> <span style="color:#006666;">4</span>, <span style="color:#996600;">&quot; a &quot;</span> <span style="color:#008000; font-style:italic;"># =&gt; walk a dog</span></pre></td></tr></table></div>

<h2>Cut off last character</h2>
<p>You can cut out last character of your string using &#8220;chop&#8221; method. &#8220;chop&#8221; also comes with &#8220;chop!&#8221; version that will make a change on a string, and not just return a new version of a string.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">string</span> = <span style="color:#996600;">&quot;Animals&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#CC0066; font-weight:bold;">chop</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;Animal&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;Animals&quot;</span>
<span style="color:#CC0066; font-weight:bold;">string</span>.<span style="color:#CC0066; font-weight:bold;">chop!</span> 
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;Animal&quot;</span></pre></td></tr></table></div>

<h2>CHANGEING CaSE of A STRing</h2>
<p>Ruby supports easy case changing for entire strings. It includes changing all characters to lower case using the &#8220;downcase&#8221; method and changing string to all caps with &#8220;upcase&#8221; method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;A normal string&quot;</span>.<span style="color:#9900CC;">downcase</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;a normal string&quot; </span>
<span style="color:#996600;">&quot;A normal string&quot;</span>.<span style="color:#9900CC;">upcase</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;A NORMAL STRING&quot;</span></pre></td></tr></table></div>

<p>But ruby can do even cooler stuff. Like capitalizing strings, and swapping case of all characters. When capitalizing strings only first character will be in upper case, it won&#8217;t do capitalization for each sentence.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#996600;">&quot;this is NICE!&quot;</span>.<span style="color:#9900CC;">capitalize</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;This is nice!&quot; </span>
<span style="color:#996600;">&quot;this is NICE! but could be nicer.&quot;</span>.<span style="color:#9900CC;">capitalize</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;This is nice! but could be nicer.&quot; </span>
<span style="color:#996600;">&quot;this is NICE!&quot;</span>.<span style="color:#9900CC;">swapcase</span> <span style="color:#008000; font-style:italic;"># =&gt; &quot;THIS IS nice!&quot;</span></pre></td></tr></table></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<div class="d">
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/&amp;title=Basic+string+manipulation+in+ruby" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/&amp;title=Basic+string+manipulation+in+ruby" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Basic+string+manipulation+in+ruby&amp;url=http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/&amp;title=Basic+string+manipulation+in+ruby" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/&amp;title=Basic+string+manipulation+in+ruby" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/&amp;title=Basic+string+manipulation+in+ruby" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/&amp;title=Basic+string+manipulation+in+ruby" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Basic+string+manipulation+in+ruby+@+http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://dabrorius.com/ruby/basic-string-manipulation-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails many-to-many association tutorial</title>
		<link>http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/</link>
		<comments>http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/#comments</comments>
		<pubDate>Wed, 23 Mar 2011 10:58:20 +0000</pubDate>
		<dc:creator>dabrorius</dc:creator>
				<category><![CDATA[Ruby on rails tutorials]]></category>
		<category><![CDATA[Active record]]></category>

		<guid isPermaLink="false">http://dabrorius.com/?p=128</guid>
		<description><![CDATA[Has and belongs to many
If you just need to set a simple many-to-many relationship the easiest way to do this is with has_and_belongs_to_many. Here is how your models should look like.

1
2
3
4
5
6
7
class Post &#60; ActiveRecord::Base
  has_and_belongs_to_many :tags
end
&#160;
class Tag &#60; ActiveRecord::Base
  has_and_belongs_to_many :posts
end

This association is supported by an additional database table that will store post_id [...]]]></description>
			<content:encoded><![CDATA[<h2>Has and belongs to many</h2>
<p>If you just need to set a simple many-to-many relationship the easiest way to do this is with has_and_belongs_to_many. Here is how your models should look like.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Post <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:tags</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Tag <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_and_belongs_to_many <span style="color:#ff3333; font-weight:bold;">:posts</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>This association is supported by an additional database table that will store post_id and tag_id for each relationship. This table should be called posts_tags and it will not be created for you, so you will have to create your own migration.</p>
<p><img src="http://dabrorius.com/wp-content/uploads/2011/03/posts.jpg" alt="has and belongs to many er-model" title="has and belongs to many er-model" width="326" height="211" class="aligncenter size-full wp-image-146" /><br />
<span id="more-128"></span><br />
Notice that model names inside join table are in lexical order, so the table will be called posts_tags and not tags_posts (because &#8216;p&#8217; comes before &#8216;t&#8217;). You can also specify a different name for that table using the :join_table option. </p>
<p>So generate a new migration (you could do this with &#8220;rails generate migration create_post_tag_join_table&#8221; and enter following code. Then run &#8220;rake db:migrate&#8221; to make the changes inside your database.</p>
<p> You should use &#8220;:id=>false&#8221; option in your migration, because join table does not need additional primary key, also notice that this table won&#8217;t have &#8220;created_at&#8221; and &#8220;updated_at&#8221; timestamps.</p>
<div class="terminal">
> rails generate migration create_post_tag_join_table
</div>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">classCreatePostTagJoinTable <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  defself.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:posts_tags</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:post_id</span>
      t.<span style="color:#CC0066; font-weight:bold;">integer</span> <span style="color:#ff3333; font-weight:bold;">:tag_id</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  defself.<span style="color:#9900CC;">down</span>
    drop_table:posts_tags
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<div class="terminal">
> rake db:migrate
</div>
<p>Accessing related posts and tags is now easy. You can get an array of related tags by calling &#8216;tags&#8217; method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@post</span> = Post.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0066ff; font-weight:bold;">@tags</span> = <span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">tags</span></pre></td></tr></table></div>

<p>Simplest way to add existing objects is using the << operator.  For example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@tag</span> = Tag.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#0066ff; font-weight:bold;">@tag</span>.<span style="color:#9900CC;">posts</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> post</pre></td></tr></table></div>

<p>Or if you want to create brand new object you can use a create method. This method returns created object. The object will be saved in databases, and link through join table will be created.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@tag</span>.<span style="color:#9900CC;">posts</span>.<span style="color:#9900CC;">create</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:title <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'New title'</span>, <span style="color:#ff3333; font-weight:bold;">:content</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Hello world'</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>Collections also come with delete method. It will remove objects relationship, but will not destroy the object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@post</span>.<span style="color:#9900CC;">tags</span>.<span style="color:#9900CC;">delete</span><span style="color:#006600; font-weight:bold;">&#40;</span> <span style="color:#0066ff; font-weight:bold;">@tag</span> <span style="color:#006600; font-weight:bold;">&#41;</span></pre></td></tr></table></div>

<p>More info can be found inside <a href="http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association">rails documentation</a>.</p>
<h2>Has many through</h2>
<p>In case we need to store additional information inside join table, we will need to use a more complicated version of many-to-many relationship. Lets say that we have two models Student and Test. Each student has taken many tests and each test was taken by many students, but each time a student took a test he got a grade. This is where has_many :through relationship comes in.</p>
<p><img src="http://dabrorius.com/wp-content/uploads/2011/03/students.jpg" alt="has many through er-model" title="has many through er-model" width="326" height="197" class="aligncenter size-full wp-image-147" /></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">classStudent <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:grades</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:tests</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:grades</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Grade <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:student</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:test</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
classTest <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:grades</span>
  has_many <span style="color:#ff3333; font-weight:bold;">:students</span>, <span style="color:#ff3333; font-weight:bold;">:through</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:grades</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Notice that beside setting the has_many :tests relationship, in order to use :through option you need to notify model that it has grades. In other words, you cant &#8216;go through&#8217; models that you don&#8217;t have.  Notice that in this case Grade is a normal model, and it must include ID field to work properly. However timestamps may be excluded if you don&#8217;t plan to use them.</p>
<p>This relationships behave similar to has_and_belongs_to_many relationship. More info can be found inside <a href="http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association">rails documentation</a>.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<div class="d">
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/&amp;title=Rails+many-to-many+association+tutorial" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/&amp;title=Rails+many-to-many+association+tutorial" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Rails+many-to-many+association+tutorial&amp;url=http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/&amp;title=Rails+many-to-many+association+tutorial" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/&amp;title=Rails+many-to-many+association+tutorial" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/&amp;title=Rails+many-to-many+association+tutorial" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/&amp;title=Rails+many-to-many+association+tutorial" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Rails+many-to-many+association+tutorial+@+http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://dabrorius.com/ruby-on-rails-tutorials/rails-many-to-many-association-tutorial/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Extremely simple PubSubHubbub application with Ruby and Sinatra</title>
		<link>http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/</link>
		<comments>http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 11:51:47 +0000</pubDate>
		<dc:creator>dabrorius</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[PubSubHubbub]]></category>
		<category><![CDATA[Sinatra]]></category>

		<guid isPermaLink="false">http://dabrorius.com/?p=116</guid>
		<description><![CDATA[I&#8217;m starting to work on my project that is supposed to use PubSubHubbub. I had some difficulty understanding it as I found documentation inadequate. I have managed to get this simple app to work with a public Hub (http://pubsubhubbub.appspot.com/).

All it does is confirm all subscription requests, and when it recieves data it prints it on [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m starting to work on my project that is supposed to use PubSubHubbub. I had some difficulty understanding it as I found documentation inadequate. I have managed to get this simple app to work with a public Hub (http://pubsubhubbub.appspot.com/).</p>
<p>
All it does is confirm all subscription requests, and when it recieves data it prints it on screen. It uses Sinatra for handling http requests. It&#8217;s not much but maybe someone will find it useful. </p>
<p><span id="more-116"></span></p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'sinatra'</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#accepting all requests</span>
get <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
	<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;response: '&quot;</span><span style="color:#006600; font-weight:bold;">+</span> params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'hub.challenge'</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;'&quot;</span>
&nbsp;
	content_type <span style="color:#996600;">'text/plain'</span>, <span style="color:#ff3333; font-weight:bold;">:charset</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'utf-8'</span>
	params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'hub.challenge'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#accepting messages</span>
post <span style="color:#996600;">'/'</span> <span style="color:#9966CC; font-weight:bold;">do</span>
	<span style="color:#CC0066; font-weight:bold;">puts</span> request.<span style="color:#9900CC;">body</span>.<span style="color:#9900CC;">read</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>
</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<div class="d">
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/&amp;title=Extremely+simple+PubSubHubbub+application+with+Ruby+and+Sinatra" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/&amp;title=Extremely+simple+PubSubHubbub+application+with+Ruby+and+Sinatra" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Extremely+simple+PubSubHubbub+application+with+Ruby+and+Sinatra&amp;url=http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/&amp;title=Extremely+simple+PubSubHubbub+application+with+Ruby+and+Sinatra" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/&amp;title=Extremely+simple+PubSubHubbub+application+with+Ruby+and+Sinatra" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/&amp;title=Extremely+simple+PubSubHubbub+application+with+Ruby+and+Sinatra" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/&amp;title=Extremely+simple+PubSubHubbub+application+with+Ruby+and+Sinatra" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Extremely+simple+PubSubHubbub+application+with+Ruby+and+Sinatra+@+http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://dabrorius.com/ruby/extremely-simple-pubsubhubbub-application-with-ruby-and-sinatra/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Favorite Django Tips</title>
		<link>http://dabrorius.com/django-tutorials/favorite-django-tips/</link>
		<comments>http://dabrorius.com/django-tutorials/favorite-django-tips/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 13:31:56 +0000</pubDate>
		<dc:creator>heyman.info tag feed</dc:creator>
				<category><![CDATA[Django tutorials]]></category>

		<guid isPermaLink="false">http://heyman.info/2010/mar/3/favorite-django-tips/</guid>
		<description><![CDATA[<p>A few months ago I found a really useful <a href="http://stackoverflow.com/questions/550632/favorite-django-tips-features">Stack Overflow Question</a>. Here are my favorites from the answers.</p>
<h3>Use <em>render_to</em> decorator instead of <em>render_to_response</em></h3>
<p>This decorator is found in the app <a href="http://bitbucket.org/offline/django-annoying/wiki/Home">django annoying</a>, and is a very nice shortcut for declaring what template a view should render.</p>
<p>Instead of returning the response of <em>render_to_response</em>, you just return a python dict which will be used as the template context for the template specified as argument to the @render_to decorator. If anything else than a dict is returned, normal view processing will occur, so this won't break redirects or any other cases where you might return a HttpResponse (for example normal <em>render_to_response</em> code).</p>
<p>Anyway, here is an example on how to use it:</p>
<pre><code>:::python
@render_to("list.html")
def list_posts(request):
    posts = BlogPost.objects.all()
    return {"blog_posts": posts}
</code></pre>
<p>This equals to:</p>
<pre><code>:::python
def list_posts(request):
    posts = BlogPost.objects.all()
    return render_to_response('list.html',
        {'blog_posts': posts},
        context_instance=RequestContext(request))
</code></pre>
<h3>Load custom template tags in all templates</h3>
<p>Custom template tags that you use all over your templates can be auto loaded. Just add the following in a module that is loaded (i.e. your urlconf if you want the template tags to be loaded for the whole project)</p>
<pre><code>:::python
from django import template
template.add_to_builtins('project.app.templatetags.custom_tag_module')
</code></pre>
<h3>Use relative paths in settings.py</h3>
<p>I hesitated about adding this tips, since I think it's quite obvious, but since so many people on Stack Overflow has voted it up, I guess there are people who use(d) absolute paths in their settings.py. </p>
<p>Don't use absolute paths in settings.py (i.e /home/jonatan/...), instead use relative paths so that the project will work wherever it resides.</p>
<pre><code>:::python
import os.path
TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), "templates"),
)
</code></pre>]]></description>
			<content:encoded><![CDATA[A few months ago I found a really useful <a href="http://stackoverflow.com/questions/550632/favorite-django-tips-features">Stack Overflow Question</a>. Here are my favorites from the answers.
<h3>Use <em>render_to</em> decorator instead of <em>render_to_response</em></h3>
This decorator is found in the app <a href="http://bitbucket.org/offline/django-annoying/wiki/Home">django annoying</a>, and is a very nice shortcut for declaring what template a view should render.

Instead of returning the response of <em>render_to_response</em>, you just return a python dict which will be used as the template context for the template specified as argument to the @render_to decorator. If anything else than a dict is returned, normal view processing will occur, so this won't break redirects or any other cases where you might return a HttpResponse (for example normal <em>render_to_response</em> code).

<span id="more-107"></span>

Anyway, here is an example on how to use it:
<pre><code>:::python
@render_to("list.html")
def list_posts(request):
    posts = BlogPost.objects.all()
    return {"blog_posts": posts}
</code></pre>
This equals to:
<pre><code>:::python
def list_posts(request):
    posts = BlogPost.objects.all()
    return render_to_response('list.html',
        {'blog_posts': posts},
        context_instance=RequestContext(request))
</code></pre>
<h3>Load custom template tags in all templates</h3>
Custom template tags that you use all over your templates can be auto loaded. Just add the following in a module that is loaded (i.e. your urlconf if you want the template tags to be loaded for the whole project)
<pre><code>:::python
from django import template
template.add_to_builtins('project.app.templatetags.custom_tag_module')
</code></pre>
<h3>Use relative paths in settings.py</h3>
I hesitated about adding this tips, since I think it's quite obvious, but since so many people on Stack Overflow has voted it up, I guess there are people who use(d) absolute paths in their settings.py.

Don't use absolute paths in settings.py (i.e /home/jonatan/...), instead use relative paths so that the project will work wherever it resides.
<pre><code>:::python
import os.path
TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), "templates"),
)
</code></pre>]]></content:encoded>
			<wfw:commentRss>http://dabrorius.com/django-tutorials/favorite-django-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing Python 2.5 in Fedora 12 for Google App Engine</title>
		<link>http://dabrorius.com/errors/installing-python-2-5-in-fedora-12-for-google-app-engine/</link>
		<comments>http://dabrorius.com/errors/installing-python-2-5-in-fedora-12-for-google-app-engine/#comments</comments>
		<pubDate>Fri, 16 Apr 2010 16:43:00 +0000</pubDate>
		<dc:creator>Andriy Drozdyuk</dc:creator>
				<category><![CDATA[Errors]]></category>
		<category><![CDATA[Python tutorials]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">tag:blogger.com,1999:blog-7446841198555618766.post-7915916019383107117</guid>
		<description><![CDATA[This stupid install process gets me every time! How do you run Google App Engine which currently requires Python2.5 on a machine that has Python2.6 installed? This also goes for anybody trying to use Django with App-Engine.

Everytime I try I get an er...]]></description>
			<content:encoded><![CDATA[This stupid install process gets me every time! How do you run Google App Engine which currently <strong>requires</strong> Python2.5 on a machine that has Python2.6 installed? This also goes for anybody trying to use Django with App-Engine.<span id="more-85"></span>

Everytime I try I get an error like this:
<code>
Traceback (most recent call last):
File "/home/andriy/lib/google_appengine/dev_appserver.py", line 68, in
run_file(__file__, globals())
File "/home/andriy/lib/google_appengine/dev_appserver.py", line 64, in run_file
execfile(script_path, globals_)
File "/home/andriy/lib/google_appengine/google/appengine/tools/dev_appserver_main.py", line 82, in
from google.appengine.tools import appcfg
File "/home/andriy/lib/google_appengine/google/appengine/tools/appcfg.py", line 59, in
from google.appengine.tools import appengine_rpc
File "/home/andriy/lib/google_appengine/google/appengine/tools/appengine_rpc.py", line 32, in
https_handler = urllib2.HTTPSHandler
AttributeError: 'module' object has no attribute 'HTTPSHandler'
</code>

Now some people say that you need to "compile python with ssl support". But it ain't so. There is actually nothing to <strong>configure</strong> in the python install to make it include ssl. Python finds the ssl libraries automatically if they are installed. The trick is you need the dev ssl libraries.

So here it is for my own reference:
<ol>
	<li>Download the openssl-dev packages! Yes... you need them: sudo yum install openssl-dev*</li>
	<li>Download python2.5</li>
	<li>Configure it: ./configure</li>
	<li>Build it: sudo make</li>
	<li>Alt-install it to make it reside near your current python install: sudo make altinstall</li>
</ol>
Now download App-Engine and make sure to use python2.5 command with it.
<div class="blogger-post-footer"><img src="https://blogger.googleusercontent.com/tracker/7446841198555618766-7915916019383107117?l=drozdyuk.blogspot.com" alt="" width="1" height="1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dabrorius.com/errors/installing-python-2-5-in-fedora-12-for-google-app-engine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Views in subfolder</title>
		<link>http://dabrorius.com/django-tutorials/views-in-subfolder/</link>
		<comments>http://dabrorius.com/django-tutorials/views-in-subfolder/#comments</comments>
		<pubDate>Thu, 15 Apr 2010 23:05:17 +0000</pubDate>
		<dc:creator>dabrorius</dc:creator>
				<category><![CDATA[Django tutorials]]></category>
		<category><![CDATA[Errors]]></category>

		<guid isPermaLink="false">http://django-python.com/?p=78</guid>
		<description><![CDATA[If you want to put your views in a subfolder and you are getting an import error, all you have to do is create a blank  &#8220;__init__.py&#8221; file within that subfolder and everything should work fine.  
















]]></description>
			<content:encoded><![CDATA[<p>If you want to put your views in a subfolder and you are getting an import error, all you have to do is create a blank  &#8220;__init__.py&#8221; file within that subfolder and everything should work fine. <img src='http://dabrorius.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<div class="d">
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://dabrorius.com/django-tutorials/views-in-subfolder/&amp;title=Views+in+subfolder" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://dabrorius.com/django-tutorials/views-in-subfolder/&amp;title=Views+in+subfolder" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Views+in+subfolder&amp;url=http://dabrorius.com/django-tutorials/views-in-subfolder/&amp;title=Views+in+subfolder" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://dabrorius.com/django-tutorials/views-in-subfolder/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://dabrorius.com/django-tutorials/views-in-subfolder/&amp;title=Views+in+subfolder" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://dabrorius.com/django-tutorials/views-in-subfolder/&amp;title=Views+in+subfolder" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://dabrorius.com/django-tutorials/views-in-subfolder/&amp;title=Views+in+subfolder" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://dabrorius.com/django-tutorials/views-in-subfolder/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Views+in+subfolder+@+http://dabrorius.com/django-tutorials/views-in-subfolder/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://dabrorius.com/django-tutorials/views-in-subfolder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Debugging production Django deployment</title>
		<link>http://dabrorius.com/django-tutorials/debugging-production-django-deployment/</link>
		<comments>http://dabrorius.com/django-tutorials/debugging-production-django-deployment/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 05:41:00 +0000</pubDate>
		<dc:creator>Andriy Drozdyuk</dc:creator>
				<category><![CDATA[Django tutorials]]></category>
		<category><![CDATA[deploying]]></category>

		<guid isPermaLink="false">tag:blogger.com,1999:blog-7446841198555618766.post-1799279649732304257</guid>
		<description><![CDATA[Deploying Django is a process that can drive one bananas. There are a lot of things to setup to go from your development environment to the production. Aside from the regular hassles - there come special little buggers that can really make you mad. If ...]]></description>
			<content:encoded><![CDATA[Deploying Django is a process that can drive one bananas. There are <a href="http://docs.djangoproject.com/en/dev/howto/deployment/">a lot of things</a> to setup to go from your development environment to the production. Aside from the regular hassles - there come special little buggers that can really make you mad. If you ever had problems with 500.html pages, url configurations or import errors - you know what I mean.

However it doesn't all have to be that bad. There are quite a few steps you can take early on to minimize the pain. If you do get into trouble - there are some things you can do to debug out of it.<span id="more-86"></span>

<a name="more"></a>
<strong>Preventative Measures</strong>
<ol>
	<li>Deploy early
If you want to avoid the hassle of debugging 100 things at once, deploy your project as soon as possible. By "as soon as possible" I mean right after you create it - when there are no models, views and funky url settings!</li>
	<li>Commit often
You are of course using <a href="http://git-scm.com/">source control</a> for your project, correct? By committing often you allow for greater granularity of your project. In a way you "isolate" bugs in a particular commit. This makes finding them later on particularly easy.</li>
	<li>Deploy often
Same as #1 - the only way to make sure it works - is to put it out into the wild!</li>
	<li>Separate production from development.
You should have something like this in your settings.py file:
if socket.gethostname() == 'server40.online.com':
DEBUG = False
else:
DEBUG = True
and feed all your other settings (i.e. database, media, etc) from this variable. This will make for painless deployment.</li>
	<li>Use dynamic path names
Don't just hardcode "C:\Documents" or "/home/user/app" in your settings. Don't be lazy. This is how we do it:

import os
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

and then go crazy for all the other path veriables:
MEDIA_ROOT = os.path.join(SITE_ROOT, 'core/media')
TEMPLATE_DIRS = ( os.path.join(SITE_ROOT, 'core/templates') )
and so on...</li>
	<li>Checkout source code into production
Don't just copy your code to production server with FTP or SCP! Install revision control like <a href="http://git-scm.com/">git</a> or <a href="http://mercurial.selenic.com/">mercurial</a> on the server, so you can hack away at the code without being afraid of messing things up, and more importantly - not loosing your important "tweaks" that you can only do in production.</li>
</ol>
<div><strong>Debugging the Deployed</strong></div>
<div>
<ol>
	<li>Check your path and full-qualify the import names
Make sure you python path is correct. Sometimes the modules you can see in your development - are invisible in production.
If your project structure is something like this:
/project
---/app
------- views.py
and you are importing your views in your urls.py files like so:
from app import views
than you might get a wierd handler500 error that makes no sense at all.
Either reference your modules with full qualified names, i.e. "project.app.views" or put the path to your app directory into your path: "/home/user/blah/project/app".</li>
	<li>Back up - test - roll forward
Sometimes you can't tell if the bug is from your latest update. Because production can keep some code in memory - one often fails to notice a bug until it is more than one update later.
Your first order of business should be to find the bug exactly. Figure out which "commit" added that bug into the system.
For this you must follow these steps:
a) Roll back to some point in the code where it worked
b) Restart your server, services etc... Make sure it works.
c) Roll forward to the newer revision
d) Restart. If everything still works - go to c).
e) If stuff is broken Roll back to some point before a).
Essentially you are doing a "binary" search on your commit history to isolate the bug. If you are using git  - you can automate this process with <a href="http://www.kernel.org/pub/software/scm/git/docs/git-bisect.html">git-bisect</a>.
Once you find the offending commit - roll your code to the revision before it, and start by copying by hand the code from the offending revision.
Hopefully because you kept your commits small, it should not be too long before you find the bug!</li>
	<li>90% it's urls and imports
Most likely you screwed up your imports or urls. Check them over.</li>
	<li>The other 10% it's your 500.html and 404.html
Did you create your 500.html and 404.html pages in your templates directory? The production environment won't work without them.</li>
	<li>User postgresql_psycopg2 and not postgresql
This is more of a tip than debugging technique, but one often confuses the two. The new psycopg is termed psycopg2 and so we must use postgresql_psycopg2 for our database engine setting.</li>
	<li>Set DEBUG to True on production
Just below your logic which determines your DEBUG variable, overwrite it temporarily with true.
if blah..
DEBUG = True
else:
DEBUG = False
DEBUG = True # Overwrite temporarily

This is a real hack, but it can temporarily give you some great insight into the problem.</li>
</ol>
<div><strong>Conclusion</strong></div>
<div>In the end if you try to keep the "production" on your mind from the start, your deployment should be not a problem. Because one does not deploy very often, one tends to forget all the mistakes that were made previously. This makes it really challenging to get it "right" every time. Some time is spent going over the error logs and figuring out the same problems. However we cannot solve this. It will always be like so.
The best thing you can do is prepare for this process ahead of time - and once there, have effective ways of dealing with it.</div>
</div>
<div class="blogger-post-footer"><img src="https://blogger.googleusercontent.com/tracker/7446841198555618766-1799279649732304257?l=drozdyuk.blogspot.com" alt="" width="1" height="1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dabrorius.com/django-tutorials/debugging-production-django-deployment/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tips for readable python code</title>
		<link>http://dabrorius.com/python-tutorials/tips-for-readable-python-code/</link>
		<comments>http://dabrorius.com/python-tutorials/tips-for-readable-python-code/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 00:59:35 +0000</pubDate>
		<dc:creator>dabrorius</dc:creator>
				<category><![CDATA[Python tutorials]]></category>
		<category><![CDATA[clean code]]></category>
		<category><![CDATA[readable code]]></category>

		<guid isPermaLink="false">http://django-python.com/?p=40</guid>
		<description><![CDATA[In this post I will try to explain some tricks I use to make python code more readable. These tips won&#8217;t make your code shorter and it won&#8217;t be better optimized, it will actually use more memory than before, but it will be much easier to understand and edit for someone else.

Using descriptive names
Ok, this [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will try to explain some tricks I use to make python code more readable. These tips won&#8217;t make your code shorter and it won&#8217;t be better optimized, it will actually use more memory than before, but it will be much easier to understand and edit for someone else.<br />
<span id="more-40"></span></p>
<h3>Using descriptive names</h3>
<p>Ok, this is quite obvious but I will write it anyway. You should avoid using names like &#8220;var24&#8243; , &#8220;cpnow(x)&#8221;, &#8220;move&#8221; and similar.</p>
<table border="0" width="100%">
<tbody>
<tr>
<td width="50%" valign="top">Old code</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">x = usr.<span style="color: #008000;">id</span>
add<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
show<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span></pre></div></div>

</td>
<td>New code</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">current_user_id = User.<span style="color: #008000;">id</span>
add_user_to_admins<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>
show_user_data<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<h3>Proximity</h3>
<p>Proximity is often used in graphical design, if text is grouped together we assume it is related. Be sure to use this. Here is an example of good use of proximity.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">marketing_data.<span style="color: black;">compute_quarterly</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
marketing_data.<span style="color: black;">compute_annual</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
marketing_data.<span style="color: black;">print_data</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
visitors_data.<span style="color: black;">compute_quarterly</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
visitors_data.<span style="color: black;">compute_annual</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
visitors_data.<span style="color: black;">print_data</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
sales_data.<span style="color: black;">compute_quarterly</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
sales_data.<span style="color: black;">compute_annual</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
sales_data.<span style="color: black;">print_data</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h3>Avoiding magic numbers</h3>
<p>All literal numeric values except 0 and 1 should be avoided. Instead you could use a variable with a descriptive name. This way code will be more readable and it will be easier to change later.<br />
Here is one trivial example.</p>
<table border="0" width="100%">
<tbody>
<tr>
<td width="50%" valign="top">Old code</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">x = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span></pre></div></div>

</td>
<td width="50%" valign="top">New code</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">x = <span style="color: #ff4500;">0</span>
SKIP_LINES = <span style="color: #ff4500;">5</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>SKIP_LINES<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span></pre></div></div>

</td>
</tr>
</tbody>
</table>
<p>This may seem unnecessary, but imagine a lot longer code with multiple occurrences of skip lines constant. This way you will avoid editing that number more than once if your code needs to be changed, and it&#8217;s much easier to understand a piece of code if variables with descriptive names are used.</p>
<h3>Logical expressions</h3>
<p>You can avoid writing big, multiple lines logical expressions by using boolean variables. Be sure to give descriptive names to those variables. Here is an example of how not to do it.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span>  <span style="color: black;">&#40;</span> post.<span style="color: black;">length</span> <span style="color: #66cc66;">&gt;</span> minimum_post_length <span style="color: #ff7700;font-weight:bold;">and</span> post.<span style="color: black;">length</span> <span style="color: #66cc66;">&lt;</span> maximum_post_length <span style="color: #ff7700;font-weight:bold;">and</span>
    post.<span style="color: black;">number_of_comments</span> == <span style="color: #ff4500;">0</span> <span style="color: black;">&#41;</span>:
    do_something<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>: do_something_else<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Now try to read if statement again, isn&#8217;t this better? And we really didn&#8217;t use much more memory.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">post_is_longer_than_minimum = post.<span style="color: black;">length</span> <span style="color: #66cc66;">&gt;</span> minimum_post_length
post_is_shorter_than_maximum = post.<span style="color: black;">length</span> <span style="color: #66cc66;">&lt;</span> maximum_post_length
post_has_no_comments = post.<span style="color: black;">number_of_comments</span> == <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> post_is_longer_than_minimum <span style="color: #ff7700;font-weight:bold;">and</span> post_is_shorter_than_maximum <span style="color: #ff7700;font-weight:bold;">and</span> post_has_no_comments:
    do_something<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>: do_something_else<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h3>Using multiple if branchings properly</h3>
<p>You should always put more common (or normal) cases first, and less common cases and exceptions later. This way normal program flow will be obvious.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">current_user = get_user<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> current_user:
    show_error<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> user_is_admin:
        show_admin_interface<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        show_normal_interface<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>You should first handle case when there was no errors when trying to get user. And assuming there are lot more normal users than admins you should check if user isn&#8217;t admin first.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">current_user = get_user<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> CurrentUser:
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> user_is_admin:
        show_normal_interface<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        show_admin_interface<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">else</span>:
    show_error<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h3>Conclusion</h3>
<p>So, that&#8217;s it! If you have any more ideas, or know better methods of cleaning python code feel free to comment.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<div class="d">
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://dabrorius.com/python-tutorials/tips-for-readable-python-code/&amp;title=Tips+for+readable+python+code" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://dabrorius.com/python-tutorials/tips-for-readable-python-code/&amp;title=Tips+for+readable+python+code" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Tips+for+readable+python+code&amp;url=http://dabrorius.com/python-tutorials/tips-for-readable-python-code/&amp;title=Tips+for+readable+python+code" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://dabrorius.com/python-tutorials/tips-for-readable-python-code/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://dabrorius.com/python-tutorials/tips-for-readable-python-code/&amp;title=Tips+for+readable+python+code" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://dabrorius.com/python-tutorials/tips-for-readable-python-code/&amp;title=Tips+for+readable+python+code" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://dabrorius.com/python-tutorials/tips-for-readable-python-code/&amp;title=Tips+for+readable+python+code" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://dabrorius.com/python-tutorials/tips-for-readable-python-code/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Tips+for+readable+python+code+@+http://dabrorius.com/python-tutorials/tips-for-readable-python-code/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://dabrorius.com/python-tutorials/tips-for-readable-python-code/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Override adding plural &#8220;s&#8221; in django admin</title>
		<link>http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/</link>
		<comments>http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 10:06:53 +0000</pubDate>
		<dc:creator>dabrorius</dc:creator>
				<category><![CDATA[Django tutorials]]></category>
		<category><![CDATA[django admin]]></category>

		<guid isPermaLink="false">http://dabrorius.com/?p=11</guid>
		<description><![CDATA[If you have non-english names for your models, they will look bad when django admin tries to make plural out of them by adding &#8220;s&#8221; in the end. Fortunately it is easy to override by defining &#8220;verbose_name_plural&#8221; in your model. Here is an example:

1
2
3
4
5
6
class Tag&#40;models.Model&#41;:
    class Meta:
      [...]]]></description>
			<content:encoded><![CDATA[<p>If you have non-english names for your models, they will look bad when django admin tries to make plural out of them by adding &#8220;s&#8221; in the end. Fortunately it is easy to override by defining &#8220;verbose_name_plural&#8221; in your model. Here is an example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> Tag<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">class</span> Meta:
        verbose_name_plural = <span style="color: #483d8b;">&quot;Tagovi&quot;</span>
&nbsp;
    ime = models.<span style="color: black;">CharField</span><span style="color: black;">&#40;</span>max_length=<span style="color: #483d8b;">&quot;300&quot;</span><span style="color: black;">&#41;</span>
    opis = models.<span style="color: black;">TextField</span><span style="color: black;">&#40;</span>blank = <span style="color: #008000;">True</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<div class="d">
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/&amp;title=Override+adding+plural+%26%238220%3Bs%26%238221%3B+in+django+admin" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/&amp;title=Override+adding+plural+%26%238220%3Bs%26%238221%3B+in+django+admin" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Override+adding+plural+%26%238220%3Bs%26%238221%3B+in+django+admin&amp;url=http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/&amp;title=Override+adding+plural+%26%238220%3Bs%26%238221%3B+in+django+admin" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/&amp;title=Override+adding+plural+%26%238220%3Bs%26%238221%3B+in+django+admin" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/&amp;title=Override+adding+plural+%26%238220%3Bs%26%238221%3B+in+django+admin" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/&amp;title=Override+adding+plural+%26%238220%3Bs%26%238221%3B+in+django+admin" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Override+adding+plural+%26%238220%3Bs%26%238221%3B+in+django+admin+@+http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://dabrorius.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://dabrorius.com/django-tutorials/override-adding-plural-s-in-django-admin/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

