<?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>Pedro Assunção &#187; code snipplets</title>
	<atom:link href="http://pedroassuncao.com/category/code-snipplets/feed/" rel="self" type="application/rss+xml" />
	<link>http://pedroassuncao.com</link>
	<description>Thoughts on technology, skydiving, life, and the universe...</description>
	<lastBuildDate>Mon, 02 Jan 2012 19:40:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Full list of Coca Cola brands &#8211; had no idea there were so many</title>
		<link>http://pedroassuncao.com/2011/02/full-list-of-coca-cola-companies-had-no-idea-there-were-so-many/</link>
		<comments>http://pedroassuncao.com/2011/02/full-list-of-coca-cola-companies-had-no-idea-there-were-so-many/#comments</comments>
		<pubDate>Thu, 24 Feb 2011 10:11:52 +0000</pubDate>
		<dc:creator>Pedro Assunção</dc:creator>
				<category><![CDATA[code snipplets]]></category>
		<category><![CDATA[computers]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[findings]]></category>
		<category><![CDATA[software development]]></category>

		<guid isPermaLink="false">http://pedroassuncao.com/2011/02/full-list-of-coca-cola-companies-had-no-idea-there-were-so-many/</guid>
		<description><![CDATA[As a part of my Ruby learning process, i decided to write something to retrieve the full list of brands that Coca Cola currently owns from their website. The list is split across multiple pages, but with some Ruby magic and a nice library called Hpricot to traverse the HTML it was 5 minutes of [...]


Related posts:<ol><li><a href='http://pedroassuncao.com/2008/09/coca-cola/' rel='bookmark' title='Coca cola'>Coca cola</a></li>
<li><a href='http://pedroassuncao.com/2011/01/idea-for-a-better-customer-satisfactionfeedback-service/' rel='bookmark' title='Idea for a better customer satisfaction/feedback service'>Idea for a better customer satisfaction/feedback service</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>As a part of my <a href="http://www.ruby-lang.org/en/">Ruby</a> learning process, i decided to write something to retrieve the <a href="http://www.thecoca-colacompany.com/brands/product_list_a.html">full list of brands that Coca Cola currently owns</a> from their website. The list is split across multiple pages, but with some Ruby magic and a nice library called <a href="https://github.com/hpricot/hpricot">Hpricot</a> to traverse the HTML it was 5 minutes of work <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here&#8217;s the script:</p>
<pre>require 'open-uri'
require 'rubygems'
require 'hpricot'
require 'pp'

my_file = File.new("cola.txt", 'w')

('a'..'z').each do |letter|
open("http://www.thecoca-colacompany.com/brands/product_list_#{letter}.html") do |f|

    # meta info
    #pp f.meta

    #pp 'Content-Type: ' + f.content_type
    #pp 'Last modified: ' + f.last_modified.to_s

    doc = Hpricot.XML(f)
    div = doc/"#products_link_list"
    (div/:ul).each do |ul|
      (ul/:li).each do |li|
        if (li/:a).first
          my_file.puts((li/:a).first.inner_html)
        else
          my_file.puts li.inner_html.to_s
        end
      end
    end

  end
end</pre>
<p>And here&#8217;s the full list of brands that belong to Coca Cola:</p>
<p>A&amp;W<br />
Acueducto<br />
Ades<br />
Agua Manantial<br />
Ali<br />
Alive<br />
Alpina<br />
Ambasa<br />
Ameyal<br />
Amorino<br />
Andina/Andina light<br />
Andina Fortified<br />
Andina Fresh<br />
Andina Frut<br />
Andina Nectar<br />
Apollinaris<br />
Aqua+<br />
Aquabona<br />
Aquactivede by Aquarius<br />
Aquana<br />
Aquarius<br />
Aquarius Active Diet<br />
BACARDI Mixers*<br />
BACARDI Premium Mixers*<br />
Bajoru Gira<br />
Bankia<br />
Barq&#8217;s<br />
Barrelitos<br />
BEAT<br />
Beautia<br />
Bebere<br />
Benedictino<br />
Beverly<br />
Bevi<br />
Bibo<br />
Big Crush<br />
Big Tai<br />
Bimbo<br />
Bimbo Break<br />
Biotime<br />
Bird&#8217;s Nest*<br />
Bistra<br />
Bistrone<br />
Bjare<br />
Blackfire<br />
Boco<br />
Bogadera<br />
Bom Bit Maesil<br />
BonAqua/BonAqa<br />
Borneo<br />
Botaniq 1QQ<br />
Botany<br />
BPM<br />
Brazzi<br />
Bright &amp; Early<br />
Brisa Agua Pura<br />
Brisa by Bonaqua<br />
Brisa con un Toque de Gas<br />
Brisa Spa<br />
Bu<br />
Bubbly<br />
burn<br />
buzz<br />
CAFÉ ZU<br />
caffeine-free Barq&#8217;s<br />
caffeine-free Coca-Cola<br />
caffeine-free Diet Coke/caffeine-free Coca-Cola light<br />
Calypso<br />
Canada Dry<br />
Canning&#8217;s<br />
Cappy<br />
Caprice<br />
Caribou Coffee*<br />
Carioca<br />
Cascal<br />
Carver&#8217;s<br />
Cepita<br />
Chaqwa<br />
Charrua<br />
Chaudfontaine<br />
Cheers<br />
cherry Coke<br />
cherry Coke zero<br />
Chinotto<br />
Chinotto light<br />
Ciel<br />
CITRA<br />
Club<br />
Coca-Cola<br />
Coca-Cola Black Cherry Vanilla<br />
Coca-Cola Blāk<br />
Coca-Cola C2<br />
Coca-Cola Citra<br />
Coca-Cola light/Diet Coke<br />
Coca-Cola with Lemon<br />
Coca-Cola with Lime<br />
Coca-Cola with Raspberry<br />
Coca-Cola Zero<br />
Cocoteen<br />
Cola Guaraná Jesus<br />
Country Club<br />
Cresta<br />
Cristal<br />
Crush<br />
Crusta<br />
Crystal<br />
Damla Minera<br />
DANNON*<br />
DASANI<br />
DASANI Active<br />
DASANI Balance<br />
DASANI Flavors<br />
DASANI Nutriwater<br />
DASANI Plus<br />
Del Valle<br />
Del Valle Frut<br />
Del Valle Frutsi<br />
Del Valle Frutsi Desarrollo<br />
Del Valle Kapo<br />
Del Valle Mais light<br />
Del Valle light<br />
Delaware Punch<br />
diet A&amp;W<br />
diet Andina Frut/Andina Frut light<br />
diet Andina Nectar/Andina Nectar light<br />
diet Barq&#8217;s<br />
diet Canada Dry<br />
Diet cherry Coke<br />
Diet Coke/Coca-Cola light<br />
Diet Coke Black Cherry Vanilla<br />
Diet Coke Citra/Coca-Cola light Citra<br />
Diet Coke Plus<br />
Diet Coke Sweetened with Splenda®<br />
Diet Coke with Lemon/Coca-Cola light with Lemon<br />
Diet Coke with Lime/Coca-Cola light with Lime<br />
Diet Coke with Raspberry<br />
Diet Crush<br />
diet Dr Pepper/Dr Pepper Zero<br />
diet Fanta/Fanta light/Fanta Zero/Fanta Free<br />
diet Freskyta<br />
diet INCA KOLA<br />
diet Kia Ora<br />
diet Krest<br />
diet Lift/Lift light<br />
diet Lilt/Lilt Zero<br />
diet Master Pour<br />
diet Mello Yello<br />
diet Mezzo Mix<br />
diet Minute Maid Soft Drink<br />
diet Nestea/Nestea light*<br />
diet Northern Neck<br />
diet Oasis<br />
diet Quatro/Quatro light<br />
diet Schweppes<br />
diet Seagram&#8217;s*<br />
diet Sprite/Sprite light/diet Sprite Zero/Sprite Zero<br />
diet Squirt<br />
diet Tai/Tai light<br />
diet Vanilla Coke<br />
Disney Xtreme Cooler*<br />
Diva<br />
Dobriy<br />
Dogadan<br />
Dorna<br />
Dr Pepper<br />
E2<br />
Earth and Sky<br />
Eight O&#8217;Clock<br />
El Rayek<br />
Enviga*<br />
Epika<br />
Escuis<br />
Eva Water<br />
Fanta<br />
Fanta Verdia<br />
Far Coast<br />
Felicia<br />
Finley<br />
Fioravanti<br />
Fire<br />
Five Alive<br />
Flavor Rage<br />
Fontana<br />
Fresca<br />
Fresca 1<br />
Frescolita<br />
Freskyta<br />
Fresquinha<br />
Fress<br />
Frestea*<br />
Friolin<br />
Frisco<br />
Frugos<br />
Frugos Fresh<br />
Fruit Solutions<br />
Fruitia<br />
Fruitier<br />
Fruitopia<br />
Fruktime<br />
Frutina<br />
Frutonic<br />
Full Throttle<br />
Full Throttle Blue Demon<br />
Full Throttle Sugar Free<br />
Funchum Orange Drink<br />
Furusato Dayori<br />
FUZE Healthy Infusions<br />
GEORGIA<br />
GEORGIA Club<br />
GEORGIA Gold<br />
GEORGIA Grande<br />
Gini<br />
Girios Gira<br />
glacéau smartwater<br />
glacéau vitaminwater<br />
glacéau vitaminwater zero<br />
Gladiator<br />
Godiva* Belgian Blends<br />
Gold Peak<br />
Gold Spot<br />
Golden Crush<br />
Goombay Punch<br />
Goulburn Valley<br />
Grapette<br />
Groovy<br />
Guaraná Kuat light<br />
Guaraná Kuat Zero<br />
Hajime<br />
Haru No Mint Shukan<br />
Hawai<br />
Healthworks<br />
Heppinger<br />
Hero<br />
Hi Spot<br />
Hi-C<br />
Hires<br />
Hit<br />
Horizon<br />
Hot Point<br />
Huang<br />
Ice Dew<br />
Ice Dew 3+<br />
Ice Tea Leao<br />
illy issimo*<br />
INCA KOLA<br />
Ipsei<br />
Izvorul Alb<br />
Jaz Cola<br />
Jericho<br />
Jet Tonic<br />
Jolly Juice<br />
Joy<br />
Joya<br />
Juan Valdez<br />
Juices To Go<br />
Just Juice<br />
Kani<br />
Kapo<br />
Kapo Zero<br />
Karada Meguri-Cha<br />
Kashaya Tea<br />
Keloco<br />
Keri<br />
Kia Ora<br />
Kildevaeld<br />
Kilimanjaro<br />
Kin<br />
Kinley<br />
Kist<br />
Kiwi Blue<br />
Kochakaden<br />
Kola Inglesa<br />
Koumisoukai<br />
Krest<br />
Kristal<br />
Kropla Beskidu<br />
Krushka &amp; Bochka<br />
Kuat<br />
Kuat Guaraná<br />
Kuat light<br />
Kuli<br />
Kyun<br />
La Jolla<br />
La Vuelta<br />
Lanitis<br />
Lanitis Extra<br />
Lanitis Filaraki<br />
Leao Green Tea<br />
Leao Guaraná Power<br />
Leao Iced Tea<br />
Less Is More, Kinley<br />
Lift<br />
Lift Plus<br />
Lift Plus light<br />
Lilia<br />
Lilt<br />
Limca<br />
Limelite<br />
Limonade<br />
Linnuse<br />
Lion<br />
Love Body<br />
Maaza<br />
Mad River<br />
Malvern<br />
Manantial<br />
Mare Rosso<br />
Marocha<br />
Master Chill<br />
Master Pour<br />
Matte Leao<br />
Matusov Pramen<br />
Mazoe<br />
Mello<br />
Mello Yello<br />
Mer<br />
Mezzo<br />
Mezzo Mix<br />
Miami<br />
Mickey*<br />
Migoro-Nomigoro<br />
Minaqua<br />
Mineragua<br />
Minute Maid<br />
Minute Maid Active<br />
Minute Maid Antiox<br />
Minute Maid Deli<br />
Minute Maid Duofrutas<br />
Minute Maid Fruit Plus<br />
Minute Maid Heart Wise<br />
Minute Maid Just 10<br />
Minute Maid Light<br />
Minute Maid Mais<br />
Minute Maid Nutri+<br />
Minute Maid Premium<br />
Minute Maid Soft Drink<br />
Minute Maid Splash<br />
Mireille<br />
Mission<br />
Mone<br />
Montefiore<br />
Mori No Mizudayori<br />
Morning Deli<br />
Mother<br />
Mr. Pibb<br />
multiVita<br />
Nada<br />
Nagomi<br />
Nalu<br />
Namthip<br />
Nanairo Acha<br />
Naturaqua<br />
Natures Own*<br />
Nectarin<br />
Nestea*<br />
Nestea Choglit*<br />
Nestea COOL*<br />
Nestea Viteao*<br />
Nevada<br />
Neverfail<br />
Next<br />
Nico<br />
Nordic Mist<br />
Northern Neck<br />
NOS<br />
Nos Entendemos<br />
Oasis<br />
Odwalla<br />
Olimpija<br />
Orchy<br />
Paani Treated Water<br />
Pacific Orchard<br />
Pampa<br />
Paradise Sun<br />
Parle<br />
Pasazade<br />
Pearona<br />
Peats Ridge Springs<br />
Pibb Xtra<br />
Pibb Zero<br />
Piko<br />
Pilskania<br />
Planet Java*<br />
Play Energy Drink<br />
Pocket Dr<br />
Poiana Negri<br />
Polar Brew<br />
Poms<br />
Ponkana<br />
Pop<br />
Portello<br />
POWERADE<br />
POWERADE Advance<br />
POWERADE alive<br />
POWERADE aqua+<br />
POWERADE Balance<br />
POWERADE light<br />
POWERADE OPTION<br />
POWERADE Zero<br />
Powerplay<br />
Premio<br />
Presta<br />
Presta light<br />
Pulp Orange<br />
Pulpy<br />
Pump<br />
Pumped Enhanced Hydration<br />
Pura Sol<br />
Qoo<br />
Quatro<br />
Quwat Jabal<br />
R&#8217;fresh<br />
Ramlosa<br />
Real<br />
Real Leaf<br />
Real Gold<br />
Rehab<br />
Relentless<br />
Rich<br />
Richy<br />
Riwa<br />
Roemerquelle<br />
Rosa<br />
Rosalta<br />
Roses<br />
Royal Tru<br />
Royal Tru light<br />
Safety First<br />
Safia<br />
Sahtain<br />
Samantha<br />
Samurai<br />
San Luis<br />
Sarsi<br />
Saryusaisai<br />
Schuss<br />
Schweppes<br />
Schweppes Abbey Well<br />
Seagram&#8217;s*<br />
Seltz<br />
Sensation<br />
Sensun Gazoz<br />
SENZAO<br />
Shandy<br />
Shangri-La<br />
ShiZen*<br />
Shock<br />
Signature<br />
Sim<br />
Simba<br />
Simply Apple<br />
Simply Grapefruit<br />
Simply Lemonade<br />
Simply Limeade<br />
Simply Orange<br />
Smart<br />
Sobo<br />
Sokenbicha<br />
Solo<br />
Sonfil<br />
Soonsoo 100<br />
Southern Sun<br />
Sparkle<br />
Sparletta<br />
Sparletta Iron Brew<br />
Splash<br />
Splice<br />
Sport<br />
Sprite<br />
Sprite 3G<br />
Sprite Duo<br />
Sprite Flavors<br />
Sprite Ice<br />
Sprite Remix<br />
Sprite Zero/diet Sprite/Sprite light /diet Sprite Zero<br />
Spur<br />
Squirt<br />
Stoney Gold Ginger Beer<br />
Sucos Mais<br />
Sunfill<br />
SURGE<br />
Svali<br />
Sveva<br />
Sweecha<br />
TaB<br />
TaB energy<br />
TaB X-Tra<br />
TADAS<br />
Tai<br />
The Spirit of Georgia<br />
The Wellness From Coca-Cola<br />
Thextons<br />
Thums Up<br />
Tian Yu Di/Heaven and Earth<br />
Tiky<br />
Toppur<br />
Top&#8217;s<br />
Tropi<br />
Tropical<br />
Turkuaz<br />
Ultra<br />
Urge<br />
Urun<br />
V Florida 7<br />
Valle Frut<br />
Valpre<br />
Valser<br />
Valser Viva<br />
Vanilla Coke<br />
Vanilla Coke Zero<br />
VAULT<br />
VAULT Zero<br />
Vegitabeta<br />
VICA<br />
Vio<br />
Vita<br />
Vital<br />
Wilkins<br />
Wink<br />
Winnie the Pooh*<br />
Yangguang<br />
Yangguang Juicy T<br />
Yo Conozco a Hugo<br />
Yoli<br />
Youki<br />
Yuan Ye</p>
<div class="shr-publisher-1221"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2011%2F02%2Ffull-list-of-coca-cola-companies-had-no-idea-there-were-so-many%2F' data-shr_title='Full+list+of+Coca+Cola+brands+-+had+no+idea+there+were+so+many'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2011%2F02%2Ffull-list-of-coca-cola-companies-had-no-idea-there-were-so-many%2F' data-shr_title='Full+list+of+Coca+Cola+brands+-+had+no+idea+there+were+so+many'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->

<p>Related posts:<ol><li><a href='http://pedroassuncao.com/2008/09/coca-cola/' rel='bookmark' title='Coca cola'>Coca cola</a></li>
<li><a href='http://pedroassuncao.com/2011/01/idea-for-a-better-customer-satisfactionfeedback-service/' rel='bookmark' title='Idea for a better customer satisfaction/feedback service'>Idea for a better customer satisfaction/feedback service</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://pedroassuncao.com/2011/02/full-list-of-coca-cola-companies-had-no-idea-there-were-so-many/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Irony of searches and one iPad visit to my blog</title>
		<link>http://pedroassuncao.com/2010/04/irony-of-searches-and-one-ipad-visit-to-my-blog/</link>
		<comments>http://pedroassuncao.com/2010/04/irony-of-searches-and-one-ipad-visit-to-my-blog/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 10:47:46 +0000</pubDate>
		<dc:creator>Pedro Assunção</dc:creator>
				<category><![CDATA[apple]]></category>
		<category><![CDATA[blogging]]></category>
		<category><![CDATA[code snipplets]]></category>
		<category><![CDATA[irony]]></category>
		<category><![CDATA[jokes]]></category>

		<guid isPermaLink="false">http://diffract.me/?p=976</guid>
		<description><![CDATA[Just a couple of pictures this time. The first one is about something i noticed i was doing, while searching for a code fragment. One could call it ironic, trying to find &#8220;find&#8221; The other is proof of my amazement by the fact that there was one person who visited my blog using an iPad [...]


Related posts:<ol><li><a href='http://pedroassuncao.com/2010/01/things-i-wanted-in-apples-ipad-tablet/' rel='bookmark' title='Things i wanted in Apple&#8217;s iPad (tablet)'>Things i wanted in Apple&#8217;s iPad (tablet)</a></li>
<li><a href='http://pedroassuncao.com/2010/04/ipad-jailbroken-one-day-after-coming-out-that-was-fast/' rel='bookmark' title='iPad jailbroken one day after coming out. That was fast.'>iPad jailbroken one day after coming out. That was fast.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Just a couple of pictures this time.</p>
<p>The first one is about something i noticed i was doing, while searching for a code fragment. One could call it ironic, trying to find &#8220;find&#8221; <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://diffract.me/wp-content/uploads/2010/04/Screen-shot-2010-04-21-at-10.26.11-AM.png"><img class="alignnone size-medium wp-image-978" title="Looking for find" src="http://diffract.me/wp-content/uploads/2010/04/Screen-shot-2010-04-21-at-10.26.11-AM-300x165.png" alt="" width="300" height="165" /></a></p>
<p>The other is proof of my amazement by the fact that there was one person who visited my blog using an iPad <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><a href="http://diffract.me/wp-content/uploads/2010/04/Screen-shot-2010-04-21-at-11.41.41-AM.png"><img class="alignnone size-medium wp-image-977" title="One iPad visit, woohooo" src="http://diffract.me/wp-content/uploads/2010/04/Screen-shot-2010-04-21-at-11.41.41-AM-300x144.png" alt="" width="300" height="144" /></a></p>
<p><strong>UPDATE</strong>: Turns out i was looking at a 2 day period only. This month there have been actually 4 iPad visits, hehe <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://diffract.me/wp-content/uploads/2010/04/Screen-shot-2010-04-21-at-11.50.11-AM.png"><img class="alignnone size-medium wp-image-980" title="4 iPad visits after all" src="http://diffract.me/wp-content/uploads/2010/04/Screen-shot-2010-04-21-at-11.50.11-AM-300x84.png" alt="" width="300" height="84" /></a></p>
<div class="shr-publisher-976"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2010%2F04%2Firony-of-searches-and-one-ipad-visit-to-my-blog%2F' data-shr_title='Irony+of+searches+and+one+iPad+visit+to+my+blog'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2010%2F04%2Firony-of-searches-and-one-ipad-visit-to-my-blog%2F' data-shr_title='Irony+of+searches+and+one+iPad+visit+to+my+blog'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->

<p>Related posts:<ol><li><a href='http://pedroassuncao.com/2010/01/things-i-wanted-in-apples-ipad-tablet/' rel='bookmark' title='Things i wanted in Apple&#8217;s iPad (tablet)'>Things i wanted in Apple&#8217;s iPad (tablet)</a></li>
<li><a href='http://pedroassuncao.com/2010/04/ipad-jailbroken-one-day-after-coming-out-that-was-fast/' rel='bookmark' title='iPad jailbroken one day after coming out. That was fast.'>iPad jailbroken one day after coming out. That was fast.</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://pedroassuncao.com/2010/04/irony-of-searches-and-one-ipad-visit-to-my-blog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Websockets tutorial/example with pywebsocket</title>
		<link>http://pedroassuncao.com/2009/12/websockets-tutorialexample-with-pywebsocket/</link>
		<comments>http://pedroassuncao.com/2009/12/websockets-tutorialexample-with-pywebsocket/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 12:37:24 +0000</pubDate>
		<dc:creator>Pedro Assunção</dc:creator>
				<category><![CDATA[code snipplets]]></category>
		<category><![CDATA[computers]]></category>
		<category><![CDATA[experiments]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[tutorials]]></category>

		<guid isPermaLink="false">http://diffract.me/?p=634</guid>
		<description><![CDATA[As everyone already knows, Google Chrome now supports websockets. In essence, this allows you to keep a connection open with a webserver indefinitely (analogous to typical sockets) and send data bi-directionally. Unfortunately Chrome is the only browser currently supporting this, but I&#8217;m pretty sure this will change. So I decided to give this a try [...]


Related posts:<ol><li><a href='http://pedroassuncao.com/2010/01/my-small-pywebsocket-tutorial/' rel='bookmark' title='My small pywebsocket tutorial &#8230;'>My small pywebsocket tutorial &#8230;</a></li>
<li><a href='http://pedroassuncao.com/2010/10/java-web-start-jnlp-simple-example/' rel='bookmark' title='Java Web Start (jnlp) simple example'>Java Web Start (jnlp) simple example</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>As everyone already knows, <a href="http://www.google.com/chrome">Google Chrome</a> now supports <a href="http://dev.w3.org/html5/websockets/#websocket">websockets</a>. In essence, this allows you to keep a connection open with a webserver indefinitely (analogous to typical sockets) and send data bi-directionally. Unfortunately Chrome is the only browser currently supporting this, but I&#8217;m pretty sure this will change.</p>
<p>So I decided to give this a try and experiment a bit with it. This is my step by step process on getting a web page opening a websocket to a server and receiving the server&#8217;s date and time every second. It is based on an <a href="http://armstrongonsoftware.blogspot.com/2009/12/comet-is-dead-long-live-websockets.html">article by Joe Armstrong</a>, though he uses <a href="http://ftp.sunet.se/pub/lang/erlang/">Erlang</a> for the server, while I decided to follow the easy road and use <a href="http://code.google.com/p/pywebsocket/">Google&#8217;s pywebsocket</a> &#8211; an apache module (uses mod_python) that allows you to create handlers for websocket connections in a easy-to-use fashion. The code also contains a way to start a standalone server (i.e. not requiring apache) for testing purposes.</p>
<p>So here are the steps I took to get this working:</p>
<h4>1. Create a web page</h4>
<p>This is the code I borrowed from Joe, though slightly modified to fit my purposes (it requires <a href="http://jquery.com/">jquery</a>, by the way):</p>
<pre>&lt;html&gt;

&lt;head&gt;

&lt;script src="jquery-1.3.2.min.js"&gt;&lt;/script&gt;
&lt;script&gt;

$(document).ready(function(){

var ws;

if ("WebSocket" in window) {
debug("Horray you have web sockets. Trying to connect...");
ws = new WebSocket("ws://localhost:9998/echo");

ws.onopen = function() {
// Web Socket is connected. You can send data by send() method.
debug("connected...");
ws.send("hello from the browser");
ws.send("more from browser");
};

run = function() {
var val=$("#i1").val(); // read the entry
$("#i1").val("");       // and clear it
ws.send(val);           // tell erlang
return true;            // must do this
};

ws.onmessage = function (evt)
{
//alert(evt.data);
var data = evt.data;
var i = data.indexOf("!");
var tag = data.slice(0,i);
var val = data.slice(i+1);
$("#" + tag).html(val);
};

ws.onclose = function()
{
debug(" socket closed");
};
} else {
alert("You have no web sockets");
};

function debug(str){
$("#debug").append("&lt;p&gt;" +  str);
};

});
&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;

&lt;h1&gt;Interaction experiment&lt;/h1&gt;

&lt;h2&gt;Debug&lt;/h2&gt;
&lt;div id="debug"&gt;&lt;/div&gt;

&lt;fieldset&gt;
&lt;legend&gt;Clock&lt;/legend&gt;
&lt;div id="clock"&gt;I am a clock&lt;/div&gt;
&lt;/fieldset&gt;

&lt;/body&gt;

&lt;/html&gt;</pre>
<h4>2. Download and install pywebsocket</h4>
<p>Checkout the code with</p>
<pre><span>svn checkout http</span><span>:</span><span>//pywebsocket.googlecode.com/svn/trunk/ pywebsocket-read-only
</span></pre>
<p>Then do <strong>python setup.py build</strong> and <strong>sudo python setup.py install</strong> inside the <strong>src</strong> folder. This will install it into your python environment.</p>
<h4>3. Being lazy, means we will change an example handler</h4>
<p>The way pywebsocket works is delegating the connections to something they call handlers. In the <strong><span>pywebsocket-read-only/src/example</span></strong><span> folder you will find a file named <strong>echo_wsh.py</strong>. They have this convention where handlers are named <strong>&lt;entry_point&gt;_wsh.py</strong>. This means that when you later call (from your web page) the url <strong>http://localhost:9998/echo</strong> the server will delegate the processing of that connection to that file.</span></p>
<p><span>I modified that file to something like this:</span></p>
<pre># Copyright 2009, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

from mod_pywebsocket import msgutil
from datetime import datetime
import time

_GOODBYE_MESSAGE = 'Goodbye'

def web_socket_do_extra_handshake(request):
 print 'Connected.'
 pass  # Always accept.

def web_socket_transfer_data(request):
 while True:
 time.sleep(1)
 date = datetime.now()
 #try:
 #    line = msgutil.receive_message(request)
 #except Exception, e:
 #    print 'Foi com os porcos'
 #    raise e
 #print 'Got something: %s' % line
 #msgutil.send_message(request, line)
 msgutil.send_message(request, 'clock!%s' % date)
 #if line == _GOODBYE_MESSAGE:
 #    return</pre>
<p>So basically whenever a new connection is made to this entry point, a call to <strong>web_socket_do_extra_handshake</strong> is made. After that, <strong>web_socket_transfer_data</strong> is called and it&#8217;s your responsibility to create the loop that receives messages and handles the flow (as you can see in the commented lines). I don&#8217;t care about that right now, since I only want to push the date and time to the client every second.</p>
<h4>3. Start the standalone server</h4>
<p>Go to the <strong><span>pywebsocket-read-only/src/mod_pywebsocket</span></strong> folder and run the following command:</p>
<pre>sudo python standalone.py -p 9998 -w ../example/</pre>
<p>This will start the server in port 9998 and use the handlers directory specified by the -w option. That is where our echo_wsh.py lives.</p>
<h4>4. Test it <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </h4>
<p>So now open your browser (remember, only chrome supports websockets currently) and open the html file your created in the beginning. Voilá, server&#8217;s date and time every second in the clock div.</p>
<div class="shr-publisher-634"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F12%2Fwebsockets-tutorialexample-with-pywebsocket%2F' data-shr_title='Websockets+tutorial%2Fexample+with+pywebsocket'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F12%2Fwebsockets-tutorialexample-with-pywebsocket%2F' data-shr_title='Websockets+tutorial%2Fexample+with+pywebsocket'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->

<p>Related posts:<ol><li><a href='http://pedroassuncao.com/2010/01/my-small-pywebsocket-tutorial/' rel='bookmark' title='My small pywebsocket tutorial &#8230;'>My small pywebsocket tutorial &#8230;</a></li>
<li><a href='http://pedroassuncao.com/2010/10/java-web-start-jnlp-simple-example/' rel='bookmark' title='Java Web Start (jnlp) simple example'>Java Web Start (jnlp) simple example</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://pedroassuncao.com/2009/12/websockets-tutorialexample-with-pywebsocket/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>Android location provider mock</title>
		<link>http://pedroassuncao.com/2009/11/android-location-provider-mock/</link>
		<comments>http://pedroassuncao.com/2009/11/android-location-provider-mock/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 15:50:42 +0000</pubDate>
		<dc:creator>Pedro Assunção</dc:creator>
				<category><![CDATA[code snipplets]]></category>
		<category><![CDATA[computers]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://diffract.me/?p=578</guid>
		<description><![CDATA[So, yeah, I resumed playing around with android, this time version 2.0. I&#8217;m really tempted to buy the new Motorola Milestone that should come out in Europe sometime between&#8230; now&#8230; and early next year, so I wanna be ready to create all the crazy stuff I have in mind for it One of the things [...]


Related posts:<ol><li><a href='http://pedroassuncao.com/2009/07/ssh-login-without-password-authorized_keys/' rel='bookmark' title='SSH login without password (authorized_keys)'>SSH login without password (authorized_keys)</a></li>
<li><a href='http://pedroassuncao.com/2009/01/double-clicktap-detection-on-androids-mapview/' rel='bookmark' title='Double click/tap detection on android&#8217;s MapView'>Double click/tap detection on android&#8217;s MapView</a></li>
<li><a href='http://pedroassuncao.com/video/' rel='bookmark' title='Video'>Video</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>So, yeah, I resumed playing around with android, this time version 2.0.</p>
<p>I&#8217;m really tempted to buy the new <a href="http://developer.motorola.com/products/milestone/">Motorola Milestone</a> that should come out in Europe sometime between&#8230; now&#8230; and early next year, so I wanna be ready to create all the crazy stuff I have in mind for it <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>One of the things I noticed was that it&#8217;s not that straightforward to provide sample GPS data. Somehow the emulator&#8217;s GPX and KMZ functionality is not working fine for me and, even if it was, I don&#8217;t want to load the file everytime (i.e. I&#8217;m lazy).</p>
<p>So, after some googling around, here&#8217;s my solution to read points from a file and feed them 1 per second to the location manager, so I can finally work with them in my app:</p>
<p>My main activity implements <strong>LocationListener</strong>, so it can be passed to the <strong>LocationManager</strong> to receive GPS events. Here&#8217;s what I do when creating my activity:</p>
<pre>public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		((TextView) this.findViewById(R.id.textView)).setText("Something else");

		// LocationManager locationManager = (LocationManager)
		// getSystemService(Context.LOCATION_SERVICE);
		// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
		// 0, 0, this);

		LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		String mocLocationProvider = LocationManager.GPS_PROVIDER;
		locationManager.addTestProvider(mocLocationProvider, false, false,
				false, false, true, true, true, 0, 5);
		locationManager.setTestProviderEnabled(mocLocationProvider, true);
		locationManager.requestLocationUpdates(mocLocationProvider, 0, 0, this);

		try {

			List data = new ArrayList();
			InputStream is = getAssets().open("data.txt");
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			String line = null;
			while ((line = reader.readLine()) != null) {

				data.add(line);
			}
			Log.e(LOG_TAG, data.size() + " lines");

			new MockLocationProvider(locationManager, mocLocationProvider, data).start();

		} catch (IOException e) {

			e.printStackTrace();
		}
	}</pre>
<p>This will basically setup the test location provider and read the points into a list. Then I feed that stuff to my mock location provider (just a normal thread) that will read them 1 per second and trigger the new location back to this activity. Here&#8217;s the code for <strong>MockLocationProvider</strong>:</p>
<pre>public class MockLocationProvider extends Thread {

    private List data;

    private LocationManager locationManager;

    private String mocLocationProvider;

    private String LOG_TAG = "faren";

    public MockLocationProvider(LocationManager locationManager,
            String mocLocationProvider, List data) throws IOException {

        this.locationManager = locationManager;
        this.mocLocationProvider = mocLocationProvider;
        this.data = data;
    }

    @Override
    public void run() {

        for (String str : data) {

            try {

                Thread.sleep(1000);

            } catch (InterruptedException e) {

                e.printStackTrace();
            }

            // Set one position
            String[] parts = str.split(",");
            Double latitude = Double.valueOf(parts[0]);
            Double longitude = Double.valueOf(parts[1]);
            Double altitude = Double.valueOf(parts[2]);
            Location location = new Location(mocLocationProvider);
            location.setLatitude(latitude);
            location.setLongitude(longitude);
            location.setAltitude(altitude);

            Log.e(LOG_TAG, location.toString());

            // set the time in the location. If the time on this location
            // matches the time on the one in the previous set call, it will be
            // ignored
            location.setTime(System.currentTimeMillis());

            locationManager.setTestProviderLocation(mocLocationProvider,
                    location);
        }
    }
}</pre>
<p>Notice the <em><strong>location.setTime()</strong></em> call. Read the comment why it is necessary. Took me forever to find this one in google <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>Peace and great Androiding <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<div class="shr-publisher-578"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F11%2Fandroid-location-provider-mock%2F' data-shr_title='Android+location+provider+mock'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F11%2Fandroid-location-provider-mock%2F' data-shr_title='Android+location+provider+mock'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->

<p>Related posts:<ol><li><a href='http://pedroassuncao.com/2009/07/ssh-login-without-password-authorized_keys/' rel='bookmark' title='SSH login without password (authorized_keys)'>SSH login without password (authorized_keys)</a></li>
<li><a href='http://pedroassuncao.com/2009/01/double-clicktap-detection-on-androids-mapview/' rel='bookmark' title='Double click/tap detection on android&#8217;s MapView'>Double click/tap detection on android&#8217;s MapView</a></li>
<li><a href='http://pedroassuncao.com/video/' rel='bookmark' title='Video'>Video</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://pedroassuncao.com/2009/11/android-location-provider-mock/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Grabbing title tag from web page</title>
		<link>http://pedroassuncao.com/2009/10/grabbing-title-tag-from-web-page/</link>
		<comments>http://pedroassuncao.com/2009/10/grabbing-title-tag-from-web-page/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 13:16:24 +0000</pubDate>
		<dc:creator>Pedro Assunção</dc:creator>
				<category><![CDATA[code snipplets]]></category>
		<category><![CDATA[computers]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://diffract.me/?p=433</guid>
		<description><![CDATA[At least a couple of options, the first using BeautifulSoup: import urllib import BeautifulSoup soup = BeautifulSoup.BeautifulSoup(urllib.urlopen("https://www.google.com")) print soup.title.string And the second one using lxml: import lxml.html t = lxml.html.parse(url) print t.find(".//title").text Related posts:Several years of email statistics &#8211; code included Java HTTP proxy servlet (with Spring)


Related posts:<ol><li><a href='http://pedroassuncao.com/2009/07/several-years-of-email-statistics-code-included/' rel='bookmark' title='Several years of email statistics &#8211; code included'>Several years of email statistics &#8211; code included</a></li>
<li><a href='http://pedroassuncao.com/2009/08/java-http-proxy-servlet-with-spring/' rel='bookmark' title='Java HTTP proxy servlet (with Spring)'>Java HTTP proxy servlet (with Spring)</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>At least a couple of options, the first using <a href="http://www.crummy.com/software/BeautifulSoup/">BeautifulSoup</a>:</p>
<pre>import urllib
import BeautifulSoup

soup = BeautifulSoup.BeautifulSoup(urllib.urlopen("https://www.google.com"))
print soup.title.string</pre>
<p>And the second one using <a href="http://codespeak.net/lxml/">lxml</a>:</p>
<pre>import lxml.html
t = lxml.html.parse(url)
print t.find(".//title").text</pre>
<div class="shr-publisher-433"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F10%2Fgrabbing-title-tag-from-web-page%2F' data-shr_title='Grabbing+title+tag+from+web+page'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F10%2Fgrabbing-title-tag-from-web-page%2F' data-shr_title='Grabbing+title+tag+from+web+page'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->

<p>Related posts:<ol><li><a href='http://pedroassuncao.com/2009/07/several-years-of-email-statistics-code-included/' rel='bookmark' title='Several years of email statistics &#8211; code included'>Several years of email statistics &#8211; code included</a></li>
<li><a href='http://pedroassuncao.com/2009/08/java-http-proxy-servlet-with-spring/' rel='bookmark' title='Java HTTP proxy servlet (with Spring)'>Java HTTP proxy servlet (with Spring)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://pedroassuncao.com/2009/10/grabbing-title-tag-from-web-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django template filter: Show list of objects as table with fixed number of columns</title>
		<link>http://pedroassuncao.com/2009/09/django-template-filter-show-list-of-objects-as-table-with-fixed-number-of-columns/</link>
		<comments>http://pedroassuncao.com/2009/09/django-template-filter-show-list-of-objects-as-table-with-fixed-number-of-columns/#comments</comments>
		<pubDate>Wed, 30 Sep 2009 12:14:41 +0000</pubDate>
		<dc:creator>Pedro Assunção</dc:creator>
				<category><![CDATA[code snipplets]]></category>
		<category><![CDATA[computers]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://diffract.me/?p=414</guid>
		<description><![CDATA[I recently ran into the following problem: I needed to be able to display a list of users in a table that had a maximum of X columns. Since I could not find the solution on the Internet I decided to give it a try and here is my resulting template filter to do it: [...]


Related posts:<ol><li><a href='http://pedroassuncao.com/2009/08/django-dynamic-redirection-after-login/' rel='bookmark' title='[Django] Dynamic redirection after login'>[Django] Dynamic redirection after login</a></li>
<li><a href='http://pedroassuncao.com/2009/08/django-reverse-http-redirect-with-parameters/' rel='bookmark' title='Django: Reverse HTTP redirect with parameters'>Django: Reverse HTTP redirect with parameters</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>I recently ran into the following problem: I needed to be able to display a list of users in a table that had a maximum of X columns. Since I could not find the solution on the Internet I decided to give it a try and here is my resulting template filter to do it:</p>
<pre>def tablecols(data, cols):
    rows = []
    row = []
    index = 0
    for user in data:
        row.append(user)
        index = index + 1
        if index % cols == 0:
            rows.append(row)
            row = []
    # Still stuff missing?
    if len(row) &gt; 0:
        rows.append(row)
    return rows
register.filter_function(tablecols)</pre>
<p>Then you can use it in your templates like so:</p>
<pre>&lt;table&gt;
{% for row in members|tablecols:5 %}
    &lt;tr&gt;
    {% for member in row %}
        &lt;td&gt;
            {% show_simple_profile member user %}
        &lt;/td&gt;
    {% endfor %}
    &lt;/tr&gt;
{% endfor %}
&lt;/table&gt;</pre>
<p>It will break down the &#8220;members&#8221; list into a list of lists, each of the first being a group of (in this case) 5 users max.</p>
<p>Have fun <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="shr-publisher-414"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F09%2Fdjango-template-filter-show-list-of-objects-as-table-with-fixed-number-of-columns%2F' data-shr_title='Django+template+filter%3A+Show+list+of+objects+as+table+with+fixed+number+of+columns'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F09%2Fdjango-template-filter-show-list-of-objects-as-table-with-fixed-number-of-columns%2F' data-shr_title='Django+template+filter%3A+Show+list+of+objects+as+table+with+fixed+number+of+columns'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->

<p>Related posts:<ol><li><a href='http://pedroassuncao.com/2009/08/django-dynamic-redirection-after-login/' rel='bookmark' title='[Django] Dynamic redirection after login'>[Django] Dynamic redirection after login</a></li>
<li><a href='http://pedroassuncao.com/2009/08/django-reverse-http-redirect-with-parameters/' rel='bookmark' title='Django: Reverse HTTP redirect with parameters'>Django: Reverse HTTP redirect with parameters</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://pedroassuncao.com/2009/09/django-template-filter-show-list-of-objects-as-table-with-fixed-number-of-columns/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Python: Sort list of tuples by second item</title>
		<link>http://pedroassuncao.com/2009/09/sort-list-of-tuples-by-second-item/</link>
		<comments>http://pedroassuncao.com/2009/09/sort-list-of-tuples-by-second-item/#comments</comments>
		<pubDate>Wed, 16 Sep 2009 11:38:01 +0000</pubDate>
		<dc:creator>Pedro Assunção</dc:creator>
				<category><![CDATA[code snipplets]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://diffract.me/?p=401</guid>
		<description><![CDATA[Straight from the PythonInfo Wiki: &#62;&#62;&#62; import operator &#62;&#62;&#62; L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)] &#62;&#62;&#62; map(operator.itemgetter(0), L) ['c', 'd', 'a', 'b'] &#62;&#62;&#62; map(operator.itemgetter(1), L) [2, 1, 4, 3] &#62;&#62;&#62; sorted(L, key=operator.itemgetter(1)) [('d', 1), ('c', 2), ('b', 3), ('a', 4)] Related posts:JIRA issues reporting with Python Django template filter: Show list [...]


Related posts:<ol><li><a href='http://pedroassuncao.com/2009/08/jira-issues-reporting-with-python/' rel='bookmark' title='JIRA issues reporting with Python'>JIRA issues reporting with Python</a></li>
<li><a href='http://pedroassuncao.com/2009/09/django-template-filter-show-list-of-objects-as-table-with-fixed-number-of-columns/' rel='bookmark' title='Django template filter: Show list of objects as table with fixed number of columns'>Django template filter: Show list of objects as table with fixed number of columns</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Straight from the <a href="http://wiki.python.org/moin/HowTo/Sorting">PythonInfo Wiki</a>:</p>
<pre>&gt;&gt;&gt; import operator
&gt;&gt;&gt; L = [('c', 2), ('d', 1), ('a', 4), ('b', 3)]
&gt;&gt;&gt; map(operator.itemgetter(0), L)
['c', 'd', 'a', 'b']
&gt;&gt;&gt; map(operator.itemgetter(1), L)
[2, 1, 4, 3]
&gt;&gt;&gt; sorted(L, key=operator.itemgetter(1))
[('d', 1), ('c', 2), ('b', 3), ('a', 4)]</pre>
<div class="shr-publisher-401"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F09%2Fsort-list-of-tuples-by-second-item%2F' data-shr_title='Python%3A+Sort+list+of+tuples+by+second+item'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F09%2Fsort-list-of-tuples-by-second-item%2F' data-shr_title='Python%3A+Sort+list+of+tuples+by+second+item'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->

<p>Related posts:<ol><li><a href='http://pedroassuncao.com/2009/08/jira-issues-reporting-with-python/' rel='bookmark' title='JIRA issues reporting with Python'>JIRA issues reporting with Python</a></li>
<li><a href='http://pedroassuncao.com/2009/09/django-template-filter-show-list-of-objects-as-table-with-fixed-number-of-columns/' rel='bookmark' title='Django template filter: Show list of objects as table with fixed number of columns'>Django template filter: Show list of objects as table with fixed number of columns</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://pedroassuncao.com/2009/09/sort-list-of-tuples-by-second-item/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Django: Reverse HTTP redirect with parameters</title>
		<link>http://pedroassuncao.com/2009/08/django-reverse-http-redirect-with-parameters/</link>
		<comments>http://pedroassuncao.com/2009/08/django-reverse-http-redirect-with-parameters/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 10:08:52 +0000</pubDate>
		<dc:creator>Pedro Assunção</dc:creator>
				<category><![CDATA[code snipplets]]></category>
		<category><![CDATA[computers]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://diffract.me/?p=376</guid>
		<description><![CDATA[Here&#8217;s how to, from a view, redirect to another URL passing parameters to it. For instance, to redirect the user to a certain page after login: return HttpResponseRedirect(reverse('dz_details', kwargs={'dz_id':dz.id})) This will lookup the &#8216;dz_details&#8217; name in your urls.py file, which could be defined like so: url(r'^details/(\d+)/$', views.dz_details, name='dz_details'), There is a simpler way to do [...]


Related posts:<ol><li><a href='http://pedroassuncao.com/2009/08/django-dynamic-redirection-after-login/' rel='bookmark' title='[Django] Dynamic redirection after login'>[Django] Dynamic redirection after login</a></li>
<li><a href='http://pedroassuncao.com/2009/08/java-http-proxy-servlet-with-spring/' rel='bookmark' title='Java HTTP proxy servlet (with Spring)'>Java HTTP proxy servlet (with Spring)</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Here&#8217;s how to, from a view, redirect to another URL passing parameters to it. For instance, to redirect the user to a certain page after login:</p>
<pre>return HttpResponseRedirect(reverse('dz_details', kwargs={'dz_id':dz.id}))</pre>
<p>This will lookup the &#8216;dz_details&#8217; name in your urls.py file, which could be defined like so:</p>
<pre>url(r'^details/(\d+)/$', views.dz_details, name='dz_details'),</pre>
<p>There is a simpler way to do it, if you don&#8217;t want to use the parameters&#8217; names, but you have to know the order on which they appear in the method:</p>
<pre>return HttpResponseRedirect(reverse('dz_details', args=[ dz.id ]))</pre>
<div class="shr-publisher-376"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F08%2Fdjango-reverse-http-redirect-with-parameters%2F' data-shr_title='Django%3A+Reverse+HTTP+redirect+with+parameters'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F08%2Fdjango-reverse-http-redirect-with-parameters%2F' data-shr_title='Django%3A+Reverse+HTTP+redirect+with+parameters'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->

<p>Related posts:<ol><li><a href='http://pedroassuncao.com/2009/08/django-dynamic-redirection-after-login/' rel='bookmark' title='[Django] Dynamic redirection after login'>[Django] Dynamic redirection after login</a></li>
<li><a href='http://pedroassuncao.com/2009/08/java-http-proxy-servlet-with-spring/' rel='bookmark' title='Java HTTP proxy servlet (with Spring)'>Java HTTP proxy servlet (with Spring)</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://pedroassuncao.com/2009/08/django-reverse-http-redirect-with-parameters/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[Django] Dynamic redirection after login</title>
		<link>http://pedroassuncao.com/2009/08/django-dynamic-redirection-after-login/</link>
		<comments>http://pedroassuncao.com/2009/08/django-dynamic-redirection-after-login/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 17:17:24 +0000</pubDate>
		<dc:creator>Pedro Assunção</dc:creator>
				<category><![CDATA[code snipplets]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://diffract.me/?p=344</guid>
		<description><![CDATA[Here&#8217;s how to redirect to the login page in django, making it go to a certain view (by name) after a successful login: login_url = '%s?next=%s' % (reverse('acct_login'), reverse('jumplog_index')) return HttpResponseRedirect(login_url) Notes: acct_login is django&#8217;s view name for the login page (double check this, as it might change in future django versions); jumplog_index is the [...]


Related posts:<ol><li><a href='http://pedroassuncao.com/2009/08/django-reverse-http-redirect-with-parameters/' rel='bookmark' title='Django: Reverse HTTP redirect with parameters'>Django: Reverse HTTP redirect with parameters</a></li>
<li><a href='http://pedroassuncao.com/2008/06/django-in-appengine/' rel='bookmark' title='Django in AppEngine'>Django in AppEngine</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Here&#8217;s how to redirect to the login page in django, making it go to a certain view (by name) after a successful login:</p>
<pre>login_url = '%s?next=%s' % (reverse('acct_login'), reverse('jumplog_index'))
return HttpResponseRedirect(login_url)</pre>
<p>Notes:</p>
<ul>
<li><strong>acct_login</strong> is django&#8217;s view name for the login page (double check this, as it might change in future django versions);</li>
<li><strong>jumplog_index</strong> is the target view where I want the user to go after successfully logging in.</li>
</ul>
<p>UPDATE: An alternative, much more awesome, is to use the @login_required decorator on the view you want login to be required. Like so:</p>
<pre>@login_required
def index(request):
   # Your code here</pre>
<p>The decorator can be imported from this module:</p>
<pre>from django.contrib.auth.decorators import login_required</pre>
<p>I guess the first dirtier method can be used when you want to do something more custom, like sending some data to the login view, or something like that <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks for the tip, Steve <img src='http://pedroassuncao.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="shr-publisher-344"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F08%2Fdjango-dynamic-redirection-after-login%2F' data-shr_title='%5BDjango%5D+Dynamic+redirection+after+login'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fpedroassuncao.com%2F2009%2F08%2Fdjango-dynamic-redirection-after-login%2F' data-shr_title='%5BDjango%5D+Dynamic+redirection+after+login'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->

<p>Related posts:<ol><li><a href='http://pedroassuncao.com/2009/08/django-reverse-http-redirect-with-parameters/' rel='bookmark' title='Django: Reverse HTTP redirect with parameters'>Django: Reverse HTTP redirect with parameters</a></li>
<li><a href='http://pedroassuncao.com/2008/06/django-in-appengine/' rel='bookmark' title='Django in AppEngine'>Django in AppEngine</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://pedroassuncao.com/2009/08/django-dynamic-redirection-after-login/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

