<?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>mFabrik - mobile sites, apps, HTML5 and CMS software development &#187; parse</title>
	<atom:link href="http://blog.mfabrik.com/tag/parse/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mfabrik.com</link>
	<description>Freedom delivered.</description>
	<lastBuildDate>Wed, 03 Aug 2011 09:47:41 +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>How to split() strings of indefined item count to Python variables elegantly?</title>
		<link>http://blog.mfabrik.com/2010/04/17/how-to-split-strings-of-indefined-item-count-to-python-variables-elegantly/</link>
		<comments>http://blog.mfabrik.com/2010/04/17/how-to-split-strings-of-indefined-item-count-to-python-variables-elegantly/#comments</comments>
		<pubDate>Sat, 17 Apr 2010 10:29:47 +0000</pubDate>
		<dc:creator>Mikko Ohtamaa</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[technology]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[split]]></category>
		<category><![CDATA[string]]></category>

		<guid isPermaLink="false">http://blog.mfabrik.com/?p=504</guid>
		<description><![CDATA[A common problem for  (space) separated string parsing is that there are a number of fixed items followed by some amount of optional items. You want to extract parsed values to Python variables and initialize optional values with None if they are missing. E.g. we have option format description string with 2 or 3 items [...]]]></description>
			<content:encoded><![CDATA[<p>A common problem for  (space) separated string parsing is that there are a number of fixed items followed by some amount of optional items. You want to extract parsed values to Python variables and initialize optional values with None if they are missing.</p>
<p>E.g. we have option format description string with 2 or 3 items <em>value1 value2 [optional value]</em></p>
<pre>s = "foo bar" # This is a valid example option string</pre>
<pre>s2 = "foo bar optional" # This is also, with one optional item</pre>
<p>Usually we&#8217;d like to parse this so that non-existing options are set to None. Traditonally one does Python code like below:</p>
<pre># Try get extration and deletion
parts = s.split(" ")
value1 = parts[0]
value2 = parts[1]
if len(parts) &gt;= 3:
    optional_value = parts[2]
else:
    optionanl_value = None</pre>
<p>However, this looks little bit ugly, considering  if there would always be just two options one could write:</p>
<pre>value1, value2 = s.split(" ")</pre>
<p>When the number of optional options grow, the boiler-plate code starts annoy greatly.</p>
<p>I have been thinking how to write a split() code to parse options with non-existing items to be set None. Below is my approach for a string with total of 4 options of which any number can be optional.</p>
<pre>parts = s.split(" ", 4) # Will raise exception if too many options 
parts += [None] * (4 - len(parts)) # Assume we can have max. 4 items. Fill in missing entries with None.
value1, value2, optional_value, optional_value2 = parts</pre>
<p>Any ideas for something more elegant?
<p class="signature">
<a href="http://mfabrik.com/@@zoho-contact-form"><img valign="middle"  src="http://blog.mfabrik.com/wp-content/uploads/mfabrik-24.png"></a> <a href="http://mfabrik.com/@@zoho-contact-form">Get developers</a> <a href="http://feeds.feedburner.com/mFabrikWebAndMobileDevelopment" rel="alternate" type="application/rss+xml"><img valign="middle" src="http://www.feedburner.com/fb/images/pub/feed-icon16x16.png" alt="" style="vertical-align:middle;border:0"/></a> <a href="http://feeds.feedburner.com/mFabrikWebAndMobileDevelopment" rel="alternate" type="application/rss+xml">Subscribe mFabrik blog in a reader</a> <a href="http://twitter.com/mfabrik"> <img valign="middle"  src="http://blog.mfabrik.com/wp-content/uploads/twitter-24.png"></a> <a href="http://twitter.com/moo9000">Follow me on Twitter</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mfabrik.com/2010/04/17/how-to-split-strings-of-indefined-item-count-to-python-variables-elegantly/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Python-like urlparser module for Javascript</title>
		<link>http://blog.mfabrik.com/2008/11/29/python-like-urlparser-module-for-javascript/</link>
		<comments>http://blog.mfabrik.com/2008/11/29/python-like-urlparser-module-for-javascript/#comments</comments>
		<pubDate>Sat, 29 Nov 2008 01:56:42 +0000</pubDate>
		<dc:creator>Mikko Ohtamaa</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[fragment]]></category>
		<category><![CDATA[hostname]]></category>
		<category><![CDATA[href]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[location]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[url]]></category>
		<category><![CDATA[urlparse]]></category>
		<category><![CDATA[urlparser]]></category>

		<guid isPermaLink="false">http://blog.redinnovation.com/?p=152</guid>
		<description><![CDATA[I had to deconstruct and reconstruct URLs from pieces when doing advanced Javascripting for Plone. I found this nice library from Denis Laprise. However, it had a bug with fragment extractor and lacked reconstruction possibilies. So I decided to make a new version. Download urlparse.js version 0.2. thank you Couple of examples: var p = [...]]]></description>
			<content:encoded><![CDATA[<p>I had to deconstruct and reconstruct URLs from pieces when doing advanced Javascripting for Plone.</p>
<p>I found this <a href="https://code.poly9.com/trac/browser/urlparser/urlparser.js">nice library</a> from <span class="c">Denis Laprise. However, it had a bug with fragment extractor and lacked reconstruction possibilies. So I decided to make a new version.</span></p>
<p><a href="http://snipplr.com/view/10139/urlparse--pythonlike-url-parser-and-manipulator/">Download urlparse.js version 0.2</a>. thank you <img src='http://blog.mfabrik.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Couple of examples:</p>
<pre>var p = new Poly9.URLParser('http://user:password@poly9.com/pathname?arguments=1#fragment');
p.getHost() == 'poly9.com';
p.getProtocol() == 'http';
p.getPathname() == '/pathname';
p.getQuerystring() == 'arguments=1';
p.getFragment() == 'fragment';
p.getUsername() == 'user';
p.getPassword() == 'password';

var p = new Poly9.URLParser("http://localhost:8080/path);

p.setQuerystring("foo=bar");
p.setFragment("anchor");
p.setPort(7070);

var url = p.getURL() // http://localhost:7070/path?foo=bar#anchor</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.mfabrik.com/2008/11/29/python-like-urlparser-module-for-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

